Disable syslog in heat-translator for functest integration
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / service / GraphService.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.DataNotFoundException;
18 import it.polito.escape.verify.exception.ForbiddenException;
19 import it.polito.escape.verify.model.Graph;
20 import it.polito.escape.verify.model.Neighbour;
21 import it.polito.escape.verify.model.Node;
22
23 public class GraphService {
24
25         private Map<Long, Graph> graphs = DatabaseClass.getInstance().getGraphs();
26
27         public GraphService() {
28
29         }
30
31         public List<Graph> getAllGraphs() {
32                 return new ArrayList<Graph>(graphs.values());
33         }
34
35         public Graph getGraph(long id) {
36                 if (id <= 0) {
37                         throw new ForbiddenException("Illegal graph id: " + id);
38                 }
39                 Graph graph = graphs.get(id);
40                 if (graph == null) {
41                         throw new DataNotFoundException("Graph with id " + id + " not found");
42                 }
43                 return graph;
44         }
45
46         public Graph updateGraph(Graph graph) {
47                 if (graph.getId() <= 0) {
48                         throw new ForbiddenException("Illegal graph id: " + graph.getId());
49                 }
50                 Graph localGraph = graphs.get(graph.getId());
51                 if (localGraph == null) {
52                         throw new DataNotFoundException("Graph with id " + graph.getId() + " not found");
53                 }
54
55                 validateGraph(graph);
56
57 //              int numberOfNodes = 0;
58 //              for (Node node : graph.getNodes().values()) {
59 //
60 //                      node.setId(++numberOfNodes);
61 //
62 //                      int numberOfNodeNeighbours = 0;
63 //                      for (Neighbour neighbour : node.getNeighbours().values()) {
64 //                              neighbour.setId(++numberOfNodeNeighbours);
65 //                      }
66 //              }
67
68                 for (Map.Entry<Long, Node> nodeEntry : graph.getNodes().entrySet()){
69                         nodeEntry.getValue().setId(nodeEntry.getKey());
70
71                         for (Map.Entry<Long, Neighbour> neighbourEntry : nodeEntry.getValue().getNeighbours().entrySet()){
72                                 neighbourEntry.getValue().setId(neighbourEntry.getKey());
73                         }
74                 }
75
76                 synchronized(this){
77                         graphs.put(graph.getId(), graph);
78                         DatabaseClass.persistDatabase();
79                         return graph;
80                 }
81         }
82
83         public Graph removeGraph(long id) {
84                 if (id <= 0) {
85                         throw new ForbiddenException("Illegal graph id: " + id);
86                 }
87                 synchronized(this){
88                         return graphs.remove(id);
89                 }
90         }
91
92         public Graph addGraph(Graph graph) {
93                 validateGraph(graph);
94
95                 synchronized (this) {
96                         graph.setId(DatabaseClass.getInstance().getNumberOfGraphs() + 1);
97                 }
98 //              int numberOfNodes = 0;
99 //              for (Node node : graph.getNodes().values()) {
100 //
101 //                      node.setId(++numberOfNodes);
102 //
103 //                      int numberOfNodeNeighbours = 0;
104 //                      for (Neighbour neighbour : node.getNeighbours().values()) {
105 //                              neighbour.setId(++numberOfNodeNeighbours);
106 //                      }
107 //              }
108
109                 for (Map.Entry<Long, Node> nodeEntry : graph.getNodes().entrySet()){
110                         nodeEntry.getValue().setId(nodeEntry.getKey());
111
112                         for (Map.Entry<Long, Neighbour> neighbourEntry : nodeEntry.getValue().getNeighbours().entrySet()){
113                                 neighbourEntry.getValue().setId(neighbourEntry.getKey());
114                         }
115                 }
116
117                 synchronized(this){
118                         graphs.put(graph.getId(), graph);
119                         DatabaseClass.persistDatabase();
120                         return graph;
121                 }
122         }
123
124         public static void validateGraph(Graph graph) {
125                 for (Node node : graph.getNodes().values()) {
126                         NodeService.validateNode(graph, node);
127                 }
128         }
129 }