--- /dev/null
+/*******************************************************************************\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