summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/tosca/converter/grpc/GrpcToXml.java
blob: 002737ab84f41d329a86ece4914ec709f5e54531 (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
/*******************************************************************************
 * 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.List;

import javax.xml.namespace.QName;

import it.polito.tosca.jaxb.Configuration;
import it.polito.tosca.jaxb.Definitions;
import it.polito.tosca.jaxb.TEntityTemplate.Properties;
import it.polito.tosca.jaxb.TNodeTemplate;
import it.polito.tosca.jaxb.TRelationshipTemplate;
import it.polito.tosca.jaxb.TRelationshipTemplate.SourceElement;
import it.polito.tosca.jaxb.TRelationshipTemplate.TargetElement;
import it.polito.tosca.jaxb.TServiceTemplate;
import it.polito.tosca.jaxb.TTopologyTemplate;
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.tosca.MappingUtils;

public class GrpcToXml {

    public static Definitions mapGraph(TopologyTemplateGrpc topologyGrpc) {
        Definitions definitions = new Definitions();
        TServiceTemplate serviceTemplate = new TServiceTemplate();
        TTopologyTemplate topologyTemplate = new TTopologyTemplate();

        for(NodeTemplateGrpc node : topologyGrpc.getNodeTemplateList()) {
            TNodeTemplate nodeTemplate = mapNode(node);
            topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(nodeTemplate);
        }
        for(RelationshipTemplateGrpc relat : topologyGrpc.getRelationshipTemplateList()) {
            TRelationshipTemplate relationshipTemplate = mapRelationship(relat, topologyGrpc.getNodeTemplateList());
            topologyTemplate.getNodeTemplateOrRelationshipTemplate().add(relationshipTemplate);
        }

        try {
            serviceTemplate.setId(String.valueOf(topologyGrpc.getId()));
        } catch (NullPointerException e) {
            throw new NullPointerException("The TopologyTemplateGrpc must have an ID.");
        }
        serviceTemplate.setTopologyTemplate(topologyTemplate);
        definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(serviceTemplate);
        return definitions;
    }


    private static TNodeTemplate mapNode(NodeTemplateGrpc node){
        TNodeTemplate nodeTemplate = new TNodeTemplate();

        try {
            nodeTemplate.setId(String.valueOf(node.getId()));
        } catch (NullPointerException e) {
            throw new NullPointerException("The NodeTemplateGrpc must have an ID.");
        }
        try {
            nodeTemplate.setName(node.getName());
        } catch (NullPointerException e) {
            throw new NullPointerException("The NodeTemplateGrpc must have a name.");
        }

        try {
            //QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12/ToscaVerigraphDefinition")
            QName type = new QName("http://docs.oasis-open.org/tosca/ns/2011/12",
                    node.getType().toString().substring(0, 1).toUpperCase() +
                    node.getType().toString().substring(1) + "Type");
            nodeTemplate.setType(type);
        } catch (NullPointerException e) {
            throw new NullPointerException("The NodeTemplateGrpc must have a valid type.");
        }

        Configuration config = mapModelConfiguration(node.getConfiguration(), node.getType().toString().toLowerCase());
        nodeTemplate.setProperties(new Properties());
        nodeTemplate.getProperties().setAny(config);
        return nodeTemplate;
    }


    private static TRelationshipTemplate mapRelationship(RelationshipTemplateGrpc relat, List<NodeTemplateGrpc> nodeList) {
        TRelationshipTemplate relationship = new TRelationshipTemplate();
        SourceElement source = new SourceElement();
        TargetElement target = new TargetElement();
        int check = 0;

        TNodeTemplate sourceNode = new TNodeTemplate();
        TNodeTemplate targetNode = new TNodeTemplate();

        try {
            for(NodeTemplateGrpc node : nodeList) {
                if(node.getId().equals(relat.getIdSourceNodeTemplate())) {
                    sourceNode = mapNode(node);
                    check++;
                }
                if(node.getId().equals(relat.getIdTargetNodeTemplate())) {
                    targetNode = mapNode(node);
                    check++;
                }
            }
        } catch (NullPointerException e) {
            throw new BadRequestException("A RelationshipTemplateGrpc must contain both source and target node ID.");
        }
        if(check != 2)
            throw new BadRequestException("A RelationshipTemplateGrpc must contain both source and target node ID.");

        source.setRef(sourceNode);
        target.setRef(targetNode);

        relationship.setId(relat.getId()); //TODO da valutare
        relationship.setSourceElement(source);
        relationship.setTargetElement(target);
        relationship.setName(sourceNode.getName()+"To"+targetNode.getName());

        return relationship;
    }


    private static it.polito.tosca.jaxb.Configuration mapModelConfiguration(ToscaConfigurationGrpc toscaConfigurationGrpc, String type) {
        it.polito.tosca.jaxb.Configuration configuration = new it.polito.tosca.jaxb.Configuration();
        try {
            //We are passing the configuration type to the Deserializer context
            configuration = MappingUtils.obtainToscaConfiguration(toscaConfigurationGrpc, type);

            //In Graph, ID and DESCRIPTION are always empty
            //configuration.setConfID(confGrpc.getId());
            //configuration.setConfDescr(confGrpc.getDescription());

        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
        }
        return configuration;
    }

}