b067f3bfeb17088f4012593821db2ff8a560ff58
[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         "fmt"
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 //-----------------------------------------------------------------------------
33 //
34 //-----------------------------------------------------------------------------
35 type SubRouteInfo struct {
36         Command Action
37         EpList  RmrEndpointList
38         SubID   uint16
39 }
40
41 func (sri *SubRouteInfo) String() string {
42         return "routeinfo(" + sri.Command.String() + "/" + strconv.FormatUint(uint64(sri.SubID), 10) + "/[" + sri.EpList.String() + "])"
43 }
44
45 //-----------------------------------------------------------------------------
46 //
47 //-----------------------------------------------------------------------------
48 type RtmgrClient struct {
49         rtClient         *rtmgrclient.RoutingManager
50         xappHandleParams *rtmgrhandle.ProvideXappSubscriptionHandleParams
51         xappDeleteParams *rtmgrhandle.DeleteXappSubscriptionHandleParams
52 }
53
54 func (rc *RtmgrClient) SubscriptionRequestUpdate(subRouteAction SubRouteInfo) error {
55         subID := int32(subRouteAction.SubID)
56         xapp.Logger.Debug("%s ongoing", subRouteAction.String())
57         xappSubReq := rtmgr_models.XappSubscriptionData{&subRouteAction.EpList.Endpoints[0].Addr, &subRouteAction.EpList.Endpoints[0].Port, &subID}
58         var err error
59         switch subRouteAction.Command {
60         case CREATE:
61                 _, err = rc.rtClient.Handle.ProvideXappSubscriptionHandle(rc.xappHandleParams.WithXappSubscriptionData(&xappSubReq))
62         case DELETE:
63                 _, _, err = rc.rtClient.Handle.DeleteXappSubscriptionHandle(rc.xappDeleteParams.WithXappSubscriptionData(&xappSubReq))
64         default:
65                 return fmt.Errorf("%s unknown", subRouteAction.String())
66         }
67
68         if err != nil && !(strings.Contains(err.Error(), "status 200")) {
69                 return fmt.Errorf("%s failed with error: %s", subRouteAction.String(), err.Error())
70         }
71         xapp.Logger.Debug("%s successful", subRouteAction.String())
72         return nil
73
74 }