aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/asdc/local/LocalAsdcClient.java
blob: bcb201d2e525a29a6549392f742716c587be4c81 (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
package org.onap.vid.asdc.local;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.vid.asdc.AsdcCatalogException;
import org.onap.vid.asdc.AsdcClient;
import org.onap.vid.asdc.beans.Service;
import org.onap.vid.exceptions.GenericUncheckedException;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

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


    public static final String SERVICES = "services";
    /**
     * The catalog.
     */
    private final JSONObject catalog;

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

    /**
     * Instantiates a new in local sdc client.
     *
     * @param builder the builder
     */
    public LocalAsdcClient(org.onap.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.onap.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.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
     */
    public Path getServiceToscaModel(UUID serviceUuid) {

        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");
            }
        }
        if (toscaModelURL == null) {
            return null;
        }
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource(toscaModelURL).getFile());

        try {
            //using URLDecoder.decode to convert special characters from %XX to real character
            //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
            return Paths.get(URLDecoder.decode(file.getPath(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new GenericUncheckedException(e);
        }
    }

    /**
     * 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.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
            this.catalog = catalog;
            return this;
        }

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

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

}