Merge 1806 code of vid-common
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / parser / ToscaParserImpl.java
1 package org.onap.vid.asdc.parser;
2
3 import org.apache.commons.lang3.mutable.MutableBoolean;
4 import org.onap.vid.asdc.AsdcCatalogException;
5 import org.onap.vid.asdc.beans.Service;
6 import org.onap.vid.asdc.beans.tosca.NodeTemplate;
7 import org.onap.vid.asdc.beans.tosca.ToscaCsar;
8 import org.onap.vid.asdc.beans.tosca.ToscaMeta;
9 import org.onap.vid.asdc.beans.tosca.ToscaModel;
10 import org.onap.vid.model.*;
11 import org.onap.vid.properties.VidProperties;
12 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
13 import org.yaml.snakeyaml.Yaml;
14 import org.yaml.snakeyaml.error.YAMLException;
15
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.file.Path;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
23 import java.util.zip.ZipFile;
24
25 public class ToscaParserImpl implements ToscaParser {
26         /** The Constant LOG. */
27         static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(ToscaParserImpl.class);
28
29         private static final String ASDC_MODEL_NAMESPACE = VidProperties.getAsdcModelNamespace();
30         private static final String VNF_TAG = ASDC_MODEL_NAMESPACE + ModelConstants.VNF;
31         private static final String NETWORK_TAG = ASDC_MODEL_NAMESPACE + ModelConstants.NETWORK;
32
33
34         @Override
35         public ToscaCsar parse(Path path) throws AsdcCatalogException {
36                 return getToscaCsar(path);
37         }
38
39         private ToscaCsar getToscaCsar(final Path csarFile) throws AsdcCatalogException {
40                 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
41
42                         final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
43                         final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
44                         final String entryDefinitions = toscaMeta.get("Entry-Definitions");
45                         final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
46
47                         try {
48                                 return createToscaCsar(csar, toscaParentEntryYamlStream);
49                         } catch (YAMLException e) {
50                                 throw new AsdcCatalogException("Caught exception while processing TOSCA YAML", e);
51                         }
52                 } catch (IOException e) {
53                         throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
54                 }
55         }
56
57         private ToscaCsar createToscaCsar(ZipFile csar, InputStream toscaParentEntryYamlStream) throws IOException {
58                 final Yaml yaml = new Yaml();
59                 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
60
61                 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
62
63                 for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
64                         LOG.debug("imports = " + imports.toString());
65                         for (Entry<String, Map<String, String>> entry : imports.entrySet()) {
66                                 if (entry.getValue() != null) {
67                                         String fname = entry.getValue().get("file");
68                                         if ((fname != null) && (fname.startsWith("service") || fname.startsWith("resource"))) {
69                                                 LOG.debug("fname = " + fname);
70                                                 final InputStream toscaChildEntryYamlStream = csar
71                                                                 .getInputStream(csar.getEntry("Definitions/" + fname));
72
73                                                 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
74                                                 csarBuilder.addVnf(childModel);
75                                         }
76                                 }
77                         }
78                 }
79
80                 return csarBuilder.build();
81         }
82
83         public ServiceModel makeServiceModel(String uuid, final Path serviceCsar,Service service ) throws AsdcCatalogException {
84
85
86                 final ServiceModel serviceModel = new ServiceModel();
87                 ToscaCsar toscaCsar = getToscaCsar(serviceCsar);
88                 String methodName = "getServices";
89                 MutableBoolean isNewFlow = new MutableBoolean(false);
90                 final Map<String, VNF> vnfs = new HashMap<>();
91                 final Map<String, Network> networks = new HashMap<>();
92                 final ToscaModel asdcServiceToscaModel = toscaCsar.getParent();
93                 serviceModel.setService(ServiceModel.extractService(asdcServiceToscaModel, service));
94
95
96
97                 populateVnfsAndNetwork(methodName, isNewFlow, vnfs, networks, asdcServiceToscaModel, serviceModel);
98
99
100                 // If we see customization uuid under vnf or network, follow 1702 flow
101                 if (isNewFlow.getValue()) {
102                         return (getCustomizedServices(asdcServiceToscaModel, serviceModel));
103                 } else {
104                         VNF vnf = null;
105                         for (ToscaModel vnfModel : toscaCsar.getChildren()) {
106                                 // using uuid to match should only be valid for 1610 models
107                                 final String vnfUuid = (vnfModel.getMetadata().getUUID());
108                                 // find the VNF with that uuid, uuid is not the key anymore
109                                 vnf = findVNFAccordingToUUID(vnfs, vnfUuid);
110                                 if (vnf == null) {
111                                         LOG.warn("Couldn't find VNF object " + vnfUuid + ". Problem with Tosca model?");
112                                         continue;
113                                 }
114                                 extractAndUpdateInputs(vnf, vnfModel);
115                                 ServiceModel.extractGroups(vnfModel, serviceModel);
116                         }
117
118                         serviceModel.setVnfs(vnfs);
119                         serviceModel.setNetworks(networks);
120                         return serviceModel;
121                 }
122         }
123
124         private VNF findVNFAccordingToUUID(final Map<String, VNF> vnfs,  final String vnfUuid) {
125                 VNF vnf = null;
126                 for (Entry<String, VNF> vnfComp : vnfs.entrySet()) {
127                         if (((vnfComp.getValue().getUuid()).equalsIgnoreCase(vnfUuid))) {
128                                 // found the vnf
129                                 vnf = vnfComp.getValue();
130                         }
131                 }
132                 return vnf;
133         }
134
135         private void extractAndUpdateInputs(VNF vnf, ToscaModel vnfModel) {
136                 vnf.setInputs(vnfModel.gettopology_template().getInputs());
137         }
138
139         private static void populateVnfsAndNetwork(String methodName, MutableBoolean isNewFlow, final Map<String, VNF> vnfs,
140                                                                                            final Map<String, Network> networks, final ToscaModel asdcServiceToscaModel, ServiceModel serviceModel) {
141                 for (Entry<String, NodeTemplate> component : extractNodeTemplates(asdcServiceToscaModel)) {
142                         final String modelCustomizationName = component.getKey();
143                         LOG.debug(EELFLoggerDelegate.debugLogger, methodName
144                                         + " model customization name: " + modelCustomizationName);
145                         final NodeTemplate nodeTemplate = component.getValue();
146                         final String type = nodeTemplate.getType();
147
148                         if (type.startsWith(VNF_TAG)) {
149                                 LOG.debug(EELFLoggerDelegate.debugLogger,
150                                                         methodName + " found node template type: " + type);
151                                 final VNF vnf = new VNF();
152                                 vnf.extractVnf(modelCustomizationName, nodeTemplate);
153                                 LOG.debug(EELFLoggerDelegate.debugLogger,
154                                                         methodName + " VNF commands: " + vnf.getCommands());
155                                 vnfs.put(modelCustomizationName, vnf);
156                                 isNewFlow.setValue(isNewFlow(vnf));
157                         }
158                         // Networks
159                         if (type.startsWith(NETWORK_TAG)) {
160                                 LOG.debug(EELFLoggerDelegate.debugLogger,
161                                                  methodName + " found node template type: " + type);
162                                 final Network network = new Network();
163                                 network.extractNetwork(modelCustomizationName, nodeTemplate);
164                                 isNewFlow.setValue(isNewFlow(network));
165                                 networks.put(modelCustomizationName, network);
166
167                         }
168                 }
169                 serviceModel.setVnfs(vnfs);
170                 serviceModel.setNetworks(networks);
171
172         }
173
174         private static Set<Entry<String, NodeTemplate>> extractNodeTemplates(final ToscaModel asdcServiceToscaModel) {
175                 return asdcServiceToscaModel.gettopology_template().getnode_templates().entrySet();
176         }
177
178         private static boolean isNewFlow(Node node) {
179                 return (node.getCustomizationUuid() != null) && (node.getCustomizationUuid().length() > 0);
180         }
181
182         private ServiceModel getCustomizedServices(ToscaModel asdcServiceToscaModel, ServiceModel serviceModel) {
183                 String methodName = "asdcServiceToscaModel";
184
185                 // asdcServiceToscaModel should have vf modules and vol groups populated
186                 // at this point but
187                 // they are not associated with the VNFs
188                 ServiceModel.extractGroups(asdcServiceToscaModel,serviceModel);
189                 // Now put the vf modules and volume groups under the VNF they belong
190                 // too
191                 serviceModel.associateGroups();
192                 return (serviceModel);
193         }
194
195 }