1b74b0094fa89109e5a3091ccecf8ccc69c2ba0c
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019, Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi;
22
23 import org.apache.commons.io.IOUtils;
24 import org.onap.sdc.tosca.parser.utils.YamlToObjectConverter;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
27 import org.openecomp.sdc.tosca.csar.Manifest;
28 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
29 import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding;
30 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
31
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.util.List;
36 import java.util.Map;
37
38 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
39 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA;
40 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
41 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_CHANGE_LOG;
42 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_MANIFEST;
43 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME;
44 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
45
46 public class ETSIServiceImpl implements ETSIService {
47
48     private Configuration configuration;
49
50     public ETSIServiceImpl() throws IOException {
51         InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml");
52         if (io == null) {
53             throw new IOException("Non Mano configuration not found");
54         }
55         String data = IOUtils.toString(io, StandardCharsets.UTF_8);
56         YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
57         configuration = yamlToObjectConverter.convertFromString(data, Configuration.class);
58     }
59
60     public ETSIServiceImpl(Configuration configuration) {
61         this.configuration = configuration;
62     }
63
64     @Override
65     public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) throws IOException {
66         Map<String, byte[]> templates = handler.getFiles();
67         return isMetaFilePresent(templates) && hasMetaMandatoryEntries(getMetadata(handler));
68     }
69
70     @Override
71     public void moveNonManoFileToArtifactFolder(FileContentHandler handler, Manifest manifest) {
72         for (Map.Entry<String, List<String>> entry : manifest.getNonManoSources().entrySet()) {
73             String e = entry.getKey();
74             List<String> k = entry.getValue();
75             updateNonManoLocation(handler, e, k);
76         }
77     }
78
79
80     private void updateNonManoLocation(FileContentHandler handler, String nonManoKey, List<String> sources) {
81         Map<String, byte[]> files = handler.getFiles();
82         for (String key : sources) {
83             if (files.containsKey(key)) {
84                 updateLocation(key, nonManoKey, files);
85             }
86         }
87     }
88
89     private void updateLocation(String key, String nonManoKey, Map<String, byte[]> files) {
90         if (nonManoKey == null || nonManoKey.isEmpty()) {
91             return;
92         }
93         Map<String, NonManoType> map = configuration.getNonManoKeyFolderMapping();
94         if (map.containsKey(nonManoKey)) {
95             NonManoType nonManoPair = map.get(nonManoKey);
96             String newLocation = nonManoPair.getType() + "/" +
97                     nonManoPair.getLocation() + "/" + getFileName(key);
98             if (!files.containsKey(newLocation)) {
99                 files.put(newLocation, files.remove(key));
100             }
101         }
102     }
103
104     private String getFileName(String key) {
105         return key.substring(key.lastIndexOf('/') + 1);
106     }
107
108     private boolean hasMetaMandatoryEntries(ToscaMetadata toscaMetadata) {
109         Map<String, String> metaDataEntries = toscaMetadata.getMetaEntries();
110         return metaDataEntries.containsKey(TOSCA_META_ENTRY_DEFINITIONS) && metaDataEntries.containsKey(TOSCA_META_ETSI_ENTRY_MANIFEST)
111                 && metaDataEntries.containsKey(TOSCA_META_ETSI_ENTRY_CHANGE_LOG);
112     }
113
114     private boolean isMetaFilePresent(Map<String, byte[]> handler) {
115         return handler.containsKey(TOSCA_META_PATH_FILE_NAME) || handler.containsKey(TOSCA_META_ORIG_PATH_FILE_NAME);
116     }
117
118     public ResourceTypeEnum getResourceType(FileContentHandler handler) throws IOException {
119         ToscaMetadata metadata = getMetadata(handler);
120         Manifest manifest = getManifest(handler, metadata.getMetaEntries().get(TOSCA_META_ETSI_ENTRY_MANIFEST));
121         return getResourceType(manifest);
122     }
123
124     public ResourceTypeEnum getResourceType(Manifest manifest) {
125         // Valid manifest should contain whether vnf or pnf related metadata data exclusively in SOL004 standard,
126         // validation of manifest done during package upload stage
127         if (manifest != null && !manifest.getMetadata().isEmpty()
128                 && MANIFEST_PNF_METADATA.stream().anyMatch(e -> manifest.getMetadata().containsKey(e))) {
129             return ResourceTypeEnum.PNF;
130         }
131         // VNF is default resource type
132         return ResourceTypeEnum.VF;
133     }
134
135     public Manifest getManifest(FileContentHandler handler) throws IOException {
136         ToscaMetadata metadata = getMetadata(handler);
137         return getManifest(handler, metadata.getMetaEntries().get(TOSCA_META_ETSI_ENTRY_MANIFEST));
138     }
139
140     private Manifest getManifest(FileContentHandler handler, String manifestLocation) throws IOException {
141         try(InputStream manifestInputStream = getManifestInputStream(handler, manifestLocation)) {
142             Manifest onboardingManifest = new SOL004ManifestOnboarding();
143             onboardingManifest.parse(manifestInputStream);
144             return onboardingManifest;
145         }
146     }
147
148     private ToscaMetadata getMetadata(FileContentHandler handler) throws IOException {
149         ToscaMetadata metadata;
150         if (handler.containsFile(TOSCA_META_PATH_FILE_NAME)) {
151             metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContent(TOSCA_META_PATH_FILE_NAME));
152         } else if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) {
153             metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContent(TOSCA_META_ORIG_PATH_FILE_NAME));
154         } else {
155             throw new IOException("TOSCA.meta file not found!");
156         }
157         return metadata;
158     }
159
160     private InputStream getManifestInputStream(FileContentHandler handler, String manifestLocation) throws IOException {
161         InputStream io;
162         if (manifestLocation == null || !handler.containsFile(manifestLocation)) {
163             io = handler.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME);
164         } else {
165             io = handler.getFileContent(manifestLocation);
166         }
167
168         if (io == null) {
169             throw new IOException("Manifest file not found!");
170         }
171         return io;
172     }
173 }