summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/tosca/deserializer
diff options
context:
space:
mode:
Diffstat (limited to 'verigraph/src/it/polito/verigraph/tosca/deserializer')
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java177
-rw-r--r--verigraph/src/it/polito/verigraph/tosca/deserializer/YamlConfigurationDeserializer.java251
2 files changed, 428 insertions, 0 deletions
diff --git a/verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java b/verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java
new file mode 100644
index 0000000..7e2ee4d
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java
@@ -0,0 +1,177 @@
+/*******************************************************************************
+ * 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.deserializer;
+
+import it.polito.tosca.jaxb.Configuration;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.ObjectCodec;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+public class XmlConfigurationDeserializer extends JsonDeserializer<Configuration> {
+
+ @Override
+ public Configuration deserialize(JsonParser jp, DeserializationContext ctxt)
+ throws IOException, JsonProcessingException {
+ ObjectCodec oc = jp.getCodec();
+ JsonNode node = oc.readTree(jp);
+ Configuration deserialized;
+
+ try {
+ //Get the content from the array wrapping the JSON configuration
+ final Iterator<JsonNode> elements = node.elements();
+ deserialized = new Configuration();
+
+
+ if(!elements.hasNext()) {
+ //System.out.println("The provided configuration is empty.");
+ //return null; //TODO shall we return an empty configuration?
+
+ return new Configuration();
+ }
+
+ switch ((String) ctxt.findInjectableValue("type", null, null)) {
+
+ case "antispam":
+ Configuration.AntispamConfiguration antispam = new Configuration.AntispamConfiguration();
+ List<String> sources = antispam.getSource();
+ while (elements.hasNext()) {
+ sources.add(elements.next().asText());
+ }
+ deserialized.setAntispamConfiguration(antispam);
+ break;
+
+ case "cache":
+ Configuration.CacheConfiguration cache = new Configuration.CacheConfiguration();
+ List<String> resources = cache.getResource();
+ while(elements.hasNext()) {
+ resources.add(elements.next().asText());
+ }
+ deserialized.setCacheConfiguration(cache);
+ break;
+
+ case "endhost":
+ Configuration.EndhostConfiguration endhost = new Configuration.EndhostConfiguration();
+ JsonNode thisnode = elements.next();
+
+ if(thisnode.has("body"))
+ endhost.setBody(thisnode.findValue("body").asText());
+ if(thisnode.has("sequence"))
+ endhost.setSequence(BigInteger.valueOf(Long.valueOf(thisnode.findValue("sequence").asText())));
+ if(thisnode.has("protocol"))
+ endhost.setProtocol(thisnode.findValue("protocol").asText());
+ if(thisnode.has("email_from"))
+ endhost.setEmailFrom(thisnode.findValue("email_from").asText());
+ if(thisnode.has("url"))
+ endhost.setUrl(thisnode.findValue("url").asText());
+ if(thisnode.has("options"))
+ endhost.setOptions(thisnode.findValue("options").asText());
+ if(thisnode.has("destination"))
+ endhost.setDestination(thisnode.findValue("destination").asText());
+
+ deserialized.setEndhostConfiguration(endhost);
+ break;
+
+ case "endpoint":
+ Configuration.EndpointConfiguration endpoint = new Configuration.EndpointConfiguration();
+ deserialized.setEndpointConfiguration(endpoint);
+ break;
+
+ case "fieldmodifier":
+ Configuration.FieldmodifierConfiguration fieldmodifier = new Configuration.FieldmodifierConfiguration();
+ deserialized.setFieldmodifierConfiguration(fieldmodifier);
+ break;
+
+ case "firewall":
+ Configuration.FirewallConfiguration firewall = new Configuration.FirewallConfiguration();
+ List<Configuration.FirewallConfiguration.Elements> fwelements = firewall.getElements();
+ Configuration.FirewallConfiguration.Elements element;
+
+ Iterator<Map.Entry<String, JsonNode>> current = elements.next().fields();
+ Map.Entry<String, JsonNode> entry;
+ while(current.hasNext()) {
+ entry = current.next();
+ element = new Configuration.FirewallConfiguration.Elements();
+ element.setSource(entry.getKey());
+ element.setDestination(entry.getValue().asText());
+ fwelements.add(element);
+ }
+
+ deserialized.setFirewallConfiguration(firewall);
+ break;
+
+ case "mailclient":
+ Configuration.MailclientConfiguration mailclient = new Configuration.MailclientConfiguration();
+ mailclient.setMailserver(elements.next().findValue("mailserver").asText());
+ deserialized.setMailclientConfiguration(mailclient);
+ break;
+
+ case "mailserver":
+ Configuration.MailserverConfiguration mailserver = new Configuration.MailserverConfiguration();
+ deserialized.setMailserverConfiguration(mailserver);
+ break;
+
+ case "nat":
+ Configuration.NatConfiguration nat = new Configuration.NatConfiguration();
+ List<String> natsource = nat.getSource();
+ while(elements.hasNext()) {
+ natsource.add(elements.next().asText());
+ }
+ deserialized.setNatConfiguration(nat);
+ break;
+
+ case "vpnaccess":
+ Configuration.VpnaccessConfiguration vpnaccess = new Configuration.VpnaccessConfiguration();
+ vpnaccess.setVpnexit(elements.next().findValue("vpnexit").asText());
+ deserialized.setVpnaccessConfiguration(vpnaccess);
+ break;
+
+ case "vpnexit":
+ Configuration.VpnexitConfiguration vpnexit = new Configuration.VpnexitConfiguration();
+ vpnexit.setVpnaccess(elements.next().findValue("vpnaccess").asText());
+ deserialized.setVpnexitConfiguration(vpnexit);
+ break;
+
+ case "webclient":
+ Configuration.WebclientConfiguration webclient = new Configuration.WebclientConfiguration();
+ webclient.setNameWebServer(elements.next().findValue("webserver").asText());
+ deserialized.setWebclientConfiguration(webclient);
+ break;
+
+ case "webserver":
+ Configuration.WebserverConfiguration webserver = new Configuration.WebserverConfiguration();
+ deserialized.setWebserverConfiguration(webserver);
+ break;
+
+ default:
+ Configuration.FieldmodifierConfiguration defaultForwarder = new Configuration.FieldmodifierConfiguration();
+ deserialized.setFieldmodifierConfiguration(defaultForwarder);
+ break;
+ }
+
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ System.err.println("Error converting the Json to XmlConfiguration");
+ e.printStackTrace();
+ return null;
+ }
+
+ return deserialized;
+
+ }
+
+}
diff --git a/verigraph/src/it/polito/verigraph/tosca/deserializer/YamlConfigurationDeserializer.java b/verigraph/src/it/polito/verigraph/tosca/deserializer/YamlConfigurationDeserializer.java
new file mode 100644
index 0000000..8fb7e52
--- /dev/null
+++ b/verigraph/src/it/polito/verigraph/tosca/deserializer/YamlConfigurationDeserializer.java
@@ -0,0 +1,251 @@
+/*******************************************************************************
+ * 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.deserializer;
+
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.ObjectCodec;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonNode;
+
+import it.polito.verigraph.tosca.yaml.beans.AntispamConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.CacheConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.DpiConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.EndhostConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.EndpointConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.FirewallConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.MailClientConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.MailServerConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.NatConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.VpnExitConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.WebClientConfigurationYaml;
+import it.polito.verigraph.tosca.yaml.beans.WebServerConfigurationYaml;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+public class YamlConfigurationDeserializer extends JsonDeserializer<ConfigurationYaml> {
+
+ @Override
+ public ConfigurationYaml deserialize(JsonParser jp, DeserializationContext ctxt)
+ throws IOException, JsonProcessingException {
+ ObjectCodec oc = jp.getCodec();
+ JsonNode node = oc.readTree(jp);
+ ConfigurationYaml deserialized;
+ boolean emptyConfig = false;
+
+ try {
+ //Get the content from the array wrapping the JSON configuration
+ final Iterator<JsonNode> elements = node.elements();
+
+ if(!elements.hasNext()) {
+ //System.out.println("The provided configuration is empty.");
+ emptyConfig = true;
+ }
+
+ switch ((String)ctxt.findInjectableValue("type", null, null)) {
+
+ case "antispam":
+ if(emptyConfig) {
+ deserialized = new AntispamConfigurationYaml();
+ break;
+ }
+ AntispamConfigurationYaml antispam = new AntispamConfigurationYaml();
+ antispam.setSources(new ArrayList<String>());
+ while (elements.hasNext()) {
+ antispam.getSources().add(elements.next().asText());
+ }
+ deserialized = antispam;
+ break;
+
+ case "cache":
+ if(emptyConfig) {
+ deserialized = new CacheConfigurationYaml();
+ break;
+ }
+ CacheConfigurationYaml cache = new CacheConfigurationYaml();
+ cache.setResources(new ArrayList<String>());
+ while(elements.hasNext()) {
+ cache.getResources().add(elements.next().asText());
+ }
+ deserialized = cache;
+ break;
+
+ case "dpi":
+ if(emptyConfig) {
+ deserialized = new DpiConfigurationYaml();
+ break;
+ }
+ DpiConfigurationYaml dpi = new DpiConfigurationYaml();
+ dpi.setNotAllowedList(new ArrayList<String>());
+ while(elements.hasNext()) {
+ dpi.getNotAllowedList().add(elements.next().asText());
+ }
+ deserialized = dpi;
+ break;
+
+ case "endhost":
+ if(emptyConfig) {
+ deserialized = new EndhostConfigurationYaml();
+ break;
+ }
+ EndhostConfigurationYaml endhost = new EndhostConfigurationYaml();
+ JsonNode thisnode = elements.next();
+ if(thisnode.has("body"))
+ endhost.setBody(thisnode.findValue("body").asText());
+ if(thisnode.has("sequence"))
+ endhost.setSequence(Integer.valueOf(thisnode.findValue("sequence").asText()));
+ if(thisnode.has("protocol"))
+ endhost.setProtocol(thisnode.findValue("protocol").asText());
+ if(thisnode.has("email_from"))
+ endhost.setEmail_from(thisnode.findValue("email_from").asText());
+ if(thisnode.has("options"))
+ endhost.setOptions(thisnode.findValue("options").asText());
+ if(thisnode.has("url"))
+ endhost.setUrl(thisnode.findValue("url").asText());
+ if(thisnode.has("destination"))
+ endhost.setDestination(thisnode.findValue("destination").asText());
+
+ deserialized = endhost;
+ break;
+
+ case "endpoint":
+ if(emptyConfig) {
+ deserialized = new EndpointConfigurationYaml();
+ break;
+ }
+ EndpointConfigurationYaml endpoint = new EndpointConfigurationYaml();
+ deserialized = endpoint;
+ break;
+
+ case "fieldmodifier":
+ if(emptyConfig) {
+ deserialized = new FieldModifierConfigurationYaml();
+ break;
+ }
+ FieldModifierConfigurationYaml fieldmodifier = new FieldModifierConfigurationYaml();
+ deserialized = fieldmodifier;
+ break;
+
+ case "firewall":
+ if(emptyConfig) {
+ deserialized = new FirewallConfigurationYaml();
+ break;
+ }
+ FirewallConfigurationYaml firewall = new FirewallConfigurationYaml();
+ firewall.setElements(new HashMap<String,String>());
+ JsonNode current = elements.next();
+ Iterator<Map.Entry<String, JsonNode>> iter = current.fields();
+
+ while (iter.hasNext()) {
+ Map.Entry<String, JsonNode> entry = iter.next();
+ firewall.getElements().put(entry.getKey(), entry.getValue().asText());
+ }
+
+ deserialized = firewall;
+ break;
+
+ case "mailclient":
+ if(emptyConfig) {
+ deserialized = new MailClientConfigurationYaml();
+ break;
+ }
+ MailClientConfigurationYaml mailclient = new MailClientConfigurationYaml();
+ mailclient.setMailserver(elements.next().findValue("mailserver").asText());
+ deserialized = mailclient;
+ break;
+
+ case "mailserver":
+ if(emptyConfig) {
+ deserialized = new MailServerConfigurationYaml();
+ break;
+ }
+ MailServerConfigurationYaml mailserver = new MailServerConfigurationYaml();
+ deserialized = mailserver;
+ break;
+
+ case "nat":
+ if(emptyConfig) {
+ deserialized = new NatConfigurationYaml();
+ break;
+ }
+ NatConfigurationYaml nat = new NatConfigurationYaml();
+ nat.setSources(new ArrayList<String>());
+ while(elements.hasNext()) {
+ nat.getSources().add(elements.next().asText());
+ }
+ deserialized = nat;
+ break;
+
+ case "vpnaccess":
+ if(emptyConfig) {
+ deserialized = new VpnAccessConfigurationYaml();
+ break;
+ }
+ VpnAccessConfigurationYaml vpnaccess = new VpnAccessConfigurationYaml();
+ vpnaccess.setVpnexit(elements.next().findValue("vpnexit").asText());
+ deserialized = vpnaccess;
+ break;
+
+ case "vpnexit":
+ if(emptyConfig) {
+ deserialized = new VpnExitConfigurationYaml();
+ break;
+ }
+ VpnExitConfigurationYaml vpnexit = new VpnExitConfigurationYaml();
+ vpnexit.setVpnaccess(elements.next().findValue("vpnaccess").asText());
+ deserialized = vpnexit;
+ break;
+
+ case "webclient":
+ if(emptyConfig) {
+ deserialized = new WebClientConfigurationYaml();
+ break;
+ }
+ WebClientConfigurationYaml webclient = new WebClientConfigurationYaml();
+ webclient.setNameWebServer(elements.next().findValue("webserver").asText());
+ deserialized = webclient;
+ break;
+
+ case "webserver":
+ if(emptyConfig) {
+ deserialized = new WebServerConfigurationYaml();
+ break;
+ }
+ WebServerConfigurationYaml webserver = new WebServerConfigurationYaml();
+ deserialized = webserver;
+ break;
+
+ default:
+ deserialized = new FieldModifierConfigurationYaml();
+ break;
+ }
+
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ //System.err.println("Error converting the Json to YamlConfiguration");
+ e.printStackTrace();
+ return new FieldModifierConfigurationYaml();
+ }
+
+ return deserialized;
+
+ }
+
+} \ No newline at end of file