blob: 07b3ec2910bf15874f6518e148abd1f26214fc22 [file] [log] [blame]
ss412g07ef76d2019-08-12 17:26:40 +03001//
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
18package controllers
19
20import (
21 "e2mgr/configuration"
22 "e2mgr/e2managererrors"
23 "e2mgr/logger"
24 "e2mgr/models"
25 "e2mgr/providers"
26 "e2mgr/rNibWriter"
27 "encoding/json"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
29 "github.com/julienschmidt/httprouter"
30 "net/http"
31)
32
33type Controller struct {
34 logger *logger.Logger
35 handlerProvider *providers.IncomingRequestHandlerProvider
36 rmrResponseChannel chan<- *models.NotificationResponse
37}
38
39func NewController(logger *logger.Logger, rNibReaderProvider func() reader.RNibReader, rNibWriterProvider func() rNibWriter.RNibWriter,
40 config *configuration.Configuration, rmrResponseChannel chan<- *models.NotificationResponse) *Controller {
41
42 provider := providers.NewIncomingRequestHandlerProvider(logger, config, rNibWriterProvider, rNibReaderProvider)
43 return &Controller{
44 logger: logger,
45 handlerProvider: provider,
46 rmrResponseChannel: rmrResponseChannel,
47 }
48}
49
50func (c *Controller)ShutdownHandler(writer http.ResponseWriter, r *http.Request, params httprouter.Params){
51
52 c.handleRequest(writer, &r.Header, providers.ShutdownRequest,nil, false, http.StatusNoContent)
53}
54
55//TODO create struct to input parameters
56func (c *Controller) handleRequest(writer http.ResponseWriter, header *http.Header, requestName providers.IncomingRequest,
57 request *models.Request, validateHeader bool, httpStatusResponse int) {
58
59 c.logger.Infof("[Client -> E2 Manager] #controller.handleRequest - request: %v", requestName) //TODO print request if exist
60
61 if validateHeader {
62
63 err := c.validateRequestHeader(header)
64 if err != nil {
65 c.handleErrorResponse(err, writer)
66 return
67 }
68 }
69
70 handler,err := c.handlerProvider.GetHandler(requestName)
71 if err != nil {
72 c.handleErrorResponse(err, writer)
73 return
74 }
75
76 err = handler.Handle(c.logger, request, c.rmrResponseChannel)
77
78 if err != nil {
79 c.handleErrorResponse(err, writer)
80 return
81 }
82
83 writer.WriteHeader(httpStatusResponse)
84 c.logger.Infof("[E2 Manager -> Client] #controller.handleRequest - status response: %v", httpStatusResponse)
85}
86
87func (c *Controller) validateRequestHeader( header *http.Header) error {
88
89 if header.Get("Content-Type") != "application/json"{
90 c.logger.Errorf("#controller.validateRequestHeader - validation failure, incorrect content type")
91
92 return e2managererrors.NewHeaderValidationError()
93 }
94 return nil
95}
96
97func (c *Controller) handleErrorResponse(err error, writer http.ResponseWriter){
98
99 var errorResponseDetails models.ErrorResponse
100 var httpError int
101
102 if err != nil {
103 switch err.(type) {
104 case *e2managererrors.RnibDbError:
105 e2Error, _ := err.(*e2managererrors.RnibDbError)
106 errorResponseDetails = models.ErrorResponse{Code: e2Error.Err.Code, Message: e2Error.Err.Message}
107 httpError = http.StatusInternalServerError
108 case *e2managererrors.CommandAlreadyInProgressError:
109 e2Error, _ := err.(*e2managererrors.CommandAlreadyInProgressError)
110 errorResponseDetails = models.ErrorResponse{Code: e2Error.Err.Code, Message: e2Error.Err.Message}
111 httpError = http.StatusMethodNotAllowed
112 case *e2managererrors.HeaderValidationError:
113 e2Error, _ := err.(*e2managererrors.HeaderValidationError)
114 errorResponseDetails = models.ErrorResponse{Code: e2Error.Err.Code, Message: e2Error.Err.Message}
115 httpError = http.StatusUnsupportedMediaType
116 default:
117 e2Error, _ := err.(*e2managererrors.InternalError)
118 errorResponseDetails = models.ErrorResponse{Code: e2Error.Err.Code, Message: e2Error.Err.Message}
119 httpError = http.StatusInternalServerError
120 }
121 }
122 errorResponse, _ := json.Marshal(errorResponseDetails)
123
124 c.logger.Errorf("[E2 Manager -> Client] #controller.handleErrorResponse - http status: %d, error response: %+v", httpError, errorResponseDetails)
125
126 writer.Header().Set("Content-Type", "application/json")
127 writer.WriteHeader(httpError)
128 _, err = writer.Write(errorResponse)
129
130 if err != nil {
131 c.logger.Errorf("#controller.handleErrorResponse - Cannot send response. writer:%v", writer)
132 }
133}