summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/escape/verify/validation/VpnexitValidator.java
blob: 782e124793269792c19d49feaff77bdb0faad76d (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
/*******************************************************************************
 * 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.escape.verify.validation;

import com.fasterxml.jackson.databind.JsonNode;

import it.polito.escape.verify.model.Configuration;
import it.polito.escape.verify.model.Graph;
import it.polito.escape.verify.model.Node;
import it.polito.escape.verify.validation.exception.ValidationException;

public class VpnexitValidator implements ValidationInterface {

	@Override
	public void validate(Graph graph, Node node, Configuration configuration) throws ValidationException {
		JsonNode configurationNode = configuration.getConfiguration();

		if (!configurationNode.isArray())
			throw new ValidationException("configuration must be an array");

		for (JsonNode object : configurationNode) {
			JsonNode vpnexit = object.get("vpnaccess");
			if (!vpnexit.isTextual())
				throw new ValidationException("value corresponding to the key 'vpnaccess' must be a string");
			validateObject(graph, node, vpnexit.asText());
		}

	}

	private void validateObject(Graph g, Node node, String field) throws ValidationException {
		boolean isValid = false;
		for (Node n : g.getNodes().values()) {
			if ((n.getFunctional_type().equals("vpnaccess")) && (n.getName().equals(field)))
				isValid = true;
		}

		if (!isValid)
			throw new ValidationException("'"	+ field + "' is not a valid value for the configuration of a type '"
											+ node.getFunctional_type()
											+ "'. Please use the name of an existing node of type 'vpnaccess'.");

	}

}