From fcda9807cfa6a89d691877126b406c5d3909d9b9 Mon Sep 17 00:00:00 2001 From: "serena.spinoso" Date: Thu, 26 Apr 2018 14:19:16 +0200 Subject: Support TOSCA in verigraph (gRPC service) JIRA: PARSER-179 Add TOSCA service description in gRPC server. Add a TOSCA-based client to use the new functionality. Add a JUnit class for testing gRPC service with TOSCA descriptor Change-Id: Id3217a674f076714cd48e3b7e4236e7445d89cd2 Signed-off-by: serena.spinoso --- .../tosca/converter/grpc/GrpcToGraph.java | 150 +++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java (limited to 'verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java') diff --git a/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java new file mode 100644 index 0000000..76906b3 --- /dev/null +++ b/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToGraph.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * Copyright (c) 2018 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.verigraph.tosca.converter.grpc; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +import it.polito.verigraph.exception.BadRequestException; +import it.polito.verigraph.grpc.NodeTemplateGrpc; +import it.polito.verigraph.grpc.RelationshipTemplateGrpc; +import it.polito.verigraph.grpc.TopologyTemplateGrpc; +import it.polito.verigraph.grpc.ToscaConfigurationGrpc; +import it.polito.verigraph.model.Configuration; +import it.polito.verigraph.model.Graph; +import it.polito.verigraph.model.Neighbour; +import it.polito.verigraph.model.Node; + +public class GrpcToGraph { + + /** Mapping method --> from grpc TopologyTemplateGrpc to model Graph */ + public static Graph deriveGraph(TopologyTemplateGrpc request) throws BadRequestException, JsonProcessingException, IOException { + Graph graph = new Graph(); + Map nodes = new HashMap<>(); + + try { + //Create a list of Node without Neighbour + for(NodeTemplateGrpc nodetempl : request.getNodeTemplateList()){ + Node node = deriveNode(nodetempl); + //It necessary to check uniqueness here otherwise a .put with the same key will overwrite the old node + if(nodes.containsKey(node.getId())) + throw new BadRequestException("The NodeTemplate ID must be unique."); + else + nodes.put(node.getId(), node); + } + + //Add Neighbour to the Node of the list + List relatList = request.getRelationshipTemplateList(); + nodes = deriveNeighboursNode(nodes, relatList); + + //Add Node and ID to the graph + graph.setNodes(nodes); + try { + graph.setId(Long.valueOf(request.getId())); + } catch(NumberFormatException ex) { + throw new BadRequestException("If you want to store your TopologyTemplate on this server," + + "the TopologyTemplate ID must be a number."); + } + + return graph; + + } catch (NullPointerException e) { + throw new BadRequestException("The TopologyTemplate received has invalid fields."); + } + + } + + + /** Mapping method --> from grpc NodeTemplate to model Node (with no Neighbour) */ + private static Node deriveNode(NodeTemplateGrpc nodegrpc) throws BadRequestException, JsonProcessingException, IOException { + Node node = new Node(); + try { + try { + node.setId(Long.valueOf(nodegrpc.getId())); + } catch(NumberFormatException ex) { + throw new BadRequestException("The NodeTemplate ID must be a number."); + } + + node.setName(nodegrpc.getName()); + Configuration conf = deriveConfiguration(nodegrpc.getConfiguration()); + node.setConfiguration(conf); + node.setFunctional_type(nodegrpc.getType().toString()); + + } catch(NullPointerException ex) { + throw new BadRequestException("A NodeTemplate has wrong fields representation."); + } + + return node; + } + + + + /** Mapping method --> from a list of model Node to a list of model Node with their Neighbour */ + private static Map deriveNeighboursNode(Map nodes, List relatList) + throws BadRequestException{ + Map updNodes = nodes; //new list to be filled with updated Node (update = Node + its Neighbour) + for(RelationshipTemplateGrpc relat : relatList) { + try { + //Retrieve the target Node name and generate a new Neighbour + String neighName = updNodes.get(Long.valueOf(relat.getIdTargetNodeTemplate())).getName(); + Neighbour neigh = new Neighbour(); + neigh.setName(neighName); + neigh.setId(Long.valueOf(relat.getId())); + + //Retrieve the Neighbour map of the source Node and add the Neighbour + Node source = updNodes.get(Long.valueOf(relat.getIdSourceNodeTemplate())); + Map sourceNodeNeighMap = source.getNeighbours(); + if(sourceNodeNeighMap.containsKey(neigh.getId())) + throw new BadRequestException("The RelationshipTemplate ID must be unique."); + else + sourceNodeNeighMap.put(neigh.getId(), neigh); + source.setNeighbours(sourceNodeNeighMap); + + //Update the Node list + updNodes.put(Long.valueOf(relat.getIdSourceNodeTemplate()), source); + } catch(NullPointerException | NumberFormatException ex) { + throw new BadRequestException("A RelationshipTemplate has wrong fields representation."); + } + } + return updNodes; + } + + /** Mapping method --> from ToscaConfiguration to model Configuration */ + private static Configuration deriveConfiguration(ToscaConfigurationGrpc request) + throws BadRequestException, JsonProcessingException, IOException { + Configuration conf = new Configuration(); + ObjectMapper mapper = new ObjectMapper(); + JsonNode rootNode = null; + + try { + conf.setId(request.getId()); + } catch (NullPointerException e) {} + + try { + conf.setDescription(request.getDescription()); + } catch (NullPointerException e) {} + + try { + if ("".equals(request.getConfiguration())) + rootNode=mapper.readTree("[]"); + else + rootNode = mapper.readTree(request.getConfiguration()); + } catch (NullPointerException e) { + rootNode=mapper.readTree("[]"); + } + conf.setConfiguration(rootNode); + return conf; + } +} -- cgit 1.2.3-korg