summaryrefslogtreecommitdiffstats
path: root/verigraph/src/main/java/it/polito/grpc/GrpcUtils.java
blob: 0ed002cce57e197934933c11f492619254be6dcd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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;
	  }
}