Copy latest code
[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         "fmt"
23         "os"
24         "os/signal"
25         "strconv"
26         "time"
27         "xappmock/dispatcher"
28         "xappmock/frontend"
29         "xappmock/logger"
30         "xappmock/rmr"
31         "xappmock/sender"
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
43         logLevel, _ := logger.LogLevelTokenToLevel("info")
44         logger, err := logger.InitLogger(logLevel)
45         if err != nil {
46                 fmt.Printf("#app.main - failed to initialize logger, error: %s", err)
47                 os.Exit(1)
48         }
49
50         var rmrContext *rmr.Context
51         var rmrConfig = rmr.Config{Port: RMR_PORT_DEFAULT, MaxMsgSize: rmr.RMR_MAX_MSG_SIZE, MaxRetries: 10, Flags: 0}
52
53         if port, err := strconv.ParseUint(os.Getenv(ENV_RMR_PORT), 10, 16); err == nil {
54                 rmrConfig.Port = int(port)
55         } else {
56                 logger.Infof("#main - %s: %s, using default (%d).", ENV_RMR_PORT, err, RMR_PORT_DEFAULT)
57         }
58
59         rmrService = rmr.NewService(rmrConfig, rmrContext)
60         jsonSender := sender.NewJsonSender(logger)
61         dispatcherDesc := dispatcher.New(logger, rmrService, jsonSender)
62
63         /* Load configuration file*/
64         err = frontend.ProcessConfigurationFile("resources", "conf", ".json",
65                 func(data []byte) error {
66                         return frontend.JsonCommandsDecoder(data, dispatcherDesc.JsonCommandsDecoderCB)
67                 })
68
69         if err != nil {
70                 logger.Errorf("#main - processing error: %s", err)
71                 os.Exit(1)
72         }
73
74         logger.Infof("#main - xApp Mock is up and running...")
75
76         flag.Parse()
77         cmd := flag.Arg(0) /*first remaining argument after flags have been processed*/
78
79         command, err := frontend.DecodeJsonCommand([]byte(cmd))
80
81         if err != nil {
82                 logger.Errorf("#main - command decoding error: %s", err)
83                 rmrService.CloseContext()
84                 logger.Infof("#main - xApp Mock is down")
85                 return
86         }
87
88         c := make(chan os.Signal, 1)
89         signal.Notify(c, os.Interrupt)
90         ctx, cancel := context.WithCancel(context.Background())
91
92         go func() {
93                 oscall := <-c
94                 logger.Infof("system call:%+v", oscall)
95                 cancel()
96                 rmrService.CloseContext()
97         }()
98
99         dispatcherDesc.ProcessJsonCommand(ctx, command)
100         pr := dispatcherDesc.GetProcessResult()
101
102         if pr.Err != nil {
103                 logger.Errorf("#main - command processing Error: %s", pr.Err)
104         }
105
106         if pr.StartTime != nil {
107                 processElapsedTimeInMs := float64(time.Since(*pr.StartTime)) / float64(time.Millisecond)
108                 logger.Infof("#main - processing (sending/receiving) messages took %.2f ms", processElapsedTimeInMs)
109
110         }
111         logger.Infof("#main - process stats: %s", pr.Stats)
112
113         rmrService.CloseContext() // TODO: called twice
114         logger.Infof("#main - xApp Mock is down")
115 }