2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
25 "github.com/pkg/errors"
32 ENV_RMR_PORT = "RMR_PORT"
33 RMR_PORT_DEFAULT = 5001
36 var rmrService *rmr.Service
39 var rmrContext *rmr.Context
41 var rmrConfig rmr.Config = rmr.Config{Port: RMR_PORT_DEFAULT, MaxMsgSize: rmr.RMR_MAX_MSG_SIZE, MaxRetries: 3, Flags: 0}
42 if port, err := strconv.ParseUint(os.Getenv(ENV_RMR_PORT), 10, 16); err == nil {
43 rmrConfig.Port = int(port)
45 log.Printf("%s: %s, using default (%d).", ENV_RMR_PORT, err,RMR_PORT_DEFAULT)
47 rmrService = rmr.NewService(rmrConfig, rmrContext)
49 /* Load configuration file*/
50 err := frontend.ProcessConfigurationFile("resources","conf", ".json",
51 func(data []byte) error {
52 return frontend.JsonCommandsDecoder(data,jsonCommandsDecoderCB)
55 log.Fatalf("processing Error: %s", err)
58 log.Print("xapp_mock is up and running.")
60 cmd:= flag.Arg(0) /*first remaining argument after flags have been processed*/
61 if err := frontend.JsonCommandDecoder([]byte(cmd),jsonCommandDecoderCB); err != nil {
62 log.Printf("command processing Error: %s", err)
65 rmrService.CloseContext()
67 log.Print("xapp_mock is down.")
71 // TODO: move callbacks to Dispatcher.
72 func jsonCommandsDecoderCB(command *frontend.JsonCommand) error {
73 if len(command.Id) == 0{
74 return errors.New(fmt.Sprintf("invalid command, no Id"))
76 frontend.Configuration[command.Id] = command
77 if rmrMsgId, err := rmr.MessageIdToUint(command.WaitForRmrMessageType); err != nil {
78 return errors.New(fmt.Sprintf("invalid rmr message id: %s",command.WaitForRmrMessageType))
80 frontend.WaitedForRmrMessageType[int(rmrMsgId)] = command
85 // TODO: merge command with configuration
86 func jsonCommandDecoderCB(command *frontend.JsonCommand) error {
87 if len(command.Id) == 0{
88 return errors.New(fmt.Sprintf("invalid command, no Id"))
90 switch command.Action {
91 case frontend.SendRmrMessage:
92 if err := sender.SendJsonRmrMessage(*command, nil, rmrService); err != nil {
95 if len(command.WaitForRmrMessageType) > 0 {
96 rmrService.ListenAndHandle() //TODO: handle error
98 case frontend.ReceiveRmrMessage:
99 if rmrMsgId, err := rmr.MessageIdToUint(command.RmrMessageType); err != nil {
100 return errors.New(fmt.Sprintf("invalid rmr message id: %s",command.WaitForRmrMessageType))
102 frontend.WaitedForRmrMessageType[int(rmrMsgId)] = command
104 rmrService.ListenAndHandle() //TODO: handle error
106 return errors.New(fmt.Sprintf("invalid command action %s", command.Action))