diff options
author | serena.spinoso <serena.spinoso@polito.it> | 2017-02-25 12:00:55 +0100 |
---|---|---|
committer | serena.spinoso <serena.spinoso@polito.it> | 2017-02-27 13:39:18 +0100 |
commit | 53d83244c1bf36af86e90ce5fe758a369f73563e (patch) | |
tree | f5780b3033763d6b3cd62c40f5d4a63dfac35f7a /verigraph/src/main/java/it/polito/grpc/GrpcUtils.java | |
parent | f0108f26fb1f62fefbbc4d5484b87241d563737b (diff) |
Add verigraph code base
JIRA: PARSER-111
Change-Id: Ie76e14fabbb6c388ebc89d9a15dd3021b25fa892
Signed-off-by: serena.spinoso <serena.spinoso@polito.it>
Diffstat (limited to 'verigraph/src/main/java/it/polito/grpc/GrpcUtils.java')
-rw-r--r-- | verigraph/src/main/java/it/polito/grpc/GrpcUtils.java | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/verigraph/src/main/java/it/polito/grpc/GrpcUtils.java b/verigraph/src/main/java/it/polito/grpc/GrpcUtils.java new file mode 100644 index 0000000..0ed002c --- /dev/null +++ b/verigraph/src/main/java/it/polito/grpc/GrpcUtils.java @@ -0,0 +1,156 @@ +/******************************************************************************* + * Copyright (c) 2017 Politecnico di Torino and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Apache License, Version 2.0 + * which accompanies this distribution, and is available at + * http://www.apache.org/licenses/LICENSE-2.0 + *******************************************************************************/ + +package it.polito.grpc; + +import java.io.IOException; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Splitter; + +import io.grpc.verigraph.ConfigurationGrpc; +import io.grpc.verigraph.GraphGrpc; +import io.grpc.verigraph.NeighbourGrpc; +import io.grpc.verigraph.NodeGrpc; +import io.grpc.verigraph.TestGrpc; +import io.grpc.verigraph.VerificationGrpc; +import io.grpc.verigraph.NodeGrpc.FunctionalType; +import it.polito.escape.verify.model.Configuration; +import it.polito.escape.verify.model.Graph; +import it.polito.escape.verify.model.Neighbour; +import it.polito.escape.verify.model.Node; +import it.polito.escape.verify.model.Test; +import it.polito.escape.verify.model.Verification; + +public class GrpcUtils { + private static final Logger logger = Logger.getLogger(GrpcUtils.class.getName()); + + public static NeighbourGrpc obtainNeighbour(Neighbour ne){ + return NeighbourGrpc.newBuilder() + .setId(ne.getId()) + .setName(ne.getName()) + .build(); + } + + public static Neighbour deriveNeighbour(NeighbourGrpc request) { + //id is not present + Neighbour ne = new Neighbour(); + ne.setName(request.getName()); + return ne; + } + + public static ConfigurationGrpc obtainConfiguration(Configuration conf){ + return ConfigurationGrpc.newBuilder() + .setId(conf.getId()) + .setDescription(conf.getDescription()) + .setConfiguration(conf.getConfiguration().toString()) + .build(); + } + + public static Configuration deriveConfiguration(ConfigurationGrpc request) { + Configuration conf = new Configuration(); + conf.setId(request.getId()); + conf.setDescription(request.getDescription()); + ObjectMapper mapper = new ObjectMapper(); + JsonNode rootNode = null; + try { + if ("".equals(request.getConfiguration())) + rootNode=mapper.readTree("[]"); + else + rootNode = mapper.readTree(request.getConfiguration()); + } catch (IOException e) { + logger.log(Level.WARNING, e.getMessage()); + } + conf.setConfiguration(rootNode); + return conf; + } + + public static NodeGrpc obtainNode(Node node) { + NodeGrpc.Builder nr = NodeGrpc.newBuilder(); + nr.setId(node.getId()); + nr.setName(node.getName()); + nr.setFunctionalType(FunctionalType.valueOf(node.getFunctional_type())); + for(Neighbour neighbour:node.getNeighbours().values()){ + NeighbourGrpc ng = obtainNeighbour(neighbour); + nr.addNeighbour(ng); + } + nr.setConfiguration(obtainConfiguration(node.getConfiguration())); + return nr.build(); + } + + public static Node deriveNode(NodeGrpc request) { + //id is not present + Node node = new Node(); + node.setName(request.getName()); + node.setFunctional_type(request.getFunctionalType().toString()); + Configuration conf = deriveConfiguration(request.getConfiguration()); + node.setConfiguration(conf); + + Map<Long,Neighbour> neighours = node.getNeighbours(); + long i = 1; + for(NeighbourGrpc neighbour:request.getNeighbourList()){ + Neighbour ng = deriveNeighbour(neighbour); + neighours.put(i++, ng); + } + + return node; + } + + public static GraphGrpc obtainGraph(Graph graph){ + GraphGrpc.Builder gr = GraphGrpc.newBuilder(); + gr.setId(graph.getId()); + for(Node node:graph.getNodes().values()){ + NodeGrpc ng = obtainNode(node); + gr.addNode(ng); + } + return gr.build(); + } + + public static Graph deriveGraph(GraphGrpc request) { + //id is not present + Graph graph = new Graph(); + + long i=1; + Map<Long, Node> nodes = graph.getNodes(); + for(NodeGrpc node:request.getNodeList()){ + Node ng = deriveNode(node); + nodes.put(i++, ng); + } + + return graph; + } + + public static VerificationGrpc obtainVerification(Verification verify){ + VerificationGrpc.Builder ver = VerificationGrpc.newBuilder(); + ver.setComment(verify.getComment()); + ver.setResult(verify.getResult()); + for(Test test:verify.getTests()){ + TestGrpc.Builder tst = TestGrpc.newBuilder().setResult(test.getResult()); + for(Node node:test.getPath()){ + NodeGrpc ng = obtainNode(node); + tst.addNode(ng); + } + ver.addTest(tst); + } + return ver.build(); + } + + /**Intended for string that begins with "?" + * */ + public static Map<String,String> getParamGivenString(String str){ + String string = str.substring(1); + final Map<String, String> map = Splitter.on('&').trimResults().withKeyValueSeparator("="). + split(string); + return map; + } +} |