summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/escape/verify/service/NodeService.java
blob: e6ad67219d9c9cee4ebb69d305087e9776f25d87 (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
/*******************************************************************************
 * 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.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import javax.ws.rs.InternalServerErrorException;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchema;

import it.polito.escape.verify.database.DatabaseClass;
import it.polito.escape.verify.exception.BadRequestException;
import it.polito.escape.verify.exception.DataNotFoundException;
import it.polito.escape.verify.exception.ForbiddenException;
import it.polito.escape.verify.model.Configuration;
import it.polito.escape.verify.model.Graph;
import it.polito.escape.verify.model.Neighbour;
import it.polito.escape.verify.model.Node;

public class NodeService {

	private Map<Long, Graph> graphs = DatabaseClass.getInstance().getGraphs();

	public NodeService() {

	}

	public List<Node> getAllNodes(long graphId) {
		if (graphId <= 0) {
			throw new ForbiddenException("Illegal graph id: " + graphId);
		}
		Graph graph = graphs.get(graphId);
		if (graph == null)
			throw new DataNotFoundException("Graph with id " + graphId + " not found");
		Map<Long, Node> nodes = graph.getNodes();
		return new ArrayList<Node>(nodes.values());
	}

	public Node getNode(long graphId, long nodeId) {
		if (graphId <= 0) {
			throw new ForbiddenException("Illegal graph id: " + graphId);
		}
		if (nodeId <= 0) {
			throw new ForbiddenException("Illegal node id: " + nodeId);
		}
		Graph graph = graphs.get(graphId);
		if (graph == null)
			throw new DataNotFoundException("Graph with id " + graphId + " not found");
		Map<Long, Node> nodes = graph.getNodes();
		Node node = nodes.get(nodeId);
		if (node == null) {
			throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
		}
		return node;
	}

	public Node updateNode(long graphId, Node node) {
		if (graphId <= 0) {
			throw new ForbiddenException("Illegal graph id: " + graphId);
		}
		if (node.getId() <= 0) {
			throw new ForbiddenException("Illegal node id: " + node.getId());
		}
		Graph graph = graphs.get(graphId);
		if (graph == null)
			throw new DataNotFoundException("Graph with id " + graphId + " not found");
		Map<Long, Node> nodes = graph.getNodes();
		Node localNode = nodes.get(node.getId());
		if (localNode == null) {
			throw new DataNotFoundException("Node with id " + node.getId() + " not found in graph with id " + graphId);
		}

		Graph graphCopy = new Graph();
		graphCopy.setId(graph.getId());
		graphCopy.setNodes(new HashMap<Long, Node>(graph.getNodes()));
		graphCopy.getNodes().remove(node.getId());

		// int numberOfNeighbours = 0;
		// for(Neighbour neighbour : node.getNeighbours().values()){
		// neighbour.setId(++numberOfNeighbours);
		// }

		for (Map.Entry<Long, Neighbour> neighbourEntry : node.getNeighbours().entrySet()) {
			neighbourEntry.getValue().setId(neighbourEntry.getKey());
		}

		validateNode(graphCopy, node);

		synchronized (this) {
			nodes.put(node.getId(), node);
			DatabaseClass.persistDatabase();
			return node;
		}
	}

	public Node removeNode(long graphId, long nodeId) {
		if (graphId <= 0) {
			throw new ForbiddenException("Illegal graph id: " + graphId);
		}
		if (nodeId <= 0) {
			throw new ForbiddenException("Illegal node id: " + nodeId);
		}
		Graph graph = graphs.get(graphId);
		if (graph == null)
			throw new DataNotFoundException("Graph with id " + graphId + " not found");
		Map<Long, Node> nodes = graph.getNodes();

		synchronized (this) {
			return nodes.remove(nodeId);
		}
	}

	public Node addNode(long graphId, Node node) {
		if (graphId <= 0) {
			throw new ForbiddenException("Illegal graph id: " + graphId);
		}
		Graph graph = graphs.get(graphId);
		if (graph == null)
			throw new DataNotFoundException("Graph with id " + graphId + " not found");
		Map<Long, Node> nodes = graph.getNodes();

		validateNode(graph, node);

		synchronized (this) {
			node.setId(DatabaseClass.getInstance().getGraphNumberOfNodes(graphId) + 1);
		}

		// int numberOfNeighbours = 0;

		for (Map.Entry<Long, Neighbour> neighbourEntry : node.getNeighbours().entrySet()) {
			neighbourEntry.getValue().setId(neighbourEntry.getKey());
		}

		// for (Neighbour neighbour : node.getNeighbours().values()) {
		// neighbour.setId(++numberOfNeighbours);
		// }

		synchronized (this) {
			nodes.put(node.getId(), node);
			DatabaseClass.persistDatabase();
			return node;
		}
	}

	public Node searchByName(long graphId, String nodeName) {
		if (graphId <= 0) {
			throw new ForbiddenException("Illegal graph id: " + graphId);
		}
		Graph graph = graphs.get(graphId);
		if (graph == null)
			throw new DataNotFoundException("Graph with id " + graphId + " not found");
		Map<Long, Node> nodes = graph.getNodes();

		for (Node node : nodes.values()) {
			if (node.getName().equals(nodeName))
				return node;
		}
		return null;
	}

	public static void validateNode(Graph graph, Node node) {
		if (graph == null)
			throw new BadRequestException("Node validation failed: cannot validate null graph");
		if (node == null)
			throw new BadRequestException("Node validation failed: cannot validate null node");

		if (node.getName() == null)
			throw new BadRequestException("Node validation failed: node 'name' field cannot be null");
		if (node.getFunctional_type() == null)
			throw new BadRequestException("Node validation failed: node 'functional_type' field cannot be null");

		if (node.getName().equals(""))
			throw new BadRequestException("Node validation failed: node 'name' field cannot be an empty string");
		if (node.getFunctional_type().equals(""))
			throw new BadRequestException("Node validation failed: node 'functional_type' field cannot be an empty string");

		Node nodeFound = graph.searchNodeByName(node.getName());
		if ((nodeFound != null) && (nodeFound.equals(node) == false))
			throw new BadRequestException("Node validation failed: graph already has a node named '"	+ node.getName()
											+ "'");
		Configuration configuration = node.getConfiguration();
		if (configuration != null) {
			JsonNode configurationJsonNode = configuration.getConfiguration();
			// validate configuration against schema file
			validateNodeConfigurationAgainstSchemaFile(node, configurationJsonNode);
			JsonValidationService jsonValidator = new JsonValidationService(graph, node);
			boolean hasCustomValidator = jsonValidator.validateNodeConfiguration();
			if (!hasCustomValidator) {
				jsonValidator.validateFieldsAgainstNodeNames(configurationJsonNode);
			}
		}

		// validate neighbours
		Map<Long, Neighbour> nodeNeighboursMap = node.getNeighbours();
		if (nodeNeighboursMap == null)
			throw new BadRequestException("Node validation failed: node 'neighbours' cannot be null");
		for (Neighbour neighbour : nodeNeighboursMap.values()) {
			NeighbourService.validateNeighbour(graph, node, neighbour);
		}
	}

	public static void validateNodeConfigurationAgainstSchemaFile(Node node, JsonNode configurationJson) {
		String schemaFileName = node.getFunctional_type() + ".json";

		File schemaFile = new File(System.getProperty("catalina.base") + "/webapps/verify/json/" + schemaFileName);

		if (!schemaFile.exists()) {
			//if no REST client, try gRPC application
			schemaFile = new File("src/main/webapp/json/" + schemaFileName);

			if (!schemaFile.exists()) {
				throw new ForbiddenException("Functional type '"	+ node.getFunctional_type()
											+ "' is not supported! Please edit 'functional_type' field of node '"
											+ node.getName() + "'");
			}
		}

		JsonSchema schemaNode = null;
		try {
			schemaNode = ValidationUtils.getSchemaNode(schemaFile);
		}
		catch (IOException e) {
			throw new InternalServerErrorException("Unable to load '" + schemaFileName + "' schema file");
		}
		catch (ProcessingException e) {
			throw new InternalServerErrorException("Unable to resolve '"	+ schemaFileName
													+ "' schema file as a schema node");
		}

		try {
			ValidationUtils.validateJson(schemaNode, configurationJson);
		}
		catch (ProcessingException e) {
			throw new BadRequestException("Something went wrong trying to validate node '"	+ node.getName()
											+ "' with the following configuration: '" + configurationJson.toString()
											+ "' against the json schema '" + schemaFile.getName() + "': "
											+ e.getMessage());

		}

	}
}