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