summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/client/VerifyClient.java
blob: 751e7b66b1cd7ab188535218251f99acf5ffcaeb (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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*******************************************************************************
 * Copyright (c) 2017 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.client;

import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.ResponseProcessingException;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import it.polito.verigraph.model.ErrorMessage;
import it.polito.verigraph.model.Graph;
import it.polito.verigraph.model.Neighbour;
import it.polito.verigraph.model.Node;
import it.polito.verigraph.model.Verification;

public class VerifyClient {

    private WebTarget baseTarget;
    private WebTarget graphsTarget;
    private WebTarget graphTarget;
    private WebTarget nodesTarget;
    private WebTarget nodeTarget;
    private WebTarget neighboursTarget;
    private WebTarget neighbourTarget;
    private WebTarget reachabilityTarget;
    private WebTarget isolationTarget;
    private WebTarget traversalTarget;

    public VerifyClient(String address) {
        
        Client client = ClientBuilder.newClient();
        this.baseTarget = client.target(address);
        this.graphsTarget = baseTarget.path("graphs");
        this.graphTarget = graphsTarget.path("/{graphId}");
        this.nodesTarget = graphTarget.path("/nodes");
        this.nodeTarget = nodesTarget.path("//{nodeId}");
        this.neighboursTarget = nodeTarget.path("/neighbours");
        this.neighbourTarget = neighboursTarget.path("/{neighbourId}");
        this.reachabilityTarget = graphTarget.path("/policy");
        this.isolationTarget = graphTarget.path("/policy");
        this.traversalTarget = graphTarget.path("/policy");
    }

    public void checkResponse(Response response) throws VerifyClientException {
        int status = response.getStatus();

        // 400
        if (status == Response.Status.BAD_REQUEST.getStatusCode()) {
            try {
                // String responseString = response.readEntity(String.class);
                // System.out.println(responseString);
                ErrorMessage errorMessage = response.readEntity(ErrorMessage.class);
                String message = errorMessage.getErrorMessage();
                throw new VerifyClientException("Bad request: " + message);
            }
            catch (ProcessingException e) {
                throw new VerifyClientException("the content of the message cannot be mapped to an entity of the 'ErrorMessage': "
                        + e.getMessage());
            }
            catch (IllegalStateException e) {
                throw new VerifyClientException("the entity is not backed by an input stream or the original entity input stream has already been consumed without buffering the entity data prior consuming: "
                        + e.getMessage());
            }
        }
        // 403
        if (status == Response.Status.FORBIDDEN.getStatusCode()) {
            try {
                ErrorMessage errorMessage = response.readEntity(ErrorMessage.class);
                String message = errorMessage.getErrorMessage();
                throw new VerifyClientException("Forbidden: " + message);
            }
            catch (ProcessingException e) {
                throw new VerifyClientException("the content of the message cannot be mapped to an entity of the 'ErrorMessage': "
                        + e.getMessage());
            }
            catch (IllegalStateException e) {
                throw new VerifyClientException("the entity is not backed by an input stream or the original entity input stream has already been consumed without buffering the entity data prior consuming: "
                        + e.getMessage());
            }
        }
        // 404
        if (status == Response.Status.NOT_FOUND.getStatusCode()) {
            try {
                ErrorMessage errorMessage = response.readEntity(ErrorMessage.class);
                String message = errorMessage.getErrorMessage();
                throw new VerifyClientException("Not found: " + message);
            }
            catch (ProcessingException e) {
                throw new VerifyClientException("the content of the message cannot be mapped to an entity of the 'ErrorMessage': "
                        + e.getMessage());
            }
            catch (IllegalStateException e) {
                throw new VerifyClientException("the 'Response' entity is not backed by an input stream or the original entity input stream has already been consumed without buffering the entity data prior consuming: "
                        + e.getMessage());
            }
        }
        // 500
        if (status == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
            try {
                ErrorMessage errorMessage = response.readEntity(ErrorMessage.class);
                String message = errorMessage.getErrorMessage();
                throw new VerifyClientException("Internal server error: " + message);
            }
            catch (ProcessingException e) {
                throw new VerifyClientException("the content of the message cannot be mapped to an entity of the 'ErrorMessage': "
                        + e.getMessage());
            }
            catch (IllegalStateException e) {
                throw new VerifyClientException("the entity is not backed by an input stream or the original entity input stream has already been consumed without buffering the entity data prior consuming: "
                        + e.getMessage());
            }
        }
        if (status != Response.Status.ACCEPTED.getStatusCode() && status != Response.Status.CREATED.getStatusCode()
                && status != Response.Status.NO_CONTENT.getStatusCode() && status != Response.Status.OK.getStatusCode())
            throw new VerifyClientException("Unknown error");
    }

    public Response createGraph(Graph graph) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = graphsTarget.request().post(Entity.json(graph));
        checkResponse(response);
        return response;
    }

    public Response createGraph(String graph) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = graphsTarget.request().post(Entity.entity(graph, "application/json"));
        checkResponse(response);
        return response;
    }

    public Response retrieveGraph(long graphId) throws VerifyClientException, ProcessingException {
        Response response = graphTarget.resolveTemplate("graphId", graphId).request().get();
        checkResponse(response);
        return response;
    }

    public Response updateGraph(long graphId, Graph graph) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = graphTarget.resolveTemplate("graphId", graphId).request().put(Entity.json(graph));
        checkResponse(response);
        return response;
    }

    public Response deleteGraph(long graphId) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = graphTarget.resolveTemplate("graphId", graphId).request().delete();
        checkResponse(response);
        return response;
    }

    public Response createNode(long graphId, Node node) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = nodesTarget.resolveTemplate("graphId", graphId).request().post(Entity.json(node));
        checkResponse(response);
        return response;
    }

    public Response retrieveNode(long graphId, long nodeId) throws VerifyClientException, ProcessingException {
        Response response = nodeTarget.resolveTemplate("graphId", graphId)
                .resolveTemplate("nodeId", nodeId)
                .request()
                .get();
        checkResponse(response);
        return response;
    }

    public Response updateNode(long graphId, long nodeId, Node node) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = nodeTarget.resolveTemplate("graphId", graphId)
                .resolveTemplate("nodeId", nodeId)
                .request()
                .put(Entity.json(node));
        checkResponse(response);
        return response;
    }

    public Response deleteNode(long graphId, long nodeId) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = nodeTarget.resolveTemplate("graphId", graphId)
                .resolveTemplate("nodeId", nodeId)
                .request()
                .delete();
        checkResponse(response);
        return response;
    }

    public Response createNeighbour(long graphId, long nodeId, Neighbour neighbour) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = neighboursTarget.resolveTemplate("graphId", graphId)
                .resolveTemplate("nodeId", nodeId)
                .request()
                .post(Entity.json(neighbour));
        checkResponse(response);
        return response;
    }

    public Response retrieveNeighbour(long graphId, long nodeId, long neighbourId) throws VerifyClientException, ProcessingException {
        Response response = neighbourTarget.resolveTemplate("graphId", graphId)
                .resolveTemplate("nodeId", nodeId)
                .resolveTemplate("neighbourId", neighbourId)
                .request()
                .get();
        checkResponse(response);
        return response;
    }

    public Response updateNeighbour(long graphId, long nodeId, long neighbourId,
            Neighbour neighbour) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = neighbourTarget.resolveTemplate("graphId", graphId)
                .resolveTemplate("nodeId", nodeId)
                .resolveTemplate("neighbourId", neighbourId)
                .request()
                .put(Entity.json(neighbour));
        checkResponse(response);
        return response;
    }

    public Response deleteNeighbour(long graphId, long nodeId, long neighbourId) throws VerifyClientException, ResponseProcessingException, ProcessingException {
        Response response = neighbourTarget.resolveTemplate("graphId", graphId)
                .resolveTemplate("nodeId", nodeId)
                .resolveTemplate("neighbourId", neighbourId)
                .request()
                .delete();
        checkResponse(response);
        return response;
    }

    public Verification getReachability(long graphId, String source, String destination) throws VerifyClientException, ProcessingException{
        Response response = reachabilityTarget.resolveTemplate("graphId", graphId)
                .queryParam("source", source)
                .queryParam("destination", destination)
                .queryParam("type", "reachability")
                .request()
                .get();
        checkResponse(response);
        try{
            Verification verification = response.readEntity(Verification.class);
            return verification;
        }
        catch (ProcessingException e) {
            throw new VerifyClientException("the content of the message cannot be mapped to an entity of the 'Verification': "
                    + e.getMessage());
        }
        catch (IllegalStateException e) {
            throw new VerifyClientException("the 'Verification' entity is not backed by an input stream or the original entity input stream has already been consumed without buffering the entity data prior consuming: "
                    + e.getMessage());
        }
    }

    public Verification getIsolation(long graphId, String source, String destination, String middlebox) throws VerifyClientException, ProcessingException{
        Response response = isolationTarget.resolveTemplate("graphId", graphId)
                .queryParam("source", source)
                .queryParam("destination", destination)
                .queryParam("middlebox", middlebox)
                .queryParam("type", "isolation")
                .request()
                .get();
        checkResponse(response);
        try{
            Verification verification = response.readEntity(Verification.class);
            return verification;
        }
        catch (ProcessingException e) {
            throw new VerifyClientException("the content of the message cannot be mapped to an entity of the 'Verification': "
                    + e.getMessage());
        }
        catch (IllegalStateException e) {
            throw new VerifyClientException("the 'Verification' entity is not backed by an input stream or the original entity input stream has already been consumed without buffering the entity data prior consuming: "
                    + e.getMessage());
        }
    }

    public Verification getTraversal(long graphId, String source, String destination, String middlebox) throws VerifyClientException, ProcessingException{
        Response response = traversalTarget.resolveTemplate("graphId", graphId)
                .queryParam("source", source)
                .queryParam("destination", destination)
                .queryParam("middlebox", middlebox)
                .queryParam("type", "traversal")
                .request()
                .get();
        checkResponse(response);
        try{
            Verification verification = response.readEntity(Verification.class);
            return verification;
        }
        catch (ProcessingException e) {
            throw new VerifyClientException("the content of the message cannot be mapped to an entity of the 'Verification': "
                    + e.getMessage());
        }
        catch (IllegalStateException e) {
            throw new VerifyClientException("the 'Verification' entity is not backed by an input stream or the original entity input stream has already been consumed without buffering the entity data prior consuming: "
                    + e.getMessage());
        }
    }

    @SuppressWarnings("unused")
    private static String deserializeString(File file) throws IOException {
        int len;
        char[] chr = new char[4096];
        final StringBuffer buffer = new StringBuffer();
        final FileReader reader = new FileReader(file);
        try {
            while ((len = reader.read(chr)) > 0) {
                buffer.append(chr, 0, len);
            }
        }
        finally {
            reader.close();
        }
        return buffer.toString();
    }

    public List<File> getFiles() {
        List<File> filesList = new ArrayList<File>();

        String folderString = System.getProperty("folder");
        File folder;
        if (folderString == null)
            folder = new File(System.getProperty("user.dir") + "/examples");
        else
            folder = new File(folderString);

        System.out.println("Folder set to " + folder.getAbsolutePath());

        File[] files = folder.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".json");
            }
        });

        for (File f : files) {
            filesList.add(f);
        }

        return filesList;
    }

    public Graph addGraphFromFile(File file) throws JsonParseException, JsonMappingException, IOException, Exception {
        System.out.println("Parsing graph of file '" + file.getAbsolutePath() + "'...");
        Graph graph = new ObjectMapper().readValue(file, Graph.class);
        Response createGraphResponse = createGraph(graph);
        if (createGraphResponse.getStatus() != Status.CREATED.getStatusCode()) {
            throw new Exception("Creation of graph contained in file '"+ file.getAbsolutePath() + "' returned status "
                    + createGraphResponse.getStatus());
        }
        String responseString = createGraphResponse.readEntity(String.class);
        System.out.println("Response:");
        System.out.println(responseString);
        Graph response = new ObjectMapper().readValue(responseString, Graph.class);
        printGraph(response);
        return response;
    }

    public void printGraph(Graph graph) {
        System.out.println("Graph " + graph.getId());
        for (Node n : graph.getNodes().values()) {
            System.out.println("\tNode " + n.getId());
            System.out.println("\tName " + n.getName());
            System.out.println("\tFunctional type: " + n.getFunctional_type());
            for (Neighbour neighbour : n.getNeighbours().values()) {
                System.out.println("\t\tNeighbour " + neighbour.getId());
                System.out.println("\t\tName: " + neighbour.getName());
            }
        }
    }

    public Map<String, Graph> addGraphsFromFiles(List<File> files)throws JsonParseException, JsonMappingException, IOException,
    Exception {
        Map<String, Graph> graphs = new HashMap<String, Graph>();

        for (File f : files) {
            Graph graph = addGraphFromFile(f);
            graphs.put(f.getName(), graph);
        }

        for (Map.Entry<String, Graph> graph : graphs.entrySet()) {
            System.out.println(graph.getKey() + " -> graph " + graph.getValue().getId());
        }
        System.out.println("Graphs added");

        return graphs;
    }

    public static void main(String[] args) throws IOException, Exception {
        System.out.println("Adding graphs");

        VerifyClient verifyClient = new VerifyClient("http://localhost:8080/verigraph/api");

        List<File> files = verifyClient.getFiles();
        Map<String, Graph> graphs = verifyClient.addGraphsFromFiles(files);

        for (Graph g : graphs.values()) {
            Response response = verifyClient.retrieveGraph(g.getId());
            String responseString = response.readEntity(String.class);

            System.out.println("Response");
            System.out.println(responseString);
            Graph graph = new ObjectMapper().readValue(responseString, Graph.class);
            System.out.println("Read graph " + graph.getId());
            System.out.println(response.getStatus());
        }

        Graph graph = graphs.get("budapest_sat.json");
        System.out.println("graphId set to " + graph.getId());
        System.out.println("Getting reachability from 'user1' to 'websever' in 'budapest' graph (expecting SAT)...");
        Verification verification = verifyClient.getReachability(graph.getId(), "user1", "webserver");
        System.out.println(verification.getResult());

        graph = graphs.get("budapest_unsat.json");
        System.out.println("graphId set to " + graph.getId());
        System.out.println("Getting reachability from 'user1' to 'websever' in 'budapest' graph (expecting UNSAT)...");
        verification = verifyClient.getReachability(graph.getId(), "user1", "webserver");
        System.out.println(verification.getResult());

    }

}