[RICPLT-1853] Add UT & more
[ric-plt/e2mgr.git] / E2Manager / handlers / setup_request_handler.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 package handlers
19
20 import (
21         "e2mgr/logger"
22         "e2mgr/rNibWriter"
23         "e2mgr/rnibBuilders"
24         "fmt"
25         "os"
26         "sync"
27         "time"
28
29         "e2mgr/models"
30         "e2mgr/rmrCgo"
31         "e2mgr/sessions"
32 )
33
34 const (
35         ENV_RIC_ID                       = "RIC_ID"
36         MaxAsn1CodecAllocationBufferSize = 64 * 1024
37         MaxAsn1PackedBufferSize          = 4096
38         MaxAsn1CodecMessageBufferSize    = 4096
39 )
40
41 const (
42         shortMacro_eNB_ID = 18
43         macro_eNB_ID      = 20
44         longMacro_eNB_ID  = 21
45         home_eNB_ID       = 28
46 )
47
48 /*The Ric Id is the combination of pLMNId and ENBId*/
49 var pLMNId []byte
50 var eNBId []byte
51 var eNBIdBitqty uint
52 var ricFlag = [3]byte{0xbb, 0xbc, 0xcc} /*pLMNId [3]bytes*/
53
54 type SetupRequestHandler struct {
55         rnibWriterProvider func() rNibWriter.RNibWriter
56 }
57
58 func NewSetupRequestHandler(rnibWriterProvider func() rNibWriter.RNibWriter) *SetupRequestHandler {
59         return &SetupRequestHandler{
60                 rnibWriterProvider: rnibWriterProvider,
61         }
62 }
63
64 func (handler SetupRequestHandler) PreHandle(logger *logger.Logger, details *models.RequestDetails) error {
65         nodebInfo, nodebIdentity := rnibBuilders.CreateInitialNodeInfo(details)
66
67         rNibErr := handler.rnibWriterProvider().SaveNodeb(nodebIdentity, nodebInfo)
68         if rNibErr != nil {
69                 logger.Errorf("#setup_request_handler.PreHandle - failed to save initial nodeb entity for ran name: %v in RNIB. Error: %s", details.RanName, rNibErr.Error())
70         } else {
71                 logger.Infof("#setup_request_handler.PreHandle - initial nodeb entity for ran name: %v was saved to RNIB ", details.RanName)
72         }
73
74         return rNibErr
75 }
76
77 func (SetupRequestHandler) CreateMessage(logger *logger.Logger, requestDetails *models.RequestDetails, messageChannel chan *models.E2RequestMessage, e2sessions sessions.E2Sessions, startTime time.Time, wg sync.WaitGroup) {
78
79         wg.Add(1)
80
81         payload, err := packX2apSetupRequest(logger, MaxAsn1CodecAllocationBufferSize /*allocation buffer*/, MaxAsn1PackedBufferSize /*max packed buffer*/, MaxAsn1CodecMessageBufferSize /*max message buffer*/, pLMNId, eNBId, eNBIdBitqty)
82         if err != nil {
83                 logger.Errorf("#setup_request_handler.CreateMessage - pack was failed. Error: %v", err)
84         } else {
85                 transactionId := requestDetails.RanName
86                 e2sessions[transactionId] = sessions.E2SessionDetails{SessionStart: startTime, Request: requestDetails}
87                 setupRequestMessage := models.NewE2RequestMessage(transactionId, requestDetails.RanIp, requestDetails.RanPort, requestDetails.RanName, payload)
88
89                 logger.Debugf("#setup_request_handler.CreateMessage - setupRequestMessage was created successfully. setup request details(transactionId = [%s]): %+v", transactionId, setupRequestMessage)
90                 messageChannel <- setupRequestMessage
91         }
92
93         wg.Done()
94 }
95
96 //Expected value in RIC_ID = pLMN_Identity-eNB_ID/<eNB_ID size in bits>
97 //<6 hex digits>-<6 or 8 hex digits>/<18|20|21|28>
98 //Each byte is represented by two hex digits, the value in the lowest byte of the eNB_ID must be assigned to the lowest bits
99 //For example, to get the value of ffffeab/28  the last byte must be 0x0b, not 0xb0 (-ffffea0b/28).
100 func parseRicID(ricId string) error {
101         if _, err := fmt.Sscanf(ricId, "%6x-%8x/%2d", &pLMNId, &eNBId, &eNBIdBitqty); err != nil {
102                 return fmt.Errorf("unable to extract the value of %s: %s", ENV_RIC_ID, err)
103         }
104
105         if len(pLMNId) < 3 {
106                 return fmt.Errorf("invalid value for %s, len(pLMNId:%v) != 3", ENV_RIC_ID, pLMNId)
107         }
108
109         if len(eNBId) < 3 {
110                 return fmt.Errorf("invalid value for %s, len(eNBId:%v) != 3 or 4", ENV_RIC_ID, eNBId)
111         }
112
113         if eNBIdBitqty != shortMacro_eNB_ID && eNBIdBitqty != macro_eNB_ID && eNBIdBitqty != longMacro_eNB_ID && eNBIdBitqty != home_eNB_ID {
114                 return fmt.Errorf("invalid value for %s, eNBIdBitqty: %d", ENV_RIC_ID, eNBIdBitqty)
115         }
116
117         return nil
118 }
119
120 //TODO: remove Get
121 func (SetupRequestHandler) GetMessageType() int {
122         return rmrCgo.RIC_X2_SETUP_REQ
123 }
124
125 func init() {
126         ricId := os.Getenv(ENV_RIC_ID)
127         //ricId="bbbccc-ffff0e/20"
128         //ricId="bbbccc-abcd0e/20"
129         if err := parseRicID(ricId); err != nil {
130                 panic(err)
131         }
132
133 }