RICPLT-2954 Enhance error and info log writings
[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         "errors"
24         rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
25         rtmgrhandle "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client/handle"
26         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models"
27         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
28         "strconv"
29         "strings"
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(subRouteAction SubRouteInfo) error {
39         xapp.Logger.Debug("SubscriptionRequestUpdate() invoked")
40         subID := int32(subRouteAction.SubID)
41         xapp.Logger.Debug("Subscription action details received. subRouteAction.Command: %v, Address %s, Port %v, subID %v", int16(subRouteAction.Command), subRouteAction.Address, subRouteAction.Port, subID)
42         xappSubReq := rtmgr_models.XappSubscriptionData{&subRouteAction.Address, &subRouteAction.Port, &subID}
43
44         switch subRouteAction.Command {
45         case CREATE:
46                 _, postErr := rc.rtClient.Handle.ProvideXappSubscriptionHandle(rc.xappHandleParams.WithXappSubscriptionData(&xappSubReq))
47                 if postErr != nil && !(strings.Contains(postErr.Error(), "status 200")) {
48                         xapp.Logger.Error("Updating routing manager about subscription id = %d failed with error: %v", subID, postErr)
49                         return postErr
50                 } else {
51                         xapp.Logger.Info("Succesfully updated routing manager about the subscription: %d", subID)
52                         return nil
53                 }
54         case DELETE:
55                 _, _, deleteErr := rc.rtClient.Handle.DeleteXappSubscriptionHandle(rc.xappDeleteParams.WithXappSubscriptionData(&xappSubReq))
56                 if deleteErr != nil && !(strings.Contains(deleteErr.Error(), "status 200")) {
57                         xapp.Logger.Error("Deleting subscription id = %d  in routing manager, failed with error: %v", subID, deleteErr)
58                         return deleteErr
59                 } else {
60                         xapp.Logger.Info("Succesfully deleted subscription: %d in routing manager.", subID)
61                         return nil
62                 }
63         default:
64                 xapp.Logger.Debug("Unknown subRouteAction.Command: %v, Address %s, Port %v, subID: %v", subRouteAction.Command, subRouteAction.Address, subRouteAction.Port, subID)
65                 return nil
66         }
67 }
68
69 func (rc *RtmgrClient) SplitSource(src string) (*string, *uint16, error) {
70         tcpSrc := strings.Split(src, ":")
71         if len(tcpSrc) != 2 {
72                 err := errors.New("unable to get the source details of the xapp - check the source string received from the rmr")
73                 return nil, nil, err
74         }
75         srcAddr := tcpSrc[0]
76         xapp.Logger.Debug("Debugging Inside splitsource tcpsrc[0] = %s and tcpsrc[1]= %s ", tcpSrc[0], tcpSrc[1])
77         srcPort, err := strconv.ParseUint(tcpSrc[1], 10, 16)
78         if err != nil {
79                 return nil, nil, err
80         }
81         srcPortInt := uint16(srcPort)
82         return &srcAddr, &srcPortInt, nil
83 }