Fix release notes
[ric-plt/e2mgr.git] / tools / xapp_mock / main / xapp_mock.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package main
21
22 import (
23         "../frontend"
24         "../rmr"
25         "../sender"
26         "flag"
27         "fmt"
28         "github.com/pkg/errors"
29         "log"
30         "os"
31         "strconv"
32 )
33
34 const (
35         ENV_RMR_PORT = "RMR_PORT"
36         RMR_PORT_DEFAULT = 5001
37 )
38
39 var rmrService *rmr.Service
40
41 func main() {
42         var rmrContext *rmr.Context
43
44         var rmrConfig rmr.Config = rmr.Config{Port: RMR_PORT_DEFAULT, MaxMsgSize: rmr.RMR_MAX_MSG_SIZE, MaxRetries: 3, Flags: 0}
45         if port, err := strconv.ParseUint(os.Getenv(ENV_RMR_PORT), 10, 16); err == nil {
46                 rmrConfig.Port = int(port)
47         } else {
48                 log.Printf("%s: %s, using default (%d).", ENV_RMR_PORT, err,RMR_PORT_DEFAULT)
49         }
50         rmrService = rmr.NewService(rmrConfig, rmrContext)
51
52         /* Load configuration file*/
53         err := frontend.ProcessConfigurationFile("resources","conf",  ".json",
54                 func(data []byte) error {
55                         return  frontend.JsonCommandsDecoder(data,jsonCommandsDecoderCB)
56                 })
57         if err != nil {
58                 log.Fatalf("processing Error: %s", err)
59         }
60
61         log.Print("xapp_mock is up and running.")
62
63         cmd:= flag.Arg(0) /*first remaining argument after flags have been processed*/
64         if err :=  frontend.JsonCommandDecoder([]byte(cmd),jsonCommandDecoderCB); err != nil {
65                 log.Printf("command processing Error: %s", err)
66         }
67
68         rmrService.CloseContext()
69
70         log.Print("xapp_mock is down.")
71 }
72
73
74 // TODO: move callbacks to Dispatcher.
75 func jsonCommandsDecoderCB(command *frontend.JsonCommand) error {
76         if len(command.Id) == 0{
77                 return errors.New(fmt.Sprintf("invalid command, no Id"))
78         }
79         frontend.Configuration[command.Id] = command
80         if rmrMsgId, err := rmr.MessageIdToUint(command.WaitForRmrMessageType); err != nil {
81                 return errors.New(fmt.Sprintf("invalid rmr message id: %s",command.WaitForRmrMessageType))
82         } else {
83                 frontend.WaitedForRmrMessageType[int(rmrMsgId)] = command
84         }
85         return nil
86 }
87
88 // TODO: merge command with configuration
89 func jsonCommandDecoderCB(command *frontend.JsonCommand) error {
90         if len(command.Id) == 0{
91                 return errors.New(fmt.Sprintf("invalid command, no Id"))
92         }
93         switch command.Action {
94         case frontend.SendRmrMessage:
95                 if  err := sender.SendJsonRmrMessage(*command, nil, rmrService); err != nil {
96                         return err
97                 }
98                 if len(command.WaitForRmrMessageType) > 0 {
99                         rmrService.ListenAndHandle() //TODO: handle error
100                 }
101         case frontend.ReceiveRmrMessage:
102                 if rmrMsgId, err := rmr.MessageIdToUint(command.RmrMessageType); err != nil {
103                         return errors.New(fmt.Sprintf("invalid rmr message id: %s",command.WaitForRmrMessageType))
104                 } else {
105                         frontend.WaitedForRmrMessageType[int(rmrMsgId)] = command
106                 }
107                 rmrService.ListenAndHandle() //TODO: handle error
108         default:
109                 return errors.New(fmt.Sprintf("invalid command action %s", command.Action))
110         }
111         return nil
112 }