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 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
20 package httpmsghandlers
23 "e2mgr/e2managererrors"
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
30 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
31 "github.com/go-ozzo/ozzo-validation"
32 "github.com/go-ozzo/ozzo-validation/is"
36 X2SetupActivityName = "X2_SETUP"
37 EndcSetupActivityName = "ENDC_SETUP"
40 type SetupRequestHandler struct {
41 rNibDataService services.RNibDataService
43 ranSetupManager *managers.RanSetupManager
44 protocol entities.E2ApplicationProtocol
47 func NewSetupRequestHandler(logger *logger.Logger, rNibDataService services.RNibDataService,
48 ranSetupManager *managers.RanSetupManager, protocol entities.E2ApplicationProtocol) *SetupRequestHandler {
49 return &SetupRequestHandler{
51 rNibDataService: rNibDataService,
52 ranSetupManager: ranSetupManager,
57 func (handler *SetupRequestHandler) Handle(request models.Request) (models.IResponse, error) {
59 setupRequest := request.(models.SetupRequest)
61 err := handler.validateRequestDetails(setupRequest)
66 nodebInfo, err := handler.rNibDataService.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 nil, 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.rNibDataService.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