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