blob: 01d89dca356f8888502f0636b255d09e794e5886 [file] [log] [blame]
ss412gefcb4522019-12-02 16:59:19 +02001//
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
ss412g697bea22019-12-24 22:38:19 +020018// This source code is part of the near-RT RIC (RAN Intelligent Controller)
19// platform project (RICP).
20
21
ss412gefcb4522019-12-02 16:59:19 +020022package controllers
23
24import (
25 "e2mgr/e2managererrors"
26 "e2mgr/logger"
27 "e2mgr/models"
28 "e2mgr/providers/httpmsghandlerprovider"
29 "encoding/json"
30 "net/http"
31 "net/http/httputil"
32 "strings"
33)
34
35type IE2TController interface {
36 GetE2TInstances(writer http.ResponseWriter, r *http.Request)
37}
38
39type E2TController struct {
40 logger *logger.Logger
41 handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider
42}
43
44func NewE2TController(logger *logger.Logger, handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider) *E2TController {
45 return &E2TController{
46 logger: logger,
47 handlerProvider: handlerProvider,
48 }
49}
50
51func (c *E2TController) GetE2TInstances(writer http.ResponseWriter, r *http.Request) {
52 c.logger.Infof("[Client -> E2 Manager] #E2TController.GetE2TInstances - request: %v", c.prettifyRequest(r))
53 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetE2TInstancesRequest, nil, false)
54}
55
56func (c *E2TController) handleRequest(writer http.ResponseWriter, header *http.Header, requestName httpmsghandlerprovider.IncomingRequest, request models.Request, validateHeader bool) {
57
58 handler, err := c.handlerProvider.GetHandler(requestName)
59
60 if err != nil {
61 c.handleErrorResponse(err, writer)
62 return
63 }
64
65 response, err := handler.Handle(request)
66
67 if err != nil {
68 c.handleErrorResponse(err, writer)
69 return
70 }
71
72 if response == nil {
73 writer.WriteHeader(http.StatusNoContent)
74 c.logger.Infof("[E2 Manager -> Client] #E2TController.handleRequest - status response: %v", http.StatusNoContent)
75 return
76 }
77
78 result, err := response.Marshal()
79
80 if err != nil {
81 c.handleErrorResponse(err, writer)
82 return
83 }
84
85 c.logger.Infof("[E2 Manager -> Client] #E2TController.handleRequest - response: %s", result)
86 writer.Header().Set("Content-Type", "application/json")
87 writer.Write(result)
88}
89
90func (c *E2TController) handleErrorResponse(err error, writer http.ResponseWriter) {
91
92 var errorResponseDetails models.ErrorResponse
93 var httpError int
94
95 if err != nil {
96 switch err.(type) {
97 case *e2managererrors.RnibDbError:
98 e2Error, _ := err.(*e2managererrors.RnibDbError)
99 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
100 httpError = http.StatusInternalServerError
101 default:
102 e2Error := e2managererrors.NewInternalError()
103 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
104 httpError = http.StatusInternalServerError
105 }
106 }
107
108 errorResponse, _ := json.Marshal(errorResponseDetails)
109
110 c.logger.Errorf("[E2 Manager -> Client] #E2TController.handleErrorResponse - http status: %d, error response: %+v", httpError, errorResponseDetails)
111
112 writer.Header().Set("Content-Type", "application/json")
113 writer.WriteHeader(httpError)
114 _, err = writer.Write(errorResponse)
115
116 if err != nil {
117 c.logger.Errorf("#E2TController.handleErrorResponse - Cannot send response. writer:%v", writer)
118 }
119}
120
121func (c *E2TController) prettifyRequest(request *http.Request) string {
122 dump, _ := httputil.DumpRequest(request, true)
123 requestPrettyPrint := strings.Replace(string(dump), "\r\n", " ", -1)
124 return strings.Replace(requestPrettyPrint, "\n", "", -1)
125}