a792a45384c42a0597feab2d324ca5c82f83b620
[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/rNibWriter"
25         "e2mgr/rnibBuilders"
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"
31 )
32
33 const (
34         X2SetupActivityName   = "X2_SETUP"
35         EndcSetupActivityName = "ENDC_SETUP"
36 )
37
38 type SetupRequestHandler struct {
39         readerProvider  func() reader.RNibReader
40         writerProvider  func() rNibWriter.RNibWriter
41         logger          *logger.Logger
42         ranSetupManager *managers.RanSetupManager
43         protocol        entities.E2ApplicationProtocol
44 }
45
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{
49                 logger:          logger,
50                 readerProvider:  readerProvider,
51                 writerProvider:  writerProvider,
52                 ranSetupManager: ranSetupManager,
53                 protocol:        protocol,
54         }
55 }
56
57 func (handler *SetupRequestHandler) Handle(request models.Request) error {
58
59         setupRequest := request.(models.SetupRequest)
60
61         err := handler.validateRequestDetails(setupRequest)
62         if err != nil {
63                 return err
64         }
65
66         nodebInfo, err := handler.readerProvider().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 e2managererrors.NewRnibDbError()
73                 }
74
75                 result := handler.connectNewRan(&setupRequest, handler.protocol)
76                 return result
77         }
78
79         result := handler.connectExistingRan(nodebInfo)
80         return 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.writerProvider().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 }