Metrics for submgr
[ric-plt/submgr.git] / pkg / control / client.go
index 598c7ef..11367ed 100644 (file)
 package control
 
 import (
+       "fmt"
        rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
        rtmgrhandle "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client/handle"
        "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models"
-       "strings"
-       "strconv"
-       "errors"
        "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+       "strconv"
+       "strings"
+       "time"
 )
 
-type RtmgrClient struct {
-       rtClient         *rtmgrclient.RoutingManager
-       xappHandleParams *rtmgrhandle.ProvideXappSubscriptionHandleParams
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type SubRouteInfo struct {
+       EpList xapp.RmrEndpointList
+       SubID  uint16
 }
 
-func (rc *RtmgrClient) SubscriptionRequestUpdate() error {
-       xapp.Logger.Debug("SubscriptionRequestUpdate() invoked")
-       subRouteAction := <-SubscriptionReqChan
-       // Routing manager handles subscription id as int32 to accomodate -1 and uint16 values
-       subID := int32(subRouteAction.SubID)
-
-       xapp.Logger.Debug("Subscription action details received: ", subRouteAction)
+func (sri *SubRouteInfo) String() string {
+       return "routeinfo(" + strconv.FormatUint(uint64(sri.SubID), 10) + "/[" + sri.EpList.String() + "])"
+}
 
-       xappSubReq := rtmgr_models.XappSubscriptionData{&subRouteAction.Address, &subRouteAction.Port, &subID}
+//-----------------------------------------------------------------------------
+//
+//-----------------------------------------------------------------------------
+type RtmgrClient struct {
+       rtClient *rtmgrclient.RoutingManager
+}
 
-       switch subRouteAction.Command {
-       case CREATE:
-               _, postErr := rc.rtClient.Handle.ProvideXappSubscriptionHandle(rc.xappHandleParams.WithXappSubscriptionData(&xappSubReq))
-               if postErr != nil && !(strings.Contains(postErr.Error(), "status 200"))  {
-                       xapp.Logger.Error("Updating routing manager about subscription id = %d failed with error: %v", subID, postErr)
-                       return postErr
-               } else {
-                       xapp.Logger.Info("Succesfully updated routing manager about the subscription: %d", subID)
-                       return nil
-               }
-       default:
-               return nil
+func (rc *RtmgrClient) SubscriptionRequestCreate(subRouteAction SubRouteInfo) error {
+       subID := int32(subRouteAction.SubID)
+       xapp.Logger.Debug("CREATE %s ongoing", subRouteAction.String())
+       createData := rtmgr_models.XappSubscriptionData{&subRouteAction.EpList.Endpoints[0].Addr, &subRouteAction.EpList.Endpoints[0].Port, &subID}
+       createHandle := rtmgrhandle.NewProvideXappSubscriptionHandleParamsWithTimeout(2 * time.Second)
+       createHandle.WithXappSubscriptionData(&createData)
+       _, err := rc.rtClient.Handle.ProvideXappSubscriptionHandle(createHandle)
+       if err != nil && !(strings.Contains(err.Error(), "status 200")) {
+               return fmt.Errorf("CREATE %s failed with error: %s", subRouteAction.String(), err.Error())
        }
+       xapp.Logger.Debug("CREATE %s successful", subRouteAction.String())
+       return nil
 }
 
-func (rc *RtmgrClient) SplitSource(src string) (*string, *uint16, error) {
-       tcpSrc := strings.Split(src, ":")
-       if len(tcpSrc) != 2 {
-               err := errors.New("Unable to get the source details of the xapp. Check the source string received from the rmr.")
-               return nil, nil, err
+func (rc *RtmgrClient) SubscriptionRequestUpdate(subRouteAction SubRouteInfo) error {
+       xapp.Logger.Debug("UPDATE %s ongoing", subRouteAction.String())
+       var updateData rtmgr_models.XappList
+       for i := range subRouteAction.EpList.Endpoints {
+               updateData = append(updateData, &rtmgr_models.XappElement{Address: &subRouteAction.EpList.Endpoints[i].Addr, Port: &subRouteAction.EpList.Endpoints[i].Port})
+       }
+       updateHandle := rtmgrhandle.NewUpdateXappSubscriptionHandleParamsWithTimeout(2 * time.Second)
+       updateHandle.WithSubscriptionID(subRouteAction.SubID)
+       updateHandle.WithXappList(updateData)
+       _, err := rc.rtClient.Handle.UpdateXappSubscriptionHandle(updateHandle)
+       if err != nil && !(strings.Contains(err.Error(), "status 200")) {
+               return fmt.Errorf("UPDATE %s failed with error: %s", subRouteAction.String(), err.Error())
        }
-       srcAddr := tcpSrc[0]
-       xapp.Logger.Info("---Debugging Inside splitsource tcpsrc[0] = %s and tcpsrc[1]= %s ", tcpSrc[0], tcpSrc[1])
-       srcPort, err := strconv.ParseUint(tcpSrc[1], 10, 16)
-       if err != nil {
-               return nil, nil, err
+       xapp.Logger.Debug("UPDATE %s successful", subRouteAction.String())
+       return nil
+
+}
+
+func (rc *RtmgrClient) SubscriptionRequestDelete(subRouteAction SubRouteInfo) error {
+       subID := int32(subRouteAction.SubID)
+       xapp.Logger.Debug("DELETE %s ongoing", subRouteAction.String())
+       deleteData := rtmgr_models.XappSubscriptionData{&subRouteAction.EpList.Endpoints[0].Addr, &subRouteAction.EpList.Endpoints[0].Port, &subID}
+       deleteHandle := rtmgrhandle.NewDeleteXappSubscriptionHandleParamsWithTimeout(2 * time.Second)
+       deleteHandle.WithXappSubscriptionData(&deleteData)
+       _, _, err := rc.rtClient.Handle.DeleteXappSubscriptionHandle(deleteHandle)
+       if err != nil && !(strings.Contains(err.Error(), "status 200")) {
+               return fmt.Errorf("DELETE %s failed with error: %s", subRouteAction.String(), err.Error())
        }
-       srcPortInt := uint16(srcPort)
-       return &srcAddr, &srcPortInt, nil
+       xapp.Logger.Debug("DELETE %s successful", subRouteAction.String())
+       return nil
 }