blob: a0126afe9a313b8a8493b376a724a3f859257305 [file] [log] [blame]
ss412g07ef76d2019-08-12 17:26:40 +03001//
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
18package services
19
20import (
21 "e2mgr/logger"
ss412g07ef76d2019-08-12 17:26:40 +030022 "e2mgr/models"
23 "e2mgr/rmrCgo"
24 "e2mgr/sessions"
25 "strconv"
26 "sync"
27)
28
ss412g07ef76d2019-08-12 17:26:40 +030029type RmrConfig struct {
30 Port int
31 MaxMsgSize int
32 Flags int
33 Logger *logger.Logger
34}
35
36func NewRmrConfig(port int, maxMsgSize int, flags int, logger *logger.Logger) *RmrConfig {
37 return &RmrConfig{port, maxMsgSize, flags, logger}
38}
39
40// RmrService holds an instance of RMR messenger as well as its configuration
41type RmrService struct {
rh362jf9e31bb2019-08-15 15:37:32 +030042 Config *RmrConfig
43 Messenger *rmrCgo.RmrMessenger
44 E2sessions sessions.E2Sessions
45 RmrResponse chan *models.NotificationResponse
ss412g07ef76d2019-08-12 17:26:40 +030046}
47
48// NewRmrService instantiates a new Rmr service instance
rh362jf9e31bb2019-08-15 15:37:32 +030049func NewRmrService(rmrConfig *RmrConfig, msrImpl rmrCgo.RmrMessenger, e2sessions sessions.E2Sessions, rmrResponse chan *models.NotificationResponse) *RmrService {
ss412g07ef76d2019-08-12 17:26:40 +030050
51 return &RmrService{
rh362jf9e31bb2019-08-15 15:37:32 +030052 Config: rmrConfig,
53 Messenger: msrImpl.Init("tcp:"+strconv.Itoa(rmrConfig.Port), rmrConfig.MaxMsgSize, rmrConfig.Flags, rmrConfig.Logger),
54 E2sessions: e2sessions,
55 RmrResponse: rmrResponse,
ss412g07ef76d2019-08-12 17:26:40 +030056 }
57}
58
59func (r *RmrService) SendMessage(messageType int, messageChannel chan *models.E2RequestMessage, errorChannel chan error,
60 wg sync.WaitGroup) {
61
62 wg.Add(1)
63 setupRequestMessage := <-messageChannel
rh362jf9e31bb2019-08-15 15:37:32 +030064 e2Message := setupRequestMessage.GetMessageAsBytes(r.Config.Logger)
ss412g07ef76d2019-08-12 17:26:40 +030065
66 transactionId := []byte(setupRequestMessage.TransactionId())
67
rh362jf9e31bb2019-08-15 15:37:32 +030068 msg := rmrCgo.NewMBuf(messageType, len(e2Message) /*r.config.MaxMsgSize*/, setupRequestMessage.RanName(), &e2Message, &transactionId)
ss412g07ef76d2019-08-12 17:26:40 +030069
rh362jf9e31bb2019-08-15 15:37:32 +030070 r.Config.Logger.Debugf("#rmr_service.SendMessage - Going to send the message: %#v\n", msg)
71 _, err := (*r.Messenger).SendMsg(msg, r.Config.MaxMsgSize)
ss412g07ef76d2019-08-12 17:26:40 +030072
73 errorChannel <- err
74 wg.Done()
75}
76
rh362jf9e31bb2019-08-15 15:37:32 +030077func (r *RmrService) SendRmrMessage(response *models.NotificationResponse) error {
ss412g07ef76d2019-08-12 17:26:40 +030078
rh362jf9e31bb2019-08-15 15:37:32 +030079 msgAsBytes := response.GetMessageAsBytes(r.Config.Logger)
ss412g07ef76d2019-08-12 17:26:40 +030080 transactionIdByteArr := []byte(response.RanName)
81
82 msg := rmrCgo.NewMBuf(response.MgsType, len(msgAsBytes), response.RanName, &msgAsBytes, &transactionIdByteArr)
83
rh362jf9e31bb2019-08-15 15:37:32 +030084 _, err := (*r.Messenger).SendMsg(msg, r.Config.MaxMsgSize)
ss412g07ef76d2019-08-12 17:26:40 +030085
86 if err != nil {
rh362jf9e31bb2019-08-15 15:37:32 +030087 r.Config.Logger.Errorf("#rmr_service.SendRmrMessage - error: %#v\n", err)
88 return err
ss412g07ef76d2019-08-12 17:26:40 +030089 }
rh362jf9e31bb2019-08-15 15:37:32 +030090 return nil
ss412g07ef76d2019-08-12 17:26:40 +030091}
92
rh362jf9e31bb2019-08-15 15:37:32 +030093func (r *RmrService) SendResponse() {
ss412g07ef76d2019-08-12 17:26:40 +030094 for {
ss412g07ef76d2019-08-12 17:26:40 +030095
rh362jf9e31bb2019-08-15 15:37:32 +030096 response, ok := <-r.RmrResponse
ss412g07ef76d2019-08-12 17:26:40 +030097 if !ok {
98
rh362jf9e31bb2019-08-15 15:37:32 +030099 r.Config.Logger.Errorf("#rmr_service.SendResponse - channel closed")
ss412g07ef76d2019-08-12 17:26:40 +0300100 break
101 }
102
rh362jf9e31bb2019-08-15 15:37:32 +0300103 r.Config.Logger.Debugf("#rmr_service.SendResponse - Going to send message: %#v\n", response)
ss412g07ef76d2019-08-12 17:26:40 +0300104 r.SendRmrMessage(response)
105 }
106}
107
108func (r *RmrService) CloseContext() {
rh362jf9e31bb2019-08-15 15:37:32 +0300109 if r.Config.Logger.DebugEnabled() {
110 r.Config.Logger.Debugf("#rmr_service.CloseContext - RMR is ready: %v", (*r.Messenger).IsReady())
111 (*r.Messenger).Close()
112 r.Config.Logger.Debugf("#rmr_service.CloseContext - RMR is ready: %v", (*r.Messenger).IsReady())
ss412g07ef76d2019-08-12 17:26:40 +0300113 }
114}