From 3df51c6bb79df182be4f0f1bac20bde75f434494 Mon Sep 17 00:00:00 2001 From: Hong Guan Date: Wed, 7 Mar 2018 12:21:17 -0500 Subject: Fix dashboard repo issue Change-Id: I264bc0ec06bc308816bd96982efdfef28264978c Issue-ID: CCSDK-196 Signed-off-by: Hong Guan --- .../dashboard/controller/CloudifyController.java | 1274 ++++++++++---------- .../dashboard/controller/ConsulController.java | 890 +++++++------- .../controller/DashboardHomeController.java | 324 ++--- .../DashboardRestrictedBaseController.java | 550 ++++----- .../controller/ECDSingleSignOnController.java | 589 ++++----- .../controller/HealthCheckController.java | 172 +-- .../ccsdk/dashboard/domain/ControllerEndpoint.java | 142 +-- .../model/ControllerEndpointCredentials.java | 230 ++-- .../dashboard/rest/ControllerRestClientImpl.java | 824 ++++++------- .../rest/ControllerRestClientMockImpl.java | 622 +++++----- .../service/ControllerEndpointServiceImpl.java | 152 +-- .../onap/fusionapp/service/AdminAuthExtension.java | 84 +- 12 files changed, 2931 insertions(+), 2922 deletions(-) (limited to 'ccsdk-app-common/src/main/java/org') diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/CloudifyController.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/CloudifyController.java index 1962c4f..7b05841 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/CloudifyController.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/CloudifyController.java @@ -1,637 +1,637 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.controller; - -import com.fasterxml.jackson.core.JsonProcessingException; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.onap.ccsdk.dashboard.model.CloudifyDeploymentList; -import org.onap.ccsdk.dashboard.model.CloudifyExecutionRequest; -import org.onap.ccsdk.dashboard.exception.DashboardControllerException; -import org.onap.ccsdk.dashboard.model.CloudifyBlueprint; -import org.onap.ccsdk.dashboard.model.CloudifyBlueprintUpload; -import org.onap.ccsdk.dashboard.model.CloudifyDeployment; -import org.onap.ccsdk.dashboard.model.CloudifyDeploymentRequest; -import org.onap.ccsdk.dashboard.model.CloudifyExecution; -import org.onap.ccsdk.dashboard.model.CloudifyExecutionList; -import org.onap.ccsdk.dashboard.model.ECTransportModel; -import org.onap.ccsdk.dashboard.model.RestResponseError; -import org.onap.ccsdk.dashboard.model.RestResponsePage; -import org.onap.ccsdk.dashboard.rest.IControllerRestClient; -import org.openecomp.portalsdk.core.domain.User; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.openecomp.portalsdk.core.util.SystemProperties; -import org.openecomp.portalsdk.core.web.support.UserUtils; -import org.slf4j.MDC; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.client.HttpStatusCodeException; - -/** - * Controller for Cloudify features: blueprints, deployments, executions. - * Methods serve Ajax requests made by Angular scripts on pages that show - * content. - */ -@Controller -@RequestMapping("/") -public class CloudifyController extends DashboardRestrictedBaseController { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CloudifyController.class); - - /** - * Enum for selecting an item type. - */ - public enum CloudifyDataItem { - BLUEPRINT, DEPLOYMENT, EXECUTION; - } - - private static final String BLUEPRINTS_PATH = "blueprints"; - private static final String VIEW_BLUEPRINTS_PATH = "viewblueprints"; - private static final String DEPLOYMENTS_PATH = "deployments"; - private static final String EXECUTIONS_PATH = "executions"; - - /** - * Supports sorting blueprints by ID - */ - private static Comparator blueprintComparator = Comparator.comparing(o -> o.id); - - /** - * Supports sorting deployments by ID - */ - private static Comparator deploymentComparator = Comparator.comparing(o -> o.id); - - /** - * Supports sorting executions by ID - */ - private static Comparator executionComparator = Comparator.comparing(o -> o.id); - - /** - * Gets one page of objects and supporting information via the REST client. - * On success, returns a PaginatedRestResponse object as String. - * - * @param option - * Specifies which item list type to get - * @param pageNum - * Page number of results - * @param pageSize - * Number of items per browser page - * @return JSON block as String, see above. - * @throws DashboardControllerException - * On any error; e.g., Network failure. - */ - @SuppressWarnings({"rawtypes", "unchecked"}) - private String getItemListForPage(long userId, CloudifyDataItem option, int pageNum, int pageSize) - throws DashboardControllerException, JsonProcessingException { - IControllerRestClient restClient = getControllerRestClient(userId); - List itemList; - switch (option) { - case BLUEPRINT: - itemList = restClient.getBlueprints().items; - itemList.sort(blueprintComparator); - break; - case DEPLOYMENT: - itemList = restClient.getDeployments().items; - itemList.sort(deploymentComparator); - break; - default: - throw new DashboardControllerException( - "getItemListForPage failed: unimplemented case: " + option.name()); - } - - // Shrink if needed - final int totalItems = itemList.size(); - final int pageCount = (int) Math.ceil((double) totalItems / pageSize); - if (totalItems > pageSize) { - itemList = getPageOfList(pageNum, pageSize, itemList); - } - RestResponsePage model = new RestResponsePage<>(totalItems, pageCount, itemList); - return objectMapper.writeValueAsString(model); - } - - /** - * Gets one page of the specified items. This method traps exceptions and - * constructs an appropriate JSON block to report errors. - * - * @param request - * Inbound request - * @param option - * Item type to get - * @return JSON with one page of objects; or an error. - */ - protected String getItemListForPageWrapper(HttpServletRequest request, CloudifyDataItem option) { - String outboundJson = null; - try { - User appUser = UserUtils.getUserSession(request); - if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) { - throw new DashboardControllerException("getItemListForPageWrapper: Failed to get application user"); - } - int pageNum = getRequestPageNumber(request); - int pageSize = getRequestPageSize(request); - outboundJson = getItemListForPage(appUser.getId(), option, pageNum, pageSize); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "getItemListForPageWrapper caught exception", ex); - RestResponseError result; - if (ex instanceof HttpStatusCodeException) { - result = new RestResponseError(((HttpStatusCodeException) ex).getResponseBodyAsString()); - } else { - result = new RestResponseError("Failed to get " + option.name(), ex); - } - try { - outboundJson = objectMapper.writeValueAsString(result); - } catch (JsonProcessingException jpe) { - // Should never, ever happen - outboundJson = "{ \"error\" : \"" + jpe.toString() + "\"}"; - } - } - return outboundJson; - } - - /** - * Serves one page of blueprints - * - * @param request - * HttpServletRequest - * @return List of CloudifyBlueprint objects - */ - @RequestMapping(value = {BLUEPRINTS_PATH}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getBlueprintsByPage(HttpServletRequest request) { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - String json = getItemListForPageWrapper(request, CloudifyDataItem.BLUEPRINT); - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return json; - } - - /** - * Serves one page of deployments - * - * @param request - * HttpServletRequest - * @return List of CloudifyDeployment objects - */ - @RequestMapping(value = {DEPLOYMENTS_PATH}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getDeploymentsByPage(HttpServletRequest request) { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - String json = getItemListForPageWrapper(request, CloudifyDataItem.DEPLOYMENT); - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return json; - } - - /** - * Gets the specified blueprint metadata. - * - * @param id - * Blueprint ID - * @param request - * HttpServletRequest - * @return Blueprint as JSON; or error. - * @throws JsonProcessingException - * on serialization error - * - */ - @RequestMapping(value = {BLUEPRINTS_PATH + "/{id}"}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getBlueprintById(@PathVariable("id") String id, HttpServletRequest request) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.getBlueprint(id); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("getBlueprintById failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Gets the specified blueprint content for viewing. - * - * @param id - * Blueprint ID - * @param request - * HttpServletRequest - * @return Blueprint as YAML; or error. - * @throws JsonProcessingException - * on serialization error - * - */ - @RequestMapping(value = { - VIEW_BLUEPRINTS_PATH + "/{id}"}, method = RequestMethod.GET, produces = "application/yaml") - @ResponseBody - public String viewBlueprintContentById(@PathVariable("id") String id, HttpServletRequest request) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.viewBlueprint(id); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("getBlueprintContentById failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Processes request to upload a blueprint from a remote server. - * - * @param request - * HttpServletRequest - * @param blueprint - * Cloudify blueprint - * @return Blueprint as uploaded; or error. - * @throws JsonProcessingException - * on serialization error - */ - @RequestMapping(value = {BLUEPRINTS_PATH}, method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public String uploadBlueprint(HttpServletRequest request, @RequestBody CloudifyBlueprintUpload blueprint) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.uploadBlueprint(blueprint); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("uploadBlueprint failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Deletes the specified blueprint. - * - * @param id - * Blueprint ID - * @param request - * HttpServletRequest - * @param response - * HttpServletResponse - * @return No content on success; error on failure. - * @throws JsonProcessingException - * On serialization failure - */ - @RequestMapping(value = {BLUEPRINTS_PATH + "/{id}"}, method = RequestMethod.DELETE, produces = "application/json") - @ResponseBody - public String deleteBlueprint(@PathVariable("id") String id, HttpServletRequest request, - HttpServletResponse response) throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - int code = restClient.deleteBlueprint(id); - response.setStatus(code); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("deleteBlueprint failed on ID " + id, t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - if (result == null) { - return null; - } else { - return objectMapper.writeValueAsString(result); - } - } - - /** - * Gets the specified deployment. - * - * @param id - * Deployment ID - * @param request - * HttpServletRequest - * @return Deployment for the specified ID; error on failure. - * @throws JsonProcessingException - * On serialization failure - * - */ - @RequestMapping(value = {DEPLOYMENTS_PATH + "/{id}"}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getDeploymentById(@PathVariable("id") String id, HttpServletRequest request) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.getDeployment(id); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("getDeploymentById failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Processes request to create a deployment based on a blueprint. - * - * @param request - * HttpServletRequest - * @param deployment - * Deployment to upload - * @return Body of deployment; error on failure - * @throws JsonProcessingException - * On serialization failure - */ - @RequestMapping(value = {DEPLOYMENTS_PATH}, method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public String createDeployment(HttpServletRequest request, @RequestBody CloudifyDeploymentRequest deployment) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.createDeployment(deployment); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("createDeployment failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Deletes the specified deployment. - * - * @param id - * Deployment ID - * @param ignoreLiveNodes - * Boolean indicator whether to force a delete in case of live - * nodes - * @param request - * HttpServletRequest - * @param response - * HttpServletResponse - * @return Passes through HTTP status code from remote endpoint; no body on - * success - * @throws JsonProcessingException - * on serialization failure - */ - @RequestMapping(value = { - DEPLOYMENTS_PATH + "/{id}"}, method = RequestMethod.DELETE, produces = "application/json") - @ResponseBody - public String deleteDeployment(@PathVariable("id") String id, - @RequestParam(value = "ignore_live_nodes", required = false) Boolean ignoreLiveNodes, - HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - int code = restClient.deleteDeployment(id, ignoreLiveNodes == null ? false : ignoreLiveNodes); - response.setStatus(code); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("deleteDeployment failed on ID " + id, t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - if (result == null) { - return null; - } else { - return objectMapper.writeValueAsString(result); - } - } - - /** - * Gets and serves one page of executions: - *
    - *
  1. Gets all deployments; OR uses the specified deployment ID if the - * query parameter is present - *
  2. Gets executions for each deployment ID - *
  3. Sorts by execution ID - *
  4. Reduces the list to the page size (if needed) - *
  5. If the optional request parameter "status" is present, reduces the - * list to the executions with that status. - *
- * - * @param request - * HttpServletRequest - * @param deployment_id - * Optional request parameter; if found, only executions for that - * deployment ID are returned. - * @param status - * Optional request parameter; if found, only executions with - * that status are returned. - * @return List of CloudifyExecution objects - * @throws JsonProcessingException - * on serialization failure - */ - @SuppressWarnings("unchecked") - @RequestMapping(value = {EXECUTIONS_PATH}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getExecutionsByPage(HttpServletRequest request, - @RequestParam(value = "deployment_id", required = false) String deployment_id, - @RequestParam(value = "status", required = false) String status) throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - List itemList = new ArrayList<>(); - IControllerRestClient restClient = getControllerRestClient(request); - List depIds = new ArrayList<>(); - if (deployment_id == null) { - CloudifyDeploymentList depList = restClient.getDeployments(); - for (CloudifyDeployment cd : depList.items) { - depIds.add(cd.id); - } - } else { - depIds.add(deployment_id); - } - for (String depId : depIds) { - CloudifyExecutionList exeList = restClient.getExecutions(depId); - itemList.addAll(exeList.items); - } - // Filter down to specified status as needed - if (status != null) { - itemList.removeIf(ce -> !status.equals(ce.status)); - } - itemList.sort(executionComparator); - - // Paginate - final int pageNum = getRequestPageNumber(request); - final int pageSize = getRequestPageSize(request); - final int totalItems = itemList.size(); - final int pageCount = (int) Math.ceil((double) totalItems / pageSize); - // Shrink if needed - if (totalItems > pageSize) { - itemList = getPageOfList(pageNum, pageSize, itemList); - } - result = new RestResponsePage<>(totalItems, pageCount, itemList); - } catch (Exception t) { - result = new RestResponseError("getExecutionsByPage failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Gets the specified execution for one deployment. - * - * It's not clear why the deployment ID is needed. - * - * @param execution_id - * Execution ID (path variable) - * @param deployment_id - * Deployment ID (query parameter) - * @param request - * HttpServletRequest - * @return CloudifyExecutionList - * @throws JsonProcessingException - * on serialization failure - */ - @RequestMapping(value = {EXECUTIONS_PATH + "/{id}"}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getExecutionByIdAndDeploymentId(@PathVariable("id") String execution_id, - @RequestParam("deployment_id") String deployment_id, HttpServletRequest request) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.getExecutions(deployment_id); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("getExecutionByIdAndDeploymentId failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Processes request to create an execution based on a deployment. - * - * @param request - * HttpServletRequest - * @param execution - * Execution model - * @return Information about the execution - * @throws JsonProcessingException - * on serialization failure - */ - @RequestMapping(value = {EXECUTIONS_PATH}, method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public String startExecution(HttpServletRequest request, @RequestBody CloudifyExecutionRequest execution) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.startExecution(execution); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("startExecution failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Cancels an execution. - * - * @param id - * Execution ID - * @param deploymentId - * Deployment ID (not clear why this is needed) - * @param action - * Action to perform (not clear why this is needed) - * @param request - * HttpServletRequest - * @param response - * HttpServletRequest - * @return Passes through HTTP status code from remote endpoint; no body on success - * @throws JsonProcessingException - * on serialization failure - */ - @RequestMapping(value = {EXECUTIONS_PATH + "/{id}"}, method = RequestMethod.DELETE, produces = "application/json") - @ResponseBody - public String cancelExecution(@PathVariable("id") String id, - @RequestParam(value = "deployment_id") String deploymentId, @RequestParam(value = "action") String action, - HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - int code = restClient.cancelExecution(id, deploymentId, action); - response.setStatus(code); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("cancelExecution failed on ID " + id, t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - if (result == null) { - return null; - } else { - return objectMapper.writeValueAsString(result); - } - } -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.controller; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Date; +import java.util.List; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.ccsdk.dashboard.model.CloudifyDeploymentList; +import org.onap.ccsdk.dashboard.model.CloudifyExecutionRequest; +import org.onap.ccsdk.dashboard.exception.DashboardControllerException; +import org.onap.ccsdk.dashboard.model.CloudifyBlueprint; +import org.onap.ccsdk.dashboard.model.CloudifyBlueprintUpload; +import org.onap.ccsdk.dashboard.model.CloudifyDeployment; +import org.onap.ccsdk.dashboard.model.CloudifyDeploymentRequest; +import org.onap.ccsdk.dashboard.model.CloudifyExecution; +import org.onap.ccsdk.dashboard.model.CloudifyExecutionList; +import org.onap.ccsdk.dashboard.model.ECTransportModel; +import org.onap.ccsdk.dashboard.model.RestResponseError; +import org.onap.ccsdk.dashboard.model.RestResponsePage; +import org.onap.ccsdk.dashboard.rest.IControllerRestClient; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.slf4j.MDC; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.client.HttpStatusCodeException; + +/** + * Controller for Cloudify features: blueprints, deployments, executions. + * Methods serve Ajax requests made by Angular scripts on pages that show + * content. + */ +@Controller +@RequestMapping("/") +public class CloudifyController extends DashboardRestrictedBaseController { + + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CloudifyController.class); + + /** + * Enum for selecting an item type. + */ + public enum CloudifyDataItem { + BLUEPRINT, DEPLOYMENT, EXECUTION; + } + + private static final String BLUEPRINTS_PATH = "blueprints"; + private static final String VIEW_BLUEPRINTS_PATH = "viewblueprints"; + private static final String DEPLOYMENTS_PATH = "deployments"; + private static final String EXECUTIONS_PATH = "executions"; + + /** + * Supports sorting blueprints by ID + */ + private static Comparator blueprintComparator = Comparator.comparing(o -> o.id); + + /** + * Supports sorting deployments by ID + */ + private static Comparator deploymentComparator = Comparator.comparing(o -> o.id); + + /** + * Supports sorting executions by ID + */ + private static Comparator executionComparator = Comparator.comparing(o -> o.id); + + /** + * Gets one page of objects and supporting information via the REST client. + * On success, returns a PaginatedRestResponse object as String. + * + * @param option + * Specifies which item list type to get + * @param pageNum + * Page number of results + * @param pageSize + * Number of items per browser page + * @return JSON block as String, see above. + * @throws DashboardControllerException + * On any error; e.g., Network failure. + */ + @SuppressWarnings({"rawtypes", "unchecked"}) + private String getItemListForPage(long userId, CloudifyDataItem option, int pageNum, int pageSize) + throws DashboardControllerException, JsonProcessingException { + IControllerRestClient restClient = getControllerRestClient(userId); + List itemList; + switch (option) { + case BLUEPRINT: + itemList = restClient.getBlueprints().items; + itemList.sort(blueprintComparator); + break; + case DEPLOYMENT: + itemList = restClient.getDeployments().items; + itemList.sort(deploymentComparator); + break; + default: + throw new DashboardControllerException( + "getItemListForPage failed: unimplemented case: " + option.name()); + } + + // Shrink if needed + final int totalItems = itemList.size(); + final int pageCount = (int) Math.ceil((double) totalItems / pageSize); + if (totalItems > pageSize) { + itemList = getPageOfList(pageNum, pageSize, itemList); + } + RestResponsePage model = new RestResponsePage<>(totalItems, pageCount, itemList); + return objectMapper.writeValueAsString(model); + } + + /** + * Gets one page of the specified items. This method traps exceptions and + * constructs an appropriate JSON block to report errors. + * + * @param request + * Inbound request + * @param option + * Item type to get + * @return JSON with one page of objects; or an error. + */ + protected String getItemListForPageWrapper(HttpServletRequest request, CloudifyDataItem option) { + String outboundJson = null; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) { + throw new DashboardControllerException("getItemListForPageWrapper: Failed to get application user"); + } + int pageNum = getRequestPageNumber(request); + int pageSize = getRequestPageSize(request); + outboundJson = getItemListForPage(appUser.getId(), option, pageNum, pageSize); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "getItemListForPageWrapper caught exception", ex); + RestResponseError result; + if (ex instanceof HttpStatusCodeException) { + result = new RestResponseError(((HttpStatusCodeException) ex).getResponseBodyAsString()); + } else { + result = new RestResponseError("Failed to get " + option.name(), ex); + } + try { + outboundJson = objectMapper.writeValueAsString(result); + } catch (JsonProcessingException jpe) { + // Should never, ever happen + outboundJson = "{ \"error\" : \"" + jpe.toString() + "\"}"; + } + } + return outboundJson; + } + + /** + * Serves one page of blueprints + * + * @param request + * HttpServletRequest + * @return List of CloudifyBlueprint objects + */ + @RequestMapping(value = {BLUEPRINTS_PATH}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getBlueprintsByPage(HttpServletRequest request) { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + String json = getItemListForPageWrapper(request, CloudifyDataItem.BLUEPRINT); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return json; + } + + /** + * Serves one page of deployments + * + * @param request + * HttpServletRequest + * @return List of CloudifyDeployment objects + */ + @RequestMapping(value = {DEPLOYMENTS_PATH}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getDeploymentsByPage(HttpServletRequest request) { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + String json = getItemListForPageWrapper(request, CloudifyDataItem.DEPLOYMENT); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return json; + } + + /** + * Gets the specified blueprint metadata. + * + * @param id + * Blueprint ID + * @param request + * HttpServletRequest + * @return Blueprint as JSON; or error. + * @throws JsonProcessingException + * on serialization error + * + */ + @RequestMapping(value = {BLUEPRINTS_PATH + "/{id}"}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getBlueprintById(@PathVariable("id") String id, HttpServletRequest request) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.getBlueprint(id); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("getBlueprintById failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Gets the specified blueprint content for viewing. + * + * @param id + * Blueprint ID + * @param request + * HttpServletRequest + * @return Blueprint as YAML; or error. + * @throws JsonProcessingException + * on serialization error + * + */ + @RequestMapping(value = { + VIEW_BLUEPRINTS_PATH + "/{id}"}, method = RequestMethod.GET, produces = "application/yaml") + @ResponseBody + public String viewBlueprintContentById(@PathVariable("id") String id, HttpServletRequest request) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.viewBlueprint(id); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("getBlueprintContentById failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Processes request to upload a blueprint from a remote server. + * + * @param request + * HttpServletRequest + * @param blueprint + * Cloudify blueprint + * @return Blueprint as uploaded; or error. + * @throws JsonProcessingException + * on serialization error + */ + @RequestMapping(value = {BLUEPRINTS_PATH}, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String uploadBlueprint(HttpServletRequest request, @RequestBody CloudifyBlueprintUpload blueprint) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.uploadBlueprint(blueprint); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("uploadBlueprint failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Deletes the specified blueprint. + * + * @param id + * Blueprint ID + * @param request + * HttpServletRequest + * @param response + * HttpServletResponse + * @return No content on success; error on failure. + * @throws JsonProcessingException + * On serialization failure + */ + @RequestMapping(value = {BLUEPRINTS_PATH + "/{id}"}, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String deleteBlueprint(@PathVariable("id") String id, HttpServletRequest request, + HttpServletResponse response) throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + int code = restClient.deleteBlueprint(id); + response.setStatus(code); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("deleteBlueprint failed on ID " + id, t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + if (result == null) { + return null; + } else { + return objectMapper.writeValueAsString(result); + } + } + + /** + * Gets the specified deployment. + * + * @param id + * Deployment ID + * @param request + * HttpServletRequest + * @return Deployment for the specified ID; error on failure. + * @throws JsonProcessingException + * On serialization failure + * + */ + @RequestMapping(value = {DEPLOYMENTS_PATH + "/{id}"}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getDeploymentById(@PathVariable("id") String id, HttpServletRequest request) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.getDeployment(id); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("getDeploymentById failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Processes request to create a deployment based on a blueprint. + * + * @param request + * HttpServletRequest + * @param deployment + * Deployment to upload + * @return Body of deployment; error on failure + * @throws JsonProcessingException + * On serialization failure + */ + @RequestMapping(value = {DEPLOYMENTS_PATH}, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String createDeployment(HttpServletRequest request, @RequestBody CloudifyDeploymentRequest deployment) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.createDeployment(deployment); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("createDeployment failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Deletes the specified deployment. + * + * @param id + * Deployment ID + * @param ignoreLiveNodes + * Boolean indicator whether to force a delete in case of live + * nodes + * @param request + * HttpServletRequest + * @param response + * HttpServletResponse + * @return Passes through HTTP status code from remote endpoint; no body on + * success + * @throws JsonProcessingException + * on serialization failure + */ + @RequestMapping(value = { + DEPLOYMENTS_PATH + "/{id}"}, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String deleteDeployment(@PathVariable("id") String id, + @RequestParam(value = "ignore_live_nodes", required = false) Boolean ignoreLiveNodes, + HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + int code = restClient.deleteDeployment(id, ignoreLiveNodes == null ? false : ignoreLiveNodes); + response.setStatus(code); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("deleteDeployment failed on ID " + id, t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + if (result == null) { + return null; + } else { + return objectMapper.writeValueAsString(result); + } + } + + /** + * Gets and serves one page of executions: + *
    + *
  1. Gets all deployments; OR uses the specified deployment ID if the + * query parameter is present + *
  2. Gets executions for each deployment ID + *
  3. Sorts by execution ID + *
  4. Reduces the list to the page size (if needed) + *
  5. If the optional request parameter "status" is present, reduces the + * list to the executions with that status. + *
+ * + * @param request + * HttpServletRequest + * @param deployment_id + * Optional request parameter; if found, only executions for that + * deployment ID are returned. + * @param status + * Optional request parameter; if found, only executions with + * that status are returned. + * @return List of CloudifyExecution objects + * @throws JsonProcessingException + * on serialization failure + */ + @SuppressWarnings("unchecked") + @RequestMapping(value = {EXECUTIONS_PATH}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getExecutionsByPage(HttpServletRequest request, + @RequestParam(value = "deployment_id", required = false) String deployment_id, + @RequestParam(value = "status", required = false) String status) throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + List itemList = new ArrayList<>(); + IControllerRestClient restClient = getControllerRestClient(request); + List depIds = new ArrayList<>(); + if (deployment_id == null) { + CloudifyDeploymentList depList = restClient.getDeployments(); + for (CloudifyDeployment cd : depList.items) { + depIds.add(cd.id); + } + } else { + depIds.add(deployment_id); + } + for (String depId : depIds) { + CloudifyExecutionList exeList = restClient.getExecutions(depId); + itemList.addAll(exeList.items); + } + // Filter down to specified status as needed + if (status != null) { + itemList.removeIf(ce -> !status.equals(ce.status)); + } + itemList.sort(executionComparator); + + // Paginate + final int pageNum = getRequestPageNumber(request); + final int pageSize = getRequestPageSize(request); + final int totalItems = itemList.size(); + final int pageCount = (int) Math.ceil((double) totalItems / pageSize); + // Shrink if needed + if (totalItems > pageSize) { + itemList = getPageOfList(pageNum, pageSize, itemList); + } + result = new RestResponsePage<>(totalItems, pageCount, itemList); + } catch (Exception t) { + result = new RestResponseError("getExecutionsByPage failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Gets the specified execution for one deployment. + * + * It's not clear why the deployment ID is needed. + * + * @param execution_id + * Execution ID (path variable) + * @param deployment_id + * Deployment ID (query parameter) + * @param request + * HttpServletRequest + * @return CloudifyExecutionList + * @throws JsonProcessingException + * on serialization failure + */ + @RequestMapping(value = {EXECUTIONS_PATH + "/{id}"}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getExecutionByIdAndDeploymentId(@PathVariable("id") String execution_id, + @RequestParam("deployment_id") String deployment_id, HttpServletRequest request) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.getExecutions(deployment_id); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("getExecutionByIdAndDeploymentId failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Processes request to create an execution based on a deployment. + * + * @param request + * HttpServletRequest + * @param execution + * Execution model + * @return Information about the execution + * @throws JsonProcessingException + * on serialization failure + */ + @RequestMapping(value = {EXECUTIONS_PATH}, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String startExecution(HttpServletRequest request, @RequestBody CloudifyExecutionRequest execution) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.startExecution(execution); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("startExecution failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Cancels an execution. + * + * @param id + * Execution ID + * @param deploymentId + * Deployment ID (not clear why this is needed) + * @param action + * Action to perform (not clear why this is needed) + * @param request + * HttpServletRequest + * @param response + * HttpServletRequest + * @return Passes through HTTP status code from remote endpoint; no body on success + * @throws JsonProcessingException + * on serialization failure + */ + @RequestMapping(value = {EXECUTIONS_PATH + "/{id}"}, method = RequestMethod.DELETE, produces = "application/json") + @ResponseBody + public String cancelExecution(@PathVariable("id") String id, + @RequestParam(value = "deployment_id") String deploymentId, @RequestParam(value = "action") String action, + HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + int code = restClient.cancelExecution(id, deploymentId, action); + response.setStatus(code); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("cancelExecution failed on ID " + id, t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + if (result == null) { + return null; + } else { + return objectMapper.writeValueAsString(result); + } + } +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/ConsulController.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/ConsulController.java index 9f22456..8350737 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/ConsulController.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/ConsulController.java @@ -1,445 +1,445 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.controller; - -import com.fasterxml.jackson.core.JsonProcessingException; -import java.net.URI; -import java.time.Instant; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import javax.servlet.http.HttpServletRequest; - -import org.onap.ccsdk.dashboard.model.ConsulHealthServiceRegistration; -import org.onap.ccsdk.dashboard.model.ConsulNodeInfo; -import org.onap.ccsdk.dashboard.model.ConsulServiceHealth; -import org.onap.ccsdk.dashboard.model.RestResponsePage; -import org.onap.ccsdk.dashboard.rest.IControllerRestClient; -import org.onap.ccsdk.dashboard.exception.DashboardControllerException; -import org.onap.ccsdk.dashboard.model.ConsulServiceInfo; -import org.onap.ccsdk.dashboard.model.ECTransportModel; -import org.onap.ccsdk.dashboard.model.RestResponseError; -import org.onap.ccsdk.dashboard.model.RestResponseSuccess; -import org.openecomp.portalsdk.core.domain.User; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.openecomp.portalsdk.core.util.SystemProperties; -import org.openecomp.portalsdk.core.web.support.UserUtils; -import org.slf4j.MDC; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.client.HttpStatusCodeException; - -/** - * Controller for Consul features: health checks of services, nodes, data - * centers. Methods serve Ajax requests made by Angular scripts on pages that - * show content. - */ -@Controller -@RequestMapping("/healthservices") -public class ConsulController extends DashboardRestrictedBaseController { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ConsulController.class); - - /** - * Enum for selecting an item type. - */ - public enum ConsulDataItem { - SERVICE_INFO, SERVICE_HEALTH, NODES, DATACENTERS; - } - - private static final String NODES_PATH = "/nodes"; - private static final String SERVICES_PATH = "/services"; - - /** - * Supports sorting results by node name - */ - private static Comparator nodeHealthComparator = Comparator.comparing(o -> o.node); - - /** - * Supports sorting results by service name - */ - private static Comparator serviceHealthComparator = Comparator.comparing(o -> o.serviceName); - - /** - * Supports sorting results by service name - */ - private static Comparator serviceInfoComparator = Comparator.comparing(o -> o.name); - - /** - * Gets one page of objects and supporting information via the REST client. On - * success, returns a page of objects as String. - * - * @param option - * Specifies which item type to get - * @param pageNum - * Page number of results - * @param pageSize - * Number of items per browser page - * @return JSON block as String, see above. - * @throws DashboardControllerException, - * JsonProcessingException On any error; e.g., Network failure. - */ - @SuppressWarnings({"unchecked", "rawtypes"}) - private String getItemListForPage(long userId, ConsulDataItem option, int pageNum, int pageSize) - throws DashboardControllerException, JsonProcessingException { - IControllerRestClient restClient = getControllerRestClient(userId); - List itemList; - switch (option) { - case NODES: - itemList = restClient.getNodes(); - itemList.sort(nodeHealthComparator); - break; - case DATACENTERS: - itemList = restClient.getDatacenters(); - break; - default: - throw new DashboardControllerException( - "getItemListForPage failed: unimplemented case: " + option.name()); - } - - // Shrink if needed - if (itemList.size() > pageSize) { - itemList = getPageOfList(pageNum, pageSize, itemList); - } - int pageCount = (int) Math.ceil((double) itemList.size() / pageSize); - RestResponsePage model = new RestResponsePage<>(itemList.size(), pageCount, itemList); - return objectMapper.writeValueAsString(model); - } - - /** - * Gets one page of the specified items. This method traps exceptions and - * constructs an appropriate JSON block to report errors. - * - * @param request - * Inbound request - * @param option - * Item type to get - * @return JSON with one page of objects; or an error. - */ - protected String getItemListForPageWrapper(HttpServletRequest request, ConsulDataItem option) { - String outboundJson; - try { - User appUser = UserUtils.getUserSession(request); - if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) { - throw new DashboardControllerException("getItemListForPageWrapper: Failed to get application user"); - } - int pageNum = getRequestPageNumber(request); - int pageSize = getRequestPageSize(request); - outboundJson = getItemListForPage(appUser.getId(), option, pageNum, pageSize); - } catch (Exception ex) { - // Remote service failed; build descriptive error message - logger.error(EELFLoggerDelegate.errorLogger, "getItemListForPageWrapper caught exception", ex); - RestResponseError result = new RestResponseError("Failed to get " + option.name(), ex); - try { - outboundJson = objectMapper.writeValueAsString(result); - } catch (JsonProcessingException jpe) { - // Should never, ever happen - outboundJson = "{ \"error\" : \"" + jpe.toString() + "\"}"; - } - } - return outboundJson; - } - - /** - * Serves all service details. - * - * @param request - * HttpServletRequest - * @return List of ConsulServiceInfo objects, as JSON - * @throws JsonProcessingException - * if serialization fails - */ - @RequestMapping(value = {SERVICES_PATH}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getServices(HttpServletRequest request) throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - Object result; - try { - IControllerRestClient restClient = getControllerRestClient(request); - List itemList = restClient.getServices(); - itemList.sort(serviceInfoComparator); - result = itemList; - } catch (Exception t) { - result = new RestResponseError("getServices failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Serves service health details - not paginated. - * - * @param request - * HttpServletRequest - * @param serviceId - * Service ID - * @return List of ConsulServiceHealth objects as JSON - * @throws JsonProcessingException - * if serialization fails - */ - @RequestMapping(value = { - SERVICES_PATH + "/{serviceId}"}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getServiceHealthDetails(HttpServletRequest request, @PathVariable String serviceId) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - Object result; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.getServiceHealth(serviceId); - } catch (Exception t) { - result = new RestResponseError("getServiceHealthDetails failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Serves service health historical data - not paginated. - * - * @param request - * HttpServletRequest - * @param serviceName - * Service name as path parameter - * @param start - * Earliest date-time as an ISO 8061 value, such as - * 2007-12-03T10:15:30+01:00 - * @param end - * Latest date-time as an ISO 8061 value, such as - * 2007-12-03T10:15:30+01:00 - * @return List of ConsulServiceHealth objects as JSON - * @throws JsonProcessingException - * if serialization fails - */ - @RequestMapping(value = {"/svchist/{serviceName}"}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getServiceHealthHistory(HttpServletRequest request, // - @PathVariable String serviceName, // - @RequestParam String start, // - @RequestParam String end) throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - Object result = null; - try { - Instant startDateTime = Instant.parse(start); - Instant endDateTime = Instant.parse(end); - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.getServiceHealthHistory(serviceName, startDateTime, endDateTime); - } catch (HttpStatusCodeException e) { - // Rare, but can happen - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - // Work around the hack to report no-match. - result = new RestResponseError("getServiceHealthHistory failed: " + t.getMessage()); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Serves one page of service health information by getting all service names, - * then iterating over them to get the health of each service. - * - * ECOMP-C does NOT provide an API to get the health of all services in one - * request. - * - * @param request - * HttpServletRequest - * @return List of ConsulServiceHealth objects, as JSON - * @throws JsonProcessingException - * on serialization exception - */ - @SuppressWarnings("unchecked") - @RequestMapping(value = {"/serviceshealth"}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getServicesHealth(HttpServletRequest request) throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - List itemList = new ArrayList<>(); - IControllerRestClient restClient = getControllerRestClient(request); - List svcInfoList = restClient.getServices(); - for (ConsulServiceInfo csi : svcInfoList) { - List csh = restClient.getServiceHealth(csi.name); - itemList.addAll(csh); - } - itemList.sort(serviceHealthComparator); - // Paginate - final int pageNum = getRequestPageNumber(request); - final int pageSize = getRequestPageSize(request); - final int totalItems = itemList.size(); - final int pageCount = (int) Math.ceil((double) totalItems / pageSize); - // Shrink if needed - if (totalItems > pageSize) { - itemList = getPageOfList(pageNum, pageSize, itemList); - } - result = new RestResponsePage<>(totalItems, pageCount, itemList); - } catch (Exception t) { - result = new RestResponseError("getServicesHealth failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Serves one page of node information. - * - * @param request - * HttpServletRequest - * @return List of ConsulNodeInfo objects, as JSON - */ - @RequestMapping(value = {NODES_PATH}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getNodesInfo(HttpServletRequest request) { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - String json = getItemListForPageWrapper(request, ConsulDataItem.NODES); - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return json; - } - - /** - * Serves node services health details - not paginated. - * - * @param request - * HttpServletRequest - * @param nodeName - * Node name - * @return List of ConsulServiceHealth objects as JSON - * @throws JsonProcessingException - * if serialization fails - */ - @RequestMapping(value = {NODES_PATH + "/{nodeName}"}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getNodeServicesHealth(HttpServletRequest request, @PathVariable String nodeName) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - Object result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - result = restClient.getNodeServicesHealth(nodeName); - } catch (Exception t) { - result = new RestResponseError("getNodeServicesHealth failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Serves one page of data centers health. - * - * @param request - * HttpServletRequest - * @return List of ConsulHealthStatus objects - */ - @RequestMapping(value = {"/datacenters"}, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getDatacentersHealth(HttpServletRequest request) { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - String json = getItemListForPageWrapper(request, ConsulDataItem.DATACENTERS); - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return json; - } - - /** - * Processes request to register a service for health checks. - * - * @param request - * HttpServletRequest - * @param registration - * Consul service registration - * @return URI of the newly registered resource - * @throws JsonProcessingException - * on serialization error - */ - @RequestMapping(value = {"/register"}, method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public String registerService(HttpServletRequest request, @RequestBody ConsulHealthServiceRegistration registration) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - URI uri = restClient.registerService(registration); - result = new RestResponseSuccess(uri.toString()); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("registerService failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } - - /** - * Processes request to deregister a service for health checks. - * - * @param request - * HttpServletRequest - * @param serviceName - * Consul service name to deregister - * @return Success or error indicator - * @throws JsonProcessingException - * on serialization error - */ - @RequestMapping(value = { - "/deregister" + "/{serviceName}"}, method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public String deregisterService(HttpServletRequest request, @PathVariable String serviceName) - throws JsonProcessingException { - MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - ECTransportModel result = null; - try { - IControllerRestClient restClient = getControllerRestClient(request); - int code = restClient.deregisterService(serviceName); - result = new RestResponseSuccess("Deregistration yielded code " + Integer.toString(code)); - } catch (HttpStatusCodeException e) { - result = new RestResponseError(e.getResponseBodyAsString()); - } catch (Exception t) { - result = new RestResponseError("deregisterService failed", t); - } - MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - return objectMapper.writeValueAsString(result); - } -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.controller; + +import com.fasterxml.jackson.core.JsonProcessingException; +import java.net.URI; +import java.time.Instant; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Date; +import java.util.List; +import javax.servlet.http.HttpServletRequest; + +import org.onap.ccsdk.dashboard.model.ConsulHealthServiceRegistration; +import org.onap.ccsdk.dashboard.model.ConsulNodeInfo; +import org.onap.ccsdk.dashboard.model.ConsulServiceHealth; +import org.onap.ccsdk.dashboard.model.RestResponsePage; +import org.onap.ccsdk.dashboard.rest.IControllerRestClient; +import org.onap.ccsdk.dashboard.exception.DashboardControllerException; +import org.onap.ccsdk.dashboard.model.ConsulServiceInfo; +import org.onap.ccsdk.dashboard.model.ECTransportModel; +import org.onap.ccsdk.dashboard.model.RestResponseError; +import org.onap.ccsdk.dashboard.model.RestResponseSuccess; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.slf4j.MDC; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.client.HttpStatusCodeException; + +/** + * Controller for Consul features: health checks of services, nodes, data + * centers. Methods serve Ajax requests made by Angular scripts on pages that + * show content. + */ +@Controller +@RequestMapping("/healthservices") +public class ConsulController extends DashboardRestrictedBaseController { + + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ConsulController.class); + + /** + * Enum for selecting an item type. + */ + public enum ConsulDataItem { + SERVICE_INFO, SERVICE_HEALTH, NODES, DATACENTERS; + } + + private static final String NODES_PATH = "/nodes"; + private static final String SERVICES_PATH = "/services"; + + /** + * Supports sorting results by node name + */ + private static Comparator nodeHealthComparator = Comparator.comparing(o -> o.node); + + /** + * Supports sorting results by service name + */ + private static Comparator serviceHealthComparator = Comparator.comparing(o -> o.serviceName); + + /** + * Supports sorting results by service name + */ + private static Comparator serviceInfoComparator = Comparator.comparing(o -> o.name); + + /** + * Gets one page of objects and supporting information via the REST client. On + * success, returns a page of objects as String. + * + * @param option + * Specifies which item type to get + * @param pageNum + * Page number of results + * @param pageSize + * Number of items per browser page + * @return JSON block as String, see above. + * @throws DashboardControllerException, + * JsonProcessingException On any error; e.g., Network failure. + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + private String getItemListForPage(long userId, ConsulDataItem option, int pageNum, int pageSize) + throws DashboardControllerException, JsonProcessingException { + IControllerRestClient restClient = getControllerRestClient(userId); + List itemList; + switch (option) { + case NODES: + itemList = restClient.getNodes(); + itemList.sort(nodeHealthComparator); + break; + case DATACENTERS: + itemList = restClient.getDatacenters(); + break; + default: + throw new DashboardControllerException( + "getItemListForPage failed: unimplemented case: " + option.name()); + } + + // Shrink if needed + if (itemList.size() > pageSize) { + itemList = getPageOfList(pageNum, pageSize, itemList); + } + int pageCount = (int) Math.ceil((double) itemList.size() / pageSize); + RestResponsePage model = new RestResponsePage<>(itemList.size(), pageCount, itemList); + return objectMapper.writeValueAsString(model); + } + + /** + * Gets one page of the specified items. This method traps exceptions and + * constructs an appropriate JSON block to report errors. + * + * @param request + * Inbound request + * @param option + * Item type to get + * @return JSON with one page of objects; or an error. + */ + protected String getItemListForPageWrapper(HttpServletRequest request, ConsulDataItem option) { + String outboundJson; + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) { + throw new DashboardControllerException("getItemListForPageWrapper: Failed to get application user"); + } + int pageNum = getRequestPageNumber(request); + int pageSize = getRequestPageSize(request); + outboundJson = getItemListForPage(appUser.getId(), option, pageNum, pageSize); + } catch (Exception ex) { + // Remote service failed; build descriptive error message + logger.error(EELFLoggerDelegate.errorLogger, "getItemListForPageWrapper caught exception", ex); + RestResponseError result = new RestResponseError("Failed to get " + option.name(), ex); + try { + outboundJson = objectMapper.writeValueAsString(result); + } catch (JsonProcessingException jpe) { + // Should never, ever happen + outboundJson = "{ \"error\" : \"" + jpe.toString() + "\"}"; + } + } + return outboundJson; + } + + /** + * Serves all service details. + * + * @param request + * HttpServletRequest + * @return List of ConsulServiceInfo objects, as JSON + * @throws JsonProcessingException + * if serialization fails + */ + @RequestMapping(value = {SERVICES_PATH}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getServices(HttpServletRequest request) throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + Object result; + try { + IControllerRestClient restClient = getControllerRestClient(request); + List itemList = restClient.getServices(); + itemList.sort(serviceInfoComparator); + result = itemList; + } catch (Exception t) { + result = new RestResponseError("getServices failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Serves service health details - not paginated. + * + * @param request + * HttpServletRequest + * @param serviceId + * Service ID + * @return List of ConsulServiceHealth objects as JSON + * @throws JsonProcessingException + * if serialization fails + */ + @RequestMapping(value = { + SERVICES_PATH + "/{serviceId}"}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getServiceHealthDetails(HttpServletRequest request, @PathVariable String serviceId) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + Object result; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.getServiceHealth(serviceId); + } catch (Exception t) { + result = new RestResponseError("getServiceHealthDetails failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Serves service health historical data - not paginated. + * + * @param request + * HttpServletRequest + * @param serviceName + * Service name as path parameter + * @param start + * Earliest date-time as an ISO 8061 value, such as + * 2007-12-03T10:15:30+01:00 + * @param end + * Latest date-time as an ISO 8061 value, such as + * 2007-12-03T10:15:30+01:00 + * @return List of ConsulServiceHealth objects as JSON + * @throws JsonProcessingException + * if serialization fails + */ + @RequestMapping(value = {"/svchist/{serviceName}"}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getServiceHealthHistory(HttpServletRequest request, // + @PathVariable String serviceName, // + @RequestParam String start, // + @RequestParam String end) throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + Object result = null; + try { + Instant startDateTime = Instant.parse(start); + Instant endDateTime = Instant.parse(end); + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.getServiceHealthHistory(serviceName, startDateTime, endDateTime); + } catch (HttpStatusCodeException e) { + // Rare, but can happen + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + // Work around the hack to report no-match. + result = new RestResponseError("getServiceHealthHistory failed: " + t.getMessage()); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Serves one page of service health information by getting all service names, + * then iterating over them to get the health of each service. + * + * ECOMP-C does NOT provide an API to get the health of all services in one + * request. + * + * @param request + * HttpServletRequest + * @return List of ConsulServiceHealth objects, as JSON + * @throws JsonProcessingException + * on serialization exception + */ + @SuppressWarnings("unchecked") + @RequestMapping(value = {"/serviceshealth"}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getServicesHealth(HttpServletRequest request) throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + List itemList = new ArrayList<>(); + IControllerRestClient restClient = getControllerRestClient(request); + List svcInfoList = restClient.getServices(); + for (ConsulServiceInfo csi : svcInfoList) { + List csh = restClient.getServiceHealth(csi.name); + itemList.addAll(csh); + } + itemList.sort(serviceHealthComparator); + // Paginate + final int pageNum = getRequestPageNumber(request); + final int pageSize = getRequestPageSize(request); + final int totalItems = itemList.size(); + final int pageCount = (int) Math.ceil((double) totalItems / pageSize); + // Shrink if needed + if (totalItems > pageSize) { + itemList = getPageOfList(pageNum, pageSize, itemList); + } + result = new RestResponsePage<>(totalItems, pageCount, itemList); + } catch (Exception t) { + result = new RestResponseError("getServicesHealth failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Serves one page of node information. + * + * @param request + * HttpServletRequest + * @return List of ConsulNodeInfo objects, as JSON + */ + @RequestMapping(value = {NODES_PATH}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getNodesInfo(HttpServletRequest request) { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + String json = getItemListForPageWrapper(request, ConsulDataItem.NODES); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return json; + } + + /** + * Serves node services health details - not paginated. + * + * @param request + * HttpServletRequest + * @param nodeName + * Node name + * @return List of ConsulServiceHealth objects as JSON + * @throws JsonProcessingException + * if serialization fails + */ + @RequestMapping(value = {NODES_PATH + "/{nodeName}"}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getNodeServicesHealth(HttpServletRequest request, @PathVariable String nodeName) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + Object result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + result = restClient.getNodeServicesHealth(nodeName); + } catch (Exception t) { + result = new RestResponseError("getNodeServicesHealth failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Serves one page of data centers health. + * + * @param request + * HttpServletRequest + * @return List of ConsulHealthStatus objects + */ + @RequestMapping(value = {"/datacenters"}, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getDatacentersHealth(HttpServletRequest request) { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + String json = getItemListForPageWrapper(request, ConsulDataItem.DATACENTERS); + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return json; + } + + /** + * Processes request to register a service for health checks. + * + * @param request + * HttpServletRequest + * @param registration + * Consul service registration + * @return URI of the newly registered resource + * @throws JsonProcessingException + * on serialization error + */ + @RequestMapping(value = {"/register"}, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String registerService(HttpServletRequest request, @RequestBody ConsulHealthServiceRegistration registration) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + URI uri = restClient.registerService(registration); + result = new RestResponseSuccess(uri.toString()); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("registerService failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } + + /** + * Processes request to deregister a service for health checks. + * + * @param request + * HttpServletRequest + * @param serviceName + * Consul service name to deregister + * @return Success or error indicator + * @throws JsonProcessingException + * on serialization error + */ + @RequestMapping(value = { + "/deregister" + "/{serviceName}"}, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String deregisterService(HttpServletRequest request, @PathVariable String serviceName) + throws JsonProcessingException { + MDC.put(SystemProperties.AUDITLOG_BEGIN_TIMESTAMP, logDateFormat.format(new Date())); + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + ECTransportModel result = null; + try { + IControllerRestClient restClient = getControllerRestClient(request); + int code = restClient.deregisterService(serviceName); + result = new RestResponseSuccess("Deregistration yielded code " + Integer.toString(code)); + } catch (HttpStatusCodeException e) { + result = new RestResponseError(e.getResponseBodyAsString()); + } catch (Exception t) { + result = new RestResponseError("deregisterService failed", t); + } + MDC.put(SystemProperties.AUDITLOG_END_TIMESTAMP, logDateFormat.format(new Date())); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + return objectMapper.writeValueAsString(result); + } +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/DashboardHomeController.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/DashboardHomeController.java index 618e84d..8ec1d2c 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/DashboardHomeController.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/DashboardHomeController.java @@ -1,162 +1,162 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.controller; - -import java.util.ArrayList; - -import javax.servlet.http.HttpServletRequest; - -import org.onap.ccsdk.dashboard.domain.ControllerEndpoint; -import org.onap.ccsdk.dashboard.exception.DashboardControllerException; -import org.onap.ccsdk.dashboard.model.ControllerEndpointCredentials; -import org.onap.ccsdk.dashboard.model.ControllerEndpointTransport; -import org.onap.ccsdk.dashboard.model.RestResponseError; -import org.onap.ccsdk.dashboard.model.RestResponseSuccess; -import org.onap.ccsdk.dashboard.service.ControllerEndpointService; -import org.openecomp.portalsdk.core.domain.User; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.openecomp.portalsdk.core.web.support.UserUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.servlet.ModelAndView; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * This controller maps requests for the application's landing page, which is an - * Angular single-page application. - */ -@Controller -@RequestMapping("/") -public class DashboardHomeController extends DashboardRestrictedBaseController { - - /** - * This path is embedded in the database, so it's nontrivial to change. - */ - public static final String APP_CONTEXT_PATH = "/ecd"; - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardHomeController.class); - - @Autowired - private ControllerEndpointService controllerEndpointService; - - /** - * For general use in these methods - */ - private final ObjectMapper mapper; - - private static final String CONTROLLERS_PATH = "controllers"; - - /** - * Spring autowires fields AFTER the constructor is called. - */ - public DashboardHomeController() { - mapper = new ObjectMapper(); - // Do not serialize null values - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - } - - /** - * @return View name key, which is resolved to a file using an Apache tiles - * "definitions.xml" file. - */ - @RequestMapping(value = { APP_CONTEXT_PATH }, method = RequestMethod.GET) - public ModelAndView dbcDefaultController() { - // a model is only useful for JSP; this app is angular. - return new ModelAndView("oom_home_tdkey"); - } - - /** - * Gets the available controller endpoints. - * - * @param request - * HttpServletRequest - * @return List of ControllerEndpointTransport objects, or an error on failure - */ - @RequestMapping(value = { CONTROLLERS_PATH }, method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public String getControllers(HttpServletRequest request) { - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - String outboundJson = null; - // Static data - ControllerEndpointCredentials[] configured = getControllerEndpoints(); - try { - User appUser = UserUtils.getUserSession(request); - if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) - throw new DashboardControllerException("getControllers: Failed to get application user"); - ControllerEndpointCredentials selectedInDb = getOrSetControllerEndpointSelection(appUser.getId()); - // Built result from properties - ArrayList list = new ArrayList<>(); - for (ControllerEndpointCredentials ctrl : configured) { - // Check if this is the selected endpoint in DB - boolean selected = (selectedInDb != null && selectedInDb.getUrl() != null - && selectedInDb.getUrl().equals(ctrl.getUrl())); - // Result has no privileged information - ControllerEndpointTransport transport = new ControllerEndpointTransport(selected, ctrl.getName(), - ctrl.getUrl()); - list.add(transport); - } - outboundJson = mapper.writeValueAsString(list); - } catch (Exception ex) { - RestResponseError response = new RestResponseError("Failed to get controller endpoint list", ex); - outboundJson = response.toJson(); - } - return outboundJson; - } - - /** - * Sets the controller endpoint selection for the user. - * - * @param request - * HttpServletRequest - * @param endpoint - * Body with endpoint details - * @return Result indicating success or failure - * @throws DashboardControllerException, - * if application user is not found - * @throws JsonProcessingException - * If serialization fails - */ - @RequestMapping(value = { CONTROLLERS_PATH }, method = RequestMethod.POST, produces = "application/json") - @ResponseBody - public String setControllerSelection(HttpServletRequest request, @RequestBody ControllerEndpointTransport endpoint) - throws DashboardControllerException, JsonProcessingException { - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - String outboundJson = null; - User appUser = UserUtils.getUserSession(request); - if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) - throw new DashboardControllerException("setControllerSelection: Failed to get application user"); - ControllerEndpoint dbEntry = new ControllerEndpoint(appUser.getId(), endpoint.getName(), endpoint.getUrl()); - controllerEndpointService.updateControllerEndpointSelection(dbEntry); - RestResponseSuccess success = new RestResponseSuccess("Updated selection to " + endpoint.getName()); - outboundJson = mapper.writeValueAsString(success); - return outboundJson; - } -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.controller; + +import java.util.ArrayList; + +import javax.servlet.http.HttpServletRequest; + +import org.onap.ccsdk.dashboard.domain.ControllerEndpoint; +import org.onap.ccsdk.dashboard.exception.DashboardControllerException; +import org.onap.ccsdk.dashboard.model.ControllerEndpointCredentials; +import org.onap.ccsdk.dashboard.model.ControllerEndpointTransport; +import org.onap.ccsdk.dashboard.model.RestResponseError; +import org.onap.ccsdk.dashboard.model.RestResponseSuccess; +import org.onap.ccsdk.dashboard.service.ControllerEndpointService; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * This controller maps requests for the application's landing page, which is an + * Angular single-page application. + */ +@Controller +@RequestMapping("/") +public class DashboardHomeController extends DashboardRestrictedBaseController { + + /** + * This path is embedded in the database, so it's nontrivial to change. + */ + public static final String APP_CONTEXT_PATH = "/ecd"; + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardHomeController.class); + + @Autowired + private ControllerEndpointService controllerEndpointService; + + /** + * For general use in these methods + */ + private final ObjectMapper mapper; + + private static final String CONTROLLERS_PATH = "controllers"; + + /** + * Spring autowires fields AFTER the constructor is called. + */ + public DashboardHomeController() { + mapper = new ObjectMapper(); + // Do not serialize null values + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + + /** + * @return View name key, which is resolved to a file using an Apache tiles + * "definitions.xml" file. + */ + @RequestMapping(value = { APP_CONTEXT_PATH }, method = RequestMethod.GET) + public ModelAndView dbcDefaultController() { + // a model is only useful for JSP; this app is angular. + return new ModelAndView("oom_home_tdkey"); + } + + /** + * Gets the available controller endpoints. + * + * @param request + * HttpServletRequest + * @return List of ControllerEndpointTransport objects, or an error on failure + */ + @RequestMapping(value = { CONTROLLERS_PATH }, method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public String getControllers(HttpServletRequest request) { + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + String outboundJson = null; + // Static data + ControllerEndpointCredentials[] configured = getControllerEndpoints(); + try { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new DashboardControllerException("getControllers: Failed to get application user"); + ControllerEndpointCredentials selectedInDb = getOrSetControllerEndpointSelection(appUser.getId()); + // Built result from properties + ArrayList list = new ArrayList<>(); + for (ControllerEndpointCredentials ctrl : configured) { + // Check if this is the selected endpoint in DB + boolean selected = (selectedInDb != null && selectedInDb.getUrl() != null + && selectedInDb.getUrl().equals(ctrl.getUrl())); + // Result has no privileged information + ControllerEndpointTransport transport = new ControllerEndpointTransport(selected, ctrl.getName(), + ctrl.getUrl()); + list.add(transport); + } + outboundJson = mapper.writeValueAsString(list); + } catch (Exception ex) { + RestResponseError response = new RestResponseError("Failed to get controller endpoint list", ex); + outboundJson = response.toJson(); + } + return outboundJson; + } + + /** + * Sets the controller endpoint selection for the user. + * + * @param request + * HttpServletRequest + * @param endpoint + * Body with endpoint details + * @return Result indicating success or failure + * @throws DashboardControllerException, + * if application user is not found + * @throws JsonProcessingException + * If serialization fails + */ + @RequestMapping(value = { CONTROLLERS_PATH }, method = RequestMethod.POST, produces = "application/json") + @ResponseBody + public String setControllerSelection(HttpServletRequest request, @RequestBody ControllerEndpointTransport endpoint) + throws DashboardControllerException, JsonProcessingException { + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, APP_NAME); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + String outboundJson = null; + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new DashboardControllerException("setControllerSelection: Failed to get application user"); + ControllerEndpoint dbEntry = new ControllerEndpoint(appUser.getId(), endpoint.getName(), endpoint.getUrl()); + controllerEndpointService.updateControllerEndpointSelection(dbEntry); + RestResponseSuccess success = new RestResponseSuccess("Updated selection to " + endpoint.getName()); + outboundJson = mapper.writeValueAsString(success); + return outboundJson; + } +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/DashboardRestrictedBaseController.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/DashboardRestrictedBaseController.java index 6f80d22..02f8770 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/DashboardRestrictedBaseController.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/DashboardRestrictedBaseController.java @@ -1,275 +1,275 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.controller; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; - -import org.onap.ccsdk.dashboard.exception.DashboardControllerException; -import org.onap.ccsdk.dashboard.rest.ControllerRestClientMockImpl; -import org.onap.ccsdk.dashboard.service.ControllerEndpointService; -import org.onap.ccsdk.dashboard.domain.ControllerEndpoint; -import org.onap.ccsdk.dashboard.rest.IControllerRestClient; -import org.onap.ccsdk.dashboard.util.DashboardProperties; -import org.onap.ccsdk.dashboard.model.ControllerEndpointCredentials; -import org.onap.ccsdk.dashboard.rest.ControllerRestClientImpl; -import org.openecomp.portalsdk.core.controller.RestrictedBaseController; -import org.openecomp.portalsdk.core.domain.User; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.openecomp.portalsdk.core.web.support.UserUtils; -import org.springframework.beans.factory.annotation.Autowired; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * This base class provides utility methods to child controllers. - */ -public class DashboardRestrictedBaseController extends RestrictedBaseController { - - /** - * Logger that conforms with ECOMP guidelines - */ - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardRestrictedBaseController.class); - - /** - * Application name - */ - protected static final String APP_NAME = "ecd-app"; - - /** - * EELF-approved format - */ - protected static final DateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); - - /** - * Query parameter for desired page number - */ - protected static final String PAGE_NUM_QUERY_PARAM = "pageNum"; - - /** - * Query parameter for desired items per page - */ - protected static final String PAGE_SIZE_QUERY_PARAM = "viewPerPage"; - - /** - * For general use in these methods and subclasses - */ - protected final ObjectMapper objectMapper = new ObjectMapper(); - - /** - * Application properties - NOT available to constructor. - */ - @Autowired - private DashboardProperties appProperties; - - /** - * For getting selected controller - */ - @Autowired - private ControllerEndpointService controllerEndpointService; - - /** - * Hello Spring, here's your no-arg constructor. - */ - public DashboardRestrictedBaseController() { - // Do not serialize null values - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - } - - /** - * Access method for subclasses. - * - * @return DbcappProperties object that was autowired by Spring. - */ - protected DashboardProperties getAppProperties() { - return appProperties; - } - - /** - * Gets the requested page number from a query parameter in the - * HttpServletRequest. Defaults to 1, which is useful to allow manual - * testing of endpoints without supplying those pesky parameters. - * - * @param request - * HttpServletRequest - * @return Value of query parameter {@link #PAGE_NUM_QUERY_PARAM}; 1 if not - * found. - */ - protected int getRequestPageNumber(HttpServletRequest request) { - int pageNum = 1; - String param = request.getParameter(PAGE_NUM_QUERY_PARAM); - if (param != null) - pageNum = Integer.parseInt(param); - return pageNum; - } - - /** - * Gets the requested page size from a query parameter in the - * HttpServletRequest. Defaults to 50, which is useful to allow manual - * testing of endpoints without supplying those pesky parameters. - * - * @param request - * HttpServletRequest - * @return Value of query parameter {@link #PAGE_SIZE_QUERY_PARAM}; 50 if - * not found. - */ - protected int getRequestPageSize(HttpServletRequest request) { - int pageSize = 50; - String param = request.getParameter(PAGE_SIZE_QUERY_PARAM); - if (param != null) - pageSize = Integer.parseInt(param); - return pageSize; - } - - /** - * Gets the items for the specified page from the specified list. - * - * @param pageNum - * Page number requested by user, indexed from 1 - * @param pageSize - * Number of items per page - * @param itemList - * List of items to adjust - * @return List of items; empty list if from==to - */ - @SuppressWarnings("rawtypes") - protected static List getPageOfList(final int pageNum, final int pageSize, final List itemList) { - int firstIndexOnThisPage = pageSize * (pageNum - 1); - int firstIndexOnNextPage = pageSize * pageNum; - int fromIndex = firstIndexOnThisPage < itemList.size() ? firstIndexOnThisPage : itemList.size(); - int toIndex = firstIndexOnNextPage < itemList.size() ? firstIndexOnNextPage : itemList.size(); - return itemList.subList(fromIndex, toIndex); - } - - /** - * Gets all configured controllers from properties. - * - * @return Array of ControllerEndpointRestricted objects - * @throws IllegalStateException - * if a required property is not found - */ - protected ControllerEndpointCredentials[] getControllerEndpoints() { - final String[] controllerKeys = appProperties.getCsvListProperty(DashboardProperties.CONTROLLER_KEY_LIST); - ControllerEndpointCredentials[] controllers = new ControllerEndpointCredentials[controllerKeys.length]; - for (int i = 0; i < controllerKeys.length; ++i) { - String key = controllerKeys[i]; - final String name = appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_NAME); - final String url = appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_URL); - final String user = appProperties.getControllerProperty(key, - DashboardProperties.CONTROLLER_SUBKEY_USERNAME); - final String pass = appProperties.getControllerProperty(key, - DashboardProperties.CONTROLLER_SUBKEY_PASSWORD); - final boolean encr = Boolean.parseBoolean ( - appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_ENCRYPTED)); - logger.debug(EELFLoggerDelegate.debugLogger, "getConfiguredControllers: key {} yields url {}", key, url); - controllers[i] = new ControllerEndpointCredentials(false, name, url, user, pass, encr); - } - return controllers; - } - - /** - * Gets the controller endpoint for the specified user ID. Chooses the first - * one from properties if the user has not selected one previously. - * - * @param userId - * Database User ID - * @return ControllerEndpointCredentials for the specified user - */ - protected ControllerEndpointCredentials getOrSetControllerEndpointSelection(long userId) { - // Always need the complete list from properties - ControllerEndpointCredentials[] configured = getControllerEndpoints(); - // See if the database has an entry for this user - ControllerEndpoint dbEntry = controllerEndpointService.getControllerEndpointSelection(userId); - // If no row found DAO returns an object with null entries. - if (dbEntry == null || dbEntry.getName() == null) { - // Arbitrarily choose the first one - ControllerEndpointCredentials first = configured[0]; - dbEntry = new ControllerEndpoint(userId, first.getName(), first.getUrl()); - controllerEndpointService.updateControllerEndpointSelection(dbEntry); - } - // Fetch complete details for the selected item - ControllerEndpointCredentials selected = null; - for (ControllerEndpointCredentials cec : configured) { - if (dbEntry.getUrl().equals(cec.getUrl())) { - selected = cec; - break; - } - } - // Defend against a stale database entry. - if (selected == null) { - selected = configured[0]; - dbEntry = new ControllerEndpoint(userId, selected.getName(), selected.getUrl()); - controllerEndpointService.updateControllerEndpointSelection(dbEntry); - } - return selected; - } - - /** - * Convenience method that gets the user ID from the session and fetches the - * REST client. Factors code out of subclass methods. - * - * @param request - * HttpServletRequest - * @return REST client appropriate for the user - * @throws DashboardControllerException - */ - protected IControllerRestClient getControllerRestClient(HttpServletRequest request) throws DashboardControllerException { - User appUser = UserUtils.getUserSession(request); - if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) - throw new DashboardControllerException("getControllerRestClient: Failed to get application user"); - return getControllerRestClient(appUser.getId()); - } - - /** - * Gets a REST client; either a mock client (returns canned data), or a real - * client with appropriate credentials from properties. - * - * @return REST client. - * @throws DashboardControllerException on any failure; e.g., if the password cannot be decrypted. - */ - protected IControllerRestClient getControllerRestClient(long userId) throws DashboardControllerException { - IControllerRestClient result = null; - // Be robust to missing development-only property - boolean mock = false; - if (appProperties.containsProperty(DashboardProperties.CONTROLLER_MOCK_DATA)) - mock = appProperties.getBooleanProperty(DashboardProperties.CONTROLLER_MOCK_DATA); - if (mock) { - result = new ControllerRestClientMockImpl(); - } else { - try { - ControllerEndpointCredentials details = getOrSetControllerEndpointSelection(userId); - final String clearText = details.getEncryptedPassword() ? details.decryptPassword() : details.getPassword(); - result = new ControllerRestClientImpl(details.getUrl(), details.getUsername(), clearText); - } - catch (Exception ex) { - logger.error("getControllerRestClient failed", ex); - throw new DashboardControllerException(ex); - } - } - return result; - } - -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.controller; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.onap.ccsdk.dashboard.exception.DashboardControllerException; +import org.onap.ccsdk.dashboard.rest.ControllerRestClientMockImpl; +import org.onap.ccsdk.dashboard.service.ControllerEndpointService; +import org.onap.ccsdk.dashboard.domain.ControllerEndpoint; +import org.onap.ccsdk.dashboard.rest.IControllerRestClient; +import org.onap.ccsdk.dashboard.util.DashboardProperties; +import org.onap.ccsdk.dashboard.model.ControllerEndpointCredentials; +import org.onap.ccsdk.dashboard.rest.ControllerRestClientImpl; +import org.onap.portalsdk.core.controller.RestrictedBaseController; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.beans.factory.annotation.Autowired; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * This base class provides utility methods to child controllers. + */ +public class DashboardRestrictedBaseController extends RestrictedBaseController { + + /** + * Logger that conforms with ECOMP guidelines + */ + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardRestrictedBaseController.class); + + /** + * Application name + */ + protected static final String APP_NAME = "ecd-app"; + + /** + * EELF-approved format + */ + protected static final DateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + + /** + * Query parameter for desired page number + */ + protected static final String PAGE_NUM_QUERY_PARAM = "pageNum"; + + /** + * Query parameter for desired items per page + */ + protected static final String PAGE_SIZE_QUERY_PARAM = "viewPerPage"; + + /** + * For general use in these methods and subclasses + */ + protected final ObjectMapper objectMapper = new ObjectMapper(); + + /** + * Application properties - NOT available to constructor. + */ + @Autowired + private DashboardProperties appProperties; + + /** + * For getting selected controller + */ + @Autowired + private ControllerEndpointService controllerEndpointService; + + /** + * Hello Spring, here's your no-arg constructor. + */ + public DashboardRestrictedBaseController() { + // Do not serialize null values + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + + /** + * Access method for subclasses. + * + * @return DbcappProperties object that was autowired by Spring. + */ + protected DashboardProperties getAppProperties() { + return appProperties; + } + + /** + * Gets the requested page number from a query parameter in the + * HttpServletRequest. Defaults to 1, which is useful to allow manual + * testing of endpoints without supplying those pesky parameters. + * + * @param request + * HttpServletRequest + * @return Value of query parameter {@link #PAGE_NUM_QUERY_PARAM}; 1 if not + * found. + */ + protected int getRequestPageNumber(HttpServletRequest request) { + int pageNum = 1; + String param = request.getParameter(PAGE_NUM_QUERY_PARAM); + if (param != null) + pageNum = Integer.parseInt(param); + return pageNum; + } + + /** + * Gets the requested page size from a query parameter in the + * HttpServletRequest. Defaults to 50, which is useful to allow manual + * testing of endpoints without supplying those pesky parameters. + * + * @param request + * HttpServletRequest + * @return Value of query parameter {@link #PAGE_SIZE_QUERY_PARAM}; 50 if + * not found. + */ + protected int getRequestPageSize(HttpServletRequest request) { + int pageSize = 50; + String param = request.getParameter(PAGE_SIZE_QUERY_PARAM); + if (param != null) + pageSize = Integer.parseInt(param); + return pageSize; + } + + /** + * Gets the items for the specified page from the specified list. + * + * @param pageNum + * Page number requested by user, indexed from 1 + * @param pageSize + * Number of items per page + * @param itemList + * List of items to adjust + * @return List of items; empty list if from==to + */ + @SuppressWarnings("rawtypes") + protected static List getPageOfList(final int pageNum, final int pageSize, final List itemList) { + int firstIndexOnThisPage = pageSize * (pageNum - 1); + int firstIndexOnNextPage = pageSize * pageNum; + int fromIndex = firstIndexOnThisPage < itemList.size() ? firstIndexOnThisPage : itemList.size(); + int toIndex = firstIndexOnNextPage < itemList.size() ? firstIndexOnNextPage : itemList.size(); + return itemList.subList(fromIndex, toIndex); + } + + /** + * Gets all configured controllers from properties. + * + * @return Array of ControllerEndpointRestricted objects + * @throws IllegalStateException + * if a required property is not found + */ + protected ControllerEndpointCredentials[] getControllerEndpoints() { + final String[] controllerKeys = appProperties.getCsvListProperty(DashboardProperties.CONTROLLER_KEY_LIST); + ControllerEndpointCredentials[] controllers = new ControllerEndpointCredentials[controllerKeys.length]; + for (int i = 0; i < controllerKeys.length; ++i) { + String key = controllerKeys[i]; + final String name = appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_NAME); + final String url = appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_URL); + final String user = appProperties.getControllerProperty(key, + DashboardProperties.CONTROLLER_SUBKEY_USERNAME); + final String pass = appProperties.getControllerProperty(key, + DashboardProperties.CONTROLLER_SUBKEY_PASSWORD); + final boolean encr = Boolean.parseBoolean ( + appProperties.getControllerProperty(key, DashboardProperties.CONTROLLER_SUBKEY_ENCRYPTED)); + logger.debug(EELFLoggerDelegate.debugLogger, "getConfiguredControllers: key {} yields url {}", key, url); + controllers[i] = new ControllerEndpointCredentials(false, name, url, user, pass, encr); + } + return controllers; + } + + /** + * Gets the controller endpoint for the specified user ID. Chooses the first + * one from properties if the user has not selected one previously. + * + * @param userId + * Database User ID + * @return ControllerEndpointCredentials for the specified user + */ + protected ControllerEndpointCredentials getOrSetControllerEndpointSelection(long userId) { + // Always need the complete list from properties + ControllerEndpointCredentials[] configured = getControllerEndpoints(); + // See if the database has an entry for this user + ControllerEndpoint dbEntry = controllerEndpointService.getControllerEndpointSelection(userId); + // If no row found DAO returns an object with null entries. + if (dbEntry == null || dbEntry.getName() == null) { + // Arbitrarily choose the first one + ControllerEndpointCredentials first = configured[0]; + dbEntry = new ControllerEndpoint(userId, first.getName(), first.getUrl()); + controllerEndpointService.updateControllerEndpointSelection(dbEntry); + } + // Fetch complete details for the selected item + ControllerEndpointCredentials selected = null; + for (ControllerEndpointCredentials cec : configured) { + if (dbEntry.getUrl().equals(cec.getUrl())) { + selected = cec; + break; + } + } + // Defend against a stale database entry. + if (selected == null) { + selected = configured[0]; + dbEntry = new ControllerEndpoint(userId, selected.getName(), selected.getUrl()); + controllerEndpointService.updateControllerEndpointSelection(dbEntry); + } + return selected; + } + + /** + * Convenience method that gets the user ID from the session and fetches the + * REST client. Factors code out of subclass methods. + * + * @param request + * HttpServletRequest + * @return REST client appropriate for the user + * @throws DashboardControllerException + */ + protected IControllerRestClient getControllerRestClient(HttpServletRequest request) throws DashboardControllerException { + User appUser = UserUtils.getUserSession(request); + if (appUser == null || appUser.getLoginId() == null || appUser.getLoginId().length() == 0) + throw new DashboardControllerException("getControllerRestClient: Failed to get application user"); + return getControllerRestClient(appUser.getId()); + } + + /** + * Gets a REST client; either a mock client (returns canned data), or a real + * client with appropriate credentials from properties. + * + * @return REST client. + * @throws DashboardControllerException on any failure; e.g., if the password cannot be decrypted. + */ + protected IControllerRestClient getControllerRestClient(long userId) throws DashboardControllerException { + IControllerRestClient result = null; + // Be robust to missing development-only property + boolean mock = false; + if (appProperties.containsProperty(DashboardProperties.CONTROLLER_MOCK_DATA)) + mock = appProperties.getBooleanProperty(DashboardProperties.CONTROLLER_MOCK_DATA); + if (mock) { + result = new ControllerRestClientMockImpl(); + } else { + try { + ControllerEndpointCredentials details = getOrSetControllerEndpointSelection(userId); + final String clearText = details.getEncryptedPassword() ? details.decryptPassword() : details.getPassword(); + result = new ControllerRestClientImpl(details.getUrl(), details.getUsername(), clearText); + } + catch (Exception ex) { + logger.error("getControllerRestClient failed", ex); + throw new DashboardControllerException(ex); + } + } + return result; + } + +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/ECDSingleSignOnController.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/ECDSingleSignOnController.java index 953adff..28f939c 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/ECDSingleSignOnController.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/ECDSingleSignOnController.java @@ -1,290 +1,299 @@ -/*- - * ================================================================================ - * ECOMP Portal SDK - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ================================================================================ - */ - -package org.onap.ccsdk.dashboard.controller; - -import java.io.UnsupportedEncodingException; - -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; - -import org.onap.ccsdk.dashboard.exception.DashboardControllerException; -import org.openecomp.portalsdk.core.auth.LoginStrategy; -import org.openecomp.portalsdk.core.command.LoginBean; -import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController; -import org.openecomp.portalsdk.core.domain.User; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.openecomp.portalsdk.core.menu.MenuProperties; -import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException; -import org.openecomp.portalsdk.core.onboarding.listener.PortalTimeoutHandler; -import org.openecomp.portalsdk.core.onboarding.util.PortalApiConstants; -import org.openecomp.portalsdk.core.onboarding.util.PortalApiProperties; -import org.openecomp.portalsdk.core.service.LoginService; -import org.openecomp.portalsdk.core.util.SystemProperties; -import org.openecomp.portalsdk.core.web.support.AppUtils; -import org.openecomp.portalsdk.core.web.support.UserUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.client.RestClientException; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.util.WebUtils; - -@Controller -@RequestMapping("/") -/** - * Replicated from - * org.openecomp.portalapp.controller.core.SingleSignOnController to modify the - * behavior of sending user's browser on a detour of Portal app to get the - * EPService cookie. - */ -public class ECDSingleSignOnController extends UnRestrictedBaseController { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ECDSingleSignOnController.class); - private static final String REDIRECT = "redirect:"; - - @Autowired - private LoginService loginService; - - @Autowired - private LoginStrategy loginStrategy; - - private String viewName; - private String welcomeView; - - /** - * Handles requests directed to the single sign-on page by the session timeout - * interceptor. - * - * @param request - * HttpServletRequest - * @param response - * HttpServletResponse - * @return Redirect to an appropriate address - * @throws DashboardControllerException - * User not found - * @throws PortalAPIException - * User ID can't be fetched - * @throws UnsupportedEncodingException - * Encoding fails - */ - @RequestMapping(value = { "/single_signon.htm" }, method = RequestMethod.GET) - public ModelAndView singleSignOnLogin(HttpServletRequest request, HttpServletResponse response) - throws DashboardControllerException, PortalAPIException, UnsupportedEncodingException { - - Map model = new HashMap<>(); - HashMap additionalParamsMap = new HashMap<>(); - LoginBean commandBean = new LoginBean(); - - // SessionTimeoutInterceptor sets these parameters - String forwardURL = URLDecoder.decode(request.getParameter("forwardURL"), "UTF-8"); - String redirectToPortal = request.getParameter("redirectToPortal"); - - if (isLoginCookieExist(request) && redirectToPortal == null) { - HttpSession session = null; - session = AppUtils.getSession(request); - User user = UserUtils.getUserSession(request); - if (session == null || user == null) { - - final String authMech = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM); - String userId = loginStrategy.getUserId(request); - commandBean.setUserid(userId); - try { - commandBean = getLoginService().findUser(commandBean, - (String) request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY), - additionalParamsMap); - } catch (Exception ex) { - logger.error("singleSignOnLogin failed", ex); - throw new DashboardControllerException(ex); - } - if (commandBean.getUser() == null) { - String loginErrorMessage = (commandBean.getLoginErrorMessage() != null) - ? commandBean.getLoginErrorMessage() - : SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_NOT_FOUND; - model.put(LoginStrategy.ERROR_MESSAGE_KEY, SystemProperties.getProperty(loginErrorMessage)); - final String redirectUrl = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL) - + "?noUserError=Yes"; - logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: user is null, redirect URL is {}", - redirectUrl); - return new ModelAndView(REDIRECT + redirectUrl); - } else { - // store the user's information in the session - String loginMethod; - if (null == authMech || "".equals(authMech) || "BOTH".equals(authMech)) { - loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_CSP); - } else if ("CSP".equals(authMech)) { - loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_CSP); - } else { - loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_WEB_JUNCTION); - } - UserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), - commandBean.getBusinessDirectMenu(), loginMethod); - initateSessionMgtHandler(request); - logger.debug(EELFLoggerDelegate.debugLogger, - "singleSignOnLogin: create new user session for expired user {}; user {} exists in the system", - userId, commandBean.getUser().getOrgUserId()); - return new ModelAndView(REDIRECT + forwardURL); - } - } // user is null or session is null - else { - // both user and session are non-null. - logger.info(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: redirecting to the forwardURL {}", - forwardURL); - return new ModelAndView(REDIRECT + forwardURL); - } - } else { - /* - * Login cookie not found, or redirect-to-portal parameter was found. - */ - if (isPortalAvailable()) { - /* - * Redirect the user to the portal with a suitable return URL. The forwardURL - * parameter that arrives as a parameter is a partial (not absolute) request - * path for a page in the application. The challenge here is to compute the - * correct absolute path for the original request so the portal can redirect the - * user back to the right place. If the application sits behind WebJunction, or - * if separate FE-BE hosts are used, then the URL yielded by the request has a - * host name that is not reachable by the user. - */ - String returnToAppUrl = null; - if (SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)) { - // New feature as of 1610, release 3.3.3: - // application can publish a base URL in system.properties - String appUrl = SystemProperties.getProperty(SystemProperties.APP_BASE_URL); - returnToAppUrl = appUrl + (appUrl.endsWith("/") ? "" : "/") + forwardURL; - logger.debug(EELFLoggerDelegate.debugLogger, - "singleSignOnLogin: using app base URL {} and redirectURL {}", appUrl, returnToAppUrl); - } else { - /** - * Be backward compatible with applications that don't need this feature. This - * is the controller for the single_signon.htm page, so the replace should - * always find the specified token. - */ - returnToAppUrl = request.getRequestURL().toString() - .replace("single_signon.htm", forwardURL); - logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: computed redirectURL {}", - returnToAppUrl); - } - final String encodedReturnToAppUrl = URLEncoder.encode(returnToAppUrl, "UTF-8"); - // Also send the application's UEB key so Portal can block URL - // reflection attacks. - final String uebAppKey = PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY); - final String url = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL); - final String portalUrl = url.substring(0, url.lastIndexOf('/')) + "/process_csp"; - final String redirectUrl = portalUrl + "?uebAppKey=" + uebAppKey + "&redirectUrl=" - + encodedReturnToAppUrl; - logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: portal-bound redirect URL is {}", - redirectUrl); - return new ModelAndView(REDIRECT + redirectUrl); - } // portal is available - - else { - /* - * Portal is not available. Redirect user to the login page, ignoring the - * forwardURL parameter. - */ - return new ModelAndView("redirect:login.htm"); - } - - } - } - - /** - * Discover if the portal is available by GET-ing a resource from the REST URL - * specified in portal.properties, using a very short timeout. - * - * @return True if the portal answers, otherwise false. - */ - private boolean isPortalAvailable() { - HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); - final int oneSecond = 1000; - httpRequestFactory.setConnectionRequestTimeout(oneSecond); - httpRequestFactory.setConnectTimeout(oneSecond); - httpRequestFactory.setReadTimeout(oneSecond); - RestTemplate restTemplate = new RestTemplate(httpRequestFactory); - boolean avail = true; - try { - final String portalUrl = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL); - String s = restTemplate.getForObject(portalUrl, String.class); - logger.trace("isPortalAvailable got response {}", s); - } catch (RestClientException ex) { - logger.debug("isPortalAvailable failed", ex); - avail = false; - } - return avail; - } - - protected void initateSessionMgtHandler(HttpServletRequest request) { - String portalJSessionId = getPortalJSessionId(request); - String jSessionId = getJessionId(request); - PortalTimeoutHandler.sessionCreated(portalJSessionId, jSessionId, AppUtils.getSession(request)); - } - - public boolean isLoginCookieExist(HttpServletRequest request) { - Cookie ep = WebUtils.getCookie(request, LoginStrategy.EP_SERVICE); - return (ep != null); - } - - public String getPortalJSessionId(HttpServletRequest request) { - Cookie ep = WebUtils.getCookie(request, LoginStrategy.EP_SERVICE); - return ep.getValue(); - } - - public String getJessionId(HttpServletRequest request) { - return request.getSession().getId(); - } - - @Override - public String getViewName() { - return viewName; - } - - @Override - public void setViewName(String viewName) { - this.viewName = viewName; - } - - public String getWelcomeView() { - return welcomeView; - } - - public void setWelcomeView(String welcomeView) { - this.welcomeView = welcomeView; - } - - public LoginService getLoginService() { - return loginService; - } - - public void setLoginService(LoginService loginService) { - this.loginService = loginService; - } - -} +/*- + * ================================================================================ + * ECOMP Portal SDK + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ================================================================================ + */ + +package org.onap.ccsdk.dashboard.controller; + +import java.io.UnsupportedEncodingException; + +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.onap.ccsdk.dashboard.exception.DashboardControllerException; +import org.onap.portalsdk.core.auth.LoginStrategy; +import org.onap.portalsdk.core.command.LoginBean; +import org.onap.portalsdk.core.controller.UnRestrictedBaseController; +import org.onap.portalsdk.core.domain.User; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.menu.MenuProperties; +import org.onap.portalsdk.core.onboarding.exception.PortalAPIException; +import org.onap.portalsdk.core.onboarding.listener.PortalTimeoutHandler; +import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; +import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; +import org.onap.portalsdk.core.service.LoginService; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.portalsdk.core.web.support.AppUtils; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.onap.portalsdk.core.service.RoleService; +import org.onap.portalsdk.core.domain.RoleFunction; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.util.WebUtils; + +@Controller +@RequestMapping("/") +/** + * Replicated from + * org.onap.portalapp.controller.core.SingleSignOnController to modify the + * behavior of sending user's browser on a detour of Portal app to get the + * EPService cookie. + */ +public class ECDSingleSignOnController extends UnRestrictedBaseController { + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ECDSingleSignOnController.class); + private static final String REDIRECT = "redirect:"; + + @Autowired + private LoginService loginService; + + @Autowired + private LoginStrategy loginStrategy; + + @Autowired + private RoleService roleService; + + private String viewName; + private String welcomeView; + + /** + * Handles requests directed to the single sign-on page by the session timeout + * interceptor. + * + * @param request + * HttpServletRequest + * @param response + * HttpServletResponse + * @return Redirect to an appropriate address + * @throws DashboardControllerException + * User not found + * @throws PortalAPIException + * User ID can't be fetched + * @throws UnsupportedEncodingException + * Encoding fails + */ + @RequestMapping(value = { "/single_signon.htm" }, method = RequestMethod.GET) + public ModelAndView singleSignOnLogin(HttpServletRequest request, HttpServletResponse response) throws Exception { + //throws DashboardControllerException, PortalAPIException, UnsupportedEncodingException { + + Map model = new HashMap<>(); + HashMap additionalParamsMap = new HashMap<>(); + LoginBean commandBean = new LoginBean(); + + // SessionTimeoutInterceptor sets these parameters + String forwardURL = URLDecoder.decode(request.getParameter("forwardURL"), "UTF-8"); + String redirectToPortal = request.getParameter("redirectToPortal"); + + if (isLoginCookieExist(request) && redirectToPortal == null) { + HttpSession session = null; + session = AppUtils.getSession(request); + User user = UserUtils.getUserSession(request); + if (session == null || user == null) { + + final String authMech = SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM); + String userId = loginStrategy.getUserId(request); + commandBean.setUserid(userId); + commandBean = getLoginService().findUser(commandBean, + (String) request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY), additionalParamsMap); + List roleFunctionList = roleService.getRoleFunctions(userId); + try { + commandBean = getLoginService().findUser(commandBean, + (String) request.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY), + additionalParamsMap); + } catch (Exception ex) { + logger.error("singleSignOnLogin failed", ex); + throw new DashboardControllerException(ex); + } + if (commandBean.getUser() == null) { + String loginErrorMessage = (commandBean.getLoginErrorMessage() != null) + ? commandBean.getLoginErrorMessage() + : SystemProperties.MESSAGE_KEY_LOGIN_ERROR_USER_NOT_FOUND; + model.put(LoginStrategy.ERROR_MESSAGE_KEY, SystemProperties.getProperty(loginErrorMessage)); + final String redirectUrl = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL) + + "?noUserError=Yes"; + logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: user is null, redirect URL is {}", + redirectUrl); + return new ModelAndView(REDIRECT + redirectUrl); + } else { + // store the user's information in the session + String loginMethod; + if (null == authMech || "".equals(authMech) || "BOTH".equals(authMech)) { + loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_CSP); + } else if ("CSP".equals(authMech)) { + loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_CSP); + } else { + loginMethod = SystemProperties.getProperty(SystemProperties.LOGIN_METHOD_WEB_JUNCTION); + } + UserUtils.setUserSession(request, commandBean.getUser(), commandBean.getMenu(), + commandBean.getBusinessDirectMenu(), loginMethod, roleFunctionList); + initateSessionMgtHandler(request); + logger.debug(EELFLoggerDelegate.debugLogger, + "singleSignOnLogin: create new user session for expired user {}; user {} exists in the system", + userId, commandBean.getUser().getOrgUserId()); + return new ModelAndView(REDIRECT + forwardURL); + } + } // user is null or session is null + else { + // both user and session are non-null. + logger.info(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: redirecting to the forwardURL {}", + forwardURL); + return new ModelAndView(REDIRECT + forwardURL); + } + } else { + /* + * Login cookie not found, or redirect-to-portal parameter was found. + */ + if (isPortalAvailable()) { + /* + * Redirect the user to the portal with a suitable return URL. The forwardURL + * parameter that arrives as a parameter is a partial (not absolute) request + * path for a page in the application. The challenge here is to compute the + * correct absolute path for the original request so the portal can redirect the + * user back to the right place. If the application sits behind WebJunction, or + * if separate FE-BE hosts are used, then the URL yielded by the request has a + * host name that is not reachable by the user. + */ + String returnToAppUrl = null; + if (SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)) { + // New feature as of 1610, release 3.3.3: + // application can publish a base URL in system.properties + String appUrl = SystemProperties.getProperty(SystemProperties.APP_BASE_URL); + returnToAppUrl = appUrl + (appUrl.endsWith("/") ? "" : "/") + forwardURL; + logger.debug(EELFLoggerDelegate.debugLogger, + "singleSignOnLogin: using app base URL {} and redirectURL {}", appUrl, returnToAppUrl); + } else { + /** + * Be backward compatible with applications that don't need this feature. This + * is the controller for the single_signon.htm page, so the replace should + * always find the specified token. + */ + returnToAppUrl = request.getRequestURL().toString() + .replace("single_signon.htm", forwardURL); + logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: computed redirectURL {}", + returnToAppUrl); + } + final String encodedReturnToAppUrl = URLEncoder.encode(returnToAppUrl, "UTF-8"); + // Also send the application's UEB key so Portal can block URL + // reflection attacks. + final String uebAppKey = PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY); + final String url = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL); + final String portalUrl = url.substring(0, url.lastIndexOf('/')) + "/process_csp"; + final String redirectUrl = portalUrl + "?uebAppKey=" + uebAppKey + "&redirectUrl=" + + encodedReturnToAppUrl; + logger.debug(EELFLoggerDelegate.debugLogger, "singleSignOnLogin: portal-bound redirect URL is {}", + redirectUrl); + return new ModelAndView(REDIRECT + redirectUrl); + } // portal is available + + else { + /* + * Portal is not available. Redirect user to the login page, ignoring the + * forwardURL parameter. + */ + return new ModelAndView("redirect:login.htm"); + } + + } + } + + /** + * Discover if the portal is available by GET-ing a resource from the REST URL + * specified in portal.properties, using a very short timeout. + * + * @return True if the portal answers, otherwise false. + */ + private boolean isPortalAvailable() { + HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory(); + final int oneSecond = 1000; + httpRequestFactory.setConnectionRequestTimeout(oneSecond); + httpRequestFactory.setConnectTimeout(oneSecond); + httpRequestFactory.setReadTimeout(oneSecond); + RestTemplate restTemplate = new RestTemplate(httpRequestFactory); + boolean avail = true; + try { + final String portalUrl = PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL); + String s = restTemplate.getForObject(portalUrl, String.class); + logger.trace("isPortalAvailable got response {}", s); + } catch (RestClientException ex) { + logger.debug("isPortalAvailable failed", ex); + avail = false; + } + return avail; + } + + protected void initateSessionMgtHandler(HttpServletRequest request) { + String portalJSessionId = getPortalJSessionId(request); + String jSessionId = getJessionId(request); + PortalTimeoutHandler.sessionCreated(portalJSessionId, jSessionId, AppUtils.getSession(request)); + } + + public boolean isLoginCookieExist(HttpServletRequest request) { + Cookie ep = WebUtils.getCookie(request, LoginStrategy.EP_SERVICE); + return (ep != null); + } + + public String getPortalJSessionId(HttpServletRequest request) { + Cookie ep = WebUtils.getCookie(request, LoginStrategy.EP_SERVICE); + return ep.getValue(); + } + + public String getJessionId(HttpServletRequest request) { + return request.getSession().getId(); + } + + @Override + public String getViewName() { + return viewName; + } + + @Override + public void setViewName(String viewName) { + this.viewName = viewName; + } + + public String getWelcomeView() { + return welcomeView; + } + + public void setWelcomeView(String welcomeView) { + this.welcomeView = welcomeView; + } + + public LoginService getLoginService() { + return loginService; + } + + public void setLoginService(LoginService loginService) { + this.loginService = loginService; + } + +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java index 3393236..be099a7 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/controller/HealthCheckController.java @@ -1,86 +1,86 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.controller; - -import java.util.List; - -import javax.servlet.http.HttpServletRequest; - -import org.onap.ccsdk.dashboard.model.HealthStatus; -import org.openecomp.portalsdk.core.controller.UnRestrictedBaseController; -import org.openecomp.portalsdk.core.domain.App; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.openecomp.portalsdk.core.service.DataAccessService; -import org.openecomp.portalsdk.core.util.SystemProperties; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.EnableAspectJAutoProxy; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -/** - * This controller responds to probes for application health, returning a JSON - * body to indicate current status. - */ -@RestController -@Configuration -@EnableAspectJAutoProxy -@RequestMapping("/") -public class HealthCheckController extends UnRestrictedBaseController { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class); - - private static final String HEALTH_CHECK_PATH = "/healthCheck"; - - @Autowired - private DataAccessService dataAccessService; - - /** - * Checks application health by making a trivial query to (what??). - * - * @param request - * HttpServletRequest - * @return 200 if database access succeeds, 500 if it fails. - */ - @RequestMapping(value = { HEALTH_CHECK_PATH }, method = RequestMethod.GET, produces = "application/json") - public HealthStatus healthCheck(HttpServletRequest request) { - logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DashboardRestrictedBaseController.APP_NAME); - logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); - HealthStatus healthStatus = null; - try { - logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); - @SuppressWarnings("unchecked") - // Get the single app. - List list = dataAccessService.getList(App.class, null); - if (!list.isEmpty()) - healthStatus = new HealthStatus(200, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check succeeded"); - else - healthStatus = new HealthStatus(500, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check failed to run db query"); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex); - healthStatus = new HealthStatus(500, "health check failed: " + ex.toString()); - } - return healthStatus; - } - -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.controller; + +import java.util.List; + +import javax.servlet.http.HttpServletRequest; + +import org.onap.ccsdk.dashboard.model.HealthStatus; +import org.onap.portalsdk.core.controller.UnRestrictedBaseController; +import org.onap.portalsdk.core.domain.App; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.service.DataAccessService; +import org.onap.portalsdk.core.util.SystemProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * This controller responds to probes for application health, returning a JSON + * body to indicate current status. + */ +@RestController +@Configuration +@EnableAspectJAutoProxy +@RequestMapping("/") +public class HealthCheckController extends UnRestrictedBaseController { + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HealthCheckController.class); + + private static final String HEALTH_CHECK_PATH = "/healthCheck"; + + @Autowired + private DataAccessService dataAccessService; + + /** + * Checks application health by making a trivial query to (what??). + * + * @param request + * HttpServletRequest + * @return 200 if database access succeeds, 500 if it fails. + */ + @RequestMapping(value = { HEALTH_CHECK_PATH }, method = RequestMethod.GET, produces = "application/json") + public HealthStatus healthCheck(HttpServletRequest request) { + logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, DashboardRestrictedBaseController.APP_NAME); + logger.info(EELFLoggerDelegate.auditLogger, request.getMethod() + request.getRequestURI()); + HealthStatus healthStatus = null; + try { + logger.debug(EELFLoggerDelegate.debugLogger, "Performing health check"); + @SuppressWarnings("unchecked") + // Get the single app. + List list = dataAccessService.getList(App.class, null); + if (!list.isEmpty()) + healthStatus = new HealthStatus(200, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check succeeded"); + else + healthStatus = new HealthStatus(500, SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME) + " health check failed to run db query"); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "Failed to perform health check", ex); + healthStatus = new HealthStatus(500, "health check failed: " + ex.toString()); + } + return healthStatus; + } + +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/domain/ControllerEndpoint.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/domain/ControllerEndpoint.java index 1e74611..397f2ed 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/domain/ControllerEndpoint.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/domain/ControllerEndpoint.java @@ -1,71 +1,71 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.domain; - -import org.openecomp.portalsdk.core.domain.support.DomainVo; - -/** - * Model for controller endpoint information stored in database. A single row - * for a user represents a selected endpoint. - */ -public class ControllerEndpoint extends DomainVo { - - private static final long serialVersionUID = 8785223545128054402L; - - private long userId; - private String name; - private String url; - - public ControllerEndpoint() { - } - - public ControllerEndpoint(long userId, String name, String url) { - this.userId = userId; - this.name = name; - this.url = url; - } - - public long getUserId() { - return userId; - } - - public void setUserId(long userId) { - this.userId = userId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.domain; + +import org.onap.portalsdk.core.domain.support.DomainVo; + +/** + * Model for controller endpoint information stored in database. A single row + * for a user represents a selected endpoint. + */ +public class ControllerEndpoint extends DomainVo { + + private static final long serialVersionUID = 8785223545128054402L; + + private long userId; + private String name; + private String url; + + public ControllerEndpoint() { + } + + public ControllerEndpoint(long userId, String name, String url) { + this.userId = userId; + this.name = name; + this.url = url; + } + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/model/ControllerEndpointCredentials.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/model/ControllerEndpointCredentials.java index 02c892f..7af9e49 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/model/ControllerEndpointCredentials.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/model/ControllerEndpointCredentials.java @@ -1,116 +1,116 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.model; - -import org.onap.ccsdk.dashboard.exception.DashboardControllerException; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.openecomp.portalsdk.core.onboarding.util.CipherUtil; - -/** - * Model with Controller username and password for use only within the back end; - * never serialized as JSON. - */ -public class ControllerEndpointCredentials extends ControllerEndpointTransport { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ControllerEndpointCredentials.class); - - public String username; - public String password; - public boolean isEncryptedPass; - - public ControllerEndpointCredentials(boolean selected, String name, String url, String username, String password, - boolean isEncryptedPass) { - super(selected, name, url); - this.username = username; - this.password = password; - this.isEncryptedPass = isEncryptedPass; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public boolean getEncryptedPassword() { - return isEncryptedPass; - } - - public void setEncryptedPassword(boolean isEncryptedPass) { - this.isEncryptedPass = isEncryptedPass; - } - - /** - * Convenience method to yield a ControllerEndpointTransport object. - * - * @return ControllerEndpoint with copy of the non-privileged data - */ - public ControllerEndpointTransport toControllerEndpointTransport() { - return new ControllerEndpointTransport(getSelected(), getName(), getUrl()); - } - - /** - * Accepts clear text and stores an encrypted value; as a side effect, sets the - * encrypted flag to true. - * - * @param plainText - * Clear-text password - * @throws DashboardControllerException - * If encryption fails - */ - public void encryptPassword(final String plainText) throws DashboardControllerException { - try { - this.password = CipherUtil.encrypt(plainText); - this.isEncryptedPass = true; - } catch (Exception ex) { - logger.error("encryptPassword failed", ex); - throw new DashboardControllerException(ex); - } - } - - /** - * Client should call this method if {@link #getEncryptedPassword()} returns - * true. - * - * @return Clear-text password. - * @throws DashboardControllerException - * If decryption fails - */ - public String decryptPassword() throws DashboardControllerException { - try { - return CipherUtil.decrypt(password); - } catch (Exception ex) { - logger.error("decryptPassword failed", ex); - throw new DashboardControllerException(ex); - } - } +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.model; + +import org.onap.ccsdk.dashboard.exception.DashboardControllerException; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.onboarding.util.CipherUtil; + +/** + * Model with Controller username and password for use only within the back end; + * never serialized as JSON. + */ +public class ControllerEndpointCredentials extends ControllerEndpointTransport { + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ControllerEndpointCredentials.class); + + public String username; + public String password; + public boolean isEncryptedPass; + + public ControllerEndpointCredentials(boolean selected, String name, String url, String username, String password, + boolean isEncryptedPass) { + super(selected, name, url); + this.username = username; + this.password = password; + this.isEncryptedPass = isEncryptedPass; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public boolean getEncryptedPassword() { + return isEncryptedPass; + } + + public void setEncryptedPassword(boolean isEncryptedPass) { + this.isEncryptedPass = isEncryptedPass; + } + + /** + * Convenience method to yield a ControllerEndpointTransport object. + * + * @return ControllerEndpoint with copy of the non-privileged data + */ + public ControllerEndpointTransport toControllerEndpointTransport() { + return new ControllerEndpointTransport(getSelected(), getName(), getUrl()); + } + + /** + * Accepts clear text and stores an encrypted value; as a side effect, sets the + * encrypted flag to true. + * + * @param plainText + * Clear-text password + * @throws DashboardControllerException + * If encryption fails + */ + public void encryptPassword(final String plainText) throws DashboardControllerException { + try { + this.password = CipherUtil.encrypt(plainText); + this.isEncryptedPass = true; + } catch (Exception ex) { + logger.error("encryptPassword failed", ex); + throw new DashboardControllerException(ex); + } + } + + /** + * Client should call this method if {@link #getEncryptedPassword()} returns + * true. + * + * @return Clear-text password. + * @throws DashboardControllerException + * If decryption fails + */ + public String decryptPassword() throws DashboardControllerException { + try { + return CipherUtil.decrypt(password); + } catch (Exception ex) { + logger.error("decryptPassword failed", ex); + throw new DashboardControllerException(ex); + } + } } \ No newline at end of file diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/ControllerRestClientImpl.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/ControllerRestClientImpl.java index bb79c9f..2e28ad2 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/ControllerRestClientImpl.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/ControllerRestClientImpl.java @@ -1,412 +1,412 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.rest; - -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; -import java.time.Instant; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.apache.http.HttpHost; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.onap.ccsdk.dashboard.model.CloudifyBlueprintContent; -import org.onap.ccsdk.dashboard.model.CloudifyBlueprintList; -import org.onap.ccsdk.dashboard.model.CloudifyBlueprintUpload; -import org.onap.ccsdk.dashboard.model.CloudifyDeploymentList; -import org.onap.ccsdk.dashboard.model.CloudifyDeploymentRequest; -import org.onap.ccsdk.dashboard.model.CloudifyExecution; -import org.onap.ccsdk.dashboard.model.CloudifyExecutionList; -import org.onap.ccsdk.dashboard.model.CloudifyExecutionRequest; -import org.onap.ccsdk.dashboard.model.ConsulDatacenter; -import org.onap.ccsdk.dashboard.model.ConsulHealthServiceRegistration; -import org.onap.ccsdk.dashboard.model.ConsulNodeInfo; -import org.onap.ccsdk.dashboard.model.ConsulServiceHealth; -import org.onap.ccsdk.dashboard.model.ConsulServiceHealthHistory; -import org.onap.ccsdk.dashboard.model.ConsulServiceInfo; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * Provides methods for accessing the ECOMP Controller API via REST. Most - * methods are just simple proxies. Only the methods that fetch one page of data - * have to do any real work. - * - * Implemented using Spring RestTemplate. Supports basic HTTP authentication. - * - */ -public class ControllerRestClientImpl implements IControllerRestClient { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ControllerRestClientImpl.class); - private static final String DEPLOYMENT_ID = "deployment_id"; - - private final String baseUrl; - private final RestTemplate restTemplate; - private final ObjectMapper objectMapper = new ObjectMapper(); - - /** - * Builds a restTemplate. If username and password are supplied, uses basic - * HTTP authentication. - * - * @param webapiUrl - * URL of the web endpoint - * @param user - * user name; ignored if null - * @param pass - * password - */ - public ControllerRestClientImpl(String webapiUrl, String user, String pass) { - if (webapiUrl == null) - throw new IllegalArgumentException("Null URL not permitted"); - - URL url = null; - try { - url = new URL(webapiUrl); - baseUrl = url.toExternalForm(); - } catch (MalformedURLException ex) { - throw new RuntimeException("Failed to parse URL", ex); - } - final HttpHost httpHost = new HttpHost(url.getHost(), url.getPort()); - - // Build a client with a credentials provider - CloseableHttpClient httpClient = null; - if (user != null && pass != null) { - CredentialsProvider credsProvider = new BasicCredentialsProvider(); - credsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(user, pass)); - httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build(); - } else { - httpClient = HttpClientBuilder.create().build(); - } - // Create request factory - HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth( - httpHost); - requestFactory.setHttpClient(httpClient); - - // Put the factory in the template - this.restTemplate = new RestTemplate(); - restTemplate.setRequestFactory(requestFactory); - } - - /** - * Builds URL ensuring appropriate separators. The base comes from - * properties file so could have many problems. - * - * @param base - * @param suffix - * @param queryParams - * key-value pairs; i.e. must have an even number of entries. - * Ignored if null. - * @return - */ - private String buildUrl(final String[] path, final String[] queryParams) { - StringBuilder sb = new StringBuilder(path[0]); - for (int p = 1; p < path.length; ++p) { - if (!path[p - 1].endsWith("/") && !path[p].startsWith("/")) - sb.append('/'); - sb.append(path[p]); - } - if (queryParams != null && queryParams.length > 0) { - sb.append('?'); - int i = 0; - while (i < queryParams.length) { - if (i > 0) - sb.append('&'); - sb.append(queryParams[i]); - sb.append('='); - sb.append(queryParams[i + 1]); - i += 2; - } - } - return sb.toString(); - } - - @Override - public CloudifyBlueprintList getBlueprints() { - String url = buildUrl(new String[] { baseUrl, blueprintsPath }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "getBlueprints: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference() { - }); - return response.getBody(); - } - - @Override - public CloudifyBlueprintList getBlueprint(final String id) { - String url = buildUrl(new String[] { baseUrl, blueprintsPath }, new String[] { "id", id }); - logger.debug(EELFLoggerDelegate.debugLogger, "getBlueprint: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference() { - }); - return response.getBody(); - } - - @Override - public CloudifyBlueprintContent viewBlueprint(final String id) { - String url = buildUrl(new String[] { baseUrl, viewBlueprintsPath }, new String[] { "id", id }); - logger.debug(EELFLoggerDelegate.debugLogger, "viewBlueprint: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, String.class); - String yaml = response.getBody(); - return new CloudifyBlueprintContent(id, yaml); - } - - @Override - public CloudifyBlueprintList uploadBlueprint(CloudifyBlueprintUpload blueprint) { - String url = buildUrl(new String[] { baseUrl, blueprintsPath }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "uploadBlueprint: url {}", url); - return restTemplate.postForObject(url, blueprint, CloudifyBlueprintList.class); - } - - @Override - public int deleteBlueprint(final String id) { - String url = buildUrl(new String[] { baseUrl, blueprintsPath, id }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "deleteBlueprint: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.DELETE, null, - new ParameterizedTypeReference() { - }); - return response.getStatusCode().value(); - } - - @Override - public CloudifyDeploymentList getDeployments() { - String url = buildUrl(new String[] { baseUrl, deploymentsPath }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "getDeployments: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference() { - }); - return response.getBody(); - } - - @Override - public CloudifyDeploymentList getDeployment(final String id) { - String url = buildUrl(new String[] { baseUrl, deploymentsPath }, new String[] { "id", id }); - logger.debug(EELFLoggerDelegate.debugLogger, "getDeployment: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference() { - }); - return response.getBody(); - } - - @Override - public CloudifyDeploymentList createDeployment(CloudifyDeploymentRequest deployment) { - String url = buildUrl(new String[] { baseUrl, deploymentsPath }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "createDeployment: url {}", url); - return restTemplate.postForObject(url, deployment, CloudifyDeploymentList.class); - } - - @Override - public int deleteDeployment(final String id, boolean ignoreLiveNodes) { - String url = buildUrl(new String[] { baseUrl, deploymentsPath, id }, - new String[] { "ignore_live_nodes", Boolean.toString(ignoreLiveNodes) }); - logger.debug(EELFLoggerDelegate.debugLogger, "deleteDeployment: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.DELETE, null, - new ParameterizedTypeReference() { - }); - return response.getStatusCode().value(); - } - - @Override - public CloudifyExecutionList getExecutions(final String deploymentId) { - String url = buildUrl(new String[]{baseUrl, executionsPath}, new String[]{DEPLOYMENT_ID, deploymentId}); - logger.debug(EELFLoggerDelegate.debugLogger, "getExecutions: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference() { - }); - return response.getBody(); - } - - @Override - public CloudifyExecutionList getExecution(String executionId, String deploymentId) { - String url = buildUrl(new String[] { baseUrl, executionsPath, executionId }, - new String[]{DEPLOYMENT_ID, deploymentId}); - logger.debug(EELFLoggerDelegate.debugLogger, "getExecution: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference() { - }); - return response.getBody(); - } - - @Override - public CloudifyExecution startExecution(CloudifyExecutionRequest execution) { - String url = buildUrl(new String[] { baseUrl, executionsPath }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "startExecution: url {}", url); - return restTemplate.postForObject(url, execution, CloudifyExecution.class); - } - - @Override - public int cancelExecution(final String executionId, final String deploymentId, final String action) { - String url = buildUrl(new String[] { baseUrl, executionsPath, executionId }, - new String[]{DEPLOYMENT_ID, deploymentId, "action", action}); - logger.debug(EELFLoggerDelegate.debugLogger, "deleteExecution: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.DELETE, null, - new ParameterizedTypeReference() { - }); - return response.getStatusCode().value(); - } - - @Override - public URI registerService(ConsulHealthServiceRegistration registration) { - String url = buildUrl(new String[] { baseUrl, healthServicesPath, "register" }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "registerService: url {}", url); - return restTemplate.postForLocation(url, registration); - } - - @Override - public int deregisterService(String serviceName) { - String url = buildUrl(new String[] { baseUrl, healthServicesPath, "deregister" , serviceName}, null); - logger.debug(EELFLoggerDelegate.debugLogger, "deregisterService: url {}", url); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, null, - new ParameterizedTypeReference() { - }); - return response.getStatusCode().value(); - } - - /** - * Translates the awkward map of String-to-List of IP into a list of - * ConsulServiceInfo objects - */ - @SuppressWarnings("unchecked") - @Override - public List getServices() { - String url = buildUrl(new String[] { baseUrl, healthServicesPath, "services" }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "getServicesHealth: url {}", url); - ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference>() { - }); - Map serviceInfo = response.getBody(); - List list = new ArrayList<>(); - for (Map.Entry entry : serviceInfo.entrySet()) { - // Be defensive - List addrs; - if (entry.getValue() instanceof List) - addrs = (List) entry.getValue(); - else - addrs = new ArrayList<>(); - list.add(new ConsulServiceInfo(entry.getKey(), addrs)); - } - return list; - } - - @Override - public List getServiceHealth(String serviceName) { - String url = buildUrl(new String[] { baseUrl, healthServicesPath, "services", serviceName }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "getServiceHealth: url {}", url); - ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference>() { - }); - return response.getBody(); - } - - @Override - public List getServiceHealthHistory(String serviceName, Instant start, - Instant end) { - String url = buildUrl(new String[] { baseUrl, healthServicesPath, "svchist", serviceName }, - new String[] { "start", start.toString(), "end", end.toString() }); - logger.debug(EELFLoggerDelegate.debugLogger, "getServiceHealthHistory: url {}", url); - // Hack around an odd interface that returns non-JSON on error: - // "No health Data found for the selected dates or service" - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference() { }); - if (response.getBody().startsWith("No health")) - throw new RuntimeException(response.getBody()); - List result = null; - try { - TypeReference> typeRef = new TypeReference>() { }; - result = objectMapper.readValue(response.getBody(), typeRef); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "getServiceHealthHistory failed to parse response body", ex); - } - return result; - } - - @Override - public List getNodes() { - String url = buildUrl(new String[] { baseUrl, healthServicesPath, "nodes" }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "getNodesHealth: url {}", url); - ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference>() { - }); - return response.getBody(); - } - - @Override - public List getNodeServicesHealth(String nodeId) { - String url = buildUrl(new String[] { baseUrl, healthServicesPath, "nodes", nodeId }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "getNodeServicesHealth: url {}", url); - ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference>() { - }); - return response.getBody(); - } - - @Override - public List getDatacenters() { - String url = buildUrl(new String[] { baseUrl, healthServicesPath, "datacenters" }, null); - logger.debug(EELFLoggerDelegate.debugLogger, "getDatacentersHealth: url {}", url); - ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, - new ParameterizedTypeReference>() { - }); - List list = response.getBody(); - List result = new ArrayList<>(); - for (String dc : list) - result.add(new ConsulDatacenter(dc)); - return result; - } - - /** - * Simple test - * - * @param args - * blueprint ID - * @throws IllegalArgumentException - * On bad arguments - */ - public static void main(String[] args) throws IllegalArgumentException { - if (args.length != 1) - throw new IllegalArgumentException("Single argument expected: blueprint-id"); - ControllerRestClientImpl client = new ControllerRestClientImpl("http://localhost:8081/controller", "dbus_user", - "dbus_pass"); - final String id = args[0]; - logger.info("Requesting blueprint for " + id); - CloudifyBlueprintList list = client.getBlueprint(id); - if (list == null) - logger.error("Received null"); - else - for (int i = 0; i < list.items.size(); ++i) { - logger.info("Blueprint " + Integer.toString(i)); - logger.info(list.items.get(i).toString()); - } - } - -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.rest; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.onap.ccsdk.dashboard.model.CloudifyBlueprintContent; +import org.onap.ccsdk.dashboard.model.CloudifyBlueprintList; +import org.onap.ccsdk.dashboard.model.CloudifyBlueprintUpload; +import org.onap.ccsdk.dashboard.model.CloudifyDeploymentList; +import org.onap.ccsdk.dashboard.model.CloudifyDeploymentRequest; +import org.onap.ccsdk.dashboard.model.CloudifyExecution; +import org.onap.ccsdk.dashboard.model.CloudifyExecutionList; +import org.onap.ccsdk.dashboard.model.CloudifyExecutionRequest; +import org.onap.ccsdk.dashboard.model.ConsulDatacenter; +import org.onap.ccsdk.dashboard.model.ConsulHealthServiceRegistration; +import org.onap.ccsdk.dashboard.model.ConsulNodeInfo; +import org.onap.ccsdk.dashboard.model.ConsulServiceHealth; +import org.onap.ccsdk.dashboard.model.ConsulServiceHealthHistory; +import org.onap.ccsdk.dashboard.model.ConsulServiceInfo; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Provides methods for accessing the ECOMP Controller API via REST. Most + * methods are just simple proxies. Only the methods that fetch one page of data + * have to do any real work. + * + * Implemented using Spring RestTemplate. Supports basic HTTP authentication. + * + */ +public class ControllerRestClientImpl implements IControllerRestClient { + + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ControllerRestClientImpl.class); + private static final String DEPLOYMENT_ID = "deployment_id"; + + private final String baseUrl; + private final RestTemplate restTemplate; + private final ObjectMapper objectMapper = new ObjectMapper(); + + /** + * Builds a restTemplate. If username and password are supplied, uses basic + * HTTP authentication. + * + * @param webapiUrl + * URL of the web endpoint + * @param user + * user name; ignored if null + * @param pass + * password + */ + public ControllerRestClientImpl(String webapiUrl, String user, String pass) { + if (webapiUrl == null) + throw new IllegalArgumentException("Null URL not permitted"); + + URL url = null; + try { + url = new URL(webapiUrl); + baseUrl = url.toExternalForm(); + } catch (MalformedURLException ex) { + throw new RuntimeException("Failed to parse URL", ex); + } + final HttpHost httpHost = new HttpHost(url.getHost(), url.getPort()); + + // Build a client with a credentials provider + CloseableHttpClient httpClient = null; + if (user != null && pass != null) { + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(user, pass)); + httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build(); + } else { + httpClient = HttpClientBuilder.create().build(); + } + // Create request factory + HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth( + httpHost); + requestFactory.setHttpClient(httpClient); + + // Put the factory in the template + this.restTemplate = new RestTemplate(); + restTemplate.setRequestFactory(requestFactory); + } + + /** + * Builds URL ensuring appropriate separators. The base comes from + * properties file so could have many problems. + * + * @param base + * @param suffix + * @param queryParams + * key-value pairs; i.e. must have an even number of entries. + * Ignored if null. + * @return + */ + private String buildUrl(final String[] path, final String[] queryParams) { + StringBuilder sb = new StringBuilder(path[0]); + for (int p = 1; p < path.length; ++p) { + if (!path[p - 1].endsWith("/") && !path[p].startsWith("/")) + sb.append('/'); + sb.append(path[p]); + } + if (queryParams != null && queryParams.length > 0) { + sb.append('?'); + int i = 0; + while (i < queryParams.length) { + if (i > 0) + sb.append('&'); + sb.append(queryParams[i]); + sb.append('='); + sb.append(queryParams[i + 1]); + i += 2; + } + } + return sb.toString(); + } + + @Override + public CloudifyBlueprintList getBlueprints() { + String url = buildUrl(new String[] { baseUrl, blueprintsPath }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "getBlueprints: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference() { + }); + return response.getBody(); + } + + @Override + public CloudifyBlueprintList getBlueprint(final String id) { + String url = buildUrl(new String[] { baseUrl, blueprintsPath }, new String[] { "id", id }); + logger.debug(EELFLoggerDelegate.debugLogger, "getBlueprint: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference() { + }); + return response.getBody(); + } + + @Override + public CloudifyBlueprintContent viewBlueprint(final String id) { + String url = buildUrl(new String[] { baseUrl, viewBlueprintsPath }, new String[] { "id", id }); + logger.debug(EELFLoggerDelegate.debugLogger, "viewBlueprint: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, String.class); + String yaml = response.getBody(); + return new CloudifyBlueprintContent(id, yaml); + } + + @Override + public CloudifyBlueprintList uploadBlueprint(CloudifyBlueprintUpload blueprint) { + String url = buildUrl(new String[] { baseUrl, blueprintsPath }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "uploadBlueprint: url {}", url); + return restTemplate.postForObject(url, blueprint, CloudifyBlueprintList.class); + } + + @Override + public int deleteBlueprint(final String id) { + String url = buildUrl(new String[] { baseUrl, blueprintsPath, id }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "deleteBlueprint: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.DELETE, null, + new ParameterizedTypeReference() { + }); + return response.getStatusCode().value(); + } + + @Override + public CloudifyDeploymentList getDeployments() { + String url = buildUrl(new String[] { baseUrl, deploymentsPath }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "getDeployments: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference() { + }); + return response.getBody(); + } + + @Override + public CloudifyDeploymentList getDeployment(final String id) { + String url = buildUrl(new String[] { baseUrl, deploymentsPath }, new String[] { "id", id }); + logger.debug(EELFLoggerDelegate.debugLogger, "getDeployment: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference() { + }); + return response.getBody(); + } + + @Override + public CloudifyDeploymentList createDeployment(CloudifyDeploymentRequest deployment) { + String url = buildUrl(new String[] { baseUrl, deploymentsPath }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "createDeployment: url {}", url); + return restTemplate.postForObject(url, deployment, CloudifyDeploymentList.class); + } + + @Override + public int deleteDeployment(final String id, boolean ignoreLiveNodes) { + String url = buildUrl(new String[] { baseUrl, deploymentsPath, id }, + new String[] { "ignore_live_nodes", Boolean.toString(ignoreLiveNodes) }); + logger.debug(EELFLoggerDelegate.debugLogger, "deleteDeployment: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.DELETE, null, + new ParameterizedTypeReference() { + }); + return response.getStatusCode().value(); + } + + @Override + public CloudifyExecutionList getExecutions(final String deploymentId) { + String url = buildUrl(new String[]{baseUrl, executionsPath}, new String[]{DEPLOYMENT_ID, deploymentId}); + logger.debug(EELFLoggerDelegate.debugLogger, "getExecutions: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference() { + }); + return response.getBody(); + } + + @Override + public CloudifyExecutionList getExecution(String executionId, String deploymentId) { + String url = buildUrl(new String[] { baseUrl, executionsPath, executionId }, + new String[]{DEPLOYMENT_ID, deploymentId}); + logger.debug(EELFLoggerDelegate.debugLogger, "getExecution: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference() { + }); + return response.getBody(); + } + + @Override + public CloudifyExecution startExecution(CloudifyExecutionRequest execution) { + String url = buildUrl(new String[] { baseUrl, executionsPath }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "startExecution: url {}", url); + return restTemplate.postForObject(url, execution, CloudifyExecution.class); + } + + @Override + public int cancelExecution(final String executionId, final String deploymentId, final String action) { + String url = buildUrl(new String[] { baseUrl, executionsPath, executionId }, + new String[]{DEPLOYMENT_ID, deploymentId, "action", action}); + logger.debug(EELFLoggerDelegate.debugLogger, "deleteExecution: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.DELETE, null, + new ParameterizedTypeReference() { + }); + return response.getStatusCode().value(); + } + + @Override + public URI registerService(ConsulHealthServiceRegistration registration) { + String url = buildUrl(new String[] { baseUrl, healthServicesPath, "register" }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "registerService: url {}", url); + return restTemplate.postForLocation(url, registration); + } + + @Override + public int deregisterService(String serviceName) { + String url = buildUrl(new String[] { baseUrl, healthServicesPath, "deregister" , serviceName}, null); + logger.debug(EELFLoggerDelegate.debugLogger, "deregisterService: url {}", url); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, null, + new ParameterizedTypeReference() { + }); + return response.getStatusCode().value(); + } + + /** + * Translates the awkward map of String-to-List of IP into a list of + * ConsulServiceInfo objects + */ + @SuppressWarnings("unchecked") + @Override + public List getServices() { + String url = buildUrl(new String[] { baseUrl, healthServicesPath, "services" }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "getServicesHealth: url {}", url); + ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference>() { + }); + Map serviceInfo = response.getBody(); + List list = new ArrayList<>(); + for (Map.Entry entry : serviceInfo.entrySet()) { + // Be defensive + List addrs; + if (entry.getValue() instanceof List) + addrs = (List) entry.getValue(); + else + addrs = new ArrayList<>(); + list.add(new ConsulServiceInfo(entry.getKey(), addrs)); + } + return list; + } + + @Override + public List getServiceHealth(String serviceName) { + String url = buildUrl(new String[] { baseUrl, healthServicesPath, "services", serviceName }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "getServiceHealth: url {}", url); + ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference>() { + }); + return response.getBody(); + } + + @Override + public List getServiceHealthHistory(String serviceName, Instant start, + Instant end) { + String url = buildUrl(new String[] { baseUrl, healthServicesPath, "svchist", serviceName }, + new String[] { "start", start.toString(), "end", end.toString() }); + logger.debug(EELFLoggerDelegate.debugLogger, "getServiceHealthHistory: url {}", url); + // Hack around an odd interface that returns non-JSON on error: + // "No health Data found for the selected dates or service" + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference() { }); + if (response.getBody().startsWith("No health")) + throw new RuntimeException(response.getBody()); + List result = null; + try { + TypeReference> typeRef = new TypeReference>() { }; + result = objectMapper.readValue(response.getBody(), typeRef); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "getServiceHealthHistory failed to parse response body", ex); + } + return result; + } + + @Override + public List getNodes() { + String url = buildUrl(new String[] { baseUrl, healthServicesPath, "nodes" }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "getNodesHealth: url {}", url); + ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference>() { + }); + return response.getBody(); + } + + @Override + public List getNodeServicesHealth(String nodeId) { + String url = buildUrl(new String[] { baseUrl, healthServicesPath, "nodes", nodeId }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "getNodeServicesHealth: url {}", url); + ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference>() { + }); + return response.getBody(); + } + + @Override + public List getDatacenters() { + String url = buildUrl(new String[] { baseUrl, healthServicesPath, "datacenters" }, null); + logger.debug(EELFLoggerDelegate.debugLogger, "getDatacentersHealth: url {}", url); + ResponseEntity> response = restTemplate.exchange(url, HttpMethod.GET, null, + new ParameterizedTypeReference>() { + }); + List list = response.getBody(); + List result = new ArrayList<>(); + for (String dc : list) + result.add(new ConsulDatacenter(dc)); + return result; + } + + /** + * Simple test + * + * @param args + * blueprint ID + * @throws IllegalArgumentException + * On bad arguments + */ + public static void main(String[] args) throws IllegalArgumentException { + if (args.length != 1) + throw new IllegalArgumentException("Single argument expected: blueprint-id"); + ControllerRestClientImpl client = new ControllerRestClientImpl("http://localhost:8081/controller", "dbus_user", + "dbus_pass"); + final String id = args[0]; + logger.info("Requesting blueprint for " + id); + CloudifyBlueprintList list = client.getBlueprint(id); + if (list == null) + logger.error("Received null"); + else + for (int i = 0; i < list.items.size(); ++i) { + logger.info("Blueprint " + Integer.toString(i)); + logger.info(list.items.get(i).toString()); + } + } + +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/ControllerRestClientMockImpl.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/ControllerRestClientMockImpl.java index 2523538..6b0f3d0 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/ControllerRestClientMockImpl.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/ControllerRestClientMockImpl.java @@ -1,311 +1,311 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.rest; - -import java.io.InputStream; -import java.net.URI; -import java.time.Instant; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Scanner; - -import org.onap.ccsdk.dashboard.exception.DashboardControllerException; -import org.onap.ccsdk.dashboard.model.CloudifyBlueprintContent; -import org.onap.ccsdk.dashboard.model.CloudifyBlueprintList; -import org.onap.ccsdk.dashboard.model.CloudifyBlueprintUpload; -import org.onap.ccsdk.dashboard.model.CloudifyDeploymentList; -import org.onap.ccsdk.dashboard.model.CloudifyDeploymentRequest; -import org.onap.ccsdk.dashboard.model.CloudifyExecution; -import org.onap.ccsdk.dashboard.model.CloudifyExecutionList; -import org.onap.ccsdk.dashboard.model.CloudifyExecutionRequest; -import org.onap.ccsdk.dashboard.model.ConsulDatacenter; -import org.onap.ccsdk.dashboard.model.ConsulHealthServiceRegistration; -import org.onap.ccsdk.dashboard.model.ConsulNodeInfo; -import org.onap.ccsdk.dashboard.model.ConsulServiceHealth; -import org.onap.ccsdk.dashboard.model.ConsulServiceHealthHistory; -import org.onap.ccsdk.dashboard.model.ConsulServiceInfo; -import org.onap.ccsdk.dashboard.model.ECTransportModel; -import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * Provides mock implementations that return contents of files on the classpath. - */ -public class ControllerRestClientMockImpl implements IControllerRestClient { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ControllerRestClientMockImpl.class); - - /** - * For mock outputs - */ - private final ObjectMapper objectMapper = new ObjectMapper(); - - /** - * No-arg constructor - */ - public ControllerRestClientMockImpl() { - } - - private String getMockDataContent(final String path) { - String result = null; - try { - InputStream is = getClass().getResourceAsStream(path); - if (is == null) - throw new DashboardControllerException("Failed to find resource at path " + path); - Scanner scanner = new Scanner(is, "UTF-8"); - result = scanner.useDelimiter("\\A").next(); - scanner.close(); - is.close(); - } catch (Exception ex) { - logger.error("getMockDataContent failed", ex); - throw new RuntimeException(ex); - } - return result; - } - - /** - * Creates an input stream using the specified path and requests the mapper - * create an object of the specified type. - * - * @param modelClass - * Model class - * @param path - * Path to classpath resource - * @return Instance of modelClass - */ - private ECTransportModel getMockData(final Class modelClass, final String path) { - ECTransportModel result; - String json = getMockDataContent(path); - try { - result = objectMapper.readValue(json, modelClass); - } catch (Exception ex) { - logger.error("getMockData failed", ex); - throw new RuntimeException(ex); - } - return result; - } - - @Override - public CloudifyBlueprintList getBlueprints() { - return (CloudifyBlueprintList) getMockData(CloudifyBlueprintList.class, "/blueprintList.json"); - } - - @Override - public CloudifyBlueprintList getBlueprint(final String id) { - return (CloudifyBlueprintList) getMockData(CloudifyBlueprintList.class, "/blueprintByID.json"); - } - - @Override - public CloudifyBlueprintContent viewBlueprint(final String id) { - String yaml = getMockDataContent("/blueprintContent.yaml"); - return new CloudifyBlueprintContent(id, yaml); - } - - @Override - public CloudifyBlueprintList uploadBlueprint(CloudifyBlueprintUpload blueprint) { - logger.debug(EELFLoggerDelegate.debugLogger, "uploadBlueprint: {}", blueprint.toString()); - return new CloudifyBlueprintList(null, null); - } - - @Override - public int deleteBlueprint(final String id) { - logger.debug(EELFLoggerDelegate.debugLogger, "deleteBlueprint: {}", id); - return 204; - } - - @Override - public CloudifyDeploymentList getDeployments() { - return (CloudifyDeploymentList) getMockData(CloudifyDeploymentList.class, "/deploymentList.json"); - } - - @Override - public CloudifyDeploymentList getDeployment(final String id) { - return (CloudifyDeploymentList) getMockData(CloudifyDeploymentList.class, "/deploymentByID.json"); - } - - public CloudifyDeploymentList createDeployment(CloudifyDeploymentRequest deployment) { - logger.debug(EELFLoggerDelegate.debugLogger, "createDeployment: {}", deployment.toString()); - return new CloudifyDeploymentList(null, null); - } - - @Override - public int deleteDeployment(final String id, boolean ignoreLiveNodes) { - logger.debug(EELFLoggerDelegate.debugLogger, "deleteDeployment: id {}, ignoreLiveNodes", id, ignoreLiveNodes); - return 204; - } - - @Override - public CloudifyExecutionList getExecutions(final String deploymentId) { - return (CloudifyExecutionList) getMockData(CloudifyExecutionList.class, "/listExecutionForDeploymentID.json"); - } - - @Override - public CloudifyExecutionList getExecution(String executionId, String deploymentId) { - return (CloudifyExecutionList) getMockData(CloudifyExecutionList.class, "/listExecutionForDeploymentID.json"); - } - - @Override - public CloudifyExecution startExecution(CloudifyExecutionRequest execution) { - logger.debug(EELFLoggerDelegate.debugLogger, "startExecution: {}", execution.toString()); - return new CloudifyExecution(null, null, null, null, null, null, null, null, null); - } - - @Override - public int cancelExecution(String executionId, String deploymentId, String action) { - logger.debug(EELFLoggerDelegate.debugLogger, "deleteExecution: executionId {}, deploymentId {}, action {}", - executionId, deploymentId, action); - return 204; - } - - @Override - public URI registerService(ConsulHealthServiceRegistration registration) { - logger.debug(EELFLoggerDelegate.debugLogger, "registerService: {}", registration); - return null; - } - - @Override - public int deregisterService(String serviceName) { - logger.debug(EELFLoggerDelegate.debugLogger, "deregisterService: {}", serviceName); - return 200; - } - - @Override - public List getServiceHealth(String serviceName) { - logger.debug(EELFLoggerDelegate.debugLogger, "getServiceHealth: serviceName={}", serviceName); - String json = getMockDataContent("/serviceHealth.json"); - TypeReference> typeRef = new TypeReference>() { - }; - List result = null; - try { - result = objectMapper.readValue(json, typeRef); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "getServiceHealth failed", ex); - } - return result; - } - - @Override - public List getServiceHealthHistory(String serviceName, Instant start, Instant end) { - logger.debug(EELFLoggerDelegate.debugLogger, "getServiceHealthHistory: serviceName={}", serviceName); - String json = getMockDataContent("/serviceHealthHistory.json"); - TypeReference> typeRef = new TypeReference>() { - }; - List result = null; - try { - result = objectMapper.readValue(json, typeRef); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "getServiceHealthHistory failed", ex); - } - return result; - } - - @Override - public List getNodeServicesHealth(String nodeId) { - logger.debug(EELFLoggerDelegate.debugLogger, "getNodeServicesHealth: nodeId={}", nodeId); - String json = getMockDataContent("/nodeServicesHealth.json"); - TypeReference> typeRef = new TypeReference>() { - }; - List result = null; - try { - result = objectMapper.readValue(json, typeRef); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "getNodeServicesHealth failed", ex); - } - return result; - } - - @Override - public List getServices() { - logger.debug(EELFLoggerDelegate.debugLogger, "getServices"); - String json = getMockDataContent("/services.json"); - TypeReference> typeRef = new TypeReference>() { - }; - HashMap map = null; - try { - map = objectMapper.readValue(json, typeRef); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "getNode failed", ex); - } - ArrayList result = new ArrayList<>(); - if (map != null) { - for (Map.Entry entry : map.entrySet()) { - final String service = entry.getKey(); - @SuppressWarnings("unchecked") final List addrs = (List) entry.getValue(); - result.add(new ConsulServiceInfo(service, addrs)); - } - } - return result; - } - - @Override - public List getNodes() { - logger.debug(EELFLoggerDelegate.debugLogger, "getNodes"); - String json = getMockDataContent("/nodesHealth.json"); - TypeReference> typeRef = new TypeReference>() { - }; - List result = null; - try { - result = objectMapper.readValue(json, typeRef); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "getNode failed", ex); - - } - return result; - } - - @Override - public List getDatacenters() { - logger.debug(EELFLoggerDelegate.debugLogger, "getDatacentersHealth"); - return null; - } - - /** - * Simple test - * - * @param args - * blueprint ID - * @throws DashboardControllerException - * On any failure - */ - public static void main(String[] args) throws DashboardControllerException { - logger.info("Testing paths and parsing mock data"); - ControllerRestClientMockImpl client = new ControllerRestClientMockImpl(); - CloudifyBlueprintList list1 = client.getBlueprints(); - CloudifyBlueprintList list2 = client.getBlueprint("mock"); - CloudifyDeploymentList list3 = client.getDeployments(); - CloudifyDeploymentList list4 = client.getDeployment("mock"); - CloudifyExecutionList list5 = client.getExecutions("mock"); - List list6 = client.getServices(); - List list7 = client.getNodes(); - List list8 = client.getServiceHealth("mock"); - List list9 = client.getServiceHealthHistory("mock", Instant.now(), Instant.now()); - if (list1 == null || list2 == null || list3 == null || list4 == null || list5 == null || list6 == null - || list7 == null || list8 == null || list9 == null) - throw new DashboardControllerException("Failed"); - logger.info("Pass."); - } - -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.rest; + +import java.io.InputStream; +import java.net.URI; +import java.time.Instant; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +import org.onap.ccsdk.dashboard.exception.DashboardControllerException; +import org.onap.ccsdk.dashboard.model.CloudifyBlueprintContent; +import org.onap.ccsdk.dashboard.model.CloudifyBlueprintList; +import org.onap.ccsdk.dashboard.model.CloudifyBlueprintUpload; +import org.onap.ccsdk.dashboard.model.CloudifyDeploymentList; +import org.onap.ccsdk.dashboard.model.CloudifyDeploymentRequest; +import org.onap.ccsdk.dashboard.model.CloudifyExecution; +import org.onap.ccsdk.dashboard.model.CloudifyExecutionList; +import org.onap.ccsdk.dashboard.model.CloudifyExecutionRequest; +import org.onap.ccsdk.dashboard.model.ConsulDatacenter; +import org.onap.ccsdk.dashboard.model.ConsulHealthServiceRegistration; +import org.onap.ccsdk.dashboard.model.ConsulNodeInfo; +import org.onap.ccsdk.dashboard.model.ConsulServiceHealth; +import org.onap.ccsdk.dashboard.model.ConsulServiceHealthHistory; +import org.onap.ccsdk.dashboard.model.ConsulServiceInfo; +import org.onap.ccsdk.dashboard.model.ECTransportModel; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Provides mock implementations that return contents of files on the classpath. + */ +public class ControllerRestClientMockImpl implements IControllerRestClient { + + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ControllerRestClientMockImpl.class); + + /** + * For mock outputs + */ + private final ObjectMapper objectMapper = new ObjectMapper(); + + /** + * No-arg constructor + */ + public ControllerRestClientMockImpl() { + } + + private String getMockDataContent(final String path) { + String result = null; + try { + InputStream is = getClass().getResourceAsStream(path); + if (is == null) + throw new DashboardControllerException("Failed to find resource at path " + path); + Scanner scanner = new Scanner(is, "UTF-8"); + result = scanner.useDelimiter("\\A").next(); + scanner.close(); + is.close(); + } catch (Exception ex) { + logger.error("getMockDataContent failed", ex); + throw new RuntimeException(ex); + } + return result; + } + + /** + * Creates an input stream using the specified path and requests the mapper + * create an object of the specified type. + * + * @param modelClass + * Model class + * @param path + * Path to classpath resource + * @return Instance of modelClass + */ + private ECTransportModel getMockData(final Class modelClass, final String path) { + ECTransportModel result; + String json = getMockDataContent(path); + try { + result = objectMapper.readValue(json, modelClass); + } catch (Exception ex) { + logger.error("getMockData failed", ex); + throw new RuntimeException(ex); + } + return result; + } + + @Override + public CloudifyBlueprintList getBlueprints() { + return (CloudifyBlueprintList) getMockData(CloudifyBlueprintList.class, "/blueprintList.json"); + } + + @Override + public CloudifyBlueprintList getBlueprint(final String id) { + return (CloudifyBlueprintList) getMockData(CloudifyBlueprintList.class, "/blueprintByID.json"); + } + + @Override + public CloudifyBlueprintContent viewBlueprint(final String id) { + String yaml = getMockDataContent("/blueprintContent.yaml"); + return new CloudifyBlueprintContent(id, yaml); + } + + @Override + public CloudifyBlueprintList uploadBlueprint(CloudifyBlueprintUpload blueprint) { + logger.debug(EELFLoggerDelegate.debugLogger, "uploadBlueprint: {}", blueprint.toString()); + return new CloudifyBlueprintList(null, null); + } + + @Override + public int deleteBlueprint(final String id) { + logger.debug(EELFLoggerDelegate.debugLogger, "deleteBlueprint: {}", id); + return 204; + } + + @Override + public CloudifyDeploymentList getDeployments() { + return (CloudifyDeploymentList) getMockData(CloudifyDeploymentList.class, "/deploymentList.json"); + } + + @Override + public CloudifyDeploymentList getDeployment(final String id) { + return (CloudifyDeploymentList) getMockData(CloudifyDeploymentList.class, "/deploymentByID.json"); + } + + public CloudifyDeploymentList createDeployment(CloudifyDeploymentRequest deployment) { + logger.debug(EELFLoggerDelegate.debugLogger, "createDeployment: {}", deployment.toString()); + return new CloudifyDeploymentList(null, null); + } + + @Override + public int deleteDeployment(final String id, boolean ignoreLiveNodes) { + logger.debug(EELFLoggerDelegate.debugLogger, "deleteDeployment: id {}, ignoreLiveNodes", id, ignoreLiveNodes); + return 204; + } + + @Override + public CloudifyExecutionList getExecutions(final String deploymentId) { + return (CloudifyExecutionList) getMockData(CloudifyExecutionList.class, "/listExecutionForDeploymentID.json"); + } + + @Override + public CloudifyExecutionList getExecution(String executionId, String deploymentId) { + return (CloudifyExecutionList) getMockData(CloudifyExecutionList.class, "/listExecutionForDeploymentID.json"); + } + + @Override + public CloudifyExecution startExecution(CloudifyExecutionRequest execution) { + logger.debug(EELFLoggerDelegate.debugLogger, "startExecution: {}", execution.toString()); + return new CloudifyExecution(null, null, null, null, null, null, null, null, null); + } + + @Override + public int cancelExecution(String executionId, String deploymentId, String action) { + logger.debug(EELFLoggerDelegate.debugLogger, "deleteExecution: executionId {}, deploymentId {}, action {}", + executionId, deploymentId, action); + return 204; + } + + @Override + public URI registerService(ConsulHealthServiceRegistration registration) { + logger.debug(EELFLoggerDelegate.debugLogger, "registerService: {}", registration); + return null; + } + + @Override + public int deregisterService(String serviceName) { + logger.debug(EELFLoggerDelegate.debugLogger, "deregisterService: {}", serviceName); + return 200; + } + + @Override + public List getServiceHealth(String serviceName) { + logger.debug(EELFLoggerDelegate.debugLogger, "getServiceHealth: serviceName={}", serviceName); + String json = getMockDataContent("/serviceHealth.json"); + TypeReference> typeRef = new TypeReference>() { + }; + List result = null; + try { + result = objectMapper.readValue(json, typeRef); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "getServiceHealth failed", ex); + } + return result; + } + + @Override + public List getServiceHealthHistory(String serviceName, Instant start, Instant end) { + logger.debug(EELFLoggerDelegate.debugLogger, "getServiceHealthHistory: serviceName={}", serviceName); + String json = getMockDataContent("/serviceHealthHistory.json"); + TypeReference> typeRef = new TypeReference>() { + }; + List result = null; + try { + result = objectMapper.readValue(json, typeRef); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "getServiceHealthHistory failed", ex); + } + return result; + } + + @Override + public List getNodeServicesHealth(String nodeId) { + logger.debug(EELFLoggerDelegate.debugLogger, "getNodeServicesHealth: nodeId={}", nodeId); + String json = getMockDataContent("/nodeServicesHealth.json"); + TypeReference> typeRef = new TypeReference>() { + }; + List result = null; + try { + result = objectMapper.readValue(json, typeRef); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "getNodeServicesHealth failed", ex); + } + return result; + } + + @Override + public List getServices() { + logger.debug(EELFLoggerDelegate.debugLogger, "getServices"); + String json = getMockDataContent("/services.json"); + TypeReference> typeRef = new TypeReference>() { + }; + HashMap map = null; + try { + map = objectMapper.readValue(json, typeRef); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "getNode failed", ex); + } + ArrayList result = new ArrayList<>(); + if (map != null) { + for (Map.Entry entry : map.entrySet()) { + final String service = entry.getKey(); + @SuppressWarnings("unchecked") final List addrs = (List) entry.getValue(); + result.add(new ConsulServiceInfo(service, addrs)); + } + } + return result; + } + + @Override + public List getNodes() { + logger.debug(EELFLoggerDelegate.debugLogger, "getNodes"); + String json = getMockDataContent("/nodesHealth.json"); + TypeReference> typeRef = new TypeReference>() { + }; + List result = null; + try { + result = objectMapper.readValue(json, typeRef); + } catch (Exception ex) { + logger.error(EELFLoggerDelegate.errorLogger, "getNode failed", ex); + + } + return result; + } + + @Override + public List getDatacenters() { + logger.debug(EELFLoggerDelegate.debugLogger, "getDatacentersHealth"); + return null; + } + + /** + * Simple test + * + * @param args + * blueprint ID + * @throws DashboardControllerException + * On any failure + */ + public static void main(String[] args) throws DashboardControllerException { + logger.info("Testing paths and parsing mock data"); + ControllerRestClientMockImpl client = new ControllerRestClientMockImpl(); + CloudifyBlueprintList list1 = client.getBlueprints(); + CloudifyBlueprintList list2 = client.getBlueprint("mock"); + CloudifyDeploymentList list3 = client.getDeployments(); + CloudifyDeploymentList list4 = client.getDeployment("mock"); + CloudifyExecutionList list5 = client.getExecutions("mock"); + List list6 = client.getServices(); + List list7 = client.getNodes(); + List list8 = client.getServiceHealth("mock"); + List list9 = client.getServiceHealthHistory("mock", Instant.now(), Instant.now()); + if (list1 == null || list2 == null || list3 == null || list4 == null || list5 == null || list6 == null + || list7 == null || list8 == null || list9 == null) + throw new DashboardControllerException("Failed"); + logger.info("Pass."); + } + +} diff --git a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/service/ControllerEndpointServiceImpl.java b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/service/ControllerEndpointServiceImpl.java index b1c64b5..b4b3c97 100644 --- a/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/service/ControllerEndpointServiceImpl.java +++ b/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/service/ControllerEndpointServiceImpl.java @@ -1,76 +1,76 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.ccsdk.dashboard.service; - -import org.onap.ccsdk.dashboard.domain.ControllerEndpoint; -import org.openecomp.portalsdk.core.service.DataAccessService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -/** - * Complete controller endpoint information is in properties. The database just - * stores the user's selection. Users are not expected to enter credentials so - * this hybrid solution keeps credentials out of the database. - */ -@Service("controllerEndpointService") -@Transactional -public class ControllerEndpointServiceImpl implements ControllerEndpointService { - - @Autowired - private DataAccessService dataAccessService; - - /** - * @return Data access service - */ - public DataAccessService getDataAccessService() { - return dataAccessService; - } - - /** - * @param dataAccessService - * Data access service - */ - public void setDataAccessService(DataAccessService dataAccessService) { - this.dataAccessService = dataAccessService; - } - - @Override - public ControllerEndpoint getControllerEndpointSelection(long userId) { - return (ControllerEndpoint) getDataAccessService() - .getDomainObject(ControllerEndpoint.class, userId, null); - } - - @Override - public void updateControllerEndpointSelection(ControllerEndpoint endpoint) { - getDataAccessService().saveDomainObject(endpoint, null); - } - - @Override - public void deleteControllerEndpointSelection(long userId) { - ControllerEndpoint dbEntry = (ControllerEndpoint) getDataAccessService() - .getDomainObject(ControllerEndpoint.class, userId, null); - if (dbEntry != null) - getDataAccessService().deleteDomainObject(dbEntry, null); - } - -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.ccsdk.dashboard.service; + +import org.onap.ccsdk.dashboard.domain.ControllerEndpoint; +import org.onap.portalsdk.core.service.DataAccessService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * Complete controller endpoint information is in properties. The database just + * stores the user's selection. Users are not expected to enter credentials so + * this hybrid solution keeps credentials out of the database. + */ +@Service("controllerEndpointService") +@Transactional +public class ControllerEndpointServiceImpl implements ControllerEndpointService { + + @Autowired + private DataAccessService dataAccessService; + + /** + * @return Data access service + */ + public DataAccessService getDataAccessService() { + return dataAccessService; + } + + /** + * @param dataAccessService + * Data access service + */ + public void setDataAccessService(DataAccessService dataAccessService) { + this.dataAccessService = dataAccessService; + } + + @Override + public ControllerEndpoint getControllerEndpointSelection(long userId) { + return (ControllerEndpoint) getDataAccessService() + .getDomainObject(ControllerEndpoint.class, userId, null); + } + + @Override + public void updateControllerEndpointSelection(ControllerEndpoint endpoint) { + getDataAccessService().saveDomainObject(endpoint, null); + } + + @Override + public void deleteControllerEndpointSelection(long userId) { + ControllerEndpoint dbEntry = (ControllerEndpoint) getDataAccessService() + .getDomainObject(ControllerEndpoint.class, userId, null); + if (dbEntry != null) + getDataAccessService().deleteDomainObject(dbEntry, null); + } + +} diff --git a/ccsdk-app-common/src/main/java/org/onap/fusionapp/service/AdminAuthExtension.java b/ccsdk-app-common/src/main/java/org/onap/fusionapp/service/AdminAuthExtension.java index 8a64ab5..365ee05 100644 --- a/ccsdk-app-common/src/main/java/org/onap/fusionapp/service/AdminAuthExtension.java +++ b/ccsdk-app-common/src/main/java/org/onap/fusionapp/service/AdminAuthExtension.java @@ -1,42 +1,42 @@ -/******************************************************************************* - * =============LICENSE_START========================================================= - * - * ================================================================================= - * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - *******************************************************************************/ -package org.onap.fusionapp.service; - -import org.openecomp.portalsdk.core.domain.User; - -//@Service("adminAuthExtension") -//@Transactional -/** - * Extension supporting action on authorization of user - */ -public class AdminAuthExtension { - - /** - * @param user - * User who was authenticated - */ - public void saveUserExtension(User user) { - // app's developer implement their own logic here, like updating app's - // related tables - } - -} +/******************************************************************************* + * =============LICENSE_START========================================================= + * + * ================================================================================= + * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + *******************************************************************************/ +package org.onap.fusionapp.service; + +import org.onap.portalsdk.core.domain.User; + +//@Service("adminAuthExtension") +//@Transactional +/** + * Extension supporting action on authorization of user + */ +public class AdminAuthExtension { + + /** + * @param user + * User who was authenticated + */ + public void saveUserExtension(User user) { + // app's developer implement their own logic here, like updating app's + // related tables + } + +} -- cgit 1.2.3-korg