Disable syslog in heat-translator for functest integration
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / deserializer / NodeCustomDeserializer.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.deserializer;
11
12 import java.io.IOException;
13 import java.util.List;
14 import java.util.Map;
15
16 import com.fasterxml.jackson.core.JsonParseException;
17 import com.fasterxml.jackson.core.JsonParser;
18 import com.fasterxml.jackson.core.JsonProcessingException;
19 import com.fasterxml.jackson.databind.DeserializationContext;
20 import com.fasterxml.jackson.databind.JsonDeserializer;
21 import com.fasterxml.jackson.databind.JsonMappingException;
22 import com.fasterxml.jackson.databind.JsonNode;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.type.TypeFactory;
25
26 import it.polito.escape.verify.exception.BadRequestException;
27 import it.polito.escape.verify.exception.InternalServerErrorException;
28 import it.polito.escape.verify.model.Configuration;
29 import it.polito.escape.verify.model.Neighbour;
30 import it.polito.escape.verify.model.Node;
31
32 public class NodeCustomDeserializer extends JsonDeserializer<Node> {
33
34         @Override
35         public Node deserialize(JsonParser jp, DeserializationContext context) {
36
37                 try {
38                         JsonNode root = jp.getCodec().readTree(jp);
39                         JsonNode neighboursJson = root.get("neighbours");
40                         JsonNode configurationJson = root.get("configuration");
41
42                         String nodeName = root.get("name").asText();
43                         String functionalType = root.get("functional_type").asText();
44
45                         Node node = new Node();
46                         if(root.get("id") != null){
47                                 long nodeId = root.get("id").asLong();
48                                 node.setId(nodeId);
49                         }
50                         node.setName(nodeName);
51                         node.setFunctional_type(functionalType);
52
53                         if (configurationJson == null)
54                                 node.setConfiguration(new Configuration(node.getName(), "", new ObjectMapper().createArrayNode()));
55                         else {
56                                 Configuration conf = node.getConfiguration();
57                                 conf.setId(node.getName());
58                                 conf.setDescription("");
59                                 conf.setConfiguration(configurationJson);
60                         }
61
62                         try {
63                                 List<Neighbour> neighbourList = new ObjectMapper().readValue(   neighboursJson.toString(),
64                                                                                                                                                                 TypeFactory     .defaultInstance()
65                                                                                                                                                                                         .constructCollectionType(       List.class,
66                                                                                                                                                                                                                                                 Neighbour.class));
67                                 Map<Long, Neighbour> neighbours = node.getNeighbours();
68
69                                 long numberOfNeighbours = 0;
70                                 for (Neighbour neighbour : neighbourList) {
71                                         neighbours.put(++numberOfNeighbours, neighbour);
72                                 }
73
74                                 return node;
75                         }
76                         catch (JsonParseException e) {
77                                 throw new BadRequestException("Invalid content for a node: " + e.getMessage());
78                         }
79                         catch (JsonMappingException e) {
80                                 throw new BadRequestException("Invalid input json structure for a node: " + e.getMessage());
81                         }
82                 }
83                 catch (JsonProcessingException e) {
84                         throw new InternalServerErrorException("Error parsing a node: " + e.getMessage());
85                 }
86                 catch (IOException e) {
87                         throw new InternalServerErrorException("I/O error parsing a node: " + e.getMessage());
88                 }
89
90         }
91
92 }