From 0f987a9e1b8e79caef48015adf865eef36216dbc Mon Sep 17 00:00:00 2001 From: "serena.spinoso" Date: Sun, 29 Apr 2018 10:48:44 +0200 Subject: Add CLI in verigraph. JIRA: PARSER-180 Add CLI to select one of the available interfaces (i.e. REST or gRPC), one of the supported service descriptor formats (i.e. TOSCA/XML, TOSCA/YAML, JSON), and the operations to perform (i.e. read, create, update, delete graphs and run verifications). Change-Id: I02f4d86e221baf0ac583d0ffd0ebe028aa6209be Signed-off-by: serena.spinoso --- .../deserializer/XmlConfigurationDeserializer.java | 177 +++++++++++++++ .../YamlConfigurationDeserializer.java | 251 +++++++++++++++++++++ 2 files changed, 428 insertions(+) create mode 100644 verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java create mode 100644 verigraph/src/it/polito/verigraph/tosca/deserializer/YamlConfigurationDeserializer.java (limited to 'verigraph/src/it/polito/verigraph/tosca/deserializer') 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 { + + @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 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 sources = antispam.getSource(); + while (elements.hasNext()) { + sources.add(elements.next().asText()); + } + deserialized.setAntispamConfiguration(antispam); + break; + + case "cache": + Configuration.CacheConfiguration cache = new Configuration.CacheConfiguration(); + List 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 fwelements = firewall.getElements(); + Configuration.FirewallConfiguration.Elements element; + + Iterator> current = elements.next().fields(); + Map.Entry 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 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 { + + @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 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()); + 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()); + 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()); + 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()); + JsonNode current = elements.next(); + Iterator> iter = current.fields(); + + while (iter.hasNext()) { + Map.Entry 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()); + 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 -- cgit 1.2.3-korg