Add multi-layer RIC instance selector
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / CaasIngressController.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
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.oransc.ric.portal.dashboard.DashboardConstants;
27 import org.oransc.ric.portal.dashboard.config.SimpleKubernetesClientBuilder;
28 import org.oransc.ric.portal.dashboard.k8sapi.SimpleKubernetesClient;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.context.annotation.Configuration;
33 import org.springframework.http.HttpStatus;
34 import org.springframework.http.MediaType;
35 import org.springframework.security.access.annotation.Secured;
36 import org.springframework.util.Assert;
37 import org.springframework.web.bind.annotation.GetMapping;
38 import org.springframework.web.bind.annotation.PathVariable;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RestController;
41
42 import io.swagger.annotations.ApiOperation;
43
44 /**
45  * Proxies calls from the front end to a CAAS-Ingress API, which is a proxy for
46  * a Kubernetes API.
47  * 
48  * If a method throws RestClientResponseException, it is handled by
49  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
50  * which returns status 502. All other exceptions are handled by Spring which
51  * returns status 500.
52  */
53 @Configuration
54 @RestController
55 @RequestMapping(value = CaasIngressController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
56 public class CaasIngressController {
57
58         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60         // Publish constants so tests are easy to write
61         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/caas-ingress";
62         // Endpoints
63         public static final String PODS_METHOD = "pods";
64         // Path parameters
65         public static final String PP_CLUSTER = "cluster";
66         public static final String PP_NAMESPACE = "namespace";
67         // Parameter values
68         public static final String CLUSTER_PLT = "plt";
69         public static final String CLUSTER_RIC = "ric"; // alternate for PLT
70
71         // Populated by the autowired constructor
72         private final SimpleKubernetesClientBuilder simpleKubernetesClientBuilder;
73
74         @Autowired
75         public CaasIngressController(final SimpleKubernetesClientBuilder simpleKubernetesClientBuilder) {
76                 Assert.notNull(simpleKubernetesClientBuilder, "builder must not be null");
77                 this.simpleKubernetesClientBuilder = simpleKubernetesClientBuilder;
78                 if (logger.isDebugEnabled())
79                         logger.debug("ctor: configured with builder type {}", simpleKubernetesClientBuilder.getClass().getName());
80         }
81
82         /*
83          * No need to parse the V1PodList, just pass thru as a string.
84          */
85         @ApiOperation(value = "Gets list of pods in the specified cluster for the specified namespace", response = String.class)
86         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + PODS_METHOD
87                         + "/" + PP_CLUSTER + "/{" + PP_CLUSTER + "}" + "/" + PP_NAMESPACE + "/{" + PP_NAMESPACE + "}")
88         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
89         public String listPods(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey, //
90                         @PathVariable(PP_CLUSTER) String cluster, //
91                         @PathVariable(PP_NAMESPACE) String namespace, HttpServletResponse response) {
92                 logger.debug("listPods: instance {} cluster {} namespace {}", instanceKey, cluster, namespace);
93                 SimpleKubernetesClient client = simpleKubernetesClientBuilder.getSimpleKubernetesClient(instanceKey);
94                 if (CLUSTER_PLT.equalsIgnoreCase(cluster) || CLUSTER_RIC.equalsIgnoreCase(cluster)) {
95                         return client.listPods(namespace);
96                 } else {
97                         logger.warn("listPods: unknown cluster {}", cluster);
98                         response.setStatus(HttpStatus.BAD_REQUEST.value());
99                         return null;
100                 }
101         }
102
103 }