summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/tosca/converter/grpc/XmlToGrpc.java
blob: 426bb4a7cf30d031eb31053338b9675f86f50afe (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
157
158
159
160
/*******************************************************************************
 * 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 javax.xml.bind.JAXBException;

import com.fasterxml.jackson.core.JsonProcessingException;

import it.polito.tosca.jaxb.TNodeTemplate;
import it.polito.tosca.jaxb.TRelationshipTemplate;
import it.polito.tosca.jaxb.TServiceTemplate;
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.MappingUtils;
import it.polito.verigraph.tosca.XmlParsingUtils;

public class XmlToGrpc {

    /** Returns the (first) TopologyTemplate found in the TOSCA-compliant XML file */
    public static TopologyTemplateGrpc obtainTopologyTemplateGrpc (String filepath)
            throws IOException, JAXBException, DataNotFoundException, ClassCastException, BadRequestException{
        List<TServiceTemplate> serviceTList = XmlParsingUtils.obtainServiceTemplates(filepath);
        TServiceTemplate serviceTemplate = serviceTList.get(0); //obtain only the first ServiceTemplate of the TOSCA compliance file

        //Retrieving of list of NodeTemplate and RelationshipTemplate
        List<NodeTemplateGrpc> nodes = new ArrayList<NodeTemplateGrpc>();
        List<RelationshipTemplateGrpc> relats = new ArrayList<RelationshipTemplateGrpc>();
        List<TNodeTemplate> tNodes = XmlParsingUtils.obtainNodeTemplates(serviceTemplate);
        for(TNodeTemplate nt : tNodes) {
            for(NodeTemplateGrpc alreadyAddedNode : nodes)
                if(alreadyAddedNode.getId().equals(nt.getId()))
                    throw new BadRequestException("The NodeTemplate ID must be unique.");
            nodes.add(parseNodeTemplate(nt));
        }
        for(TRelationshipTemplate rt : XmlParsingUtils.obtainRelationshipTemplates(serviceTemplate)) {
            if(!tNodes.contains(rt.getSourceElement().getRef()) || !tNodes.contains(rt.getTargetElement().getRef()))
                throw new BadRequestException("Invalid references to a Node in a Relationship.");
            if(rt.getSourceElement().getRef() == rt.getTargetElement().getRef())
                throw new BadRequestException("Source and Target cannot be equal in a Relationship.");
            relats.add(parseRelationshipTemplate(rt));
        }

        //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: TNodeTemplate(tosca) --> NodeTemplateGrpc */
    private static NodeTemplateGrpc parseNodeTemplate(TNodeTemplate nodeTempl)
            throws ClassCastException, NullPointerException {
        Boolean isVerigraphCompl = true;
        Type type;

        //NodeTemplateGrpc building
        NodeTemplateGrpc.Builder nodegrpc = NodeTemplateGrpc.newBuilder();

        //ID cannot be null
        try {
            nodegrpc.setId(nodeTempl.getId());
        } catch (NullPointerException ex) {
            throw new NullPointerException("An ID must be specified for each Node");
        }
        //Name can be null
        try {
            nodegrpc.setName(nodeTempl.getName());
        } catch (NullPointerException ex) {
            nodegrpc.setName("");
        }

        //Type cannot be null but it can be invalid
        try {
            String typestring = nodeTempl.getType().getLocalPart().toLowerCase();
            type = Type.valueOf(nodeTempl.getType().getLocalPart().toLowerCase().substring(0,typestring.length()-4));
        } 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) {
            it.polito.tosca.jaxb.Configuration nodeConfig = XmlParsingUtils.obtainConfiguration(nodeTempl);
            grpcConfig = ToscaConfigurationGrpc.newBuilder();
            //These fields are optional in TOSCA xml
            try {
                grpcConfig.setId(nodeConfig.getConfID());
            } catch(NullPointerException ex) {
                grpcConfig.setId(ToscaGrpcUtils.defaultConfID);
            }
            try {
                grpcConfig.setDescription(nodeConfig.getConfDescr());
            } catch(NullPointerException ex) {
                grpcConfig.setDescription(ToscaGrpcUtils.defaultDescr);
            }
            try {;
            grpcConfig.setConfiguration(MappingUtils.obtainStringConfiguration(nodeConfig));
            } catch(NullPointerException | JsonProcessingException ex) {
                grpcConfig.setConfiguration(ToscaGrpcUtils.defaultConfig);
            }
        }
        else {
            grpcConfig = ToscaConfigurationGrpc.newBuilder()
                    .setId(ToscaGrpcUtils.defaultConfID)
                    .setDescription(ToscaGrpcUtils.defaultDescr)
                    .setConfiguration(ToscaGrpcUtils.defaultConfig);
        }
        nodegrpc.setConfiguration(grpcConfig.build());
        return nodegrpc.build();
    }


    /** Parsing method: TRelationshipTemplate(tosca) --> RelationshipTemplateGrpc */
    private static RelationshipTemplateGrpc parseRelationshipTemplate(TRelationshipTemplate relatTempl)
            throws ClassCastException{
        String source, target;
        //RelationshipTemplateGrpc building
        RelationshipTemplateGrpc.Builder relatgrpc = RelationshipTemplateGrpc.newBuilder();

        //ID and Name can be null
        try {
            relatgrpc.setId(relatTempl.getId());
        } catch (NullPointerException ex) {}//Different Relationship with same ID are considered valid.
        try {
            relatgrpc.setName(relatTempl.getName());
        } catch (NullPointerException ex) {}

        //Source and Target values cannot be null
        try {
            TNodeTemplate sourceNode = (TNodeTemplate) relatTempl.getSourceElement().getRef();
            TNodeTemplate targetNode = (TNodeTemplate) relatTempl.getTargetElement().getRef();
            source = sourceNode.getId();
            target = targetNode.getId();
        } catch (NullPointerException ex) {
            throw new NullPointerException("Invalid NodeTemplate reference in RelationshipTemplate with id:"
                    + relatTempl.getId());
        }
        relatgrpc.setIdSourceNodeTemplate(source)
        .setIdTargetNodeTemplate(target);
        return relatgrpc.build();
    }
}