From 11e9d33f2f50ad3990905fba184b7c10d255070a Mon Sep 17 00:00:00 2001 From: kooper Date: Mon, 4 Mar 2019 13:47:51 +0000 Subject: Add manifest non mano set support Issue-ID: SDC-2141 Change-Id: I056e8f7c1da8f17ce8583207660882f85a4d11a4 Signed-off-by: kooper --- .../pom.xml | 5 + .../services/impl/etsi/Configuration.java | 10 ++ .../services/impl/etsi/ETSIServiceImpl.java | 115 ++++++++++++++++++ .../services/impl/etsi/NonManoType.java | 29 +++++ .../src/main/resources/nonManoConfig.yaml | 19 +++ .../services/impl/etsi/ETSIServiceImplTest.java | 133 +++++++++++++++++++++ .../src/test/resources/nonManoConfig.yaml | 7 ++ 7 files changed, 318 insertions(+) create mode 100644 openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/Configuration.java create mode 100644 openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java create mode 100644 openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/NonManoType.java create mode 100644 openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/resources/nonManoConfig.yaml create mode 100644 openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java create mode 100644 openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/nonManoConfig.yaml (limited to 'openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core') diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/pom.xml b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/pom.xml index 450acd6583..08e8a0881a 100644 --- a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/pom.xml +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/pom.xml @@ -112,6 +112,11 @@ openecomp-sdc-vendor-license-api ${project.version} + + org.projectlombok + lombok + provided + diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/Configuration.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/Configuration.java new file mode 100644 index 0000000000..e2f9a4efaf --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/Configuration.java @@ -0,0 +1,10 @@ +package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; + +import java.util.Map; + +import lombok.Data; + +@Data +public class Configuration { + private Map nonManoKeyFolderMapping; +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java new file mode 100644 index 0000000000..b3002abf6e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImpl.java @@ -0,0 +1,115 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019, Nordix Foundation. 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.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; + +import org.apache.commons.io.IOUtils; +import org.onap.sdc.tosca.parser.utils.YamlToObjectConverter; +import org.openecomp.core.utilities.file.FileContentHandler; + + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import org.openecomp.sdc.tosca.csar.Manifest; + +import static org.openecomp.sdc.tosca.csar.CSARConstants.*; + +public class ETSIServiceImpl implements ETSIService { + + private Configuration configuration; + + public ETSIServiceImpl() throws IOException { + InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml"); + if(io == null){ + throw new IOException("Non Mano configuration not found"); + } + String data = IOUtils.toString(io, StandardCharsets.UTF_8); + YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); + configuration = yamlToObjectConverter.convertFromString(data, Configuration.class); + } + + public ETSIServiceImpl(Configuration configuration) { + this.configuration = configuration; + } + + @Override + public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) { + Map templates = handler.getFiles(); + return isMetaFilePresent(templates) && hasMetaMandatoryEntries(templates); + } + + @Override + public void moveNonManoFileToArtifactFolder(FileContentHandler handler, Manifest manifest) { + for (Map.Entry> entry : manifest.getNonManoSources().entrySet()) { + String e = entry.getKey(); + List k = entry.getValue(); + updateNonManoLocation(handler, e, k); + } + } + + private void updateNonManoLocation(FileContentHandler handler, String nonManoKey, List sources) { + Map files = handler.getFiles(); + for (String key : sources) { + if (files.containsKey(key)) { + updateLocation(key, nonManoKey, files); + } + } + } + + private void updateLocation(String key, String nonManoKey, Map files){ + if (nonManoKey == null || nonManoKey.isEmpty()) { + return; + } + Map map = configuration.getNonManoKeyFolderMapping(); + if (map.containsKey(nonManoKey)) { + NonManoType nonManoPair = map.get(nonManoKey); + String newLocation = nonManoPair.getType() + "/" + + nonManoPair.getLocation() + "/" + getFileName(key); + if (!files.containsKey(newLocation)) { + files.put(newLocation, files.remove(key)); + } + } + } + + + private String getFileName(String key) { + return key.substring(key.lastIndexOf('/') + 1); + } + + private boolean hasMetaMandatoryEntries(Map templates) { + Optional meta = templates.entrySet().stream().filter(e -> e.getKey().equals(TOSCA_META_PATH_FILE_NAME) + || e.getKey().equals(TOSCA_META_ORIG_PATH_FILE_NAME)).findFirst().map(Map.Entry::getValue); + if (!meta.isPresent()) { + return false; + } + String metaContent = new String(meta.get(), StandardCharsets.UTF_8); + return metaContent.contains(TOSCA_META_ENTRY_DEFINITIONS) && metaContent.contains(TOSCA_META_ENTRY_MANIFEST) + && metaContent.contains(TOSCA_META_ENTRY_CHANGE_LOG); + } + + private boolean isMetaFilePresent(Map handler) { + return handler.containsKey(TOSCA_META_PATH_FILE_NAME) || handler.containsKey(TOSCA_META_ORIG_PATH_FILE_NAME); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/NonManoType.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/NonManoType.java new file mode 100644 index 0000000000..e10c102593 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/NonManoType.java @@ -0,0 +1,29 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019, Nordix Foundation. 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.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; + + +import lombok.Data; + +@Data +public class NonManoType { + private String location; + private String type; +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/resources/nonManoConfig.yaml b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/resources/nonManoConfig.yaml new file mode 100644 index 0000000000..c444e66c48 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/resources/nonManoConfig.yaml @@ -0,0 +1,19 @@ +nonManoKeyFolderMapping: + onap_ves_events: + location: VES_EVENTS + type: Deployment + onap_pm_dictionary: + location: PM_DICTIONARY + type: Deployment + onap_yang_modules: + location: YANG_MODULES + type: Deployment + onap_ansible_playbooks: + location: ANSIBLE_PLAYBOOK + type: Deployment + onap_scripts: + location: SCRIPTS + type: Deployment + onap_others: + location: OTHER + type: Informational \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java new file mode 100644 index 0000000000..ae69415f1d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/java/org/openecomp/sdc/vendorsoftwareproduct/services/impl/etsi/ETSIServiceImplTest.java @@ -0,0 +1,133 @@ +package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.sdc.tosca.parser.utils.YamlToObjectConverter; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.sdc.tosca.csar.Manifest; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ETSIServiceImplTest { + private ETSIService etsiService; + private String sol004MetaFile = "TOSCA-Meta-Version: 1.0\n" + + "CSAR-Version: 1.0\n" + + "Created-By: Kuku\n" + + "Entry-Definitions: MainServiceTemplate.yaml\n" + + "Entry-Manifest: MainServiceTemplate.mf\n" + + "Entry-Change-Log: MainServiceTemplate.log"; + private String metaFile = "TOSCA-Meta-Version: 1.0\n" + + "CSAR-Version: 1.0\n" + + "Created-By: Kuku\n" + + "Entry-Definitions: MainServiceTemplate.yaml"; + + private String finalNonManoLocation = "Deployment/VES_EVENTS/test.xml"; + private String finalOtherNonManoLocation = "Informational/OTHER/test.xml"; + + @Before + public void setUp() throws IOException { + YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); + Configuration configuration = yamlToObjectConverter.convert("src/test/resources", + Configuration.class, "nonManoConfig.yaml"); + etsiService = new ETSIServiceImpl(configuration); + } + + @After + public void tearDown() { + etsiService = null; + } + + @Test + public void testIsSol004TrueOrigin() { + FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", sol004MetaFile.getBytes(StandardCharsets.UTF_8)); + assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); + } + + @Test + public void testIsSol004True() { + FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta", sol004MetaFile.getBytes(StandardCharsets.UTF_8)); + assertTrue(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); + } + + @Test + public void testIsSol004False() { + FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("TOSCA-Metadata/TOSCA.meta.original", metaFile.getBytes(StandardCharsets.UTF_8)); + assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); + } + + @Test + public void testIsSol004FalseWithNull() { + FileContentHandler fileContentHandler = new FileContentHandler(); + assertFalse(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)); + } + + @Test + public void testMoveNonManoFileToArtifactFolder() throws IOException { + Map> nonManoSources = new HashMap<>(); + List sources = new ArrayList<>(); + sources.add("Some/test.xml"); + nonManoSources.put("Some", sources); + FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("Some/test.xml", new byte[1]); + Manifest manifest = mock(Manifest.class); + when(manifest.getNonManoSources()).thenReturn(nonManoSources); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest); + assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); + } + + @Test + public void testMoveNonManoFileToArtifactFolderFileNotInFolder() throws IOException { + Map> nonManoSources = new HashMap<>(); + List sources = new ArrayList<>(); + sources.add("test.xml"); + nonManoSources.put("foo", sources); + FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("test.xml", new byte[1]); + Manifest manifest = mock(Manifest.class); + when(manifest.getNonManoSources()).thenReturn(nonManoSources); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest); + assertFalse(fileContentHandler.containsFile(finalOtherNonManoLocation)); + } + + @Test + public void testMoveNonManoFileToArtifactFolderNoMove() throws IOException { + Map> nonManoSources = new HashMap<>(); + List sources = new ArrayList<>(); + sources.add(finalNonManoLocation); + nonManoSources.put("Some", sources); + FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile(finalNonManoLocation, new byte[1]); + Manifest manifest = mock(Manifest.class); + when(manifest.getNonManoSources()).thenReturn(nonManoSources); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest); + assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); + } + + @Test + public void testMoveNonManoFileToArtifactFolderMoveToKeyFolder() throws IOException { + Map> nonManoSources = new HashMap<>(); + List sources = new ArrayList<>(); + sources.add("Artifacts/Deployment/test.xml"); + nonManoSources.put("Some", sources); + FileContentHandler fileContentHandler = new FileContentHandler(); + fileContentHandler.addFile("Artifacts/Deployment/test.xml", new byte[1]); + Manifest manifest = mock(Manifest.class); + when(manifest.getNonManoSources()).thenReturn(nonManoSources); + etsiService.moveNonManoFileToArtifactFolder(fileContentHandler, manifest); + assertTrue(fileContentHandler.containsFile(finalNonManoLocation)); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/nonManoConfig.yaml b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/nonManoConfig.yaml new file mode 100644 index 0000000000..32c6b1ce57 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/test/resources/nonManoConfig.yaml @@ -0,0 +1,7 @@ +nonManoKeyFolderMapping: + Some: + location: VES_EVENTS + type: Deployment + Other: + location: VES_EVENTS + type: Informational \ No newline at end of file -- cgit 1.2.3-korg