2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 package httpmsghandlers
20 "e2mgr/e2managererrors"
26 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
27 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
29 "github.com/go-ozzo/ozzo-validation"
30 "github.com/go-ozzo/ozzo-validation/is"
34 X2SetupActivityName = "X2_SETUP"
35 EndcSetupActivityName = "ENDC_SETUP"
38 type SetupRequestHandler struct {
39 readerProvider func() reader.RNibReader
40 writerProvider func() rNibWriter.RNibWriter
42 ranSetupManager *managers.RanSetupManager
43 protocol entities.E2ApplicationProtocol
46 func NewSetupRequestHandler(logger *logger.Logger, writerProvider func() rNibWriter.RNibWriter, readerProvider func() reader.RNibReader,
47 ranSetupManager *managers.RanSetupManager, protocol entities.E2ApplicationProtocol) *SetupRequestHandler {
48 return &SetupRequestHandler{
50 readerProvider: readerProvider,
51 writerProvider: writerProvider,
52 ranSetupManager: ranSetupManager,
57 func (handler *SetupRequestHandler) Handle(request models.Request) error {
59 setupRequest := request.(models.SetupRequest)
61 err := handler.validateRequestDetails(setupRequest)
66 nodebInfo, err := handler.readerProvider().GetNodeb(setupRequest.RanName)
68 _, ok := err.(*common.ResourceNotFoundError)
70 handler.logger.Errorf("#SetupRequestHandler.Handle - failed to get nodeB entity for ran name: %v from RNIB. Error: %s",
71 setupRequest.RanName, err.Error())
72 return e2managererrors.NewRnibDbError()
75 result := handler.connectNewRan(&setupRequest, handler.protocol)
79 result := handler.connectExistingRan(nodebInfo)
83 func (handler *SetupRequestHandler) connectExistingRan(nodebInfo *entities.NodebInfo) error {
85 if nodebInfo.ConnectionStatus == entities.ConnectionStatus_SHUTTING_DOWN {
86 handler.logger.Errorf("#SetupRequestHandler.connectExistingRan - RAN: %s in wrong state (%s)", nodebInfo.RanName, entities.ConnectionStatus_name[int32(nodebInfo.ConnectionStatus)])
87 return e2managererrors.NewWrongStateError(handler.getActivityName(handler.protocol), entities.ConnectionStatus_name[int32(nodebInfo.ConnectionStatus)])
90 status := entities.ConnectionStatus_CONNECTING
91 if nodebInfo.ConnectionStatus == entities.ConnectionStatus_CONNECTED{
92 status = nodebInfo.ConnectionStatus
94 nodebInfo.ConnectionAttempts = 0
96 result := handler.ranSetupManager.ExecuteSetup(nodebInfo, status)
100 func (handler *SetupRequestHandler) connectNewRan(request *models.SetupRequest, protocol entities.E2ApplicationProtocol) error {
102 nodebInfo, nodebIdentity := rnibBuilders.CreateInitialNodeInfo(request, protocol)
104 rNibErr := handler.writerProvider().SaveNodeb(nodebIdentity, nodebInfo)
106 handler.logger.Errorf("#SetupRequestHandler.connectNewRan - failed to initial nodeb entity for ran name: %v in RNIB. Error: %s", request.RanName, rNibErr.Error())
107 return e2managererrors.NewRnibDbError()
109 handler.logger.Infof("#SetupRequestHandler.connectNewRan - initial nodeb entity for ran name: %v was saved to RNIB ", request.RanName)
111 result := handler.ranSetupManager.ExecuteSetup(nodebInfo, entities.ConnectionStatus_CONNECTING)
115 func (handler *SetupRequestHandler) validateRequestDetails(request models.SetupRequest) error {
117 if request.RanPort == 0 {
118 handler.logger.Errorf("#SetupRequestHandler.validateRequestDetails - validation failure: port cannot be zero")
119 return e2managererrors.NewRequestValidationError()
121 err := validation.ValidateStruct(&request,
122 validation.Field(&request.RanIp, validation.Required, is.IP),
123 validation.Field(&request.RanName, validation.Required),
127 handler.logger.Errorf("#SetupRequestHandler.validateRequestDetails - validation failure, error: %v", err)
128 return e2managererrors.NewRequestValidationError()
134 func (handler *SetupRequestHandler) getActivityName(protocol entities.E2ApplicationProtocol) string {
135 if protocol == entities.E2ApplicationProtocol_X2_SETUP_REQUEST {
136 return X2SetupActivityName
138 return EndcSetupActivityName