Initial integration version of the RIC Subscription Manager
[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   content, err := c.e2ap.GetPayloadContent(datagram.Payload)
71   xapp.Logger.Info("Subscription Request received: %v", content)
72   new_sub_id := c.registry.GetSubscriptionId()
73   payload, err := c.e2ap.SetSubscriptionSequenceNumber(datagram.Payload, new_sub_id)
74   if err != nil {
75     xapp.Logger.Error("Unable to set Subscription Sequence Number in Payload due to: "+ err.Error())
76     return
77   }
78   xapp.Logger.Info("New Subscription Accepted, Forwarding to E2T")
79   c.rmrSend(&RmrDatagram{C.RIC_SUB_REQ , new_sub_id, payload})
80   return
81 }
82
83 func (c *Control) handleSubscriptionResponse(datagram *RmrDatagram) ( err error) {
84   content, err := c.e2ap.GetPayloadContent(datagram.Payload)
85   xapp.Logger.Info("Subscription Response received: %v", content)
86   return
87 }