--- /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.deserializer;\r
+\r
+\r
+import com.fasterxml.jackson.core.JsonParser;\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.core.ObjectCodec;\r
+import com.fasterxml.jackson.databind.DeserializationContext;\r
+import com.fasterxml.jackson.databind.JsonDeserializer;\r
+import com.fasterxml.jackson.databind.JsonNode;\r
+\r
+import it.polito.verigraph.tosca.yaml.beans.AntispamConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.CacheConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.ConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.DpiConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndhostConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.EndpointConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FieldModifierConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.FirewallConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.MailServerConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.NatConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnAccessConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.VpnExitConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebClientConfigurationYaml;\r
+import it.polito.verigraph.tosca.yaml.beans.WebServerConfigurationYaml;\r
+\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+public class YamlConfigurationDeserializer extends JsonDeserializer<ConfigurationYaml> {\r
+\r
+ @Override\r
+ public ConfigurationYaml deserialize(JsonParser jp, DeserializationContext ctxt)\r
+ throws IOException, JsonProcessingException {\r
+ ObjectCodec oc = jp.getCodec();\r
+ JsonNode node = oc.readTree(jp);\r
+ ConfigurationYaml deserialized;\r
+ boolean emptyConfig = false;\r
+\r
+ try {\r
+ //Get the content from the array wrapping the JSON configuration\r
+ final Iterator<JsonNode> elements = node.elements();\r
+\r
+ if(!elements.hasNext()) {\r
+ //System.out.println("The provided configuration is empty.");\r
+ emptyConfig = true;\r
+ }\r
+\r
+ switch ((String)ctxt.findInjectableValue("type", null, null)) {\r
+\r
+ case "antispam":\r
+ if(emptyConfig) {\r
+ deserialized = new AntispamConfigurationYaml();\r
+ break;\r
+ }\r
+ AntispamConfigurationYaml antispam = new AntispamConfigurationYaml();\r
+ antispam.setSources(new ArrayList<String>());\r
+ while (elements.hasNext()) {\r
+ antispam.getSources().add(elements.next().asText());\r
+ }\r
+ deserialized = antispam;\r
+ break;\r
+\r
+ case "cache":\r
+ if(emptyConfig) {\r
+ deserialized = new CacheConfigurationYaml();\r
+ break;\r
+ }\r
+ CacheConfigurationYaml cache = new CacheConfigurationYaml();\r
+ cache.setResources(new ArrayList<String>());\r
+ while(elements.hasNext()) {\r
+ cache.getResources().add(elements.next().asText());\r
+ }\r
+ deserialized = cache;\r
+ break;\r
+\r
+ case "dpi":\r
+ if(emptyConfig) {\r
+ deserialized = new DpiConfigurationYaml();\r
+ break;\r
+ }\r
+ DpiConfigurationYaml dpi = new DpiConfigurationYaml();\r
+ dpi.setNotAllowedList(new ArrayList<String>());\r
+ while(elements.hasNext()) {\r
+ dpi.getNotAllowedList().add(elements.next().asText());\r
+ }\r
+ deserialized = dpi;\r
+ break;\r
+\r
+ case "endhost":\r
+ if(emptyConfig) {\r
+ deserialized = new EndhostConfigurationYaml();\r
+ break;\r
+ }\r
+ EndhostConfigurationYaml endhost = new EndhostConfigurationYaml();\r
+ JsonNode thisnode = elements.next();\r
+ if(thisnode.has("body"))\r
+ endhost.setBody(thisnode.findValue("body").asText());\r
+ if(thisnode.has("sequence"))\r
+ endhost.setSequence(Integer.valueOf(thisnode.findValue("sequence").asText()));\r
+ if(thisnode.has("protocol"))\r
+ endhost.setProtocol(thisnode.findValue("protocol").asText());\r
+ if(thisnode.has("email_from"))\r
+ endhost.setEmail_from(thisnode.findValue("email_from").asText());\r
+ if(thisnode.has("options"))\r
+ endhost.setOptions(thisnode.findValue("options").asText());\r
+ if(thisnode.has("url"))\r
+ endhost.setUrl(thisnode.findValue("url").asText());\r
+ if(thisnode.has("destination"))\r
+ endhost.setDestination(thisnode.findValue("destination").asText());\r
+\r
+ deserialized = endhost;\r
+ break;\r
+\r
+ case "endpoint":\r
+ if(emptyConfig) {\r
+ deserialized = new EndpointConfigurationYaml();\r
+ break;\r
+ }\r
+ EndpointConfigurationYaml endpoint = new EndpointConfigurationYaml();\r
+ deserialized = endpoint;\r
+ break;\r
+\r
+ case "fieldmodifier":\r
+ if(emptyConfig) {\r
+ deserialized = new FieldModifierConfigurationYaml();\r
+ break;\r
+ }\r
+ FieldModifierConfigurationYaml fieldmodifier = new FieldModifierConfigurationYaml();\r
+ deserialized = fieldmodifier;\r
+ break;\r
+\r
+ case "firewall":\r
+ if(emptyConfig) {\r
+ deserialized = new FirewallConfigurationYaml();\r
+ break;\r
+ }\r
+ FirewallConfigurationYaml firewall = new FirewallConfigurationYaml();\r
+ firewall.setElements(new HashMap<String,String>());\r
+ JsonNode current = elements.next();\r
+ Iterator<Map.Entry<String, JsonNode>> iter = current.fields();\r
+\r
+ while (iter.hasNext()) {\r
+ Map.Entry<String, JsonNode> entry = iter.next();\r
+ firewall.getElements().put(entry.getKey(), entry.getValue().asText());\r
+ }\r
+\r
+ deserialized = firewall;\r
+ break;\r
+\r
+ case "mailclient":\r
+ if(emptyConfig) {\r
+ deserialized = new MailClientConfigurationYaml();\r
+ break;\r
+ }\r
+ MailClientConfigurationYaml mailclient = new MailClientConfigurationYaml();\r
+ mailclient.setMailserver(elements.next().findValue("mailserver").asText());\r
+ deserialized = mailclient;\r
+ break;\r
+\r
+ case "mailserver":\r
+ if(emptyConfig) {\r
+ deserialized = new MailServerConfigurationYaml();\r
+ break;\r
+ }\r
+ MailServerConfigurationYaml mailserver = new MailServerConfigurationYaml();\r
+ deserialized = mailserver;\r
+ break;\r
+\r
+ case "nat":\r
+ if(emptyConfig) {\r
+ deserialized = new NatConfigurationYaml();\r
+ break;\r
+ }\r
+ NatConfigurationYaml nat = new NatConfigurationYaml();\r
+ nat.setSources(new ArrayList<String>());\r
+ while(elements.hasNext()) {\r
+ nat.getSources().add(elements.next().asText());\r
+ }\r
+ deserialized = nat;\r
+ break;\r
+\r
+ case "vpnaccess":\r
+ if(emptyConfig) {\r
+ deserialized = new VpnAccessConfigurationYaml();\r
+ break;\r
+ }\r
+ VpnAccessConfigurationYaml vpnaccess = new VpnAccessConfigurationYaml();\r
+ vpnaccess.setVpnexit(elements.next().findValue("vpnexit").asText());\r
+ deserialized = vpnaccess;\r
+ break;\r
+\r
+ case "vpnexit":\r
+ if(emptyConfig) {\r
+ deserialized = new VpnExitConfigurationYaml();\r
+ break;\r
+ }\r
+ VpnExitConfigurationYaml vpnexit = new VpnExitConfigurationYaml();\r
+ vpnexit.setVpnaccess(elements.next().findValue("vpnaccess").asText());\r
+ deserialized = vpnexit;\r
+ break;\r
+\r
+ case "webclient":\r
+ if(emptyConfig) {\r
+ deserialized = new WebClientConfigurationYaml();\r
+ break;\r
+ }\r
+ WebClientConfigurationYaml webclient = new WebClientConfigurationYaml();\r
+ webclient.setNameWebServer(elements.next().findValue("webserver").asText());\r
+ deserialized = webclient;\r
+ break;\r
+\r
+ case "webserver":\r
+ if(emptyConfig) {\r
+ deserialized = new WebServerConfigurationYaml();\r
+ break;\r
+ }\r
+ WebServerConfigurationYaml webserver = new WebServerConfigurationYaml();\r
+ deserialized = webserver;\r
+ break;\r
+\r
+ default:\r
+ deserialized = new FieldModifierConfigurationYaml();\r
+ break;\r
+ }\r
+\r
+\r
+ } catch (Exception e) {\r
+ // TODO Auto-generated catch block\r
+ //System.err.println("Error converting the Json to YamlConfiguration");\r
+ e.printStackTrace();\r
+ return new FieldModifierConfigurationYaml();\r
+ }\r
+\r
+ return deserialized;\r
+\r
+ }\r
+\r
+}
\ No newline at end of file