[RICPLT-2165] Add rnibDataService to support retries
[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 package httpmsghandlers
18
19 import (
20         "e2mgr/e2managererrors"
21         "e2mgr/logger"
22         "e2mgr/managers"
23         "e2mgr/models"
24         "e2mgr/rnibBuilders"
25         "e2mgr/services"
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"
30 )
31
32 const (
33         X2SetupActivityName   = "X2_SETUP"
34         EndcSetupActivityName = "ENDC_SETUP"
35 )
36
37 type SetupRequestHandler struct {
38         rNibDataService services.RNibDataService
39         logger          *logger.Logger
40         ranSetupManager *managers.RanSetupManager
41         protocol        entities.E2ApplicationProtocol
42 }
43
44 func NewSetupRequestHandler(logger *logger.Logger, rNibDataService services.RNibDataService,
45         ranSetupManager *managers.RanSetupManager, protocol entities.E2ApplicationProtocol) *SetupRequestHandler {
46         return &SetupRequestHandler{
47                 logger:          logger,
48                 rNibDataService:  rNibDataService,
49                 ranSetupManager: ranSetupManager,
50                 protocol:        protocol,
51         }
52 }
53
54 func (handler *SetupRequestHandler) Handle(request models.Request) error {
55
56         setupRequest := request.(models.SetupRequest)
57
58         err := handler.validateRequestDetails(setupRequest)
59         if err != nil {
60                 return err
61         }
62
63         nodebInfo, err := handler.rNibDataService.GetNodeb(setupRequest.RanName)
64         if err != nil {
65                 _, ok := err.(*common.ResourceNotFoundError)
66                 if !ok {
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 e2managererrors.NewRnibDbError()
70                 }
71
72                 result := handler.connectNewRan(&setupRequest, handler.protocol)
73                 return result
74         }
75
76         result := handler.connectExistingRan(nodebInfo)
77         return result
78 }
79
80 func (handler *SetupRequestHandler) connectExistingRan(nodebInfo *entities.NodebInfo) error {
81
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)])
85         }
86
87         status := entities.ConnectionStatus_CONNECTING
88         if nodebInfo.ConnectionStatus == entities.ConnectionStatus_CONNECTED{
89                 status = nodebInfo.ConnectionStatus
90         }
91         nodebInfo.ConnectionAttempts = 0
92
93         result := handler.ranSetupManager.ExecuteSetup(nodebInfo, status)
94         return result
95 }
96
97 func (handler *SetupRequestHandler) connectNewRan(request *models.SetupRequest, protocol entities.E2ApplicationProtocol) error {
98
99         nodebInfo, nodebIdentity := rnibBuilders.CreateInitialNodeInfo(request, protocol)
100
101         rNibErr := handler.rNibDataService.SaveNodeb(nodebIdentity, nodebInfo)
102         if rNibErr != nil {
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()
105         }
106         handler.logger.Infof("#SetupRequestHandler.connectNewRan - initial nodeb entity for ran name: %v was saved to RNIB ", request.RanName)
107
108         result := handler.ranSetupManager.ExecuteSetup(nodebInfo, entities.ConnectionStatus_CONNECTING)
109         return result
110 }
111
112 func (handler *SetupRequestHandler) validateRequestDetails(request models.SetupRequest) error {
113
114         if request.RanPort == 0 {
115                 handler.logger.Errorf("#SetupRequestHandler.validateRequestDetails - validation failure: port cannot be zero")
116                 return e2managererrors.NewRequestValidationError()
117         }
118         err := validation.ValidateStruct(&request,
119                 validation.Field(&request.RanIp, validation.Required, is.IP),
120                 validation.Field(&request.RanName, validation.Required),
121         )
122
123         if err != nil {
124                 handler.logger.Errorf("#SetupRequestHandler.validateRequestDetails - validation failure, error: %v", err)
125                 return e2managererrors.NewRequestValidationError()
126         }
127
128         return nil
129 }
130
131 func (handler *SetupRequestHandler) getActivityName(protocol entities.E2ApplicationProtocol) string {
132         if protocol == entities.E2ApplicationProtocol_X2_SETUP_REQUEST {
133                 return X2SetupActivityName
134         }
135         return EndcSetupActivityName
136 }