-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReachabilityTester.java
318 lines (295 loc) · 10.3 KB
/
ReachabilityTester.java
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package it.polito.dp2.NFFG.sol2;
import java.net.URI;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import it.polito.dp2.NFFG.FactoryConfigurationError;
import it.polito.dp2.NFFG.LinkReader;
import it.polito.dp2.NFFG.NffgReader;
import it.polito.dp2.NFFG.NffgVerifier;
import it.polito.dp2.NFFG.NffgVerifierException;
import it.polito.dp2.NFFG.NffgVerifierFactory;
import it.polito.dp2.NFFG.NodeReader;
import it.polito.dp2.NFFG.lab2.NoGraphException;
import it.polito.dp2.NFFG.lab2.ServiceException;
import it.polito.dp2.NFFG.lab2.UnknownNameException;
public class ReachabilityTester implements it.polito.dp2.NFFG.lab2.ReachabilityTester {
NffgReader loaded_nffg= null;
Set<NodeReader> nodes= new HashSet<NodeReader>();
static WebTarget target;
static Client client;
private static NffgVerifier ver;
static String baseURI= System.getProperty("it.polito.dp2.NFFG.lab2.URL");
public ReachabilityTester(){
client = ClientBuilder.newClient();
target = client.target(getBaseURI());
if(System.getProperty("it.polito.dp2.NFFG.lab2.URL")==null){
System.setProperty("it.polito.dp2.NFFG.lab2.URL", "http://localhost:8080/Neo4JXML/rest");
}
System.setProperty("it.polito.dp2.NFFG.NffgVerifierFactory", "it.polito.dp2.NFFG.Random.NffgVerifierFactoryImpl");
try {
ver= NffgVerifierFactory.newInstance().newNffgVerifier();
} catch (NffgVerifierException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace(); }
catch(Exception e){
e.printStackTrace(); }
}
@Override
public void loadNFFG(String name) throws UnknownNameException, ServiceException {
loaded_nffg=ver.getNffg(name);
//Check if NFFG is not null
if(loaded_nffg!=null){
nodes = loaded_nffg.getNodes();
deleteAllNodes(); //delete all nodes in the graph
for(NodeReader nr:nodes){
try {
Node node= new Node();
Property prop= new Property();
prop.setName("name");
prop.setValue(nr.getName());
node.getProperty().add(prop); //set name property
performNodePost(node);
for(LinkReader lr:nr.getLinks()){
Relationship rship= new Relationship();
//SOURCE NODE
Node srcnode= new Node();
Property sprop= new Property();
sprop.setName("name");
sprop.setValue(lr.getSourceNode().getName());
srcnode.getProperty().add(sprop); //set property name
rship.setSrcNode(nodeExists(srcnode));
//DESTINATION NODE
Node dstnode=new Node();
Property dprop= new Property();
dprop.setName("name");
dprop.setValue(lr.getDestinationNode().getName());
dstnode.getProperty().add(dprop); //set property name
rship.setDstNode(nodeExists(dstnode));
rship.setType("Link");
performRelationPost(rship);
}
}
catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
}//end of if
else{
System.out.println("Unknown nffg");
UnknownNameException u= new UnknownNameException();
throw u;
}
}
@Override
public boolean testReachability(String srcName, String destName)
throws ServiceException, NoGraphException, UnknownNameException {
UnknownNameException u= new UnknownNameException();
String src = null;
String dst = null;
try {
if(srcName==null || destName==null){
throw u; }
System.out.println("........DONE LOADING NFFG.............");
System.out.println("......TESTING REACHABILITY........");
System.out.println("FROM: "+srcName);
System.out.println("TO: "+destName);
//SOURCE NODE
Node s= new Node();
Property sprop= new Property();
sprop.setName("name");
sprop.setValue(srcName);
s.getProperty().add(sprop);
System.out.println("SOURCE NODE...CHECKING IF IT EXISTS");
//Check if source node already exists in the graph
for(Node n:getAllNodes()){
if(s.getProperty().get(0).getValue().equals(n.getProperty().get(0).getValue())){
src=n.getId(); //source node exists, get id
System.out.println("SOURCE NODE EXISTS...WITH ID: "+n.getId());
}
}
if(src==null){
//Source node doesn't exist, throw UnknownNameException
throw u;
}
Node d= new Node();
Property dprop= new Property();
dprop.setName("name");
dprop.setValue(destName);
d.getProperty().add(dprop);
System.out.println("DESTINATION NODE...CHECKING IF IT EXISTS");
//Check if destination node already exists in the graph
for(Node n:getAllNodes()){
if(d.getProperty().get(0).getValue().equals(n.getProperty().get(0).getValue())){
dst=n.getId();
System.out.println("DESTINATION NODE EXISTS...WITH ID: "+n.getId());
}
}
if(dst==null){
//Destination node doesn't exist, throw UnknownNameException
throw u;
}
System.out.println("SOURCE NODE ID: "+src);
System.out.println("DESTINATION NODE ID: "+dst);
List<Path> response= target.path("resource")
.path("node")
.path(src)
.path("paths").queryParam("dst", dst)
.request()
.accept(MediaType.APPLICATION_XML)
.get(new GenericType<List<Path>>() {});
System.out.println("...RECEIVED PATH GET RESPONSE");
for(Path p:response){
if(p.getRelationship().isEmpty()&&p.getNode().isEmpty()){
System.out.println("NOT REACHABLE...about to throw UnknownNameException..");
throw u;
}
else{
//REACHABLE
System.out.println("REACHABLE...");
return true;
}
}
} catch(Exception e){
throw e;
}
throw u;
}
@Override
public String getCurrentGraphName() {
if(loaded_nffg!=null){
return loaded_nffg.getName();}
else{
return null;
}
}
public int performNodePost(Node node){
Node response = null;
try {
System.out.println("--------------- Performing Node Post --------------- \n");
System.out.println("Node NAME: "+node.getProperty().get(0).getValue());
//CHECK IF NODE ALREADY EXISTS IN THE GRAPH
for(Node n:getAllNodes()){
if(node.getProperty().get(0).getValue().equals(n.getProperty().get(0).getValue())){
System.out.println();
System.out.println("RESPONSE OF EXISTING NODE POST RECEIVED");
System.out.println("Node ID: "+Integer.parseInt(n.getId()));
return Integer.parseInt(n.getId());
}
}
//CREATE THE NODE
client = ClientBuilder.newClient();
target = client.target(getBaseURI());
response= target.path("resource")
.path("node")
.request(MediaType.APPLICATION_XML)
.post(Entity.entity(node,MediaType.APPLICATION_XML),Node.class);
System.out.println();
System.out.println("RESPONSE OF NEW NODE POST RECEIVED");
System.out.println("Node ID: "+Integer.parseInt(response.getId()));
} catch(Exception e){
e.printStackTrace();
}
return Integer.parseInt(response.getId());
}
public void performRelationPost(Relationship rship){
try {
System.out.println("------------ Performing Relationship Post ------------ \n");
System.out.println("SOURCE: "+rship.getSrcNode());
System.out.println("DESTINATION: "+rship.getDstNode());
System.out.println("TYPE: "+rship.getType());
Relationship response= target.path("resource")
.path("node/"+rship.getSrcNode()+"/relationship")
.request(MediaType.APPLICATION_XML)
.post(Entity.entity(rship,MediaType.APPLICATION_XML), Relationship.class);
System.out.println("RESPONSE OF RELATIONSHIP POST RECEIVED");
System.out.println("RELATION ID: "+response.getId());
System.out.println("SOURCE: "+response.getSrcNode());
System.out.println("DESTINATION: "+response.getDstNode());
System.out.println("TYPE: "+response.getType());
} catch(Exception e){
e.printStackTrace();
}
}
public void deleteAllNodes(){
try {
System.out.println("-----DELETING ALL EXISTING NODES IN THE NEO4J SERVICE----------");
String response= target.path("resource")
.path("nodes")
.request(MediaType.APPLICATION_XML)
.accept(MediaType.TEXT_PLAIN)
.delete(String.class);
System.out.println("after delete");
System.out.println("RESPONSE OF DELETE ALL NODES RECEIVED");
System.out.println("LIST OF NODES: "+response);
System.out.println("..........................DONE DELETING..........................");
} catch(Exception e){
e.printStackTrace();
}
}
public String nodeExists(Node node){
Node nresponse = null;
try {
for(Node n:getAllNodes()){
if(node.getProperty().get(0).getValue().equals(n.getProperty().get(0).getValue())){
return n.getId();
}
}
//if node doesn't exist create the node and return it's id
System.out.println("NEW NODE IS BEING CREATED...");
client = ClientBuilder.newClient();
target = client.target(getBaseURI());
nresponse= target.path("resource")
.path("node")
.request(MediaType.APPLICATION_XML)
.post(Entity.entity(node,MediaType.APPLICATION_XML),Node.class);
} catch(Exception e){
e.printStackTrace();
}
return nresponse.getId();
}
public List<Node> getAllNodes(){
List<Node> response=null;
try {
target = client.target(getBaseURI());
response= target.path("resource")
.path("nodes")
.request()
.accept(MediaType.APPLICATION_XML)
.get(new GenericType<List<Node>>() {});
} catch(Exception e){
e.printStackTrace();
}
return response;
}
private static URI getBaseURI() {
try {
if(System.getProperty("it.polito.dp2.NFFG.lab2.URL")==null){
System.setProperty("it.polito.dp2.NFFG.lab2.URL", "http://localhost:8080/Neo4JXML/rest");
}
} catch(Exception e){
e.printStackTrace();}
return UriBuilder.fromUri(baseURI).build();
}
public static void main(String[] args) throws UnknownNameException, ServiceException {
try{
ReachabilityTester t= new ReachabilityTester();
client = ClientBuilder.newClient();
target = client.target(getBaseURI());
ReachabilityTester rt= new ReachabilityTester();
rt.deleteAllNodes();
} catch(Exception e){
e.printStackTrace();
}
}
}