1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Apache License, Version 2.0
6 * which accompanies this distribution, and is available at
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *******************************************************************************/
9 package it.polito.neo4j.translator;
11 import java.util.ArrayList;
12 import java.util.List;
14 import java.util.Map.Entry;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
21 import java.io.IOException;
22 import java.io.StringWriter;
23 import java.math.BigInteger;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.LinkedList;
29 import javax.xml.bind.JAXBContext;
30 import javax.xml.bind.JAXBElement;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Marshaller;
33 import javax.xml.bind.UnmarshalException;
34 import javax.xml.bind.Unmarshaller;
35 import javax.xml.bind.ValidationEvent;
36 import javax.xml.bind.ValidationEventHandler;
37 import javax.xml.bind.ValidationEventLocator;
38 import javax.xml.stream.FactoryConfigurationError;
39 import javax.xml.stream.XMLOutputFactory;
40 import javax.xml.stream.XMLStreamException;
41 import javax.xml.stream.XMLStreamWriter;
42 import javax.xml.validation.Schema;
43 import javax.xml.validation.SchemaFactory;
45 import com.fasterxml.jackson.core.JsonFactory;
46 import com.fasterxml.jackson.core.JsonGenerationException;
47 import com.fasterxml.jackson.core.JsonParseException;
48 import com.fasterxml.jackson.core.JsonParser;
49 import com.fasterxml.jackson.core.type.TypeReference;
50 import com.fasterxml.jackson.databind.JsonMappingException;
51 import com.fasterxml.jackson.databind.JsonNode;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.fasterxml.jackson.databind.node.ArrayNode;
54 import com.fasterxml.jackson.databind.node.ObjectNode;
55 import com.fasterxml.jackson.databind.util.JSONWrappedObject;
57 import it.polito.neo4j.jaxb.Antispam;
58 import it.polito.neo4j.jaxb.Cache;
59 import it.polito.neo4j.jaxb.Dpi;
60 import it.polito.neo4j.jaxb.Elements;
61 import it.polito.neo4j.jaxb.Endhost;
62 import it.polito.neo4j.jaxb.Endpoint;
63 import it.polito.neo4j.jaxb.Fieldmodifier;
64 import it.polito.neo4j.jaxb.Firewall;
65 import it.polito.neo4j.jaxb.FunctionalTypes;
66 import it.polito.neo4j.jaxb.Graph;
67 import it.polito.neo4j.jaxb.Node;
68 import it.polito.neo4j.jaxb.ObjectFactory;
69 import it.polito.neo4j.jaxb.ProtocolTypes;
70 import it.polito.neo4j.jaxb.Vpnaccess;
71 import it.polito.neo4j.jaxb.Vpnexit;
72 import it.polito.neo4j.jaxb.Webclient;
73 import it.polito.neo4j.jaxb.Webserver;
74 import it.polito.verigraph.model.Configuration;
75 import it.polito.verigraph.service.VerigraphLogger;
76 import it.polito.neo4j.jaxb.Graphs;
77 import it.polito.neo4j.jaxb.Mailclient;
78 import it.polito.neo4j.jaxb.Mailserver;
79 import it.polito.neo4j.jaxb.Nat;
80 import it.polito.neo4j.jaxb.Neighbour;
82 public class GraphToNeo4j {
83 public static VerigraphLogger vlogger = VerigraphLogger.getVerigraphlogger();
84 public static it.polito.neo4j.jaxb.Graph generateObject(it.polito.verigraph.model.Graph gr) throws JsonParseException, JsonMappingException, IOException {
85 it.polito.neo4j.jaxb.Graph graph;
86 graph=(new ObjectFactory()).createGraph();
87 graph.setId(gr.getId());
89 List<it.polito.neo4j.jaxb.Node> nodes=new ArrayList<it.polito.neo4j.jaxb.Node>();
90 for(Map.Entry<Long, it.polito.verigraph.model.Node> c : gr.getNodes().entrySet()){
91 it.polito.neo4j.jaxb.Node node=(new ObjectFactory()).createNode();
92 node.setId(c.getValue().getId());
93 node.setName(c.getValue().getName());
94 node.setFunctionalType(FunctionalTypes.fromValue(c.getValue().getFunctional_type().toUpperCase()));
95 List<Neighbour> neighbours=new ArrayList<Neighbour>();
98 for(Map.Entry<Long, it.polito.verigraph.model.Neighbour> a : c.getValue().getNeighbours().entrySet()){
99 Neighbour neighbour=(new ObjectFactory()).createNeighbour();
100 neighbour.setId(a.getValue().getId());
101 neighbour.setName(a.getValue().getName());
102 neighbours.add(neighbour);
104 node.getNeighbour().addAll(neighbours);
106 /* setConfigurations */
107 it.polito.neo4j.jaxb.Configuration configuration=(new ObjectFactory()).createConfiguration();
108 JsonNode json=c.getValue().getConfiguration().getConfiguration();
109 setConfiguration(configuration, c.getValue(), json);
110 node.setConfiguration(configuration);
113 graph.getNode().addAll(nodes);
117 public static it.polito.neo4j.jaxb.Configuration ConfToNeo4j(it.polito.verigraph.model.Configuration nodeConfiguration, it.polito.verigraph.model.Node node) throws JsonParseException, JsonMappingException, IOException {
118 it.polito.neo4j.jaxb.Configuration configuration=(new ObjectFactory()).createConfiguration();
119 JsonNode nodes=nodeConfiguration.getConfiguration();
120 setConfiguration(configuration, node, nodes);
122 return configuration;
126 @SuppressWarnings("unchecked")
127 private static void setConfiguration(it.polito.neo4j.jaxb.Configuration configuration, it.polito.verigraph.model.Node node, JsonNode nodes) throws JsonParseException, JsonMappingException, IOException {
131 switch(node.getFunctional_type().toUpperCase()){
133 configuration.setName(node.getFunctional_type().toLowerCase());
134 Firewall firewall=new Firewall();
135 List<Elements> elements_list=new ArrayList<Elements>();
137 if(!nodes.toString().equals(empty)){
138 ObjectMapper mapper=new ObjectMapper();
139 java.util.Map<String, String> map=new LinkedHashMap();
141 Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
143 input=matcher.group(1);
145 input=nodes.toString();
146 Pattern pattern=Pattern.compile("\\{([^\\}]*)\\}");
147 List<String> list = new ArrayList<String>();
148 Matcher match= pattern.matcher(input);
149 while (match.find()) {
150 list.add(match.group());
154 for(String string : list){
155 map.putAll(mapper.readValue(string, LinkedHashMap.class));
157 for(java.util.Map.Entry<String, String> m : map.entrySet()){
158 Elements e=new Elements();
159 e.setDestination(m.getKey());
160 e.setSource(m.getValue());
161 elements_list.add(e);
164 }catch(JsonGenerationException e) {
166 } catch (JsonMappingException e) {
168 } catch (IOException e) {
173 vlogger.logger.info("elements_list of Firewall " +node.getName()+" empty");
175 firewall.getElements().addAll(elements_list);
176 configuration.setFirewall(firewall);
180 configuration.setName(node.getFunctional_type().toLowerCase());
181 Antispam antispam=new Antispam();
182 List<String> source=new ArrayList<String>();
184 if(!nodes.toString().equals(empty)){
185 ObjectMapper mapper=new ObjectMapper();
186 List<String> list=new ArrayList<String>();
190 list = mapper.readValue(nodes.toString(), ArrayList.class);
192 for(String s : list){
196 } catch (JsonGenerationException e) {
198 } catch (JsonMappingException e) {
200 } catch (IOException e) {
205 vlogger.logger.info("Antispam " +node.getName()+" empty");
208 antispam.getSource().addAll(source);
209 configuration.setAntispam(antispam);
213 configuration.setName(node.getFunctional_type().toLowerCase());
214 Cache cache=new Cache();
215 List<String> resource=new ArrayList<String>();
217 if(!nodes.toString().equals(empty)){
218 ObjectMapper mapper=new ObjectMapper();
220 List<String> list=new ArrayList<String>();
223 list = mapper.readValue(nodes.toString(), ArrayList.class);
225 for(String s : list){
229 } catch (JsonGenerationException e) {
231 } catch (JsonMappingException e) {
233 } catch (IOException e) {
238 vlogger.logger.info("Cache " +node.getName()+" empty");
241 cache.getResource().addAll(resource);
242 configuration.setCache(cache);
247 configuration.setName(node.getFunctional_type().toLowerCase());
249 List<String> notAllowed=new ArrayList<String>();
251 if(!nodes.toString().equals(empty)){
252 ObjectMapper mapper=new ObjectMapper();
254 List<String> list=new ArrayList<String>();
257 list = mapper.readValue(nodes.toString(), ArrayList.class);
259 for(String s : list){
263 } catch (JsonGenerationException e) {
265 } catch (JsonMappingException e) {
267 } catch (IOException e) {
273 vlogger.logger.info("Dpi " +node.getName()+" empty");
276 dpi.getNotAllowed().addAll(notAllowed);
277 configuration.setDpi(dpi);
281 configuration.setName(node.getFunctional_type().toLowerCase());
282 Endhost endhost=new Endhost();
284 if(!nodes.toString().equals(empty)){
285 ObjectMapper mapper=new ObjectMapper();
286 java.util.Map<String, String> map=new LinkedHashMap();
288 Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
290 input=matcher.group(1);
292 input=nodes.toString();
295 map=mapper.readValue(input, java.util.LinkedHashMap.class);
296 for(java.util.Map.Entry<String, String> m : map.entrySet()){
299 endhost.setBody(m.getValue());
303 endhost.setSequence(new BigInteger(m.getValue()));
307 endhost.setProtocol(ProtocolTypes.fromValue(m.getValue()));
311 endhost.setEmailFrom(m.getValue());
315 endhost.setUrl(m.getValue());
319 endhost.setOptions(m.getValue());
323 endhost.setDestination(m.getValue());
329 }catch(JsonGenerationException e) {
331 } catch (JsonMappingException e) {
333 } catch (IOException e) {
338 vlogger.logger.info("Endhost " +node.getName()+" empty");
341 configuration.setEndhost(endhost);
345 configuration.setName(node.getFunctional_type().toLowerCase());
346 Endpoint endpoint=new Endpoint();
347 configuration.setEndpoint(endpoint);
350 case "FIELDMODIFIER":{
352 configuration.setName(node.getFunctional_type());
353 Fieldmodifier fieldmodifier=new Fieldmodifier();
354 configuration.setFieldmodifier(fieldmodifier);
359 configuration.setName(node.getFunctional_type().toLowerCase());
360 Mailclient mailclient=new Mailclient();
362 ObjectMapper mapper=new ObjectMapper();
363 java.util.Map<String, String> map=new LinkedHashMap();
365 Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
367 input=matcher.group(1);
369 input=nodes.toString();
371 Pattern pattern=Pattern.compile("\\{([^\\}]*)\\}");
372 List<String> list = new ArrayList<String>();
373 Matcher match= pattern.matcher(input);
374 while (match.find()) {
375 list.add(match.group());
379 for(String string : list){
380 map.putAll(mapper.readValue(string, LinkedHashMap.class));
381 if(map!=null || !map.isEmpty()){
382 for(java.util.Map.Entry<String, String> m : map.entrySet()){
385 mailclient.setMailserver(m.getValue());
389 vlogger.logger.info("\"mailserver\" object is required");
395 }catch(JsonGenerationException e) {
397 } catch (JsonMappingException e) {
399 } catch (IOException e) {
402 configuration.setMailclient(mailclient);
406 configuration.setName(node.getFunctional_type().toLowerCase());
407 Mailserver mailserver=new Mailserver();
408 configuration.setMailserver(mailserver);
412 configuration.setName(node.getFunctional_type().toLowerCase());
414 List<String> source=new ArrayList<String>();
416 if(!nodes.toString().equals(empty)){
417 ObjectMapper mapper=new ObjectMapper();
418 List<String> list=new ArrayList<String>();
421 list = mapper.readValue(nodes.toString(), ArrayList.class);
423 for(String s : list){
426 } catch (JsonGenerationException e) {
428 } catch (JsonMappingException e) {
430 } catch (IOException e) {
435 vlogger.logger.info("Nat " +node.getName()+" empty");
437 nat.getSource().addAll(source);
438 configuration.setNat(nat);
442 configuration.setName(node.getFunctional_type().toLowerCase());
443 Vpnaccess vpnaccess=new Vpnaccess();
444 ObjectMapper mapper=new ObjectMapper();
445 java.util.Map<String, String> map=new LinkedHashMap();
447 Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
449 input=matcher.group(1);
451 input=nodes.toString();
453 Pattern pattern=Pattern.compile("\\{([^\\}]*)\\}");
454 List<String> list = new ArrayList<String>();
455 Matcher match= pattern.matcher(input);
456 while (match.find()) {
457 list.add(match.group());
460 for(String string : list){
461 map.putAll(mapper.readValue(string, LinkedHashMap.class));
462 if(map!=null || !map.isEmpty()){
463 for(java.util.Map.Entry<String, String> m : map.entrySet()){
466 vpnaccess.setVpnexit(m.getValue());
470 vlogger.logger.info("\"vpnexit\" is required");
476 }catch(JsonGenerationException e) {
478 } catch (JsonMappingException e) {
480 } catch (IOException e) {
483 configuration.setVpnaccess(vpnaccess);
487 configuration.setName(node.getFunctional_type().toLowerCase());
488 Vpnexit vpnexit=new Vpnexit();
490 ObjectMapper mapper=new ObjectMapper();
491 java.util.Map<String, String> map=new LinkedHashMap();
493 Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
495 input=matcher.group(1);
497 input=nodes.toString();
499 Pattern pattern=Pattern.compile("\\{([^\\}]*)\\}");
500 List<String> list = new ArrayList<String>();
501 Matcher match= pattern.matcher(input);
502 while (match.find()) {
503 list.add(match.group());
507 for(String string : list){
508 /* there is only 1 string */
509 map.putAll(mapper.readValue(string, LinkedHashMap.class));
510 if(map!=null || !map.isEmpty()){
511 for(java.util.Map.Entry<String, String> m : map.entrySet()){
514 vpnexit.setVpnaccess(m.getValue());
518 vlogger.logger.info("\"vpnaccess\" is required");
525 }catch(JsonGenerationException e) {
527 } catch (JsonMappingException e) {
529 } catch (IOException e) {
532 configuration.setVpnexit(vpnexit);
536 configuration.setName(node.getFunctional_type().toLowerCase());
537 Webclient webclient=new Webclient();
538 ObjectMapper mapper=new ObjectMapper();
539 java.util.Map<String, String> map=new LinkedHashMap();
541 Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
543 input=matcher.group(1);
545 input=nodes.toString();
547 Pattern pattern=Pattern.compile("\\{([^\\}]*)\\}");
548 List<String> list = new ArrayList<String>();
549 Matcher match= pattern.matcher(input);
550 while (match.find()) {
551 list.add(match.group());
554 for(String string : list){
555 map.putAll(mapper.readValue(string, LinkedHashMap.class));
556 if(map!=null || !map.isEmpty()){
557 for(java.util.Map.Entry<String, String> m : map.entrySet()){
560 webclient.setNameWebServer(m.getValue());
564 vlogger.logger.info("\"webserver\" object is required");
571 }catch(JsonGenerationException e) {
573 } catch (JsonMappingException e) {
575 } catch (IOException e) {
578 configuration.setWebclient(webclient);
582 configuration.setName(node.getFunctional_type().toLowerCase());
583 Webserver webserver=new Webserver();
584 configuration.setWebserver(webserver);
590 public static Neighbour NeighbourToNeo4j(it.polito.verigraph.model.Neighbour n){
591 Neighbour neighbourRoot;
592 neighbourRoot=(new ObjectFactory()).createNeighbour();
593 neighbourRoot.setId(n.getId());
594 neighbourRoot.setName(n.getName());
595 return neighbourRoot;
598 public static Node NodeToNeo4j(it.polito.verigraph.model.Node n) throws JsonParseException, JsonMappingException, IOException{
600 it.polito.neo4j.jaxb.Configuration configuration=(new ObjectFactory()).createConfiguration();
601 nodeRoot=(new ObjectFactory()).createNode();
602 nodeRoot.setId(n.getId());
603 nodeRoot.setName(n.getName());
604 nodeRoot.setFunctionalType(FunctionalTypes.fromValue(n.getFunctional_type().toUpperCase()));
605 for(Map.Entry<Long, it.polito.verigraph.model.Neighbour> neighbour : n.getNeighbours().entrySet()){
606 it.polito.verigraph.model.Neighbour neighb=neighbour.getValue();
607 nodeRoot.getNeighbour().add(NeighbourToNeo4j(neighb));
609 JsonNode json=n.getConfiguration().getConfiguration();
610 setConfiguration(configuration, n, json);
611 nodeRoot.setConfiguration(configuration);