06cbc6b7fd914ceeeab30f386a7f5e56cc756d3b
[portal/ric-dashboard.git] / dashboard / 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
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.ArrayList;
24 import java.util.List;
25
26 import javax.validation.Valid;
27
28 import org.oransc.ric.portal.dashboard.DashboardApplication;
29 import org.oransc.ric.portal.dashboard.DashboardConstants;
30 import org.oransc.ric.portal.dashboard.config.E2ManagerApiBuilder;
31 import org.oransc.ric.portal.dashboard.model.RanDetailsTransport;
32 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
33 import org.oransc.ricplt.e2mgr.client.api.HealthCheckApi;
34 import org.oransc.ricplt.e2mgr.client.api.NodebApi;
35 import org.oransc.ricplt.e2mgr.client.model.GetNodebResponse;
36 import org.oransc.ricplt.e2mgr.client.model.NodebIdentity;
37 import org.oransc.ricplt.e2mgr.client.model.UpdateGnbRequest;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.http.MediaType;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.security.access.annotation.Secured;
45 import org.springframework.util.Assert;
46 import org.springframework.validation.annotation.Validated;
47 import org.springframework.web.bind.annotation.GetMapping;
48 import org.springframework.web.bind.annotation.PathVariable;
49 import org.springframework.web.bind.annotation.PutMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RestController;
53 import org.springframework.web.client.HttpStatusCodeException;
54
55 import io.swagger.annotations.ApiOperation;
56
57 /**
58  * Proxies calls from the front end to the E2 Manager API.
59  * 
60  * If a method throws RestClientResponseException, it is handled by a method in
61  * {@link CustomResponseEntityExceptionHandler} which returns status 502. All
62  * other exceptions are handled by Spring which returns status 500.
63  */
64 @Configuration
65 @RestController
66 @RequestMapping(value = E2ManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
67 public class E2ManagerController {
68
69         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
70
71         // Publish paths in constants for tests
72         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr";
73         // Dashboard only
74         public static final String HEALTH_METHOD = "health";
75         // Keep these consistent with the E2M implementation
76         /* package */ static final String NODEB_PREFIX = "nodeb";
77         public static final String RAN_METHOD = NODEB_PREFIX + "/ran";
78         public static final String NODEB_SHUTDOWN_METHOD = NODEB_PREFIX + "/shutdown";
79         public static final String NODEB_LIST_METHOD = NODEB_PREFIX + "/ids";
80         // Path parameters
81         private static final String PP_RANNAME = "ranName";
82
83         // Populated by the autowired constructor
84         private final E2ManagerApiBuilder e2ManagerApiBuilder;
85
86         @Autowired
87         public E2ManagerController(final E2ManagerApiBuilder e2ManagerApiBuilder) {
88                 Assert.notNull(e2ManagerApiBuilder, "builder must not be null");
89                 this.e2ManagerApiBuilder = e2ManagerApiBuilder;
90                 if (logger.isDebugEnabled())
91                         logger.debug("ctor: configured with builder type {}", e2ManagerApiBuilder.getClass().getName());
92         }
93
94         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
95         @GetMapping(DashboardConstants.VERSION_METHOD)
96         // No role required
97         public SuccessTransport getClientVersion() {
98                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
99         }
100
101         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
102         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + HEALTH_METHOD)
103         // No role required
104         public ResponseEntity<String> healthGet(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
105                 logger.debug("healthGet instance {}", instanceKey);
106                 HealthCheckApi api = e2ManagerApiBuilder.getHealthCheckApi(instanceKey);
107                 api.healthGet();
108                 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
109         }
110
111         // This calls other methods to simplify the task of the front-end.
112         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
113         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + RAN_METHOD)
114         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
115         public List<RanDetailsTransport> getRanDetails(
116                         @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
117                 logger.debug("getRanDetails instance {}", instanceKey);
118                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
119                 List<NodebIdentity> nodebIdList = api.getNodebIdList();
120                 logger.debug("getRanDetails: nodebIdList {}", nodebIdList);
121                 List<RanDetailsTransport> details = new ArrayList<>();
122                 for (NodebIdentity nbid : nodebIdList) {
123                         GetNodebResponse nbResp = null;
124                         try {
125                                 // Catch exceptions to keep looping despite failures
126                                 nbResp = api.getNb(nbid.getInventoryName());
127                         } catch (HttpStatusCodeException ex) {
128                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
129                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
130                                                 .ranName(nbid.getInventoryName());
131                         }
132                         details.add(new RanDetailsTransport(nbid, nbResp));
133                 }
134                 return details;
135         }
136
137         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
138         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
139                         + NODEB_LIST_METHOD)
140         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
141         public List<NodebIdentity> getNodebIdList(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
142                 logger.debug("getNodebIdList instance {}", instanceKey);
143                 return e2ManagerApiBuilder.getNodebApi(instanceKey).getNodebIdList();
144         }
145
146         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
147         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + NODEB_PREFIX
148                         + "/{" + PP_RANNAME + "}")
149         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
150         public GetNodebResponse getNb(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
151                         @PathVariable(PP_RANNAME) String ranName) {
152                 logger.debug("getNb instance {} name {}", instanceKey, ranName);
153                 return e2ManagerApiBuilder.getNodebApi(instanceKey).getNb(ranName);
154         }
155
156         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
157         @PutMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
158                         + NODEB_SHUTDOWN_METHOD)
159         @Secured({ DashboardConstants.ROLE_ADMIN })
160         public ResponseEntity<String> nodebShutdownPut(
161                         @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
162                 logger.debug("nodebShutdownPut instance {}", instanceKey);
163                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
164                 api.nodebShutdownPut();
165                 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
166         }
167
168         @ApiOperation(value = "Update GNB.")
169         @PutMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + NODEB_PREFIX
170                         + "/{" + PP_RANNAME + "}")
171         @Secured({ DashboardConstants.ROLE_ADMIN })
172         public ResponseEntity<String> updateGnb(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
173                         @PathVariable(PP_RANNAME) String ranName, //
174                         @Valid @Validated @RequestBody UpdateGnbRequest updateGnbRequest) {
175                 logger.debug("updateGnb instance {} ran {}", instanceKey, ranName);
176                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
177                 api.updateGnb(updateGnbRequest, ranName);
178                 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
179         }
180
181 }