Stop installing librairies during tests
[parser.git] / verigraph / src / main / java / it / polito / grpc / GrpcUtils.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.grpc;
11
12 import java.io.IOException;
13 import java.util.Map;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16
17 import com.fasterxml.jackson.databind.JsonNode;
18 import com.fasterxml.jackson.databind.ObjectMapper;
19 import com.google.common.base.Splitter;
20
21 import io.grpc.verigraph.ConfigurationGrpc;
22 import io.grpc.verigraph.GraphGrpc;
23 import io.grpc.verigraph.NeighbourGrpc;
24 import io.grpc.verigraph.NodeGrpc;
25 import io.grpc.verigraph.TestGrpc;
26 import io.grpc.verigraph.VerificationGrpc;
27 import io.grpc.verigraph.NodeGrpc.FunctionalType;
28 import it.polito.escape.verify.model.Configuration;
29 import it.polito.escape.verify.model.Graph;
30 import it.polito.escape.verify.model.Neighbour;
31 import it.polito.escape.verify.model.Node;
32 import it.polito.escape.verify.model.Test;
33 import it.polito.escape.verify.model.Verification;
34
35 public class GrpcUtils {
36         private static final Logger logger = Logger.getLogger(GrpcUtils.class.getName());
37
38         public static NeighbourGrpc obtainNeighbour(Neighbour ne){
39                   return NeighbourGrpc.newBuilder()
40                                   .setId(ne.getId())
41                                   .setName(ne.getName())
42                                   .build();
43           }
44
45           public static Neighbour deriveNeighbour(NeighbourGrpc request) {
46                   //id is not present
47                   Neighbour ne = new Neighbour();
48                   ne.setName(request.getName());
49                   return ne;
50           }
51
52           public static ConfigurationGrpc obtainConfiguration(Configuration conf){
53                   return ConfigurationGrpc.newBuilder()
54                                   .setId(conf.getId())
55                                   .setDescription(conf.getDescription())
56                                   .setConfiguration(conf.getConfiguration().toString())
57                                   .build();
58           }
59
60           public static Configuration deriveConfiguration(ConfigurationGrpc request) {
61                   Configuration conf = new Configuration();
62                   conf.setId(request.getId());
63                   conf.setDescription(request.getDescription());
64                   ObjectMapper mapper = new ObjectMapper();
65                   JsonNode rootNode = null;
66                   try {
67                           if ("".equals(request.getConfiguration()))
68                                   rootNode=mapper.readTree("[]");
69                           else
70                                   rootNode = mapper.readTree(request.getConfiguration());
71                   } catch (IOException e) {
72                           logger.log(Level.WARNING, e.getMessage());
73                   }
74                   conf.setConfiguration(rootNode);
75                   return conf;
76           }
77
78           public static NodeGrpc obtainNode(Node node) {
79                   NodeGrpc.Builder nr = NodeGrpc.newBuilder();
80                   nr.setId(node.getId());
81                   nr.setName(node.getName());
82                   nr.setFunctionalType(FunctionalType.valueOf(node.getFunctional_type()));
83                   for(Neighbour neighbour:node.getNeighbours().values()){
84                           NeighbourGrpc ng = obtainNeighbour(neighbour);
85                           nr.addNeighbour(ng);
86                   }
87                   nr.setConfiguration(obtainConfiguration(node.getConfiguration()));
88                   return nr.build();
89           }
90
91           public static Node deriveNode(NodeGrpc request) {
92                   //id is not present
93                   Node node = new Node();
94                   node.setName(request.getName());
95                   node.setFunctional_type(request.getFunctionalType().toString());
96                   Configuration conf = deriveConfiguration(request.getConfiguration());
97                   node.setConfiguration(conf);
98
99                   Map<Long,Neighbour> neighours = node.getNeighbours();
100                   long i = 1;
101                   for(NeighbourGrpc neighbour:request.getNeighbourList()){
102                           Neighbour ng = deriveNeighbour(neighbour);
103                           neighours.put(i++, ng);
104                   }
105
106                   return node;
107           }
108
109           public static GraphGrpc obtainGraph(Graph graph){
110                   GraphGrpc.Builder gr = GraphGrpc.newBuilder();
111                   gr.setId(graph.getId());
112                   for(Node node:graph.getNodes().values()){
113                           NodeGrpc ng = obtainNode(node);
114                           gr.addNode(ng);
115                   }
116                   return gr.build();
117           }
118
119           public static Graph deriveGraph(GraphGrpc request) {
120                 //id is not present
121                   Graph graph = new Graph();
122
123                   long i=1;
124                   Map<Long, Node> nodes = graph.getNodes();
125                   for(NodeGrpc node:request.getNodeList()){
126                           Node ng = deriveNode(node);
127                           nodes.put(i++, ng);
128                   }
129
130                   return graph;
131           }
132
133           public static VerificationGrpc obtainVerification(Verification verify){
134                   VerificationGrpc.Builder ver = VerificationGrpc.newBuilder();
135                   ver.setComment(verify.getComment());
136                   ver.setResult(verify.getResult());
137                   for(Test test:verify.getTests()){
138                           TestGrpc.Builder tst = TestGrpc.newBuilder().setResult(test.getResult());
139                           for(Node node:test.getPath()){
140                                   NodeGrpc ng = obtainNode(node);
141                                   tst.addNode(ng);
142                           }
143                           ver.addTest(tst);
144                   }
145                   return ver.build();
146           }
147
148           /**Intended for string that begins with "?"
149            * */
150           public static Map<String,String> getParamGivenString(String str){
151                   String string = str.substring(1);
152                   final Map<String, String> map = Splitter.on('&').trimResults().withKeyValueSeparator("=").
153                                   split(string);
154                   return map;
155           }
156 }