Merge "Fixed Sonar blocker issues"
authorSteve Smokowski <ss835w@att.com>
Fri, 5 Apr 2019 13:45:29 +0000 (13:45 +0000)
committerGerrit Code Review <gerrit@onap.org>
Fri, 5 Apr 2019 13:45:29 +0000 (13:45 +0000)
1  2 
adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java
adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java

@@@ -71,6 -71,7 +71,7 @@@ public class MsoMulticloudUtils extend
      public static final String VNF_ID = "vnf_id";
      public static final String VF_MODULE_ID = "vf_module_id";
      public static final String TEMPLATE_TYPE = "template_type";
+     public static final String MULTICLOUD_QUERY_BODY_NULL = "multicloudQueryBody is null";
      public static final List<String> MULTICLOUD_INPUTS =
              Arrays.asList(OOF_DIRECTIVES, SDNC_DIRECTIVES, USER_DIRECTIVES, TEMPLATE_TYPE);
  
              if (logger.isDebugEnabled()) {
                  logger.debug("Multicloud Create Response Body: {}", multicloudResponseBody);
              }
 -            return getStackStatus(cloudSiteId, cloudOwner, tenantId, canonicalName, pollForCompletion, timeoutMinutes, backout);
 +            StackInfo stackStatus = getStackStatus(cloudSiteId, cloudOwner, tenantId, canonicalName, pollForCompletion, timeoutMinutes, backout);
 +
 +            if (HeatStatus.CREATED.equals(stackStatus.getStatus())) {
 +                multicloudAaiUpdate(cloudSiteId, cloudOwner, tenantId, genericVnfId, vfModuleId, multicloudResponseBody.getWorkloadId(), pollForCompletion, timeoutMinutes);
 +            }
 +
 +            return stackStatus;
          }
          StringBuilder stackErrorStatusReason = new StringBuilder(response.getStatusInfo().getReasonPhrase());
          if (null != multicloudResponseBody) {
          if (multicloudClient != null) {
              Response response = multicloudClient.get();
              if (logger.isDebugEnabled()) {
 -                logger.debug (String.format("Mulicloud GET Response: %s", response.toString()));
 +                logger.debug (String.format("Multicloud GET Response: %s", response.toString()));
              }
  
              MulticloudQueryResponse multicloudQueryBody = null;
                  returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
              } else if (response.getStatus() == Response.Status.OK.getStatusCode() && response.hasEntity()) {
                  multicloudQueryBody = getQueryBody((java.io.InputStream)response.getEntity());
-                 returnInfo.setCanonicalName(stackName + "/" + multicloudQueryBody.getWorkloadId());
-                 returnInfo.setStatus(getHeatStatus(multicloudQueryBody.getWorkloadStatus()));
-                 returnInfo.setStatusMessage(multicloudQueryBody.getWorkloadStatus());
-                 if (logger.isDebugEnabled()) {
-                     logger.debug("Multicloud Create Response Body: " + multicloudQueryBody.toString());
+                 if (multicloudQueryBody != null) {
+                     returnInfo.setCanonicalName(stackName + "/" + multicloudQueryBody.getWorkloadId());
+                     returnInfo.setStatus(getHeatStatus(multicloudQueryBody.getWorkloadStatus()));
+                     returnInfo.setStatusMessage(multicloudQueryBody.getWorkloadStatus());
+                     if (logger.isDebugEnabled()) {
+                         logger.debug("Multicloud Create Response Body: " + multicloudQueryBody.toString());
+                     }
+                 } else {
+                     returnInfo.setStatus(HeatStatus.FAILED);
+                     returnInfo.setStatusMessage(MULTICLOUD_QUERY_BODY_NULL);
                  }
              } else {
                  returnInfo.setStatus(HeatStatus.FAILED);
          return HeatStatus.UNKNOWN;
      }
  
 +    private void multicloudAaiUpdate(String cloudSiteId, String cloudOwner, String tenantId, String genericVnfId, String vfModuleId, String workloadId,
 +            boolean pollForCompletion, int timeoutMinutes) {
 +
 +        MulticloudRequest multicloudRequest= new MulticloudRequest();
 +
 +        multicloudRequest.setGenericVnfId(genericVnfId);
 +        multicloudRequest.setVfModuleId(vfModuleId);
 +
 +        String multicloudEndpoint = getMulticloudEndpoint(cloudSiteId, cloudOwner, workloadId);
 +        RestClient multicloudClient = getMulticloudClient(multicloudEndpoint);
 +
 +        if (multicloudClient == null) {
 +            if (logger.isDebugEnabled())
 +                logger.debug("Multicloud client could not be initialized");
 +        }
 +
 +        Response response = multicloudClient.post(multicloudRequest);
 +        if (response.getStatus() != Response.Status.ACCEPTED.getStatusCode()) {
 +            if (logger.isDebugEnabled())
 +                logger.debug("Multicloud AAI update request failed: " + response.getStatus() + response.getStatusInfo());
 +            return;
 +        }
 +
 +        if (!pollForCompletion) {
 +            return;
 +        }
 +
 +        int updatePollInterval = Integer.parseInt(this.environment.getProperty(createPollIntervalProp, createPollIntervalDefault));
 +        int pollTimeout = (timeoutMinutes * 60) + updatePollInterval;
 +        boolean updateTimedOut = false;
 +        logger.debug("updatePollInterval=" + updatePollInterval + ", pollTimeout=" + pollTimeout);
 +
 +        StackInfo stackInfo = null;
 +        while (true) {
 +            try {
 +                stackInfo = queryStack(cloudSiteId, cloudOwner, tenantId, workloadId);
 +                if (logger.isDebugEnabled())
 +                    logger.debug (stackInfo.getStatus() + " (" + workloadId + ")");
 +
 +                if (HeatStatus.UPDATING.equals(stackInfo.getStatus())) {
 +                    if (pollTimeout <= 0) {
 +                        // Note that this should not occur, since there is a timeout specified
 +                        // in the Openstack (multicloud?) call.
 +                        if (logger.isDebugEnabled())
 +                            logger.debug("Multicloud AAI update timeout failure: {} {} {} {}", cloudOwner, cloudSiteId, tenantId, workloadId);
 +                        updateTimedOut = true;
 +                        break;
 +                    }
 +
 +                    sleep(updatePollInterval * 1000L);
 +
 +                    pollTimeout -= updatePollInterval;
 +                    if (logger.isDebugEnabled())
 +                        logger.debug("pollTimeout remaining: " + pollTimeout);
 +                } else {
 +                    break;
 +                }
 +            } catch (MsoException me) {
 +                if (logger.isDebugEnabled())
 +                    logger.debug("Multicloud AAI update exception: {} {} {} {}", cloudOwner, cloudSiteId, tenantId, workloadId, me);
 +                return;
 +            }
 +        }
 +        if (updateTimedOut) {
 +            if (logger.isDebugEnabled())
 +                logger.debug("Multicloud AAI update request failed: {} {}", response.getStatus(), response.getStatusInfo().toString());
 +        } else if (!HeatStatus.UPDATED.equals(stackInfo.getStatus())) {
 +            if (logger.isDebugEnabled())
 +                logger.debug("Multicloud AAI update request failed: {} {}", response.getStatus(), response.getStatusInfo().toString());
 +        } else {
 +            if (logger.isDebugEnabled())
 +                logger.debug("Multicloud AAI update successful: {} {}", response.getStatus(), response.getStatusInfo().toString());
 +        }
 +    }
 +
      private StackInfo getStackStatus(String cloudSiteId, String cloudOwner, String tenantId, String instanceId) throws MsoException {
          return getStackStatus(cloudSiteId, cloudOwner, tenantId, instanceId, false, 0, false);
      }
      }
  
      private HeatStatus mapResponseToHeatStatus(Response response) {
-         if (response.getStatusInfo().getStatusCode() == Response.Status.OK.getStatusCode()) {
+         if (response == null) {
+             return HeatStatus.FAILED;
+         } else if (response.getStatusInfo().getStatusCode() == Response.Status.OK.getStatusCode()) {
              return HeatStatus.CREATED;
          } else if (response.getStatusInfo().getStatusCode() == Response.Status.CREATED.getStatusCode()) {
              return HeatStatus.CREATED;
          return null;
      }
  
 -    private String getMulticloudEndpoint(String cloudSiteId, String cloudOwner, String workloadId) throws MsoCloudSiteNotFound {
 +    private String getMulticloudEndpoint(String cloudSiteId, String cloudOwner, String workloadId) {
          String msbIp = System.getenv().get(ONAP_IP);
          if (null == msbIp || msbIp.isEmpty()) {
              msbIp = environment.getProperty("mso.msb-ip", DEFAULT_MSB_IP);
@@@ -4,8 -4,6 +4,6 @@@
   * ================================================================================
   * Copyright (C) 2019 Samsung Intellectual Property. All rights reserved.
   * ================================================================================
-  * Modifications Copyright (c) 2019 Samsung
-  * ================================================================================
   * 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
@@@ -31,13 -29,13 +29,14 @@@ import static org.junit.Assert.assertNo
  import static org.junit.Assert.assertTrue;
  import static org.junit.Assert.fail;
  import static org.mockito.Mockito.when;
+ import static org.onap.so.openstack.utils.MsoMulticloudUtils.MULTICLOUD_QUERY_BODY_NULL;
  
  import java.io.IOException;
  import java.util.HashMap;
  import java.util.Optional;
  
  import org.apache.http.HttpStatus;
 +import org.junit.Before;
  import org.junit.Ignore;
  import org.junit.Test;
  import org.mockito.InjectMocks;
@@@ -69,43 -67,18 +68,43 @@@ public class MsoMulticloudUtilsTest ext
  
      private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
          + "\"TEST-workload\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
 +    private static final String UPDATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
 +            + "\"TEST-workload\"}";
 +    private static final String GET_CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
 +            + "\"TEST-workload\", \"workload_status\": \"CREATE_COMPLETE\"}";
 +    private static final String GET_UPDATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
 +            + "\"TEST-workload\", \"workload_status\": \"UPDATE_COMPLETE\"}";
  
 -    private static final String MULTICLOUD_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
 +    private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload";
 +    private static final String MULTICLOUD_UPDATE_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
 +    private static final String MULTICLOUD_GET_PATH = "/api/multicloud/v1/CloudOwner/MTN14/infra_workload/TEST-workload";
  
      @Test
      public void createStackSuccess() throws MsoException, IOException {
 -        wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
 +        wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
              .willReturn(aResponse().withHeader("Content-Type", "application/json")
                  .withBody(CREATE_STACK_RESPONSE)
 -                .withStatus(HttpStatus.SC_CREATED)));
 +                .withStatus(HttpStatus.SC_CREATED))
 +            .willSetStateTo("CREATING"));
 +        wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH))
 +                .inScenario("CREATE").whenScenarioStateIs("CREATING")
 +                .willReturn(aResponse().withHeader("Content-Type", "application/json")
 +                    .withBody(GET_CREATE_STACK_RESPONSE)
 +                    .withStatus(HttpStatus.SC_OK)));
 +        wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH)).inScenario("CREATE")
 +                .willReturn(aResponse().withHeader("Content-Type", "application/json")
 +                    .withBody(UPDATE_STACK_RESPONSE)
 +                    .withStatus(HttpStatus.SC_ACCEPTED))
 +                .willSetStateTo("UPDATING"));
 +        wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH))
 +                .inScenario("CREATE").whenScenarioStateIs("UPDATING")
 +                .willReturn(aResponse().withHeader("Content-Type", "application/json")
 +                    .withBody(GET_UPDATE_STACK_RESPONSE)
 +                    .withStatus(HttpStatus.SC_OK)));
          StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
 -            "TEST-heat", new HashMap<>(), false, 200, "TEST-env",
 +            "TEST-heat", new HashMap<>(), true, 200, "TEST-env",
              new HashMap<>(), new HashMap<>(), false);
 +        wireMockServer.resetScenarios();
          assertNotNull(result);
          assertEquals("TEST-stack", result.getName());
      }
          assertTrue(HeatStatus.NOTFOUND == result.getStatus());
      }
  
+     @Test
+     public void queryStackWithNullMulticloudQueryBody() throws MsoException {
+         wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/instanceId"))
+             .willReturn(aResponse().withHeader("Content-Type", "application/json")
+                 .withBody(CREATE_STACK_RESPONSE)
+                 .withStatus(HttpStatus.SC_OK)));
+         StackInfo result = multicloudUtils.queryStack("MTN13", "CloudOwner", "TEST-tenant", "instanceId");
+         assertTrue(HeatStatus.FAILED == result.getStatus());
+         assertEquals(MULTICLOUD_QUERY_BODY_NULL, result.getStatusMessage());
+     }
      @Test(expected = VduException.class)
      public void updateVdu() throws MsoException {
          multicloudUtils.updateVdu(new CloudInfo(), "instanceId", new HashMap<>(), new  VduModelInfo(),
      @Test
      public void createStackBadRequest() {
          try {
 -            wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
 +            wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH))
                  .willReturn(aResponse().withHeader("Content-Type", "application/json")
                      .withStatus(HttpStatus.SC_BAD_REQUEST)));
              multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),
  
      @Test
      public void createStackEmptyResponseEntity() throws MsoException {
 -        wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_PATH))
 +        wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH))
              .willReturn(aResponse().withHeader("Content-Type", "application/json")
                  .withStatus(HttpStatus.SC_CREATED)));
          StackInfo result = multicloudUtils.createStack("MTN14", "CloudOwner", "TEST-tenant", "TEST-stack", new VduModelInfo(),