Use O-RAN-SC
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / A1MediationController.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.net.MalformedURLException;
24 import java.net.URI;
25 import java.net.URL;
26
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.oransc.ric.portal.dashboard.DashboardConstants;
30 import org.oransc.ric.portal.dashboard.model.DelayTransport;
31 import org.oransc.ric.portal.dashboard.model.ErrorTransport;
32 import org.oransc.ric.portal.dashboard.model.IDashboardResponse;
33 import org.oransc.ric.portal.dashboard.model.LoadTransport;
34 import org.oransc.ric.portal.dashboard.model.MetricsTransport;
35 import org.oransc.ric.portal.dashboard.model.PathTransport;
36 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
37 import org.oransc.ric.portal.dashboard.model.UrlTransport;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.core.ParameterizedTypeReference;
43 import org.springframework.http.HttpMethod;
44 import org.springframework.http.MediaType;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestMapping;
48 import org.springframework.web.bind.annotation.RequestMethod;
49 import org.springframework.web.bind.annotation.RestController;
50 import org.springframework.web.client.RestTemplate;
51 import org.springframework.web.util.UriComponentsBuilder;
52
53 import io.swagger.annotations.ApiOperation;
54
55 /**
56  * Provides endpoints to get/set paths, AND to access the REST resources at
57  * those paths. This allows very late binding of deployment details.
58  */
59 @Configuration
60 @RestController
61 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/a1med", produces = MediaType.APPLICATION_JSON_VALUE)
62 public class A1MediationController {
63
64         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
65
66         private static final String A1_MEDIATION_URL = "url";
67         private static final String A1_MEDIATION_DELAY = "delay";
68         private static final String A1_MEDIATION_DELAY_PATH = A1_MEDIATION_DELAY + "path";
69         private static final String A1_MEDIATION_LOAD = "load";
70         private static final String A1_MEDIATION_LOAD_PATH = A1_MEDIATION_LOAD + "path";
71         private static final String A1_MEDIATION_METRICS = "metrics";
72         private static final String A1_MEDIATION_METRICS_PATH = A1_MEDIATION_METRICS + "path";
73
74         @Value("${a1med.basepath}")
75         private String a1MediationUrl;
76         @Value("${a1med.delaypath}")
77         private String a1MediationDelayPath;
78         @Value("${a1med.loadpath}")
79         private String a1MediationLoadPath;
80         @Value("${a1med.metricspath}")
81         private String a1MediationMetricsPath;
82
83         // For demo purposes
84         private final boolean mockData = true;
85         private final DelayTransport mockDelay = new DelayTransport(10);
86         private final LoadTransport mockLoad = new LoadTransport(1);
87         private final MetricsTransport mockMetrics = new MetricsTransport(11, 100, 123);
88
89         private final RestTemplate restTemplate = new RestTemplate();
90
91         private URI buildUri(final String baseUrl, final String[] paths) {
92                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baseUrl);
93                 for (int p = 0; p < paths.length; ++p) {
94                         if (paths[p] == null)
95                                 throw new IllegalArgumentException("Unexpected null at index " + Integer.toString(p));
96                         // this allows slashes
97                         builder.path(paths[p]);
98                 }
99                 return builder.build().encode().toUri();
100         }
101
102         @ApiOperation(value = "Gets the A1 Mediation URL.", response = IDashboardResponse.class)
103         @RequestMapping(value = A1_MEDIATION_URL, method = RequestMethod.GET)
104         public IDashboardResponse getA1MediationUrl() {
105                 return new UrlTransport(a1MediationUrl);
106         }
107
108         @ApiOperation(value = "Sets the A1 Mediation URL.", response = IDashboardResponse.class)
109         @RequestMapping(value = A1_MEDIATION_URL, method = RequestMethod.PUT)
110         public IDashboardResponse setA1MediationUrl(@RequestBody UrlTransport st, HttpServletResponse response) {
111                 try {
112                         this.a1MediationUrl = new URL(st.getUrl()).toString();
113                         return new SuccessTransport(HttpServletResponse.SC_OK, null);
114                 } catch (MalformedURLException ex) {
115                         logger.error("Failed to parse url " + st.getUrl(), ex);
116                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
117                         return new ErrorTransport(400, "Bad URL", ex);
118                 }
119         }
120
121         @ApiOperation(value = "Gets the A1 Mediation delay path.", response = IDashboardResponse.class)
122         @RequestMapping(value = A1_MEDIATION_DELAY_PATH, method = RequestMethod.GET)
123         public IDashboardResponse getA1MediationDelayPath() {
124                 return new PathTransport(a1MediationDelayPath);
125         }
126
127         @ApiOperation(value = "Sets the A1 Mediation delay path.", response = IDashboardResponse.class)
128         @RequestMapping(value = A1_MEDIATION_DELAY_PATH, method = RequestMethod.PUT)
129         public IDashboardResponse setA1MediationDelayPath(@RequestBody PathTransport st) {
130                 this.a1MediationDelayPath = st.getPath();
131                 return new SuccessTransport(HttpServletResponse.SC_OK, null);
132         }
133
134         @ApiOperation(value = "Gets the A1 Mediation load path.", response = IDashboardResponse.class)
135         @RequestMapping(value = A1_MEDIATION_LOAD_PATH, method = RequestMethod.GET)
136         public IDashboardResponse getA1MediationLoadPath() {
137                 return new PathTransport(a1MediationLoadPath);
138         }
139
140         @ApiOperation(value = "Sets the A1 Mediation load path.", response = IDashboardResponse.class)
141         @RequestMapping(value = A1_MEDIATION_LOAD_PATH, method = RequestMethod.PUT)
142         public IDashboardResponse setA1MediationLoadPath(@RequestBody PathTransport st) {
143                 this.a1MediationLoadPath = st.getPath();
144                 return new SuccessTransport(HttpServletResponse.SC_OK, null);
145         }
146
147         @ApiOperation(value = "Gets the A1 Mediation metrics path.", response = IDashboardResponse.class)
148         @RequestMapping(value = A1_MEDIATION_METRICS_PATH, method = RequestMethod.GET)
149         public IDashboardResponse getA1MediationMetricsPath() {
150                 return new PathTransport(a1MediationMetricsPath);
151         }
152
153         @ApiOperation(value = "Sets the A1 Mediation metrics path.", response = IDashboardResponse.class)
154         @RequestMapping(value = A1_MEDIATION_METRICS_PATH, method = RequestMethod.PUT)
155         public IDashboardResponse setA1MediationMetricsPath(@RequestBody PathTransport st) {
156                 this.a1MediationMetricsPath = st.getPath();
157                 return new SuccessTransport(HttpServletResponse.SC_OK, null);
158         }
159
160         @ApiOperation(value = "Gets the A1 Mediation delay value.", response = DelayTransport.class)
161         @RequestMapping(value = A1_MEDIATION_DELAY, method = RequestMethod.GET)
162         public DelayTransport getA1MediationDelay() {
163                 if (mockData) {
164                         return mockDelay;
165                 } else {
166                         URI uri = buildUri(a1MediationUrl, new String[] { a1MediationDelayPath });
167                         logger.debug("getA1MediationDelay: uri {}", uri);
168                         ResponseEntity<DelayTransport> response = restTemplate.exchange(uri, HttpMethod.GET, null,
169                                         new ParameterizedTypeReference<DelayTransport>() {
170                                         });
171                         return response.getBody();
172                 }
173         }
174
175         @ApiOperation(value = "Sets the A1 Mediation delay value.")
176         @RequestMapping(value = A1_MEDIATION_DELAY, method = RequestMethod.PUT)
177         public void putA1MediationDelay(DelayTransport value) {
178                 if (mockData) {
179                         mockDelay.setDelay(value.getDelay());
180                 } else {
181                         URI uri = buildUri(a1MediationUrl, new String[] { a1MediationDelayPath });
182                         logger.debug("putA1MediationDelay: uri {}", uri);
183                         restTemplate.put(uri, value);
184                 }
185         }
186
187         @ApiOperation(value = "Gets the A1 Mediation load value.", response = LoadTransport.class)
188         @RequestMapping(value = A1_MEDIATION_LOAD, method = RequestMethod.GET)
189         public LoadTransport getA1MediationLoad() {
190                 if (mockData) {
191                         return mockLoad;
192                 } else {
193                         URI uri = buildUri(a1MediationUrl, new String[] { a1MediationLoadPath });
194                         logger.debug("getA1MediationLoad: uri {}", uri);
195                         ResponseEntity<LoadTransport> response = restTemplate.exchange(uri, HttpMethod.GET, null,
196                                         new ParameterizedTypeReference<LoadTransport>() {
197                                         });
198                         return response.getBody();
199                 }
200         }
201
202         @ApiOperation(value = "Sets the A1 Mediation delay value.")
203         @RequestMapping(value = A1_MEDIATION_LOAD, method = RequestMethod.PUT)
204         public void putA1MediationLoad(LoadTransport value) {
205                 if (mockData) {
206                         mockLoad.setLoad(value.getLoad());
207                 } else {
208                         URI uri = buildUri(a1MediationUrl, new String[] { a1MediationDelayPath });
209                         logger.debug("putA1MediationLoad: uri {}", uri);
210                         restTemplate.put(uri, value);
211                 }
212         }
213
214         @ApiOperation(value = "Gets the A1 Mediation metrics object.", response = MetricsTransport.class)
215         @RequestMapping(value = A1_MEDIATION_METRICS, method = RequestMethod.GET)
216         public MetricsTransport getA1MediationMetrics() {
217                 if (mockData) {
218                         return mockMetrics;
219                 } else {
220                         URI uri = buildUri(a1MediationUrl, new String[] { a1MediationLoadPath });
221                         logger.debug("getA1MediationMetrics: uri {}", uri);
222                         ResponseEntity<MetricsTransport> response = restTemplate.exchange(uri, HttpMethod.GET, null,
223                                         new ParameterizedTypeReference<MetricsTransport>() {
224                                         });
225                         return response.getBody();
226                 }
227         }
228
229 }