aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/openecomp/vid/asdc/local/LocalAsdcClient.java
blob: 696e18904e5cbc35ecef01b305ee55b36cb8855e (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package org.openecomp.vid.asdc.local;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONObject;
import org.openecomp.vid.asdc.AsdcCatalogException;
import org.openecomp.vid.asdc.AsdcClient;
import org.openecomp.vid.asdc.beans.Artifact;
import org.openecomp.vid.asdc.beans.Resource;
import org.openecomp.vid.asdc.beans.Service;
import org.openecomp.vid.asdc.beans.tosca.ToscaCsar;
import org.openecomp.vid.asdc.beans.tosca.ToscaMeta;
import org.openecomp.vid.asdc.beans.tosca.ToscaModel;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.zip.ZipFile;

/**
 * The Class LocalAsdcClient.
 */
public class LocalAsdcClient implements AsdcClient {


    /**
     * The catalog.
     */
    private final JSONObject catalog;

    /**
     * The mapper.
     */
    private final ObjectMapper mapper;

    /**
     * The Class Builder.
     */
    public static class Builder {

        /**
         * The catalog.
         */
        private JSONObject catalog = new JSONObject()
                .put("resources", new JSONObject())
                .put("services", new JSONObject());

        /**
         * The mapper.
         */
        private ObjectMapper mapper = new ObjectMapper();

        /**
         * Instantiates a new builder.
         */
        public Builder() {
        }

        /**
         * Catalog.
         *
         * @param catalog the catalog
         * @return the builder
         */
        public org.openecomp.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
            this.catalog = catalog;
            return this;
        }

        /**
         * Mapper.
         *
         * @param mapper the mapper
         * @return the builder
         */
        public org.openecomp.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
            this.mapper = mapper;
            return this;
        }

        /**
         * Builds the.
         *
         * @return the in local sdc client
         */
        public org.openecomp.vid.asdc.local.LocalAsdcClient build() {
            return new org.openecomp.vid.asdc.local.LocalAsdcClient(this);
        }
    }

    /**
     * Instantiates a new in local sdc client.
     *
     * @param builder the builder
     */
    private LocalAsdcClient(org.openecomp.vid.asdc.local.LocalAsdcClient.Builder builder) {
        catalog = builder.catalog;
        mapper = builder.mapper;
    }

    /**
     * Gets the catalog.
     *
     * @return the catalog
     */
    private JSONObject getCatalog() {
        return catalog;
    }

    /**
     * Gets the mapper.
     *
     * @return the mapper
     */
    private ObjectMapper getMapper() {
        return mapper;
    }

    /**
     * Convert.
     *
     * @param <T>   the generic type
     * @param json  the json
     * @param clazz the clazz
     * @return the t
     * @throws AsdcCatalogException the sdc catalog exception
     */
    private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
        try {
            return getMapper().readValue(json.toString(), clazz);
        } catch (JsonParseException e) {
            throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
        } catch (JsonMappingException e) {
            throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
        } catch (IOException e) {
            throw new AsdcCatalogException("Failed to get a response from SDC", e);
        }
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
     */
    public Resource getResource(UUID uuid) throws AsdcCatalogException {
        final JSONObject resource = getCatalog().getJSONObject("resources")
                .getJSONObject(uuid.toString());
        return convert(resource, Resource.class);
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getResources()
     */
    public Collection<Resource> getResources() throws AsdcCatalogException {
        final Collection<Resource> resources = new LinkedList<Resource>();

        for (String key : getCatalog().getJSONObject("resources").keySet()) {
            final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
            final Resource resource = convert(json, Resource.class);
            resources.add(resource);
        }

        return resources;
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
     */
    public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
        final Collection<Resource> resources = new LinkedList<Resource>();

        for (String key : getCatalog().getJSONObject("resources").keySet()) {
            final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);

            boolean filterMatch = true;

            for (Map.Entry<String, String[]> entry : filter.entrySet()) {
                for (int i = 0; i < entry.getValue().length; i++) {
                    if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
                        filterMatch = false;
                        break;
                    }
                }
            }

            if (filterMatch) resources.add(convert(json, Resource.class));
        }

        return resources;
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
     */
    public Service getService(UUID uuid) throws AsdcCatalogException {

        JSONObject serviceJsonObject = null;
        final JSONArray categoryJsonArray = getCatalog().getJSONArray("services");

        for (int i = 0; i < categoryJsonArray.length() ; i++) {
            JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
            if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
                serviceJsonObject = jsonServiceObject;
                break;
            }
        }

        if (serviceJsonObject != null)
            return convert(serviceJsonObject, Service.class);
        else return null;
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getServices()
     */
    public Collection<Service> getServices() throws AsdcCatalogException {
        final Collection<Service> services = new LinkedList<Service>();

        JSONArray servicesArr = getCatalog().getJSONArray("services");

        for (Object objService : servicesArr) {
            JSONObject jsonServiceItem = (JSONObject) objService;
            final Service service = convert(jsonServiceItem, Service.class);
            services.add(service);
        }

        return services;
    }

    /* (non-Javadoc)
     * @see org.openecompt.vid.asdc.AsdcClient#getServices(java.util.Map)
     */
    public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
        final Collection<Service> services = new LinkedList<Service>();

        JSONArray catalogServices = catalog.getJSONArray("services");

        for (int i = 0; i < catalogServices.length(); i++) {

            JSONObject serviceJson = catalogServices.getJSONObject(i);

            boolean filterMatch = true;

            for (Map.Entry<String, String[]> entry : filter.entrySet()) {
                for (int j = 0; j < entry.getValue().length; j++) {
                    if (!serviceJson.getString(entry.getKey()).equals(entry.getValue()[j])) {
                        filterMatch = false;
                        break;
                    }
                }
            }
            if (filterMatch) services.add(convert(serviceJson, Service.class));
        }
        return services;
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
     */
    public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
        final  JSONArray artifacts = getCatalog().getJSONObject("resources")
                .getJSONObject(resourceUuid.toString())
                .getJSONArray("artifacts");

        for (int i = 0; i < artifacts.length(); i++) {
            final JSONObject artifact = artifacts.getJSONObject(i);

            if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
                return convert(artifact, Artifact.class);
            }
        }

        return null;
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
     */
    public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
        final JSONArray artifacts = getCatalog().getJSONObject("services")
                .getJSONObject(serviceUuid.toString())
                .getJSONArray("artifacts");

        for (int i = 0; i < artifacts.length(); i++) {
            final JSONObject artifact = artifacts.getJSONObject(i);

            if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
                return convert(artifact, Artifact.class);
            }
        }

        return null;
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
     */
    public Path getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
        final String toscaModelURL = getCatalog().getJSONObject("resources")
                .getJSONObject(resourceUuid.toString())
                .getString("toscaModelURL");


        final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);

        if (toscaModelStream == null) return null;

        return null;//getToscaModel(toscaModelStream);
    }

    /* (non-Javadoc)
     * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
     */
    public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {

        String toscaModelURL = null;

        final JSONArray categoryJsonArray = getCatalog().getJSONArray("services");

        for (int i = 0; i < categoryJsonArray.length() ; i++) {

            JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
            if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
                toscaModelURL = jsonServiceObject.getString("toscaModelURL");
                break;
            }
        }

        if (toscaModelURL==null){
            return null;
        }
        
        final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);

        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource(toscaModelURL).getFile());
        Path path = Paths.get(file.getPath());

        if (toscaModelStream == null) return null;

        return path;
    }

    /**
     * Gets the tosca model.
     *
     * @param csarInputStream the csar input stream
     * @return the tosca model
     * @throws AsdcCatalogException the asdc catalog exception
     */
    private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
        final Path csarFile;

        try {
            csarFile = Files.createTempFile("csar", ".zip");
            Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
        }

        try (final ZipFile csar = new ZipFile(csarFile.toFile())) {

            final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
            final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
            final String entryDefinitions = toscaMeta.get("Entry-Definitions");
            final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));

            final Yaml yaml = new Yaml();
            final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);

            final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);

            for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
                for (Map.Entry<String, Map<String, String>> entry : imports.entrySet()) {
                    final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));
                    final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
                    csarBuilder.addVnf(childModel);
                }
            }

            return csarBuilder.build();
        } catch (IOException e) {
            throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
        }
    }

}