blob: b62044e60f18cd87333c279a760a061780f4395c [file] [log] [blame]
kalnagy93cc3e22019-09-19 11:29:29 +02001/*
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
20package control
21
22import (
Juha Hyttinen31797b42020-01-16 14:05:01 +020023 "fmt"
archaggeafbf95f2021-04-14 08:54:05 +030024 "strconv"
25 "strings"
26 "time"
27
kalnagy93cc3e22019-09-19 11:29:29 +020028 rtmgrclient "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client"
29 rtmgrhandle "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_client/handle"
30 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models"
kalnagy93cc3e22019-09-19 11:29:29 +020031 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
32)
33
Juha Hyttinen31797b42020-01-16 14:05:01 +020034//-----------------------------------------------------------------------------
35//
36//-----------------------------------------------------------------------------
37type SubRouteInfo struct {
Juha Hyttinen9dc5adc2020-08-13 10:02:40 +030038 EpList xapp.RmrEndpointList
Juha Hyttinen83ada002020-01-30 10:36:33 +020039 SubID uint16
Juha Hyttinen31797b42020-01-16 14:05:01 +020040}
41
42func (sri *SubRouteInfo) String() string {
Juha Hyttinen83ada002020-01-30 10:36:33 +020043 return "routeinfo(" + strconv.FormatUint(uint64(sri.SubID), 10) + "/[" + sri.EpList.String() + "])"
Juha Hyttinen31797b42020-01-16 14:05:01 +020044}
45
46//-----------------------------------------------------------------------------
47//
48//-----------------------------------------------------------------------------
kalnagy93cc3e22019-09-19 11:29:29 +020049type RtmgrClient struct {
Juha Hyttinen12d31af2020-01-22 12:59:01 +020050 rtClient *rtmgrclient.RoutingManager
kalnagy93cc3e22019-09-19 11:29:29 +020051}
52
Juha Hyttinen83ada002020-01-30 10:36:33 +020053func (rc *RtmgrClient) SubscriptionRequestCreate(subRouteAction SubRouteInfo) error {
kalnagy93cc3e22019-09-19 11:29:29 +020054 subID := int32(subRouteAction.SubID)
Juha Hyttinen83ada002020-01-30 10:36:33 +020055 xapp.Logger.Debug("CREATE %s ongoing", subRouteAction.String())
56 createData := rtmgr_models.XappSubscriptionData{&subRouteAction.EpList.Endpoints[0].Addr, &subRouteAction.EpList.Endpoints[0].Port, &subID}
Anssi Mannila4abf1802021-01-28 13:06:46 +020057 createHandle := rtmgrhandle.NewProvideXappSubscriptionHandleParamsWithTimeout(2 * time.Second)
Juha Hyttinen83ada002020-01-30 10:36:33 +020058 createHandle.WithXappSubscriptionData(&createData)
59 _, err := rc.rtClient.Handle.ProvideXappSubscriptionHandle(createHandle)
Juha Hyttinen31797b42020-01-16 14:05:01 +020060 if err != nil && !(strings.Contains(err.Error(), "status 200")) {
Juha Hyttinen83ada002020-01-30 10:36:33 +020061 return fmt.Errorf("CREATE %s failed with error: %s", subRouteAction.String(), err.Error())
kalnagy93cc3e22019-09-19 11:29:29 +020062 }
Juha Hyttinen83ada002020-01-30 10:36:33 +020063 xapp.Logger.Debug("CREATE %s successful", subRouteAction.String())
64 return nil
65}
66
67func (rc *RtmgrClient) SubscriptionRequestUpdate(subRouteAction SubRouteInfo) error {
68 xapp.Logger.Debug("UPDATE %s ongoing", subRouteAction.String())
69 var updateData rtmgr_models.XappList
70 for i := range subRouteAction.EpList.Endpoints {
71 updateData = append(updateData, &rtmgr_models.XappElement{Address: &subRouteAction.EpList.Endpoints[i].Addr, Port: &subRouteAction.EpList.Endpoints[i].Port})
72 }
Anssi Mannila4abf1802021-01-28 13:06:46 +020073 updateHandle := rtmgrhandle.NewUpdateXappSubscriptionHandleParamsWithTimeout(2 * time.Second)
Juha Hyttinen83ada002020-01-30 10:36:33 +020074 updateHandle.WithSubscriptionID(subRouteAction.SubID)
75 updateHandle.WithXappList(updateData)
76 _, err := rc.rtClient.Handle.UpdateXappSubscriptionHandle(updateHandle)
77 if err != nil && !(strings.Contains(err.Error(), "status 200")) {
78 return fmt.Errorf("UPDATE %s failed with error: %s", subRouteAction.String(), err.Error())
79 }
80 xapp.Logger.Debug("UPDATE %s successful", subRouteAction.String())
Juha Hyttinen31797b42020-01-16 14:05:01 +020081 return nil
82
kalnagy93cc3e22019-09-19 11:29:29 +020083}
Juha Hyttinen83ada002020-01-30 10:36:33 +020084
85func (rc *RtmgrClient) SubscriptionRequestDelete(subRouteAction SubRouteInfo) error {
86 subID := int32(subRouteAction.SubID)
87 xapp.Logger.Debug("DELETE %s ongoing", subRouteAction.String())
88 deleteData := rtmgr_models.XappSubscriptionData{&subRouteAction.EpList.Endpoints[0].Addr, &subRouteAction.EpList.Endpoints[0].Port, &subID}
Anssi Mannila4abf1802021-01-28 13:06:46 +020089 deleteHandle := rtmgrhandle.NewDeleteXappSubscriptionHandleParamsWithTimeout(2 * time.Second)
Juha Hyttinen83ada002020-01-30 10:36:33 +020090 deleteHandle.WithXappSubscriptionData(&deleteData)
91 _, _, err := rc.rtClient.Handle.DeleteXappSubscriptionHandle(deleteHandle)
92 if err != nil && !(strings.Contains(err.Error(), "status 200")) {
93 return fmt.Errorf("DELETE %s failed with error: %s", subRouteAction.String(), err.Error())
94 }
95 xapp.Logger.Debug("DELETE %s successful", subRouteAction.String())
96 return nil
97}