1 package org.onap.vid.asdc.local;
3 import org.codehaus.jackson.JsonParseException;
4 import org.codehaus.jackson.map.JsonMappingException;
5 import org.codehaus.jackson.map.ObjectMapper;
6 import org.json.JSONArray;
7 import org.json.JSONObject;
8 import org.onap.vid.asdc.AsdcCatalogException;
9 import org.onap.vid.asdc.AsdcClient;
10 import org.onap.vid.asdc.beans.Artifact;
11 import org.onap.vid.asdc.beans.Resource;
12 import org.onap.vid.asdc.beans.Service;
13 import org.onap.vid.asdc.beans.tosca.ToscaCsar;
14 import org.onap.vid.asdc.beans.tosca.ToscaMeta;
15 import org.onap.vid.asdc.beans.tosca.ToscaModel;
16 import org.yaml.snakeyaml.Yaml;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.nio.file.StandardCopyOption;
25 import java.util.Collection;
26 import java.util.LinkedList;
28 import java.util.UUID;
29 import java.util.zip.ZipFile;
32 * The Class LocalAsdcClient.
34 public class LocalAsdcClient implements AsdcClient {
40 private final JSONObject catalog;
45 private final ObjectMapper mapper;
48 * Instantiates a new in local sdc client.
50 * @param builder the builder
52 public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
53 catalog = builder.catalog;
54 mapper = builder.mapper;
62 private JSONObject getCatalog() {
71 private ObjectMapper getMapper() {
78 * @param <T> the generic type
79 * @param json the json
80 * @param clazz the clazz
82 * @throws AsdcCatalogException the sdc catalog exception
84 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
86 return getMapper().readValue(json.toString(), clazz);
87 } catch (JsonParseException e) {
88 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
89 } catch (JsonMappingException e) {
90 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
91 } catch (IOException e) {
92 throw new AsdcCatalogException("Failed to get a response from SDC", e);
97 * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
99 public Resource getResource(UUID uuid) throws AsdcCatalogException {
100 final JSONObject resource = getCatalog().getJSONObject("resources")
101 .getJSONObject(uuid.toString());
102 return convert(resource, Resource.class);
106 * @see org.openecomp.vid.asdc.AsdcClient#getResources()
108 public Collection<Resource> getResources() throws AsdcCatalogException {
109 final Collection<Resource> resources = new LinkedList<Resource>();
111 for (String key : getCatalog().getJSONObject("resources").keySet()) {
112 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
113 final Resource resource = convert(json, Resource.class);
114 resources.add(resource);
121 * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
123 public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
124 final Collection<Resource> resources = new LinkedList<Resource>();
126 for (String key : getCatalog().getJSONObject("resources").keySet()) {
127 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
129 boolean filterMatch = true;
131 for (Map.Entry<String, String[]> entry : filter.entrySet()) {
132 for (int i = 0; i < entry.getValue().length; i++) {
133 if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
140 if (filterMatch) resources.add(convert(json, Resource.class));
147 * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
149 public Service getService(UUID uuid) throws AsdcCatalogException {
151 JSONObject serviceJsonObject = null;
152 final JSONArray categoryJsonArray = getCatalog().getJSONArray("services");
154 for (int i = 0; i < categoryJsonArray.length(); i++) {
155 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
156 if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
157 serviceJsonObject = jsonServiceObject;
162 if (serviceJsonObject != null)
163 return convert(serviceJsonObject, Service.class);
168 * @see org.openecomp.vid.asdc.AsdcClient#getServices()
170 public Collection<Service> getServices() throws AsdcCatalogException {
171 final Collection<Service> services = new LinkedList<Service>();
173 JSONArray servicesArr = getCatalog().getJSONArray("services");
175 for (Object objService : servicesArr) {
176 JSONObject jsonServiceItem = (JSONObject) objService;
177 final Service service = convert(jsonServiceItem, Service.class);
178 services.add(service);
185 * @see org.openecompt.vid.asdc.AsdcClient#getServices(java.util.Map)
187 public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
188 final Collection<Service> services = new LinkedList<Service>();
190 JSONArray catalogServices = catalog.getJSONArray("services");
192 for (int i = 0; i < catalogServices.length(); i++) {
194 JSONObject serviceJson = catalogServices.getJSONObject(i);
196 boolean filterMatch = true;
198 for (Map.Entry<String, String[]> entry : filter.entrySet()) {
199 for (int j = 0; j < entry.getValue().length; j++) {
200 if (!serviceJson.getString(entry.getKey()).equals(entry.getValue()[j])) {
206 if (filterMatch) services.add(convert(serviceJson, Service.class));
212 * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
214 public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
215 final JSONArray artifacts = getCatalog().getJSONObject("resources")
216 .getJSONObject(resourceUuid.toString())
217 .getJSONArray("artifacts");
219 for (int i = 0; i < artifacts.length(); i++) {
220 final JSONObject artifact = artifacts.getJSONObject(i);
222 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
223 return convert(artifact, Artifact.class);
231 * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
233 public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
234 final JSONArray artifacts = getCatalog().getJSONObject("services")
235 .getJSONObject(serviceUuid.toString())
236 .getJSONArray("artifacts");
238 for (int i = 0; i < artifacts.length(); i++) {
239 final JSONObject artifact = artifacts.getJSONObject(i);
241 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
242 return convert(artifact, Artifact.class);
250 * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
252 public Path getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
253 final String toscaModelURL = getCatalog().getJSONObject("resources")
254 .getJSONObject(resourceUuid.toString())
255 .getString("toscaModelURL");
258 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
260 if (toscaModelStream == null) return null;
262 return null;//getToscaModel(toscaModelStream);
266 * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
268 public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
270 String toscaModelURL = null;
272 final JSONArray categoryJsonArray = getCatalog().getJSONArray("services");
274 for (int i = 0; i < categoryJsonArray.length(); i++) {
276 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
277 if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
278 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
282 if (toscaModelURL == null) {
285 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
287 ClassLoader classLoader = getClass().getClassLoader();
288 File file = new File(classLoader.getResource(toscaModelURL).getFile());
289 Path path = Paths.get(file.getPath());
291 if (toscaModelStream == null) return null;
297 * Gets the tosca model.
299 * @param csarInputStream the csar input stream
300 * @return the tosca model
301 * @throws AsdcCatalogException the asdc catalog exception
303 private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
307 csarFile = Files.createTempFile("csar", ".zip");
308 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
309 } catch (IOException e) {
310 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
313 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
315 final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
316 final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
317 final String entryDefinitions = toscaMeta.get("Entry-Definitions");
318 final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
320 final Yaml yaml = new Yaml();
321 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
323 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
325 for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
326 for (Map.Entry<String, Map<String, String>> entry : imports.entrySet()) {
327 final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));
328 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
329 csarBuilder.addVnf(childModel);
333 return csarBuilder.build();
334 } catch (IOException e) {
335 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
342 public static class Builder {
347 private JSONObject catalog = new JSONObject()
348 .put("resources", new JSONObject())
349 .put("services", new JSONObject());
354 private ObjectMapper mapper = new ObjectMapper();
357 * Instantiates a new builder.
365 * @param catalog the catalog
366 * @return the builder
368 public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
369 this.catalog = catalog;
376 * @param mapper the mapper
377 * @return the builder
379 public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
380 this.mapper = mapper;
387 * @return the in local sdc client
389 public org.onap.vid.asdc.local.LocalAsdcClient build() {
390 return new org.onap.vid.asdc.local.LocalAsdcClient(this);