9a7312b0fbfa127218ce1d33a9838954c4a15dfb
[sdc.git] / catalog-be-plugins / etsi-nfv-nsd-csar-plugin / src / main / java / org / openecomp / sdc / be / plugins / etsi / nfv / nsd / generator / EtsiNfvNsCsarEntryGenerator.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
20
21 import static org.openecomp.sdc.common.api.ArtifactTypeEnum.ETSI_PACKAGE;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.plugins.CsarEntryGenerator;
29 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException;
30 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.factory.EtsiNfvNsdCsarGeneratorFactory;
31 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.config.EtsiVersion;
32 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.NsdCsar;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Generates a Network Service CSAR based on a SERVICE component and wraps it in a SDC CSAR entry.
38  */
39 @org.springframework.stereotype.Component("etsiNfvNsCsarEntryGenerator")
40 public class EtsiNfvNsCsarEntryGenerator implements CsarEntryGenerator {
41
42     static final String ETSI_NS_COMPONENT_CATEGORY = "ETSI NFV Network Service";
43     static final String NSD_FILE_PATH_FORMAT = "Artifacts/%s/%s.%s";
44     static final String SIGNED_CSAR_EXTENSION = "zip";
45     static final String UNSIGNED_CSAR_EXTENSION = "csar";
46     static final String ETSI_VERSION_METADATA = "ETSI Version";
47     private static final Logger LOGGER = LoggerFactory.getLogger(EtsiNfvNsCsarEntryGenerator.class);
48     private final EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory;
49
50     public EtsiNfvNsCsarEntryGenerator(final EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory) {
51         this.etsiNfvNsdCsarGeneratorFactory = etsiNfvNsdCsarGeneratorFactory;
52     }
53
54     /**
55      * Generates a Network Service CSAR based on a SERVICE component of category {@link EtsiNfvNsCsarEntryGenerator#ETSI_NS_COMPONENT_CATEGORY} and
56      * wraps it in a SDC CSAR entry.
57      *
58      * @param component the component to create the NS CSAR from
59      * @return an entry to be added in the Component CSAR by SDC
60      */
61     @Override
62     public Map<String, byte[]> generateCsarEntries(final Component component) {
63         final String componentName = component == null ? "null" : component.getName();
64         if (component == null || ComponentTypeEnum.SERVICE != component.getComponentType()) {
65             LOGGER.debug("Ignoring NSD CSAR generation for component '{}' as it is not a SERVICE", componentName);
66             return Collections.emptyMap();
67         }
68         final boolean isEOTemplate = component.getCategories().stream().anyMatch(category -> ETSI_NS_COMPONENT_CATEGORY.equals(category.getName()));
69         if (!isEOTemplate) {
70             LOGGER.debug("Ignoring NSD CSAR generation for component '{}' as it does not belong to the category '{}'", componentName,
71                 ETSI_NS_COMPONENT_CATEGORY);
72             return Collections.emptyMap();
73         }
74
75         final NsdCsar nsdCsar;
76         try {
77             final EtsiVersion etsiVersion = getComponentEtsiVersion(component);
78             final EtsiNfvNsdCsarGenerator etsiNfvNsdCsarGenerator = etsiNfvNsdCsarGeneratorFactory.create(etsiVersion);
79             nsdCsar = etsiNfvNsdCsarGenerator.generateNsdCsar(component);
80         } catch (final NsdException e) {
81             LOGGER.error("Could not create NSD CSAR entry for component '{}'", component.getName(), e);
82             return Collections.emptyMap();
83         } catch (final Exception e) {
84             LOGGER.error("Could not create NSD CSAR entry for component '{}'. An unexpected exception occurred", component.getName(), e);
85             return Collections.emptyMap();
86         }
87
88         return createEntry(nsdCsar);
89     }
90
91     private EtsiVersion getComponentEtsiVersion(Component component) {
92         final String etsiVersion = component.getCategorySpecificMetadata().get(ETSI_VERSION_METADATA);
93         return EtsiVersion.convertOrNull(etsiVersion);
94     }
95
96     private Map<String, byte[]> createEntry(final NsdCsar nsdCsar) {
97         final Map<String, byte[]> entryMap = new HashMap<>();
98         final String extension = nsdCsar.isSigned() ? SIGNED_CSAR_EXTENSION : UNSIGNED_CSAR_EXTENSION;
99         final String entryKey = String.format(NSD_FILE_PATH_FORMAT, ETSI_PACKAGE, nsdCsar.getFileName(), extension);
100         entryMap.put(entryKey, nsdCsar.getCsarPackage());
101         return entryMap;
102     }
103 }