sync R3 content from Azure
[ric-plt/e2mgr.git] / tools / xappmock / main / xappmock.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         "context"
21         "flag"
22         "log"
23         "os"
24         "os/signal"
25         "strconv"
26         "time"
27         "xappmock/dispatcher"
28         "xappmock/frontend"
29         "xappmock/rmr"
30 )
31
32 const (
33         ENV_RMR_PORT     = "RMR_PORT"
34         RMR_PORT_DEFAULT = 5001
35 )
36
37 var rmrService *rmr.Service
38
39 func main() {
40         var rmrContext *rmr.Context
41         var rmrConfig = rmr.Config{Port: RMR_PORT_DEFAULT, MaxMsgSize: rmr.RMR_MAX_MSG_SIZE, MaxRetries: 10, Flags: 0}
42
43         if port, err := strconv.ParseUint(os.Getenv(ENV_RMR_PORT), 10, 16); err == nil {
44                 rmrConfig.Port = int(port)
45         } else {
46                 log.Printf("#main - %s: %s, using default (%d).", ENV_RMR_PORT, err, RMR_PORT_DEFAULT)
47         }
48
49         rmrService = rmr.NewService(rmrConfig, rmrContext)
50         dispatcherDesc := dispatcher.New(rmrService)
51
52         /* Load configuration file*/
53         err := frontend.ProcessConfigurationFile("resources", "conf", ".json",
54                 func(data []byte) error {
55                         return frontend.JsonCommandsDecoder(data, dispatcherDesc.JsonCommandsDecoderCB)
56                 })
57
58         if err != nil {
59                 log.Fatalf("#main - processing error: %s", err)
60         }
61
62         log.Print("#main - xApp Mock is up and running...")
63
64         flag.Parse()
65         cmd := flag.Arg(0) /*first remaining argument after flags have been processed*/
66
67         command, err := frontend.DecodeJsonCommand([]byte(cmd))
68
69         if err != nil {
70                 log.Printf("#main - command decoding error: %s", err)
71                 rmrService.CloseContext()
72                 log.Print("#main - xApp Mock is down")
73                 return
74         }
75
76         c := make(chan os.Signal, 1)
77         signal.Notify(c, os.Interrupt)
78         ctx, cancel := context.WithCancel(context.Background())
79
80         go func() {
81                 oscall := <-c
82                 log.Printf("system call:%+v", oscall)
83                 cancel()
84                 rmrService.CloseContext()
85         }()
86
87         processStartTime := time.Now()
88         dispatcherDesc.ProcessJsonCommand(ctx, command)
89         pr := dispatcherDesc.GetProcessResult()
90
91         if pr.Err != nil {
92                 log.Printf("#main - command processing Error: %s", err)
93         }
94
95         processElapsedTimeInMs := float64(time.Since(processStartTime)) / float64(time.Millisecond)
96
97         log.Printf("#main - processing (sending/receiving) messages took %.2f ms", processElapsedTimeInMs)
98         log.Printf("#main - process result: %s", pr)
99
100         rmrService.CloseContext() // TODO: called twice
101         log.Print("#main - xApp Mock is down")
102 }