ff20a3e70b32e3e079851ca80060468d16178d93
[sdc.git] /
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
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
21
22 import static org.openecomp.sdc.common.api.ArtifactTypeEnum.ETSI_PACKAGE;
23
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.plugins.CsarEntryGenerator;
30 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException;
31 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.factory.EtsiNfvNsdCsarGeneratorFactory;
32 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.config.EtsiVersion;
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     private static final Logger LOGGER = LoggerFactory.getLogger(EtsiNfvNsCsarEntryGenerator.class);
43     static final String ETSI_NS_COMPONENT_CATEGORY = "ETSI NFV Network Service";
44     static final String NSD_FILE_PATH_FORMAT = "Artifacts/%s/%s.csar";
45     static final String ETSI_VERSION_METADATA = "ETSI Version";
46
47     private final EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory;
48
49     public EtsiNfvNsCsarEntryGenerator(final EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory) {
50         this.etsiNfvNsdCsarGeneratorFactory = etsiNfvNsdCsarGeneratorFactory;
51     }
52
53     /**
54      * Generates a Network Service CSAR based on a SERVICE component of category {@link
55      * EtsiNfvNsCsarEntryGenerator#ETSI_NS_COMPONENT_CATEGORY} and wraps it in a SDC CSAR entry.
56      *
57      * @param component the component to create the NS CSAR from
58      * @return an entry to be added in the Component CSAR by SDC
59      */
60     @Override
61     public Map<String, byte[]> generateCsarEntries(final Component component) {
62         final String componentName = component == null ? "null" : component.getName();
63         if (component == null || ComponentTypeEnum.SERVICE != component.getComponentType()) {
64             LOGGER.debug("Ignoring NSD CSAR generation for component '{}' as it is not a SERVICE", componentName);
65             return Collections.emptyMap();
66         }
67
68         final boolean isEOTemplate = component.getCategories().stream()
69             .anyMatch(category -> ETSI_NS_COMPONENT_CATEGORY.equals(category.getName()));
70         if (!isEOTemplate) {
71             LOGGER.debug("Ignoring NSD CSAR generation for component '{}' as it does not belong to the category '{}'",
72                 componentName, ETSI_NS_COMPONENT_CATEGORY);
73             return Collections.emptyMap();
74         }
75
76         final byte[] nsdCsar;
77         try {
78             final EtsiVersion etsiVersion = getComponentEtsiVersion(component);
79             final EtsiNfvNsdCsarGenerator etsiNfvNsdCsarGenerator =
80                 etsiNfvNsdCsarGeneratorFactory.create(etsiVersion);
81             nsdCsar = etsiNfvNsdCsarGenerator.generateNsdCsar(component);
82         } catch (final NsdException e) {
83             LOGGER.error("Could not create NSD CSAR entry for component '{}'"
84                 , component.getName(), e);
85             return Collections.emptyMap();
86         } catch (final Exception e) {
87             LOGGER.error("Could not create NSD CSAR entry for component '{}'. An unexpected exception occurred"
88                 , component.getName(), e);
89             return Collections.emptyMap();
90         }
91
92         return createEntry(component.getNormalizedName(), nsdCsar);
93     }
94
95     private EtsiVersion getComponentEtsiVersion(Component component) {
96         final String etsiVersion = component.getCategorySpecificMetadata().get(ETSI_VERSION_METADATA);
97         return EtsiVersion.convertOrNull(etsiVersion);
98     }
99
100     private Map<String, byte[]> createEntry(final String csarName, final byte[] nsdCsar) {
101         final Map<String, byte[]> entryMap = new HashMap<>();
102         final String entryKey = String.format(NSD_FILE_PATH_FORMAT, ETSI_PACKAGE, csarName);
103         entryMap.put(entryKey, nsdCsar);
104         return entryMap;
105     }
106 }