summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/tosca/deserializer/XmlConfigurationDeserializer.java
blob: 7e2ee4d55b122c93d99da83248db47ceaae5401c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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;

    }

}