summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/tosca/converter/xml
diff options
context:
space:
mode:
Diffstat (limited to 'verigraph/src/it/polito/verigraph/tosca/converter/xml')
-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
2 files changed, 301 insertions, 0 deletions
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.");
+ }
+ }
+}