summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/tosca/MappingUtils.java
blob: a415ad2a9d0c2f5e7339db0a07ec9119bb23688a (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*******************************************************************************
 * 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;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

import it.polito.tosca.jaxb.Definitions;
import it.polito.tosca.jaxb.TDocumentation;
import it.polito.tosca.jaxb.TServiceTemplate;
import it.polito.verigraph.grpc.ToscaConfigurationGrpc;
import it.polito.verigraph.model.Graph;
import it.polito.verigraph.model.Node;
import it.polito.verigraph.model.Test;
import it.polito.verigraph.model.Verification;
import it.polito.verigraph.tosca.converter.xml.GraphToXml;
import it.polito.verigraph.tosca.converter.yaml.GraphToYaml;
import it.polito.verigraph.tosca.deserializer.XmlConfigurationDeserializer;
import it.polito.verigraph.tosca.serializer.XmlConfigSerializer;
import it.polito.verigraph.tosca.yaml.beans.ServiceTemplateYaml;
import it.polito.verigraph.tosca.yaml.beans.VerificationYaml;

public class MappingUtils {

    public static String prettyPrintJsonString(JsonNode jsonNode) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            Object json = mapper.readValue(jsonNode.toString(), Object.class);
            return System.getProperty("line.separator") + mapper.writerWithDefaultPrettyPrinter()
            .writeValueAsString(json) + System.getProperty("line.separator");
        } catch (Exception e) {
            return "Sorry, pretty print didn't work";
        }
    }

    // From a list of nodes (path) returns a Definitions object that contains all the paths as different service templates
    public static Definitions mapPathsToXml(List<List<Node>> paths) {
        Definitions definitions = new Definitions();
        List<Graph> tempGraphs = new ArrayList<Graph>();

        int i = 0;
        for (List<Node> path: paths) {
            Graph tempGraph = new Graph();
            tempGraph.setId(i++);
            for (Node node : path)
                tempGraph.getNodes().put(node.getId(), node);
            tempGraphs.add(tempGraph);
        }

        for (Graph g: tempGraphs) {
            definitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(GraphToXml.mapPathToXml(g));
        }

        return definitions;
    }


    // From a list of nodes (path) returns a list of ServiceTemplateYaml object that represent the paths
    public static List<ServiceTemplateYaml> mapPathsToYaml(List<List<Node>> paths) {
        List<ServiceTemplateYaml> serviceTemplates = new ArrayList<ServiceTemplateYaml>();
        List<Graph> tempGraphs = new ArrayList<Graph>();

        int i = 0;
        for (List<Node> path: paths) {
            Graph tempGraph = new Graph();
            tempGraph.setId(i++);
            for (Node node : path)
                tempGraph.getNodes().put(node.getId(), node);
            tempGraphs.add(tempGraph);
        }

        for (Graph g: tempGraphs) {
            serviceTemplates.add(GraphToYaml.mapGraphYaml(g));
        }

        return serviceTemplates;
    }


    public static Definitions mapVerificationToXml(Verification verification) {
        Definitions toscaVerification = new Definitions();
        TDocumentation toscaVerificationResult = new TDocumentation();
        toscaVerificationResult.setSource(verification.getResult() + ": " + verification.getComment());
        toscaVerification.getDocumentation().add(toscaVerificationResult);

        List<TServiceTemplate> toscaPaths = new ArrayList<TServiceTemplate>();

        int i = 0;
        for (Test test: verification.getTests()) {
            Graph tempGraph = new Graph();
            tempGraph.setId(i++);
            for (Node node : test.getPath())
                tempGraph.getNodes().put(node.getId(), node);

            TServiceTemplate toscaPath = GraphToXml.mapPathToXml(tempGraph);
            TDocumentation toscaTestResult = new TDocumentation();
            toscaTestResult.setSource(test.getResult());
            toscaPath.getDocumentation().add(toscaTestResult);
            toscaPaths.add(toscaPath);
        }

        toscaVerification.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().addAll(0, toscaPaths);
        return toscaVerification;
    }


    public static VerificationYaml mapVerificationToYaml(Verification verification) {
        VerificationYaml verificationYaml = new VerificationYaml();
        verificationYaml.setResult(verification.getResult());
        verificationYaml.setComment(verification.getComment());

        List<ServiceTemplateYaml> toscaPaths = new ArrayList<ServiceTemplateYaml>();

        int i = 0;
        for (Test test: verification.getTests()) {
            Graph tempGraph = new Graph();
            tempGraph.setId(i++);
            for (Node node : test.getPath())
                tempGraph.getNodes().put(node.getId(), node);

            ServiceTemplateYaml toscaPath = GraphToYaml.mapGraphYaml(tempGraph);
            toscaPath.getMetadata().put("result", test.getResult());
            toscaPaths.add(toscaPath);
        }

        verificationYaml.setPaths(toscaPaths);
        return verificationYaml;
    }


    /** Return a string that represent the Tosca Configuration in json string.
     *
     * The string can be converted in JsonNode to be inserted in Model Configuration.
     *
     * Used for: xml-->model
     * @throws JsonProcessingException*/
    public static String obtainStringConfiguration(it.polito.tosca.jaxb.Configuration nodeConfig) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addSerializer(it.polito.tosca.jaxb.Configuration.class, new XmlConfigSerializer());
        mapper.registerModule(module);

        String stringConfiguration = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(nodeConfig);
        if (stringConfiguration.equals("") || stringConfiguration == null)
            return "[]";
        else
            return stringConfiguration;
    }


    /** Return a Tosca Configuration with inside the representation of a model Configuration (only its JsonNode)
     *
     * Used for: model-->xml
     * @throws JsonProcessingException */
    public static it.polito.tosca.jaxb.Configuration obtainToscaConfiguration(it.polito.verigraph.model.Configuration modelConfig, String type) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();

        //Passing the configuration type to the Deserializer context
        module.addDeserializer(it.polito.tosca.jaxb.Configuration.class, new XmlConfigurationDeserializer());
        mapper.registerModule(module);

        it.polito.tosca.jaxb.Configuration toscaConfig = new it.polito.tosca.jaxb.Configuration();
        try {
            toscaConfig = mapper.reader(new InjectableValues.Std().addValue("type", type))
                    .forType(it.polito.tosca.jaxb.Configuration.class)
                    .readValue(modelConfig.getConfiguration());
        } catch (IOException e) {
            //TODO shall we suppose that configuration stored on DB are always correct?
        }

        return toscaConfig;
    }

    /** Return a Tosca Configuration from a ConfigurationGrpc
     *
     * Used for: grpc-->xml
     * @throws JsonProcessingException */
    public static it.polito.tosca.jaxb.Configuration obtainToscaConfiguration(ToscaConfigurationGrpc grpcConfig, String type) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();

        //Passing the configuration type to the Deserializer context
        module.addDeserializer(it.polito.tosca.jaxb.Configuration.class, new XmlConfigurationDeserializer());
        mapper.registerModule(module);

        it.polito.tosca.jaxb.Configuration toscaConfig = new it.polito.tosca.jaxb.Configuration();
        try {
            toscaConfig = mapper.reader(new InjectableValues.Std().addValue("type", type))
                    .forType(it.polito.tosca.jaxb.Configuration.class)
                    .readValue(grpcConfig.getConfiguration());
        } catch (IOException e) {
            //TODO shall we suppose that configuration stored on DB are always correct?
        }

        return toscaConfig;
    }

}