sync from Azure to LF
[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 package main
18
19 import (
20         "../frontend"
21         "../rmr"
22         "../sender"
23         "flag"
24         "fmt"
25         "github.com/pkg/errors"
26         "log"
27         "os"
28         "strconv"
29 )
30
31 const (
32         ENV_RMR_PORT = "RMR_PORT"
33         RMR_PORT_DEFAULT = 5001
34 )
35
36 var rmrService *rmr.Service
37
38 func main() {
39         var rmrContext *rmr.Context
40
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)
44         } else {
45                 log.Printf("%s: %s, using default (%d).", ENV_RMR_PORT, err,RMR_PORT_DEFAULT)
46         }
47         rmrService = rmr.NewService(rmrConfig, rmrContext)
48
49         /* Load configuration file*/
50         err := frontend.ProcessConfigurationFile("resources","conf",  ".json",
51                 func(data []byte) error {
52                         return  frontend.JsonCommandsDecoder(data,jsonCommandsDecoderCB)
53                 })
54         if err != nil {
55                 log.Fatalf("processing Error: %s", err)
56         }
57
58         log.Print("xapp_mock is up and running.")
59
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)
63         }
64
65         rmrService.CloseContext()
66
67         log.Print("xapp_mock is down.")
68 }
69
70
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"))
75         }
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))
79         } else {
80                 frontend.WaitedForRmrMessageType[int(rmrMsgId)] = command
81         }
82         return nil
83 }
84
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"))
89         }
90         switch command.Action {
91         case frontend.SendRmrMessage:
92                 if  err := sender.SendJsonRmrMessage(*command, nil, rmrService); err != nil {
93                         return err
94                 }
95                 if len(command.WaitForRmrMessageType) > 0 {
96                         rmrService.ListenAndHandle() //TODO: handle error
97                 }
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))
101                 } else {
102                         frontend.WaitedForRmrMessageType[int(rmrMsgId)] = command
103                 }
104                 rmrService.ListenAndHandle() //TODO: handle error
105         default:
106                 return errors.New(fmt.Sprintf("invalid command action %s", command.Action))
107         }
108         return nil
109 }