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