Initial integration version of the RIC Subscription Manager

Change-Id: I14763247a45f639931b734fe836b214dd54dcf38
Signed-off-by: kalnagy <kalman.nagy@nokia.com>
diff --git a/pkg/control/control.go b/pkg/control/control.go
new file mode 100644
index 0000000..6c75063
--- /dev/null
+++ b/pkg/control/control.go
@@ -0,0 +1,87 @@
+/*
+==================================================================================
+  Copyright (c) 2019 AT&T Intellectual Property.
+  Copyright (c) 2019 Nokia
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================================
+*/
+
+package control
+
+/*
+#include <rmr/RIC_message_types.h>
+
+#cgo CFLAGS: -I../
+#cgo LDFLAGS: -lrmr_nng -lnng
+*/
+import "C"
+
+
+import (
+  "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+  "errors"
+  "strconv"
+)
+
+type Control struct {
+  e2ap *E2ap
+  registry *Registry
+}
+
+func NewControl() Control {
+  return Control{new(E2ap),new(Registry)}
+}
+
+func (c *Control) Run() {
+  xapp.Run(c)
+}
+
+func (c *Control) Consume(mtype, sub_id int, len int, payload []byte) (err error) {
+  switch mtype {
+  case C.RIC_SUB_REQ:
+    err = c.handleSubscriptionRequest(&RmrDatagram{mtype, sub_id, payload})
+  case C.RIC_SUB_RESP:
+    err = c.handleSubscriptionResponse(&RmrDatagram{mtype, sub_id, payload})
+  default:
+    err = errors.New("Message Type "+strconv.Itoa(mtype)+" discarded")
+  }
+  return
+}
+
+func (c *Control) rmrSend(datagram *RmrDatagram) (err error) {
+  if !xapp.Rmr.Send(datagram.MessageType, datagram.SubscriptionId, len(datagram.Payload), datagram.Payload) {
+    err = errors.New("rmr.Send() failed")
+  }
+  return
+}
+
+func (c *Control) handleSubscriptionRequest(datagram *RmrDatagram) ( err error) {
+  content, err := c.e2ap.GetPayloadContent(datagram.Payload)
+  xapp.Logger.Info("Subscription Request received: %v", content)
+  new_sub_id := c.registry.GetSubscriptionId()
+  payload, err := c.e2ap.SetSubscriptionSequenceNumber(datagram.Payload, new_sub_id)
+  if err != nil {
+    xapp.Logger.Error("Unable to set Subscription Sequence Number in Payload due to: "+ err.Error())
+    return
+  }
+  xapp.Logger.Info("New Subscription Accepted, Forwarding to E2T")
+  c.rmrSend(&RmrDatagram{C.RIC_SUB_REQ , new_sub_id, payload})
+  return
+}
+
+func (c *Control) handleSubscriptionResponse(datagram *RmrDatagram) ( err error) {
+  content, err := c.e2ap.GetPayloadContent(datagram.Payload)
+  xapp.Logger.Info("Subscription Response received: %v", content)
+  return
+}
\ No newline at end of file
diff --git a/pkg/control/e2ap.go b/pkg/control/e2ap.go
new file mode 100644
index 0000000..ff9c778
--- /dev/null
+++ b/pkg/control/e2ap.go
@@ -0,0 +1,84 @@
+/*
+==================================================================================
+  Copyright (c) 2019 AT&T Intellectual Property.
+  Copyright (c) 2019 Nokia
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================================
+*/
+
+package control
+
+import (
+  "encoding/gob"
+  "bytes"
+  "errors"
+)
+
+type E2ap struct {
+}
+
+func (c *E2ap) GetSubscriptionSequenceNumber(payload []byte) (int, error) {
+  asn1 := new(Asn1)
+  message, err := asn1.Decode(payload)
+  if err != nil {
+    return 0, errors.New("Unable to decode payload due to "+ err.Error()) 
+  }
+  return message.SubscriptionId, nil
+}
+
+func (c *E2ap) SetSubscriptionSequenceNumber(payload []byte, newSubscriptionid int) ([]byte ,error) {
+  asn1 := new(Asn1)
+  message, err := asn1.Decode(payload)
+  if err != nil {
+    return make([]byte,0), errors.New("Unable to decode payload due to "+ err.Error()) 
+  }
+  message.SubscriptionId = newSubscriptionid
+  payload, err = asn1.Encode(message)
+  if err != nil {
+    return make([]byte,0), errors.New("Unable to encode message due to "+ err.Error()) 
+  }
+  return payload, nil
+}
+
+
+func (c *E2ap) GetPayloadContent(payload []byte) (content string, err error) {
+  asn1 := new(Asn1)
+  message, err := asn1.Decode(payload)
+  content = message.Content
+  return
+}
+/*
+Serialize and Deserialize message using this until real ASN1 GO wrapper is not in place
+*/
+type Asn1 struct {
+}
+
+func (a *Asn1) Encode(message RmrPayload) ([]byte, error) {
+	buffer := new(bytes.Buffer)
+	asn1 := gob.NewEncoder(buffer)
+	if err := asn1.Encode(message); err != nil {
+		return nil, err
+  }
+	return buffer.Bytes(), nil
+}
+
+func (a *Asn1) Decode(data []byte) (RmrPayload, error) {
+  message := new(RmrPayload)
+  buffer := bytes.NewBuffer(data)
+	asn1 := gob.NewDecoder(buffer)
+	if err := asn1.Decode(message); err != nil {
+		return RmrPayload{}, err
+  }
+	return *message, nil
+}
diff --git a/pkg/control/libe2ap.a b/pkg/control/libe2ap.a
new file mode 100644
index 0000000..529a7ce
--- /dev/null
+++ b/pkg/control/libe2ap.a
Binary files differ
diff --git a/pkg/control/registry.go b/pkg/control/registry.go
new file mode 100644
index 0000000..8f52f97
--- /dev/null
+++ b/pkg/control/registry.go
@@ -0,0 +1,33 @@
+/*
+==================================================================================
+  Copyright (c) 2019 AT&T Intellectual Property.
+  Copyright (c) 2019 Nokia
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================================
+*/
+
+package control
+
+type Registry struct {
+  counter int
+}
+
+func (r *Registry) GetSubscriptionId() int {
+  return r.generateId()
+}
+
+func (r *Registry) generateId() int {
+  r.counter += 1
+  return r.counter
+}
diff --git a/pkg/control/types.go b/pkg/control/types.go
new file mode 100644
index 0000000..5f46e65
--- /dev/null
+++ b/pkg/control/types.go
@@ -0,0 +1,32 @@
+/*
+==================================================================================
+  Copyright (c) 2019 AT&T Intellectual Property.
+  Copyright (c) 2019 Nokia
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================================
+*/
+
+package control
+
+type RmrPayload struct {
+	MessageType int
+	SubscriptionId int
+	Content string 
+}
+
+type RmrDatagram struct {
+  MessageType int
+	SubscriptionId int
+	Payload []byte 
+}