Add version 0.5.0
[ric-plt/submgr.git] / test / e2t / e2t.go
index 2e1f4ac..631f636 100644 (file)
 package main
 
 import (
-       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
-       /* TODO: removed to being able to integrate with UEMGR
-       submgr "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/control"
-       */
+       "encoding/hex"
        "errors"
+       submgr "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/control"
+       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
+       "github.com/spf13/viper"
+       "time"
 )
 
+
 type E2t struct {
+       submgr.E2ap
+}
+
+var c chan submgr.RmrDatagram = make(chan submgr.RmrDatagram, 1)
+
+var RAWDATA string
+
+func init() {
+       viper.AutomaticEnv()
+       viper.SetEnvPrefix("e2t")
+       viper.AllowEmptyEnv(true)
+       RAWDATA = viper.GetString("rawdata")
+       if RAWDATA == "" {
+               RAWDATA = "000001ea7e000500aaaabbbb"
+       }
+       xapp.Logger.Info("Initial RAW Data: %v", RAWDATA)
 }
 
-func (e E2t ) Consume(mtype, sub_id int, len int, payload []byte) (err error) {
-       /* TODO: removed to being able to integrate with UEMGR
-       asn1 := submgr.Asn1{}
-       message, err := asn1.Decode(payload)
+func (e *E2t) GeneratePayload(sub_id uint16) (payload []byte, err error) {
+       skeleton, err := hex.DecodeString(RAWDATA)
        if err != nil {
-               xapp.Logger.Debug("E2T asn1Decoding failure due to "+ err.Error())
+               return make([]byte, 0), errors.New("Unable to decode data provided in RCO_RAWDATA environment variable")
+       }
+       payload, err = e.SetSubscriptionResponseSequenceNumber(skeleton, sub_id)
+       return
+}
+
+func (e E2t) Consume(mtype, sub_id int, len int, payload []byte) (err error) {
+       payload_seq_num, err := e.GetSubscriptionRequestSequenceNumber(payload)
+       if err != nil {
+               xapp.Logger.Error("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())     
+       }       
+       xapp.Logger.Info("Message Received: RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v", sub_id, payload_seq_num)
+       err = e.sendSubscriptionResponse(uint16(sub_id))
+       return
+}
+
+func (e *E2t) sendSubscriptionResponse(sub_id uint16) (err error) {
+       payload, err := e.GeneratePayload(sub_id)
+       if err != nil {
+               xapp.Logger.Debug(err.Error())
                return
        }
-       */
-       xapp.Logger.Info("E2T Received Message with RMR Subsriprion ID: %v, Responding...", sub_id)
-       err = e.subscriptionResponse(sub_id, payload)
+       c  <- submgr.RmrDatagram{12011, sub_id, payload}
        return
 }
 
-func (e E2t ) subscriptionResponse(sub_id int, payload []byte) (err error) {
-       /* TODO: removed to being able to integrate with UEMGR
-       asn1 := submgr.Asn1{}
-       payload, err := asn1.Encode(submgr.RmrPayload{8, sub_id, "E2T: RCO Subscribed"})
+func (e *E2t) Run() {
+       for {
+               message := <-c
+               payload_seq_num, err := e.GetSubscriptionResponseSequenceNumber(message.Payload)
+               if err != nil {
+                       xapp.Logger.Debug("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())     
+               }
+               xapp.Logger.Info("Sending Message: TYPE: %v | RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v)", message.MessageType, message.SubscriptionId, payload_seq_num)
+               xapp.Rmr.Send(message.MessageType, int(message.SubscriptionId), len(message.Payload), message.Payload)
+       }
+}
+
+func (e *E2t) sendInvalidTestMessages() {
+       payload, err := e.GeneratePayload(0)
        if err != nil {
                return
        }
-       */
-       if !xapp.Rmr.Send(12011, sub_id, len(payload), payload) {
-               err = errors.New("rmr.Send() failed")   
+       for {
+               time.Sleep(7 * time.Second)
+               c <- submgr.RmrDatagram{12011, 0, payload}
+               time.Sleep(7 * time.Second)
+               c <- submgr.RmrDatagram{12011, 0, make([]byte, 1)}
        }
-       return
 }
 
 func main() {
        e2t := E2t{}
+       go e2t.Run()
+       go e2t.sendInvalidTestMessages()
        xapp.Run(e2t)
 }