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.grpc;
12 import java.io.IOException;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
17 import com.fasterxml.jackson.databind.JsonNode;
18 import com.fasterxml.jackson.databind.ObjectMapper;
19 import com.google.common.base.Splitter;
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;
35 public class GrpcUtils {
36 private static final Logger logger = Logger.getLogger(GrpcUtils.class.getName());
38 public static NeighbourGrpc obtainNeighbour(Neighbour ne){
39 return NeighbourGrpc.newBuilder()
41 .setName(ne.getName())
45 public static Neighbour deriveNeighbour(NeighbourGrpc request) {
47 Neighbour ne = new Neighbour();
48 ne.setName(request.getName());
52 public static ConfigurationGrpc obtainConfiguration(Configuration conf){
53 return ConfigurationGrpc.newBuilder()
55 .setDescription(conf.getDescription())
56 .setConfiguration(conf.getConfiguration().toString())
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;
67 if ("".equals(request.getConfiguration()))
68 rootNode=mapper.readTree("[]");
70 rootNode = mapper.readTree(request.getConfiguration());
71 } catch (IOException e) {
72 logger.log(Level.WARNING, e.getMessage());
74 conf.setConfiguration(rootNode);
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);
87 nr.setConfiguration(obtainConfiguration(node.getConfiguration()));
91 public static Node deriveNode(NodeGrpc request) {
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);
99 Map<Long,Neighbour> neighours = node.getNeighbours();
101 for(NeighbourGrpc neighbour:request.getNeighbourList()){
102 Neighbour ng = deriveNeighbour(neighbour);
103 neighours.put(i++, ng);
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);
119 public static Graph deriveGraph(GraphGrpc request) {
121 Graph graph = new Graph();
124 Map<Long, Node> nodes = graph.getNodes();
125 for(NodeGrpc node:request.getNodeList()){
126 Node ng = deriveNode(node);
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);
148 /**Intended for string that begins with "?"
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("=").