update verigraph
[parser.git] / verigraph / src / it / polito / neo4j / translator / GraphToNeo4j.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
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;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI;
19
20 import java.io.File;
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;
28
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;
44
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;
56
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;
81
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());
88
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>();
96
97             /* setNeighbours*/
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);
103             }
104             node.getNeighbour().addAll(neighbours);
105
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);
111             nodes.add(node);
112         }
113         graph.getNode().addAll(nodes);
114         return graph;
115     }
116
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);
121
122         return configuration;
123
124     }
125
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 {
128
129         String empty="[]";
130
131         switch(node.getFunctional_type().toUpperCase()){
132         case "FIREWALL":{
133             configuration.setName(node.getFunctional_type().toLowerCase());
134             Firewall firewall=new Firewall();
135             List<Elements> elements_list=new ArrayList<Elements>();
136
137             if(!nodes.toString().equals(empty)){
138                 ObjectMapper mapper=new ObjectMapper();
139                 java.util.Map<String, String> map=new LinkedHashMap();
140                 String input;
141                 Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
142                 if(matcher.find()){
143                     input=matcher.group(1);
144                 }else
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());
151                 }
152
153                 try{
154                     for(String string : list){
155                         map.putAll(mapper.readValue(string, LinkedHashMap.class));
156                     }
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);
162                     }
163
164                 }catch(JsonGenerationException e) {
165                     e.printStackTrace();
166                 } catch (JsonMappingException e) {
167                     e.printStackTrace();
168                 } catch (IOException e) {
169                     e.printStackTrace();
170                 }
171             }
172             else{
173                 vlogger.logger.info("elements_list of Firewall " +node.getName()+" empty");
174             }
175             firewall.getElements().addAll(elements_list);
176             configuration.setFirewall(firewall);
177             break;
178         }
179         case "ANTISPAM":{
180             configuration.setName(node.getFunctional_type().toLowerCase());
181             Antispam antispam=new Antispam();
182             List<String> source=new ArrayList<String>();
183
184             if(!nodes.toString().equals(empty)){
185                 ObjectMapper mapper=new ObjectMapper();
186                 List<String> list=new ArrayList<String>();
187
188                 try {
189
190                     list = mapper.readValue(nodes.toString(), ArrayList.class);
191
192                     for(String s : list){
193                         source.add(s);
194                     }
195
196                 } catch (JsonGenerationException e) {
197                     e.printStackTrace();
198                 } catch (JsonMappingException e) {
199                     e.printStackTrace();
200                 } catch (IOException e) {
201                     e.printStackTrace();
202                 }
203             }
204             else{
205                 vlogger.logger.info("Antispam " +node.getName()+" empty");
206
207             }
208             antispam.getSource().addAll(source);
209             configuration.setAntispam(antispam);
210             break;
211         }
212         case "CACHE":{
213             configuration.setName(node.getFunctional_type().toLowerCase());
214             Cache cache=new Cache();
215             List<String> resource=new ArrayList<String>();
216
217             if(!nodes.toString().equals(empty)){
218                 ObjectMapper mapper=new ObjectMapper();
219
220                 List<String> list=new ArrayList<String>();
221                 try {
222
223                     list = mapper.readValue(nodes.toString(), ArrayList.class);
224
225                     for(String s : list){
226                         resource.add(s);
227                     }
228
229                 } catch (JsonGenerationException e) {
230                     e.printStackTrace();
231                 } catch (JsonMappingException e) {
232                     e.printStackTrace();
233                 } catch (IOException e) {
234                     e.printStackTrace();
235                 }
236             }
237             else{
238                 vlogger.logger.info("Cache " +node.getName()+" empty");
239
240             }
241             cache.getResource().addAll(resource);
242             configuration.setCache(cache);
243             break;
244
245         }
246         case "DPI":{
247             configuration.setName(node.getFunctional_type().toLowerCase());
248             Dpi dpi=new Dpi();
249             List<String> notAllowed=new ArrayList<String>();
250
251             if(!nodes.toString().equals(empty)){
252                 ObjectMapper mapper=new ObjectMapper();
253
254                 List<String> list=new ArrayList<String>();
255                 try {
256
257                     list = mapper.readValue(nodes.toString(), ArrayList.class);
258
259                     for(String s : list){
260                         notAllowed.add(s);
261                     }
262
263                 } catch (JsonGenerationException e) {
264                     e.printStackTrace();
265                 } catch (JsonMappingException e) {
266                     e.printStackTrace();
267                 } catch (IOException e) {
268                     e.printStackTrace();
269                 }
270
271             }
272             else{
273                 vlogger.logger.info("Dpi " +node.getName()+"  empty");
274
275             }
276             dpi.getNotAllowed().addAll(notAllowed);
277             configuration.setDpi(dpi);
278             break;
279         }
280         case "ENDHOST":{
281             configuration.setName(node.getFunctional_type().toLowerCase());
282             Endhost endhost=new Endhost();
283
284             if(!nodes.toString().equals(empty)){
285                 ObjectMapper mapper=new ObjectMapper();
286                 java.util.Map<String, String> map=new LinkedHashMap();
287                 String input;
288                 Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
289                 if(matcher.find()){
290                     input=matcher.group(1);
291                 }else
292                     input=nodes.toString();
293
294                 try{
295                     map=mapper.readValue(input, java.util.LinkedHashMap.class);
296                     for(java.util.Map.Entry<String, String> m : map.entrySet()){
297                         switch(m.getKey()){
298                         case "body":{
299                             endhost.setBody(m.getValue());
300                             break;
301                         }
302                         case"sequence":{
303                             endhost.setSequence(new BigInteger(m.getValue()));
304                             break;
305                         }
306                         case "protocol":{
307                             endhost.setProtocol(ProtocolTypes.fromValue(m.getValue()));
308                             break;
309                         }
310                         case "email_from":{
311                             endhost.setEmailFrom(m.getValue());
312                             break;
313                         }
314                         case "url":{
315                             endhost.setUrl(m.getValue());
316                             break;
317                         }
318                         case "option":{
319                             endhost.setOptions(m.getValue());
320                             break;
321                         }
322                         case "destination":{
323                             endhost.setDestination(m.getValue());
324                             break;
325                         }
326                         }
327                     }
328
329                 }catch(JsonGenerationException e) {
330                     e.printStackTrace();
331                 } catch (JsonMappingException e) {
332                     e.printStackTrace();
333                 } catch (IOException e) {
334                     e.printStackTrace();
335                 }
336             }
337             else{
338                 vlogger.logger.info("Endhost " +node.getName()+" empty");
339
340             }
341             configuration.setEndhost(endhost);
342             break;
343         }
344         case "ENDPOINT":{
345             configuration.setName(node.getFunctional_type().toLowerCase());
346             Endpoint endpoint=new Endpoint();
347             configuration.setEndpoint(endpoint);
348             break;
349         }
350         case "FIELDMODIFIER":{
351
352             configuration.setName(node.getFunctional_type());
353             Fieldmodifier fieldmodifier=new  Fieldmodifier();
354             configuration.setFieldmodifier(fieldmodifier);
355             break;
356         }
357
358         case "MAILCLIENT":{
359             configuration.setName(node.getFunctional_type().toLowerCase());
360             Mailclient mailclient=new Mailclient();
361
362             ObjectMapper mapper=new ObjectMapper();
363             java.util.Map<String, String> map=new LinkedHashMap();
364             String input;
365             Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
366             if(matcher.find()){
367                 input=matcher.group(1);
368             }else
369                 input=nodes.toString();
370
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());
376             }
377
378             try{
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()){
383                             switch(m.getKey()){
384                             case "mailserver":{
385                                 mailclient.setMailserver(m.getValue());
386                                 break;
387                             }
388                             default:
389                                 vlogger.logger.info("\"mailserver\" object is required");
390                                 break;
391                             }
392                         }
393                     }
394                 }
395             }catch(JsonGenerationException e) {
396                 e.printStackTrace();
397             } catch (JsonMappingException e) {
398                 e.printStackTrace();
399             } catch (IOException e) {
400                 e.printStackTrace();
401             }
402             configuration.setMailclient(mailclient);
403             break;
404         }
405         case "MAILSERVER":{
406             configuration.setName(node.getFunctional_type().toLowerCase());
407             Mailserver mailserver=new Mailserver();
408             configuration.setMailserver(mailserver);
409             break;
410         }
411         case "NAT":{
412             configuration.setName(node.getFunctional_type().toLowerCase());
413             Nat nat=new Nat();
414             List<String> source=new ArrayList<String>();
415
416             if(!nodes.toString().equals(empty)){
417                 ObjectMapper mapper=new ObjectMapper();
418                 List<String> list=new ArrayList<String>();
419                 try {
420
421                     list = mapper.readValue(nodes.toString(), ArrayList.class);
422
423                     for(String s : list){
424                         source.add(s);
425                     }
426                 } catch (JsonGenerationException e) {
427                     e.printStackTrace();
428                 } catch (JsonMappingException e) {
429                     e.printStackTrace();
430                 } catch (IOException e) {
431                     e.printStackTrace();
432                 }
433             }
434             else{
435                 vlogger.logger.info("Nat " +node.getName()+" empty");
436             }
437             nat.getSource().addAll(source);
438             configuration.setNat(nat);
439             break;
440         }
441         case "VPNACCESS":{
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();
446             String input;
447             Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
448             if(matcher.find()){
449                 input=matcher.group(1);
450             }else
451                 input=nodes.toString();
452
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());
458             }
459             try{
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()){
464                             switch(m.getKey()){
465                             case "vpnexit":{
466                                 vpnaccess.setVpnexit(m.getValue());
467                                 break;
468                             }
469                             default:
470                                 vlogger.logger.info("\"vpnexit\" is required");
471                                 break;
472                             }
473                         }
474                     }
475                 }
476             }catch(JsonGenerationException e) {
477                 e.printStackTrace();
478             } catch (JsonMappingException e) {
479                 e.printStackTrace();
480             } catch (IOException e) {
481                 e.printStackTrace();
482             }
483             configuration.setVpnaccess(vpnaccess);
484             break;
485         }
486         case "VPNEXIT":{
487             configuration.setName(node.getFunctional_type().toLowerCase());
488             Vpnexit vpnexit=new Vpnexit();
489
490             ObjectMapper mapper=new ObjectMapper();
491             java.util.Map<String, String> map=new LinkedHashMap();
492             String input;
493             Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
494             if(matcher.find()){
495                 input=matcher.group(1);
496             }else
497                 input=nodes.toString();
498
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());
504             }
505
506             try{
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()){
512                             switch(m.getKey()){
513                             case "vpnaccess":{
514                                 vpnexit.setVpnaccess(m.getValue());
515                                 break;
516                             }
517                             default:{
518                                 vlogger.logger.info("\"vpnaccess\" is required");
519                                 break;
520                             }
521                             }
522                         }
523                     }
524                 }
525             }catch(JsonGenerationException e) {
526                 e.printStackTrace();
527             } catch (JsonMappingException e) {
528                 e.printStackTrace();
529             } catch (IOException e) {
530                 e.printStackTrace();
531             }
532             configuration.setVpnexit(vpnexit);
533             break;
534         }
535         case "WEBCLIENT":{
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();
540             String input;
541             Matcher matcher = Pattern.compile("\\[([^\\]]*)\\]").matcher(nodes.toString());
542             if(matcher.find()){
543                 input=matcher.group(1);
544             }else
545                 input=nodes.toString();
546
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());
552             }
553             try{
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()){
558                             switch(m.getKey()){
559                             case "webserver":{
560                                 webclient.setNameWebServer(m.getValue());
561                                 break;
562                             }
563                             default:{
564                                 vlogger.logger.info("\"webserver\" object is required");
565                                 break;
566                             }
567                             }
568                         }
569                     }
570                 }
571             }catch(JsonGenerationException e) {
572                 e.printStackTrace();
573             } catch (JsonMappingException e) {
574                 e.printStackTrace();
575             } catch (IOException e) {
576                 e.printStackTrace();
577             }
578             configuration.setWebclient(webclient);
579             break;
580         }
581         case "WEBSERVER":{
582             configuration.setName(node.getFunctional_type().toLowerCase());
583             Webserver webserver=new Webserver();
584             configuration.setWebserver(webserver);
585             break;
586         }
587         }
588     }
589
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;
596     }
597
598     public static Node NodeToNeo4j(it.polito.verigraph.model.Node n) throws JsonParseException, JsonMappingException, IOException{
599         Node nodeRoot;
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));
608         }
609         JsonNode json=n.getConfiguration().getConfiguration();
610         setConfiguration(configuration, n, json);
611         nodeRoot.setConfiguration(configuration);
612         return nodeRoot;
613     }
614 }