d213c65e1c1179a8261bb4abf43f924e4f9c7cf3
[ric-plt/submgr.git] / pkg / control / control.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 /*
23 #include <rmr/RIC_message_types.h>
24
25 #cgo CFLAGS: -I../
26 #cgo LDFLAGS: -lrmr_nng -lnng
27 */
28 import "C"
29
30
31 import (
32   "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33   "errors"
34   "strconv"
35 )
36
37 type Control struct {
38   e2ap *E2ap
39   registry *Registry
40 }
41
42 func NewControl() Control {
43   return Control{new(E2ap),new(Registry)}
44 }
45
46 func (c *Control) Run() {
47   xapp.Run(c)
48 }
49
50 func (c *Control) Consume(mtype, sub_id int, len int, payload []byte) (err error) {
51   switch mtype {
52   case C.RIC_SUB_REQ:
53     err = c.handleSubscriptionRequest(&RmrDatagram{mtype, sub_id, payload})
54   case C.RIC_SUB_RESP:
55     err = c.handleSubscriptionResponse(&RmrDatagram{mtype, sub_id, payload})
56   default:
57     err = errors.New("Message Type "+strconv.Itoa(mtype)+" discarded")
58   }
59   return
60 }
61
62 func (c *Control) rmrSend(datagram *RmrDatagram) (err error) {
63   if !xapp.Rmr.Send(datagram.MessageType, datagram.SubscriptionId, len(datagram.Payload), datagram.Payload) {
64     err = errors.New("rmr.Send() failed")
65   }
66   return
67 }
68
69 func (c *Control) handleSubscriptionRequest(datagram *RmrDatagram) ( err error) {
70   /* TODO: removed to being able to integrate with UEMGR
71   content, err := c.e2ap.GetPayloadContent(datagram.Payload)
72   */
73   xapp.Logger.Info("Subscription Request Message received with ID: %v", datagram.SubscriptionId)
74   new_sub_id := c.registry.GetSubscriptionId()
75   /* TODO: removed to being able to integrate with UEMGR
76   payload, err := c.e2ap.SetSubscriptionSequenceNumber(datagram.Payload, new_sub_id)
77   if err != nil {
78     xapp.Logger.Error("Unable to set Subscription Sequence Number in Payload due to: "+ err.Error())
79     return
80   }
81   */
82   xapp.Logger.Info("New Subscription Registered, forwarding to E2T")
83   c.rmrSend(&RmrDatagram{C.RIC_SUB_REQ , new_sub_id, datagram.Payload})
84   return
85 }
86
87 func (c *Control) handleSubscriptionResponse(datagram *RmrDatagram) ( err error) {
88   /* TODO: removed to being able to integrate with UEMGR
89   content, err := c.e2ap.GetPayloadContent(datagram.Payload)
90   */
91   xapp.Logger.Info("Subscription Response Message received with ID: %v", datagram.SubscriptionId)
92   xapp.Logger.Info("Subscription Response Registered, forwarding to Requestor")
93   c.rmrSend(&RmrDatagram{C.RIC_SUB_RESP , datagram.SubscriptionId, datagram.Payload})
94   return
95 }