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