2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.vid.asdc.memory;
\r
23 import java.io.IOException;
\r
24 import java.io.InputStream;
\r
25 import java.nio.file.Files;
\r
26 import java.nio.file.Path;
\r
27 import java.nio.file.StandardCopyOption;
\r
28 import java.util.Collection;
\r
29 import java.util.LinkedList;
\r
30 import java.util.Map;
\r
31 import java.util.Map.Entry;
\r
32 import java.util.UUID;
\r
33 import java.util.zip.ZipFile;
\r
35 import org.codehaus.jackson.JsonParseException;
\r
36 import org.codehaus.jackson.map.JsonMappingException;
\r
37 import org.codehaus.jackson.map.ObjectMapper;
\r
38 import org.json.JSONArray;
\r
39 import org.json.JSONObject;
\r
40 import org.yaml.snakeyaml.Yaml;
\r
42 import org.openecomp.vid.asdc.AsdcCatalogException;
\r
43 import org.openecomp.vid.asdc.AsdcClient;
\r
44 import org.openecomp.vid.asdc.beans.Artifact;
\r
45 import org.openecomp.vid.asdc.beans.Resource;
\r
46 import org.openecomp.vid.asdc.beans.Service;
\r
47 import org.openecomp.vid.asdc.beans.tosca.ToscaCsar;
\r
48 import org.openecomp.vid.asdc.beans.tosca.ToscaMeta;
\r
49 import org.openecomp.vid.asdc.beans.tosca.ToscaModel;
\r
52 * The Class InMemoryAsdcClient.
\r
54 public class InMemoryAsdcClient implements AsdcClient {
\r
57 private final JSONObject catalog;
\r
60 private final ObjectMapper mapper;
\r
63 * The Class Builder.
\r
65 public static class Builder {
\r
68 private JSONObject catalog = new JSONObject()
\r
69 .put("resources", new JSONObject())
\r
70 .put("services", new JSONObject());
\r
73 private ObjectMapper mapper = new ObjectMapper();
\r
76 * Instantiates a new builder.
\r
83 * @param catalog the catalog
\r
84 * @return the builder
\r
86 public Builder catalog(JSONObject catalog) {
\r
87 this.catalog = catalog;
\r
94 * @param mapper the mapper
\r
95 * @return the builder
\r
97 public Builder mapper(ObjectMapper mapper) {
\r
98 this.mapper = mapper;
\r
105 * @return the in memory sdc client
\r
107 public InMemoryAsdcClient build() {
\r
108 return new InMemoryAsdcClient(this);
\r
113 * Instantiates a new in memory sdc client.
\r
115 * @param builder the builder
\r
117 InMemoryAsdcClient(Builder builder) {
\r
118 catalog = builder.catalog;
\r
119 mapper = builder.mapper;
\r
123 * Gets the catalog.
\r
125 * @return the catalog
\r
127 private JSONObject getCatalog() {
\r
134 * @return the mapper
\r
136 private ObjectMapper getMapper() {
\r
143 * @param <T> the generic type
\r
144 * @param json the json
\r
145 * @param clazz the clazz
\r
147 * @throws AsdcCatalogException the sdc catalog exception
\r
149 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
\r
151 return getMapper().readValue(json.toString(), clazz);
\r
152 } catch (JsonParseException e) {
\r
153 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
\r
154 } catch (JsonMappingException e) {
\r
155 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
\r
156 } catch (IOException e) {
\r
157 throw new AsdcCatalogException("Failed to get a response from SDC", e);
\r
162 * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
\r
164 public Resource getResource(UUID uuid) throws AsdcCatalogException {
\r
165 final JSONObject resource = getCatalog().getJSONObject("resources")
\r
166 .getJSONObject(uuid.toString());
\r
167 return convert(resource, Resource.class);
\r
172 public Collection<Resource> getResources() throws AsdcCatalogException {
\r
173 final Collection<Resource> resources = new LinkedList<Resource> ();
\r
175 for (String key : getCatalog().getJSONObject("resources").keySet()) {
\r
176 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
\r
177 final Resource resource = convert(json, Resource.class);
\r
178 resources.add(resource);
\r
185 * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
\r
187 public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
\r
188 final Collection<Resource> resources = new LinkedList<Resource> ();
\r
190 for (String key : getCatalog().getJSONObject("resources").keySet()) {
\r
191 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
\r
193 boolean filterMatch = true;
\r
195 for (Entry<String, String[]> entry : filter.entrySet()) {
\r
196 for (int i = 0; i < entry.getValue().length; i++) {
\r
197 if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
\r
198 filterMatch = false;
\r
204 if (filterMatch) resources.add(convert(json, Resource.class));
\r
211 * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
\r
213 public Service getService(UUID uuid) throws AsdcCatalogException {
\r
214 final JSONObject service = getCatalog().getJSONObject("services")
\r
215 .getJSONObject(uuid.toString());
\r
216 return convert(service, Service.class);
\r
220 * @see org.openecomp.vid.asdc.AsdcClient#getServices()
\r
222 public Collection<Service> getServices() throws AsdcCatalogException {
\r
223 final Collection<Service> services = new LinkedList<Service> ();
\r
225 for (String key : getCatalog().getJSONObject("services").keySet()) {
\r
226 final JSONObject json = getCatalog().getJSONObject("services").getJSONObject(key);
\r
227 final Service service = convert(json, Service.class);
\r
228 services.add(service);
\r
235 * @see org.openecompt.vid.asdc.AsdcClient#getServices(java.util.Map)
\r
237 public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
\r
238 final Collection<Service> services = new LinkedList<Service> ();
\r
240 for (String key : getCatalog().getJSONObject("services").keySet()) {
\r
241 final JSONObject json = getCatalog().getJSONObject("services").getJSONObject(key);
\r
243 boolean filterMatch = true;
\r
245 for (Entry<String, String[]> entry : filter.entrySet()) {
\r
246 for (int i = 0; i < entry.getValue().length; i++) {
\r
247 if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
\r
248 filterMatch = false;
\r
254 if (filterMatch) services.add(convert(json, Service.class));
\r
261 * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
\r
263 public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
\r
264 final JSONArray artifacts = getCatalog().getJSONObject("resources")
\r
265 .getJSONObject(resourceUuid.toString())
\r
266 .getJSONArray("artifacts");
\r
268 for (int i = 0; i < artifacts.length(); i++) {
\r
269 final JSONObject artifact = artifacts.getJSONObject(i);
\r
271 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
\r
272 return convert(artifact, Artifact.class);
\r
280 * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
\r
282 public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
\r
283 final JSONArray artifacts = getCatalog().getJSONObject("services")
\r
284 .getJSONObject(serviceUuid.toString())
\r
285 .getJSONArray("artifacts");
\r
287 for (int i = 0; i < artifacts.length(); i++) {
\r
288 final JSONObject artifact = artifacts.getJSONObject(i);
\r
290 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
\r
291 return convert(artifact, Artifact.class);
\r
299 * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
\r
301 public Path getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
\r
302 final String toscaModelURL = getCatalog().getJSONObject("resources")
\r
303 .getJSONObject(resourceUuid.toString())
\r
304 .getString("toscaModelURL");
\r
307 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
\r
309 if (toscaModelStream == null) return null;
\r
311 return null;//getToscaModel(toscaModelStream);
\r
315 * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
\r
317 public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
\r
318 final String toscaModelURL = getCatalog().getJSONObject("services")
\r
319 .getJSONObject(serviceUuid.toString())
\r
320 .getString("toscaModelURL");
\r
322 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
\r
324 if (toscaModelStream == null) return null;
\r
326 return null;//getToscaModel(toscaModelStream);
\r
330 * Gets the tosca model.
\r
332 * @param csarInputStream the csar input stream
\r
333 * @return the tosca model
\r
334 * @throws AsdcCatalogException the asdc catalog exception
\r
336 private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
\r
337 final Path csarFile;
\r
340 csarFile = Files.createTempFile("csar", ".zip");
\r
341 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
\r
342 } catch (IOException e) {
\r
343 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
\r
346 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
\r
348 final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
\r
349 final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
\r
350 final String entryDefinitions = toscaMeta.get("Entry-Definitions");
\r
351 final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
\r
353 final Yaml yaml = new Yaml();
\r
354 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
\r
356 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
\r
358 for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
\r
359 for (Entry<String, Map<String, String>> entry : imports.entrySet()) {
\r
360 final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));
\r
361 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
\r
362 csarBuilder.addVnf(childModel);
\r
366 return csarBuilder.build();
\r
367 } catch (IOException e) {
\r
368 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
\r