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