Merge "Disable syslog in heat-translator for functest integration"
[parser.git] / verigraph / src / it / polito / verigraph / 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 package it.polito.verigraph.service;
10
11 import java.io.IOException;
12 import java.util.List;
13 import javax.xml.bind.JAXBException;
14 import com.fasterxml.jackson.core.JsonParseException;
15 import com.fasterxml.jackson.core.JsonProcessingException;
16 import com.fasterxml.jackson.databind.JsonMappingException;
17 import it.polito.neo4j.manager.Neo4jDBManager;
18 import it.polito.neo4j.exceptions.MyInvalidIdException;
19 import it.polito.neo4j.exceptions.MyNotFoundException;
20 import it.polito.verigraph.exception.ForbiddenException;
21 import it.polito.verigraph.model.Graph;
22 import it.polito.verigraph.model.Node;
23
24 public class GraphService {
25
26     private Neo4jDBManager manager= new Neo4jDBManager();
27     public static VerigraphLogger vlogger = VerigraphLogger.getVerigraphlogger();
28
29     public GraphService() {}
30
31     public List<Graph> getAllGraphs() throws JsonProcessingException, MyNotFoundException {
32         List<Graph> result;
33         result=manager.getGraphs();
34         for(Graph g : result){
35             validateGraph(g);
36         }
37         return result;
38     }
39
40     public Graph getGraph(long id) throws JsonParseException, JsonMappingException, JAXBException, IOException {
41         if (id < 0) {
42             throw new ForbiddenException("Illegal graph id: " + id);
43         }
44         Graph localGraph=manager.getGraph(id);
45         validateGraph(localGraph);
46         return localGraph;
47     }
48
49     public Graph updateGraph(Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
50         if (graph.getId() < 0) {
51             throw new ForbiddenException("Illegal graph id: " + graph.getId());
52         }
53         validateGraph(graph);
54         Graph localGraph=new Graph();
55         localGraph=manager.updateGraph(graph);
56         vlogger.logger.info("Graph updated");
57         validateGraph(localGraph);
58         return localGraph;
59     }
60
61
62     public void removeGraph(long id) {
63
64         if (id < 0) {
65             throw new ForbiddenException("Illegal graph id: " + id);
66         }
67         manager.deleteGraph(id);
68     }
69
70     public Graph addGraph(Graph graph) throws JAXBException, JsonParseException, JsonMappingException, IOException, MyInvalidIdException {
71         validateGraph(graph);
72         Graph g=manager.addGraph(graph);
73         validateGraph(g);
74         return g;
75     }
76
77     public static void validateGraph(Graph graph) throws JsonProcessingException {
78         for (Node node : graph.getNodes().values()) {
79             NodeService.validateNode(graph, node);
80         }
81     }
82 }