Routing manager needs to support for xApps that terminate other protocols than E2

Change-Id: I97e3eebecca617ab94ed928abcdb1e986c4d361d
Signed-off-by: wahidw <abdulwahid.w@nokia.com>
diff --git a/RELNOTES b/RELNOTES
index d2a370b..019508c 100644
--- a/RELNOTES
+++ b/RELNOTES
@@ -1,3 +1,6 @@
+### v0.6.3
+* [RIC-369] Routing manager needs to support for xApps that terminate other protocols than E2 
+
 ### v0.6.2
 * Distribution of routes is synchronous. 
 
diff --git a/api/routing_manager.yaml b/api/routing_manager.yaml
index bdf19f1..fc93ab5 100644
--- a/api/routing_manager.yaml
+++ b/api/routing_manager.yaml
@@ -295,6 +295,53 @@
           description: "Invalid data"
         201:
           description: "ran instances disociated"
+  /handles/addrmrroute:
+    post:
+      tags:
+      - "handle"
+      summary: "API to add RMR routes"
+      description: "By performing a POST method to add RMR routes"
+      operationId: "add_rmr_route"
+      consumes:
+      - "application/json"
+      produces:
+      - "application/json"
+      parameters:
+        - in: body
+          name: routes-list
+          description: "list of routes to add"
+          required: true
+          schema:
+           $ref: "#/definitions/routelist"
+      responses:
+        400:
+          description: "Invalid data"
+        201:
+          description: "rmr routes added"     
+          
+  /handles/delrmrroute:
+    delete:
+      tags:
+      - "handle"
+      summary: "API to delete RMR routes"
+      description: "By performing a DELETE method to add RMR routes"
+      operationId: "del_rmr_route"
+      consumes:
+      - "application/json"
+      produces:
+      - "application/json"
+      parameters:
+        - in: body
+          name: routes-list
+          description: "list of routes to delete"
+          required: true
+          schema:
+           $ref: "#/definitions/routelist"
+      responses:
+        400:
+          description: "Invalid data"
+        201:
+          description: "rmr routes added" 
 definitions:
   health-status:
     type: "object"
@@ -412,6 +459,28 @@
       RouteConfigs:
         type: "string"
 
+  add-rmr-route:
+    type: "object"
+    required:
+      - "TargetEndPoint"
+      - "MessageType"
+    properties:
+      TargetEndPoint:
+        type: "string"
+      MessageType:
+        type: "integer"
+        format: "uint32"      
+      SenderEndPoint:
+        type: "string"
+      SubscriptionID:
+        type: "integer"
+        format: "int32"      
+  
+  routelist:
+    type: "array"
+    items:
+      $ref: '#/definitions/add-rmr-route'
+        
 externalDocs:
   description: "Routing Manager"
   url: "http://placeholder"
diff --git a/container-tag.yaml b/container-tag.yaml
index bff5250..fb1cfa5 100644
--- a/container-tag.yaml
+++ b/container-tag.yaml
@@ -2,4 +2,4 @@
 # By default this file is in the docker build directory,
 # but the location can configured in the JJB template.
 ---
-tag: 0.6.2
+tag: 0.6.3
diff --git a/pkg/nbi/httprestful.go b/pkg/nbi/httprestful.go
index f49f3ae..e10f42b 100644
--- a/pkg/nbi/httprestful.go
+++ b/pkg/nbi/httprestful.go
@@ -491,6 +491,26 @@
 				return debug.NewGetDebuginfoOK().WithPayload(&response)
 			}
 		})
+        api.HandleAddRmrRouteHandler = handle.AddRmrRouteHandlerFunc(
+                func(params handle.AddRmrRouteParams) middleware.Responder {
+			err := adddelrmrroute(params.RoutesList,true)
+			if err != nil {
+				return handle.NewAddRmrRouteBadRequest()
+			} else {
+				return handle.NewAddRmrRouteCreated()
+			}
+
+                })
+        api.HandleDelRmrRouteHandler = handle.DelRmrRouteHandlerFunc(
+                func(params handle.DelRmrRouteParams) middleware.Responder {
+			err := adddelrmrroute(params.RoutesList,false)
+			if err != nil {
+				return handle.NewDelRmrRouteBadRequest()
+			} else {
+				return handle.NewDelRmrRouteCreated()
+			}
+                })
+
 	// start to serve API
 	xapp.Logger.Info("Starting the HTTP Rest service")
 	if err := server.Serve(); err != nil {
@@ -655,7 +675,26 @@
 
 	// post subscription req to appmgr
 	readErr = PostSubReq(xmurl, nbiif)
+	if readErr != nil {
+		return readErr
+	}
+
+	//rlist := make(map[string]string)
+	xapp.Logger.Info("Reading SDL for any routes")
+	rlist,sdlerr := xapp.Sdl.Read("routes")
+	readErr = sdlerr
 	if readErr == nil {
+		xapp.Logger.Info("Value is %s",rlist["routes"])
+		if rlist["routes"] != nil {
+			formstring := fmt.Sprintf("%s",rlist["routes"])
+			xapp.Logger.Info("Value of formed string = %s",formstring)
+			newstring := strings.Split(formstring," ")
+			for i,_ := range newstring {
+				xapp.Logger.Info("in Loop Value of formed string = %s",newstring)
+				rtmgr.DynamicRouteList = append(rtmgr.DynamicRouteList,newstring[i])
+			}
+		}
+
 		return nil
 	}
 
@@ -769,3 +808,56 @@
 		}
 	}
 }
+
+func adddelrmrroute(routelist models.Routelist,rtflag bool) error {
+	xapp.Logger.Info("Updating rmrroute with Route list: %v,flag: %v", routelist,rtflag)
+	for _, rlist := range routelist {
+		var  subid int32
+		var data string
+		if rlist.SubscriptionID == 0 {
+			subid = -1
+		} else {
+			subid = rlist.SubscriptionID
+		}
+		if rlist.SenderEndPoint == "" && rlist.SubscriptionID != 0 {
+			data = fmt.Sprintf("mse|%d|%d|%s\n",*rlist.MessageType,rlist.SubscriptionID,*rlist.TargetEndPoint)
+		} else if rlist.SenderEndPoint == "" && rlist.SubscriptionID == 0 {
+			data = fmt.Sprintf("mse|%d|-1|%s\n",*rlist.MessageType,*rlist.TargetEndPoint)
+		} else {
+			data = fmt.Sprintf("mse|%d,%s|%d|%s\n",*rlist.MessageType,rlist.SenderEndPoint,subid,*rlist.TargetEndPoint)
+		}
+		err := checkrepeatedroute(data)
+
+		if rtflag == true {
+			if err == true {
+				xapp.Logger.Info("Given route %s is a duplicate",data)
+			}
+			rtmgr.DynamicRouteList = append(rtmgr.DynamicRouteList,data)
+			routearray := strings.Join(rtmgr.DynamicRouteList," ")
+			xapp.Sdl.Store("routes",routearray)
+		} else {
+			if err == true {
+				xapp.Logger.Info("Successfully deleted route: %s",data)
+				routearray := strings.Join(rtmgr.DynamicRouteList," ")
+				xapp.Sdl.Store("routes",routearray)
+			}else {
+				xapp.Logger.Info("No such route: %s",data)
+				return errors.New("No such route: " + data)
+			}
+
+		}
+	}
+	return sendRoutesToAll()
+}
+
+func checkrepeatedroute (data string) bool {
+	for i:=0;i<len(rtmgr.DynamicRouteList);i++ {
+		if rtmgr.DynamicRouteList[i] == data {
+			rtmgr.DynamicRouteList[i] = rtmgr.DynamicRouteList[len(rtmgr.DynamicRouteList)-1]
+			rtmgr.DynamicRouteList[len(rtmgr.DynamicRouteList)-1] = ""
+			rtmgr.DynamicRouteList = rtmgr.DynamicRouteList[:len(rtmgr.DynamicRouteList)-1]
+			return true
+		}
+	}
+	return false
+}
diff --git a/pkg/rpe/rmr.go b/pkg/rpe/rmr.go
index 346644c..446d7ec 100644
--- a/pkg/rpe/rmr.go
+++ b/pkg/rpe/rmr.go
@@ -86,6 +86,10 @@
 
 		rawrt = append(rawrt, rawrte+"\n")
 	}
+	for _,val := range rtmgr.DynamicRouteList {
+		rawrt = append(rawrt,val)
+	}
+
 	rawrt = append(rawrt, key+"newrt|end\n")
         count := 0
 
diff --git a/pkg/rtmgr/rtmgr.go b/pkg/rtmgr/rtmgr.go
index b636e12..e5e0dcb 100644
--- a/pkg/rtmgr/rtmgr.go
+++ b/pkg/rtmgr/rtmgr.go
@@ -124,6 +124,7 @@
 	Subs SubscriptionList
 	PrsCfg  *PlatformRoutes
 	Mtype MessageTypeList
+	DynamicRouteList []string
 )
 
 func GetPlatformComponents(configfile string) (*PlatformComponents, error) {