Big red button addition
[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.SetupRequest;
31 import org.oransc.ric.portal.dashboard.DashboardApplication;
32 import org.oransc.ric.portal.dashboard.DashboardConstants;
33 import org.oransc.ric.portal.dashboard.model.E2SetupRequestType;
34 import org.oransc.ric.portal.dashboard.model.E2SetupResponse;
35 import org.oransc.ric.portal.dashboard.model.IDashboardResponse;
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.util.Assert;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46 import org.springframework.web.bind.annotation.RestController;
47
48 import io.swagger.annotations.ApiOperation;
49
50 /**
51  * Provides methods to contact the E2 Manager.
52  * 
53  * As of this writing the E2 interface only supports setup connection and check
54  * health actions; it does not support query or close operations on existing
55  * connections. So this class mocks up some of that functionality.
56  */
57 @Configuration
58 @RestController
59 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr", produces = MediaType.APPLICATION_JSON_VALUE)
60 public class E2ManagerController {
61
62         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
63
64         // Populated by the autowired constructor
65         private final HealthCheckApi e2HealthCheckApi;
66         private final NodebApi e2NodebApi;
67
68         // Stores the requests and results.
69         // TODO remove when the E2 manager is extended.
70         private Set<E2SetupResponse> responses = new HashSet<>();
71
72         @Autowired
73         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi) {
74                 Assert.notNull(e2HealthCheckApi, "API must not be null");
75                 Assert.notNull(e2NodebApi, "API must not be null");
76                 this.e2HealthCheckApi = e2HealthCheckApi;
77                 this.e2NodebApi = e2NodebApi;
78         }
79
80         private void assertNotNull(Object o) {
81                 if (o == null)
82                         throw new IllegalArgumentException("Null not permitted");
83         }
84
85         private void assertNotEmpty(String s) {
86                 assertNotNull(s);
87                 if (s.isEmpty())
88                         throw new IllegalArgumentException("Empty not permitted");
89         }
90
91         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
92         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
93         public SuccessTransport getE2ManagerClientVersion() {
94                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
95         }
96
97         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
98         @RequestMapping(value = "/health", method = RequestMethod.GET)
99         public void getE2ManagerHealth(HttpServletResponse response) {
100                 e2HealthCheckApi.healthGet();
101                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
102         }
103
104         @ApiOperation(value = "Gets the unique requests submitted to the E2 manager.", response = E2SetupResponse.class, responseContainer = "List")
105         @RequestMapping(value = "/setup", method = RequestMethod.GET)
106         public Iterable<E2SetupResponse> getRequests() {
107                 logger.debug("getRequests");
108                 return responses;
109         }
110
111         // TODO replace with actual delete all RAN connections functionality
112         @ApiOperation(value = "Disconnect all RAN Connections.")
113         @RequestMapping(value = "/disconnectAllRAN", method = RequestMethod.DELETE)
114         public void disconnectAllRANConnections() {
115                 logger.debug("disconnectAllRANConnections");
116                 responses.clear();
117         }
118
119         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.", response = E2SetupResponse.class)
120         @RequestMapping(value = "/endcSetup", method = RequestMethod.POST)
121         public E2SetupResponse endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
122                 logger.debug("endcSetup {}", setupRequest);
123                 int responseCode = -1;
124                 try {
125                         assertNotEmpty(setupRequest.getRanIp());
126                         assertNotEmpty(setupRequest.getRanName());
127                         assertNotNull(setupRequest.getRanPort());
128                         e2NodebApi.endcSetup(setupRequest);
129                         responseCode = e2NodebApi.getApiClient().getStatusCode().value();
130                 } catch (Exception ex) {
131                         logger.warn("endcSetup failed", ex);
132                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
133                         responseCode = HttpServletResponse.SC_BAD_REQUEST;
134                 }
135                 E2SetupResponse r = new E2SetupResponse(E2SetupRequestType.ENDC, setupRequest, responseCode);
136                 responses.add(r);
137                 return r;
138         }
139
140         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.", response = E2SetupResponse.class)
141         @RequestMapping(value = "/x2Setup", method = RequestMethod.POST)
142         public IDashboardResponse x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
143                 logger.debug("x2Setup {}", setupRequest);
144                 int responseCode = -1;
145                 try {
146                         assertNotEmpty(setupRequest.getRanIp());
147                         assertNotEmpty(setupRequest.getRanName());
148                         assertNotNull(setupRequest.getRanPort());
149                         e2NodebApi.x2Setup(setupRequest);
150                         responseCode = e2NodebApi.getApiClient().getStatusCode().value();
151                 } catch (Exception ex) {
152                         logger.warn("x2Setup failed", ex);
153                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
154                         responseCode = HttpServletResponse.SC_BAD_REQUEST;
155                 }
156                 E2SetupResponse r = new E2SetupResponse(E2SetupRequestType.X2, setupRequest, responseCode);
157                 responses.add(r);
158                 return r;
159         }
160
161 }