1 package org.onap.vid.asdc.parser;
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;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.nio.file.Path;
19 import java.util.HashMap;
21 import java.util.Map.Entry;
23 import java.util.zip.ZipFile;
25 public class ToscaParserImpl implements ToscaParser {
26 /** The Constant LOG. */
27 static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(ToscaParserImpl.class);
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;
35 public ToscaCsar parse(Path path) throws AsdcCatalogException {
36 return getToscaCsar(path);
39 private ToscaCsar getToscaCsar(final Path csarFile) throws AsdcCatalogException {
40 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
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));
48 return createToscaCsar(csar, toscaParentEntryYamlStream);
49 } catch (YAMLException e) {
50 throw new AsdcCatalogException("Caught exception while processing TOSCA YAML", e);
52 } catch (IOException e) {
53 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
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);
61 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
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));
73 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
74 csarBuilder.addVnf(childModel);
80 return csarBuilder.build();
83 public ServiceModel makeServiceModel(String uuid, final Path serviceCsar,Service service ) throws AsdcCatalogException {
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));
97 populateVnfsAndNetwork(methodName, isNewFlow, vnfs, networks, asdcServiceToscaModel, serviceModel);
100 // If we see customization uuid under vnf or network, follow 1702 flow
101 if (isNewFlow.getValue()) {
102 return (getCustomizedServices(asdcServiceToscaModel, serviceModel));
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);
111 LOG.warn("Couldn't find VNF object " + vnfUuid + ". Problem with Tosca model?");
114 extractAndUpdateInputs(vnf, vnfModel);
115 ServiceModel.extractGroups(vnfModel, serviceModel);
118 serviceModel.setVnfs(vnfs);
119 serviceModel.setNetworks(networks);
124 private VNF findVNFAccordingToUUID(final Map<String, VNF> vnfs, final String vnfUuid) {
126 for (Entry<String, VNF> vnfComp : vnfs.entrySet()) {
127 if (((vnfComp.getValue().getUuid()).equalsIgnoreCase(vnfUuid))) {
129 vnf = vnfComp.getValue();
135 private void extractAndUpdateInputs(VNF vnf, ToscaModel vnfModel) {
136 vnf.setInputs(vnfModel.gettopology_template().getInputs());
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();
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));
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);
169 serviceModel.setVnfs(vnfs);
170 serviceModel.setNetworks(networks);
174 private static Set<Entry<String, NodeTemplate>> extractNodeTemplates(final ToscaModel asdcServiceToscaModel) {
175 return asdcServiceToscaModel.gettopology_template().getnode_templates().entrySet();
178 private static boolean isNewFlow(Node node) {
179 return (node.getCustomizationUuid() != null) && (node.getCustomizationUuid().length() > 0);
182 private ServiceModel getCustomizedServices(ToscaModel asdcServiceToscaModel, ServiceModel serviceModel) {
183 String methodName = "asdcServiceToscaModel";
185 // asdcServiceToscaModel should have vf modules and vol groups populated
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
191 serviceModel.associateGroups();
192 return (serviceModel);