598c7efb0a8f49ef3fc6ff92ea88a622bb58eef9
[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 }
36
37 func (rc *RtmgrClient) SubscriptionRequestUpdate() error {
38         xapp.Logger.Debug("SubscriptionRequestUpdate() invoked")
39         subRouteAction := <-SubscriptionReqChan
40         // Routing manager handles subscription id as int32 to accomodate -1 and uint16 values
41         subID := int32(subRouteAction.SubID)
42
43         xapp.Logger.Debug("Subscription action details received: ", subRouteAction)
44
45         xappSubReq := rtmgr_models.XappSubscriptionData{&subRouteAction.Address, &subRouteAction.Port, &subID}
46
47         switch subRouteAction.Command {
48         case CREATE:
49                 _, postErr := rc.rtClient.Handle.ProvideXappSubscriptionHandle(rc.xappHandleParams.WithXappSubscriptionData(&xappSubReq))
50                 if postErr != nil && !(strings.Contains(postErr.Error(), "status 200"))  {
51                         xapp.Logger.Error("Updating routing manager about subscription id = %d failed with error: %v", subID, postErr)
52                         return postErr
53                 } else {
54                         xapp.Logger.Info("Succesfully updated routing manager about the subscription: %d", subID)
55                         return nil
56                 }
57         default:
58                 return nil
59         }
60 }
61
62 func (rc *RtmgrClient) SplitSource(src string) (*string, *uint16, error) {
63         tcpSrc := strings.Split(src, ":")
64         if len(tcpSrc) != 2 {
65                 err := errors.New("Unable to get the source details of the xapp. Check the source string received from the rmr.")
66                 return nil, nil, err
67         }
68         srcAddr := tcpSrc[0]
69         xapp.Logger.Info("---Debugging Inside splitsource tcpsrc[0] = %s and tcpsrc[1]= %s ", tcpSrc[0], tcpSrc[1])
70         srcPort, err := strconv.ParseUint(tcpSrc[1], 10, 16)
71         if err != nil {
72                 return nil, nil, err
73         }
74         srcPortInt := uint16(srcPort)
75         return &srcAddr, &srcPortInt, nil
76 }