summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/tosca/converter/grpc/YamlToGrpc.java
blob: 1a713bd6a4976e2b9e68b8deacb592cc7be6186e (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
/*******************************************************************************
 * 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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.JAXBException;

import it.polito.verigraph.exception.BadRequestException;
import it.polito.verigraph.exception.DataNotFoundException;
import it.polito.verigraph.grpc.NodeTemplateGrpc;
import it.polito.verigraph.grpc.NodeTemplateGrpc.Type;
import it.polito.verigraph.grpc.RelationshipTemplateGrpc;
import it.polito.verigraph.grpc.TopologyTemplateGrpc;
import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
import it.polito.verigraph.tosca.YamlParsingUtils;
import it.polito.verigraph.tosca.yaml.beans.NodeTemplateYaml;
import it.polito.verigraph.tosca.yaml.beans.RelationshipTemplateYaml;
import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;

public class YamlToGrpc {

    /** Returns the (first) TopologyTemplate found in the TOSCA-compliant yaml file */
    public static TopologyTemplateGrpc obtainTopologyTemplateGrpc (String filepath)
            throws IOException, JAXBException, DataNotFoundException, ClassCastException, BadRequestException{
        ServiceTemplateYaml serviceTemplate = YamlParsingUtils.obtainServiceTemplate(filepath);

        //Retrieving of list of NodeTemplate and RelationshipTemplate
        List<NodeTemplateGrpc> nodes = new ArrayList<NodeTemplateGrpc>();
        List<RelationshipTemplateGrpc> relats = new ArrayList<RelationshipTemplateGrpc>();

        try {
            for(Map.Entry<String, NodeTemplateYaml> node : YamlParsingUtils.obtainNodeTemplates(serviceTemplate).entrySet()) {
                for(NodeTemplateGrpc alreadyAddedNode : nodes)
                    if(alreadyAddedNode.getId().equals(node.getKey()))
                        throw new BadRequestException("The NodeTemplate ID must be unique.");
                nodes.add(parseNodeTemplate(node));
            }
        } catch (NullPointerException e) {
            throw new BadRequestException("There is not any NodeTemplate in the ServiceTemplate provided.");
        }

        try {
            for(Map.Entry<String, RelationshipTemplateYaml> rel : YamlParsingUtils.obtainRelationshipTemplates(serviceTemplate).entrySet()) {
                relats.add(parseRelationshipTemplate(rel, nodes));
            }
        } catch (NullPointerException e) {
            throw new BadRequestException("There is not any RelationshipTemplate in the ServiceTemplate provided.");
        }

        //Creating TopologyTemplateGrpc object to be sent to server
        return TopologyTemplateGrpc.newBuilder()
                .setId("0") //useless value since the server chooses the actual value for the GraphID
                .addAllNodeTemplate(nodes)
                .addAllRelationshipTemplate(relats)
                .build();
    }

    /** Parsing method: RelationshipTemplateYaml(tosca) --> RelationshipTemplateGrpc */
    private static RelationshipTemplateGrpc parseRelationshipTemplate(Entry<String, RelationshipTemplateYaml> rel, List<NodeTemplateGrpc> nodes) throws BadRequestException{
        String source, target;
        boolean valid_source = false;
        boolean valid_target = false;

        //RelationshipTemplateGrpc building
        RelationshipTemplateGrpc.Builder relatgrpc = RelationshipTemplateGrpc.newBuilder();  

        //ID can be null
        try {
            relatgrpc.setId(rel.getKey());
            relatgrpc.setName(rel.getValue().getProperties().get("name"));
            source = rel.getValue().getProperties().get("source_id");
            target = rel.getValue().getProperties().get("target_id");
        } catch (NullPointerException ex) {
            throw new BadRequestException("Incorrect fields in RelationshipTemplate:" + rel.getKey());
        }

        //Source and Target values must correctly refer to a NodeTemplate
        if(source.equals(target))
            throw new BadRequestException("Source and Target cannot be the same value");
        for(NodeTemplateGrpc node : nodes) {
            if(node.getId().equals(source))
                valid_source = true;
            if(node.getId().equals(target))
                valid_target = true;
        }
        if(!(valid_source && valid_target))
            throw new BadRequestException("Invalid NodeTemplate reference in RelationshipTemplate:" + rel.getKey());

        return relatgrpc.setIdSourceNodeTemplate(source).setIdTargetNodeTemplate(target).build();

    }

    /** Parsing method: NodeTemplateYaml(tosca) --> NodeTemplateGrpc */
    private static NodeTemplateGrpc parseNodeTemplate(Entry<String, NodeTemplateYaml> node)
            throws ClassCastException, NullPointerException, BadRequestException {   
        Boolean isVerigraphCompl = true;
        Type type;

        //NodeTemplateGrpc building
        NodeTemplateGrpc.Builder nodegrpc = NodeTemplateGrpc.newBuilder()
                .setId(node.getKey());

        try {
            nodegrpc.setName(node.getValue().getName());
        } catch (NullPointerException ex) {
            throw new BadRequestException("Invalid name in a NodeTemplate.");
        }

        //Type cannot be null but it can be invalid
        try {
            type = Type.valueOf(node.getValue().getType().replace("verigraph.nodeTypes.", "").toLowerCase());
        } catch (IllegalArgumentException | NullPointerException ex) {
            //in case the NodeTemplate is not TOSCA-Verigraph compliant, we assume it to be a fieldmodifier node
            type = Type.fieldmodifier;
            isVerigraphCompl = false;
        }
        nodegrpc.setType(type);
        ToscaConfigurationGrpc.Builder grpcConfig;   
        if(isVerigraphCompl) {
            String jsonConfig = YamlParsingUtils.obtainConfiguration(node.getValue());
            grpcConfig = ToscaConfigurationGrpc.newBuilder()
                    .setId("")
                    .setDescription("")
                    .setConfiguration(jsonConfig);
        }
        else {
            grpcConfig = ToscaConfigurationGrpc.newBuilder()
                    .setId(ToscaGrpcUtils.defaultConfID)
                    .setDescription(ToscaGrpcUtils.defaultDescr)
                    .setConfiguration(ToscaGrpcUtils.defaultConfig);
        }   
        nodegrpc.setConfiguration(grpcConfig.build());
        return nodegrpc.build();
    }

}