update verigraph
[parser.git] / verigraph / src / it / polito / verigraph / validation / VpnaccessValidator.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.verigraph.validation;
10
11 import com.fasterxml.jackson.databind.JsonNode;
12
13 import it.polito.verigraph.model.Configuration;
14 import it.polito.verigraph.model.Graph;
15 import it.polito.verigraph.model.Node;
16 import it.polito.verigraph.validation.exception.ValidationException;
17
18 public class VpnaccessValidator implements ValidationInterface {
19
20     @Override
21     public void validate(Graph graph, Node node, Configuration configuration) throws ValidationException {
22         JsonNode configurationNode = configuration.getConfiguration();
23
24         if (!configurationNode.isArray())
25             throw new ValidationException("configuration must be an array");
26
27         for (JsonNode object : configurationNode) {
28             JsonNode vpnexit = object.get("vpnexit");
29             if (!vpnexit.isTextual())
30                 throw new ValidationException("value corresponding to the key 'vpnexit' must be a string");
31             validateObject(graph, node, vpnexit.asText());
32         }
33
34     }
35
36     private void validateObject(Graph g, Node node, String field) throws ValidationException {
37         boolean isValid = false;
38         for (Node n : g.getNodes().values()) {
39             if ((n.getFunctional_type().equals("vpnexit")) && (n.getName().equals(field)))
40                 isValid = true;
41         }
42
43         if (!isValid)
44             throw new ValidationException("'"+ field + "' is not a valid value for the configuration of a type '"
45                     + node.getFunctional_type()
46                     + "'. Please use the name of an existing node of type 'vpnexit'.");
47
48     }
49 }