Subscription manager v0.10.0
[ric-plt/submgr.git] / pkg / control / client.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package control
21
22 import (
23         rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
24         rtmgrhandle "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client/handle"
25         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models"
26         "strings"
27         "strconv"
28         "errors"
29         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
30 )
31
32 type RtmgrClient struct {
33         rtClient         *rtmgrclient.RoutingManager
34         xappHandleParams *rtmgrhandle.ProvideXappSubscriptionHandleParams
35         xappDeleteParams *rtmgrhandle.DeleteXappSubscriptionHandleParams
36 }
37
38 func (rc *RtmgrClient) SubscriptionRequestUpdate() error {
39         xapp.Logger.Debug("SubscriptionRequestUpdate() invoked")
40         subRouteAction := <-SubscriptionReqChan
41         // Routing manager handles subscription id as int32 to accomodate -1 and uint16 values
42         subID := int32(subRouteAction.SubID)
43
44         xapp.Logger.Debug("Subscription action details received: ", subRouteAction)
45
46         xappSubReq := rtmgr_models.XappSubscriptionData{&subRouteAction.Address, &subRouteAction.Port, &subID}
47
48         switch subRouteAction.Command {
49         case CREATE:
50                 _, postErr := rc.rtClient.Handle.ProvideXappSubscriptionHandle(rc.xappHandleParams.WithXappSubscriptionData(&xappSubReq))
51                 if postErr != nil && !(strings.Contains(postErr.Error(), "status 200"))  {
52                         xapp.Logger.Error("Updating routing manager about subscription id = %d failed with error: %v", subID, postErr)
53                         return postErr
54                 } else {
55                         xapp.Logger.Info("Succesfully updated routing manager about the subscription: %d", subID)
56                         return nil
57                 }
58         case DELETE:
59                 _, _, deleteErr := rc.rtClient.Handle.DeleteXappSubscriptionHandle(rc.xappDeleteParams.WithXappSubscriptionData(&xappSubReq))
60                 if deleteErr != nil && !(strings.Contains(deleteErr.Error(), "status 200"))  {
61                         xapp.Logger.Error("Deleting subscription id = %d  in routing manager, failed with error: %v", subID, deleteErr)
62                         return deleteErr
63                 } else {
64                         xapp.Logger.Info("Succesfully deleted subscription: %d in routing manager.", subID)
65                         return nil
66                 }
67         default:
68                 return nil
69         }
70 }
71
72 func (rc *RtmgrClient) SplitSource(src string) (*string, *uint16, error) {
73         tcpSrc := strings.Split(src, ":")
74         if len(tcpSrc) != 2 {
75                 err := errors.New("Unable to get the source details of the xapp. Check the source string received from the rmr.")
76                 return nil, nil, err
77         }
78         srcAddr := tcpSrc[0]
79         xapp.Logger.Info("---Debugging Inside splitsource tcpsrc[0] = %s and tcpsrc[1]= %s ", tcpSrc[0], tcpSrc[1])
80         srcPort, err := strconv.ParseUint(tcpSrc[1], 10, 16)
81         if err != nil {
82                 return nil, nil, err
83         }
84         srcPortInt := uint16(srcPort)
85         return &srcAddr, &srcPortInt, nil
86 }