Update E2 manager API to version 20190515
[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 getVersion() {
94                 logger.debug("getVersion enter");
95                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
96         }
97
98         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
99         @RequestMapping(value = "/health", method = RequestMethod.GET)
100         public void getHealth(HttpServletResponse response) {
101                 logger.debug("getHealth");
102                 e2HealthCheckApi.healthGet();
103                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
104         }
105
106         @ApiOperation(value = "Gets the unique requests submitted to the E2 manager.", response = E2SetupResponse.class, responseContainer = "List")
107         @RequestMapping(value = "/setup", method = RequestMethod.GET)
108         public Iterable<E2SetupResponse> getRequests() {
109                 logger.debug("getRequests");
110                 return responses;
111         }
112
113         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.", response = E2SetupResponse.class)
114         @RequestMapping(value = "/endcSetup", method = RequestMethod.POST)
115         public E2SetupResponse endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
116                 logger.debug("endcSetup {}", setupRequest);
117                 int responseCode = -1;
118                 try {
119                         assertNotEmpty(setupRequest.getRanIp());
120                         assertNotEmpty(setupRequest.getRanName());
121                         assertNotNull(setupRequest.getRanPort());
122                         e2NodebApi.endcSetup(setupRequest);
123                         responseCode = e2NodebApi.getApiClient().getStatusCode().value();
124                 } catch (Exception ex) {
125                         logger.warn("endcSetup failed", ex);
126                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
127                         responseCode = HttpServletResponse.SC_BAD_REQUEST;
128                 }
129                 E2SetupResponse r = new E2SetupResponse(E2SetupRequestType.ENDC, setupRequest, responseCode);
130                 responses.add(r);
131                 return r;
132         }
133
134         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.", response = E2SetupResponse.class)
135         @RequestMapping(value = "/x2Setup", method = RequestMethod.POST)
136         public IDashboardResponse x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
137                 logger.debug("x2Setup {}", setupRequest);
138                 int responseCode = -1;
139                 try {
140                         assertNotEmpty(setupRequest.getRanIp());
141                         assertNotEmpty(setupRequest.getRanName());
142                         assertNotNull(setupRequest.getRanPort());
143                         e2NodebApi.x2Setup(setupRequest);
144                         responseCode = e2NodebApi.getApiClient().getStatusCode().value();
145                 } catch (Exception ex) {
146                         logger.warn("x2Setup failed", ex);
147                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
148                         responseCode = HttpServletResponse.SC_BAD_REQUEST;
149                 }
150                 E2SetupResponse r = new E2SetupResponse(E2SetupRequestType.X2, setupRequest, responseCode);
151                 responses.add(r);
152                 return r;
153         }
154
155 }