Adding new comments for Oran in all files with licenses
[ric-plt/e2mgr.git] / E2Manager / handlers / httpmsghandlers / 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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package httpmsghandlers
21
22 import (
23         "e2mgr/e2managererrors"
24         "e2mgr/logger"
25         "e2mgr/managers"
26         "e2mgr/models"
27         "e2mgr/rnibBuilders"
28         "e2mgr/services"
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"
33 )
34
35 const (
36         X2SetupActivityName   = "X2_SETUP"
37         EndcSetupActivityName = "ENDC_SETUP"
38 )
39
40 type SetupRequestHandler struct {
41         rNibDataService services.RNibDataService
42         logger          *logger.Logger
43         ranSetupManager *managers.RanSetupManager
44         protocol        entities.E2ApplicationProtocol
45 }
46
47 func NewSetupRequestHandler(logger *logger.Logger, rNibDataService services.RNibDataService,
48         ranSetupManager *managers.RanSetupManager, protocol entities.E2ApplicationProtocol) *SetupRequestHandler {
49         return &SetupRequestHandler{
50                 logger:          logger,
51                 rNibDataService:  rNibDataService,
52                 ranSetupManager: ranSetupManager,
53                 protocol:        protocol,
54         }
55 }
56
57 func (handler *SetupRequestHandler) Handle(request models.Request) (models.IResponse, error) {
58
59         setupRequest := request.(models.SetupRequest)
60
61         err := handler.validateRequestDetails(setupRequest)
62         if err != nil {
63                 return nil, err
64         }
65
66         nodebInfo, err := handler.rNibDataService.GetNodeb(setupRequest.RanName)
67         if err != nil {
68                 _, ok := err.(*common.ResourceNotFoundError)
69                 if !ok {
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()
73                 }
74
75                 result := handler.connectNewRan(&setupRequest, handler.protocol)
76                 return nil, result
77         }
78
79         result := handler.connectExistingRan(nodebInfo)
80         return nil, result
81 }
82
83 func (handler *SetupRequestHandler) connectExistingRan(nodebInfo *entities.NodebInfo) error {
84
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)])
88         }
89
90         status := entities.ConnectionStatus_CONNECTING
91         if nodebInfo.ConnectionStatus == entities.ConnectionStatus_CONNECTED{
92                 status = nodebInfo.ConnectionStatus
93         }
94         nodebInfo.ConnectionAttempts = 0
95
96         result := handler.ranSetupManager.ExecuteSetup(nodebInfo, status)
97         return result
98 }
99
100 func (handler *SetupRequestHandler) connectNewRan(request *models.SetupRequest, protocol entities.E2ApplicationProtocol) error {
101
102         nodebInfo, nodebIdentity := rnibBuilders.CreateInitialNodeInfo(request, protocol)
103
104         rNibErr := handler.rNibDataService.SaveNodeb(nodebIdentity, nodebInfo)
105         if rNibErr != nil {
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()
108         }
109         handler.logger.Infof("#SetupRequestHandler.connectNewRan - initial nodeb entity for ran name: %v was saved to RNIB ", request.RanName)
110
111         result := handler.ranSetupManager.ExecuteSetup(nodebInfo, entities.ConnectionStatus_CONNECTING)
112         return result
113 }
114
115 func (handler *SetupRequestHandler) validateRequestDetails(request models.SetupRequest) error {
116
117         if request.RanPort == 0 {
118                 handler.logger.Errorf("#SetupRequestHandler.validateRequestDetails - validation failure: port cannot be zero")
119                 return e2managererrors.NewRequestValidationError()
120         }
121         err := validation.ValidateStruct(&request,
122                 validation.Field(&request.RanIp, validation.Required, is.IP),
123                 validation.Field(&request.RanName, validation.Required),
124         )
125
126         if err != nil {
127                 handler.logger.Errorf("#SetupRequestHandler.validateRequestDetails - validation failure, error: %v", err)
128                 return e2managererrors.NewRequestValidationError()
129         }
130
131         return nil
132 }
133
134 func (handler *SetupRequestHandler) getActivityName(protocol entities.E2ApplicationProtocol) string {
135         if protocol == entities.E2ApplicationProtocol_X2_SETUP_REQUEST {
136                 return X2SetupActivityName
137         }
138         return EndcSetupActivityName
139 }