3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2020 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
22 import static org.openecomp.sdc.common.api.ArtifactTypeEnum.ETSI_PACKAGE;
24 import java.util.Collections;
25 import java.util.HashMap;
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;
37 * Generates a Network Service CSAR based on a SERVICE component and wraps it in a SDC CSAR entry.
39 @org.springframework.stereotype.Component("etsiNfvNsCsarEntryGenerator")
40 public class EtsiNfvNsCsarEntryGenerator implements CsarEntryGenerator {
42 static final String ETSI_NS_COMPONENT_CATEGORY = "ETSI NFV Network Service";
43 static final String NSD_FILE_PATH_FORMAT = "Artifacts/%s/%s.csar";
44 static final String ETSI_VERSION_METADATA = "ETSI Version";
45 private static final Logger LOGGER = LoggerFactory.getLogger(EtsiNfvNsCsarEntryGenerator.class);
46 private final EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory;
48 public EtsiNfvNsCsarEntryGenerator(final EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory) {
49 this.etsiNfvNsdCsarGeneratorFactory = etsiNfvNsdCsarGeneratorFactory;
53 * Generates a Network Service CSAR based on a SERVICE component of category {@link EtsiNfvNsCsarEntryGenerator#ETSI_NS_COMPONENT_CATEGORY} and
54 * wraps it in a SDC CSAR entry.
56 * @param component the component to create the NS CSAR from
57 * @return an entry to be added in the Component CSAR by SDC
60 public Map<String, byte[]> generateCsarEntries(final Component component) {
61 final String componentName = component == null ? "null" : component.getName();
62 if (component == null || ComponentTypeEnum.SERVICE != component.getComponentType()) {
63 LOGGER.debug("Ignoring NSD CSAR generation for component '{}' as it is not a SERVICE", componentName);
64 return Collections.emptyMap();
66 final boolean isEOTemplate = component.getCategories().stream().anyMatch(category -> ETSI_NS_COMPONENT_CATEGORY.equals(category.getName()));
68 LOGGER.debug("Ignoring NSD CSAR generation for component '{}' as it does not belong to the category '{}'", componentName,
69 ETSI_NS_COMPONENT_CATEGORY);
70 return Collections.emptyMap();
74 final EtsiVersion etsiVersion = getComponentEtsiVersion(component);
75 final EtsiNfvNsdCsarGenerator etsiNfvNsdCsarGenerator = etsiNfvNsdCsarGeneratorFactory.create(etsiVersion);
76 nsdCsar = etsiNfvNsdCsarGenerator.generateNsdCsar(component);
77 } catch (final NsdException e) {
78 LOGGER.error("Could not create NSD CSAR entry for component '{}'", component.getName(), e);
79 return Collections.emptyMap();
80 } catch (final Exception e) {
81 LOGGER.error("Could not create NSD CSAR entry for component '{}'. An unexpected exception occurred", component.getName(), e);
82 return Collections.emptyMap();
84 return createEntry(component.getNormalizedName(), nsdCsar);
87 private EtsiVersion getComponentEtsiVersion(Component component) {
88 final String etsiVersion = component.getCategorySpecificMetadata().get(ETSI_VERSION_METADATA);
89 return EtsiVersion.convertOrNull(etsiVersion);
92 private Map<String, byte[]> createEntry(final String csarName, final byte[] nsdCsar) {
93 final Map<String, byte[]> entryMap = new HashMap<>();
94 final String entryKey = String.format(NSD_FILE_PATH_FORMAT, ETSI_PACKAGE, csarName);
95 entryMap.put(entryKey, nsdCsar);