summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/grpc/server
diff options
context:
space:
mode:
Diffstat (limited to 'verigraph/src/it/polito/verigraph/grpc/server')
-rw-r--r--verigraph/src/it/polito/verigraph/grpc/server/GrpcUtils.java29
-rw-r--r--verigraph/src/it/polito/verigraph/grpc/server/Service.java169
2 files changed, 180 insertions, 18 deletions
diff --git a/verigraph/src/it/polito/verigraph/grpc/server/GrpcUtils.java b/verigraph/src/it/polito/verigraph/grpc/server/GrpcUtils.java
index 43859db..96c52a5 100644
--- a/verigraph/src/it/polito/verigraph/grpc/server/GrpcUtils.java
+++ b/verigraph/src/it/polito/verigraph/grpc/server/GrpcUtils.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2017 Politecnico di Torino and others.
+ * Copyright (c) 2017/18 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
@@ -12,16 +12,18 @@ 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 it.polito.verigraph.grpc.ConfigurationGrpc;
import it.polito.verigraph.grpc.GraphGrpc;
import it.polito.verigraph.grpc.NeighbourGrpc;
import it.polito.verigraph.grpc.NodeGrpc;
+import it.polito.verigraph.grpc.NodeGrpc.FunctionalType;
import it.polito.verigraph.grpc.TestGrpc;
import it.polito.verigraph.grpc.VerificationGrpc;
-import it.polito.verigraph.grpc.NodeGrpc.FunctionalType;
import it.polito.verigraph.model.Configuration;
import it.polito.verigraph.model.Graph;
import it.polito.verigraph.model.Neighbour;
@@ -29,6 +31,7 @@ import it.polito.verigraph.model.Node;
import it.polito.verigraph.model.Test;
import it.polito.verigraph.model.Verification;
+
public class GrpcUtils {
private static final Logger logger = Logger.getLogger(GrpcUtils.class.getName());
@@ -43,6 +46,7 @@ public class GrpcUtils {
//id is not present
Neighbour ne = new Neighbour();
ne.setName(request.getName());
+ ne.setId(request.getId());
return ne;
}
@@ -86,13 +90,17 @@ public class GrpcUtils {
}
public static Node deriveNode(NodeGrpc request) {
- //id is not present
+ //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);
+ //Modification for Tosca CLI
+ Long id = request.getId();
+ if( id != null) node.setId(request.getId());
+
Map<Long,Neighbour> neighours = node.getNeighbours();
long i = 1;
for(NeighbourGrpc neighbour:request.getNeighbourList()){
@@ -106,7 +114,7 @@ public class GrpcUtils {
public static GraphGrpc obtainGraph(Graph graph){
GraphGrpc.Builder gr = GraphGrpc.newBuilder();
gr.setId(graph.getId());
- for(Node node:graph.getNodes().values()){
+ for(Node node : graph.getNodes().values()){
NodeGrpc ng = obtainNode(node);
gr.addNode(ng);
}
@@ -116,14 +124,16 @@ public class GrpcUtils {
public static Graph deriveGraph(GraphGrpc request) {
//id is not present
Graph graph = new Graph();
-
+ //Modification for Tosca CLI
+ Long id = request.getId();
+ if( id != null) graph.setId(request.getId());
+
long i=1;
Map<Long, Node> nodes= graph.getNodes();
for(NodeGrpc node:request.getNodeList()){
Node ng = deriveNode(node);
- nodes.put(i++, ng);
+ nodes.put(i++, ng);
}
-
return graph;
}
@@ -142,12 +152,13 @@ public class GrpcUtils {
return ver.build();
}
- /**Intended for string that begins with "?"
- * */
+
+ /** 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;
}
+
}
diff --git a/verigraph/src/it/polito/verigraph/grpc/server/Service.java b/verigraph/src/it/polito/verigraph/grpc/server/Service.java
index 1839b7e..c6b69b0 100644
--- a/verigraph/src/it/polito/verigraph/grpc/server/Service.java
+++ b/verigraph/src/it/polito/verigraph/grpc/server/Service.java
@@ -15,9 +15,13 @@ import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
+
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
+import it.polito.verigraph.exception.BadRequestException;
+import it.polito.verigraph.exception.DataNotFoundException;
+import it.polito.verigraph.exception.ForbiddenException;
import it.polito.verigraph.grpc.ConfigurationGrpc;
import it.polito.verigraph.grpc.GetRequest;
import it.polito.verigraph.grpc.GraphGrpc;
@@ -25,15 +29,17 @@ import it.polito.verigraph.grpc.NeighbourGrpc;
import it.polito.verigraph.grpc.NewGraph;
import it.polito.verigraph.grpc.NewNeighbour;
import it.polito.verigraph.grpc.NewNode;
+import it.polito.verigraph.grpc.NewTopologyTemplate;
import it.polito.verigraph.grpc.NodeGrpc;
import it.polito.verigraph.grpc.Policy;
import it.polito.verigraph.grpc.RequestID;
import it.polito.verigraph.grpc.Status;
+import it.polito.verigraph.grpc.TopologyTemplateGrpc;
+import it.polito.verigraph.grpc.ToscaPolicy;
+import it.polito.verigraph.grpc.ToscaRequestID;
+import it.polito.verigraph.grpc.ToscaVerificationGrpc;
import it.polito.verigraph.grpc.VerificationGrpc;
import it.polito.verigraph.grpc.VerigraphGrpc;
-import it.polito.verigraph.exception.BadRequestException;
-import it.polito.verigraph.exception.DataNotFoundException;
-import it.polito.verigraph.exception.ForbiddenException;
import it.polito.verigraph.model.Configuration;
import it.polito.verigraph.model.Graph;
import it.polito.verigraph.model.Neighbour;
@@ -44,6 +50,8 @@ import it.polito.verigraph.service.GraphService;
import it.polito.verigraph.service.NeighbourService;
import it.polito.verigraph.service.NodeService;
import it.polito.verigraph.service.VerificationService;
+import it.polito.verigraph.tosca.converter.grpc.GraphToGrpc;
+import it.polito.verigraph.tosca.converter.grpc.GrpcToGraph;
public class Service {
/** Port on which the server should run. */
@@ -107,7 +115,7 @@ public class Service {
}
}
- /**Here start method of my implementation*/
+ /** Here start methods */
private class VerigraphImpl extends VerigraphGrpc.VerigraphImplBase{
/** Here start methods of GraphResource*/
@@ -307,8 +315,7 @@ public class Service {
@Override
public void updateNode(NodeGrpc request, StreamObserver<NewNode> responseObserver) {
- NewNode.Builder response = NewNode.newBuilder();
- try{
+ NewNode.Builder response = NewNode.newBuilder(); try{
Node node = GrpcUtils.deriveNode(request);
node.setId(request.getId());
Node newNode = nodeService.updateNode(request.getIdGraph(), node);
@@ -340,7 +347,8 @@ public class Service {
}
Node node = nodeService.getNode(request.getIdGraph(), request.getIdNode());
if (node == null){
- throw new BadRequestException("Node with id " + request.getIdNode() + " not found in graph with id " + request.getIdGraph());
+ throw new BadRequestException("Node with id " + request.getIdNode() +
+ " not found in graph with id " + request.getIdGraph());
}
Configuration nodeConfiguration = GrpcUtils.deriveConfiguration(request);
Node nodeCopy = new Node();
@@ -386,8 +394,7 @@ public class Service {
NeighbourGrpc nr = NeighbourGrpc.newBuilder().setErrorMessage(internalError).build();
responseObserver.onNext(nr);
logger.log(Level.WARNING, ex.getMessage());
- }
- responseObserver.onCompleted();
+ } responseObserver.onCompleted();
}
@Override
@@ -462,5 +469,149 @@ public class Service {
responseObserver.onNext(response.build());
responseObserver.onCompleted();
}
+
+ /** Here start methods of TOSCA gRPC server */ @Override
+ public void getTopologyTemplates (GetRequest request, StreamObserver<TopologyTemplateGrpc> responseObserver) {
+ boolean not_correct = false;
+ try {
+ for(Graph item : graphService.getAllGraphs()) {
+ TopologyTemplateGrpc topol = GraphToGrpc.obtainTopologyTemplate(item);
+ responseObserver.onNext(topol);
+ }
+ } catch(Exception ex){
+ logger.log(Level.WARNING, ex.getMessage());
+ not_correct = true;
+ }
+ if(not_correct)
+ responseObserver.onNext(TopologyTemplateGrpc.newBuilder()
+ .setErrorMessage("Internal Server Error while retrieving TopologyTemplate").build());
+ responseObserver.onCompleted();
+ }
+
+ @Override
+ public void getTopologyTemplate (ToscaRequestID request, StreamObserver<TopologyTemplateGrpc> responseObserver) {
+ try {
+ Long graphID = Long.valueOf(request.getIdTopologyTemplate());
+ //this method will throw a NumberFormatException in case the ID is not representable as a long
+ Graph graph = graphService.getGraph(graphID);
+ TopologyTemplateGrpc topol = GraphToGrpc.obtainTopologyTemplate(graph);
+ responseObserver.onNext(topol);
+ } catch(ForbiddenException | DataNotFoundException ex) {
+ TopologyTemplateGrpc topolError = TopologyTemplateGrpc.newBuilder().setErrorMessage(ex.getMessage()).build();
+ responseObserver.onNext(topolError);
+ logger.log(Level.WARNING, ex.getMessage());
+ } catch(NumberFormatException ex) {
+ TopologyTemplateGrpc topolError = TopologyTemplateGrpc.newBuilder()
+ .setErrorMessage("The TopologyTemplate ID must be a long value.").build();
+ responseObserver.onNext(topolError);
+ logger.log(Level.WARNING, ex.getMessage());
+ } catch(Exception ex) {
+ TopologyTemplateGrpc topolError = TopologyTemplateGrpc.newBuilder().setErrorMessage(internalError).build();
+ responseObserver.onNext(topolError);
+ logger.log(Level.WARNING, ex.getMessage());
+ }
+ responseObserver.onCompleted();
+ }
+
+ @Override
+ public void createTopologyTemplate (TopologyTemplateGrpc request, StreamObserver<NewTopologyTemplate> responseObserver) {
+ NewTopologyTemplate.Builder response = NewTopologyTemplate.newBuilder();
+ try{
+ Graph graph = GrpcToGraph.deriveGraph(request);
+ Graph newGraph = graphService.addGraph(graph);
+ response.setSuccess(true).setTopologyTemplate(GraphToGrpc.obtainTopologyTemplate(newGraph));
+ } catch(BadRequestException ex) {
+ ex.printStackTrace();
+ response.setSuccess(false).setErrorMessage("Provided invalid request to the service.");
+ logger.log(Level.WARNING, ex.getClass().toString());
+ logger.log(Level.WARNING, ex.getMessage());
+ } catch(Exception ex) {
+ ex.printStackTrace();
+ response.setSuccess(false).setErrorMessage(internalError);
+ logger.log(Level.WARNING, ex.getClass().toString());
+ logger.log(Level.WARNING, ex.getMessage());
+ }
+ responseObserver.onNext(response.build());
+ responseObserver.onCompleted();
+ }
+
+
+ @Override
+ public void updateTopologyTemplate (TopologyTemplateGrpc request, StreamObserver<NewTopologyTemplate> responseObserver) {
+ NewTopologyTemplate.Builder response = NewTopologyTemplate.newBuilder();
+ try{
+ Graph graph = GrpcToGraph.deriveGraph(request);
+ Graph newGraph = graphService.updateGraph(graph);
+ response.setSuccess(true).setTopologyTemplate(GraphToGrpc.obtainTopologyTemplate(newGraph));
+ } catch(ForbiddenException | DataNotFoundException | BadRequestException ex){
+ response.setSuccess(false).setErrorMessage(ex.getMessage());
+ logger.log(Level.WARNING, ex.getMessage());
+ } catch(Exception ex){
+ response.setSuccess(false).setErrorMessage(internalError);
+ logger.log(Level.WARNING, ex.getMessage());
+ }
+ responseObserver.onNext(response.build());
+ responseObserver.onCompleted();
+ }
+
+
+ @Override
+ public void deleteTopologyTemplate (ToscaRequestID request, StreamObserver<Status> responseObserver) {
+ Status.Builder response = Status.newBuilder();
+ Long graphID = null;
+ try{
+ graphID = Long.valueOf(request.getIdTopologyTemplate());
+ //this method will throw a NumberFormatException in case the ID is not representable as a long
+ graphService.removeGraph(graphID);
+ response.setSuccess(true);
+ } catch(ForbiddenException | DataNotFoundException ex) {
+ response.setSuccess(false).setErrorMessage(ex.getMessage());
+ logger.log(Level.WARNING, ex.getMessage());
+ } catch(NumberFormatException ex) {
+ response.setSuccess(false).setErrorMessage("The TopologyTemplate ID must be a long value.");
+ logger.log(Level.WARNING, ex.getMessage());
+ } catch(Exception ex) {
+ response.setSuccess(false).setErrorMessage(internalError);
+ logger.log(Level.WARNING, ex.getMessage());
+ }
+ responseObserver.onNext(response.build());
+ responseObserver.onCompleted();
+ }
+
+
+ @Override
+ public void verifyToscaPolicy(ToscaPolicy request, StreamObserver<ToscaVerificationGrpc> responseObserver) {
+ try{
+ //Convert request
+ VerificationBean verify = new VerificationBean();
+ verify.setDestination(request.getDestination());
+ verify.setSource(request.getSource());
+ verify.setType(request.getType().toString());
+ verify.setMiddlebox(request.getMiddlebox());
+
+ //Convert Response
+ Long graphID = Long.valueOf(request.getIdTopologyTemplate());
+ //this method will throw a NumberFormatException in case the ID is not representable as a long
+ Verification ver = verificationService.verify(graphID, verify);
+ responseObserver.onNext(GraphToGrpc.obtainToscaVerification(ver));
+ } catch(ForbiddenException | DataNotFoundException | BadRequestException ex) {
+ ToscaVerificationGrpc verError = ToscaVerificationGrpc.newBuilder().setSuccessOfOperation(false)
+ .setErrorMessage(ex.getMessage()).build();
+ responseObserver.onNext(verError);
+ logger.log(Level.WARNING, ex.getMessage());
+ } catch(NumberFormatException ex) {
+ ToscaVerificationGrpc verError = ToscaVerificationGrpc.newBuilder().setSuccessOfOperation(false)
+ .setErrorMessage("The TopologyTemplate ID must be a long value.").build();
+ responseObserver.onNext(verError);
+ logger.log(Level.WARNING, ex.getMessage());
+ } catch(Exception ex) {
+ ToscaVerificationGrpc verError = ToscaVerificationGrpc.newBuilder().setSuccessOfOperation(false)
+ .setErrorMessage(internalError).build();
+ responseObserver.onNext(verError);
+ logger.log(Level.WARNING, ex.getMessage());
+ } responseObserver.onCompleted();
+ }
+
+
}
}