summaryrefslogtreecommitdiffstats
path: root/dashboard/src/dashboard/templatetags/__init__.py
diff options
context:
space:
mode:
authorParker Berberian <pberberian@iol.unh.edu>2019-06-26 13:15:31 +0000
committerGerrit Code Review <gerrit@opnfv.org>2019-06-26 13:15:31 +0000
commitfce290547954709c5b6184fddf31b370d23bbf0a (patch)
treeecebfc7ec9d5174703bfb2a97123ca30e87fca1b /dashboard/src/dashboard/templatetags/__init__.py
parent032376249e846f487323ee98ece5bf0844fbee28 (diff)
parent2941c38938e06f205d9cbb2a1ef9f0145c5a4c66 (diff)
Merge "Fixes a typo"HEADmaster
Diffstat (limited to 'dashboard/src/dashboard/templatetags/__init__.py')
0 files changed, 0 insertions, 0 deletions
n100'>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());

		}

	}
}