631f6366d1adc347d775861fbf5f73f9bfdc76d1
[ric-plt/submgr.git] / test / e2t / e2t.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 main
21
22 import (
23         "encoding/hex"
24         "errors"
25         submgr "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/control"
26         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
27         "github.com/spf13/viper"
28         "time"
29 )
30
31
32 type E2t struct {
33         submgr.E2ap
34 }
35
36 var c chan submgr.RmrDatagram = make(chan submgr.RmrDatagram, 1)
37
38 var RAWDATA string
39
40 func init() {
41         viper.AutomaticEnv()
42         viper.SetEnvPrefix("e2t")
43         viper.AllowEmptyEnv(true)
44         RAWDATA = viper.GetString("rawdata")
45         if RAWDATA == "" {
46                 RAWDATA = "000001ea7e000500aaaabbbb"
47         }
48         xapp.Logger.Info("Initial RAW Data: %v", RAWDATA)
49 }
50
51 func (e *E2t) GeneratePayload(sub_id uint16) (payload []byte, err error) {
52         skeleton, err := hex.DecodeString(RAWDATA)
53         if err != nil {
54                 return make([]byte, 0), errors.New("Unable to decode data provided in RCO_RAWDATA environment variable")
55         }
56         payload, err = e.SetSubscriptionResponseSequenceNumber(skeleton, sub_id)
57         return
58 }
59
60 func (e E2t) Consume(mtype, sub_id int, len int, payload []byte) (err error) {
61         payload_seq_num, err := e.GetSubscriptionRequestSequenceNumber(payload)
62         if err != nil {
63                 xapp.Logger.Error("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())     
64         }       
65         xapp.Logger.Info("Message Received: RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v", sub_id, payload_seq_num)
66         err = e.sendSubscriptionResponse(uint16(sub_id))
67         return
68 }
69
70 func (e *E2t) sendSubscriptionResponse(sub_id uint16) (err error) {
71         payload, err := e.GeneratePayload(sub_id)
72         if err != nil {
73                 xapp.Logger.Debug(err.Error())
74                 return
75         }
76         c  <- submgr.RmrDatagram{12011, sub_id, payload}
77         return
78 }
79
80 func (e *E2t) Run() {
81         for {
82                 message := <-c
83                 payload_seq_num, err := e.GetSubscriptionResponseSequenceNumber(message.Payload)
84                 if err != nil {
85                         xapp.Logger.Debug("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())     
86                 }
87                 xapp.Logger.Info("Sending Message: TYPE: %v | RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v)", message.MessageType, message.SubscriptionId, payload_seq_num)
88                 xapp.Rmr.Send(message.MessageType, int(message.SubscriptionId), len(message.Payload), message.Payload)
89         }
90 }
91
92 func (e *E2t) sendInvalidTestMessages() {
93         payload, err := e.GeneratePayload(0)
94         if err != nil {
95                 return
96         }
97         for {
98                 time.Sleep(7 * time.Second)
99                 c <- submgr.RmrDatagram{12011, 0, payload}
100                 time.Sleep(7 * time.Second)
101                 c <- submgr.RmrDatagram{12011, 0, make([]byte, 1)}
102         }
103 }
104
105 func main() {
106         e2t := E2t{}
107         go e2t.Run()
108         go e2t.sendInvalidTestMessages()
109         xapp.Run(e2t)
110 }