Merge 1806 code of vid-common
[vid.git] / vid-app-common / src / main / java / org / onap / vid / asdc / parser / ToscaParserImpl.java
index 37f4b0a..27445c0 100644 (file)
@@ -1,49 +1,34 @@
 package org.onap.vid.asdc.parser;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.file.Path;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-import java.util.Map.Entry;
-import java.util.zip.ZipFile;
-
 import org.apache.commons.lang3.mutable.MutableBoolean;
-import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
-import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
 import org.onap.vid.asdc.AsdcCatalogException;
 import org.onap.vid.asdc.beans.Service;
 import org.onap.vid.asdc.beans.tosca.NodeTemplate;
 import org.onap.vid.asdc.beans.tosca.ToscaCsar;
 import org.onap.vid.asdc.beans.tosca.ToscaMeta;
 import org.onap.vid.asdc.beans.tosca.ToscaModel;
-import org.onap.vid.model.ModelConstants;
-import org.onap.vid.model.Network;
-import org.onap.vid.model.Node;
-import org.onap.vid.model.ServiceModel;
-import org.onap.vid.model.VNF;
+import org.onap.vid.model.*;
 import org.onap.vid.properties.VidProperties;
-import org.springframework.beans.factory.annotation.Autowired;
+import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 import org.yaml.snakeyaml.Yaml;
 import org.yaml.snakeyaml.error.YAMLException;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.zip.ZipFile;
+
 public class ToscaParserImpl implements ToscaParser {
        /** The Constant LOG. */
        static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(ToscaParserImpl.class);
 
-       @Autowired
-       private final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
-
-
-       private static final String asdcModelNamespace = VidProperties.getAsdcModelNamespace();
-       private static final String vnfTag = asdcModelNamespace + ModelConstants.VNF;
-       private static final String networkTag = asdcModelNamespace + ModelConstants.NETWORK;
-       private static final String vfModuleTag = asdcModelNamespace + ModelConstants.VF_MODULE;
+       private static final String ASDC_MODEL_NAMESPACE = VidProperties.getAsdcModelNamespace();
+       private static final String VNF_TAG = ASDC_MODEL_NAMESPACE + ModelConstants.VNF;
+       private static final String NETWORK_TAG = ASDC_MODEL_NAMESPACE + ModelConstants.NETWORK;
 
 
        @Override
@@ -60,29 +45,7 @@ public class ToscaParserImpl implements ToscaParser {
                        final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
 
                        try {
-                               final Yaml yaml = new Yaml();
-                               final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
-
-                               final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
-
-                               for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
-                                       LOG.debug("imports = " + imports.toString());
-                                       for (Entry<String, Map<String, String>> entry : imports.entrySet()) {
-                                               if (entry.getValue() != null) {
-                                                       String fname = entry.getValue().get("file");
-                                                       if ((fname != null) && (fname.startsWith("service") || fname.startsWith("resource"))) {
-                                                               LOG.debug("fname = " + fname);
-                                                               final InputStream toscaChildEntryYamlStream = csar
-                                                                               .getInputStream(csar.getEntry("Definitions/" + fname));
-
-                                                               final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
-                                                               csarBuilder.addVnf(childModel);
-                                                       }
-                                               }
-                                       }
-                               }
-
-                               return csarBuilder.build();
+                               return createToscaCsar(csar, toscaParentEntryYamlStream);
                        } catch (YAMLException e) {
                                throw new AsdcCatalogException("Caught exception while processing TOSCA YAML", e);
                        }
@@ -91,22 +54,49 @@ public class ToscaParserImpl implements ToscaParser {
                }
        }
 
-       public ServiceModel makeServiceModel(String uuid, final Path serviceCsar,Service service ) throws AsdcCatalogException, SdcToscaParserException {
+       private ToscaCsar createToscaCsar(ZipFile csar, InputStream toscaParentEntryYamlStream) throws IOException {
+               final Yaml yaml = new Yaml();
+               final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
+
+               final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
+
+               for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
+                       LOG.debug("imports = " + imports.toString());
+                       for (Entry<String, Map<String, String>> entry : imports.entrySet()) {
+                               if (entry.getValue() != null) {
+                                       String fname = entry.getValue().get("file");
+                                       if ((fname != null) && (fname.startsWith("service") || fname.startsWith("resource"))) {
+                                               LOG.debug("fname = " + fname);
+                                               final InputStream toscaChildEntryYamlStream = csar
+                                                               .getInputStream(csar.getEntry("Definitions/" + fname));
+
+                                               final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
+                                               csarBuilder.addVnf(childModel);
+                                       }
+                               }
+                       }
+               }
+
+               return csarBuilder.build();
+       }
+
+       public ServiceModel makeServiceModel(String uuid, final Path serviceCsar,Service service ) throws AsdcCatalogException {
 
 
                final ServiceModel serviceModel = new ServiceModel();
                ToscaCsar toscaCsar = getToscaCsar(serviceCsar);
                String methodName = "getServices";
-               LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " start");
                MutableBoolean isNewFlow = new MutableBoolean(false);
-               final Map<String, VNF> vnfs = new HashMap<String, VNF>();
-               final Map<String, Network> networks = new HashMap<String, Network>();
+               final Map<String, VNF> vnfs = new HashMap<>();
+               final Map<String, Network> networks = new HashMap<>();
                final ToscaModel asdcServiceToscaModel = toscaCsar.getParent();
                serviceModel.setService(ServiceModel.extractService(asdcServiceToscaModel, service));
 
 
+
                populateVnfsAndNetwork(methodName, isNewFlow, vnfs, networks, asdcServiceToscaModel, serviceModel);
 
+
                // If we see customization uuid under vnf or network, follow 1702 flow
                if (isNewFlow.getValue()) {
                        return (getCustomizedServices(asdcServiceToscaModel, serviceModel));
@@ -147,33 +137,30 @@ public class ToscaParserImpl implements ToscaParser {
        }
 
        private static void populateVnfsAndNetwork(String methodName, MutableBoolean isNewFlow, final Map<String, VNF> vnfs,
-                                                                                          final Map<String, Network> networks, final ToscaModel asdcServiceToscaModel, ServiceModel serviceModel)
-                       throws AsdcCatalogException, SdcToscaParserException {
+                                                                                          final Map<String, Network> networks, final ToscaModel asdcServiceToscaModel, ServiceModel serviceModel) {
                for (Entry<String, NodeTemplate> component : extractNodeTemplates(asdcServiceToscaModel)) {
                        final String modelCustomizationName = component.getKey();
-                       LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName
+                       LOG.debug(EELFLoggerDelegate.debugLogger, methodName
                                        + " model customization name: " + modelCustomizationName);
                        final NodeTemplate nodeTemplate = component.getValue();
                        final String type = nodeTemplate.getType();
 
-                       if (type.startsWith(vnfTag)) {
+                       if (type.startsWith(VNF_TAG)) {
                                LOG.debug(EELFLoggerDelegate.debugLogger,
-                                               dateFormat.format(new Date()) + methodName + " found node template type: " + type);
+                                                       methodName + " found node template type: " + type);
                                final VNF vnf = new VNF();
                                vnf.extractVnf(modelCustomizationName, nodeTemplate);
-//                             populateNodeVersionIfMissing(nodeTemplate, vnf,service);
                                LOG.debug(EELFLoggerDelegate.debugLogger,
-                                               dateFormat.format(new Date()) + methodName + " VNF commands: " + vnf.getCommands());
+                                                       methodName + " VNF commands: " + vnf.getCommands());
                                vnfs.put(modelCustomizationName, vnf);
                                isNewFlow.setValue(isNewFlow(vnf));
                        }
                        // Networks
-                       if (type.startsWith(networkTag)) {
+                       if (type.startsWith(NETWORK_TAG)) {
                                LOG.debug(EELFLoggerDelegate.debugLogger,
-                                               dateFormat.format(new Date()) + methodName + " found node template type: " + type);
+                                                methodName + " found node template type: " + type);
                                final Network network = new Network();
                                network.extractNetwork(modelCustomizationName, nodeTemplate);
-//                             populateNodeVersionIfMissing(nodeTemplate, network, service);
                                isNewFlow.setValue(isNewFlow(network));
                                networks.put(modelCustomizationName, network);
 
@@ -192,20 +179,8 @@ public class ToscaParserImpl implements ToscaParser {
                return (node.getCustomizationUuid() != null) && (node.getCustomizationUuid().length() > 0);
        }
 
-       private static boolean isNodeVersionMissing(Node Node) {
-               return Node.getVersion() == null;
-       }
-
-       private static void populateNodeVersionIfMissing(final NodeTemplate nodeTemplate, final Node node, Service service)
-                       throws AsdcCatalogException {
-               if (isNodeVersionMissing(node)) {
-                       node.setVersion(service.getVersion());
-               }
-       }
-
        private ServiceModel getCustomizedServices(ToscaModel asdcServiceToscaModel, ServiceModel serviceModel) {
                String methodName = "asdcServiceToscaModel";
-               LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " start");
 
                // asdcServiceToscaModel should have vf modules and vol groups populated
                // at this point but
@@ -217,10 +192,4 @@ public class ToscaParserImpl implements ToscaParser {
                return (serviceModel);
        }
 
-
-       private UUID extractUUIDFromNodeTemplate(final NodeTemplate nodeTemplate) {
-               return UUID.fromString(nodeTemplate.getMetadata().getUUID());
-       }
-
-
 }
\ No newline at end of file