diff options
author | Tomasz Golabek <tomasz.golabek@nokia.com> | 2019-02-21 14:05:41 +0100 |
---|---|---|
committer | Tomasz Golabek <tomasz.golabek@nokia.com> | 2019-02-25 12:24:43 +0100 |
commit | 26788e7e2f25cd296efa187cb5c911843d00c9a8 (patch) | |
tree | 6cca44131af06537e2adfc2beb6f057c9e0c603c /src | |
parent | f08a27fede1086e17632dde816e0ae6214cab286 (diff) |
Parsing CSAR to retrieve policy model
Content of policies.xml taken from the CSAR file and ready to store into the db.
Change-Id: I9ee0d0474fa355ae4c9db1e1ee40a3f087002e88
Issue-ID: CLAMP-261
Signed-off-by: Tomasz Golabek <tomasz.golabek@nokia.com>
Diffstat (limited to 'src')
8 files changed, 98 insertions, 6 deletions
diff --git a/src/main/java/org/onap/clamp/clds/exception/policy/PolicyModelException.java b/src/main/java/org/onap/clamp/clds/exception/policy/PolicyModelException.java new file mode 100644 index 000000000..0b64b78ea --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/exception/policy/PolicyModelException.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2019 Nokia 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============================================ + * =================================================================== + * + */ + +package org.onap.clamp.clds.exception.policy; + +public class PolicyModelException extends Exception { + + public PolicyModelException(String msg, Throwable throwable) { + super(msg, throwable); + } +} diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java b/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java index 5959c0fb5..c9405d202 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java @@ -33,6 +33,7 @@ import java.util.concurrent.ThreadLocalRandom; import org.onap.clamp.clds.config.ClampProperties;
import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration;
+import org.onap.clamp.clds.exception.policy.PolicyModelException;
import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
import org.onap.clamp.clds.exception.sdc.controller.SdcControllerException;
@@ -257,6 +258,10 @@ public class SdcSingleController { logger.error("SdcDownloadException exception caught during the notification processing", e);
sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DOWNLOAD,
DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage());
+ } catch (PolicyModelException e) {
+ logger.error("PolicyModelException exception caught during the notification processing", e);
+ sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,
+ DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
} catch (InterruptedException e) {
logger.error("Interrupt exception caught during the notification processing", e);
sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,
@@ -266,7 +271,7 @@ public class SdcSingleController { logger.error("Unexpected exception caught during the notification processing", e);
sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,
DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
- } finally {
+ } finally {
this.changeControllerStatus(SdcSingleControllerStatus.IDLE);
}
}
diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java index 1a99919ee..f2c75ef28 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandler.java @@ -36,6 +36,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -68,6 +69,7 @@ public class CsarHandler { private INotificationData sdcNotification; public static final String RESOURCE_INSTANCE_NAME_PREFIX = "/Artifacts/Resources/"; public static final String RESOURCE_INSTANCE_NAME_SUFFIX = "/Deployment/"; + public static final String POLICY_DEFINITION_NAME_SUFFIX = "Definitions/policies.yml"; public CsarHandler(INotificationData iNotif, String controller, String clampCsarPath) throws CsarHandlerException { this.sdcNotification = iNotif; @@ -167,4 +169,17 @@ public class CsarHandler { public Map<String, BlueprintArtifact> getMapOfBlueprints() { return mapOfBlueprints; } + + Optional<String> getPolicyModelYaml() throws IOException { + String result = null; + try (ZipFile zipFile = new ZipFile(csarFilePath)) { + ZipEntry entry = zipFile.getEntry(POLICY_DEFINITION_NAME_SUFFIX); + if (entry != null) { + result = IOUtils.toString(zipFile.getInputStream(entry)); + } else{ + logger.info("Policy model not found inside the CSAR file: " + csarFilePath); + } + return Optional.ofNullable(result); + } + } } diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstaller.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstaller.java index b5c025ecb..12a761dbf 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstaller.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstaller.java @@ -23,11 +23,13 @@ package org.onap.clamp.clds.sdc.controller.installer; +import org.onap.clamp.clds.exception.policy.PolicyModelException; import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException; public interface CsarInstaller { boolean isCsarAlreadyDeployed(CsarHandler csar) throws SdcArtifactInstallerException; - public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException, InterruptedException; + public void installTheCsar(CsarHandler csar) + throws SdcArtifactInstallerException, InterruptedException, PolicyModelException; } diff --git a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java index bfda6924d..6841b87b0 100644 --- a/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java +++ b/src/main/java/org/onap/clamp/clds/sdc/controller/installer/CsarInstallerImpl.java @@ -34,6 +34,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import javax.annotation.PostConstruct; import javax.xml.transform.TransformerException; @@ -43,6 +44,7 @@ import org.onap.clamp.clds.client.DcaeInventoryServices; import org.onap.clamp.clds.config.sdc.BlueprintParserFilesConfiguration; import org.onap.clamp.clds.config.sdc.BlueprintParserMappingConfiguration; import org.onap.clamp.clds.dao.CldsDao; +import org.onap.clamp.clds.exception.policy.PolicyModelException; import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException; import org.onap.clamp.clds.model.CldsModel; import org.onap.clamp.clds.model.CldsTemplate; @@ -125,7 +127,8 @@ public class CsarInstallerImpl implements CsarInstaller { @Override @Transactional - public void installTheCsar(CsarHandler csar) throws SdcArtifactInstallerException, InterruptedException { + public void installTheCsar(CsarHandler csar) + throws SdcArtifactInstallerException, InterruptedException, PolicyModelException { try { logger.info("Installing the CSAR " + csar.getFilePath()); for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) { @@ -135,6 +138,7 @@ public class CsarInstallerImpl implements CsarInstaller { this.searchForRightMapping(blueprint.getValue())), queryDcaeToGetServiceTypeId(blueprint.getValue())); } + createPolicyModel(csar); logger.info("Successfully installed the CSAR " + csar.getFilePath()); } catch (IOException e) { throw new SdcArtifactInstallerException("Exception caught during the Csar installation in database", e); @@ -143,6 +147,15 @@ public class CsarInstallerImpl implements CsarInstaller { } } + private void createPolicyModel(CsarHandler csar) throws PolicyModelException { + try{ + Optional<String> policyModelYaml = csar.getPolicyModelYaml(); + // save policy model into the database + } catch (IOException e) { + throw new PolicyModelException("TransformerException when decoding the YamlText", e); + } + } + private BlueprintParserFilesConfiguration searchForRightMapping(BlueprintArtifact blueprintArtifact) throws SdcArtifactInstallerException { List<BlueprintParserFilesConfiguration> listConfig = new ArrayList<>(); diff --git a/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java b/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java index 2c24dcb5c..0df252388 100644 --- a/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java +++ b/src/test/java/org/onap/clamp/clds/it/sdc/controller/installer/CsarInstallerItCase.java @@ -42,6 +42,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.onap.clamp.clds.dao.CldsDao; +import org.onap.clamp.clds.exception.policy.PolicyModelException; import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException; import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException; import org.onap.clamp.clds.model.CldsModel; @@ -78,7 +79,7 @@ public class CsarInstallerItCase { @Test(expected = SdcArtifactInstallerException.class) public void testInstallTheCsarFail() throws SdcArtifactInstallerException, SdcToscaParserException, - CsarHandlerException, IOException, InterruptedException { + CsarHandlerException, IOException, InterruptedException, PolicyModelException { CsarHandler csarHandler = Mockito.mock(CsarHandler.class); BlueprintArtifact blueprintArtifact = Mockito.mock(BlueprintArtifact.class); Mockito.when(blueprintArtifact.getResourceAttached()).thenReturn(Mockito.mock(IResourceInstance.class)); @@ -145,7 +146,7 @@ public class CsarInstallerItCase { @Test public void testIsCsarAlreadyDeployedTca() throws SdcArtifactInstallerException, SdcToscaParserException, - CsarHandlerException, IOException, InterruptedException { + CsarHandlerException, IOException, InterruptedException, PolicyModelException { String generatedName = RandomStringUtils.randomAlphanumeric(5); CsarHandler csarHandler = buildFakeCsarHandler(generatedName); assertFalse(csarInstaller.isCsarAlreadyDeployed(csarHandler)); @@ -155,7 +156,7 @@ public class CsarInstallerItCase { @Test public void testInstallTheCsarTca() throws SdcArtifactInstallerException, SdcToscaParserException, - CsarHandlerException, IOException, JSONException, InterruptedException { + CsarHandlerException, IOException, JSONException, InterruptedException, PolicyModelException { String generatedName = RandomStringUtils.randomAlphanumeric(5); CsarHandler csar = buildFakeCsarHandler(generatedName); csarInstaller.installTheCsar(csar); diff --git a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java index bb4fd01f2..544c8ca1d 100644 --- a/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java +++ b/src/test/java/org/onap/clamp/clds/sdc/controller/installer/CsarHandlerTest.java @@ -24,6 +24,7 @@ package org.onap.clamp.clds.sdc.controller.installer; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; @@ -115,6 +116,13 @@ public class CsarHandlerTest { return resultArtifact; } + private IDistributionClientDownloadResult buildFakeSdcResultWithoutPolicyModel() throws IOException { + IDistributionClientDownloadResult resultArtifact = Mockito.mock(IDistributionClientDownloadResult.class); + Mockito.when(resultArtifact.getArtifactPayload()).thenReturn( + IOUtils.toByteArray(ResourceFileUtil.getResourceAsStream("example/sdc/service-without-policy.csar"))); + return resultArtifact; + } + @Test public void testSave() throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException { @@ -148,6 +156,23 @@ public class CsarHandlerTest { } @Test + public void testLoadingOfPolicyModelFromCsar() + throws CsarHandlerException, IOException, SdcArtifactInstallerException, SdcToscaParserException { + CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests"); + csar.save(buildFakeSdcResut()); + String policyModelYaml = csar.getPolicyModelYaml().get(); + assertTrue(policyModelYaml.contains("tosca_simple_yaml_1_1")); + } + + @Test + public void testLoadingOfNonexistentPolicyModelFromCsar() + throws CsarHandlerException, IOException, SdcArtifactInstallerException, SdcToscaParserException { + CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests"); + csar.save(buildFakeSdcResultWithoutPolicyModel()); + assertFalse(csar.getPolicyModelYaml().isPresent()); + } + + @Test public void testDoubleSave() throws SdcArtifactInstallerException, SdcToscaParserException, CsarHandlerException, IOException { CsarHandler csar = new CsarHandler(buildFakeSdcNotification(), "test-controller", "/tmp/csar-handler-tests"); diff --git a/src/test/resources/example/sdc/service-without-policy.csar b/src/test/resources/example/sdc/service-without-policy.csar Binary files differnew file mode 100644 index 000000000..20c4eef65 --- /dev/null +++ b/src/test/resources/example/sdc/service-without-policy.csar |