kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 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 ( |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 23 | "errors" |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 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" |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 27 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 28 | "strconv" |
| 29 | "strings" |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | type RtmgrClient struct { |
| 33 | rtClient *rtmgrclient.RoutingManager |
| 34 | xappHandleParams *rtmgrhandle.ProvideXappSubscriptionHandleParams |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 35 | xappDeleteParams *rtmgrhandle.DeleteXappSubscriptionHandleParams |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 38 | func (rc *RtmgrClient) SubscriptionRequestUpdate(subRouteAction SubRouteInfo) error { |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 39 | xapp.Logger.Debug("SubscriptionRequestUpdate() invoked") |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 40 | subID := int32(subRouteAction.SubID) |
Anssi Mannila | 2e99e2f | 2019-12-05 13:57:06 +0200 | [diff] [blame] | 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) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 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)) |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 47 | if postErr != nil && !(strings.Contains(postErr.Error(), "status 200")) { |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 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 | } |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 54 | case DELETE: |
| 55 | _, _, deleteErr := rc.rtClient.Handle.DeleteXappSubscriptionHandle(rc.xappDeleteParams.WithXappSubscriptionData(&xappSubReq)) |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 56 | if deleteErr != nil && !(strings.Contains(deleteErr.Error(), "status 200")) { |
kalnagy | e001868 | 2019-09-26 16:28:25 +0200 | [diff] [blame] | 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 | } |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 63 | default: |
Anssi Mannila | 0a9f260 | 2019-12-09 15:26:44 +0200 | [diff] [blame] | 64 | xapp.Logger.Debug("Unknown subRouteAction.Command: %v, Address %s, Port %v, subID: %v", subRouteAction.Command, subRouteAction.Address, subRouteAction.Port, subID) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 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 { |
kalnagy | 1455c85 | 2019-10-21 13:06:23 +0200 | [diff] [blame] | 72 | err := errors.New("unable to get the source details of the xapp - check the source string received from the rmr") |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 73 | return nil, nil, err |
| 74 | } |
| 75 | srcAddr := tcpSrc[0] |
Anssi Mannila | 26c922a | 2019-12-17 11:18:08 +0200 | [diff] [blame^] | 76 | xapp.Logger.Debug("Debugging Inside splitsource tcpsrc[0] = %s and tcpsrc[1]= %s ", tcpSrc[0], tcpSrc[1]) |
kalnagy | 93cc3e2 | 2019-09-19 11:29:29 +0200 | [diff] [blame] | 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 | } |