summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/tosca/converter
diff options
context:
space:
mode:
Diffstat (limited to 'verigraph/src/it/polito/verigraph/tosca/converter')
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/grpc/GraphToGrpc.java101
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java150
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToXml.java145
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToYaml.java284
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/grpc/ToscaGrpcUtils.java18
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/grpc/XmlToGrpc.java160
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/grpc/YamlToGrpc.java147
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/xml/GraphToXml.java134
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/xml/XmlToGraph.java167
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/yaml/GraphToYaml.java270
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/converter/yaml/YamlToGraph.java138
11 files changed, 1714 insertions, 0 deletions
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GraphToGrpc.java b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GraphToGrpc.java
new file mode 100644
index 0000000..fb52c7b
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GraphToGrpc.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * 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.converter.grpc;
+
+import java.util.Map;
+
+import it.polito.verigraph.grpc.NodeTemplateGrpc;
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
+import it.polito.verigraph.grpc.ToscaTestGrpc;
+import it.polito.verigraph.grpc.ToscaVerificationGrpc;
+import it.polito.verigraph.model.Configuration;
+import it.polito.verigraph.model.Graph;
+import it.polito.verigraph.model.Neighbour;
+import it.polito.verigraph.model.Node;
+import it.polito.verigraph.model.Test;
+import it.polito.verigraph.model.Verification;
+
+public class GraphToGrpc {
+
+ /** Mapping method --> from model Graph to grpc TopologyTemplate */
+ public static TopologyTemplateGrpc obtainTopologyTemplate(Graph graph) {
+ TopologyTemplateGrpc.Builder topol = TopologyTemplateGrpc.newBuilder();
+ topol.setId(String.valueOf(graph.getId()));
+
+ //NodeTemplate
+ for(Node node : graph.getNodes().values()) {
+ NodeTemplateGrpc nt = obtainNodeTemplate(node);
+ topol.addNodeTemplate(nt);
+ //RelationshipTemplate
+ Map<Long,Neighbour> neighMap = node.getNeighbours();
+ for (Map.Entry<Long, Neighbour> myentry : neighMap.entrySet()) {
+ Neighbour neigh = myentry.getValue();
+ RelationshipTemplateGrpc relat = obtainRelationshipTemplate(neigh, node);
+ topol.addRelationshipTemplate(relat);
+ }
+ }
+ return topol.build();
+ }
+
+
+ /** Mapping method --> from model Node to grpc NodeTemplate */
+ private static NodeTemplateGrpc obtainNodeTemplate(Node node){
+ NodeTemplateGrpc.Builder nodegrpc = NodeTemplateGrpc.newBuilder();
+
+ nodegrpc.setId(String.valueOf(node.getId()));
+ nodegrpc.setName(node.getName());
+ nodegrpc.setType(NodeTemplateGrpc.Type.valueOf(node.getFunctional_type().toLowerCase()));
+
+ ToscaConfigurationGrpc config = obtainToscaConfiguration(node.getConfiguration());
+ nodegrpc.setConfiguration(config);
+
+ return nodegrpc.build();
+ }
+
+
+ /** Mapping method --> from model Neighbour to grpc RelationshipTemplate */
+ private static RelationshipTemplateGrpc obtainRelationshipTemplate(Neighbour neigh, Node sourceNode) {
+ RelationshipTemplateGrpc.Builder relat = RelationshipTemplateGrpc.newBuilder();
+ relat.setId(String.valueOf(sourceNode.getId()));
+ //Neighbour does not have a neighbourID! RelationshipTemplate does, so it is set to sourceNodeID
+ relat.setIdSourceNodeTemplate(String.valueOf(sourceNode.getId()));
+ relat.setIdTargetNodeTemplate(String.valueOf(neigh.getId()));
+ relat.setName(sourceNode.getName()+"to"+neigh.getName());
+ return relat.build();
+ }
+
+
+ /** Mapping method --> from model Configuration to grpc ToscaConfigurationGrpc */
+ private static ToscaConfigurationGrpc obtainToscaConfiguration(Configuration conf) {
+ return ToscaConfigurationGrpc.newBuilder()
+ .setId(conf.getId())
+ .setDescription(conf.getDescription())
+ .setConfiguration(conf.getConfiguration().toString())
+ .build();
+ }
+
+ /** Mapping method --> from model Verification to grpc ToscaVerificationGrpc */
+ public static ToscaVerificationGrpc obtainToscaVerification(Verification verify){
+ ToscaVerificationGrpc.Builder ver = ToscaVerificationGrpc.newBuilder();
+ ver.setComment(verify.getComment());
+ ver.setResult(verify.getResult());
+ for(Test test:verify.getTests()){
+ ToscaTestGrpc.Builder tst = ToscaTestGrpc.newBuilder().setResult(test.getResult());
+ for(Node node:test.getPath()){
+ NodeTemplateGrpc nodetempl = obtainNodeTemplate(node);
+ tst.addNodeTemplate(nodetempl);
+ }
+ ver.addTest(tst);
+ }
+ return ver.build();
+ }
+
+}
diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java
new file mode 100644
index 0000000..76906b3
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java
@@ -0,0 +1,150 @@
+/*******************************************************************************
+ * 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.converter.grpc;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.grpc.NodeTemplateGrpc;
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
+import it.polito.verigraph.model.Configuration;
+import it.polito.verigraph.model.Graph;
+import it.polito.verigraph.model.Neighbour;
+import it.polito.verigraph.model.Node;
+
+public class GrpcToGraph {
+
+ /** Mapping method --> from grpc TopologyTemplateGrpc to model Graph */
+ public static Graph deriveGraph(TopologyTemplateGrpc request) throws BadRequestException, JsonProcessingException, IOException {
+ Graph graph = new Graph();
+ Map<Long, Node> nodes = new HashMap<>();
+
+ try {
+ //Create a list of Node without Neighbour
+ for(NodeTemplateGrpc nodetempl : request.getNodeTemplateList()){
+ Node node = deriveNode(nodetempl);
+ //It necessary to check uniqueness here otherwise a .put with the same key will overwrite the old node
+ if(nodes.containsKey(node.getId()))
+ throw new BadRequestException("The NodeTemplate ID must be unique.");
+ else
+ nodes.put(node.getId(), node);
+ }
+
+ //Add Neighbour to the Node of the list
+ List<RelationshipTemplateGrpc> relatList = request.getRelationshipTemplateList();
+ nodes = deriveNeighboursNode(nodes, relatList);
+
+ //Add Node and ID to the graph
+ graph.setNodes(nodes);
+ try {
+ graph.setId(Long.valueOf(request.getId()));
+ } catch(NumberFormatException ex) {
+ throw new BadRequestException("If you want to store your TopologyTemplate on this server,"
+ + "the TopologyTemplate ID must be a number.");
+ }
+
+ return graph;
+
+ } catch (NullPointerException e) {
+ throw new BadRequestException("The TopologyTemplate received has invalid fields.");
+ }
+
+ }
+
+
+ /** Mapping method --> from grpc NodeTemplate to model Node (with no Neighbour) */
+ private static Node deriveNode(NodeTemplateGrpc nodegrpc) throws BadRequestException, JsonProcessingException, IOException {
+ Node node = new Node();
+ try {
+ try {
+ node.setId(Long.valueOf(nodegrpc.getId()));
+ } catch(NumberFormatException ex) {
+ throw new BadRequestException("The NodeTemplate ID must be a number.");
+ }
+
+ node.setName(nodegrpc.getName());
+ Configuration conf = deriveConfiguration(nodegrpc.getConfiguration());
+ node.setConfiguration(conf);
+ node.setFunctional_type(nodegrpc.getType().toString());
+
+ } catch(NullPointerException ex) {
+ throw new BadRequestException("A NodeTemplate has wrong fields representation.");
+ }
+
+ return node;
+ }
+
+
+
+ /** Mapping method --> from a list of model Node to a list of model Node with their Neighbour */
+ private static Map<Long,Node> deriveNeighboursNode(Map<Long,Node> nodes, List<RelationshipTemplateGrpc> relatList)
+ throws BadRequestException{
+ Map<Long,Node> updNodes = nodes; //new list to be filled with updated Node (update = Node + its Neighbour)
+ for(RelationshipTemplateGrpc relat : relatList) {
+ try {
+ //Retrieve the target Node name and generate a new Neighbour
+ String neighName = updNodes.get(Long.valueOf(relat.getIdTargetNodeTemplate())).getName();
+ Neighbour neigh = new Neighbour();
+ neigh.setName(neighName);
+ neigh.setId(Long.valueOf(relat.getId()));
+
+ //Retrieve the Neighbour map of the source Node and add the Neighbour
+ Node source = updNodes.get(Long.valueOf(relat.getIdSourceNodeTemplate()));
+ Map<Long,Neighbour> sourceNodeNeighMap = source.getNeighbours();
+ if(sourceNodeNeighMap.containsKey(neigh.getId()))
+ throw new BadRequestException("The RelationshipTemplate ID must be unique.");
+ else
+ sourceNodeNeighMap.put(neigh.getId(), neigh);
+ source.setNeighbours(sourceNodeNeighMap);
+
+ //Update the Node list
+ updNodes.put(Long.valueOf(relat.getIdSourceNodeTemplate()), source);
+ } catch(NullPointerException | NumberFormatException ex) {
+ throw new BadRequestException("A RelationshipTemplate has wrong fields representation.");
+ }
+ }
+ return updNodes;
+ }
+
+ /** Mapping method --> from ToscaConfiguration to model Configuration */
+ private static Configuration deriveConfiguration(ToscaConfigurationGrpc request)
+ throws BadRequestException, JsonProcessingException, IOException {
+ Configuration conf = new Configuration();
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode rootNode = null;
+
+ try {
+ conf.setId(request.getId());
+ } catch (NullPointerException e) {}
+
+ try {
+ conf.setDescription(request.getDescription());
+ } catch (NullPointerException e) {}
+
+ try {
+ if ("".equals(request.getConfiguration()))
+ rootNode=mapper.readTree("[]");
+ else
+ rootNode = mapper.readTree(request.getConfiguration());
+ } catch (NullPointerException e) {
+ rootNode=mapper.readTree("[]");
+ }
+ conf.setConfiguration(rootNode);
+ return conf;
+ }
+}
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
index 0000000..002737a
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToXml.java
@@ -0,0 +1,145 @@
+/*******************************************************************************
+ * 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.converter.grpc;
+
+import java.io.IOException;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import it.polito.tosca.jaxb.Configuration;
+import it.polito.tosca.jaxb.Definitions;
+import it.polito.tosca.jaxb.TEntityTemplate.Properties;
+import it.polito.tosca.jaxb.TNodeTemplate;
+import it.polito.tosca.jaxb.TRelationshipTemplate;
+import it.polito.tosca.jaxb.TRelationshipTemplate.SourceElement;
+import it.polito.tosca.jaxb.TRelationshipTemplate.TargetElement;
+import it.polito.tosca.jaxb.TServiceTemplate;
+import it.polito.tosca.jaxb.TTopologyTemplate;
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.grpc.NodeTemplateGrpc;
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
+import it.polito.verigraph.tosca.MappingUtils;
+
+public class GrpcToXml {
+
+ public static Definitions mapGraph(TopologyTemplateGrpc topologyGrpc) {
+ Definitions definitions = new Definitions();
+ TServiceTemplate serviceTemplate = new TServiceTemplate();
+ TTopologyTemplate topologyTemplate = new TTopologyTemplate();
+
+ for(NodeTemplateGrpc node : topologyGrpc.getNodeTemplateList()) {
+ TNodeTemplate nodeTemplate = mapNode(node);
+ topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(nodeTemplate);
+ }
+ for(RelationshipTemplateGrpc relat : topologyGrpc.getRelationshipTemplateList()) {
+ TRelationshipTemplate relationshipTemplate = mapRelationship(relat, topologyGrpc.getNodeTemplateList());
+ topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(relationshipTemplate);
+ }
+
+ try {
+ serviceTemplate.setId(String.valueOf(topologyGrpc.getId()));
+ } catch (NullPointerException e) {
+ throw new NullPointerException("The TopologyTemplateGrpc must have an ID.");
+ }
+ serviceTemplate.setTopologyTemplate(topologyTemplate);
+ definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(serviceTemplate);
+ return definitions;
+ }
+
+
+ private static TNodeTemplate mapNode(NodeTemplateGrpc node){
+ TNodeTemplate nodeTemplate = new TNodeTemplate();
+
+ try {
+ nodeTemplate.setId(String.valueOf(node.getId()));
+ } catch (NullPointerException e) {
+ throw new NullPointerException("The NodeTemplateGrpc must have an ID.");
+ }
+ try {
+ nodeTemplate.setName(node.getName());
+ } catch (NullPointerException e) {
+ throw new NullPointerException("The NodeTemplateGrpc must have a name.");
+ }
+
+ try {
+ //QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12/ToscaVerigraphDefinition")
+ QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12",
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1) + "Type");
+ nodeTemplate.setType(type);
+ } catch (NullPointerException e) {
+ throw new NullPointerException("The NodeTemplateGrpc must have a valid type.");
+ }
+
+ Configuration config = mapModelConfiguration(node.getConfiguration(), node.getType().toString().toLowerCase());
+ nodeTemplate.setProperties(new Properties());
+ nodeTemplate.getProperties().setAny(config);
+ return nodeTemplate;
+ }
+
+
+ private static TRelationshipTemplate mapRelationship(RelationshipTemplateGrpc relat, List<NodeTemplateGrpc> nodeList) {
+ TRelationshipTemplate relationship = new TRelationshipTemplate();
+ SourceElement source = new SourceElement();
+ TargetElement target = new TargetElement();
+ int check = 0;
+
+ TNodeTemplate sourceNode = new TNodeTemplate();
+ TNodeTemplate targetNode = new TNodeTemplate();
+
+ try {
+ for(NodeTemplateGrpc node : nodeList) {
+ if(node.getId().equals(relat.getIdSourceNodeTemplate())) {
+ sourceNode = mapNode(node);
+ check++;
+ }
+ if(node.getId().equals(relat.getIdTargetNodeTemplate())) {
+ targetNode = mapNode(node);
+ check++;
+ }
+ }
+ } catch (NullPointerException e) {
+ throw new BadRequestException("A RelationshipTemplateGrpc must contain both source and target node ID.");
+ }
+ if(check != 2)
+ throw new BadRequestException("A RelationshipTemplateGrpc must contain both source and target node ID.");
+
+ source.setRef(sourceNode);
+ target.setRef(targetNode);
+
+ relationship.setId(relat.getId()); //TODO da valutare
+ relationship.setSourceElement(source);
+ relationship.setTargetElement(target);
+ relationship.setName(sourceNode.getName()+"To"+targetNode.getName());
+
+ return relationship;
+ }
+
+
+ private static it.polito.tosca.jaxb.Configuration mapModelConfiguration(ToscaConfigurationGrpc toscaConfigurationGrpc, String type) {
+ it.polito.tosca.jaxb.Configuration configuration = new it.polito.tosca.jaxb.Configuration();
+ try {
+ //We are passing the configuration type to the Deserializer context
+ configuration = MappingUtils.obtainToscaConfiguration(toscaConfigurationGrpc, type);
+
+ //In Graph, ID and DESCRIPTION are always empty
+ //configuration.setConfID(confGrpc.getId());
+ //configuration.setConfDescr(confGrpc.getDescription());
+
+ } catch (IOException | NullPointerException e) {
+ e.printStackTrace();
+ }
+ return configuration;
+ }
+
+}
+
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
index 0000000..64f8ae4
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToYaml.java
@@ -0,0 +1,284 @@
+/*******************************************************************************
+ * 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.converter.grpc;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.InjectableValues;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+
+import it.polito.neo4j.jaxb.FunctionalTypes;
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.grpc.NodeTemplateGrpc;
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;
+import it.polito.verigraph.tosca.deserializer.YamlConfigurationDeserializer;
+import it.polito.verigraph.tosca.yaml.beans.AntispamConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.AntispamNode;
+import it.polito.verigraph.tosca.yaml.beans.CacheConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.CacheNode;
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.DpiConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.DpiNode;
+import it.polito.verigraph.tosca.yaml.beans.EndhostConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.EndhostNode;
+import it.polito.verigraph.tosca.yaml.beans.EndpointConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.EndpointNode;
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierNode;
+import it.polito.verigraph.tosca.yaml.beans.FirewallConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.FirewallNode;
+import it.polito.verigraph.tosca.yaml.beans.MailClientConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.MailClientNode;
+import it.polito.verigraph.tosca.yaml.beans.MailServerConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.MailServerNode;
+import it.polito.verigraph.tosca.yaml.beans.NatConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.NatNode;
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.TopologyTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessNode;
+import it.polito.verigraph.tosca.yaml.beans.VpnExitConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.VpnExitNode;
+import it.polito.verigraph.tosca.yaml.beans.WebClientConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.WebClientNode;
+import it.polito.verigraph.tosca.yaml.beans.WebServerConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.WebServerNode;
+
+public class GrpcToYaml {
+ public static ServiceTemplateYaml mapGraphYaml(TopologyTemplateGrpc topologyGrpc) {
+ ServiceTemplateYaml serviceYaml = new ServiceTemplateYaml();
+ TopologyTemplateYaml topologyYaml = new TopologyTemplateYaml();
+
+ topologyYaml.setNode_templates(new HashMap<String,NodeTemplateYaml>());
+ topologyYaml.setRelationship_templates(new HashMap<String,RelationshipTemplateYaml>());
+ serviceYaml.setMetadata(new HashMap<String,String>());
+
+ for(NodeTemplateGrpc node : topologyGrpc.getNodeTemplateList()) {
+ NodeTemplateYaml nodeTemplate = new NodeTemplateYaml();
+ try {
+ nodeTemplate = mapNodeYaml(node);
+ } catch (IOException e) {
+ throw new BadRequestException("Error while mapping a Node in Yaml object.");
+ }
+ topologyYaml.getNode_templates().put(String.valueOf(node.getId()), nodeTemplate);
+ //shall we catch NumberFormatException?
+ }
+ for(RelationshipTemplateGrpc relationship : topologyGrpc.getRelationshipTemplateList()) {
+ RelationshipTemplateYaml rel = mapRelationshipYaml(relationship, topologyGrpc.getNodeTemplateList());
+ topologyYaml.getRelationship_templates().put(String.valueOf(relationship.getId()), rel);
+ }
+
+ serviceYaml.getMetadata().put("template_id", String.valueOf(topologyGrpc.getId()));
+ serviceYaml.setTopology_template(topologyYaml);
+ return serviceYaml;
+ }
+
+
+ private static NodeTemplateYaml mapNodeYaml(NodeTemplateGrpc node) throws JsonParseException, JsonMappingException, IOException {
+
+ ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
+ SimpleModule module = new SimpleModule();
+ String stringConfig = null;
+ try {
+ stringConfig = node.getConfiguration().getConfiguration();
+ } catch (NullPointerException e) {
+ throw new NullPointerException("A NodeTemplateGrpc does not contain the configuration.");
+ }
+ JsonNode nodeConfig = mapper.readTree(stringConfig);
+ //Passing the configuration type to the Deserializer context
+ module.addDeserializer(ConfigurationYaml.class, new YamlConfigurationDeserializer());
+ mapper.registerModule(module);
+
+ if(node.getType() == null)
+ throw new NullPointerException("A NodeTemplateGrpc does not contain a type.");
+ ConfigurationYaml yamlConfig = mapper
+ .reader(new InjectableValues.Std().addValue("type", node.getType().toString().toLowerCase()))
+ .forType(ConfigurationYaml.class)
+ .readValue(nodeConfig);
+
+
+ FunctionalTypes nodeType = FunctionalTypes.valueOf(node.getType().toString().toUpperCase());
+ switch(nodeType) {
+ case ANTISPAM:
+ AntispamNode antispamNode = new AntispamNode();
+ antispamNode.setName(node.getName());
+ antispamNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ antispamNode.setProperties((AntispamConfigurationYaml) yamlConfig);
+ return antispamNode;
+
+ case CACHE:
+ CacheNode cacheNode = new CacheNode();
+ cacheNode.setName(node.getName());
+ cacheNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ cacheNode.setProperties((CacheConfigurationYaml) yamlConfig);
+ return cacheNode;
+
+ case DPI:
+ DpiNode dpiNode = new DpiNode();
+ dpiNode.setName(node.getName());
+ dpiNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ dpiNode.setProperties((DpiConfigurationYaml) yamlConfig);
+ return dpiNode;
+
+ case ENDHOST:
+ EndhostNode endhostNode = new EndhostNode();
+ endhostNode.setName(node.getName());
+ endhostNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ endhostNode.setProperties((EndhostConfigurationYaml) yamlConfig);
+ return endhostNode;
+
+ case ENDPOINT:
+ EndpointNode endpointNode = new EndpointNode();
+ endpointNode.setName(node.getName());
+ endpointNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ endpointNode.setProperties((EndpointConfigurationYaml) yamlConfig);
+ return endpointNode;
+
+ case FIELDMODIFIER:
+ FieldModifierNode fieldmodifierNode = new FieldModifierNode();
+ fieldmodifierNode.setName(node.getName());
+ fieldmodifierNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ fieldmodifierNode.setProperties((FieldModifierConfigurationYaml) yamlConfig);
+ return fieldmodifierNode;
+
+ case FIREWALL:
+ FirewallNode firewallNode = new FirewallNode();
+ firewallNode.setName(node.getName());
+ firewallNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ firewallNode.setProperties((FirewallConfigurationYaml) yamlConfig);
+ return firewallNode;
+
+ case MAILCLIENT:
+ MailClientNode mailclientNode = new MailClientNode();
+ mailclientNode.setName(node.getName());
+ mailclientNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ mailclientNode.setProperties((MailClientConfigurationYaml) yamlConfig);
+ return mailclientNode;
+
+ case MAILSERVER:
+ MailServerNode mailserverNode = new MailServerNode();
+ mailserverNode.setName(node.getName());
+ mailserverNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ mailserverNode.setProperties((MailServerConfigurationYaml) yamlConfig);
+ return mailserverNode;
+
+ case NAT:
+ NatNode natNode = new NatNode();
+ natNode.setName(node.getName());
+ natNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ natNode.setProperties((NatConfigurationYaml) yamlConfig);
+ return natNode;
+
+ case VPNACCESS:
+ VpnAccessNode vpnaccessNode = new VpnAccessNode();
+ vpnaccessNode.setName(node.getName());
+ vpnaccessNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ vpnaccessNode.setProperties((VpnAccessConfigurationYaml) yamlConfig);
+ return vpnaccessNode;
+
+ case VPNEXIT:
+ VpnExitNode vpnexitNode = new VpnExitNode();
+ vpnexitNode.setName(node.getName());
+ vpnexitNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ vpnexitNode.setProperties((VpnExitConfigurationYaml) yamlConfig);
+ return vpnexitNode;
+
+ case WEBCLIENT:
+ WebClientNode webclientNode = new WebClientNode();
+ webclientNode.setName(node.getName());
+ webclientNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ webclientNode.setProperties((WebClientConfigurationYaml) yamlConfig);
+ return webclientNode;
+
+ case WEBSERVER:
+ WebServerNode webserverNode = new WebServerNode();
+ webserverNode.setName(node.getName());
+ webserverNode.setType("verigraph.nodeTypes." +
+ node.getType().toString().substring(0, 1).toUpperCase() +
+ node.getType().toString().substring(1));
+ webserverNode.setProperties((WebServerConfigurationYaml) yamlConfig);
+ return webserverNode;
+
+ default:
+ FieldModifierNode defaultNode = new FieldModifierNode();
+ defaultNode.setName(node.getName());
+ defaultNode.setType("verigraph.nodeTypes.Fieldmodifier");
+ defaultNode.setProperties(new FieldModifierConfigurationYaml());
+ return defaultNode;
+ }
+
+ }
+
+ private static RelationshipTemplateYaml mapRelationshipYaml(RelationshipTemplateGrpc relat, List<NodeTemplateGrpc> nodeList) {
+ RelationshipTemplateYaml relationship = new RelationshipTemplateYaml();
+ relationship.setProperties(new HashMap<String,String>());
+ String sourceNode = null;
+ String targetNode = null;
+ int check = 0;
+
+ relationship.setType("verigraph.relationshipType.generic");
+ relationship.getProperties().put("source_id", String.valueOf(relat.getIdSourceNodeTemplate())); //to be catched?
+ relationship.getProperties().put("target_id", String.valueOf(relat.getIdTargetNodeTemplate()));
+
+ for(NodeTemplateGrpc node : nodeList) {
+ if(node.getId().equals(relat.getIdSourceNodeTemplate())) {
+ sourceNode = node.getName();
+ check++;
+ }
+ if(node.getId().equals(relat.getIdTargetNodeTemplate())) {
+ targetNode = node.getName();
+ check++;
+ }
+ }
+
+ if(check!=2)
+ throw new BadRequestException("A RelationshipTemplateGrpc must contain both source and target node ID.");
+
+ relationship.getProperties().put("name", sourceNode+"To"+targetNode);
+
+ return relationship;
+ }
+}
+
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
index 0000000..e43ef21
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/ToscaGrpcUtils.java
@@ -0,0 +1,18 @@
+/*******************************************************************************
+ * 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.converter.grpc;
+
+public class ToscaGrpcUtils {
+
+ /** Default configuration for a Tosca NodeTemplate non compliant with Verigraph types*/
+ public static final String defaultConfID = new String("");
+ public static final String defaultDescr = new String("Default Configuration");
+ public static final String defaultConfig = new String("[]");
+
+}
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
index 0000000..426bb4a
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/XmlToGrpc.java
@@ -0,0 +1,160 @@
+/*******************************************************************************
+ * 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.converter.grpc;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.bind.JAXBException;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+import it.polito.tosca.jaxb.TNodeTemplate;
+import it.polito.tosca.jaxb.TRelationshipTemplate;
+import it.polito.tosca.jaxb.TServiceTemplate;
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.exception.DataNotFoundException;
+import it.polito.verigraph.grpc.NodeTemplateGrpc;
+import it.polito.verigraph.grpc.NodeTemplateGrpc.Type;
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
+import it.polito.verigraph.tosca.MappingUtils;
+import it.polito.verigraph.tosca.XmlParsingUtils;
+
+public class XmlToGrpc {
+
+ /** Returns the (first) TopologyTemplate found in the TOSCA-compliant XML file */
+ public static TopologyTemplateGrpc obtainTopologyTemplateGrpc (String filepath)
+ throws IOException, JAXBException, DataNotFoundException, ClassCastException, BadRequestException{
+ List<TServiceTemplate> serviceTList = XmlParsingUtils.obtainServiceTemplates(filepath);
+ TServiceTemplate serviceTemplate = serviceTList.get(0); //obtain only the first ServiceTemplate of the TOSCA compliance file
+
+ //Retrieving of list of NodeTemplate and RelationshipTemplate
+ List<NodeTemplateGrpc> nodes = new ArrayList<NodeTemplateGrpc>();
+ List<RelationshipTemplateGrpc> relats = new ArrayList<RelationshipTemplateGrpc>();
+ List<TNodeTemplate> tNodes = XmlParsingUtils.obtainNodeTemplates(serviceTemplate);
+ for(TNodeTemplate nt : tNodes) {
+ for(NodeTemplateGrpc alreadyAddedNode : nodes)
+ if(alreadyAddedNode.getId().equals(nt.getId()))
+ throw new BadRequestException("The NodeTemplate ID must be unique.");
+ nodes.add(parseNodeTemplate(nt));
+ }
+ for(TRelationshipTemplate rt : XmlParsingUtils.obtainRelationshipTemplates(serviceTemplate)) {
+ if(!tNodes.contains(rt.getSourceElement().getRef()) || !tNodes.contains(rt.getTargetElement().getRef()))
+ throw new BadRequestException("Invalid references to a Node in a Relationship.");
+ if(rt.getSourceElement().getRef() == rt.getTargetElement().getRef())
+ throw new BadRequestException("Source and Target cannot be equal in a Relationship.");
+ relats.add(parseRelationshipTemplate(rt));
+ }
+
+ //Creating TopologyTemplateGrpc object to be sent to server
+ return TopologyTemplateGrpc.newBuilder()
+ .setId("0") //useless value since the server chooses the actual value for the GraphID
+ .addAllNodeTemplate(nodes)
+ .addAllRelationshipTemplate(relats)
+ .build();
+ }
+
+
+ /** Parsing method: TNodeTemplate(tosca) --> NodeTemplateGrpc */
+ private static NodeTemplateGrpc parseNodeTemplate(TNodeTemplate nodeTempl)
+ throws ClassCastException, NullPointerException {
+ Boolean isVerigraphCompl = true;
+ Type type;
+
+ //NodeTemplateGrpc building
+ NodeTemplateGrpc.Builder nodegrpc = NodeTemplateGrpc.newBuilder();
+
+ //ID cannot be null
+ try {
+ nodegrpc.setId(nodeTempl.getId());
+ } catch (NullPointerException ex) {
+ throw new NullPointerException("An ID must be specified for each Node");
+ }
+ //Name can be null
+ try {
+ nodegrpc.setName(nodeTempl.getName());
+ } catch (NullPointerException ex) {
+ nodegrpc.setName("");
+ }
+
+ //Type cannot be null but it can be invalid
+ try {
+ String typestring = nodeTempl.getType().getLocalPart().toLowerCase();
+ type = Type.valueOf(nodeTempl.getType().getLocalPart().toLowerCase().substring(0,typestring.length()-4));
+ } catch (IllegalArgumentException | NullPointerException ex) {
+ //in case the NodeTemplate is not TOSCA-Verigraph compliant, we assume it to be a fieldmodifier node
+ type = Type.fieldmodifier;
+ isVerigraphCompl = false;
+ }
+ nodegrpc.setType(type);
+ ToscaConfigurationGrpc.Builder grpcConfig;
+ if(isVerigraphCompl) {
+ it.polito.tosca.jaxb.Configuration nodeConfig = XmlParsingUtils.obtainConfiguration(nodeTempl);
+ grpcConfig = ToscaConfigurationGrpc.newBuilder();
+ //These fields are optional in TOSCA xml
+ try {
+ grpcConfig.setId(nodeConfig.getConfID());
+ } catch(NullPointerException ex) {
+ grpcConfig.setId(ToscaGrpcUtils.defaultConfID);
+ }
+ try {
+ grpcConfig.setDescription(nodeConfig.getConfDescr());
+ } catch(NullPointerException ex) {
+ grpcConfig.setDescription(ToscaGrpcUtils.defaultDescr);
+ }
+ try {;
+ grpcConfig.setConfiguration(MappingUtils.obtainStringConfiguration(nodeConfig));
+ } catch(NullPointerException | JsonProcessingException ex) {
+ grpcConfig.setConfiguration(ToscaGrpcUtils.defaultConfig);
+ }
+ }
+ else {
+ grpcConfig = ToscaConfigurationGrpc.newBuilder()
+ .setId(ToscaGrpcUtils.defaultConfID)
+ .setDescription(ToscaGrpcUtils.defaultDescr)
+ .setConfiguration(ToscaGrpcUtils.defaultConfig);
+ }
+ nodegrpc.setConfiguration(grpcConfig.build());
+ return nodegrpc.build();
+ }
+
+
+ /** Parsing method: TRelationshipTemplate(tosca) --> RelationshipTemplateGrpc */
+ private static RelationshipTemplateGrpc parseRelationshipTemplate(TRelationshipTemplate relatTempl)
+ throws ClassCastException{
+ String source, target;
+ //RelationshipTemplateGrpc building
+ RelationshipTemplateGrpc.Builder relatgrpc = RelationshipTemplateGrpc.newBuilder();
+
+ //ID and Name can be null
+ try {
+ relatgrpc.setId(relatTempl.getId());
+ } catch (NullPointerException ex) {}//Different Relationship with same ID are considered valid.
+ try {
+ relatgrpc.setName(relatTempl.getName());
+ } catch (NullPointerException ex) {}
+
+ //Source and Target values cannot be null
+ try {
+ TNodeTemplate sourceNode = (TNodeTemplate) relatTempl.getSourceElement().getRef();
+ TNodeTemplate targetNode = (TNodeTemplate) relatTempl.getTargetElement().getRef();
+ source = sourceNode.getId();
+ target = targetNode.getId();
+ } catch (NullPointerException ex) {
+ throw new NullPointerException("Invalid NodeTemplate reference in RelationshipTemplate with id:"
+ + relatTempl.getId());
+ }
+ relatgrpc.setIdSourceNodeTemplate(source)
+ .setIdTargetNodeTemplate(target);
+ return relatgrpc.build();
+ }
+}
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
index 0000000..1a713bd
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/YamlToGrpc.java
@@ -0,0 +1,147 @@
+/*******************************************************************************
+ * 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.converter.grpc;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.xml.bind.JAXBException;
+
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.exception.DataNotFoundException;
+import it.polito.verigraph.grpc.NodeTemplateGrpc;
+import it.polito.verigraph.grpc.NodeTemplateGrpc.Type;
+import it.polito.verigraph.grpc.RelationshipTemplateGrpc;
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;
+import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
+import it.polito.verigraph.tosca.YamlParsingUtils;
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;
+
+public class YamlToGrpc {
+
+ /** Returns the (first) TopologyTemplate found in the TOSCA-compliant yaml file */
+ public static TopologyTemplateGrpc obtainTopologyTemplateGrpc (String filepath)
+ throws IOException, JAXBException, DataNotFoundException, ClassCastException, BadRequestException{
+ ServiceTemplateYaml serviceTemplate = YamlParsingUtils.obtainServiceTemplate(filepath);
+
+ //Retrieving of list of NodeTemplate and RelationshipTemplate
+ List<NodeTemplateGrpc> nodes = new ArrayList<NodeTemplateGrpc>();
+ List<RelationshipTemplateGrpc> relats = new ArrayList<RelationshipTemplateGrpc>();
+
+ try {
+ for(Map.Entry<String, NodeTemplateYaml> node : YamlParsingUtils.obtainNodeTemplates(serviceTemplate).entrySet()) {
+ for(NodeTemplateGrpc alreadyAddedNode : nodes)
+ if(alreadyAddedNode.getId().equals(node.getKey()))
+ throw new BadRequestException("The NodeTemplate ID must be unique.");
+ nodes.add(parseNodeTemplate(node));
+ }
+ } catch (NullPointerException e) {
+ throw new BadRequestException("There is not any NodeTemplate in the ServiceTemplate provided.");
+ }
+
+ try {
+ for(Map.Entry<String, RelationshipTemplateYaml> rel : YamlParsingUtils.obtainRelationshipTemplates(serviceTemplate).entrySet()) {
+ relats.add(parseRelationshipTemplate(rel, nodes));
+ }
+ } catch (NullPointerException e) {
+ throw new BadRequestException("There is not any RelationshipTemplate in the ServiceTemplate provided.");
+ }
+
+ //Creating TopologyTemplateGrpc object to be sent to server
+ return TopologyTemplateGrpc.newBuilder()
+ .setId("0") //useless value since the server chooses the actual value for the GraphID
+ .addAllNodeTemplate(nodes)
+ .addAllRelationshipTemplate(relats)
+ .build();
+ }
+
+ /** Parsing method: RelationshipTemplateYaml(tosca) --> RelationshipTemplateGrpc */
+ private static RelationshipTemplateGrpc parseRelationshipTemplate(Entry<String, RelationshipTemplateYaml> rel, List<NodeTemplateGrpc> nodes) throws BadRequestException{
+ String source, target;
+ boolean valid_source = false;
+ boolean valid_target = false;
+
+ //RelationshipTemplateGrpc building
+ RelationshipTemplateGrpc.Builder relatgrpc = RelationshipTemplateGrpc.newBuilder();
+
+ //ID can be null
+ try {
+ relatgrpc.setId(rel.getKey());
+ relatgrpc.setName(rel.getValue().getProperties().get("name"));
+ source = rel.getValue().getProperties().get("source_id");
+ target = rel.getValue().getProperties().get("target_id");
+ } catch (NullPointerException ex) {
+ throw new BadRequestException("Incorrect fields in RelationshipTemplate:" + rel.getKey());
+ }
+
+ //Source and Target values must correctly refer to a NodeTemplate
+ if(source.equals(target))
+ throw new BadRequestException("Source and Target cannot be the same value");
+ for(NodeTemplateGrpc node : nodes) {
+ if(node.getId().equals(source))
+ valid_source = true;
+ if(node.getId().equals(target))
+ valid_target = true;
+ }
+ if(!(valid_source && valid_target))
+ throw new BadRequestException("Invalid NodeTemplate reference in RelationshipTemplate:" + rel.getKey());
+
+ return relatgrpc.setIdSourceNodeTemplate(source).setIdTargetNodeTemplate(target).build();
+
+ }
+
+ /** Parsing method: NodeTemplateYaml(tosca) --> NodeTemplateGrpc */
+ private static NodeTemplateGrpc parseNodeTemplate(Entry<String, NodeTemplateYaml> node)
+ throws ClassCastException, NullPointerException, BadRequestException {
+ Boolean isVerigraphCompl = true;
+ Type type;
+
+ //NodeTemplateGrpc building
+ NodeTemplateGrpc.Builder nodegrpc = NodeTemplateGrpc.newBuilder()
+ .setId(node.getKey());
+
+ try {
+ nodegrpc.setName(node.getValue().getName());
+ } catch (NullPointerException ex) {
+ throw new BadRequestException("Invalid name in a NodeTemplate.");
+ }
+
+ //Type cannot be null but it can be invalid
+ try {
+ type = Type.valueOf(node.getValue().getType().replace("verigraph.nodeTypes.", "").toLowerCase());
+ } catch (IllegalArgumentException | NullPointerException ex) {
+ //in case the NodeTemplate is not TOSCA-Verigraph compliant, we assume it to be a fieldmodifier node
+ type = Type.fieldmodifier;
+ isVerigraphCompl = false;
+ }
+ nodegrpc.setType(type);
+ ToscaConfigurationGrpc.Builder grpcConfig;
+ if(isVerigraphCompl) {
+ String jsonConfig = YamlParsingUtils.obtainConfiguration(node.getValue());
+ grpcConfig = ToscaConfigurationGrpc.newBuilder()
+ .setId("")
+ .setDescription("")
+ .setConfiguration(jsonConfig);
+ }
+ else {
+ grpcConfig = ToscaConfigurationGrpc.newBuilder()
+ .setId(ToscaGrpcUtils.defaultConfID)
+ .setDescription(ToscaGrpcUtils.defaultDescr)
+ .setConfiguration(ToscaGrpcUtils.defaultConfig);
+ }
+ nodegrpc.setConfiguration(grpcConfig.build());
+ return nodegrpc.build();
+ }
+
+} \ 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
index 0000000..613588f
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/xml/GraphToXml.java
@@ -0,0 +1,134 @@
+/*******************************************************************************
+ * 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.converter.xml;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import it.polito.verigraph.model.Configuration;
+import it.polito.verigraph.model.Graph;
+import it.polito.verigraph.model.Neighbour;
+import it.polito.verigraph.model.Node;
+import it.polito.verigraph.tosca.MappingUtils;
+import it.polito.tosca.jaxb.Definitions;
+import it.polito.tosca.jaxb.TEntityTemplate.Properties;
+import it.polito.tosca.jaxb.TNodeTemplate;
+import it.polito.tosca.jaxb.TRelationshipTemplate;
+import it.polito.tosca.jaxb.TRelationshipTemplate.SourceElement;
+import it.polito.tosca.jaxb.TRelationshipTemplate.TargetElement;
+import it.polito.tosca.jaxb.TServiceTemplate;
+import it.polito.tosca.jaxb.TTopologyTemplate;
+
+public class GraphToXml {
+ /** model --> tosca_xml*/
+
+ public static Definitions mapGraph(Graph graph) {
+ Definitions definitions = new Definitions();
+ TServiceTemplate serviceTemplate = mapPathToXml(graph);
+ definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(serviceTemplate);
+
+ return definitions;
+ }
+
+ // These functions have been split so that they can be reused for obtaining all the paths
+ //into a single Definitions (see mapPathsToXml)
+ public static TServiceTemplate mapPathToXml(Graph graph) {
+
+ TServiceTemplate serviceTemplate = new TServiceTemplate();
+ TTopologyTemplate topologyTemplate = new TTopologyTemplate();
+
+ for(Node node : graph.getNodes().values()) {
+ long i = 0;
+ TNodeTemplate nodeTemplate = mapNode(node);
+ topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(nodeTemplate);
+
+ // RelationshipTemplate mapping
+ Map<Long,Neighbour> neighMap = node.getNeighbours();
+ for (Map.Entry<Long, Neighbour> myentry : neighMap.entrySet()) {
+ Neighbour neigh = myentry.getValue();
+ if (graph.getNodes().containsKey(neigh.getId())) {
+ // I have to check that because if I'm mapping a path (and not a graph) I could have
+ //as neighbour a node which is not in the path
+ TRelationshipTemplate relat = mapRelationship(graph, node, neigh, i);
+ topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(relat);
+ i++; //Neighbour does not have a neighbourID! RelationshipTemplate does,
+ //so it is an incremental number for each node
+ }
+ }
+ }
+
+ serviceTemplate.setId(String.valueOf(graph.getId()));
+ serviceTemplate.setTopologyTemplate(topologyTemplate);
+
+ return serviceTemplate;
+ }
+
+
+ private static TNodeTemplate mapNode(Node node){
+ TNodeTemplate nodeTemplate = new TNodeTemplate();
+
+ nodeTemplate.setId(String.valueOf(node.getId()));
+ nodeTemplate.setName(node.getName());
+
+ //QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12/ToscaVerigraphDefinition")
+ QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12",
+ node.getFunctional_type().substring(0, 1).toUpperCase() + node.
+ getFunctional_type().substring(1) + "Type");
+ nodeTemplate.setType(type);
+
+ it.polito.tosca.jaxb.Configuration config = mapModelConfiguration(node.getConfiguration(),
+ node.getFunctional_type().toLowerCase());
+ //nodeTemplate.getAny().add(config);
+ nodeTemplate.setProperties(new Properties());
+ nodeTemplate.getProperties().setAny(config);
+ return nodeTemplate;
+ }
+
+
+ private static TRelationshipTemplate mapRelationship(Graph graph, Node sourceNode, Neighbour neigh, long i) {
+ TRelationshipTemplate relationship = new TRelationshipTemplate();
+ SourceElement source = new SourceElement();
+ TargetElement target = new TargetElement();
+
+ Node targetNode = graph.getNodes().get(neigh.getId());
+
+ TNodeTemplate sourceNT = mapNode(sourceNode);
+ TNodeTemplate targetNT = mapNode(targetNode);
+
+ source.setRef(sourceNT);
+ target.setRef(targetNT);
+
+ relationship.setId(String.valueOf(i));
+ relationship.setSourceElement(source);
+ relationship.setTargetElement(target);
+ relationship.setName(sourceNode.getName()+"to"+neigh.getName());
+
+ return relationship;
+ }
+
+
+ private static it.polito.tosca.jaxb.Configuration mapModelConfiguration(Configuration conf, String type) {
+ it.polito.tosca.jaxb.Configuration configuration = new it.polito.tosca.jaxb.Configuration();
+ try {
+ //We are passing the configuration type to the Deserializer context
+ configuration = MappingUtils.obtainToscaConfiguration(conf, type);
+
+ //In Graph, ID and DESCRIPTION are always empty
+ //configuration.setConfID(conf.getId());
+ //configuration.setConfDescr(conf.getDescription());
+
+ } catch (IOException | NullPointerException e) {
+ e.printStackTrace();
+ }
+ return configuration;
+ }
+
+}
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
index 0000000..3744ba2
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/xml/XmlToGraph.java
@@ -0,0 +1,167 @@
+/*******************************************************************************
+ * 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.converter.xml;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.exception.DataNotFoundException;
+import it.polito.verigraph.model.Configuration;
+import it.polito.verigraph.model.Graph;
+import it.polito.verigraph.model.Neighbour;
+import it.polito.verigraph.model.Node;
+import it.polito.verigraph.tosca.MappingUtils;
+import it.polito.verigraph.tosca.XmlParsingUtils;
+import it.polito.tosca.jaxb.Definitions;
+import it.polito.tosca.jaxb.TExtensibleElements;
+import it.polito.tosca.jaxb.TNodeTemplate;
+import it.polito.tosca.jaxb.TRelationshipTemplate;
+import it.polito.tosca.jaxb.TServiceTemplate;
+
+public class XmlToGraph {
+ public static Graph mapTopologyTemplate(Definitions definitions) throws DataNotFoundException, BadRequestException {
+ Graph graph = new Graph();
+ Map<Long, Node> nodes = new HashMap<>();
+
+ 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 Definitions object");
+
+ List<TNodeTemplate> nodeTemplates = XmlParsingUtils.obtainNodeTemplates(serviceTemplates.get(0));
+
+ for (TNodeTemplate nodeTemplate : nodeTemplates) {
+ Node node = mapNodeTemplate(nodeTemplate);
+ nodes.put(Long.valueOf(nodeTemplate.getId()), node);
+ }
+
+ // Add Neighbour to the Node of the list
+ List<TRelationshipTemplate> relationshipTemplates = XmlParsingUtils.obtainRelationshipTemplates(serviceTemplates.get(0));
+ mapRelationshipTemplates(nodes, relationshipTemplates);
+
+ // Add Nodes and ID to the graph
+ graph.setNodes(nodes);
+
+ try {
+ graph.setId(Long.valueOf(serviceTemplates.get(0).getId()));
+ } catch (NumberFormatException ex) {
+ throw new BadRequestException("If you want to store your TopologyTemplate on this server, "
+ + "the TopologyTemplate ID must be a number.");
+ }
+
+ return graph;
+ }
+
+
+ private static Node mapNodeTemplate(TNodeTemplate nodeTemplate) {
+ Node node = new Node();
+
+ String toscaType = nodeTemplate.getType().toString();
+ toscaType = toscaType.replace("Type", "").replace("{http://docs.oasis-open.org/tosca/ns/2011/12}", "");
+ toscaType = toscaType.toLowerCase();
+
+ try {
+ node.setId(Long.valueOf(nodeTemplate.getId()));
+ } catch(NumberFormatException ex) {
+ throw new BadRequestException("The NodeTemplate ID must be a number.");
+ }
+ try {
+ node.setName(nodeTemplate.getName());
+ Configuration conf = mapToscaConfiguration(XmlParsingUtils.obtainConfiguration(nodeTemplate));
+ node.setConfiguration(conf);
+ node.setFunctional_type(toscaType);
+ } catch(NullPointerException | IOException ex) {
+ throw new BadRequestException("The NodeTemplate id:"+node.getId()+" has wrong fields representation.");
+ }
+ return node;
+ }
+
+
+ private static void mapRelationshipTemplates(Map<Long, Node> nodes, List<TRelationshipTemplate> relationshipTemplates) {
+ //update of nodes... (update = Node + its Neighbours)
+ for(TRelationshipTemplate relationshipTemplate : relationshipTemplates) {
+ if (relationshipTemplate != null) {
+ try {
+ if(relationshipTemplate.getSourceElement().getRef() == relationshipTemplate.getTargetElement().getRef())
+ throw new BadRequestException("Source and Target cannot be equal in a Relationship.");
+
+ // Retrieve the target Node name and generate a new Neighbour
+ TNodeTemplate targetNodeTemplate = (TNodeTemplate) relationshipTemplate.getTargetElement().getRef();
+ String neighName = nodes.get(Long.valueOf(targetNodeTemplate.getId())).getName();
+ //this manages invalid/inexistent node ID for target node
+ Neighbour neigh = new Neighbour();
+ neigh.setName(neighName);
+ neigh.setId(Long.valueOf(relationshipTemplate.getId()));
+
+ //Retrieve the Neighbour map of the source Node and add the Neighbour
+ TNodeTemplate sourceNodeTemplate = (TNodeTemplate) relationshipTemplate.getSourceElement().getRef();
+ Node source = nodes.get(Long.valueOf(sourceNodeTemplate.getId()));
+ //this manages invalid/inexistent node ID for source node
+ Map<Long,Neighbour> sourceNodeNeighMap = source.getNeighbours();
+ if(sourceNodeNeighMap.containsKey(neigh.getId()))
+ throw new BadRequestException("The RelationshipTemplate ID must be unique.");
+ else
+ sourceNodeNeighMap.put(neigh.getId(), neigh);
+ source.setNeighbours(sourceNodeNeighMap);
+
+ //Update the Node list
+ nodes.put(Long.valueOf(sourceNodeTemplate.getId()), source);
+ } catch(NullPointerException | NumberFormatException ex) {
+ throw new BadRequestException("A RelationshipTemplate has wrong fields representation.");
+ }
+ }
+ }
+ }
+
+ private static Configuration mapToscaConfiguration(it.polito.tosca.jaxb.Configuration configuration)
+ throws JsonProcessingException, IOException {
+ Configuration conf = new Configuration();
+ ObjectMapper mapper = new ObjectMapper();
+ String stringConfiguration;
+
+ //Retrieve configuration ID (optional)
+ if (configuration.getConfID() != null)
+ conf.setId(configuration.getConfID());
+ else
+ conf.setId("");
+
+ //Retrieve description (optional)
+ if (configuration.getConfDescr() != null)
+ conf.setDescription(configuration.getConfDescr());
+ else
+ conf.setDescription("");
+
+ //Retrieve string of configuration
+ try {
+ stringConfiguration = MappingUtils.obtainStringConfiguration(configuration);
+ } catch(IOException ex) {
+ conf.setConfiguration(mapper.readTree("[]"));
+ System.out.println("[WARNING] Provided default configuration.");
+ return conf;
+ }
+
+ //Retrieve JsonNode from the string of configuration
+ try {
+ conf.setConfiguration(mapper.readTree(stringConfiguration));
+ return conf;
+ } catch (IOException e) {
+ throw new BadRequestException("NodeTemplate configuration is invalid.");
+ }
+ }
+}
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
index 0000000..8766ac5
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/yaml/GraphToYaml.java
@@ -0,0 +1,270 @@
+/*******************************************************************************
+ * 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.converter.yaml;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.InjectableValues;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+
+import it.polito.neo4j.jaxb.FunctionalTypes;
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.model.Graph;
+import it.polito.verigraph.model.Neighbour;
+import it.polito.verigraph.model.Node;
+import it.polito.verigraph.tosca.deserializer.YamlConfigurationDeserializer;
+import it.polito.verigraph.tosca.yaml.beans.AntispamConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.AntispamNode;
+import it.polito.verigraph.tosca.yaml.beans.CacheConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.CacheNode;
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.DpiConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.DpiNode;
+import it.polito.verigraph.tosca.yaml.beans.EndhostConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.EndhostNode;
+import it.polito.verigraph.tosca.yaml.beans.EndpointConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.EndpointNode;
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierNode;
+import it.polito.verigraph.tosca.yaml.beans.FirewallConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.FirewallNode;
+import it.polito.verigraph.tosca.yaml.beans.MailClientConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.MailClientNode;
+import it.polito.verigraph.tosca.yaml.beans.MailServerConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.MailServerNode;
+import it.polito.verigraph.tosca.yaml.beans.NatConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.NatNode;
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.TopologyTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessNode;
+import it.polito.verigraph.tosca.yaml.beans.VpnExitConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.VpnExitNode;
+import it.polito.verigraph.tosca.yaml.beans.WebClientConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.WebClientNode;
+import it.polito.verigraph.tosca.yaml.beans.WebServerConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.WebServerNode;
+
+public class GraphToYaml {
+ public static ServiceTemplateYaml mapGraphYaml(Graph graph) {
+ ServiceTemplateYaml serviceTemplate = new ServiceTemplateYaml();
+ TopologyTemplateYaml topologyTemplate = new TopologyTemplateYaml();
+
+ topologyTemplate.setNode_templates(new HashMap<String,NodeTemplateYaml>());
+ topologyTemplate.setRelationship_templates(new HashMap<String,RelationshipTemplateYaml>());
+ serviceTemplate.setMetadata(new HashMap<String,String>());
+
+ //Neighbour does not have a neighbourID!
+ //RelationshipTemplate does, so it is an incremental number for each node
+ long i = 0; //This counter will act as a fake incremental id of relationships
+ for(Node node : graph.getNodes().values()) {
+ NodeTemplateYaml nodeTemplate;
+ try {
+ nodeTemplate = mapNodeYaml(node);
+ } catch (IOException e) {
+ throw new BadRequestException("Error while mapping a Node in Yaml object.");
+ }
+ topologyTemplate.getNode_templates().put(String.valueOf(node.getId()), nodeTemplate);
+ //shall we catch NumberFormatException?
+
+ Map<Long,Neighbour> neighMap = node.getNeighbours();
+ for (Map.Entry<Long, Neighbour> myentry : neighMap.entrySet()) {
+ Neighbour neigh = myentry.getValue();
+ if (graph.getNodes().containsKey(neigh.getId())) {
+ // I have to check that because if I'm mapping a path (and not a graph)
+ //I could have as neighbour a node which is not in the path
+ RelationshipTemplateYaml relat = mapRelationshipYaml(node, neigh);
+ topologyTemplate.getRelationship_templates().put(String.valueOf(i), relat);
+ i++;
+ }
+ }
+ }
+
+ serviceTemplate.getMetadata().put("template_id", String.valueOf(graph.getId()));
+ serviceTemplate.setTopology_template(topologyTemplate);
+ return serviceTemplate;
+ }
+
+
+ private static NodeTemplateYaml mapNodeYaml(Node node) throws JsonParseException, JsonMappingException, IOException {
+
+ ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
+ JsonNode configNode = node.getConfiguration().getConfiguration();
+ SimpleModule module = new SimpleModule();
+
+ //Passing the configuration type to the Deserializer context
+ module.addDeserializer(ConfigurationYaml.class, new YamlConfigurationDeserializer());
+ mapper.registerModule(module);
+
+ //Here we use the custom deserializer to convert the JsonNode into a Yaml bean
+ //The injectable value allows to provide an additional info to the deserializer that will be able to
+ //know that it is parsing a certain type of Configuration.
+ ConfigurationYaml yamlConfig = mapper
+ .reader(new InjectableValues.Std().addValue("type", node.getFunctional_type().toLowerCase()))
+ .forType(ConfigurationYaml.class).readValue(configNode);
+
+ FunctionalTypes nodeType = FunctionalTypes.valueOf(node.getFunctional_type().toUpperCase());
+ switch(nodeType) {
+ case ANTISPAM:
+ AntispamNode antispamNode = new AntispamNode();
+ antispamNode.setName(node.getName());
+ antispamNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ antispamNode.setProperties((AntispamConfigurationYaml) yamlConfig);
+ return antispamNode;
+
+ case CACHE:
+ CacheNode cacheNode = new CacheNode();
+ cacheNode.setName(node.getName());
+ cacheNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ cacheNode.setProperties((CacheConfigurationYaml) yamlConfig);
+ return cacheNode;
+
+ case DPI:
+ DpiNode dpiNode = new DpiNode();
+ dpiNode.setName(node.getName());
+ dpiNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ dpiNode.setProperties((DpiConfigurationYaml) yamlConfig);
+ return dpiNode;
+
+ case ENDHOST:
+ EndhostNode endhostNode = new EndhostNode();
+ endhostNode.setName(node.getName());
+ endhostNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ endhostNode.setProperties((EndhostConfigurationYaml) yamlConfig);
+ return endhostNode;
+
+ case ENDPOINT:
+ EndpointNode endpointNode = new EndpointNode();
+ endpointNode.setName(node.getName());
+ endpointNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ endpointNode.setProperties((EndpointConfigurationYaml) yamlConfig);
+ return endpointNode;
+
+ case FIELDMODIFIER:
+ FieldModifierNode fieldmodifierNode = new FieldModifierNode();
+ fieldmodifierNode.setName(node.getName());
+ fieldmodifierNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ fieldmodifierNode.setProperties((FieldModifierConfigurationYaml) yamlConfig);
+ return fieldmodifierNode;
+
+ case FIREWALL:
+ FirewallNode firewallNode = new FirewallNode();
+ firewallNode.setName(node.getName());
+ firewallNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ firewallNode.setProperties((FirewallConfigurationYaml) yamlConfig);
+ return firewallNode;
+
+ case MAILCLIENT:
+ MailClientNode mailclientNode = new MailClientNode();
+ mailclientNode.setName(node.getName());
+ mailclientNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ mailclientNode.setProperties((MailClientConfigurationYaml) yamlConfig);
+ return mailclientNode;
+
+ case MAILSERVER:
+ MailServerNode mailserverNode = new MailServerNode();
+ mailserverNode.setName(node.getName());
+ mailserverNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ mailserverNode.setProperties((MailServerConfigurationYaml) yamlConfig);
+ return mailserverNode;
+
+ case NAT:
+ NatNode natNode = new NatNode();
+ natNode.setName(node.getName());
+ natNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ natNode.setProperties((NatConfigurationYaml) yamlConfig);
+ return natNode;
+
+ case VPNACCESS:
+ VpnAccessNode vpnaccessNode = new VpnAccessNode();
+ vpnaccessNode.setName(node.getName());
+ vpnaccessNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ vpnaccessNode.setProperties((VpnAccessConfigurationYaml) yamlConfig);
+ return vpnaccessNode;
+
+ case VPNEXIT:
+ VpnExitNode vpnexitNode = new VpnExitNode();
+ vpnexitNode.setName(node.getName());
+ vpnexitNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ vpnexitNode.setProperties((VpnExitConfigurationYaml) yamlConfig);
+ return vpnexitNode;
+
+ case WEBCLIENT:
+ WebClientNode webclientNode = new WebClientNode();
+ webclientNode.setName(node.getName());
+ webclientNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ webclientNode.setProperties((WebClientConfigurationYaml) yamlConfig);
+ return webclientNode;
+
+ case WEBSERVER:
+ WebServerNode webserverNode = new WebServerNode();
+ webserverNode.setName(node.getName());
+ webserverNode.setType("verigraph.nodeTypes." +
+ node.getFunctional_type().substring(0, 1).toUpperCase() +
+ node.getFunctional_type().substring(1));
+ webserverNode.setProperties((WebServerConfigurationYaml) yamlConfig);
+ return webserverNode;
+
+ default:
+ FieldModifierNode defaultNode = new FieldModifierNode();
+ defaultNode.setName(node.getName());
+ defaultNode.setType("verigraph.nodeTypes.Fieldmodifier");
+ defaultNode.setProperties(new FieldModifierConfigurationYaml());
+ return defaultNode;
+ }
+
+ }
+
+ private static RelationshipTemplateYaml mapRelationshipYaml(Node sourceNode, Neighbour neigh) {
+ RelationshipTemplateYaml relationship = new RelationshipTemplateYaml();
+ relationship.setProperties(new HashMap<String,String>());
+
+ relationship.setType("verigraph.relationshipType.generic");
+ relationship.getProperties().put("source_id", String.valueOf(sourceNode.getId())); //to be catched?
+ relationship.getProperties().put("target_id", String.valueOf(neigh.getId()));
+ relationship.getProperties().put("name", sourceNode.getName()+"to"+neigh.getName());
+
+ return relationship;
+ }
+}
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
index 0000000..3922cf6
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/converter/yaml/YamlToGraph.java
@@ -0,0 +1,138 @@
+/*******************************************************************************
+ * 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.converter.yaml;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.model.Configuration;
+import it.polito.verigraph.model.Graph;
+import it.polito.verigraph.model.Neighbour;
+import it.polito.verigraph.model.Node;
+import it.polito.verigraph.tosca.YamlParsingUtils;
+import it.polito.verigraph.tosca.serializer.YamlConfigSerializer;
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;
+import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;
+
+public class YamlToGraph {
+ public static Graph mapTopologyTemplateYaml(ServiceTemplateYaml yamlServiceTemplate) throws BadRequestException {
+ Graph graph = new Graph();
+ Map<Long, Node> graphNodes = new HashMap<>();
+ Map<String, NodeTemplateYaml> nodes = new HashMap<>();
+ Map<String, RelationshipTemplateYaml> relats = new HashMap<>();
+
+ nodes = yamlServiceTemplate.getTopology_template().getNode_templates();
+
+ for (Map.Entry<String, NodeTemplateYaml> nodeYamlEntry : nodes.entrySet()) {
+ Node node = mapNodeTemplateYaml(nodeYamlEntry.getValue());
+
+ try {
+ graphNodes.put(Long.valueOf(nodeYamlEntry.getKey()), node);
+ } catch (NumberFormatException e) {
+ throw new BadRequestException("The NodeTemplate ID must be a number.");
+ }
+
+ }
+
+ // Add Neighbours to the Nodes of the list
+ relats = yamlServiceTemplate.getTopology_template().getRelationship_templates();
+ mapRelationshipTemplatesYaml(graphNodes, relats);
+
+ // Add Nodes and ID to the graph
+ graph.setNodes(graphNodes);
+ try {
+ graph.setId(Long.valueOf(yamlServiceTemplate.getMetadata().get("template_id")));
+ } catch (NumberFormatException ex) {
+ throw new BadRequestException("If you want to use this service, the TopologyTemplate ID must be a number.");
+ } catch (NullPointerException ex) {} //ID is not mandatory for the user since VeriGraph provides its IDs
+
+ return graph;
+ }
+
+
+ private static Node mapNodeTemplateYaml(NodeTemplateYaml yamlNodeTemplate) {
+ Node node = new Node();
+
+ String type = yamlNodeTemplate.getType().replace("verigraph.nodeTypes.", "").toLowerCase();
+
+ try {
+ node.setName(yamlNodeTemplate.getName());
+ Configuration conf = mapConfigurationYaml(yamlNodeTemplate);
+ node.setConfiguration(conf);
+ node.setFunctional_type(type);
+ } catch(NullPointerException ex) {
+ throw new BadRequestException("A NodeTemplate has wrong fields representation.");
+ }
+ return node;
+ }
+
+
+ private static void mapRelationshipTemplatesYaml(Map<Long, Node> graphNodes, Map<String, RelationshipTemplateYaml> relats) {
+ //updated nodes (update = Node + its Neighbours)
+ for(Map.Entry<String, RelationshipTemplateYaml> yamlRelationshipTemplate : relats.entrySet()) {
+ try {
+ // Retrieve relationship information
+ String target = yamlRelationshipTemplate.getValue().getProperties().get("target_id");
+ String source = yamlRelationshipTemplate.getValue().getProperties().get("source_id");
+ String name = graphNodes.get(Long.valueOf(target)).getName();
+
+ Neighbour neigh = new Neighbour();
+ neigh.setName(name);
+ neigh.setId(Long.valueOf(target));
+
+ //Retrieve the Neighbour map of the source Node and add the Neighbour
+ Node sourceNode = graphNodes.get(Long.valueOf(source));
+ Map<Long,Neighbour> sourceNodeNeighMap = sourceNode.getNeighbours();
+ if(sourceNodeNeighMap.containsKey(neigh.getId()))
+ throw new BadRequestException("The RelationshipTemplate ID must be unique.");
+ else
+ sourceNodeNeighMap.put(neigh.getId(), neigh);
+ sourceNode.setNeighbours(sourceNodeNeighMap);
+
+ //Update the Node list
+ graphNodes.put(Long.valueOf(source), sourceNode);
+
+ } catch(NullPointerException | NumberFormatException ex) {
+ throw new BadRequestException("A RelationshipTemplate has wrong fields representation.");
+ }
+
+ }
+
+ }
+
+
+ private static Configuration mapConfigurationYaml(NodeTemplateYaml node) {
+ Configuration config = new Configuration();
+ JsonNode jsonConfiguration = null;
+ ObjectMapper mapper = new ObjectMapper();
+ SimpleModule module = new SimpleModule();
+ module.addSerializer(ConfigurationYaml.class, new YamlConfigSerializer());
+ mapper.registerModule(module);
+
+ try{
+ String stringConfiguration = YamlParsingUtils.obtainConfiguration(node);
+ jsonConfiguration = mapper.readTree(stringConfiguration);
+ config.setConfiguration(jsonConfiguration);
+ config.setDescription("");
+ config.setId("");
+ } catch (NullPointerException | IOException | BadRequestException e) {
+ throw new BadRequestException("Not able to retrieve a valid configuration");
+ }
+
+ return config;
+ }
+}