diff options
Diffstat (limited to 'adapters/mso-adapter-utils')
2 files changed, 144 insertions, 17 deletions
diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java index f39962b2ce..1fabcb279c 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoMulticloudUtils.java @@ -71,6 +71,7 @@ public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ 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); @@ -228,7 +229,13 @@ public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ 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) { @@ -287,7 +294,7 @@ public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ 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; @@ -296,11 +303,16 @@ public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ 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); @@ -371,6 +383,81 @@ public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ 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); } @@ -543,7 +630,9 @@ public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ } 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; @@ -596,7 +685,7 @@ public class MsoMulticloudUtils extends MsoHeatUtils implements VduPlugin{ 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); diff --git a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java index fbe532d1fe..e066c4eec6 100644 --- a/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java +++ b/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoMulticloudUtilsTest.java @@ -4,8 +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 @@ -23,6 +21,7 @@ package org.onap.so.openstack.utils; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; import static org.junit.Assert.assertEquals; @@ -30,12 +29,14 @@ import static org.junit.Assert.assertNotNull; 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; @@ -67,18 +68,43 @@ public class MsoMulticloudUtilsTest extends BaseTest { 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()); } @@ -96,6 +122,18 @@ public class MsoMulticloudUtilsTest extends BaseTest { 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(), @@ -131,7 +169,7 @@ public class MsoMulticloudUtilsTest extends BaseTest { @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(), @@ -146,7 +184,7 @@ public class MsoMulticloudUtilsTest extends BaseTest { @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(), |