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