Add version 0.5.0
[ric-plt/submgr.git] / test / rco / rco.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         "os"
30 )
31
32 type Rco struct {
33         submgr.E2ap
34 }
35
36 var c chan submgr.RmrDatagram = make(chan submgr.RmrDatagram, 1)
37
38 var RAWDATA string
39 var SEEDSN uint16
40
41 func init() {
42         viper.AutomaticEnv()
43         viper.SetEnvPrefix("rco")
44         viper.AllowEmptyEnv(true)
45         RAWDATA = viper.GetString("rawdata")
46         if RAWDATA == "" {
47                 RAWDATA = "000003ea7e000500aaaaccccea6300020000ea81000e00045465737400ea6b0003000100"
48         }
49         xapp.Logger.Info("Initial RAW DATA: %v", RAWDATA)
50         SEEDSN = uint16(viper.GetInt("seed_sn"))
51         if SEEDSN == 0 || SEEDSN > 65535 {
52                 SEEDSN = 12345
53         }
54         xapp.Logger.Info("Initial SEQUENCE NUMBER: %v", SEEDSN)
55 }
56
57 func (r *Rco) GeneratePayload(sub_id uint16) (payload []byte, err error) {
58         skeleton, err := hex.DecodeString(RAWDATA)
59         if err != nil {
60                 return make([]byte, 0), errors.New("Unable to decode data provided in RCO_RAWDATA environment variable")
61         }
62         payload, err = r.SetSubscriptionRequestSequenceNumber(skeleton, sub_id)
63         return
64 }
65
66 func (r Rco) Consume(mtype, sub_id int, len int, payload []byte) (err error) {
67         payload_seq_num, err := r.GetSubscriptionResponseSequenceNumber(payload)
68         if err != nil {
69                 xapp.Logger.Error("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())     
70         }
71         xapp.Logger.Info("Message Received: RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v", sub_id, payload_seq_num)
72         return
73 }
74
75 func (r *Rco) SendSubscriptionRequests() (err error) {
76         message, err := r.GeneratePayload(SEEDSN)
77         if err != nil {
78                 xapp.Logger.Debug(err.Error())
79                 return
80         }
81         for {
82                 time.Sleep(2 * time.Second)
83                 c <- submgr.RmrDatagram{12010, SEEDSN, message}
84         }
85         return
86 }
87
88 func (r *Rco) Run() {
89         for {
90                 message := <-c
91                 payload_seq_num, err := r.GetSubscriptionRequestSequenceNumber(message.Payload)
92                 if err != nil {
93                         xapp.Logger.Debug("Unable to get Subscription Sequence Number from Payload due to: " + err.Error())     
94                 }
95                 xapp.Logger.Info("Sending Message: TYPE: %v | RMR SUBSCRIPTION_ID: %v | PAYLOAD SEQUENCE_NUMBER: %v)", message.MessageType, message.SubscriptionId, payload_seq_num)
96                 xapp.Rmr.Send(message.MessageType, int(message.SubscriptionId), len(message.Payload), message.Payload)
97         }
98 }
99
100 func (r *Rco) sendInvalidTestMessages(){
101         for {
102                 time.Sleep(7 * time.Second)
103                 c <- submgr.RmrDatagram{10000, 0, make([]byte, 1)}
104                 time.Sleep(7 * time.Second)
105                 c <- submgr.RmrDatagram{12010, 0, make([]byte, 1)}
106         }
107 }
108
109 func main() {
110         rco := Rco{}
111         go xapp.Rmr.Start(rco)
112         go rco.Run()
113         go rco.sendInvalidTestMessages()
114         err := rco.SendSubscriptionRequests()
115         if err != nil {
116                 os.Exit(1)
117         }
118 }