1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
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 *******************************************************************************/
10 package it.polito.escape.verify.deserializer;
12 import java.io.IOException;
13 import java.util.List;
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;
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;
32 public class NodeCustomDeserializer extends JsonDeserializer<Node> {
35 public Node deserialize(JsonParser jp, DeserializationContext context) {
38 JsonNode root = jp.getCodec().readTree(jp);
39 JsonNode neighboursJson = root.get("neighbours");
40 JsonNode configurationJson = root.get("configuration");
42 String nodeName = root.get("name").asText();
43 String functionalType = root.get("functional_type").asText();
45 Node node = new Node();
46 if(root.get("id") != null){
47 long nodeId = root.get("id").asLong();
50 node.setName(nodeName);
51 node.setFunctional_type(functionalType);
53 if (configurationJson == null)
54 node.setConfiguration(new Configuration(node.getName(), "", new ObjectMapper().createArrayNode()));
56 Configuration conf = node.getConfiguration();
57 conf.setId(node.getName());
58 conf.setDescription("");
59 conf.setConfiguration(configurationJson);
63 List<Neighbour> neighbourList = new ObjectMapper().readValue( neighboursJson.toString(),
64 TypeFactory .defaultInstance()
65 .constructCollectionType( List.class,
67 Map<Long, Neighbour> neighbours = node.getNeighbours();
69 long numberOfNeighbours = 0;
70 for (Neighbour neighbour : neighbourList) {
71 neighbours.put(++numberOfNeighbours, neighbour);
76 catch (JsonParseException e) {
77 throw new BadRequestException("Invalid content for a node: " + e.getMessage());
79 catch (JsonMappingException e) {
80 throw new BadRequestException("Invalid input json structure for a node: " + e.getMessage());
83 catch (JsonProcessingException e) {
84 throw new InternalServerErrorException("Error parsing a node: " + e.getMessage());
86 catch (IOException e) {
87 throw new InternalServerErrorException("I/O error parsing a node: " + e.getMessage());