Disable syslog in heat-translator for functest integration
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / service / NeighbourService.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
10 package it.polito.escape.verify.service;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15
16 import it.polito.escape.verify.database.DatabaseClass;
17 import it.polito.escape.verify.exception.BadRequestException;
18 import it.polito.escape.verify.exception.DataNotFoundException;
19 import it.polito.escape.verify.exception.ForbiddenException;
20 import it.polito.escape.verify.model.Graph;
21 import it.polito.escape.verify.model.Neighbour;
22 import it.polito.escape.verify.model.Node;
23
24 public class NeighbourService {
25
26         private Map<Long, Graph> graphs = DatabaseClass.getInstance().getGraphs();
27
28         public List<Neighbour> getAllNeighbours(long graphId, long nodeId) {
29                 if (graphId <= 0) {
30                         throw new ForbiddenException("Illegal graph id: " + graphId);
31                 }
32                 if (nodeId <= 0) {
33                         throw new ForbiddenException("Illegal node id: " + nodeId);
34                 }
35                 Graph graph = graphs.get(graphId);
36                 if (graph == null)
37                         throw new DataNotFoundException("Graph with id " + graphId + " not found");
38                 Map<Long, Node> nodes = graph.getNodes();
39                 Node node = nodes.get(nodeId);
40                 if (node == null)
41                         throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
42                 Map<Long, Neighbour> neighbours = node.getNeighbours();
43                 return new ArrayList<Neighbour>(neighbours.values());
44         }
45
46         public Neighbour getNeighbour(long graphId, long nodeId, long neighbourId) {
47                 if (graphId <= 0) {
48                         throw new ForbiddenException("Illegal graph id: " + graphId);
49                 }
50                 if (nodeId <= 0) {
51                         throw new ForbiddenException("Illegal node id: " + nodeId);
52                 }
53                 if (neighbourId <= 0) {
54                         throw new ForbiddenException("Illegal neighbour id: " + neighbourId);
55                 }
56                 Graph graph = graphs.get(graphId);
57                 if (graph == null)
58                         throw new DataNotFoundException("Graph with id " + graphId + " not found");
59                 Map<Long, Node> nodes = graph.getNodes();
60                 Node node = nodes.get(nodeId);
61                 if (node == null) {
62                         throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
63                 }
64                 Map<Long, Neighbour> neighbours = node.getNeighbours();
65                 Neighbour neighbour = neighbours.get(neighbourId);
66                 if (neighbour == null) {
67                         throw new DataNotFoundException("Neighbour with id "    + neighbourId + " not found for node with id " + nodeId
68                                                                                         + " in graph with id " + graphId);
69                 }
70                 return neighbour;
71         }
72
73         public Neighbour addNeighbour(long graphId, long nodeId, Neighbour neighbour) {
74                 if (graphId <= 0) {
75                         throw new ForbiddenException("Illegal graph id: " + graphId);
76                 }
77                 if (nodeId <= 0) {
78                         throw new ForbiddenException("Illegal node id: " + nodeId);
79                 }
80                 Graph graph = graphs.get(graphId);
81                 if (graph == null)
82                         throw new DataNotFoundException("Graph with id " + graphId + " not found");
83                 Map<Long, Node> nodes = graph.getNodes();
84                 Node node = nodes.get(nodeId);
85                 if (node == null) {
86                         throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
87                 }
88                 Map<Long, Neighbour> neighbours = node.getNeighbours();
89
90                 validateNeighbour(graph, node, neighbour);
91
92                 synchronized (this) {
93                         neighbour.setId(neighbours.size() + 1);
94                         neighbours.put(neighbour.getId(), neighbour);
95                         DatabaseClass.persistDatabase();
96                         return neighbour;
97                 }
98         }
99
100         public Neighbour updateNeighbour(long graphId, long nodeId, Neighbour neighbour) {
101                 if (graphId <= 0) {
102                         throw new ForbiddenException("Illegal graph id: " + graphId);
103                 }
104                 if (nodeId <= 0) {
105                         throw new ForbiddenException("Illegal node id: " + nodeId);
106                 }
107                 if (neighbour.getId() <= 0) {
108                         throw new ForbiddenException("Illegal neighbour id: " + nodeId);
109                 }
110                 Graph graph = graphs.get(graphId);
111                 if (graph == null)
112                         throw new DataNotFoundException("Graph with id " + graphId + " not found");
113                 Map<Long, Node> nodes = graph.getNodes();
114                 Node node = nodes.get(nodeId);
115                 if (node == null) {
116                         throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
117                 }
118                 Map<Long, Neighbour> neighbours = node.getNeighbours();
119                 Neighbour currentNeighbour = neighbours.get(neighbour.getId());
120                 if (currentNeighbour == null) {
121                         throw new DataNotFoundException("Neighbour with id "    + neighbour.getId() + " not found for node with id "
122                                                                                         + nodeId + " in graph with id " + graphId);
123                 }
124
125                 validateNeighbour(graph, node, neighbour);
126
127                 synchronized (this) {
128                         neighbours.put(neighbour.getId(), neighbour);
129                         DatabaseClass.persistDatabase();
130                         return neighbour;
131                 }
132         }
133
134         public Neighbour removeNeighbour(long graphId, long nodeId, long neighbourId) {
135                 if (graphId <= 0) {
136                         throw new ForbiddenException("Illegal graph id: " + graphId);
137                 }
138                 if (nodeId <= 0) {
139                         throw new ForbiddenException("Illegal node id: " + nodeId);
140                 }
141                 if (neighbourId <= 0) {
142                         throw new ForbiddenException("Illegal neighbour id: " + nodeId);
143                 }
144                 Graph graph = graphs.get(graphId);
145                 if (graph == null)
146                         throw new DataNotFoundException("Graph with id " + graphId + " not found");
147                 Map<Long, Node> nodes = graph.getNodes();
148                 Node node = nodes.get(nodeId);
149                 if (node == null) {
150                         throw new DataNotFoundException("Node with id " + nodeId + " not found in graph with id " + graphId);
151                 }
152                 Map<Long, Neighbour> neighbours = node.getNeighbours();
153
154                 synchronized(this){
155                         return neighbours.remove(neighbourId);
156                 }
157         }
158
159         public static void validateNeighbour(Graph graph, Node node, Neighbour neighbour) {
160                 if (graph == null)
161                         throw new BadRequestException("Neighbour validation failed: cannot validate null graph");
162                 if (node == null)
163                         throw new BadRequestException("Neighbour validation failed: cannot validate null node");
164                 if (neighbour == null)
165                         throw new BadRequestException("Neighbour validation failed: cannot validate null neighbour");
166
167                 if (neighbour.getName() == null)
168                         throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be null");
169                 if (neighbour.getName().equals(""))
170                         throw new BadRequestException("Neighbour validation failed: neighbour 'name' field cannot be an empty string");
171
172                 Node nodeFound = graph.searchNodeByName(neighbour.getName());
173                 if ((nodeFound == null) || (nodeFound.getName().equals(node.getName())))
174                         throw new BadRequestException("Neighbour validation failed: '"  + neighbour.getName()
175                                                                                         + "' is not a valid name for a neighbour of node '" + node.getName() + "'");
176
177                 Neighbour neighbourFound = node.searchNeighbourByName(neighbour.getName());
178                 if ((neighbourFound != null) && (neighbourFound.equals(neighbour) == false))
179                         throw new BadRequestException("Neighbour validation failed: node '"     + node.getName()
180                                                                                         + "' already has a neighbour named '" + neighbour.getName() + "'");
181         }
182 }