1 package org.openecomp.vid.asdc.parser;
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;
9 import java.util.HashMap;
12 import java.util.UUID;
13 import java.util.Map.Entry;
14 import java.util.zip.ZipFile;
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;
34 public class ToscaParserImpl implements ToscaParser {
35 /** The Constant LOG. */
36 static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(ToscaParserImpl.class);
39 private final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
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;
49 public ToscaCsar parse(Path path) throws AsdcCatalogException {
50 return getToscaCsar(path);
53 private ToscaCsar getToscaCsar(final Path csarFile) throws AsdcCatalogException {
54 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
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));
62 final Yaml yaml = new Yaml();
63 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
65 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
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));
77 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
78 csarBuilder.addVnf(childModel);
84 return csarBuilder.build();
85 } catch (YAMLException e) {
86 throw new AsdcCatalogException("Caught exception while processing TOSCA YAML", e);
88 } catch (IOException e) {
89 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
93 public ServiceModel makeServiceModel(String uuid, final Path serviceCsar,Service service ) throws AsdcCatalogException, SdcToscaParserException {
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));
107 populateVnfsAndNetwork(methodName, isNewFlow, vnfs, networks, asdcServiceToscaModel, serviceModel);
109 // If we see customization uuid under vnf or network, follow 1702 flow
111 return (getCustomizedServices(asdcServiceToscaModel, serviceModel));
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);
120 LOG.warn("Couldn't find VNF object " + vnfUuid + ". Problem with Tosca model?");
123 extractAndUpdateInputs(vnf, vnfModel);
124 ServiceModel.extractGroups(vnfModel, serviceModel);
127 serviceModel.setVnfs(vnfs);
128 serviceModel.setNetworks(networks);
133 private VNF findVNFAccordingToUUID(final Map<String, VNF> vnfs, final String vnfUuid) {
135 for (Entry<String, VNF> vnfComp : vnfs.entrySet()) {
136 if (((vnfComp.getValue().getUuid()).equalsIgnoreCase(vnfUuid))) {
138 vnf = vnfComp.getValue();
144 private void extractAndUpdateInputs(VNF vnf, ToscaModel vnfModel) {
145 vnf.setInputs(vnfModel.gettopology_template().getInputs());
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();
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);
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);
181 serviceModel.setVnfs(vnfs);
182 serviceModel.setNetworks(networks);
186 private static Set<Entry<String, NodeTemplate>> extractNodeTemplates(final ToscaModel asdcServiceToscaModel) {
187 return asdcServiceToscaModel.gettopology_template().getnode_templates().entrySet();
190 private static boolean isNewFlow(Node node) {
191 return (node.getCustomizationUuid() != null) && (node.getCustomizationUuid().length() > 0);
194 private static boolean isNodeVersionMissing(Node Node) {
195 return Node.getVersion() == null;
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());
205 private ServiceModel getCustomizedServices(ToscaModel asdcServiceToscaModel, ServiceModel serviceModel) {
206 String methodName = "asdcServiceToscaModel";
207 LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " start");
209 // asdcServiceToscaModel should have vf modules and vol groups populated
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
215 serviceModel.associateGroups();
216 return (serviceModel);
220 private UUID extractUUIDFromNodeTemplate(final NodeTemplate nodeTemplate) {
221 return UUID.fromString(nodeTemplate.getMetadata().getUUID());