Upgrade to E2 spec of 2 May 2019
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / E2ManagerController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * ORAN-OSC
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.X2SetupRequestApi;
30 import org.oransc.ric.e2mgr.client.model.SetupRequest;
31 import org.oransc.ric.portal.dashboard.DashboardConstants;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.context.annotation.Configuration;
36 import org.springframework.http.MediaType;
37 import org.springframework.util.Assert;
38 import org.springframework.web.bind.annotation.RequestBody;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41 import org.springframework.web.bind.annotation.RestController;
42
43 import io.swagger.annotations.ApiOperation;
44
45 /**
46  * Provides methods to contact the E2 Manager.
47  * 
48  * As of this writing the E2 interface only supports setup connection and check
49  * health actions; it does not support query or close operations on existing
50  * connections. So this class mocks up some of that needed functionality.
51  */
52 @Configuration
53 @RestController
54 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr", produces = MediaType.APPLICATION_JSON_VALUE)
55 public class E2ManagerController {
56
57         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58
59         // Populated by the autowired constructor
60         private final HealthCheckApi healthCheckApi;
61         private final X2SetupRequestApi x2SetupRequestApi;
62
63         // Tracks the requests previously submitted.
64         // TODO remove when the E2 manager is extended.
65         private Set<SetupRequest> requests = new HashSet<>();
66
67         @Autowired
68         public E2ManagerController(final HealthCheckApi healthCheckApi, final X2SetupRequestApi x2SetupRequestApi) {
69                 Assert.notNull(healthCheckApi, "API must not be null");
70                 this.healthCheckApi = healthCheckApi;
71                 this.x2SetupRequestApi = x2SetupRequestApi;
72         }
73
74         private void assertNotNull(Object o) {
75                 if (o == null)
76                         throw new IllegalArgumentException("Null not permitted");
77         }
78
79         private void assertNotEmpty(String s) {
80                 assertNotNull(s);
81                 if (s.isEmpty())
82                         throw new IllegalArgumentException("Empty not permitted");
83         }
84
85         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
86         @RequestMapping(value = "/health", method = RequestMethod.GET)
87         public void getHealth(HttpServletResponse response) {
88                 logger.debug("getHealth");
89                 healthCheckApi.healthCheck();
90                 response.setStatus(healthCheckApi.getApiClient().getStatusCode().value());
91         }
92
93         @ApiOperation(value = "Gets the unique requests submitted to the E2 manager.", response = SetupRequest.class, responseContainer = "List")
94         @RequestMapping(value = "/setup", method = RequestMethod.GET)
95         public Iterable<SetupRequest> getRequests() {
96                 logger.debug("getRequests");
97                 return requests;
98         }
99
100         @ApiOperation(value = "Sets up a RAN connection via the E2 manager.")
101         @RequestMapping(value = "/setup", method = RequestMethod.POST)
102         public void setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
103                 logger.debug("setup {}", setupRequest);
104                 try {
105                         assertNotEmpty(setupRequest.getRanIp());
106                         assertNotEmpty(setupRequest.getRanName());
107                         assertNotNull(setupRequest.getRanPort());
108                 } catch (Exception ex) {
109                         logger.error("Bad request", ex);
110                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
111                 }
112                 try {
113                         requests.add(setupRequest);
114                         x2SetupRequestApi.setup(setupRequest);
115                 } catch (Exception ex) {
116                         logger.error("Failed", ex);
117                         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
118                 }
119         }
120
121 }