Fix AC xPP page title alignment
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / E2ManagerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.controller;
21
22 import java.lang.invoke.MethodHandles;
23 import java.util.HashSet;
24 import java.util.Set;
25
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.oransc.ric.e2mgr.client.api.HealthCheckApi;
29 import org.oransc.ric.e2mgr.client.api.NodebApi;
30 import org.oransc.ric.e2mgr.client.model.GetNodebResponse;
31 import org.oransc.ric.e2mgr.client.model.SetupRequest;
32 import org.oransc.ric.portal.dashboard.DashboardApplication;
33 import org.oransc.ric.portal.dashboard.DashboardConstants;
34 import org.oransc.ric.portal.dashboard.model.E2SetupRequestType;
35 import org.oransc.ric.portal.dashboard.model.E2SetupResponse;
36 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.util.Assert;
44 import org.springframework.web.bind.annotation.PathVariable;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestMethod;
48 import org.springframework.web.bind.annotation.RestController;
49 import org.springframework.web.client.HttpStatusCodeException;
50
51 import io.swagger.annotations.ApiOperation;
52
53 /**
54  * Proxies calls from the front end to the E2 Manager API. All methods answer
55  * 502 on failure: <blockquote>HTTP server received an invalid response from a
56  * server it consulted when acting as a proxy or gateway.</blockquote>
57  * 
58  * As of this writing the E2 interface does not support get-all, so this class
59  * mocks up some of that functionality.
60  */
61 @Configuration
62 @RestController
63 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr", produces = MediaType.APPLICATION_JSON_VALUE)
64 public class E2ManagerController {
65
66         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
67
68         // Path parameters
69         private static final String PP_RANNAME = "ranName";
70
71         // Populated by the autowired constructor
72         private final HealthCheckApi e2HealthCheckApi;
73         private final NodebApi e2NodebApi;
74
75         // Stores the requests and results.
76         // TODO remove when the E2 manager is extended.
77         private Set<E2SetupResponse> responses = new HashSet<>();
78
79         @Autowired
80         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi) {
81                 Assert.notNull(e2HealthCheckApi, "API must not be null");
82                 Assert.notNull(e2NodebApi, "API must not be null");
83                 this.e2HealthCheckApi = e2HealthCheckApi;
84                 this.e2NodebApi = e2NodebApi;
85         }
86
87         private void assertNotNull(Object o) {
88                 if (o == null)
89                         throw new IllegalArgumentException("Null not permitted");
90         }
91
92         private void assertNotEmpty(String s) {
93                 assertNotNull(s);
94                 if (s.isEmpty())
95                         throw new IllegalArgumentException("Empty not permitted");
96         }
97
98         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
99         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
100         public SuccessTransport getE2ManagerClientVersion() {
101                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
102         }
103
104         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
105         @RequestMapping(value = "/health", method = RequestMethod.GET)
106         public Object healthGet(HttpServletResponse response) {
107                 logger.debug("healthGet");
108                 try {
109                         e2HealthCheckApi.healthGet();
110                         response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
111                         return null;
112                 } catch (HttpStatusCodeException ex) {
113                         logger.warn("healthGet failed: {}", ex.toString());
114                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
115                 }
116         }
117
118         // TODO replace with actual functionality
119         @ApiOperation(value = "Gets the unique requests submitted to the E2 manager.", response = E2SetupResponse.class, responseContainer = "List")
120         @RequestMapping(value = "/setup", method = RequestMethod.GET)
121         public Iterable<E2SetupResponse> getRequests() {
122                 logger.debug("getRequests");
123                 return responses;
124         }
125
126         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
127         @RequestMapping(value = "/nodeb/{" + PP_RANNAME + "}", method = RequestMethod.GET)
128         public Object getNb(@PathVariable(PP_RANNAME) String ranName) {
129                 logger.debug("getNb {}", ranName);
130                 try {
131                         return e2NodebApi.getNb(ranName);
132                 } catch (HttpStatusCodeException ex) {
133                         logger.warn("getNb failed: {}", ex.toString());
134                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
135                 }
136         }
137
138         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
139         @RequestMapping(value = "/nodeb", method = RequestMethod.DELETE)
140         public void nodebDelete() {
141                 logger.debug("nodebDelete");
142                 e2NodebApi.nodebDelete();
143                 // TODO: remove this mock functionality
144                 responses.clear();
145         }
146
147         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.", response = E2SetupResponse.class)
148         @RequestMapping(value = "/endcSetup", method = RequestMethod.POST)
149         public Object endcSetup(@RequestBody SetupRequest setupRequest) {
150                 logger.debug("endcSetup {}", setupRequest);
151                 try {
152                         assertNotEmpty(setupRequest.getRanIp());
153                         assertNotEmpty(setupRequest.getRanName());
154                         assertNotNull(setupRequest.getRanPort());
155                 } catch (Exception ex) {
156                         return new E2SetupResponse(E2SetupRequestType.ENDC, setupRequest, HttpServletResponse.SC_BAD_REQUEST);
157                 }
158                 try {
159                         e2NodebApi.endcSetup(setupRequest);
160                         E2SetupResponse r = new E2SetupResponse(E2SetupRequestType.ENDC, setupRequest,
161                                         e2NodebApi.getApiClient().getStatusCode().value());
162                         responses.add(r);
163                         return r;
164                 } catch (HttpStatusCodeException ex) {
165                         logger.warn("endcSetup failed: {}", ex.toString());
166                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
167                 }
168         }
169
170         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.", response = E2SetupResponse.class)
171         @RequestMapping(value = "/x2Setup", method = RequestMethod.POST)
172         public Object x2Setup(@RequestBody SetupRequest setupRequest) {
173                 logger.debug("x2Setup {}", setupRequest);
174                 try {
175                         assertNotEmpty(setupRequest.getRanIp());
176                         assertNotEmpty(setupRequest.getRanName());
177                         assertNotNull(setupRequest.getRanPort());
178                 } catch (Exception ex) {
179                         return new E2SetupResponse(E2SetupRequestType.ENDC, setupRequest, HttpServletResponse.SC_BAD_REQUEST);
180                 }
181                 try {
182                         e2NodebApi.x2Setup(setupRequest);
183                         E2SetupResponse r = new E2SetupResponse(E2SetupRequestType.X2, setupRequest,
184                                         e2NodebApi.getApiClient().getStatusCode().value());
185                         responses.add(r);
186                         return r;
187                 } catch (HttpStatusCodeException ex) {
188                         logger.warn("x2Setup failed: {}", ex.toString());
189                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
190                 }
191         }
192
193 }