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