1 /*******************************************************************************
\r
2 * Copyright (c) 2018 Politecnico di Torino and others.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Apache License, Version 2.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.apache.org/licenses/LICENSE-2.0
\r
8 *******************************************************************************/
\r
9 package it.polito.verigraph.tosca.converter.grpc;
\r
11 import java.io.IOException;
\r
12 import java.util.ArrayList;
\r
13 import java.util.List;
\r
14 import java.util.Map;
\r
15 import java.util.Map.Entry;
\r
17 import javax.xml.bind.JAXBException;
\r
19 import it.polito.verigraph.exception.BadRequestException;
\r
20 import it.polito.verigraph.exception.DataNotFoundException;
\r
21 import it.polito.verigraph.grpc.NodeTemplateGrpc;
\r
22 import it.polito.verigraph.grpc.NodeTemplateGrpc.Type;
\r
23 import it.polito.verigraph.grpc.RelationshipTemplateGrpc;
\r
24 import it.polito.verigraph.grpc.TopologyTemplateGrpc;
\r
25 import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
\r
26 import it.polito.verigraph.tosca.YamlParsingUtils;
\r
27 import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;
\r
28 import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;
\r
29 import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;
\r
31 public class YamlToGrpc {
\r
33 /** Returns the (first) TopologyTemplate found in the TOSCA-compliant yaml file */
\r
34 public static TopologyTemplateGrpc obtainTopologyTemplateGrpc (String filepath)
\r
35 throws IOException, JAXBException, DataNotFoundException, ClassCastException, BadRequestException{
\r
36 ServiceTemplateYaml serviceTemplate = YamlParsingUtils.obtainServiceTemplate(filepath);
\r
38 //Retrieving of list of NodeTemplate and RelationshipTemplate
\r
39 List<NodeTemplateGrpc> nodes = new ArrayList<NodeTemplateGrpc>();
\r
40 List<RelationshipTemplateGrpc> relats = new ArrayList<RelationshipTemplateGrpc>();
\r
43 for(Map.Entry<String, NodeTemplateYaml> node : YamlParsingUtils.obtainNodeTemplates(serviceTemplate).entrySet()) {
\r
44 for(NodeTemplateGrpc alreadyAddedNode : nodes)
\r
45 if(alreadyAddedNode.getId().equals(node.getKey()))
\r
46 throw new BadRequestException("The NodeTemplate ID must be unique.");
\r
47 nodes.add(parseNodeTemplate(node));
\r
49 } catch (NullPointerException e) {
\r
50 throw new BadRequestException("There is not any NodeTemplate in the ServiceTemplate provided.");
\r
54 for(Map.Entry<String, RelationshipTemplateYaml> rel : YamlParsingUtils.obtainRelationshipTemplates(serviceTemplate).entrySet()) {
\r
55 relats.add(parseRelationshipTemplate(rel, nodes));
\r
57 } catch (NullPointerException e) {
\r
58 throw new BadRequestException("There is not any RelationshipTemplate in the ServiceTemplate provided.");
\r
61 //Creating TopologyTemplateGrpc object to be sent to server
\r
62 return TopologyTemplateGrpc.newBuilder()
\r
63 .setId("0") //useless value since the server chooses the actual value for the GraphID
\r
64 .addAllNodeTemplate(nodes)
\r
65 .addAllRelationshipTemplate(relats)
\r
69 /** Parsing method: RelationshipTemplateYaml(tosca) --> RelationshipTemplateGrpc */
\r
70 private static RelationshipTemplateGrpc parseRelationshipTemplate(Entry<String, RelationshipTemplateYaml> rel, List<NodeTemplateGrpc> nodes) throws BadRequestException{
\r
71 String source, target;
\r
72 boolean valid_source = false;
\r
73 boolean valid_target = false;
\r
75 //RelationshipTemplateGrpc building
\r
76 RelationshipTemplateGrpc.Builder relatgrpc = RelationshipTemplateGrpc.newBuilder();
\r
80 relatgrpc.setId(rel.getKey());
\r
81 relatgrpc.setName(rel.getValue().getProperties().get("name"));
\r
82 source = rel.getValue().getProperties().get("source_id");
\r
83 target = rel.getValue().getProperties().get("target_id");
\r
84 } catch (NullPointerException ex) {
\r
85 throw new BadRequestException("Incorrect fields in RelationshipTemplate:" + rel.getKey());
\r
88 //Source and Target values must correctly refer to a NodeTemplate
\r
89 if(source.equals(target))
\r
90 throw new BadRequestException("Source and Target cannot be the same value");
\r
91 for(NodeTemplateGrpc node : nodes) {
\r
92 if(node.getId().equals(source))
\r
93 valid_source = true;
\r
94 if(node.getId().equals(target))
\r
95 valid_target = true;
\r
97 if(!(valid_source && valid_target))
\r
98 throw new BadRequestException("Invalid NodeTemplate reference in RelationshipTemplate:" + rel.getKey());
\r
100 return relatgrpc.setIdSourceNodeTemplate(source).setIdTargetNodeTemplate(target).build();
\r
104 /** Parsing method: NodeTemplateYaml(tosca) --> NodeTemplateGrpc */
\r
105 private static NodeTemplateGrpc parseNodeTemplate(Entry<String, NodeTemplateYaml> node)
\r
106 throws ClassCastException, NullPointerException, BadRequestException {
\r
107 Boolean isVerigraphCompl = true;
\r
110 //NodeTemplateGrpc building
\r
111 NodeTemplateGrpc.Builder nodegrpc = NodeTemplateGrpc.newBuilder()
\r
112 .setId(node.getKey());
\r
115 nodegrpc.setName(node.getValue().getName());
\r
116 } catch (NullPointerException ex) {
\r
117 throw new BadRequestException("Invalid name in a NodeTemplate.");
\r
120 //Type cannot be null but it can be invalid
\r
122 type = Type.valueOf(node.getValue().getType().replace("verigraph.nodeTypes.", "").toLowerCase());
\r
123 } catch (IllegalArgumentException | NullPointerException ex) {
\r
124 //in case the NodeTemplate is not TOSCA-Verigraph compliant, we assume it to be a fieldmodifier node
\r
125 type = Type.fieldmodifier;
\r
126 isVerigraphCompl = false;
\r
128 nodegrpc.setType(type);
\r
129 ToscaConfigurationGrpc.Builder grpcConfig;
\r
130 if(isVerigraphCompl) {
\r
131 String jsonConfig = YamlParsingUtils.obtainConfiguration(node.getValue());
\r
132 grpcConfig = ToscaConfigurationGrpc.newBuilder()
\r
134 .setDescription("")
\r
135 .setConfiguration(jsonConfig);
\r
138 grpcConfig = ToscaConfigurationGrpc.newBuilder()
\r
139 .setId(ToscaGrpcUtils.defaultConfID)
\r
140 .setDescription(ToscaGrpcUtils.defaultDescr)
\r
141 .setConfiguration(ToscaGrpcUtils.defaultConfig);
\r
143 nodegrpc.setConfiguration(grpcConfig.build());
\r
144 return nodegrpc.build();
\r