2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property and Nokia
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
23 import java.security.KeyManagementException;
24 import java.security.NoSuchAlgorithmException;
26 import javax.servlet.http.HttpServletResponse;
28 import org.oransc.ric.portal.dashboard.DashboardConstants;
29 import org.oransc.ric.portal.dashboard.k8sapi.SimpleKubernetesClient;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.context.annotation.Configuration;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.MediaType;
36 import org.springframework.security.access.annotation.Secured;
37 import org.springframework.util.Assert;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PathVariable;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RestController;
43 import io.swagger.annotations.ApiOperation;
46 * Proxies calls from the front end to a CAAS-Ingress API, which is a proxy for
49 * If a method throws RestClientResponseException, it is handled by
50 * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
51 * which returns status 502. All other exceptions are handled by Spring which
56 @RequestMapping(value = CaasIngressController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
57 public class CaasIngressController {
59 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61 // Publish constants so tests are easy to write
62 public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/caas-ingress";
64 public static final String PODS_METHOD = "/pods";
66 public static final String PP_CLUSTER = "cluster";
67 public static final String PP_NAMESPACE = "namespace";
69 public static final String CLUSTER_AUX = "aux";
70 public static final String CLUSTER_PLT = "plt";
71 public static final String CLUSTER_RIC = "ric"; // alternate for PLT
73 private final SimpleKubernetesClient ciAuxClient;
74 private final SimpleKubernetesClient ciPltClient;
77 public CaasIngressController(final SimpleKubernetesClient ciAuxApi, final SimpleKubernetesClient ciPltApi)
78 throws KeyManagementException, NoSuchAlgorithmException {
79 Assert.notNull(ciAuxApi, "auxApi must not be null");
80 Assert.notNull(ciPltApi, "pltApi must not be null");
81 this.ciAuxClient = ciAuxApi;
82 this.ciPltClient = ciPltApi;
83 if (logger.isDebugEnabled())
84 logger.debug("ctor: configured with aux api {}, plt api {}", ciAuxClient.getClass().getName(),
85 ciPltClient.getClass().getName());
89 * No need to parse the V1PodList, just pass thru as a string.
91 @ApiOperation(value = "Gets list of pods in the specified cluster for the specified namespace", response = String.class)
92 @GetMapping(PODS_METHOD + "/" + PP_CLUSTER + "/{" + PP_CLUSTER + "}" + "/" + PP_NAMESPACE + "/{" + PP_NAMESPACE
94 @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
95 public String listPods(@PathVariable(PP_CLUSTER) String cluster, @PathVariable(PP_NAMESPACE) String namespace,
96 HttpServletResponse response) {
97 logger.debug("listPods: cluster {}, namespace {}", cluster, namespace);
98 if (CLUSTER_AUX.equalsIgnoreCase(cluster)) {
99 return ciAuxClient.listPods(namespace);
100 } else if (CLUSTER_PLT.equalsIgnoreCase(cluster) || CLUSTER_RIC.equalsIgnoreCase(cluster)) {
101 return ciPltClient.listPods(namespace);
103 logger.warn("listPods: unknown cluster {}", cluster);
104 response.setStatus(HttpStatus.BAD_REQUEST.value());