Support TOSCA in verigraph (gRPC service)
[parser.git] / verigraph / src / it / polito / verigraph / tosca / converter / grpc / GrpcToGraph.java
1 /*******************************************************************************\r
2  * Copyright (c) 2018 Politecnico di Torino and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Apache License, Version 2.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  *******************************************************************************/\r
9 package it.polito.verigraph.tosca.converter.grpc;\r
10 \r
11 import java.io.IOException;\r
12 import java.util.HashMap;\r
13 import java.util.List;\r
14 import java.util.Map;\r
15 \r
16 import com.fasterxml.jackson.core.JsonProcessingException;\r
17 import com.fasterxml.jackson.databind.JsonNode;\r
18 import com.fasterxml.jackson.databind.ObjectMapper;\r
19 \r
20 import it.polito.verigraph.exception.BadRequestException;\r
21 import it.polito.verigraph.grpc.NodeTemplateGrpc;\r
22 import it.polito.verigraph.grpc.RelationshipTemplateGrpc;\r
23 import it.polito.verigraph.grpc.TopologyTemplateGrpc;\r
24 import it.polito.verigraph.grpc.ToscaConfigurationGrpc;\r
25 import it.polito.verigraph.model.Configuration;\r
26 import it.polito.verigraph.model.Graph;\r
27 import it.polito.verigraph.model.Neighbour;\r
28 import it.polito.verigraph.model.Node;\r
29 \r
30 public class GrpcToGraph {\r
31 \r
32     /** Mapping method --> from grpc TopologyTemplateGrpc to model Graph */\r
33     public static Graph deriveGraph(TopologyTemplateGrpc request) throws BadRequestException, JsonProcessingException, IOException {\r
34         Graph graph = new Graph();\r
35         Map<Long, Node> nodes = new HashMap<>();\r
36 \r
37         try {\r
38             //Create a list of Node without Neighbour\r
39             for(NodeTemplateGrpc nodetempl : request.getNodeTemplateList()){\r
40                 Node node = deriveNode(nodetempl);\r
41               //It necessary to check uniqueness here otherwise a .put with the same key will overwrite the old node\r
42                 if(nodes.containsKey(node.getId()))\r
43                     throw new BadRequestException("The NodeTemplate ID must be unique.");\r
44                 else\r
45                     nodes.put(node.getId(), node);\r
46             }\r
47 \r
48             //Add Neighbour to the Node of the list\r
49             List<RelationshipTemplateGrpc> relatList = request.getRelationshipTemplateList();\r
50             nodes = deriveNeighboursNode(nodes, relatList);\r
51 \r
52             //Add Node and ID to the graph\r
53             graph.setNodes(nodes);\r
54             try {\r
55                 graph.setId(Long.valueOf(request.getId()));\r
56             } catch(NumberFormatException ex) {\r
57                 throw new BadRequestException("If you want to store your TopologyTemplate on this server,"\r
58                         + "the TopologyTemplate ID must be a number.");\r
59             }\r
60 \r
61             return graph;\r
62 \r
63         } catch (NullPointerException e) {\r
64             throw new BadRequestException("The TopologyTemplate received has invalid fields.");\r
65         }\r
66 \r
67     }\r
68 \r
69 \r
70     /** Mapping method --> from grpc NodeTemplate to model Node (with no Neighbour) */\r
71     private static Node deriveNode(NodeTemplateGrpc nodegrpc) throws BadRequestException, JsonProcessingException, IOException {\r
72         Node node = new Node();\r
73         try {\r
74             try {\r
75                 node.setId(Long.valueOf(nodegrpc.getId()));\r
76             } catch(NumberFormatException ex) {\r
77                 throw new BadRequestException("The NodeTemplate ID must be a number.");\r
78             }\r
79 \r
80             node.setName(nodegrpc.getName());\r
81             Configuration conf = deriveConfiguration(nodegrpc.getConfiguration());\r
82             node.setConfiguration(conf);\r
83             node.setFunctional_type(nodegrpc.getType().toString());\r
84 \r
85         } catch(NullPointerException ex) {\r
86             throw new BadRequestException("A NodeTemplate has wrong fields representation.");\r
87         }\r
88 \r
89         return node;\r
90     }\r
91 \r
92 \r
93 \r
94     /** Mapping method --> from a list of model Node to a list of model Node with their Neighbour */\r
95     private static Map<Long,Node> deriveNeighboursNode(Map<Long,Node> nodes, List<RelationshipTemplateGrpc> relatList)\r
96             throws BadRequestException{\r
97         Map<Long,Node> updNodes = nodes; //new list to be filled with updated Node (update = Node + its Neighbour)\r
98         for(RelationshipTemplateGrpc relat : relatList) {\r
99             try {\r
100                 //Retrieve the target Node name and generate a new Neighbour\r
101                 String neighName = updNodes.get(Long.valueOf(relat.getIdTargetNodeTemplate())).getName();\r
102                 Neighbour neigh = new Neighbour();\r
103                 neigh.setName(neighName);\r
104                 neigh.setId(Long.valueOf(relat.getId()));\r
105 \r
106                 //Retrieve the Neighbour map of the source Node and add the Neighbour\r
107                 Node source = updNodes.get(Long.valueOf(relat.getIdSourceNodeTemplate()));\r
108                 Map<Long,Neighbour> sourceNodeNeighMap = source.getNeighbours();\r
109                 if(sourceNodeNeighMap.containsKey(neigh.getId()))\r
110                     throw new BadRequestException("The RelationshipTemplate ID must be unique.");\r
111                 else\r
112                     sourceNodeNeighMap.put(neigh.getId(), neigh);\r
113                 source.setNeighbours(sourceNodeNeighMap);\r
114 \r
115                 //Update the Node list\r
116                 updNodes.put(Long.valueOf(relat.getIdSourceNodeTemplate()), source);\r
117             } catch(NullPointerException | NumberFormatException ex) {\r
118                 throw new BadRequestException("A RelationshipTemplate has wrong fields representation.");\r
119             }\r
120         }\r
121         return updNodes;\r
122     }\r
123 \r
124     /** Mapping method --> from ToscaConfiguration to model Configuration */\r
125     private static Configuration deriveConfiguration(ToscaConfigurationGrpc request)\r
126             throws BadRequestException, JsonProcessingException, IOException {\r
127         Configuration conf = new Configuration();\r
128         ObjectMapper mapper = new ObjectMapper();\r
129         JsonNode rootNode = null;\r
130 \r
131         try {\r
132             conf.setId(request.getId());\r
133         } catch (NullPointerException e) {}\r
134 \r
135         try {\r
136             conf.setDescription(request.getDescription());\r
137         } catch (NullPointerException e) {}\r
138 \r
139         try {\r
140             if ("".equals(request.getConfiguration()))\r
141                 rootNode=mapper.readTree("[]");\r
142             else\r
143                 rootNode = mapper.readTree(request.getConfiguration());\r
144         } catch (NullPointerException e) {\r
145             rootNode=mapper.readTree("[]");\r
146         }\r
147         conf.setConfiguration(rootNode);\r
148         return conf;\r
149     }\r
150 }\r