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 "github.com/go-ozzo/ozzo-validation"
29 "github.com/go-ozzo/ozzo-validation/is"
33 X2SetupActivityName = "X2_SETUP"
34 EndcSetupActivityName = "ENDC_SETUP"
37 type SetupRequestHandler struct {
38 rNibDataService services.RNibDataService
40 ranSetupManager *managers.RanSetupManager
41 protocol entities.E2ApplicationProtocol
44 func NewSetupRequestHandler(logger *logger.Logger, rNibDataService services.RNibDataService,
45 ranSetupManager *managers.RanSetupManager, protocol entities.E2ApplicationProtocol) *SetupRequestHandler {
46 return &SetupRequestHandler{
48 rNibDataService: rNibDataService,
49 ranSetupManager: ranSetupManager,
54 func (handler *SetupRequestHandler) Handle(request models.Request) (models.IResponse, error) {
56 setupRequest := request.(models.SetupRequest)
58 err := handler.validateRequestDetails(setupRequest)
63 nodebInfo, err := handler.rNibDataService.GetNodeb(setupRequest.RanName)
65 _, ok := err.(*common.ResourceNotFoundError)
67 handler.logger.Errorf("#SetupRequestHandler.Handle - failed to get nodeB entity for ran name: %v from RNIB. Error: %s",
68 setupRequest.RanName, err.Error())
69 return nil, e2managererrors.NewRnibDbError()
72 result := handler.connectNewRan(&setupRequest, handler.protocol)
76 result := handler.connectExistingRan(nodebInfo)
80 func (handler *SetupRequestHandler) connectExistingRan(nodebInfo *entities.NodebInfo) error {
82 if nodebInfo.ConnectionStatus == entities.ConnectionStatus_SHUTTING_DOWN {
83 handler.logger.Errorf("#SetupRequestHandler.connectExistingRan - RAN: %s in wrong state (%s)", nodebInfo.RanName, entities.ConnectionStatus_name[int32(nodebInfo.ConnectionStatus)])
84 return e2managererrors.NewWrongStateError(handler.getActivityName(handler.protocol), entities.ConnectionStatus_name[int32(nodebInfo.ConnectionStatus)])
87 status := entities.ConnectionStatus_CONNECTING
88 if nodebInfo.ConnectionStatus == entities.ConnectionStatus_CONNECTED{
89 status = nodebInfo.ConnectionStatus
91 nodebInfo.ConnectionAttempts = 0
93 result := handler.ranSetupManager.ExecuteSetup(nodebInfo, status)
97 func (handler *SetupRequestHandler) connectNewRan(request *models.SetupRequest, protocol entities.E2ApplicationProtocol) error {
99 nodebInfo, nodebIdentity := rnibBuilders.CreateInitialNodeInfo(request, protocol)
101 rNibErr := handler.rNibDataService.SaveNodeb(nodebIdentity, nodebInfo)
103 handler.logger.Errorf("#SetupRequestHandler.connectNewRan - failed to initial nodeb entity for ran name: %v in RNIB. Error: %s", request.RanName, rNibErr.Error())
104 return e2managererrors.NewRnibDbError()
106 handler.logger.Infof("#SetupRequestHandler.connectNewRan - initial nodeb entity for ran name: %v was saved to RNIB ", request.RanName)
108 result := handler.ranSetupManager.ExecuteSetup(nodebInfo, entities.ConnectionStatus_CONNECTING)
112 func (handler *SetupRequestHandler) validateRequestDetails(request models.SetupRequest) error {
114 if request.RanPort == 0 {
115 handler.logger.Errorf("#SetupRequestHandler.validateRequestDetails - validation failure: port cannot be zero")
116 return e2managererrors.NewRequestValidationError()
118 err := validation.ValidateStruct(&request,
119 validation.Field(&request.RanIp, validation.Required, is.IP),
120 validation.Field(&request.RanName, validation.Required),
124 handler.logger.Errorf("#SetupRequestHandler.validateRequestDetails - validation failure, error: %v", err)
125 return e2managererrors.NewRequestValidationError()
131 func (handler *SetupRequestHandler) getActivityName(protocol entities.E2ApplicationProtocol) string {
132 if protocol == entities.E2ApplicationProtocol_X2_SETUP_REQUEST {
133 return X2SetupActivityName
135 return EndcSetupActivityName