Add CLI in verigraph. 75/56875/1
authorserena.spinoso <serena.spinoso@polito.it>
Sun, 29 Apr 2018 08:48:44 +0000 (10:48 +0200)
committerserena.spinoso <serena.spinoso@polito.it>
Sun, 29 Apr 2018 08:48:44 +0000 (10:48 +0200)
JIRA: PARSER-180

Add CLI to select one of the available interfaces (i.e. REST or gRPC), one of the supported service descriptor formats (i.e. TOSCA/XML, TOSCA/YAML, JSON),
and the operations to perform (i.e. read, create, update, delete graphs and run verifications).

Change-Id: I02f4d86e221baf0ac583d0ffd0ebe028aa6209be
Signed-off-by: serena.spinoso <serena.spinoso@polito.it>
54 files changed:
verigraph/src/it/polito/verigraph/exception/InvalidServiceTemplateException.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/exception/InvalidServiceTemplateExceptionMapper.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/MappingUtils.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/README_CLI.txt [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/ToscaCLI.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/XmlParsingUtils.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/YamlParsingUtils.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToXml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/grpc/ToscaGrpcUtils.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/grpc/XmlToGrpc.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/grpc/YamlToGrpc.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/xml/GraphToXml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/xml/XmlToGraph.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/yaml/GraphToYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/converter/yaml/YamlToGraph.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/deserializer/YamlConfigurationDeserializer.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/serializer/XmlConfigSerializer.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/serializer/YamlConfigSerializer.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/AntispamConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/AntispamNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/CacheConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/CacheNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/ConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/DpiConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/DpiNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndhostConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndhostNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndpointConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndpointNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/FieldModifierConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/FieldModifierNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/FirewallConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/FirewallNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailClientConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailClientNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailServerConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailServerNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/NatConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/NatNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/NodeTemplateYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/RelationshipTemplateYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/ServiceTemplateYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/TopologyTemplateYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/VerificationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnAccessConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnAccessNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnExitConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnExitNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebClientConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebClientNode.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebServerConfigurationYaml.java [new file with mode: 0644]
verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebServerNode.java [new file with mode: 0644]

diff --git a/verigraph/src/it/polito/verigraph/exception/InvalidServiceTemplateException.java b/verigraph/src/it/polito/verigraph/exception/InvalidServiceTemplateException.java
new file mode 100644 (file)
index 0000000..b5fb868
--- /dev/null
@@ -0,0 +1,18 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.exception;\r
+\r
+public class InvalidServiceTemplateException extends RuntimeException {\r
+\r
+  private static final long serialVersionUID = -3138131670694139585L;\r
+\r
+  public InvalidServiceTemplateException(String message) {\r
+        super(message);\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/exception/InvalidServiceTemplateExceptionMapper.java b/verigraph/src/it/polito/verigraph/exception/InvalidServiceTemplateExceptionMapper.java
new file mode 100644 (file)
index 0000000..e796119
--- /dev/null
@@ -0,0 +1,29 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.exception;\r
+\r
+import javax.ws.rs.core.Response;\r
+import javax.ws.rs.core.Response.Status;\r
+import javax.ws.rs.ext.ExceptionMapper;\r
+import javax.ws.rs.ext.Provider;\r
+\r
+import it.polito.verigraph.model.ErrorMessage;\r
+\r
+@Provider\r
+public class InvalidServiceTemplateExceptionMapper implements ExceptionMapper<InvalidServiceTemplateException> {\r
+\r
+    @Override\r
+    public Response toResponse(InvalidServiceTemplateException exception) {\r
+        ErrorMessage errorMessage = new ErrorMessage( exception.getMessage(),\r
+                400,\r
+                "http://localhost:8080/verigraph/api-docs/");\r
+        return Response.status(Status.BAD_REQUEST).entity(errorMessage).build();\r
+    }\r
+\r
+}
\ No newline at end of file
diff --git a/verigraph/src/it/polito/verigraph/tosca/MappingUtils.java b/verigraph/src/it/polito/verigraph/tosca/MappingUtils.java
new file mode 100644 (file)
index 0000000..a415ad2
--- /dev/null
@@ -0,0 +1,215 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Politecnico di Torino and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Apache License, Version 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *******************************************************************************/
+
+package it.polito.verigraph.tosca;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.InjectableValues;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+
+import it.polito.tosca.jaxb.Definitions;
+import it.polito.tosca.jaxb.TDocumentation;
+import it.polito.tosca.jaxb.TServiceTemplate;
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
+import it.polito.verigraph.model.Graph;
+import it.polito.verigraph.model.Node;
+import it.polito.verigraph.model.Test;
+import it.polito.verigraph.model.Verification;
+import it.polito.verigraph.tosca.converter.xml.GraphToXml;
+import it.polito.verigraph.tosca.converter.yaml.GraphToYaml;
+import it.polito.verigraph.tosca.deserializer.XmlConfigurationDeserializer;
+import it.polito.verigraph.tosca.serializer.XmlConfigSerializer;
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.VerificationYaml;
+
+public class MappingUtils {
+
+    public static String prettyPrintJsonString(JsonNode jsonNode) {
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            Object json = mapper.readValue(jsonNode.toString(), Object.class);
+            return System.getProperty("line.separator") + mapper.writerWithDefaultPrettyPrinter()
+            .writeValueAsString(json) + System.getProperty("line.separator");
+        } catch (Exception e) {
+            return "Sorry, pretty print didn't work";
+        }
+    }
+
+    // From a list of nodes (path) returns a Definitions object that contains all the paths as different service templates
+    public static Definitions mapPathsToXml(List<List<Node>> paths) {
+        Definitions definitions = new Definitions();
+        List<Graph> tempGraphs = new ArrayList<Graph>();
+
+        int i = 0;
+        for (List<Node> path: paths) {
+            Graph tempGraph = new Graph();
+            tempGraph.setId(i++);
+            for (Node node : path)
+                tempGraph.getNodes().put(node.getId(), node);
+            tempGraphs.add(tempGraph);
+        }
+
+        for (Graph g: tempGraphs) {
+            definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(GraphToXml.mapPathToXml(g));
+        }
+
+        return definitions;
+    }
+
+
+    // From a list of nodes (path) returns a list of ServiceTemplateYaml object that represent the paths
+    public static List<ServiceTemplateYaml> mapPathsToYaml(List<List<Node>> paths) {
+        List<ServiceTemplateYaml> serviceTemplates = new ArrayList<ServiceTemplateYaml>();
+        List<Graph> tempGraphs = new ArrayList<Graph>();
+
+        int i = 0;
+        for (List<Node> path: paths) {
+            Graph tempGraph = new Graph();
+            tempGraph.setId(i++);
+            for (Node node : path)
+                tempGraph.getNodes().put(node.getId(), node);
+            tempGraphs.add(tempGraph);
+        }
+
+        for (Graph g: tempGraphs) {
+            serviceTemplates.add(GraphToYaml.mapGraphYaml(g));
+        }
+
+        return serviceTemplates;
+    }
+
+
+    public static Definitions mapVerificationToXml(Verification verification) {
+        Definitions toscaVerification = new Definitions();
+        TDocumentation toscaVerificationResult = new TDocumentation();
+        toscaVerificationResult.setSource(verification.getResult() + ": " + verification.getComment());
+        toscaVerification.getDocumentation().add(toscaVerificationResult);
+
+        List<TServiceTemplate> toscaPaths = new ArrayList<TServiceTemplate>();
+
+        int i = 0;
+        for (Test test: verification.getTests()) {
+            Graph tempGraph = new Graph();
+            tempGraph.setId(i++);
+            for (Node node : test.getPath())
+                tempGraph.getNodes().put(node.getId(), node);
+
+            TServiceTemplate toscaPath = GraphToXml.mapPathToXml(tempGraph);
+            TDocumentation toscaTestResult = new TDocumentation();
+            toscaTestResult.setSource(test.getResult());
+            toscaPath.getDocumentation().add(toscaTestResult);
+            toscaPaths.add(toscaPath);
+        }
+
+        toscaVerification.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().addAll(0, toscaPaths);
+        return toscaVerification;
+    }
+
+
+    public static VerificationYaml mapVerificationToYaml(Verification verification) {
+        VerificationYaml verificationYaml = new VerificationYaml();
+        verificationYaml.setResult(verification.getResult());
+        verificationYaml.setComment(verification.getComment());
+
+        List<ServiceTemplateYaml> toscaPaths = new ArrayList<ServiceTemplateYaml>();
+
+        int i = 0;
+        for (Test test: verification.getTests()) {
+            Graph tempGraph = new Graph();
+            tempGraph.setId(i++);
+            for (Node node : test.getPath())
+                tempGraph.getNodes().put(node.getId(), node);
+
+            ServiceTemplateYaml toscaPath = GraphToYaml.mapGraphYaml(tempGraph);
+            toscaPath.getMetadata().put("result", test.getResult());
+            toscaPaths.add(toscaPath);
+        }
+
+        verificationYaml.setPaths(toscaPaths);
+        return verificationYaml;
+    }
+
+
+    /** Return a string that represent the Tosca Configuration in json string.
+     *
+     * The string can be converted in JsonNode to be inserted in Model Configuration.
+     *
+     * Used for: xml-->model
+     * @throws JsonProcessingException*/
+    public static String obtainStringConfiguration(it.polito.tosca.jaxb.Configuration nodeConfig) throws JsonProcessingException {
+        ObjectMapper mapper = new ObjectMapper();
+        SimpleModule module = new SimpleModule();
+        module.addSerializer(it.polito.tosca.jaxb.Configuration.class, new XmlConfigSerializer());
+        mapper.registerModule(module);
+
+        String stringConfiguration = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(nodeConfig);
+        if (stringConfiguration.equals("") || stringConfiguration == null)
+            return "[]";
+        else
+            return stringConfiguration;
+    }
+
+
+    /** Return a Tosca Configuration with inside the representation of a model Configuration (only its JsonNode)
+     *
+     * Used for: model-->xml
+     * @throws JsonProcessingException */
+    public static it.polito.tosca.jaxb.Configuration obtainToscaConfiguration(it.polito.verigraph.model.Configuration modelConfig, String type) throws JsonProcessingException {
+
+        ObjectMapper mapper = new ObjectMapper();
+        SimpleModule module = new SimpleModule();
+
+        //Passing the configuration type to the Deserializer context
+        module.addDeserializer(it.polito.tosca.jaxb.Configuration.class, new XmlConfigurationDeserializer());
+        mapper.registerModule(module);
+
+        it.polito.tosca.jaxb.Configuration toscaConfig = new it.polito.tosca.jaxb.Configuration();
+        try {
+            toscaConfig = mapper.reader(new InjectableValues.Std().addValue("type", type))
+                    .forType(it.polito.tosca.jaxb.Configuration.class)
+                    .readValue(modelConfig.getConfiguration());
+        } catch (IOException e) {
+            //TODO shall we suppose that configuration stored on DB are always correct?
+        }
+
+        return toscaConfig;
+    }
+
+    /** Return a Tosca Configuration from a ConfigurationGrpc
+     *
+     * Used for: grpc-->xml
+     * @throws JsonProcessingException */
+    public static it.polito.tosca.jaxb.Configuration obtainToscaConfiguration(ToscaConfigurationGrpc grpcConfig, String type) throws JsonProcessingException {
+
+        ObjectMapper mapper = new ObjectMapper();
+        SimpleModule module = new SimpleModule();
+
+        //Passing the configuration type to the Deserializer context
+        module.addDeserializer(it.polito.tosca.jaxb.Configuration.class, new XmlConfigurationDeserializer());
+        mapper.registerModule(module);
+
+        it.polito.tosca.jaxb.Configuration toscaConfig = new it.polito.tosca.jaxb.Configuration();
+        try {
+            toscaConfig = mapper.reader(new InjectableValues.Std().addValue("type", type))
+                    .forType(it.polito.tosca.jaxb.Configuration.class)
+                    .readValue(grpcConfig.getConfiguration());
+        } catch (IOException e) {
+            //TODO shall we suppose that configuration stored on DB are always correct?
+        }
+
+        return toscaConfig;
+    }
+
+}
diff --git a/verigraph/src/it/polito/verigraph/tosca/README_CLI.txt b/verigraph/src/it/polito/verigraph/tosca/README_CLI.txt
new file mode 100644 (file)
index 0000000..6ee1332
--- /dev/null
@@ -0,0 +1,66 @@
+** Verigraph Verification Service - Command Line Interface **
+
+The CLI allows to interact with the Verigraph Verification Service using both the RESTful and gRPC interface.
+The CLI allows CRUD operations on graphs and three kind of verification.
+
+
+-- Available commands --
+> CONFIGURE -use <Interface> -format <contentFormat> -port <servicePort> -host <hostname>
+> HELP
+> EXIT
+> GETALL
+> GET <graphId>
+> CREATE <validFilePath>
+> UPDATE <graphId> <validFilePath>
+> DELETE <graphId>
+> VERIFY <graphId> <verificationType> <sourceNode> <destinationNode> [ <middleboxNode> ]
+
+
+-- CLI commands --
+> CONFIGURE -use <Interface> -format <contentFormat>
+Allows to configure connection parameters (host and port ), the interface (REST or gRPC ) and the data format
+(JSON, XML or YAML ) to be used to communicate with the verification service, XML and YAML formats exploit an extension
+of TOSCA specification. At program start the default configuration uses the REST interface with XML data format. 
+Note that the JSON format is not supported by the grpc interface.
+
+> HELP
+Prints on screen the CLI documentation.
+
+> EXIT
+Closes REST/gRPC client and exits.
+
+
+-- CRUD on graphs --
+> GETALL
+Performs a request without specifying a particular graph id. The service will return
+a list of graph templates in the currently selected format and will print them on screen. 
+
+> GET <graphId>
+Performs a request for a specific graph whose id MUST be specified as a long integer
+value. If present the graph will be returned and printed on screen in the currently selected format.
+
+> CREATE <validFilePath>
+Performs a create graph request providing a graph template as a file.
+The <validFilePath> must point to an existing file  whose filenme must be coherent with the currently selected 
+data format. The server can accept or not the provided graph upon its validation against Tosca Verigraph specification
+for XML/YAML or against Verigraph JSON schema. For further info see Verigraph Service documentation at [...].
+
+> UPDATE <graphId> <validFilePath>
+Performs an update request for a specific graph providing a graph template as a file.
+The provided filename must be coherent with the currently selected data format and the <LongId> must be a long integer 
+corresponding to one of the graphs previously created. The server can accept or not the provided graph upon its 
+validation against Tosca Verigraph specification for XML/YAML or against Verigraph JSON schema.
+For further info see Verigraph Service documentation at [...].
+
+> DELETE <graphId>
+Performs a delete request for a specific graph.
+The provided id must be a long integer corresponding to a previously created graph.
+
+
+-- Verification --
+> VERIFY <graphId> <verificationType> <sourceNode> <destinationNode> [ <middleboxNode> ]
+Performs a verification request for a specific graph.
+The <graphId> must be the long integer id of a previously created graph. Three types of verification services are
+available: reachability, isolation and traversal. The source, destination and middlebox node parameters 
+must be provided as string and must correspond to the name of a Node in the specified graph. The middlebox parameter
+must not be provided in case of reachability verification.
\ No newline at end of file
diff --git a/verigraph/src/it/polito/verigraph/tosca/ToscaCLI.java b/verigraph/src/it/polito/verigraph/tosca/ToscaCLI.java
new file mode 100644 (file)
index 0000000..c9f01c2
--- /dev/null
@@ -0,0 +1,1055 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+\r
+package it.polito.verigraph.tosca;\r
+\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.StringReader;\r
+import java.io.StringWriter;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.NoSuchElementException;\r
+import java.util.Scanner;\r
+import java.util.regex.Pattern;\r
+\r
+import javax.ws.rs.ProcessingException;\r
+import javax.ws.rs.client.Client;\r
+import javax.ws.rs.client.ClientBuilder;\r
+import javax.ws.rs.client.Entity;\r
+import javax.ws.rs.client.Invocation.Builder;\r
+import javax.ws.rs.client.WebTarget;\r
+import javax.ws.rs.core.MediaType;\r
+import javax.ws.rs.core.Response;\r
+import javax.xml.bind.JAXBContext;\r
+import javax.xml.bind.JAXBException;\r
+import javax.xml.bind.Marshaller;\r
+import javax.xml.transform.OutputKeys;\r
+import javax.xml.transform.Source;\r
+import javax.xml.transform.Transformer;\r
+import javax.xml.transform.TransformerFactory;\r
+import javax.xml.transform.stream.StreamResult;\r
+import javax.xml.transform.stream.StreamSource;\r
+\r
+import com.fasterxml.jackson.annotation.JsonInclude;\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;\r
+import com.sun.research.ws.wadl.ObjectFactory;\r
+\r
+import it.polito.tosca.jaxb.Configuration;\r
+import it.polito.tosca.jaxb.Definitions;\r
+import it.polito.tosca.jaxb.TDefinitions;\r
+import it.polito.verigraph.grpc.GraphGrpc;\r
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;\r
+import it.polito.verigraph.grpc.ToscaPolicy;\r
+import it.polito.verigraph.grpc.client.ToscaClient;\r
+import it.polito.verigraph.grpc.server.GrpcUtils;\r
+import it.polito.verigraph.model.Graph;\r
+import it.polito.verigraph.tosca.converter.grpc.GraphToGrpc;\r
+import it.polito.verigraph.tosca.converter.grpc.GrpcToGraph;\r
+import it.polito.verigraph.tosca.converter.grpc.GrpcToXml;\r
+import it.polito.verigraph.tosca.converter.grpc.GrpcToYaml;\r
+import it.polito.verigraph.tosca.converter.grpc.XmlToGrpc;\r
+import it.polito.verigraph.tosca.converter.grpc.YamlToGrpc;\r
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;\r
+\r
+\r
+public class ToscaCLI {\r
+\r
+    private static final String helper = "./README_CLI.txt";\r
+\r
+    //Service parameters.\r
+    private String host;\r
+    private int port;\r
+\r
+    //New media type for yaml rest request\r
+    private static final MediaType yamlMedia = new MediaType("application", "x-yaml");\r
+    private static final String defaultHost = "localhost";\r
+    private static final int defaultRestPort = 8080;\r
+    private static final int defaultGrpcPort = 50051;\r
+\r
+    //Input validation patterns\r
+    private static final Pattern yamlSource = Pattern.compile(".*\\.yaml$");\r
+    private static final Pattern xmlSource = Pattern.compile(".*\\.xml");\r
+    private static final Pattern jsonSource = Pattern.compile(".*\\.json$");\r
+    private static final Pattern configOpt = Pattern.compile("-use|-format|-port|-host", Pattern.CASE_INSENSITIVE);\r
+    private static final Pattern useOpt = Pattern.compile("grpc|rest", Pattern.CASE_INSENSITIVE);\r
+    private static final Pattern formatOpt = Pattern.compile("yaml|json|xml", Pattern.CASE_INSENSITIVE);\r
+    private static final Pattern policies = Pattern.compile("reachability|isolation|traversal", Pattern.CASE_INSENSITIVE);\r
+\r
+    //Configuration parameters\r
+    private Boolean useRest;\r
+    private String mediatype;\r
+    private Client restClient;\r
+    private ToscaClient grpcClient;\r
+\r
+    public ToscaCLI(){\r
+        //Variables representing the client environment\r
+        this.useRest = true;\r
+        this.port = defaultRestPort;\r
+        this.host = defaultHost;\r
+        this.mediatype = MediaType.APPLICATION_XML;\r
+        this.restClient = null;\r
+        this.grpcClient = null;\r
+    }\r
+\r
+\r
+    public static void main(String[] args) {\r
+        ToscaCLI myclient = new ToscaCLI();\r
+        try {\r
+            myclient.clientStart();\r
+        } catch (Exception e) {\r
+            System.out.println("-- Unexpected error, service closing.");\r
+        }\r
+        return;\r
+    }\r
+\r
+    //Build base Uri for REST service\r
+    private String buildBaseUri() {\r
+        return "http://" + this.host + ":" + String.valueOf(this.port) + "/verigraph/api/graphs";\r
+    }\r
+\r
+    //Function iterating getting user commands.\r
+    public void clientStart(){\r
+        System.out.println("++ Welcome to Verigraph Verification Service...");\r
+        System.out.println("++ Type HELP for instructions on client use...");\r
+\r
+        Scanner reader = null;\r
+        InputStream input = System.in;\r
+        Scanner scan = new Scanner(System.in);\r
+        String commandline;\r
+\r
+        while(true) {\r
+            System.out.print("++ Please insert command : ");\r
+            try{\r
+\r
+                while(input.available()!=0) input.skip(input.available());\r
+                commandline = scan.nextLine();\r
+                reader = new Scanner(commandline);\r
+\r
+                switch (reader.next().toUpperCase()) {\r
+                case "GETALL":\r
+                    if(useRest) this.restGetAll(reader);\r
+                    else this.grpcGetAll(reader);\r
+                    break;\r
+                case "GET":\r
+                    if(useRest) this.restGet(reader);\r
+                    else this.grpcGet(reader);\r
+                    break;\r
+                case "CREATE":\r
+                    if(useRest) this.restCreate(reader);\r
+                    else this.grpcCreate(reader);\r
+                    break;\r
+                case "DELETE":\r
+                    if(useRest) this.restDelete(reader);\r
+                    else grpcDelete(reader);\r
+                    break;\r
+                case "UPDATE":\r
+                    if(useRest) this.restUpdate(reader);\r
+                    else this.grpcUpdate(reader);\r
+                    break;\r
+                case "VERIFY":\r
+                    if(useRest) this.restVerify(reader);\r
+                    else this.grpcVerify(reader);\r
+                    break;\r
+                case "HELP":\r
+                    this.printHelper();\r
+                    break;\r
+                case "CONFIGURE":\r
+                    this.setConfig(reader);\r
+                    break;\r
+                case "EXIT":\r
+                    System.out.println("++ Client closing...");\r
+                    scan.close();\r
+                    input.close();\r
+                    reader.close();\r
+                    if(grpcClient != null) this.grpcClient.shutdown();\r
+                    if(restClient != null) this.restClient.close();\r
+                    System.out.println("++ Goodbye!");\r
+                    System.exit(0);\r
+                    break;\r
+                default:\r
+                    System.out.println("-- Unknown or bad formed command, type HELP to show commands documentation.");\r
+                    break;\r
+                }\r
+\r
+            }catch(NoSuchElementException ex) {\r
+                System.err.println("-- Unrecognized or incorrect command,"\r
+                        + " type help to know how to use the client...");\r
+                continue;\r
+            }catch(IOException | InterruptedException ex){\r
+                handleError(ex);\r
+            }finally {\r
+                reader.close();\r
+            }\r
+        }\r
+\r
+    }\r
+\r
+\r
+    public void printHelper() {\r
+        Scanner filereader = null;\r
+        try {\r
+            File inputfile = new File(helper);\r
+            filereader = new Scanner(inputfile).useDelimiter("\\Z");\r
+            String content = filereader.next();\r
+            if (filereader.ioException() != null) {\r
+                throw new IOException(filereader.ioException());\r
+            }\r
+            if(content != null) System.out.println(content);\r
+        } catch (IOException e) {\r
+            handleError(e);\r
+        }finally {\r
+            if(filereader != null) filereader.close();\r
+        }\r
+\r
+    }\r
+\r
+    public void setConfig(Scanner reader) throws InterruptedException {\r
+        if(!reader.hasNext(configOpt)) {\r
+            System.out.println("-- No or bad formed configuration options provided.");\r
+            return;\r
+        }\r
+        while(reader.hasNext(configOpt)) {\r
+            switch(reader.next().toLowerCase()) {\r
+            case "-use":\r
+                if(reader.hasNext(useOpt)) {\r
+                    if(reader.next().toLowerCase().equals("rest")) {\r
+                        if(grpcClient != null) {\r
+                            grpcClient.shutdown();\r
+                            grpcClient = null;\r
+                        }\r
+                        this.port = defaultRestPort;\r
+                        restClient = ClientBuilder.newClient();\r
+                        useRest = true;\r
+                    }\r
+                    else {\r
+                        if(restClient != null) {\r
+                            restClient.close();\r
+                            restClient = null;\r
+                        }\r
+                        this.port = defaultGrpcPort;\r
+                        grpcClient = new ToscaClient(host, port);\r
+                        useRest = false;\r
+                    }\r
+                }else {\r
+                    System.out.println("-- Unrecognized values for option -use, accepted values are: rest, grpc.");\r
+                }\r
+                break;\r
+            case "-format":\r
+                if(reader.hasNext(formatOpt)) {\r
+                    String command = reader.next();\r
+                    if(command.toLowerCase().equals("json")) mediatype = MediaType.APPLICATION_JSON;\r
+                    else if(command.toLowerCase().equals("xml")) mediatype = MediaType.APPLICATION_XML;\r
+                    else if(command.toLowerCase().equals("yaml")) mediatype = "application/x-yaml";\r
+                }else {\r
+                    System.out.println("-- Unrecognized values for option -format, accepted formats are: json, xml, yaml.");\r
+                }\r
+                break;\r
+            case "-host":\r
+                if(reader.hasNext()) {\r
+                    this.host = reader.next();\r
+                    if(grpcClient != null) {\r
+                        grpcClient.shutdown();\r
+                        grpcClient = new ToscaClient(host, port);\r
+                        System.out.println("++ Host configuration changed restarting grpc client...");\r
+                    }\r
+                }\r
+                else {\r
+                    System.out.println("-- Provide a valid hostname.");\r
+                }\r
+                break;\r
+            case "-port":\r
+                if(reader.hasNextInt()) {\r
+                    int newvalue = reader.nextInt();\r
+                    if(0 > newvalue || 65535 < newvalue) {\r
+                        System.out.println("-- The provided port number is not valid, port has not been modified.");\r
+                    }else {\r
+                        this.port = newvalue;\r
+                        if(grpcClient != null) {\r
+                            grpcClient.shutdown();\r
+                            grpcClient = new ToscaClient(host, port);\r
+                            System.out.println("++ Port configuration changed restarting grpc client...");\r
+                        }\r
+                    }\r
+                }\r
+                else {\r
+                    System.out.println("-- Provide a port as an integer.");\r
+                }\r
+                break;\r
+            default:\r
+                System.out.println("-- Unrecognized option!");\r
+            }\r
+        }\r
+\r
+    }\r
+\r
+    //Utility function used only to print exception message\r
+    public void handleError(Exception e) {\r
+        String errMsg = e.getMessage();\r
+        if(errMsg == null) {\r
+            System.out.println("-- Error: unexpected error occurred.");\r
+        }else {\r
+            System.out.println("-- Error: " + errMsg);\r
+        }\r
+        return;\r
+    }\r
+\r
+\r
+    // RESTful service interface CRUD and Verify functions\r
+    public void restGetAll(Scanner reader) {\r
+        try {\r
+            // Build a new client if it does not exist\r
+            if (restClient == null)\r
+                restClient = ClientBuilder.newClient();\r
+\r
+            // targeting the graphs resource\r
+            WebTarget target = restClient.target(this.buildBaseUri());\r
+\r
+            // Performing the request and reading the response\r
+            Response res = target.request(mediatype).get();\r
+            this.readResponseRest("GETALL", res);\r
+\r
+        }catch(ProcessingException e) {\r
+            System.out.println("-- Error: the provided host address is not valid.");\r
+        }catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+        return;\r
+    }\r
+\r
+\r
+    public void restGet(Scanner reader) {\r
+\r
+        try {\r
+            // Build a new client if it does not exist\r
+            if (restClient == null)\r
+                restClient = ClientBuilder.newClient();\r
+\r
+            if (!reader.hasNextLong()) {\r
+                System.out.println("-- Provide the integer Id for the requested graph.");\r
+                return;\r
+            }\r
+\r
+            // Targeting the specified graph resource\r
+            WebTarget target = restClient.target(this.buildBaseUri() + "/" + String.valueOf(reader.nextLong()));\r
+\r
+            // Performing the request and reading the response\r
+            Response res = target.request(mediatype).get();\r
+            this.readResponseRest("GET", res);\r
+\r
+        }catch(ProcessingException e) {\r
+            System.out.println("-- Error: the provided host address is not valid.");\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+    }\r
+\r
+\r
+\r
+    public void restCreate(Scanner reader) {\r
+\r
+        try {\r
+            // Getting file content\r
+            String content = readFile(reader);\r
+            if (content == null) {\r
+                System.out.println("-- The required operation can't be performed.");\r
+                return;\r
+            }\r
+\r
+            // Build a new client if it does not exist\r
+            if (restClient == null)\r
+                restClient = ClientBuilder.newClient();\r
+\r
+            // Targeting the resource\r
+            WebTarget target = restClient.target(this.buildBaseUri());\r
+\r
+            // Performing the request and reading the response\r
+            Builder mypost = target.request(mediatype);\r
+            Response res = null;\r
+            switch (mediatype) {\r
+            case MediaType.APPLICATION_JSON:\r
+                res = mypost.post(Entity.json(content));\r
+                break;\r
+            case MediaType.APPLICATION_XML:\r
+                res = mypost.post(Entity.xml(content));\r
+                break;\r
+            case "application/x-yaml":\r
+                res = mypost.post(Entity.entity(content, yamlMedia));\r
+                break;\r
+            }\r
+\r
+            this.readResponseRest("CREATE", res);\r
+        }catch(ProcessingException e) {\r
+            System.out.println("-- Error: the provided host address is not valid.");\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+        return;\r
+    }\r
+\r
+\r
+\r
+    public void restDelete(Scanner reader) {\r
+        try {\r
+            // Build a new client if it does not exist\r
+            if (restClient == null)\r
+                restClient = ClientBuilder.newClient();\r
+\r
+            if (!reader.hasNextLong()) {\r
+                System.out.println("-- Provide the integer Id of the graph you want to delete.");\r
+                return;\r
+            }\r
+\r
+            // Targeting the specified graph resource\r
+            WebTarget target = restClient.target(this.buildBaseUri() + "/" + String.valueOf(reader.nextLong()));\r
+\r
+            // Performing the request and reading the response\r
+            Response res = target.request(mediatype).delete();\r
+            this.readResponseRest("DELETE", res);\r
+        }catch(ProcessingException e) {\r
+            System.out.println("-- Error: the provided host address is not valid.");\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+        return;\r
+    }\r
+\r
+\r
+    public  void restUpdate(Scanner reader) {\r
+        try {\r
+\r
+            //Getting the target graph\r
+            if(!reader.hasNextLong()) {\r
+                System.out.println("-- Please provide a valid id for the graph to be update");\r
+                return;\r
+            }\r
+\r
+            // Build a new client if it does not exist\r
+            if (restClient == null)\r
+                restClient = ClientBuilder.newClient();\r
+\r
+            // Targeting the resource\r
+            WebTarget target = restClient.target(this.buildBaseUri() + "/" + reader.next());\r
+\r
+            // Getting file content\r
+            String content = readFile(reader);\r
+            if (content == null) {\r
+                System.out.println("-- The required operation can't be performed.");\r
+                return;\r
+            }\r
+\r
+            // Performing the request and reading the resonse\r
+            Builder myupdate = target.request(mediatype);\r
+            Response res = null;\r
+            switch (mediatype) {\r
+            case MediaType.APPLICATION_JSON:\r
+                res = myupdate.put(Entity.json(content));\r
+                break;\r
+            case MediaType.APPLICATION_XML:\r
+                res = myupdate.put(Entity.xml(content));\r
+                break;\r
+            case "application/x-yaml":\r
+                res = myupdate.put(Entity.entity(content, yamlMedia));\r
+                break;\r
+            }\r
+\r
+            this.readResponseRest("UPDATE", res);\r
+\r
+        }catch(ProcessingException e) {\r
+            System.out.println("-- Error: the provided host address is not valid.");\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+    }\r
+\r
+\r
+    public  void restVerify(Scanner reader) {\r
+        String whichpolicy = null;\r
+        String graphId, source, destination, middlebox = null;\r
+\r
+        try {\r
+            if(!reader.hasNextLong()) {\r
+                System.out.println("-- Provide the graph on which you want to perform verification.");\r
+                return;\r
+            }\r
+            graphId = reader.next();\r
+\r
+            if (!reader.hasNext(policies)) {\r
+                System.out.println("-- Provide the requested type of verfication.");\r
+                return;\r
+            }\r
+            whichpolicy = reader.next().toLowerCase();\r
+\r
+            try {\r
+                source = reader.next();\r
+                destination = reader.next();\r
+                if(!whichpolicy.equals("reachability")) {\r
+                    middlebox = reader.next();\r
+                }\r
+            }catch(NoSuchElementException ex) {\r
+                System.out.println("-- Wrong or missing verification parameters.");\r
+                return;\r
+            }\r
+\r
+            // Build a new client if it does not exist\r
+            if (restClient == null)\r
+                restClient = ClientBuilder.newClient();\r
+\r
+            // Targeting the resource\r
+            WebTarget target = restClient.target(this.buildBaseUri() + "/" + graphId + "/policy")\r
+                    .queryParam("source", source)\r
+                    .queryParam("destination", destination)\r
+                    .queryParam("type", whichpolicy);\r
+            if(!whichpolicy.equals("reachability")) {\r
+                target = target.queryParam("middlebox", middlebox);\r
+            }\r
+\r
+            Response res = target.request(mediatype).get();\r
+            this.readResponseRest("VERIFY", res);\r
+        }catch(ProcessingException e) {\r
+            System.out.println("-- Error: the provided host address is not valid.");\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+    }\r
+\r
+    //gRPC service interface CRUD and Verify functions\r
+    public void grpcGetAll(Scanner reader) {\r
+\r
+        try {\r
+            if(grpcClient == null)\r
+                grpcClient = new ToscaClient(host, port);\r
+\r
+            //Added for backward compatibility with JSON grpc\r
+            if(mediatype == MediaType.APPLICATION_JSON) {\r
+                List<GraphGrpc> receivedGraphsGrpc = grpcClient.getGraphs();\r
+\r
+                if(receivedGraphsGrpc == null) {\r
+                    System.out.println("-- GET Failed : was not possible to perform the required operations.");\r
+                    return;\r
+                }\r
+                else if(receivedGraphsGrpc.isEmpty()) {\r
+                    System.out.println("++ GET Success no graph was returned.");\r
+                    return;\r
+                }\r
+\r
+                List<Graph> receivedGraphs = new ArrayList<Graph>();\r
+                for(GraphGrpc curr : receivedGraphsGrpc) {\r
+                    receivedGraphs.add(GrpcUtils.deriveGraph(curr));\r
+                }\r
+                this.marshallToJson(receivedGraphs);\r
+                return;\r
+            }\r
+\r
+            //Code for the Tosca compliant implementation\r
+            List<TopologyTemplateGrpc> templates;\r
+            templates =  grpcClient.getTopologyTemplates();\r
+\r
+            if(templates == null) {\r
+                System.out.println("-- GET Failed : was not possible to perform the required operations.");\r
+                return;\r
+            }\r
+            else if(templates.isEmpty()) {\r
+                System.out.println("++ GET Success no graph was returned.");\r
+                return;\r
+            }\r
+\r
+            switch(mediatype) {\r
+            case MediaType.APPLICATION_XML:\r
+                List<Definitions> receivedDefs = new ArrayList<Definitions>();\r
+                for(TopologyTemplateGrpc curr : templates) {\r
+                    receivedDefs.add(GrpcToXml.mapGraph(curr));\r
+                }\r
+                this.marshallToXml(receivedDefs);\r
+                break;\r
+\r
+            case "application/x-yaml":\r
+                List<ServiceTemplateYaml> receivedTempls = new ArrayList<ServiceTemplateYaml>();\r
+                for(TopologyTemplateGrpc curr : templates) {\r
+                    receivedTempls.add(GrpcToYaml.mapGraphYaml(curr));\r
+                }\r
+                this.marshallToYaml(receivedTempls);\r
+                break;\r
+\r
+            }\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+    }\r
+\r
+\r
+    public void grpcGet(Scanner reader) {\r
+\r
+        try {\r
+            if (grpcClient == null)\r
+                grpcClient = new ToscaClient(host, port);\r
+\r
+            if (!reader.hasNextLong()) {\r
+                System.out.println("-- Provide the integer Id for the requested graph.");\r
+                return;\r
+            }\r
+\r
+            //Added for backward compatibility with JSON grpc\r
+            if(mediatype == MediaType.APPLICATION_JSON) {\r
+                GraphGrpc graph = grpcClient.getGraph(reader.nextLong());\r
+                if(graph == null || !graph.getErrorMessage().equals(""));\r
+                List<Graph> receivedGraphs = new ArrayList<Graph>();\r
+                receivedGraphs.add(GrpcUtils.deriveGraph(graph));\r
+                this.marshallToJson(receivedGraphs);\r
+                return;\r
+            }\r
+\r
+            //Code for Tosca compliant implementation\r
+            TopologyTemplateGrpc templ = grpcClient.getTopologyTemplate(reader.next());\r
+            if(templ == null || !templ.getErrorMessage().equals("")) {\r
+                return;\r
+            }\r
+            switch(mediatype) {\r
+            //      case MediaType.APPLICATION_JSON:\r
+            //        Graph obt = GrpcToGraph.deriveGraph(templ);\r
+            //        List<Graph> list = new ArrayList<Graph>();\r
+            //        list.add(obt);\r
+            //        marshallToJson(list);\r
+            //        break;\r
+            case MediaType.APPLICATION_XML:\r
+                List<Definitions> receivedDefs = new ArrayList<Definitions>();\r
+                receivedDefs.add(GrpcToXml.mapGraph(templ));\r
+                this.marshallToXml(receivedDefs);\r
+                break;\r
+\r
+            case "application/x-yaml":\r
+                List<ServiceTemplateYaml> receivedTempls = new ArrayList<ServiceTemplateYaml>();\r
+                receivedTempls.add(GrpcToYaml.mapGraphYaml(templ));\r
+                this.marshallToYaml(receivedTempls);\r
+                break;\r
+            }\r
+\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+    }\r
+\r
+    public void grpcCreate(Scanner reader) {\r
+        try {\r
+            if (grpcClient == null)\r
+                grpcClient = new ToscaClient(host, port);\r
+\r
+            switch (mediatype) {\r
+            case MediaType.APPLICATION_JSON:\r
+                if(reader.hasNext(jsonSource)) {\r
+                    ObjectMapper mapper = new ObjectMapper();\r
+                    Graph modelGraph = mapper.readValue(readFile(reader), Graph.class);\r
+                    GraphGrpc graph = GrpcUtils.obtainGraph(modelGraph);\r
+                    grpcClient.createGraph(graph);\r
+                }else {\r
+                    System.out.println("-- The provided file is not compatible with the current configuration [json].");\r
+                    return;\r
+                }\r
+                break;\r
+            case MediaType.APPLICATION_XML:\r
+                if (reader.hasNext(xmlSource)) {\r
+                    grpcClient.createTopologyTemplate(XmlToGrpc.obtainTopologyTemplateGrpc(reader.next()));\r
+                } else {\r
+                    System.out.println("-- The provided file is not compatible with the current configuration [xml].");\r
+                    return;\r
+                }\r
+                break;\r
+\r
+            case "application/x-yaml":\r
+                if (reader.hasNext(yamlSource)) {\r
+                    grpcClient.createTopologyTemplate(YamlToGrpc.obtainTopologyTemplateGrpc(reader.next()));\r
+                } else {\r
+                    System.out.println("-- The provided file is not compatible with the current configuration [yaml].");\r
+                    return;\r
+                }\r
+                break;\r
+            }\r
+\r
+        } catch (JAXBException je) {\r
+            System.out.println("-- Error while parsing xml : " + je.getMessage());\r
+        } catch (IOException ie) {\r
+            System.out.println("-- Error reading the file : " + ie.getMessage());\r
+        } catch(Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+        return;\r
+    }\r
+\r
+\r
+    public  void grpcDelete(Scanner reader) {\r
+\r
+        try {\r
+            if (grpcClient == null)\r
+                grpcClient = new ToscaClient(host, port);\r
+\r
+            if (!reader.hasNextLong()) {\r
+                System.out.println("-- Provide the integer Id of the graph you want to delete.");\r
+                return;\r
+            }\r
+\r
+            grpcClient.deleteTopologyTemplate(reader.next());\r
+\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+        return;\r
+    }\r
+\r
+\r
+    public  void grpcUpdate(Scanner reader) {\r
+        try {\r
+            if (grpcClient == null)\r
+                grpcClient = new ToscaClient(host, port);\r
+\r
+            //Checking if user ha provided the id of the graph to be updated and retrieving it\r
+            if(!reader.hasNextLong()) {\r
+                System.out.println("-- Please provide a valid id for the graph to be update");\r
+                return;\r
+            }\r
+            String id = reader.next();\r
+\r
+            //Reading the file and performing the request according to current configuration\r
+            switch (mediatype) {\r
+            case MediaType.APPLICATION_JSON:\r
+                if(reader.hasNext(jsonSource)) {\r
+                    ObjectMapper mapper = new ObjectMapper();\r
+                    Graph modelGraph = mapper.readValue(readFile(reader), Graph.class);\r
+                    GraphGrpc graph = GrpcUtils.obtainGraph(modelGraph);\r
+                    grpcClient.updateGraph(new Long(id), graph);\r
+                }else {\r
+                    System.out.println("-- The provided file is not compatible with the current configuration [json].");\r
+                    return;\r
+                }\r
+                break;\r
+            case MediaType.APPLICATION_XML:\r
+                if (reader.hasNext(xmlSource)) {\r
+                    grpcClient.updateTopologyTemplate(XmlToGrpc.obtainTopologyTemplateGrpc(reader.next()), id);\r
+                } else {\r
+                    System.out.println("-- The provided file is not compatible with the current configuration.");\r
+                    return;\r
+                }\r
+                break;\r
+\r
+            case "application/x-yaml":\r
+                if (reader.hasNext(yamlSource)) {\r
+                    grpcClient.updateTopologyTemplate(YamlToGrpc.obtainTopologyTemplateGrpc(reader.next()), id);\r
+                } else {\r
+                    System.out.println("-- The provided file is not compatible with the current configuration.");\r
+                    return;\r
+                }\r
+                break;\r
+            }\r
+        } catch (JAXBException je) {\r
+            System.out.println("-- Error while parsing xml : " + je.getMessage());\r
+        } catch (IOException ie) {\r
+            System.out.println("-- Error reading the file : " + ie.getMessage());\r
+        } catch(Exception e) {\r
+            handleError(e);\r
+        }\r
+    }\r
+\r
+\r
+    public  void grpcVerify(Scanner reader) {\r
+        ToscaPolicy.Builder policyBuilder = ToscaPolicy.newBuilder();\r
+        String graphId, whichPolicy, source, destination, middlebox = null;\r
+\r
+        try {\r
+            if(!reader.hasNextLong()) {\r
+                System.out.println("-- Provide the graph on which you want to perform verification.");\r
+                return;\r
+            }\r
+            graphId = reader.next();\r
+\r
+            if (!reader.hasNext(policies)) {\r
+                System.out.println("-- Provide the requested type of verfication.");\r
+                return;\r
+            }\r
+            whichPolicy = reader.next().toLowerCase();\r
+\r
+            try {\r
+                source = reader.next();\r
+                destination = reader.next();\r
+                if(!whichPolicy.equals("reachability")) {\r
+                    middlebox = reader.next();\r
+                }\r
+            }catch(NoSuchElementException ex) {\r
+                System.out.println("-- Wrong or missing verification parameters.");\r
+                return;\r
+            }\r
+\r
+            policyBuilder.setIdTopologyTemplate(graphId);\r
+            policyBuilder.setDestination(destination);\r
+            policyBuilder.setSource(source);\r
+            switch(whichPolicy) {\r
+            case "reachability":\r
+                policyBuilder.setType(ToscaPolicy.PolicyType.forNumber(0));\r
+                break;\r
+            case "isolation":\r
+                policyBuilder.setType(ToscaPolicy.PolicyType.forNumber(1));\r
+                policyBuilder.setMiddlebox(middlebox);\r
+                break;\r
+            case "traversal":\r
+                policyBuilder.setType(ToscaPolicy.PolicyType.forNumber(2));\r
+                policyBuilder.setMiddlebox(middlebox);\r
+                break;\r
+            }\r
+\r
+            if (grpcClient == null)\r
+                grpcClient = new ToscaClient(host, port);\r
+\r
+            //Sending verification request\r
+            grpcClient.verifyPolicy(policyBuilder.build());\r
+\r
+        } catch (Exception e) {\r
+            handleError(e);\r
+        }\r
+\r
+        return;\r
+    }\r
+\r
+\r
+    public void readResponseRest(String responseOf, Response res) {\r
+        switch(responseOf) {\r
+        case "GETALL":\r
+            switch (res.getStatus()) {\r
+            case 200:\r
+                System.out.println("++ GET success :");\r
+                break;\r
+            case 500:\r
+                System.out.println("-- GET failed : internal server error.");\r
+                break;\r
+            default:\r
+                System.out.println("** Unexpected response");\r
+                break;\r
+            }\r
+            break;\r
+\r
+        case "GET":\r
+            switch (res.getStatus()) {\r
+            case 200:\r
+                System.out.println("++ GET success :");\r
+                break;\r
+            case 404:\r
+                System.out.println("-- GET failed : graph not found.");\r
+                break;\r
+            case 500:\r
+                System.out.println("-- GET failed : internal server error.");\r
+                break;\r
+            default:\r
+                System.out.println("** Unexpected response **");\r
+                break;\r
+            }\r
+            break;\r
+\r
+        case "CREATE":\r
+            switch (res.getStatus()) {\r
+            case 201:\r
+                System.out.println("++ POST success : graph created.");\r
+                break;\r
+            case 400:\r
+                System.out.println("-- POST failed : bad request.");\r
+                break;\r
+            case 500:\r
+                System.out.println("-- POST failed : internal server error.");\r
+                break;\r
+            default:\r
+                System.out.println("** Unexpected response **");\r
+                break;\r
+            }\r
+            break;\r
+        case "DELETE":\r
+            switch (res.getStatus()) {\r
+            case 204:\r
+                System.out.println("++ DELETE success : graph deleted.");\r
+                break;\r
+            case 403:\r
+                System.out.println("-- DELETE failed : invalid graph ID.");\r
+                break;\r
+            case 404:\r
+                System.out.println("-- DELETE failed : invalid graph id.");\r
+                break;\r
+            case 500:\r
+                System.out.println("-- DELETE failed : internal server error.");\r
+                break;\r
+            default:\r
+                System.out.println("** Unexpected response **");\r
+                break;\r
+            }\r
+            break;\r
+        case "UPDATE":\r
+            switch (res.getStatus()) {\r
+            case 200:\r
+                System.out.println("++ PUT success : graph correctly updated.");\r
+                break;\r
+            case 400:\r
+                System.out.println("-- PUT failed : invalid graph object.");\r
+                break;\r
+            case 403:\r
+                System.out.println("-- PUT failed : invalid graph ID.");\r
+                break;\r
+            case 404:\r
+                System.out.println("-- PUT failed : graph not found.");\r
+                break;\r
+            case 500:\r
+                System.out.println("-- PUT failed : internal server error.");\r
+                break;\r
+            default:\r
+                System.out.println("** Unexpected response **");\r
+                break;\r
+            }\r
+            break;\r
+\r
+        default:\r
+\r
+        }\r
+\r
+        //In case of errors we do not read the message body\r
+        if(res.hasEntity() && res.getStatus() <= 300) {\r
+            String responseBody = prettyFormat(res.readEntity(String.class));\r
+            if(responseBody != null) System.out.println(responseBody);\r
+        }\r
+        else {\r
+            System.out.println("++ No content in the message body");\r
+        }\r
+\r
+        return;\r
+    }\r
+\r
+\r
+    public void marshallToXml(List<Definitions> defs) {\r
+        try {\r
+            JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class, TDefinitions.class, Configuration.class);\r
+            Marshaller m = jc.createMarshaller();\r
+            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);\r
+            for (Definitions def : defs) {\r
+                // To be tested, in case of problems def must be converted to a JAXBElement\r
+                m.marshal(def, System.out);\r
+                System.out.println("\n");\r
+            }\r
+\r
+        } catch (JAXBException je) {\r
+            System.out.println("-- Error while marshalling : " + je.getMessage());\r
+        }\r
+        return;\r
+    }\r
+\r
+    public void marshallToYaml(List<ServiceTemplateYaml> templates) {\r
+        try {\r
+            YAMLMapper mapper = new YAMLMapper();\r
+            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);\r
+            for (ServiceTemplateYaml templ : templates) {\r
+                System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(templ));\r
+                System.out.println("\n");\r
+            }\r
+\r
+        } catch (JsonProcessingException je) {\r
+            System.out.println("-- Error while marshalling : " + je.getMessage());\r
+\r
+        }\r
+        return;\r
+    }\r
+\r
+    public void marshallToJson(List<Graph> templates) {\r
+        try {\r
+            ObjectMapper mapper = new ObjectMapper();\r
+            //mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);\r
+            for (Graph templ : templates) {\r
+                System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(templ));\r
+                System.out.println("\n");\r
+            }\r
+\r
+        } catch (JsonProcessingException je) {\r
+            System.out.println("-- Error while marshalling : " + je.getMessage());\r
+\r
+        }\r
+        return;\r
+    }\r
+\r
+\r
+    // Reads the whole file into a string and performs a minimum validation on file type\r
+    public String readFile(Scanner reader) {\r
+\r
+        String content = null;\r
+        Scanner filereader = null;\r
+        if ((mediatype.equals("application/x-yaml") && reader.hasNext(yamlSource))\r
+                || (mediatype.equals(MediaType.APPLICATION_XML) && reader.hasNext(xmlSource))\r
+                || (mediatype.equals(MediaType.APPLICATION_JSON) && reader.hasNext(jsonSource))) {\r
+            try {\r
+                File inputfile = new File(reader.next());\r
+                filereader = new Scanner(inputfile).useDelimiter("\\Z");\r
+                content = filereader.next();\r
+                if (filereader.ioException() != null) {\r
+                    throw new IOException(filereader.ioException());\r
+                } else {\r
+                    System.out.println("++ File correctly read.");\r
+                }\r
+            } catch (FileNotFoundException ex) {\r
+                System.out.println("-- Error : the provided file does not exist!");\r
+            }catch (IOException ex) {\r
+                System.out.println("-- Error : an error occurred reading the input file!");\r
+            }catch (Exception e) {\r
+                handleError(e);\r
+            }finally {\r
+                if(filereader != null) filereader.close();\r
+            }\r
+\r
+        } else {\r
+            System.out.println("-- Error : the file provided in input does not match with the current client configuration.");\r
+        }\r
+\r
+        return content;\r
+    }\r
+\r
+\r
+    public String prettyFormat(String input) {\r
+        String formattedString = null;\r
+\r
+        try {\r
+            switch(mediatype) {\r
+            case MediaType.APPLICATION_XML:\r
+                Source xmlInput = new StreamSource(new StringReader(input));\r
+                StringWriter stringWriter = new StringWriter();\r
+                StreamResult xmlOutput = new StreamResult(stringWriter);\r
+                TransformerFactory transformerFactory = TransformerFactory.newInstance();\r
+                transformerFactory.setAttribute("indent-number", 2);\r
+                Transformer transformer = transformerFactory.newTransformer();\r
+                transformer.setOutputProperty(OutputKeys.INDENT, "yes");\r
+                transformer.transform(xmlInput, xmlOutput);\r
+                formattedString = xmlOutput.getWriter().toString();\r
+                break;\r
+            case MediaType.APPLICATION_JSON:\r
+                ObjectMapper mapper = new ObjectMapper();\r
+                Object jsonObj = mapper.readValue(input, Object.class);\r
+                formattedString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObj);\r
+                break;\r
+            case "application/x-yaml":\r
+                formattedString = input;\r
+                break;\r
+            }\r
+        } catch (Exception e) {\r
+            formattedString = e.getCause().toString();\r
+        }\r
+\r
+        return formattedString;\r
+    }\r
+\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/XmlParsingUtils.java b/verigraph/src/it/polito/verigraph/tosca/XmlParsingUtils.java
new file mode 100644 (file)
index 0000000..5830bf8
--- /dev/null
@@ -0,0 +1,142 @@
+/*******************************************************************************
+ * Copyright (c) 2017/18 Politecnico di Torino and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Apache License, Version 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *******************************************************************************/
+package it.polito.verigraph.tosca;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+
+import com.sun.research.ws.wadl.ObjectFactory;
+
+import it.polito.verigraph.exception.DataNotFoundException;
+import it.polito.tosca.jaxb.Configuration;
+import it.polito.tosca.jaxb.Definitions;
+import it.polito.tosca.jaxb.TDefinitions;
+import it.polito.tosca.jaxb.TEntityTemplate;
+import it.polito.tosca.jaxb.TExtensibleElements;
+import it.polito.tosca.jaxb.TNodeTemplate;
+import it.polito.tosca.jaxb.TRelationshipTemplate;
+import it.polito.tosca.jaxb.TServiceTemplate;
+import it.polito.tosca.jaxb.TTopologyTemplate;
+import it.polito.verigraph.tosca.converter.grpc.ToscaGrpcUtils;
+
+
+public class XmlParsingUtils {
+
+    /** Returns a List of TServiceTemplate JAXB-generated objects, parsed from a TOSCA-compliant XML. */
+    public static Definitions obtainDefinitions(String file) throws JAXBException, IOException, ClassCastException, DataNotFoundException {
+        // Create a JAXBContext capable of handling the generated classes
+        JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class, TDefinitions.class, Configuration.class);
+        Unmarshaller u = jc.createUnmarshaller();
+
+        //Retrieve the TDefinitions object
+        Source source = new StreamSource(new FileInputStream(file));
+
+        JAXBElement<Definitions> rootElement = (JAXBElement<Definitions>)u.unmarshal(source, Definitions.class);
+        Definitions definitions = rootElement.getValue();
+        return definitions;
+    }
+
+    /** Returns a List of TServiceTemplate JAXB-generated objects, parsed from a TOSCA-compliant XML. */
+    public static List<TServiceTemplate> obtainServiceTemplates(String file) throws JAXBException, IOException, ClassCastException, DataNotFoundException {
+        // Create a JAXBContext capable of handling the generated classes
+        JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class, TDefinitions.class, Configuration.class);
+        Unmarshaller u = jc.createUnmarshaller();
+
+        //Retrieve the TDefinitions object
+        Source source = new StreamSource(new FileInputStream(file));
+
+        JAXBElement<TDefinitions> rootElement = (JAXBElement<TDefinitions>)u.unmarshal(source, TDefinitions.class);
+        TDefinitions definitions = rootElement.getValue();
+        List<TExtensibleElements> elements = definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation();
+
+        //Retrieve the list of ServiceTemplate in Definitions
+        List<TServiceTemplate> serviceTemplates = elements.stream()
+                .filter(p -> p instanceof TServiceTemplate)
+                .map(obj -> (TServiceTemplate) obj).collect(Collectors.toList());
+
+        if (serviceTemplates.isEmpty())
+            throw new DataNotFoundException("There is no ServiceTemplate into the TOSCA XML file");
+        return serviceTemplates; // Could be an empty list if there are no TServiceTemplate objects
+    }
+
+
+    /** Returns a List of TNodeTemplate JAXB-generated TOSCA objects. */
+    public static List<TNodeTemplate> obtainNodeTemplates(TServiceTemplate serviceTemplate) throws DataNotFoundException {
+        TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
+
+        // Retrieving a list of TNodeTemplate and TRelationshipTemplate JAXB objects
+        List<TEntityTemplate> entities = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
+
+        // Retrieving a List containing only TNodeTemplates objects
+        List<TNodeTemplate> nodeTemplates = entities.stream()
+                .filter(p -> p instanceof TNodeTemplate)
+                .map(obj -> (TNodeTemplate) obj).collect(Collectors.toList());
+
+        if (nodeTemplates.isEmpty())
+            throw new DataNotFoundException("There is no NodeTemplate into ServiceTemplate " + serviceTemplate.toString() + " and TopologyTemplate " + topologyTemplate.toString());
+        return nodeTemplates; // Could be an empty list if there are no TNodeTemplate objects
+    }
+
+
+    /** Returns a List of TRelationshipTemplate JAXB-generated TOSCA objects. */
+    public static List<TRelationshipTemplate> obtainRelationshipTemplates(TServiceTemplate serviceTemplate) throws DataNotFoundException {
+        TTopologyTemplate topologyTemplate = serviceTemplate.getTopologyTemplate();
+
+        // Retrieving a List of TNodeTemplate and TRelationshipTemplate JAXB objects
+        List<TEntityTemplate> entities = topologyTemplate.getNodeTemplateOrRelationshipTemplate();
+
+        // Retrieving a List containing only TRelationshipTemplate objects
+        List<TRelationshipTemplate> relationshipTemplates = entities.stream()
+                .filter(p -> p instanceof TRelationshipTemplate)
+                .map(obj -> (TRelationshipTemplate) obj).collect(Collectors.toList());
+
+        if (relationshipTemplates.isEmpty())
+            throw new DataNotFoundException("There is no RelationshipTemplate into ServiceTemplate " + serviceTemplate.toString() + " and TopologyTemplate " + topologyTemplate.toString());
+        return relationshipTemplates; // Could be an empty list if there are no TRelationshipTemplate objects
+    }
+
+
+    /** Returns the it.polito.tosca.jaxb.Configuration JAXB-generated TOSCA object of a TOSCA NodeTemplate. */
+    public static Configuration obtainConfiguration(TNodeTemplate nodeTemplate) {
+        try {
+            Configuration configuration = (Configuration)nodeTemplate.getProperties().getAny();
+
+            //This could be eventually used to cross check node type and configuration type
+            //String typename = nodeTemplate.getType().getLocalPart().toLowerCase();
+            return configuration;
+
+
+        } catch (NullPointerException | ClassCastException ex) {
+            //To be eventually defined a mechanism to distinguish hostnode from forwarder
+            System.out.println("[Warning] Node " + nodeTemplate.getId().toString()
+                    + ": missing or invalid configuration, the node will be configured as a forwarder!" );
+            Configuration defConf = new Configuration();
+            defConf.setConfDescr(ToscaGrpcUtils.defaultDescr);
+            defConf.setConfID(ToscaGrpcUtils.defaultConfID);
+
+            Configuration.FieldmodifierConfiguration defaultForward = new Configuration.FieldmodifierConfiguration();
+            defaultForward.setName("DefaultForwarder");
+
+            defConf.setFieldmodifierConfiguration(defaultForward);
+            return defConf;
+        }
+    }
+
+}
diff --git a/verigraph/src/it/polito/verigraph/tosca/YamlParsingUtils.java b/verigraph/src/it/polito/verigraph/tosca/YamlParsingUtils.java
new file mode 100644 (file)
index 0000000..6e65ac9
--- /dev/null
@@ -0,0 +1,160 @@
+/*******************************************************************************\r
+ * Copyright (c) 2017/18 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.StringWriter;\r
+import java.util.Map;\r
+\r
+import javax.xml.bind.JAXBException;\r
+\r
+import com.fasterxml.jackson.core.JsonParseException;\r
+import com.fasterxml.jackson.core.JsonParser;\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.databind.JsonMappingException;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.databind.module.SimpleModule;\r
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;\r
+\r
+import it.polito.verigraph.exception.BadRequestException;\r
+import it.polito.verigraph.exception.DataNotFoundException;\r
+import it.polito.verigraph.exception.InvalidServiceTemplateException;\r
+import it.polito.verigraph.tosca.serializer.YamlConfigSerializer;\r
+import it.polito.verigraph.tosca.yaml.beans.AntispamNode;\r
+import it.polito.verigraph.tosca.yaml.beans.CacheNode;\r
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.DpiNode;\r
+import it.polito.verigraph.tosca.yaml.beans.EndhostNode;\r
+import it.polito.verigraph.tosca.yaml.beans.EndpointNode;\r
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierNode;\r
+import it.polito.verigraph.tosca.yaml.beans.FirewallNode;\r
+import it.polito.verigraph.tosca.yaml.beans.MailClientNode;\r
+import it.polito.verigraph.tosca.yaml.beans.MailServerNode;\r
+import it.polito.verigraph.tosca.yaml.beans.NatNode;\r
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.TopologyTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessNode;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnExitNode;\r
+import it.polito.verigraph.tosca.yaml.beans.WebClientNode;\r
+import it.polito.verigraph.tosca.yaml.beans.WebServerNode;\r
+\r
+public class YamlParsingUtils {\r
+\r
+    public static ServiceTemplateYaml obtainServiceTemplate(String filePath) throws InvalidServiceTemplateException {\r
+        ServiceTemplateYaml yamlServiceTemplate = new ServiceTemplateYaml();\r
+        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());\r
+        mapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);\r
+\r
+        try {\r
+            yamlServiceTemplate = mapper.readValue(new File(filePath), ServiceTemplateYaml.class);\r
+            return yamlServiceTemplate;\r
+\r
+        } catch (JsonParseException e) {\r
+            throw new InvalidServiceTemplateException("The NodeTemplate IDs and the RelationshipTemplate IDs must be unique.");\r
+        } catch (JsonMappingException e) {\r
+            throw new InvalidServiceTemplateException("The provided file does not match the expected structure.");\r
+        } catch (InvalidServiceTemplateException e) {\r
+            throw new InvalidServiceTemplateException("The provided template contains errors or missing informations.");\r
+        } catch (IOException e) {\r
+            throw new InvalidServiceTemplateException("I/O error.");\r
+        }\r
+\r
+    }\r
+\r
+\r
+    public static Map<String, NodeTemplateYaml> obtainNodeTemplates(ServiceTemplateYaml yamlService) throws DataNotFoundException {\r
+        TopologyTemplateYaml yamlTopology;\r
+        try {\r
+            yamlTopology = yamlService.getTopology_template();\r
+        } catch(NullPointerException ex) {\r
+            throw new DataNotFoundException("The ServiceTemplate provided does not contain a TopologyTemplate.");\r
+        }\r
+        try {\r
+            Map<String, NodeTemplateYaml> nodes = yamlTopology.getNode_templates();\r
+            return nodes;\r
+        } catch(NullPointerException ex) {\r
+            throw new DataNotFoundException("The ServiceTemplate provided does not contain any NodeTemplates.");\r
+        }\r
+\r
+    }\r
+\r
+\r
+    public static Map<String, RelationshipTemplateYaml> obtainRelationshipTemplates(ServiceTemplateYaml yamlService) throws DataNotFoundException {\r
+        TopologyTemplateYaml yamlTopology;\r
+        try {\r
+            yamlTopology = yamlService.getTopology_template();\r
+        } catch(NullPointerException ex) {\r
+            throw new DataNotFoundException("The ServiceTemplate provided does not contain a TopologyTemplate.");\r
+        }\r
+        try {\r
+            Map<String,RelationshipTemplateYaml> relats = yamlTopology.getRelationship_templates();\r
+            return relats;\r
+        } catch(NullPointerException ex) {\r
+            throw new DataNotFoundException("The ServiceTemplate provided does not contain any RelationshipTemplates.");\r
+        }\r
+\r
+    }\r
+\r
+\r
+    public static String obtainConfiguration(NodeTemplateYaml node) throws BadRequestException {\r
+        ConfigurationYaml yamlConfiguration = null;\r
+        ObjectMapper mapper = new ObjectMapper();\r
+        SimpleModule module = new SimpleModule();\r
+        module.addSerializer(ConfigurationYaml.class, new YamlConfigSerializer());\r
+        mapper.registerModule(module);\r
+\r
+        // Find out node type, retrieve the corresponding configuration and convert it properly\r
+        try {\r
+            if(node instanceof AntispamNode) {\r
+                yamlConfiguration = ((AntispamNode)node).getProperties();\r
+            }else if(node instanceof CacheNode) {\r
+                yamlConfiguration = ((CacheNode)node).getProperties();\r
+            }else if(node instanceof DpiNode) {\r
+                yamlConfiguration = ((DpiNode)node).getProperties();\r
+            }else if(node instanceof EndhostNode) {\r
+                yamlConfiguration = ((EndhostNode)node).getProperties();\r
+            }else if(node instanceof EndpointNode) {\r
+                yamlConfiguration = ((EndpointNode)node).getProperties();\r
+            }else if(node instanceof FieldModifierNode) {\r
+                yamlConfiguration = ((FieldModifierNode)node).getProperties();\r
+            }else if(node instanceof FirewallNode) {\r
+                yamlConfiguration = ((FirewallNode)node).getProperties();\r
+            }else if(node instanceof MailClientNode) {\r
+                yamlConfiguration = ((MailClientNode)node).getProperties();\r
+            }else if(node instanceof MailServerNode) {\r
+                yamlConfiguration = ((MailServerNode)node).getProperties();\r
+            }else if(node instanceof NatNode) {\r
+                yamlConfiguration = ((NatNode)node).getProperties();\r
+            }else if(node instanceof VpnAccessNode) {\r
+                yamlConfiguration = ((VpnAccessNode)node).getProperties();\r
+            }else if(node instanceof VpnExitNode) {\r
+                yamlConfiguration = ((VpnExitNode)node).getProperties();\r
+            }else if(node instanceof WebClientNode) {\r
+                yamlConfiguration = ((WebClientNode)node).getProperties();\r
+            }else if(node instanceof WebServerNode) {\r
+                yamlConfiguration = ((WebServerNode)node).getProperties();\r
+            }else {\r
+                throw new BadRequestException("The provided node is of unknown type, unable to retrieve the node configuration");\r
+            }\r
+\r
+            String stringConfiguration = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(yamlConfiguration);\r
+            if (!stringConfiguration.equals("null"))\r
+                return stringConfiguration;\r
+            else\r
+                return "[]";\r
+\r
+        } catch (JsonProcessingException | NullPointerException e) {\r
+            throw new BadRequestException("Not able to retrieve a valid configuration");\r
+        }\r
+    }\r
+\r
+}
\ No newline at end of file
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToXml.java b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToXml.java
new file mode 100644 (file)
index 0000000..002737a
--- /dev/null
@@ -0,0 +1,145 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.grpc;\r
+\r
+import java.io.IOException;\r
+import java.util.List;\r
+\r
+import javax.xml.namespace.QName;\r
+\r
+import it.polito.tosca.jaxb.Configuration;\r
+import it.polito.tosca.jaxb.Definitions;\r
+import it.polito.tosca.jaxb.TEntityTemplate.Properties;\r
+import it.polito.tosca.jaxb.TNodeTemplate;\r
+import it.polito.tosca.jaxb.TRelationshipTemplate;\r
+import it.polito.tosca.jaxb.TRelationshipTemplate.SourceElement;\r
+import it.polito.tosca.jaxb.TRelationshipTemplate.TargetElement;\r
+import it.polito.tosca.jaxb.TServiceTemplate;\r
+import it.polito.tosca.jaxb.TTopologyTemplate;\r
+import it.polito.verigraph.exception.BadRequestException;\r
+import it.polito.verigraph.grpc.NodeTemplateGrpc;\r
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;\r
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;\r
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;\r
+import it.polito.verigraph.tosca.MappingUtils;\r
+\r
+public class GrpcToXml {\r
+\r
+    public static Definitions mapGraph(TopologyTemplateGrpc topologyGrpc) {\r
+        Definitions definitions = new Definitions();\r
+        TServiceTemplate serviceTemplate = new TServiceTemplate();\r
+        TTopologyTemplate topologyTemplate = new TTopologyTemplate();\r
+\r
+        for(NodeTemplateGrpc node : topologyGrpc.getNodeTemplateList()) {\r
+            TNodeTemplate nodeTemplate = mapNode(node);\r
+            topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(nodeTemplate);\r
+        }\r
+        for(RelationshipTemplateGrpc relat : topologyGrpc.getRelationshipTemplateList()) {\r
+            TRelationshipTemplate relationshipTemplate = mapRelationship(relat, topologyGrpc.getNodeTemplateList());\r
+            topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(relationshipTemplate);\r
+        }\r
+\r
+        try {\r
+            serviceTemplate.setId(String.valueOf(topologyGrpc.getId()));\r
+        } catch (NullPointerException e) {\r
+            throw new NullPointerException("The TopologyTemplateGrpc must have an ID.");\r
+        }\r
+        serviceTemplate.setTopologyTemplate(topologyTemplate);\r
+        definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(serviceTemplate);\r
+        return definitions;\r
+    }\r
+\r
+\r
+    private static TNodeTemplate mapNode(NodeTemplateGrpc node){\r
+        TNodeTemplate nodeTemplate = new TNodeTemplate();\r
+\r
+        try {\r
+            nodeTemplate.setId(String.valueOf(node.getId()));\r
+        } catch (NullPointerException e) {\r
+            throw new NullPointerException("The NodeTemplateGrpc must have an ID.");\r
+        }\r
+        try {\r
+            nodeTemplate.setName(node.getName());\r
+        } catch (NullPointerException e) {\r
+            throw new NullPointerException("The NodeTemplateGrpc must have a name.");\r
+        }\r
+\r
+        try {\r
+            //QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12/ToscaVerigraphDefinition")\r
+            QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12",\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1) + "Type");\r
+            nodeTemplate.setType(type);\r
+        } catch (NullPointerException e) {\r
+            throw new NullPointerException("The NodeTemplateGrpc must have a valid type.");\r
+        }\r
+\r
+        Configuration config = mapModelConfiguration(node.getConfiguration(), node.getType().toString().toLowerCase());\r
+        nodeTemplate.setProperties(new Properties());\r
+        nodeTemplate.getProperties().setAny(config);\r
+        return nodeTemplate;\r
+    }\r
+\r
+\r
+    private static TRelationshipTemplate mapRelationship(RelationshipTemplateGrpc relat, List<NodeTemplateGrpc> nodeList) {\r
+        TRelationshipTemplate relationship = new TRelationshipTemplate();\r
+        SourceElement source = new SourceElement();\r
+        TargetElement target = new TargetElement();\r
+        int check = 0;\r
+\r
+        TNodeTemplate sourceNode = new TNodeTemplate();\r
+        TNodeTemplate targetNode = new TNodeTemplate();\r
+\r
+        try {\r
+            for(NodeTemplateGrpc node : nodeList) {\r
+                if(node.getId().equals(relat.getIdSourceNodeTemplate())) {\r
+                    sourceNode = mapNode(node);\r
+                    check++;\r
+                }\r
+                if(node.getId().equals(relat.getIdTargetNodeTemplate())) {\r
+                    targetNode = mapNode(node);\r
+                    check++;\r
+                }\r
+            }\r
+        } catch (NullPointerException e) {\r
+            throw new BadRequestException("A RelationshipTemplateGrpc must contain both source and target node ID.");\r
+        }\r
+        if(check != 2)\r
+            throw new BadRequestException("A RelationshipTemplateGrpc must contain both source and target node ID.");\r
+\r
+        source.setRef(sourceNode);\r
+        target.setRef(targetNode);\r
+\r
+        relationship.setId(relat.getId()); //TODO da valutare\r
+        relationship.setSourceElement(source);\r
+        relationship.setTargetElement(target);\r
+        relationship.setName(sourceNode.getName()+"To"+targetNode.getName());\r
+\r
+        return relationship;\r
+    }\r
+\r
+\r
+    private static it.polito.tosca.jaxb.Configuration mapModelConfiguration(ToscaConfigurationGrpc toscaConfigurationGrpc, String type) {\r
+        it.polito.tosca.jaxb.Configuration configuration = new it.polito.tosca.jaxb.Configuration();\r
+        try {\r
+            //We are passing the configuration type to the Deserializer context\r
+            configuration = MappingUtils.obtainToscaConfiguration(toscaConfigurationGrpc, type);\r
+\r
+            //In Graph, ID and DESCRIPTION are always empty\r
+            //configuration.setConfID(confGrpc.getId());\r
+            //configuration.setConfDescr(confGrpc.getDescription());\r
+\r
+        } catch (IOException | NullPointerException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return configuration;\r
+    }\r
+\r
+}\r
+\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToYaml.java b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToYaml.java
new file mode 100644 (file)
index 0000000..64f8ae4
--- /dev/null
@@ -0,0 +1,284 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.grpc;\r
+\r
+import java.io.IOException;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+\r
+import com.fasterxml.jackson.core.JsonParseException;\r
+import com.fasterxml.jackson.databind.InjectableValues;\r
+import com.fasterxml.jackson.databind.JsonMappingException;\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.databind.module.SimpleModule;\r
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;\r
+\r
+import it.polito.neo4j.jaxb.FunctionalTypes;\r
+import it.polito.verigraph.exception.BadRequestException;\r
+import it.polito.verigraph.grpc.NodeTemplateGrpc;\r
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;\r
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;\r
+import it.polito.verigraph.tosca.deserializer.YamlConfigurationDeserializer;\r
+import it.polito.verigraph.tosca.yaml.beans.AntispamConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.AntispamNode;\r
+import it.polito.verigraph.tosca.yaml.beans.CacheConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.CacheNode;\r
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.DpiConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.DpiNode;\r
+import it.polito.verigraph.tosca.yaml.beans.EndhostConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndhostNode;\r
+import it.polito.verigraph.tosca.yaml.beans.EndpointConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndpointNode;\r
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierNode;\r
+import it.polito.verigraph.tosca.yaml.beans.FirewallConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FirewallNode;\r
+import it.polito.verigraph.tosca.yaml.beans.MailClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailClientNode;\r
+import it.polito.verigraph.tosca.yaml.beans.MailServerConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailServerNode;\r
+import it.polito.verigraph.tosca.yaml.beans.NatConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.NatNode;\r
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.TopologyTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessNode;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnExitConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnExitNode;\r
+import it.polito.verigraph.tosca.yaml.beans.WebClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebClientNode;\r
+import it.polito.verigraph.tosca.yaml.beans.WebServerConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebServerNode;\r
+\r
+public class GrpcToYaml {\r
+    public static ServiceTemplateYaml mapGraphYaml(TopologyTemplateGrpc topologyGrpc) {\r
+        ServiceTemplateYaml serviceYaml = new ServiceTemplateYaml();\r
+        TopologyTemplateYaml topologyYaml = new TopologyTemplateYaml();\r
+\r
+        topologyYaml.setNode_templates(new HashMap<String,NodeTemplateYaml>());\r
+        topologyYaml.setRelationship_templates(new HashMap<String,RelationshipTemplateYaml>());\r
+        serviceYaml.setMetadata(new HashMap<String,String>());\r
+\r
+        for(NodeTemplateGrpc node : topologyGrpc.getNodeTemplateList()) {\r
+            NodeTemplateYaml nodeTemplate = new NodeTemplateYaml();\r
+            try {\r
+                nodeTemplate = mapNodeYaml(node);\r
+            } catch (IOException e) {\r
+                throw new BadRequestException("Error while mapping a Node in Yaml object.");\r
+            }\r
+            topologyYaml.getNode_templates().put(String.valueOf(node.getId()), nodeTemplate);\r
+            //shall we catch NumberFormatException?\r
+        }\r
+        for(RelationshipTemplateGrpc relationship : topologyGrpc.getRelationshipTemplateList()) {\r
+            RelationshipTemplateYaml rel = mapRelationshipYaml(relationship, topologyGrpc.getNodeTemplateList());\r
+            topologyYaml.getRelationship_templates().put(String.valueOf(relationship.getId()), rel);\r
+        }\r
+\r
+        serviceYaml.getMetadata().put("template_id", String.valueOf(topologyGrpc.getId()));\r
+        serviceYaml.setTopology_template(topologyYaml);\r
+        return serviceYaml;\r
+    }\r
+\r
+\r
+    private static NodeTemplateYaml mapNodeYaml(NodeTemplateGrpc node) throws JsonParseException, JsonMappingException, IOException {\r
+\r
+        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());\r
+        SimpleModule module = new SimpleModule();\r
+        String stringConfig = null;\r
+        try {\r
+            stringConfig = node.getConfiguration().getConfiguration();\r
+        } catch (NullPointerException e) {\r
+            throw new NullPointerException("A NodeTemplateGrpc does not contain the configuration.");\r
+        }\r
+        JsonNode nodeConfig = mapper.readTree(stringConfig);\r
+        //Passing the configuration type to the Deserializer context\r
+        module.addDeserializer(ConfigurationYaml.class, new YamlConfigurationDeserializer());\r
+        mapper.registerModule(module);\r
+\r
+        if(node.getType() == null)\r
+            throw new NullPointerException("A NodeTemplateGrpc does not contain a type.");\r
+        ConfigurationYaml yamlConfig = mapper\r
+                .reader(new InjectableValues.Std().addValue("type", node.getType().toString().toLowerCase()))\r
+                .forType(ConfigurationYaml.class)\r
+                .readValue(nodeConfig);\r
+\r
+\r
+        FunctionalTypes nodeType = FunctionalTypes.valueOf(node.getType().toString().toUpperCase());\r
+        switch(nodeType) {\r
+        case ANTISPAM:\r
+            AntispamNode antispamNode = new AntispamNode();\r
+            antispamNode.setName(node.getName());\r
+            antispamNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            antispamNode.setProperties((AntispamConfigurationYaml) yamlConfig);\r
+            return antispamNode;\r
+\r
+        case CACHE:\r
+            CacheNode cacheNode = new CacheNode();\r
+            cacheNode.setName(node.getName());\r
+            cacheNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            cacheNode.setProperties((CacheConfigurationYaml) yamlConfig);\r
+            return cacheNode;\r
+\r
+        case DPI:\r
+            DpiNode dpiNode = new DpiNode();\r
+            dpiNode.setName(node.getName());\r
+            dpiNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            dpiNode.setProperties((DpiConfigurationYaml) yamlConfig);\r
+            return dpiNode;\r
+\r
+        case ENDHOST:\r
+            EndhostNode endhostNode = new EndhostNode();\r
+            endhostNode.setName(node.getName());\r
+            endhostNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            endhostNode.setProperties((EndhostConfigurationYaml) yamlConfig);\r
+            return endhostNode;\r
+\r
+        case ENDPOINT:\r
+            EndpointNode endpointNode = new EndpointNode();\r
+            endpointNode.setName(node.getName());\r
+            endpointNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            endpointNode.setProperties((EndpointConfigurationYaml) yamlConfig);\r
+            return endpointNode;\r
+\r
+        case FIELDMODIFIER:\r
+            FieldModifierNode fieldmodifierNode = new FieldModifierNode();\r
+            fieldmodifierNode.setName(node.getName());\r
+            fieldmodifierNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            fieldmodifierNode.setProperties((FieldModifierConfigurationYaml) yamlConfig);\r
+            return fieldmodifierNode;\r
+\r
+        case FIREWALL:\r
+            FirewallNode firewallNode = new FirewallNode();\r
+            firewallNode.setName(node.getName());\r
+            firewallNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            firewallNode.setProperties((FirewallConfigurationYaml) yamlConfig);\r
+            return firewallNode;\r
+\r
+        case MAILCLIENT:\r
+            MailClientNode mailclientNode = new MailClientNode();\r
+            mailclientNode.setName(node.getName());\r
+            mailclientNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            mailclientNode.setProperties((MailClientConfigurationYaml) yamlConfig);\r
+            return mailclientNode;\r
+\r
+        case MAILSERVER:\r
+            MailServerNode mailserverNode = new MailServerNode();\r
+            mailserverNode.setName(node.getName());\r
+            mailserverNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            mailserverNode.setProperties((MailServerConfigurationYaml) yamlConfig);\r
+            return mailserverNode;\r
+\r
+        case NAT:\r
+            NatNode natNode = new NatNode();\r
+            natNode.setName(node.getName());\r
+            natNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            natNode.setProperties((NatConfigurationYaml) yamlConfig);\r
+            return natNode;\r
+\r
+        case VPNACCESS:\r
+            VpnAccessNode vpnaccessNode = new VpnAccessNode();\r
+            vpnaccessNode.setName(node.getName());\r
+            vpnaccessNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            vpnaccessNode.setProperties((VpnAccessConfigurationYaml) yamlConfig);\r
+            return vpnaccessNode;\r
+\r
+        case VPNEXIT:\r
+            VpnExitNode vpnexitNode = new VpnExitNode();\r
+            vpnexitNode.setName(node.getName());\r
+            vpnexitNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            vpnexitNode.setProperties((VpnExitConfigurationYaml) yamlConfig);\r
+            return vpnexitNode;\r
+\r
+        case WEBCLIENT:\r
+            WebClientNode webclientNode = new WebClientNode();\r
+            webclientNode.setName(node.getName());\r
+            webclientNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            webclientNode.setProperties((WebClientConfigurationYaml) yamlConfig);\r
+            return webclientNode;\r
+\r
+        case WEBSERVER:\r
+            WebServerNode webserverNode = new WebServerNode();\r
+            webserverNode.setName(node.getName());\r
+            webserverNode.setType("verigraph.nodeTypes." +\r
+                    node.getType().toString().substring(0, 1).toUpperCase() +\r
+                    node.getType().toString().substring(1));\r
+            webserverNode.setProperties((WebServerConfigurationYaml) yamlConfig);\r
+            return webserverNode;\r
+\r
+        default:\r
+            FieldModifierNode defaultNode = new FieldModifierNode();\r
+            defaultNode.setName(node.getName());\r
+            defaultNode.setType("verigraph.nodeTypes.Fieldmodifier");\r
+            defaultNode.setProperties(new FieldModifierConfigurationYaml());\r
+            return defaultNode;\r
+        }\r
+\r
+    }\r
+\r
+    private static RelationshipTemplateYaml mapRelationshipYaml(RelationshipTemplateGrpc relat, List<NodeTemplateGrpc> nodeList) {\r
+        RelationshipTemplateYaml relationship = new RelationshipTemplateYaml();\r
+        relationship.setProperties(new HashMap<String,String>());\r
+        String sourceNode = null;\r
+        String targetNode = null;\r
+        int check = 0;\r
+\r
+        relationship.setType("verigraph.relationshipType.generic");\r
+        relationship.getProperties().put("source_id", String.valueOf(relat.getIdSourceNodeTemplate())); //to be catched?\r
+        relationship.getProperties().put("target_id", String.valueOf(relat.getIdTargetNodeTemplate()));\r
+\r
+        for(NodeTemplateGrpc node : nodeList) {\r
+            if(node.getId().equals(relat.getIdSourceNodeTemplate())) {\r
+                sourceNode = node.getName();\r
+                check++;\r
+            }\r
+            if(node.getId().equals(relat.getIdTargetNodeTemplate())) {\r
+                targetNode = node.getName();\r
+                check++;\r
+            }\r
+        }\r
+\r
+        if(check!=2)\r
+            throw new BadRequestException("A RelationshipTemplateGrpc must contain both source and target node ID.");\r
+\r
+        relationship.getProperties().put("name", sourceNode+"To"+targetNode);\r
+\r
+        return relationship;\r
+    }\r
+}\r
+\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/grpc/ToscaGrpcUtils.java b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/ToscaGrpcUtils.java
new file mode 100644 (file)
index 0000000..e43ef21
--- /dev/null
@@ -0,0 +1,18 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.grpc;\r
+\r
+public class ToscaGrpcUtils {\r
+\r
+    /** Default configuration for a Tosca NodeTemplate non compliant with Verigraph types*/\r
+    public static final String defaultConfID = new String("");\r
+    public static final String defaultDescr = new String("Default Configuration");\r
+    public static final String defaultConfig = new String("[]");\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/grpc/XmlToGrpc.java b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/XmlToGrpc.java
new file mode 100644 (file)
index 0000000..426bb4a
--- /dev/null
@@ -0,0 +1,160 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.grpc;\r
+\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import javax.xml.bind.JAXBException;\r
+\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+\r
+import it.polito.tosca.jaxb.TNodeTemplate;\r
+import it.polito.tosca.jaxb.TRelationshipTemplate;\r
+import it.polito.tosca.jaxb.TServiceTemplate;\r
+import it.polito.verigraph.exception.BadRequestException;\r
+import it.polito.verigraph.exception.DataNotFoundException;\r
+import it.polito.verigraph.grpc.NodeTemplateGrpc;\r
+import it.polito.verigraph.grpc.NodeTemplateGrpc.Type;\r
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;\r
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;\r
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;\r
+import it.polito.verigraph.tosca.MappingUtils;\r
+import it.polito.verigraph.tosca.XmlParsingUtils;\r
+\r
+public class XmlToGrpc {\r
+\r
+    /** Returns the (first) TopologyTemplate found in the TOSCA-compliant XML file */\r
+    public static TopologyTemplateGrpc obtainTopologyTemplateGrpc (String filepath)\r
+            throws IOException, JAXBException, DataNotFoundException, ClassCastException, BadRequestException{\r
+        List<TServiceTemplate> serviceTList = XmlParsingUtils.obtainServiceTemplates(filepath);\r
+        TServiceTemplate serviceTemplate = serviceTList.get(0); //obtain only the first ServiceTemplate of the TOSCA compliance file\r
+\r
+        //Retrieving of list of NodeTemplate and RelationshipTemplate\r
+        List<NodeTemplateGrpc> nodes = new ArrayList<NodeTemplateGrpc>();\r
+        List<RelationshipTemplateGrpc> relats = new ArrayList<RelationshipTemplateGrpc>();\r
+        List<TNodeTemplate> tNodes = XmlParsingUtils.obtainNodeTemplates(serviceTemplate);\r
+        for(TNodeTemplate nt : tNodes) {\r
+            for(NodeTemplateGrpc alreadyAddedNode : nodes)\r
+                if(alreadyAddedNode.getId().equals(nt.getId()))\r
+                    throw new BadRequestException("The NodeTemplate ID must be unique.");\r
+            nodes.add(parseNodeTemplate(nt));\r
+        }\r
+        for(TRelationshipTemplate rt : XmlParsingUtils.obtainRelationshipTemplates(serviceTemplate)) {\r
+            if(!tNodes.contains(rt.getSourceElement().getRef()) || !tNodes.contains(rt.getTargetElement().getRef()))\r
+                throw new BadRequestException("Invalid references to a Node in a Relationship.");\r
+            if(rt.getSourceElement().getRef() == rt.getTargetElement().getRef())\r
+                throw new BadRequestException("Source and Target cannot be equal in a Relationship.");\r
+            relats.add(parseRelationshipTemplate(rt));\r
+        }\r
+\r
+        //Creating TopologyTemplateGrpc object to be sent to server\r
+        return TopologyTemplateGrpc.newBuilder()\r
+                .setId("0") //useless value since the server chooses the actual value for the GraphID\r
+                .addAllNodeTemplate(nodes)\r
+                .addAllRelationshipTemplate(relats)\r
+                .build();\r
+    }\r
+\r
+\r
+    /** Parsing method: TNodeTemplate(tosca) --> NodeTemplateGrpc */\r
+    private static NodeTemplateGrpc parseNodeTemplate(TNodeTemplate nodeTempl)\r
+            throws ClassCastException, NullPointerException {\r
+        Boolean isVerigraphCompl = true;\r
+        Type type;\r
+\r
+        //NodeTemplateGrpc building\r
+        NodeTemplateGrpc.Builder nodegrpc = NodeTemplateGrpc.newBuilder();\r
+\r
+        //ID cannot be null\r
+        try {\r
+            nodegrpc.setId(nodeTempl.getId());\r
+        } catch (NullPointerException ex) {\r
+            throw new NullPointerException("An ID must be specified for each Node");\r
+        }\r
+        //Name can be null\r
+        try {\r
+            nodegrpc.setName(nodeTempl.getName());\r
+        } catch (NullPointerException ex) {\r
+            nodegrpc.setName("");\r
+        }\r
+\r
+        //Type cannot be null but it can be invalid\r
+        try {\r
+            String typestring = nodeTempl.getType().getLocalPart().toLowerCase();\r
+            type = Type.valueOf(nodeTempl.getType().getLocalPart().toLowerCase().substring(0,typestring.length()-4));\r
+        } catch (IllegalArgumentException | NullPointerException ex) {\r
+            //in case the NodeTemplate is not TOSCA-Verigraph compliant, we assume it to be a fieldmodifier node\r
+            type = Type.fieldmodifier;\r
+            isVerigraphCompl = false;\r
+        }\r
+        nodegrpc.setType(type);\r
+        ToscaConfigurationGrpc.Builder grpcConfig;\r
+        if(isVerigraphCompl) {\r
+            it.polito.tosca.jaxb.Configuration nodeConfig = XmlParsingUtils.obtainConfiguration(nodeTempl);\r
+            grpcConfig = ToscaConfigurationGrpc.newBuilder();\r
+            //These fields are optional in TOSCA xml\r
+            try {\r
+                grpcConfig.setId(nodeConfig.getConfID());\r
+            } catch(NullPointerException ex) {\r
+                grpcConfig.setId(ToscaGrpcUtils.defaultConfID);\r
+            }\r
+            try {\r
+                grpcConfig.setDescription(nodeConfig.getConfDescr());\r
+            } catch(NullPointerException ex) {\r
+                grpcConfig.setDescription(ToscaGrpcUtils.defaultDescr);\r
+            }\r
+            try {;\r
+            grpcConfig.setConfiguration(MappingUtils.obtainStringConfiguration(nodeConfig));\r
+            } catch(NullPointerException | JsonProcessingException ex) {\r
+                grpcConfig.setConfiguration(ToscaGrpcUtils.defaultConfig);\r
+            }\r
+        }\r
+        else {\r
+            grpcConfig = ToscaConfigurationGrpc.newBuilder()\r
+                    .setId(ToscaGrpcUtils.defaultConfID)\r
+                    .setDescription(ToscaGrpcUtils.defaultDescr)\r
+                    .setConfiguration(ToscaGrpcUtils.defaultConfig);\r
+        }\r
+        nodegrpc.setConfiguration(grpcConfig.build());\r
+        return nodegrpc.build();\r
+    }\r
+\r
+\r
+    /** Parsing method: TRelationshipTemplate(tosca) --> RelationshipTemplateGrpc */\r
+    private static RelationshipTemplateGrpc parseRelationshipTemplate(TRelationshipTemplate relatTempl)\r
+            throws ClassCastException{\r
+        String source, target;\r
+        //RelationshipTemplateGrpc building\r
+        RelationshipTemplateGrpc.Builder relatgrpc = RelationshipTemplateGrpc.newBuilder();\r
+\r
+        //ID and Name can be null\r
+        try {\r
+            relatgrpc.setId(relatTempl.getId());\r
+        } catch (NullPointerException ex) {}//Different Relationship with same ID are considered valid.\r
+        try {\r
+            relatgrpc.setName(relatTempl.getName());\r
+        } catch (NullPointerException ex) {}\r
+\r
+        //Source and Target values cannot be null\r
+        try {\r
+            TNodeTemplate sourceNode = (TNodeTemplate) relatTempl.getSourceElement().getRef();\r
+            TNodeTemplate targetNode = (TNodeTemplate) relatTempl.getTargetElement().getRef();\r
+            source = sourceNode.getId();\r
+            target = targetNode.getId();\r
+        } catch (NullPointerException ex) {\r
+            throw new NullPointerException("Invalid NodeTemplate reference in RelationshipTemplate with id:"\r
+                    + relatTempl.getId());\r
+        }\r
+        relatgrpc.setIdSourceNodeTemplate(source)\r
+        .setIdTargetNodeTemplate(target);\r
+        return relatgrpc.build();\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/grpc/YamlToGrpc.java b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/YamlToGrpc.java
new file mode 100644 (file)
index 0000000..1a713bd
--- /dev/null
@@ -0,0 +1,147 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.grpc;\r
+\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Map.Entry;\r
+\r
+import javax.xml.bind.JAXBException;\r
+\r
+import it.polito.verigraph.exception.BadRequestException;\r
+import it.polito.verigraph.exception.DataNotFoundException;\r
+import it.polito.verigraph.grpc.NodeTemplateGrpc;\r
+import it.polito.verigraph.grpc.NodeTemplateGrpc.Type;\r
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;\r
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;\r
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;\r
+import it.polito.verigraph.tosca.YamlParsingUtils;\r
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;\r
+\r
+public class YamlToGrpc {\r
+\r
+    /** Returns the (first) TopologyTemplate found in the TOSCA-compliant yaml file */\r
+    public static TopologyTemplateGrpc obtainTopologyTemplateGrpc (String filepath)\r
+            throws IOException, JAXBException, DataNotFoundException, ClassCastException, BadRequestException{\r
+        ServiceTemplateYaml serviceTemplate = YamlParsingUtils.obtainServiceTemplate(filepath);\r
+\r
+        //Retrieving of list of NodeTemplate and RelationshipTemplate\r
+        List<NodeTemplateGrpc> nodes = new ArrayList<NodeTemplateGrpc>();\r
+        List<RelationshipTemplateGrpc> relats = new ArrayList<RelationshipTemplateGrpc>();\r
+\r
+        try {\r
+            for(Map.Entry<String, NodeTemplateYaml> node : YamlParsingUtils.obtainNodeTemplates(serviceTemplate).entrySet()) {\r
+                for(NodeTemplateGrpc alreadyAddedNode : nodes)\r
+                    if(alreadyAddedNode.getId().equals(node.getKey()))\r
+                        throw new BadRequestException("The NodeTemplate ID must be unique.");\r
+                nodes.add(parseNodeTemplate(node));\r
+            }\r
+        } catch (NullPointerException e) {\r
+            throw new BadRequestException("There is not any NodeTemplate in the ServiceTemplate provided.");\r
+        }\r
+\r
+        try {\r
+            for(Map.Entry<String, RelationshipTemplateYaml> rel : YamlParsingUtils.obtainRelationshipTemplates(serviceTemplate).entrySet()) {\r
+                relats.add(parseRelationshipTemplate(rel, nodes));\r
+            }\r
+        } catch (NullPointerException e) {\r
+            throw new BadRequestException("There is not any RelationshipTemplate in the ServiceTemplate provided.");\r
+        }\r
+\r
+        //Creating TopologyTemplateGrpc object to be sent to server\r
+        return TopologyTemplateGrpc.newBuilder()\r
+                .setId("0") //useless value since the server chooses the actual value for the GraphID\r
+                .addAllNodeTemplate(nodes)\r
+                .addAllRelationshipTemplate(relats)\r
+                .build();\r
+    }\r
+\r
+    /** Parsing method: RelationshipTemplateYaml(tosca) --> RelationshipTemplateGrpc */\r
+    private static RelationshipTemplateGrpc parseRelationshipTemplate(Entry<String, RelationshipTemplateYaml> rel, List<NodeTemplateGrpc> nodes) throws BadRequestException{\r
+        String source, target;\r
+        boolean valid_source = false;\r
+        boolean valid_target = false;\r
+\r
+        //RelationshipTemplateGrpc building\r
+        RelationshipTemplateGrpc.Builder relatgrpc = RelationshipTemplateGrpc.newBuilder();  \r
+\r
+        //ID can be null\r
+        try {\r
+            relatgrpc.setId(rel.getKey());\r
+            relatgrpc.setName(rel.getValue().getProperties().get("name"));\r
+            source = rel.getValue().getProperties().get("source_id");\r
+            target = rel.getValue().getProperties().get("target_id");\r
+        } catch (NullPointerException ex) {\r
+            throw new BadRequestException("Incorrect fields in RelationshipTemplate:" + rel.getKey());\r
+        }\r
+\r
+        //Source and Target values must correctly refer to a NodeTemplate\r
+        if(source.equals(target))\r
+            throw new BadRequestException("Source and Target cannot be the same value");\r
+        for(NodeTemplateGrpc node : nodes) {\r
+            if(node.getId().equals(source))\r
+                valid_source = true;\r
+            if(node.getId().equals(target))\r
+                valid_target = true;\r
+        }\r
+        if(!(valid_source && valid_target))\r
+            throw new BadRequestException("Invalid NodeTemplate reference in RelationshipTemplate:" + rel.getKey());\r
+\r
+        return relatgrpc.setIdSourceNodeTemplate(source).setIdTargetNodeTemplate(target).build();\r
+\r
+    }\r
+\r
+    /** Parsing method: NodeTemplateYaml(tosca) --> NodeTemplateGrpc */\r
+    private static NodeTemplateGrpc parseNodeTemplate(Entry<String, NodeTemplateYaml> node)\r
+            throws ClassCastException, NullPointerException, BadRequestException {   \r
+        Boolean isVerigraphCompl = true;\r
+        Type type;\r
+\r
+        //NodeTemplateGrpc building\r
+        NodeTemplateGrpc.Builder nodegrpc = NodeTemplateGrpc.newBuilder()\r
+                .setId(node.getKey());\r
+\r
+        try {\r
+            nodegrpc.setName(node.getValue().getName());\r
+        } catch (NullPointerException ex) {\r
+            throw new BadRequestException("Invalid name in a NodeTemplate.");\r
+        }\r
+\r
+        //Type cannot be null but it can be invalid\r
+        try {\r
+            type = Type.valueOf(node.getValue().getType().replace("verigraph.nodeTypes.", "").toLowerCase());\r
+        } catch (IllegalArgumentException | NullPointerException ex) {\r
+            //in case the NodeTemplate is not TOSCA-Verigraph compliant, we assume it to be a fieldmodifier node\r
+            type = Type.fieldmodifier;\r
+            isVerigraphCompl = false;\r
+        }\r
+        nodegrpc.setType(type);\r
+        ToscaConfigurationGrpc.Builder grpcConfig;   \r
+        if(isVerigraphCompl) {\r
+            String jsonConfig = YamlParsingUtils.obtainConfiguration(node.getValue());\r
+            grpcConfig = ToscaConfigurationGrpc.newBuilder()\r
+                    .setId("")\r
+                    .setDescription("")\r
+                    .setConfiguration(jsonConfig);\r
+        }\r
+        else {\r
+            grpcConfig = ToscaConfigurationGrpc.newBuilder()\r
+                    .setId(ToscaGrpcUtils.defaultConfID)\r
+                    .setDescription(ToscaGrpcUtils.defaultDescr)\r
+                    .setConfiguration(ToscaGrpcUtils.defaultConfig);\r
+        }   \r
+        nodegrpc.setConfiguration(grpcConfig.build());\r
+        return nodegrpc.build();\r
+    }\r
+\r
+}
\ No newline at end of file
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/xml/GraphToXml.java b/verigraph/src/it/polito/verigraph/tosca/converter/xml/GraphToXml.java
new file mode 100644 (file)
index 0000000..613588f
--- /dev/null
@@ -0,0 +1,134 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.xml;\r
+\r
+import java.io.IOException;\r
+import java.util.Map;\r
+\r
+import javax.xml.namespace.QName;\r
+\r
+import it.polito.verigraph.model.Configuration;\r
+import it.polito.verigraph.model.Graph;\r
+import it.polito.verigraph.model.Neighbour;\r
+import it.polito.verigraph.model.Node;\r
+import it.polito.verigraph.tosca.MappingUtils;\r
+import it.polito.tosca.jaxb.Definitions;\r
+import it.polito.tosca.jaxb.TEntityTemplate.Properties;\r
+import it.polito.tosca.jaxb.TNodeTemplate;\r
+import it.polito.tosca.jaxb.TRelationshipTemplate;\r
+import it.polito.tosca.jaxb.TRelationshipTemplate.SourceElement;\r
+import it.polito.tosca.jaxb.TRelationshipTemplate.TargetElement;\r
+import it.polito.tosca.jaxb.TServiceTemplate;\r
+import it.polito.tosca.jaxb.TTopologyTemplate;\r
+\r
+public class GraphToXml {\r
+    /** model --> tosca_xml*/\r
+\r
+    public static Definitions mapGraph(Graph graph) {\r
+        Definitions definitions = new Definitions();\r
+        TServiceTemplate serviceTemplate = mapPathToXml(graph);\r
+        definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(serviceTemplate);\r
+\r
+        return definitions;\r
+    }\r
+\r
+    // These functions have been split so that they can be reused for obtaining all the paths\r
+    //into a single Definitions (see mapPathsToXml)\r
+    public static TServiceTemplate mapPathToXml(Graph graph) {\r
+\r
+        TServiceTemplate serviceTemplate = new TServiceTemplate();\r
+        TTopologyTemplate topologyTemplate = new TTopologyTemplate();\r
+\r
+        for(Node node : graph.getNodes().values()) {\r
+            long i = 0;\r
+            TNodeTemplate nodeTemplate = mapNode(node);\r
+            topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(nodeTemplate);\r
+\r
+            // RelationshipTemplate mapping\r
+            Map<Long,Neighbour> neighMap = node.getNeighbours();\r
+            for (Map.Entry<Long, Neighbour> myentry : neighMap.entrySet()) {\r
+                Neighbour neigh = myentry.getValue();\r
+                if (graph.getNodes().containsKey(neigh.getId())) {\r
+                    // I have to check that because if I'm mapping a path (and not a graph) I could have\r
+                    //as neighbour a node which is not in the path\r
+                    TRelationshipTemplate relat = mapRelationship(graph, node, neigh, i);\r
+                    topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(relat);\r
+                    i++; //Neighbour does not have a neighbourID! RelationshipTemplate does,\r
+                    //so it is an incremental number for each node\r
+                }\r
+            }\r
+        }\r
+\r
+        serviceTemplate.setId(String.valueOf(graph.getId()));\r
+        serviceTemplate.setTopologyTemplate(topologyTemplate);\r
+\r
+        return serviceTemplate;\r
+    }\r
+\r
+\r
+    private static TNodeTemplate mapNode(Node node){\r
+        TNodeTemplate nodeTemplate = new TNodeTemplate();\r
+\r
+        nodeTemplate.setId(String.valueOf(node.getId()));\r
+        nodeTemplate.setName(node.getName());\r
+\r
+        //QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12/ToscaVerigraphDefinition")\r
+        QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12",\r
+                node.getFunctional_type().substring(0, 1).toUpperCase() + node.\r
+                getFunctional_type().substring(1) + "Type");\r
+        nodeTemplate.setType(type);\r
+\r
+        it.polito.tosca.jaxb.Configuration config = mapModelConfiguration(node.getConfiguration(),\r
+                node.getFunctional_type().toLowerCase());\r
+        //nodeTemplate.getAny().add(config);\r
+        nodeTemplate.setProperties(new Properties());\r
+        nodeTemplate.getProperties().setAny(config);\r
+        return nodeTemplate;\r
+    }\r
+\r
+\r
+    private static TRelationshipTemplate mapRelationship(Graph graph, Node sourceNode, Neighbour neigh, long i) {\r
+        TRelationshipTemplate relationship = new TRelationshipTemplate();\r
+        SourceElement source = new SourceElement();\r
+        TargetElement target = new TargetElement();\r
+\r
+        Node targetNode = graph.getNodes().get(neigh.getId());\r
+\r
+        TNodeTemplate sourceNT = mapNode(sourceNode);\r
+        TNodeTemplate targetNT = mapNode(targetNode);\r
+\r
+        source.setRef(sourceNT);\r
+        target.setRef(targetNT);\r
+\r
+        relationship.setId(String.valueOf(i));\r
+        relationship.setSourceElement(source);\r
+        relationship.setTargetElement(target);\r
+        relationship.setName(sourceNode.getName()+"to"+neigh.getName());\r
+\r
+        return relationship;\r
+    }\r
+\r
+\r
+    private static it.polito.tosca.jaxb.Configuration mapModelConfiguration(Configuration conf, String type) {\r
+        it.polito.tosca.jaxb.Configuration configuration = new it.polito.tosca.jaxb.Configuration();\r
+        try {\r
+            //We are passing the configuration type to the Deserializer context\r
+            configuration = MappingUtils.obtainToscaConfiguration(conf, type);\r
+\r
+            //In Graph, ID and DESCRIPTION are always empty\r
+            //configuration.setConfID(conf.getId());\r
+            //configuration.setConfDescr(conf.getDescription());\r
+\r
+        } catch (IOException | NullPointerException e) {\r
+            e.printStackTrace();\r
+        }\r
+        return configuration;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/xml/XmlToGraph.java b/verigraph/src/it/polito/verigraph/tosca/converter/xml/XmlToGraph.java
new file mode 100644 (file)
index 0000000..3744ba2
--- /dev/null
@@ -0,0 +1,167 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.xml;\r
+\r
+import java.io.IOException;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.stream.Collectors;\r
+\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+\r
+import it.polito.verigraph.exception.BadRequestException;\r
+import it.polito.verigraph.exception.DataNotFoundException;\r
+import it.polito.verigraph.model.Configuration;\r
+import it.polito.verigraph.model.Graph;\r
+import it.polito.verigraph.model.Neighbour;\r
+import it.polito.verigraph.model.Node;\r
+import it.polito.verigraph.tosca.MappingUtils;\r
+import it.polito.verigraph.tosca.XmlParsingUtils;\r
+import it.polito.tosca.jaxb.Definitions;\r
+import it.polito.tosca.jaxb.TExtensibleElements;\r
+import it.polito.tosca.jaxb.TNodeTemplate;\r
+import it.polito.tosca.jaxb.TRelationshipTemplate;\r
+import it.polito.tosca.jaxb.TServiceTemplate;\r
+\r
+public class XmlToGraph {\r
+    public static Graph mapTopologyTemplate(Definitions definitions) throws DataNotFoundException, BadRequestException {\r
+        Graph graph = new Graph();\r
+        Map<Long, Node> nodes = new HashMap<>();\r
+\r
+        List<TExtensibleElements> elements = definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation();\r
+\r
+        // Retrieve the list of ServiceTemplate in Definitions\r
+        List<TServiceTemplate> serviceTemplates = elements.stream().filter(p -> p instanceof TServiceTemplate)\r
+                .map(obj -> (TServiceTemplate) obj).collect(Collectors.toList());\r
+        if (serviceTemplates.isEmpty())\r
+            throw new DataNotFoundException("There is no ServiceTemplate into the Definitions object");\r
+\r
+        List<TNodeTemplate> nodeTemplates = XmlParsingUtils.obtainNodeTemplates(serviceTemplates.get(0));\r
+\r
+        for (TNodeTemplate nodeTemplate : nodeTemplates) {\r
+            Node node = mapNodeTemplate(nodeTemplate);\r
+            nodes.put(Long.valueOf(nodeTemplate.getId()), node);\r
+        }\r
+\r
+        // Add Neighbour to the Node of the list\r
+        List<TRelationshipTemplate> relationshipTemplates = XmlParsingUtils.obtainRelationshipTemplates(serviceTemplates.get(0));\r
+        mapRelationshipTemplates(nodes, relationshipTemplates);\r
+\r
+        // Add Nodes and ID to the graph\r
+        graph.setNodes(nodes);\r
+\r
+        try {\r
+            graph.setId(Long.valueOf(serviceTemplates.get(0).getId()));\r
+        } catch (NumberFormatException ex) {\r
+            throw new BadRequestException("If you want to store your TopologyTemplate on this server, "\r
+                    + "the TopologyTemplate ID must be a number.");\r
+        }\r
+\r
+        return graph;\r
+    }\r
+\r
+\r
+    private static Node mapNodeTemplate(TNodeTemplate nodeTemplate) {\r
+        Node node = new Node();\r
+\r
+        String toscaType =  nodeTemplate.getType().toString();\r
+        toscaType = toscaType.replace("Type", "").replace("{http://docs.oasis-open.org/tosca/ns/2011/12}", "");\r
+        toscaType = toscaType.toLowerCase();\r
+\r
+        try {\r
+            node.setId(Long.valueOf(nodeTemplate.getId()));\r
+        } catch(NumberFormatException ex) {\r
+            throw new BadRequestException("The NodeTemplate ID must be a number.");\r
+        }\r
+        try {\r
+            node.setName(nodeTemplate.getName());\r
+            Configuration conf = mapToscaConfiguration(XmlParsingUtils.obtainConfiguration(nodeTemplate));\r
+            node.setConfiguration(conf);\r
+            node.setFunctional_type(toscaType);\r
+        } catch(NullPointerException | IOException ex) {\r
+            throw new BadRequestException("The NodeTemplate id:"+node.getId()+" has wrong fields representation.");\r
+        }\r
+        return node;\r
+    }\r
+\r
+\r
+    private static void mapRelationshipTemplates(Map<Long, Node> nodes, List<TRelationshipTemplate> relationshipTemplates) {\r
+        //update of nodes... (update = Node + its Neighbours)\r
+        for(TRelationshipTemplate relationshipTemplate : relationshipTemplates) {\r
+            if (relationshipTemplate != null) {\r
+                try {\r
+                    if(relationshipTemplate.getSourceElement().getRef() == relationshipTemplate.getTargetElement().getRef())\r
+                        throw new BadRequestException("Source and Target cannot be equal in a Relationship.");\r
+\r
+                    // Retrieve the target Node name and generate a new Neighbour\r
+                    TNodeTemplate targetNodeTemplate = (TNodeTemplate) relationshipTemplate.getTargetElement().getRef();\r
+                    String neighName = nodes.get(Long.valueOf(targetNodeTemplate.getId())).getName();\r
+                    //this manages invalid/inexistent node ID for target node\r
+                    Neighbour neigh = new Neighbour();\r
+                    neigh.setName(neighName);\r
+                    neigh.setId(Long.valueOf(relationshipTemplate.getId()));\r
+\r
+                    //Retrieve the Neighbour map of the source Node and add the Neighbour\r
+                    TNodeTemplate sourceNodeTemplate = (TNodeTemplate) relationshipTemplate.getSourceElement().getRef();\r
+                    Node source = nodes.get(Long.valueOf(sourceNodeTemplate.getId()));\r
+                    //this manages invalid/inexistent node ID for source node\r
+                    Map<Long,Neighbour> sourceNodeNeighMap = source.getNeighbours();\r
+                    if(sourceNodeNeighMap.containsKey(neigh.getId()))\r
+                        throw new BadRequestException("The RelationshipTemplate ID must be unique.");\r
+                    else\r
+                        sourceNodeNeighMap.put(neigh.getId(), neigh);\r
+                    source.setNeighbours(sourceNodeNeighMap);\r
+\r
+                    //Update the Node list\r
+                    nodes.put(Long.valueOf(sourceNodeTemplate.getId()), source);\r
+                } catch(NullPointerException | NumberFormatException ex) {\r
+                    throw new BadRequestException("A RelationshipTemplate has wrong fields representation.");\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+    private static Configuration mapToscaConfiguration(it.polito.tosca.jaxb.Configuration configuration)\r
+            throws JsonProcessingException, IOException {\r
+        Configuration conf = new Configuration();\r
+        ObjectMapper mapper = new ObjectMapper();\r
+        String stringConfiguration;\r
+\r
+        //Retrieve configuration ID (optional)\r
+        if (configuration.getConfID() != null)\r
+            conf.setId(configuration.getConfID());\r
+        else\r
+            conf.setId("");\r
+\r
+        //Retrieve description (optional)\r
+        if (configuration.getConfDescr() != null)\r
+            conf.setDescription(configuration.getConfDescr());\r
+        else\r
+            conf.setDescription("");\r
+\r
+        //Retrieve string of configuration\r
+        try {\r
+            stringConfiguration = MappingUtils.obtainStringConfiguration(configuration);\r
+        } catch(IOException ex) {\r
+            conf.setConfiguration(mapper.readTree("[]"));\r
+            System.out.println("[WARNING] Provided default configuration.");\r
+            return conf;\r
+        }\r
+\r
+        //Retrieve JsonNode from the string of configuration\r
+        try {\r
+            conf.setConfiguration(mapper.readTree(stringConfiguration));\r
+            return conf;\r
+        } catch (IOException e) {\r
+            throw new BadRequestException("NodeTemplate configuration is invalid.");\r
+        }\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/yaml/GraphToYaml.java b/verigraph/src/it/polito/verigraph/tosca/converter/yaml/GraphToYaml.java
new file mode 100644 (file)
index 0000000..8766ac5
--- /dev/null
@@ -0,0 +1,270 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.yaml;\r
+\r
+import java.io.IOException;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import com.fasterxml.jackson.core.JsonParseException;\r
+import com.fasterxml.jackson.databind.InjectableValues;\r
+import com.fasterxml.jackson.databind.JsonMappingException;\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.databind.module.SimpleModule;\r
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;\r
+\r
+import it.polito.neo4j.jaxb.FunctionalTypes;\r
+import it.polito.verigraph.exception.BadRequestException;\r
+import it.polito.verigraph.model.Graph;\r
+import it.polito.verigraph.model.Neighbour;\r
+import it.polito.verigraph.model.Node;\r
+import it.polito.verigraph.tosca.deserializer.YamlConfigurationDeserializer;\r
+import it.polito.verigraph.tosca.yaml.beans.AntispamConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.AntispamNode;\r
+import it.polito.verigraph.tosca.yaml.beans.CacheConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.CacheNode;\r
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.DpiConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.DpiNode;\r
+import it.polito.verigraph.tosca.yaml.beans.EndhostConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndhostNode;\r
+import it.polito.verigraph.tosca.yaml.beans.EndpointConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndpointNode;\r
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierNode;\r
+import it.polito.verigraph.tosca.yaml.beans.FirewallConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FirewallNode;\r
+import it.polito.verigraph.tosca.yaml.beans.MailClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailClientNode;\r
+import it.polito.verigraph.tosca.yaml.beans.MailServerConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailServerNode;\r
+import it.polito.verigraph.tosca.yaml.beans.NatConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.NatNode;\r
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.TopologyTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessNode;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnExitConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnExitNode;\r
+import it.polito.verigraph.tosca.yaml.beans.WebClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebClientNode;\r
+import it.polito.verigraph.tosca.yaml.beans.WebServerConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebServerNode;\r
+\r
+public class GraphToYaml {\r
+    public static ServiceTemplateYaml mapGraphYaml(Graph graph) {\r
+        ServiceTemplateYaml serviceTemplate = new ServiceTemplateYaml();\r
+        TopologyTemplateYaml topologyTemplate = new TopologyTemplateYaml();\r
+\r
+        topologyTemplate.setNode_templates(new HashMap<String,NodeTemplateYaml>());\r
+        topologyTemplate.setRelationship_templates(new HashMap<String,RelationshipTemplateYaml>());\r
+        serviceTemplate.setMetadata(new HashMap<String,String>());\r
+\r
+        //Neighbour does not have a neighbourID!\r
+        //RelationshipTemplate does, so it is an incremental number for each node\r
+        long i = 0; //This counter will act as a fake incremental id of relationships\r
+        for(Node node : graph.getNodes().values()) {\r
+            NodeTemplateYaml nodeTemplate;\r
+            try {\r
+                nodeTemplate = mapNodeYaml(node);\r
+            } catch (IOException e) {\r
+                throw new BadRequestException("Error while mapping a Node in Yaml object.");\r
+            }\r
+            topologyTemplate.getNode_templates().put(String.valueOf(node.getId()), nodeTemplate);\r
+            //shall we catch NumberFormatException?\r
+\r
+            Map<Long,Neighbour> neighMap = node.getNeighbours();\r
+            for (Map.Entry<Long, Neighbour> myentry : neighMap.entrySet()) {\r
+                Neighbour neigh = myentry.getValue();\r
+                if (graph.getNodes().containsKey(neigh.getId())) {\r
+                    // I have to check that because if I'm mapping a path (and not a graph)\r
+                    //I could have as neighbour a node which is not in the path\r
+                    RelationshipTemplateYaml relat = mapRelationshipYaml(node, neigh);\r
+                    topologyTemplate.getRelationship_templates().put(String.valueOf(i), relat);\r
+                    i++;\r
+                }\r
+            }\r
+        }\r
+\r
+        serviceTemplate.getMetadata().put("template_id", String.valueOf(graph.getId()));\r
+        serviceTemplate.setTopology_template(topologyTemplate);\r
+        return serviceTemplate;\r
+    }\r
+\r
+\r
+    private static NodeTemplateYaml mapNodeYaml(Node node) throws JsonParseException, JsonMappingException, IOException {\r
+\r
+        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());\r
+        JsonNode configNode = node.getConfiguration().getConfiguration();\r
+        SimpleModule module = new SimpleModule();\r
+\r
+        //Passing the configuration type to the Deserializer context\r
+        module.addDeserializer(ConfigurationYaml.class, new YamlConfigurationDeserializer());\r
+        mapper.registerModule(module);\r
+\r
+        //Here we use the custom deserializer to convert the JsonNode into a Yaml bean\r
+        //The injectable value allows to provide an additional info to the deserializer that will be able to\r
+        //know that it is parsing a certain type of Configuration.\r
+        ConfigurationYaml yamlConfig = mapper\r
+                .reader(new InjectableValues.Std().addValue("type", node.getFunctional_type().toLowerCase()))\r
+                .forType(ConfigurationYaml.class).readValue(configNode);\r
+\r
+        FunctionalTypes nodeType = FunctionalTypes.valueOf(node.getFunctional_type().toUpperCase());\r
+        switch(nodeType) {\r
+        case ANTISPAM:\r
+            AntispamNode antispamNode = new AntispamNode();\r
+            antispamNode.setName(node.getName());\r
+            antispamNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            antispamNode.setProperties((AntispamConfigurationYaml) yamlConfig);\r
+            return antispamNode;\r
+\r
+        case CACHE:\r
+            CacheNode cacheNode = new CacheNode();\r
+            cacheNode.setName(node.getName());\r
+            cacheNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            cacheNode.setProperties((CacheConfigurationYaml) yamlConfig);\r
+            return cacheNode;\r
+\r
+        case DPI:\r
+            DpiNode dpiNode = new DpiNode();\r
+            dpiNode.setName(node.getName());\r
+            dpiNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            dpiNode.setProperties((DpiConfigurationYaml) yamlConfig);\r
+            return dpiNode;\r
+\r
+        case ENDHOST:\r
+            EndhostNode endhostNode = new EndhostNode();\r
+            endhostNode.setName(node.getName());\r
+            endhostNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            endhostNode.setProperties((EndhostConfigurationYaml) yamlConfig);\r
+            return endhostNode;\r
+\r
+        case ENDPOINT:\r
+            EndpointNode endpointNode = new EndpointNode();\r
+            endpointNode.setName(node.getName());\r
+            endpointNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            endpointNode.setProperties((EndpointConfigurationYaml) yamlConfig);\r
+            return endpointNode;\r
+\r
+        case FIELDMODIFIER:\r
+            FieldModifierNode fieldmodifierNode = new FieldModifierNode();\r
+            fieldmodifierNode.setName(node.getName());\r
+            fieldmodifierNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            fieldmodifierNode.setProperties((FieldModifierConfigurationYaml) yamlConfig);\r
+            return fieldmodifierNode;\r
+\r
+        case FIREWALL:\r
+            FirewallNode firewallNode = new FirewallNode();\r
+            firewallNode.setName(node.getName());\r
+            firewallNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            firewallNode.setProperties((FirewallConfigurationYaml) yamlConfig);\r
+            return firewallNode;\r
+\r
+        case MAILCLIENT:\r
+            MailClientNode mailclientNode = new MailClientNode();\r
+            mailclientNode.setName(node.getName());\r
+            mailclientNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            mailclientNode.setProperties((MailClientConfigurationYaml) yamlConfig);\r
+            return mailclientNode;\r
+\r
+        case MAILSERVER:\r
+            MailServerNode mailserverNode = new MailServerNode();\r
+            mailserverNode.setName(node.getName());\r
+            mailserverNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            mailserverNode.setProperties((MailServerConfigurationYaml) yamlConfig);\r
+            return mailserverNode;\r
+\r
+        case NAT:\r
+            NatNode natNode = new NatNode();\r
+            natNode.setName(node.getName());\r
+            natNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            natNode.setProperties((NatConfigurationYaml) yamlConfig);\r
+            return natNode;\r
+\r
+        case VPNACCESS:\r
+            VpnAccessNode vpnaccessNode = new VpnAccessNode();\r
+            vpnaccessNode.setName(node.getName());\r
+            vpnaccessNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            vpnaccessNode.setProperties((VpnAccessConfigurationYaml) yamlConfig);\r
+            return vpnaccessNode;\r
+\r
+        case VPNEXIT:\r
+            VpnExitNode vpnexitNode = new VpnExitNode();\r
+            vpnexitNode.setName(node.getName());\r
+            vpnexitNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            vpnexitNode.setProperties((VpnExitConfigurationYaml) yamlConfig);\r
+            return vpnexitNode;\r
+\r
+        case WEBCLIENT:\r
+            WebClientNode webclientNode = new WebClientNode();\r
+            webclientNode.setName(node.getName());\r
+            webclientNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            webclientNode.setProperties((WebClientConfigurationYaml) yamlConfig);\r
+            return webclientNode;\r
+\r
+        case WEBSERVER:\r
+            WebServerNode webserverNode = new WebServerNode();\r
+            webserverNode.setName(node.getName());\r
+            webserverNode.setType("verigraph.nodeTypes." +\r
+                    node.getFunctional_type().substring(0, 1).toUpperCase() +\r
+                    node.getFunctional_type().substring(1));\r
+            webserverNode.setProperties((WebServerConfigurationYaml) yamlConfig);\r
+            return webserverNode;\r
+\r
+        default:\r
+            FieldModifierNode defaultNode = new FieldModifierNode();\r
+            defaultNode.setName(node.getName());\r
+            defaultNode.setType("verigraph.nodeTypes.Fieldmodifier");\r
+            defaultNode.setProperties(new FieldModifierConfigurationYaml());\r
+            return defaultNode;\r
+        }\r
+\r
+    }\r
+\r
+    private static RelationshipTemplateYaml mapRelationshipYaml(Node sourceNode, Neighbour neigh) {\r
+        RelationshipTemplateYaml relationship = new RelationshipTemplateYaml();\r
+        relationship.setProperties(new HashMap<String,String>());\r
+\r
+        relationship.setType("verigraph.relationshipType.generic");\r
+        relationship.getProperties().put("source_id", String.valueOf(sourceNode.getId())); //to be catched?\r
+        relationship.getProperties().put("target_id", String.valueOf(neigh.getId()));\r
+        relationship.getProperties().put("name", sourceNode.getName()+"to"+neigh.getName());\r
+\r
+        return relationship;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/yaml/YamlToGraph.java b/verigraph/src/it/polito/verigraph/tosca/converter/yaml/YamlToGraph.java
new file mode 100644 (file)
index 0000000..3922cf6
--- /dev/null
@@ -0,0 +1,138 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.converter.yaml;\r
+\r
+import java.io.IOException;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.fasterxml.jackson.databind.module.SimpleModule;\r
+\r
+import it.polito.verigraph.exception.BadRequestException;\r
+import it.polito.verigraph.model.Configuration;\r
+import it.polito.verigraph.model.Graph;\r
+import it.polito.verigraph.model.Neighbour;\r
+import it.polito.verigraph.model.Node;\r
+import it.polito.verigraph.tosca.YamlParsingUtils;\r
+import it.polito.verigraph.tosca.serializer.YamlConfigSerializer;\r
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;\r
+\r
+public class YamlToGraph {\r
+    public static Graph mapTopologyTemplateYaml(ServiceTemplateYaml yamlServiceTemplate) throws BadRequestException {\r
+        Graph graph = new Graph();\r
+        Map<Long, Node> graphNodes = new HashMap<>();\r
+        Map<String, NodeTemplateYaml> nodes = new HashMap<>();\r
+        Map<String, RelationshipTemplateYaml> relats = new HashMap<>();\r
+\r
+        nodes = yamlServiceTemplate.getTopology_template().getNode_templates();\r
+\r
+        for (Map.Entry<String, NodeTemplateYaml> nodeYamlEntry : nodes.entrySet()) {\r
+            Node node = mapNodeTemplateYaml(nodeYamlEntry.getValue());\r
+\r
+            try {\r
+                graphNodes.put(Long.valueOf(nodeYamlEntry.getKey()), node);\r
+            } catch (NumberFormatException e) {\r
+                throw new BadRequestException("The NodeTemplate ID must be a number.");\r
+            }\r
+\r
+        }\r
+\r
+        // Add Neighbours to the Nodes of the list\r
+        relats = yamlServiceTemplate.getTopology_template().getRelationship_templates();\r
+        mapRelationshipTemplatesYaml(graphNodes, relats);\r
+\r
+        // Add Nodes and ID to the graph\r
+        graph.setNodes(graphNodes);\r
+        try {\r
+            graph.setId(Long.valueOf(yamlServiceTemplate.getMetadata().get("template_id")));\r
+        } catch (NumberFormatException ex) {\r
+            throw new BadRequestException("If you want to use this service, the TopologyTemplate ID must be a number.");\r
+        } catch (NullPointerException ex) {} //ID is not mandatory for the user since VeriGraph provides its IDs\r
+\r
+        return graph;\r
+    }\r
+\r
+\r
+    private static Node mapNodeTemplateYaml(NodeTemplateYaml yamlNodeTemplate) {\r
+        Node node = new Node();\r
+\r
+        String type =  yamlNodeTemplate.getType().replace("verigraph.nodeTypes.", "").toLowerCase();\r
+\r
+        try {\r
+            node.setName(yamlNodeTemplate.getName());\r
+            Configuration conf = mapConfigurationYaml(yamlNodeTemplate);\r
+            node.setConfiguration(conf);\r
+            node.setFunctional_type(type);\r
+        } catch(NullPointerException ex) {\r
+            throw new BadRequestException("A NodeTemplate has wrong fields representation.");\r
+        }\r
+        return node;\r
+    }\r
+\r
+\r
+    private static void mapRelationshipTemplatesYaml(Map<Long, Node> graphNodes, Map<String, RelationshipTemplateYaml> relats) {\r
+        //updated nodes (update = Node + its Neighbours)\r
+        for(Map.Entry<String, RelationshipTemplateYaml> yamlRelationshipTemplate : relats.entrySet()) {\r
+            try {\r
+                // Retrieve relationship information\r
+                String target = yamlRelationshipTemplate.getValue().getProperties().get("target_id");\r
+                String source = yamlRelationshipTemplate.getValue().getProperties().get("source_id");\r
+                String name = graphNodes.get(Long.valueOf(target)).getName();\r
+\r
+                Neighbour neigh = new Neighbour();\r
+                neigh.setName(name);\r
+                neigh.setId(Long.valueOf(target));\r
+\r
+                //Retrieve the Neighbour map of the source Node and add the Neighbour\r
+                Node sourceNode = graphNodes.get(Long.valueOf(source));\r
+                Map<Long,Neighbour> sourceNodeNeighMap = sourceNode.getNeighbours();\r
+                if(sourceNodeNeighMap.containsKey(neigh.getId()))\r
+                    throw new BadRequestException("The RelationshipTemplate ID must be unique.");\r
+                else\r
+                    sourceNodeNeighMap.put(neigh.getId(), neigh);\r
+                sourceNode.setNeighbours(sourceNodeNeighMap);\r
+\r
+                //Update the Node list\r
+                graphNodes.put(Long.valueOf(source), sourceNode);\r
+\r
+            } catch(NullPointerException | NumberFormatException ex) {\r
+                throw new BadRequestException("A RelationshipTemplate has wrong fields representation.");\r
+            }\r
+\r
+        }\r
+\r
+    }\r
+\r
+\r
+    private static Configuration mapConfigurationYaml(NodeTemplateYaml node) {\r
+        Configuration config = new Configuration();\r
+        JsonNode jsonConfiguration = null;\r
+        ObjectMapper mapper = new ObjectMapper();\r
+        SimpleModule module = new SimpleModule();\r
+        module.addSerializer(ConfigurationYaml.class, new YamlConfigSerializer());\r
+        mapper.registerModule(module);\r
+\r
+        try{\r
+            String stringConfiguration = YamlParsingUtils.obtainConfiguration(node);\r
+            jsonConfiguration = mapper.readTree(stringConfiguration);\r
+            config.setConfiguration(jsonConfiguration);\r
+            config.setDescription("");\r
+            config.setId("");\r
+        } catch (NullPointerException | IOException | BadRequestException e) {\r
+            throw new BadRequestException("Not able to retrieve a valid configuration");\r
+        }\r
+\r
+        return config;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java b/verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java
new file mode 100644 (file)
index 0000000..7e2ee4d
--- /dev/null
@@ -0,0 +1,177 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.deserializer;\r
+\r
+import it.polito.tosca.jaxb.Configuration;\r
+import com.fasterxml.jackson.core.JsonParser;\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.core.ObjectCodec;\r
+import com.fasterxml.jackson.databind.DeserializationContext;\r
+import com.fasterxml.jackson.databind.JsonDeserializer;\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+import java.io.IOException;\r
+import java.math.BigInteger;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+public class XmlConfigurationDeserializer extends JsonDeserializer<Configuration> {\r
+\r
+    @Override\r
+    public Configuration deserialize(JsonParser jp, DeserializationContext ctxt)\r
+            throws IOException, JsonProcessingException {\r
+        ObjectCodec oc = jp.getCodec();\r
+        JsonNode node = oc.readTree(jp);\r
+        Configuration deserialized;\r
+\r
+        try {\r
+            //Get the content from the array wrapping the JSON configuration\r
+            final Iterator<JsonNode> elements = node.elements();\r
+            deserialized = new Configuration();\r
+\r
+\r
+            if(!elements.hasNext()) {\r
+                //System.out.println("The provided configuration is empty.");\r
+                //return null; //TODO shall we return an empty configuration?\r
+\r
+                return new Configuration();\r
+            }\r
+\r
+            switch ((String) ctxt.findInjectableValue("type", null, null)) {\r
+\r
+            case "antispam":\r
+                Configuration.AntispamConfiguration antispam = new Configuration.AntispamConfiguration();\r
+                List<String> sources = antispam.getSource();\r
+                while (elements.hasNext()) {\r
+                    sources.add(elements.next().asText());\r
+                }\r
+                deserialized.setAntispamConfiguration(antispam);\r
+                break;\r
+\r
+            case "cache":\r
+                Configuration.CacheConfiguration cache = new Configuration.CacheConfiguration();\r
+                List<String> resources = cache.getResource();\r
+                while(elements.hasNext()) {\r
+                    resources.add(elements.next().asText());\r
+                }\r
+                deserialized.setCacheConfiguration(cache);\r
+                break;\r
+\r
+            case "endhost":\r
+                Configuration.EndhostConfiguration endhost = new Configuration.EndhostConfiguration();\r
+                JsonNode thisnode = elements.next();\r
+\r
+                if(thisnode.has("body"))\r
+                    endhost.setBody(thisnode.findValue("body").asText());\r
+                if(thisnode.has("sequence"))\r
+                    endhost.setSequence(BigInteger.valueOf(Long.valueOf(thisnode.findValue("sequence").asText())));\r
+                if(thisnode.has("protocol"))\r
+                    endhost.setProtocol(thisnode.findValue("protocol").asText());\r
+                if(thisnode.has("email_from"))\r
+                    endhost.setEmailFrom(thisnode.findValue("email_from").asText());\r
+                if(thisnode.has("url"))\r
+                    endhost.setUrl(thisnode.findValue("url").asText());\r
+                if(thisnode.has("options"))\r
+                    endhost.setOptions(thisnode.findValue("options").asText());\r
+                if(thisnode.has("destination"))\r
+                    endhost.setDestination(thisnode.findValue("destination").asText());\r
+\r
+                deserialized.setEndhostConfiguration(endhost);\r
+                break;\r
+\r
+            case "endpoint":\r
+                Configuration.EndpointConfiguration endpoint = new Configuration.EndpointConfiguration();\r
+                deserialized.setEndpointConfiguration(endpoint);\r
+                break;\r
+\r
+            case "fieldmodifier":\r
+                Configuration.FieldmodifierConfiguration fieldmodifier = new Configuration.FieldmodifierConfiguration();\r
+                deserialized.setFieldmodifierConfiguration(fieldmodifier);\r
+                break;\r
+\r
+            case "firewall":\r
+                Configuration.FirewallConfiguration firewall = new Configuration.FirewallConfiguration();\r
+                List<Configuration.FirewallConfiguration.Elements> fwelements = firewall.getElements();\r
+                Configuration.FirewallConfiguration.Elements element;\r
+\r
+                Iterator<Map.Entry<String, JsonNode>> current = elements.next().fields();\r
+                Map.Entry<String, JsonNode> entry;\r
+                while(current.hasNext()) {\r
+                    entry = current.next();\r
+                    element = new Configuration.FirewallConfiguration.Elements();\r
+                    element.setSource(entry.getKey());\r
+                    element.setDestination(entry.getValue().asText());\r
+                    fwelements.add(element);\r
+                }\r
+\r
+                deserialized.setFirewallConfiguration(firewall);\r
+                break;\r
+\r
+            case "mailclient":\r
+                Configuration.MailclientConfiguration mailclient = new Configuration.MailclientConfiguration();\r
+                mailclient.setMailserver(elements.next().findValue("mailserver").asText());\r
+                deserialized.setMailclientConfiguration(mailclient);\r
+                break;\r
+\r
+            case "mailserver":\r
+                Configuration.MailserverConfiguration mailserver = new Configuration.MailserverConfiguration();\r
+                deserialized.setMailserverConfiguration(mailserver);\r
+                break;\r
+\r
+            case "nat":\r
+                Configuration.NatConfiguration nat = new Configuration.NatConfiguration();\r
+                List<String> natsource = nat.getSource();\r
+                while(elements.hasNext()) {\r
+                    natsource.add(elements.next().asText());\r
+                }\r
+                deserialized.setNatConfiguration(nat);\r
+                break;\r
+\r
+            case "vpnaccess":\r
+                Configuration.VpnaccessConfiguration vpnaccess = new Configuration.VpnaccessConfiguration();\r
+                vpnaccess.setVpnexit(elements.next().findValue("vpnexit").asText());\r
+                deserialized.setVpnaccessConfiguration(vpnaccess);\r
+                break;\r
+\r
+            case "vpnexit":\r
+                Configuration.VpnexitConfiguration vpnexit = new Configuration.VpnexitConfiguration();\r
+                vpnexit.setVpnaccess(elements.next().findValue("vpnaccess").asText());\r
+                deserialized.setVpnexitConfiguration(vpnexit);\r
+                break;\r
+\r
+            case "webclient":\r
+                Configuration.WebclientConfiguration webclient = new Configuration.WebclientConfiguration();\r
+                webclient.setNameWebServer(elements.next().findValue("webserver").asText());\r
+                deserialized.setWebclientConfiguration(webclient);\r
+                break;\r
+\r
+            case "webserver":\r
+                Configuration.WebserverConfiguration webserver = new Configuration.WebserverConfiguration();\r
+                deserialized.setWebserverConfiguration(webserver);\r
+                break;\r
+\r
+            default:\r
+                Configuration.FieldmodifierConfiguration defaultForwarder = new Configuration.FieldmodifierConfiguration();\r
+                deserialized.setFieldmodifierConfiguration(defaultForwarder);\r
+                break;\r
+            }\r
+\r
+\r
+        } catch (Exception e) {\r
+            // TODO Auto-generated catch block\r
+            System.err.println("Error converting the Json to XmlConfiguration");\r
+            e.printStackTrace();\r
+            return null;\r
+        }\r
+\r
+        return deserialized;\r
+\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/deserializer/YamlConfigurationDeserializer.java b/verigraph/src/it/polito/verigraph/tosca/deserializer/YamlConfigurationDeserializer.java
new file mode 100644 (file)
index 0000000..8fb7e52
--- /dev/null
@@ -0,0 +1,251 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.deserializer;\r
+\r
+\r
+import com.fasterxml.jackson.core.JsonParser;\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.core.ObjectCodec;\r
+import com.fasterxml.jackson.databind.DeserializationContext;\r
+import com.fasterxml.jackson.databind.JsonDeserializer;\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+\r
+import it.polito.verigraph.tosca.yaml.beans.AntispamConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.CacheConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.DpiConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndhostConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndpointConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FirewallConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailServerConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.NatConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnExitConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebServerConfigurationYaml;\r
+\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+public class YamlConfigurationDeserializer extends JsonDeserializer<ConfigurationYaml> {\r
+\r
+    @Override\r
+    public ConfigurationYaml deserialize(JsonParser jp, DeserializationContext ctxt)\r
+            throws IOException, JsonProcessingException {\r
+        ObjectCodec oc = jp.getCodec();\r
+        JsonNode node = oc.readTree(jp);\r
+        ConfigurationYaml deserialized;\r
+        boolean emptyConfig = false;\r
+\r
+        try {\r
+            //Get the content from the array wrapping the JSON configuration\r
+            final Iterator<JsonNode> elements = node.elements();\r
+\r
+            if(!elements.hasNext()) {\r
+                //System.out.println("The provided configuration is empty.");\r
+                emptyConfig = true;\r
+            }\r
+\r
+            switch ((String)ctxt.findInjectableValue("type", null, null)) {\r
+\r
+            case "antispam":\r
+                if(emptyConfig) {\r
+                    deserialized = new AntispamConfigurationYaml();\r
+                    break;\r
+                }\r
+                AntispamConfigurationYaml antispam = new AntispamConfigurationYaml();\r
+                antispam.setSources(new ArrayList<String>());\r
+                while (elements.hasNext()) {\r
+                    antispam.getSources().add(elements.next().asText());\r
+                }\r
+                deserialized = antispam;\r
+                break;\r
+\r
+            case "cache":\r
+                if(emptyConfig) {\r
+                    deserialized = new CacheConfigurationYaml();\r
+                    break;\r
+                }\r
+                CacheConfigurationYaml cache = new CacheConfigurationYaml();\r
+                cache.setResources(new ArrayList<String>());\r
+                while(elements.hasNext()) {\r
+                    cache.getResources().add(elements.next().asText());\r
+                }\r
+                deserialized = cache;\r
+                break;\r
+\r
+            case "dpi":\r
+                if(emptyConfig) {\r
+                    deserialized = new DpiConfigurationYaml();\r
+                    break;\r
+                }\r
+                DpiConfigurationYaml dpi = new DpiConfigurationYaml();\r
+                dpi.setNotAllowedList(new ArrayList<String>());\r
+                while(elements.hasNext()) {\r
+                    dpi.getNotAllowedList().add(elements.next().asText());\r
+                }\r
+                deserialized = dpi;\r
+                break;\r
+\r
+            case "endhost":\r
+                if(emptyConfig) {\r
+                    deserialized = new EndhostConfigurationYaml();\r
+                    break;\r
+                }\r
+                EndhostConfigurationYaml endhost = new EndhostConfigurationYaml();\r
+                JsonNode thisnode = elements.next();\r
+                if(thisnode.has("body"))\r
+                    endhost.setBody(thisnode.findValue("body").asText());\r
+                if(thisnode.has("sequence"))\r
+                    endhost.setSequence(Integer.valueOf(thisnode.findValue("sequence").asText()));\r
+                if(thisnode.has("protocol"))\r
+                    endhost.setProtocol(thisnode.findValue("protocol").asText());\r
+                if(thisnode.has("email_from"))\r
+                    endhost.setEmail_from(thisnode.findValue("email_from").asText());\r
+                if(thisnode.has("options"))\r
+                    endhost.setOptions(thisnode.findValue("options").asText());\r
+                if(thisnode.has("url"))\r
+                    endhost.setUrl(thisnode.findValue("url").asText());\r
+                if(thisnode.has("destination"))\r
+                    endhost.setDestination(thisnode.findValue("destination").asText());\r
+\r
+                deserialized = endhost;\r
+                break;\r
+\r
+            case "endpoint":\r
+                if(emptyConfig) {\r
+                    deserialized = new EndpointConfigurationYaml();\r
+                    break;\r
+                }\r
+                EndpointConfigurationYaml endpoint = new EndpointConfigurationYaml();\r
+                deserialized = endpoint;\r
+                break;\r
+\r
+            case "fieldmodifier":\r
+                if(emptyConfig) {\r
+                    deserialized = new FieldModifierConfigurationYaml();\r
+                    break;\r
+                }\r
+                FieldModifierConfigurationYaml fieldmodifier = new FieldModifierConfigurationYaml();\r
+                deserialized = fieldmodifier;\r
+                break;\r
+\r
+            case "firewall":\r
+                if(emptyConfig) {\r
+                    deserialized = new FirewallConfigurationYaml();\r
+                    break;\r
+                }\r
+                FirewallConfigurationYaml firewall = new FirewallConfigurationYaml();\r
+                firewall.setElements(new HashMap<String,String>());\r
+                JsonNode current = elements.next();\r
+                Iterator<Map.Entry<String, JsonNode>> iter = current.fields();\r
+\r
+                while (iter.hasNext()) {\r
+                    Map.Entry<String, JsonNode> entry = iter.next();\r
+                    firewall.getElements().put(entry.getKey(), entry.getValue().asText());\r
+                }\r
+\r
+                deserialized = firewall;\r
+                break;\r
+\r
+            case "mailclient":\r
+                if(emptyConfig) {\r
+                    deserialized = new MailClientConfigurationYaml();\r
+                    break;\r
+                }\r
+                MailClientConfigurationYaml mailclient = new MailClientConfigurationYaml();\r
+                mailclient.setMailserver(elements.next().findValue("mailserver").asText());\r
+                deserialized = mailclient;\r
+                break;\r
+\r
+            case "mailserver":\r
+                if(emptyConfig) {\r
+                    deserialized = new MailServerConfigurationYaml();\r
+                    break;\r
+                }\r
+                MailServerConfigurationYaml mailserver = new MailServerConfigurationYaml();\r
+                deserialized = mailserver;\r
+                break;\r
+\r
+            case "nat":\r
+                if(emptyConfig) {\r
+                    deserialized = new NatConfigurationYaml();\r
+                    break;\r
+                }\r
+                NatConfigurationYaml nat = new NatConfigurationYaml();\r
+                nat.setSources(new ArrayList<String>());\r
+                while(elements.hasNext()) {\r
+                    nat.getSources().add(elements.next().asText());\r
+                }\r
+                deserialized = nat;\r
+                break;\r
+\r
+            case "vpnaccess":\r
+                if(emptyConfig) {\r
+                    deserialized = new VpnAccessConfigurationYaml();\r
+                    break;\r
+                }\r
+                VpnAccessConfigurationYaml vpnaccess = new VpnAccessConfigurationYaml();\r
+                vpnaccess.setVpnexit(elements.next().findValue("vpnexit").asText());\r
+                deserialized = vpnaccess;\r
+                break;\r
+\r
+            case "vpnexit":\r
+                if(emptyConfig) {\r
+                    deserialized = new VpnExitConfigurationYaml();\r
+                    break;\r
+                }\r
+                VpnExitConfigurationYaml vpnexit = new VpnExitConfigurationYaml();\r
+                vpnexit.setVpnaccess(elements.next().findValue("vpnaccess").asText());\r
+                deserialized = vpnexit;\r
+                break;\r
+\r
+            case "webclient":\r
+                if(emptyConfig) {\r
+                    deserialized = new WebClientConfigurationYaml();\r
+                    break;\r
+                }\r
+                WebClientConfigurationYaml webclient = new WebClientConfigurationYaml();\r
+                webclient.setNameWebServer(elements.next().findValue("webserver").asText());\r
+                deserialized = webclient;\r
+                break;\r
+\r
+            case "webserver":\r
+                if(emptyConfig) {\r
+                    deserialized = new WebServerConfigurationYaml();\r
+                    break;\r
+                }\r
+                WebServerConfigurationYaml webserver = new WebServerConfigurationYaml();\r
+                deserialized = webserver;\r
+                break;\r
+\r
+            default:\r
+                deserialized = new FieldModifierConfigurationYaml();\r
+                break;\r
+            }\r
+\r
+\r
+        } catch (Exception e) {\r
+            // TODO Auto-generated catch block\r
+            //System.err.println("Error converting the Json to YamlConfiguration");\r
+            e.printStackTrace();\r
+            return new FieldModifierConfigurationYaml();\r
+        }\r
+\r
+        return deserialized;\r
+\r
+    }\r
+\r
+}
\ No newline at end of file
diff --git a/verigraph/src/it/polito/verigraph/tosca/serializer/XmlConfigSerializer.java b/verigraph/src/it/polito/verigraph/tosca/serializer/XmlConfigSerializer.java
new file mode 100644 (file)
index 0000000..b650420
--- /dev/null
@@ -0,0 +1,163 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.serializer;\r
+\r
+import java.io.IOException;\r
+\r
+import com.fasterxml.jackson.core.JsonGenerator;\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.databind.SerializerProvider;\r
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;\r
+\r
+import it.polito.tosca.jaxb.Configuration;\r
+\r
+//Custom serializer for XmlToscaConfigurationObject conversion to JSON\r
+public class XmlConfigSerializer extends StdSerializer<Configuration> {\r
+\r
+    //Automatically generated VersionUID\r
+    private static final long serialVersionUID = 9102508941195129607L;\r
+\r
+    public XmlConfigSerializer() {\r
+        this(null);\r
+    }\r
+\r
+    public XmlConfigSerializer(Class<Configuration> t) {\r
+        super(t);\r
+    }\r
+\r
+    @Override\r
+    public void serialize(\r
+            Configuration value, JsonGenerator jgen, SerializerProvider provider)\r
+                    throws IOException, JsonProcessingException {\r
+\r
+        if (value.getAntispamConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            for(String source : value.getAntispamConfiguration().getSource()) {\r
+                jgen.writeString(source);\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getCacheConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            for(String resource : value.getCacheConfiguration().getResource()) {\r
+                jgen.writeString(resource);\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getDpiConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            for(String notAllowed : value.getDpiConfiguration().getNotAllowed()) {\r
+                jgen.writeString(notAllowed);\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getEndhostConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            jgen.writeStartObject();\r
+\r
+            Configuration.EndhostConfiguration endhost = value.getEndhostConfiguration();\r
+            if (endhost.getBody() != null) jgen.writeObjectField("body", endhost.getBody());\r
+            if (endhost.getSequence() != null) jgen.writeObjectField("sequence", endhost.getSequence());\r
+            if (endhost.getProtocol() != null) jgen.writeObjectField("protocol", endhost.getProtocol());\r
+            if (endhost.getEmailFrom() != null) jgen.writeObjectField("email_from", endhost.getEmailFrom());\r
+            if (endhost.getUrl() != null) jgen.writeObjectField("url", endhost.getUrl());\r
+            if (endhost.getOptions() != null) jgen.writeObjectField("options", endhost.getOptions());\r
+            if (endhost.getDestination() != null) jgen.writeObjectField("destination", endhost.getDestination());\r
+\r
+            jgen.writeEndObject();\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getEndpointConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getFieldmodifierConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getFirewallConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+\r
+            for(Configuration.FirewallConfiguration.Elements elem : value.getFirewallConfiguration().getElements()) {\r
+                if ((elem.getSource() != null) && (elem.getDestination() != null)) {\r
+                    jgen.writeStartObject();\r
+                    jgen.writeObjectField(elem.getSource(), elem.getDestination());\r
+                    jgen.writeEndObject();\r
+                }\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getMailclientConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+\r
+            if (value.getMailclientConfiguration().getMailserver() != null) {\r
+                jgen.writeStartObject();\r
+                jgen.writeObjectField("mailserver", value.getMailclientConfiguration().getMailserver());\r
+                jgen.writeEndObject();\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getMailserverConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getNatConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            for(String source : value.getNatConfiguration().getSource()) {\r
+                jgen.writeString(source);\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getVpnaccessConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+\r
+            if (value.getVpnaccessConfiguration().getVpnexit()!= null) {\r
+                jgen.writeStartObject();\r
+                jgen.writeObjectField("vpnexit", value.getVpnaccessConfiguration().getVpnexit());\r
+                jgen.writeEndObject();\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getVpnexitConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+\r
+            if (value.getVpnexitConfiguration().getVpnaccess()!= null) {\r
+                jgen.writeStartObject();\r
+                jgen.writeObjectField("vpnaccess", value.getVpnexitConfiguration().getVpnaccess());\r
+                jgen.writeEndObject();\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getWebclientConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+\r
+            if (value.getWebclientConfiguration().getNameWebServer() != null) {\r
+                jgen.writeStartObject();\r
+                jgen.writeObjectField("webserver", value.getWebclientConfiguration().getNameWebServer());\r
+                jgen.writeEndObject();\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        } else if (value.getWebserverConfiguration() != null) {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+\r
+        } else {\r
+            //Case of empty Configuration\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+        }\r
+\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/serializer/YamlConfigSerializer.java b/verigraph/src/it/polito/verigraph/tosca/serializer/YamlConfigSerializer.java
new file mode 100644 (file)
index 0000000..05a1629
--- /dev/null
@@ -0,0 +1,186 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.serializer;\r
+\r
+import java.io.IOException;\r
+import java.util.Map;\r
+\r
+import com.fasterxml.jackson.core.JsonGenerator;\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.databind.SerializerProvider;\r
+import com.fasterxml.jackson.databind.ser.std.StdSerializer;\r
+\r
+import it.polito.verigraph.tosca.yaml.beans.AntispamConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.CacheConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.DpiConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndhostConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndpointConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FirewallConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailServerConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.NatConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnExitConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebServerConfigurationYaml;\r
+\r
+//Custom serializer for YamlToscaConfigurationObject conversion to JSON\r
+public class YamlConfigSerializer extends StdSerializer<ConfigurationYaml> {\r
+\r
+    //Automatically generated VersionUID\r
+    private static final long serialVersionUID = 9102508941195129607L;\r
+\r
+    public YamlConfigSerializer() {\r
+        this(null);\r
+    }\r
+\r
+    public YamlConfigSerializer(Class<ConfigurationYaml> t) {\r
+        super(t);\r
+    }\r
+\r
+    @Override\r
+    public void serialize(\r
+            ConfigurationYaml value, JsonGenerator jgen, SerializerProvider provider)\r
+                    throws IOException, JsonProcessingException {\r
+\r
+        if(value instanceof AntispamConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            for(String source : ((AntispamConfigurationYaml) value).getSources()) {\r
+                jgen.writeString(source);\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof CacheConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            for(String resource : ((CacheConfigurationYaml) value).getResources()) {\r
+                jgen.writeString(resource);\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof DpiConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            for(String notAllowed : ((DpiConfigurationYaml) value).getNotAllowedList()) {\r
+                jgen.writeString(notAllowed);\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof EndhostConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            jgen.writeStartObject();\r
+\r
+            EndhostConfigurationYaml endhost = (EndhostConfigurationYaml) value;\r
+\r
+            if(endhost.getBody() != null)\r
+                jgen.writeObjectField("body", endhost.getBody());\r
+            if(endhost.getSequence() != 0)\r
+                jgen.writeObjectField("sequence", endhost.getSequence());\r
+            if(endhost.getProtocol() != null)\r
+                jgen.writeObjectField("protocol", endhost.getProtocol());\r
+            if(endhost.getEmail_from() != null)\r
+                jgen.writeObjectField("email_from", endhost.getEmail_from());\r
+            if(endhost.getUrl() != null)\r
+                jgen.writeObjectField("url", endhost.getUrl());\r
+            if(endhost.getOptions() != null)\r
+                jgen.writeObjectField("options", endhost.getOptions());\r
+            if(endhost.getDestination() != null)\r
+                jgen.writeObjectField("destination", endhost.getDestination());\r
+\r
+            jgen.writeEndObject();\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof EndpointConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof FieldModifierConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof FirewallConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            FirewallConfigurationYaml fw = (FirewallConfigurationYaml) value;\r
+\r
+            for(Map.Entry<String, String> entry : fw.getElements().entrySet()) {\r
+                if(entry.getKey()!= null && entry.getValue() != null) {\r
+                    jgen.writeStartObject();\r
+                    jgen.writeObjectField(entry.getKey(),  entry.getValue());\r
+                    jgen.writeEndObject();\r
+                }\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof MailClientConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+\r
+            if(((MailClientConfigurationYaml) value).getMailserver() != null) {\r
+                jgen.writeStartObject();\r
+                jgen.writeObjectField("mailserver", ((MailClientConfigurationYaml) value).getMailserver());\r
+                jgen.writeEndObject();\r
+            }\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof MailServerConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof NatConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+\r
+            for(String source : ((NatConfigurationYaml) value).getSources()) {\r
+                jgen.writeString(source);\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof VpnAccessConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+\r
+            if(((VpnAccessConfigurationYaml) value).getVpnexit()!= null) {\r
+                jgen.writeStartObject();\r
+                jgen.writeObjectField("vpnexit", ((VpnAccessConfigurationYaml) value).getVpnexit());\r
+                jgen.writeEndObject();\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof VpnExitConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+\r
+            if(((VpnExitConfigurationYaml) value).getVpnaccess()!= null) {\r
+                jgen.writeStartObject();\r
+                jgen.writeObjectField("vpnaccess", ((VpnExitConfigurationYaml) value).getVpnaccess());\r
+                jgen.writeEndObject();\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof WebClientConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+\r
+            if(((WebClientConfigurationYaml) value).getNameWebServer() != null) {\r
+                jgen.writeStartObject();\r
+                jgen.writeObjectField("webserver", ((WebClientConfigurationYaml) value).getNameWebServer());\r
+                jgen.writeEndObject();\r
+            }\r
+\r
+            jgen.writeEndArray();\r
+\r
+        }else if(value instanceof WebServerConfigurationYaml) {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+\r
+        }else  {\r
+            jgen.writeStartArray();\r
+            jgen.writeEndArray();\r
+        }\r
+\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/AntispamConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/AntispamConfigurationYaml.java
new file mode 100644 (file)
index 0000000..b0a082d
--- /dev/null
@@ -0,0 +1,24 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.List;\r
+\r
+public class AntispamConfigurationYaml implements ConfigurationYaml {\r
+    private List<String> sources;\r
+\r
+    public List<String> getSources() {\r
+        return sources;\r
+    }\r
+\r
+    public void setSources(List<String> sources) {\r
+        this.sources = sources;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/AntispamNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/AntispamNode.java
new file mode 100644 (file)
index 0000000..9d02e87
--- /dev/null
@@ -0,0 +1,24 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class AntispamNode extends NodeTemplateYaml {\r
+    private AntispamConfigurationYaml properties;\r
+\r
+    public AntispamConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(AntispamConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/CacheConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/CacheConfigurationYaml.java
new file mode 100644 (file)
index 0000000..5cf03cd
--- /dev/null
@@ -0,0 +1,26 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.List;\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true) \r
+public class CacheConfigurationYaml implements ConfigurationYaml {\r
+    private List<String> resources;\r
+\r
+    public List<String> getResources() {\r
+        return resources;\r
+    }\r
+\r
+    public void setResources(List<String> resources) {\r
+        this.resources = resources;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/CacheNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/CacheNode.java
new file mode 100644 (file)
index 0000000..99adb2e
--- /dev/null
@@ -0,0 +1,22 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class CacheNode extends NodeTemplateYaml {\r
+    private CacheConfigurationYaml properties;\r
+\r
+    public CacheConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(CacheConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/ConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/ConfigurationYaml.java
new file mode 100644 (file)
index 0000000..b0e4000
--- /dev/null
@@ -0,0 +1,16 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+\r
+//Empty interface implemented by all the Yaml configuration beans allowing \r
+//an easier and type indipendet use of the objects\r
+public interface ConfigurationYaml {\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/DpiConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/DpiConfigurationYaml.java
new file mode 100644 (file)
index 0000000..3074780
--- /dev/null
@@ -0,0 +1,26 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.List;\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true) \r
+public class DpiConfigurationYaml implements ConfigurationYaml{\r
+    private List<String> notAllowedList;\r
+\r
+    public List<String> getNotAllowedList() {\r
+        return notAllowedList;\r
+    }\r
+\r
+    public void setNotAllowedList(List<String> notAllowedList) {\r
+        this.notAllowedList = notAllowedList;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/DpiNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/DpiNode.java
new file mode 100644 (file)
index 0000000..d200cc2
--- /dev/null
@@ -0,0 +1,22 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class DpiNode extends NodeTemplateYaml {\r
+    private DpiConfigurationYaml properties;\r
+\r
+    public DpiConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(DpiConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndhostConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndhostConfigurationYaml.java
new file mode 100644 (file)
index 0000000..e1cfbd8
--- /dev/null
@@ -0,0 +1,67 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true) \r
+public class EndhostConfigurationYaml implements ConfigurationYaml{\r
+    private String body;\r
+    private int sequence;\r
+    private String protocol;\r
+    private String email_from;\r
+    private String url;\r
+    private String options;\r
+    private String destination;\r
+\r
+    public String getBody() {\r
+        return body;\r
+    }\r
+    public void setBody(String body) {\r
+        this.body = body;\r
+    }\r
+    public int getSequence() {\r
+        return sequence;\r
+    }\r
+    public void setSequence(int sequence) {\r
+        this.sequence = sequence;\r
+    }\r
+    public String getProtocol() {\r
+        return protocol;\r
+    }\r
+    public void setProtocol(String protocol) {\r
+        this.protocol = protocol;\r
+    }\r
+    public String getEmail_from() {\r
+        return email_from;\r
+    }\r
+    public void setEmail_from(String email_from) {\r
+        this.email_from = email_from;\r
+    }\r
+    public String getUrl() {\r
+        return url;\r
+    }\r
+    public void setUrl(String url) {\r
+        this.url = url;\r
+    }\r
+    public String getOptions() {\r
+        return options;\r
+    }\r
+    public void setOptions(String options) {\r
+        this.options = options;\r
+    }\r
+    public String getDestination() {\r
+        return destination;\r
+    }\r
+    public void setDestination(String destination) {\r
+        this.destination = destination;\r
+    }\r
+\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndhostNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndhostNode.java
new file mode 100644 (file)
index 0000000..c47b14e
--- /dev/null
@@ -0,0 +1,22 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class EndhostNode extends NodeTemplateYaml{\r
+    private EndhostConfigurationYaml properties;\r
+\r
+    public EndhostConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(EndhostConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndpointConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndpointConfigurationYaml.java
new file mode 100644 (file)
index 0000000..76cbec6
--- /dev/null
@@ -0,0 +1,26 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.List;\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class EndpointConfigurationYaml implements ConfigurationYaml{\r
+    private List<String> names;\r
+\r
+    public List<String> getNames() {\r
+        return names;\r
+    }\r
+\r
+    public void setNames(List<String> names) {\r
+        this.names = names;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndpointNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/EndpointNode.java
new file mode 100644 (file)
index 0000000..06b50cd
--- /dev/null
@@ -0,0 +1,22 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class EndpointNode extends NodeTemplateYaml {\r
+    private EndpointConfigurationYaml properties;\r
+\r
+    public EndpointConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(EndpointConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/FieldModifierConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/FieldModifierConfigurationYaml.java
new file mode 100644 (file)
index 0000000..64ff4c7
--- /dev/null
@@ -0,0 +1,26 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.List;\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class FieldModifierConfigurationYaml implements ConfigurationYaml{\r
+    private List<String> names;\r
+\r
+    public List<String> getNames() {\r
+        return names;\r
+    }\r
+\r
+    public void setNames(List<String> names) {\r
+        this.names = names;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/FieldModifierNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/FieldModifierNode.java
new file mode 100644 (file)
index 0000000..f805337
--- /dev/null
@@ -0,0 +1,21 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class FieldModifierNode extends NodeTemplateYaml {\r
+    private FieldModifierConfigurationYaml properties;\r
+\r
+    public FieldModifierConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(FieldModifierConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/FirewallConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/FirewallConfigurationYaml.java
new file mode 100644 (file)
index 0000000..85634d4
--- /dev/null
@@ -0,0 +1,24 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.Map;\r
+\r
+public class FirewallConfigurationYaml implements ConfigurationYaml{\r
+    private Map<String, String> elements;\r
+\r
+    public Map<String, String> getElements() {\r
+        return elements;\r
+    }\r
+\r
+    public void setElements(Map<String, String> elements) {\r
+        this.elements = elements;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/FirewallNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/FirewallNode.java
new file mode 100644 (file)
index 0000000..d8952ae
--- /dev/null
@@ -0,0 +1,22 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class FirewallNode extends NodeTemplateYaml {\r
+    private FirewallConfigurationYaml properties;\r
+\r
+    public FirewallConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(FirewallConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailClientConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailClientConfigurationYaml.java
new file mode 100644 (file)
index 0000000..2120c4d
--- /dev/null
@@ -0,0 +1,24 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class MailClientConfigurationYaml implements ConfigurationYaml {\r
+    private String mailserver;\r
+\r
+    public String getMailserver() {\r
+        return mailserver;\r
+    }\r
+\r
+    public void setMailserver(String mailserver) {\r
+        this.mailserver = mailserver;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailClientNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailClientNode.java
new file mode 100644 (file)
index 0000000..fa75154
--- /dev/null
@@ -0,0 +1,21 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class MailClientNode extends NodeTemplateYaml {\r
+    private MailClientConfigurationYaml properties;\r
+\r
+    public MailClientConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(MailClientConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailServerConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailServerConfigurationYaml.java
new file mode 100644 (file)
index 0000000..3672727
--- /dev/null
@@ -0,0 +1,26 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.List;\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class MailServerConfigurationYaml implements ConfigurationYaml {\r
+    private List<String> names;\r
+\r
+    public List<String> getNames() {\r
+        return names;\r
+    }\r
+\r
+    public void setNames(List<String> names) {\r
+        this.names = names;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailServerNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/MailServerNode.java
new file mode 100644 (file)
index 0000000..6f7d493
--- /dev/null
@@ -0,0 +1,21 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class MailServerNode extends NodeTemplateYaml {\r
+    private MailServerConfigurationYaml properties;\r
+\r
+    public MailServerConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(MailServerConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/NatConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/NatConfigurationYaml.java
new file mode 100644 (file)
index 0000000..49d34c8
--- /dev/null
@@ -0,0 +1,27 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.List;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class NatConfigurationYaml implements ConfigurationYaml {\r
+    private List<String> sources;\r
+\r
+    public List<String> getSources() {\r
+        return sources;\r
+    }\r
+\r
+    public void setSources(List<String> sources) {\r
+        this.sources = sources;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/NatNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/NatNode.java
new file mode 100644 (file)
index 0000000..cd30172
--- /dev/null
@@ -0,0 +1,21 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class NatNode extends NodeTemplateYaml {\r
+    private NatConfigurationYaml properties;\r
+\r
+    public NatConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(NatConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/NodeTemplateYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/NodeTemplateYaml.java
new file mode 100644 (file)
index 0000000..9821f7c
--- /dev/null
@@ -0,0 +1,55 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+import com.fasterxml.jackson.annotation.*;\r
+\r
+//These annotations allow polimorphic deserialization of yaml text into beans by using the type field of each node\r
+//In case a specified type is unknown the default implementation will be FieldModifierNode\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.PROPERTY, property="type", defaultImpl=FieldModifierNode.class, visible= true)\r
+@JsonSubTypes({\r
+    @JsonSubTypes.Type(value = AntispamNode.class, name="verigraph.nodeTypes.Antispam"),\r
+    @JsonSubTypes.Type(value = CacheNode.class, name="verigraph.nodeTypes.Cache"),\r
+    @JsonSubTypes.Type(value = DpiNode.class, name="verigraph.nodeTypes.Dpi"),\r
+    @JsonSubTypes.Type(value = EndhostNode.class, name="verigraph.nodeTypes.Endhost"),\r
+    @JsonSubTypes.Type(value = EndpointNode.class, name="verigraph.nodeTypes.Endpoint"),\r
+    @JsonSubTypes.Type(value = FieldModifierNode.class, name="verigraph.nodeTypes.FieldModifier"),\r
+    @JsonSubTypes.Type(value = FirewallNode.class, name="verigraph.nodeTypes.Firewall"),\r
+    @JsonSubTypes.Type(value = MailClientNode.class, name="verigraph.nodeTypes.MailClient"),\r
+    @JsonSubTypes.Type(value = MailServerNode.class, name="verigraph.nodeTypes.MailServer"),\r
+    @JsonSubTypes.Type(value = NatNode.class, name="verigraph.nodeTypes.Nat"),\r
+    @JsonSubTypes.Type(value = VpnAccessNode.class, name="verigraph.nodeTypes.VpnAccess"),\r
+    @JsonSubTypes.Type(value = VpnExitNode.class, name="verigraph.nodeTypes.VpnExit"),\r
+    @JsonSubTypes.Type(value = WebClientNode.class, name="verigraph.nodeTypes.WebClient"),\r
+    @JsonSubTypes.Type(value = WebServerNode.class, name="verigraph.nodeTypes.WebServer")\r
+})\r
+public class NodeTemplateYaml {\r
+    private String name;\r
+    private String type;\r
+\r
+    public String getName() {\r
+        return name;\r
+    }\r
+    public void setName(String name) {\r
+        this.name = name;\r
+    }\r
+    public String getType() {\r
+        return type;\r
+    }\r
+    public void setType(String type) {\r
+        this.type = type;\r
+    }\r
+\r
+    // //Generic YamlNode configuration to be extended in single nodes\r
+    // public interface ConfigurationYaml {\r
+    // }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/RelationshipTemplateYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/RelationshipTemplateYaml.java
new file mode 100644 (file)
index 0000000..d828775
--- /dev/null
@@ -0,0 +1,30 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+import java.util.Map;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class RelationshipTemplateYaml {\r
+    private String type;\r
+    private Map<String, String> properties;\r
+    public String getType() {\r
+        return type;\r
+    }\r
+    public void setType(String type) {\r
+        this.type = type;\r
+    }\r
+    public Map<String, String> getProperties() {\r
+        return properties;\r
+    }\r
+    public void setProperties(Map<String, String> properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/ServiceTemplateYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/ServiceTemplateYaml.java
new file mode 100644 (file)
index 0000000..0955a7d
--- /dev/null
@@ -0,0 +1,39 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.Map;\r
+import com.fasterxml.jackson.annotation.*;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class ServiceTemplateYaml {\r
+    private Map<String, String> metadata;\r
+    private String description;\r
+    private TopologyTemplateYaml topology_template;\r
+\r
+    public Map<String, String> getMetadata() {\r
+        return metadata;\r
+    }\r
+    public void setMetadata(Map<String, String> metadata) {\r
+        this.metadata = metadata;\r
+    }\r
+    public String getDescription() {\r
+        return description;\r
+    }\r
+    public void setDescription(String description) {\r
+        this.description = description;\r
+    }\r
+    public TopologyTemplateYaml getTopology_template() {\r
+        return topology_template;\r
+    }\r
+    public void setTopology_template(TopologyTemplateYaml topology_template) {\r
+        this.topology_template = topology_template;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/TopologyTemplateYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/TopologyTemplateYaml.java
new file mode 100644 (file)
index 0000000..2653697
--- /dev/null
@@ -0,0 +1,35 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.Map;\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class TopologyTemplateYaml {\r
+    private Map<String, NodeTemplateYaml> node_templates;\r
+    private Map<String, RelationshipTemplateYaml> relationship_templates;\r
+\r
+    public Map<String, RelationshipTemplateYaml> getRelationship_templates() {\r
+        return relationship_templates;\r
+    }\r
+\r
+    public void setRelationship_templates(Map<String, RelationshipTemplateYaml> relationship_templates) {\r
+        this.relationship_templates = relationship_templates;\r
+    }\r
+\r
+    public Map<String, NodeTemplateYaml> getNode_templates() {\r
+        return node_templates;\r
+    }\r
+\r
+    public void setNode_templates(Map<String, NodeTemplateYaml> node_templates) {\r
+        this.node_templates = node_templates;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VerificationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VerificationYaml.java
new file mode 100644 (file)
index 0000000..2f572a6
--- /dev/null
@@ -0,0 +1,58 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Politecnico di Torino and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Apache License, Version 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *******************************************************************************/
+package it.polito.verigraph.tosca.yaml.beans;
+
+import java.util.List;
+import com.fasterxml.jackson.annotation.*;
+
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class VerificationYaml {
+    /*    private Map<String, String> metadata;
+        private String description;*/
+    private String result;
+    private String comment;
+    private List<ServiceTemplateYaml> paths;
+
+    /*    public Map<String, String> getMetadata() {
+        return metadata;
+    }
+    public void setMetadata(Map<String, String> metadata) {
+        this.metadata = metadata;
+    }
+    public String getDescription() {
+        return description;
+    }
+    public void setDescription(String description) {
+        this.description = description;
+    }*/
+    public String getResult() {
+        return result;
+    }
+
+    public void setResult(String result) {
+        this.result = result;
+    }
+
+    public String getComment() {
+        return comment;
+    }
+
+    public void setComment(String comment) {
+        this.comment = comment;
+    }
+
+    public List<ServiceTemplateYaml> getPaths() {
+        return paths;
+    }
+
+    public void setPaths(List<ServiceTemplateYaml> paths) {
+        this.paths = paths;
+    }
+
+}
\ No newline at end of file
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnAccessConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnAccessConfigurationYaml.java
new file mode 100644 (file)
index 0000000..807059f
--- /dev/null
@@ -0,0 +1,25 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class VpnAccessConfigurationYaml implements ConfigurationYaml {\r
+    private String vpnexit;\r
+\r
+    public String getVpnexit() {\r
+        return vpnexit;\r
+    }\r
+\r
+    public void setVpnexit(String vpnexit) {\r
+        this.vpnexit = vpnexit;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnAccessNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnAccessNode.java
new file mode 100644 (file)
index 0000000..9058615
--- /dev/null
@@ -0,0 +1,21 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class VpnAccessNode extends NodeTemplateYaml{\r
+    private VpnAccessConfigurationYaml properties;\r
+\r
+    public VpnAccessConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(VpnAccessConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnExitConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnExitConfigurationYaml.java
new file mode 100644 (file)
index 0000000..090e337
--- /dev/null
@@ -0,0 +1,24 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class VpnExitConfigurationYaml implements ConfigurationYaml {\r
+    private String vpnaccess;\r
+\r
+    public String getVpnaccess() {\r
+        return vpnaccess;\r
+    }\r
+\r
+    public void setVpnaccess(String vpnaccess) {\r
+        this.vpnaccess = vpnaccess;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnExitNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/VpnExitNode.java
new file mode 100644 (file)
index 0000000..775b8d3
--- /dev/null
@@ -0,0 +1,22 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class VpnExitNode extends NodeTemplateYaml {\r
+    private VpnExitConfigurationYaml properties;\r
+\r
+    public VpnExitConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(VpnExitConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebClientConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebClientConfigurationYaml.java
new file mode 100644 (file)
index 0000000..02825ed
--- /dev/null
@@ -0,0 +1,25 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\r
+\r
+@JsonIgnoreProperties(ignoreUnknown = true)\r
+public class WebClientConfigurationYaml implements ConfigurationYaml{\r
+    private String nameWebServer;\r
+\r
+    public String getNameWebServer() {\r
+        return nameWebServer;\r
+    }\r
+\r
+    public void setNameWebServer(String nameWebServer) {\r
+        this.nameWebServer = nameWebServer;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebClientNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebClientNode.java
new file mode 100644 (file)
index 0000000..5e01194
--- /dev/null
@@ -0,0 +1,21 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class WebClientNode extends NodeTemplateYaml {\r
+    private WebClientConfigurationYaml properties;\r
+\r
+    public WebClientConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(WebClientConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebServerConfigurationYaml.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebServerConfigurationYaml.java
new file mode 100644 (file)
index 0000000..1f37c09
--- /dev/null
@@ -0,0 +1,24 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+import java.util.List;\r
+\r
+public class WebServerConfigurationYaml implements ConfigurationYaml{\r
+    public List<String> names;\r
+\r
+    public List<String> getNames() {\r
+        return names;\r
+    }\r
+\r
+    public void setNames(List<String> names) {\r
+        this.names = names;\r
+    }\r
+\r
+}\r
diff --git a/verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebServerNode.java b/verigraph/src/it/polito/verigraph/tosca/yaml/beans/WebServerNode.java
new file mode 100644 (file)
index 0000000..5340b77
--- /dev/null
@@ -0,0 +1,21 @@
+/*******************************************************************************\r
+ * Copyright (c) 2018 Politecnico di Torino and others.\r
+ *\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Apache License, Version 2.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *******************************************************************************/\r
+package it.polito.verigraph.tosca.yaml.beans;\r
+\r
+public class WebServerNode extends NodeTemplateYaml {\r
+    private WebServerConfigurationYaml properties;\r
+\r
+    public WebServerConfigurationYaml getProperties() {\r
+        return properties;\r
+    }\r
+\r
+    public void setProperties(WebServerConfigurationYaml properties) {\r
+        this.properties = properties;\r
+    }\r
+}\r