1 package org.openecomp.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.openecomp.vid.asdc.AsdcCatalogException;
9 import org.openecomp.vid.asdc.AsdcClient;
10 import org.openecomp.vid.asdc.beans.Artifact;
11 import org.openecomp.vid.asdc.beans.Resource;
12 import org.openecomp.vid.asdc.beans.Service;
13 import org.openecomp.vid.asdc.beans.tosca.ToscaCsar;
14 import org.openecomp.vid.asdc.beans.tosca.ToscaMeta;
15 import org.openecomp.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;
26 import java.util.zip.ZipFile;
29 * The Class LocalAsdcClient.
31 public class LocalAsdcClient implements AsdcClient {
37 private final JSONObject catalog;
42 private final ObjectMapper mapper;
47 public static class Builder {
52 private JSONObject catalog = new JSONObject()
53 .put("resources", new JSONObject())
54 .put("services", new JSONObject());
59 private ObjectMapper mapper = new ObjectMapper();
62 * Instantiates a new builder.
70 * @param catalog the catalog
73 public org.openecomp.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
74 this.catalog = catalog;
81 * @param mapper the mapper
84 public org.openecomp.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
92 * @return the in local sdc client
94 public org.openecomp.vid.asdc.local.LocalAsdcClient build() {
95 return new org.openecomp.vid.asdc.local.LocalAsdcClient(this);
100 * Instantiates a new in local sdc client.
102 * @param builder the builder
104 private LocalAsdcClient(org.openecomp.vid.asdc.local.LocalAsdcClient.Builder builder) {
105 catalog = builder.catalog;
106 mapper = builder.mapper;
112 * @return the catalog
114 private JSONObject getCatalog() {
123 private ObjectMapper getMapper() {
130 * @param <T> the generic type
131 * @param json the json
132 * @param clazz the clazz
134 * @throws AsdcCatalogException the sdc catalog exception
136 private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
138 return getMapper().readValue(json.toString(), clazz);
139 } catch (JsonParseException e) {
140 throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
141 } catch (JsonMappingException e) {
142 throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
143 } catch (IOException e) {
144 throw new AsdcCatalogException("Failed to get a response from SDC", e);
149 * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
151 public Resource getResource(UUID uuid) throws AsdcCatalogException {
152 final JSONObject resource = getCatalog().getJSONObject("resources")
153 .getJSONObject(uuid.toString());
154 return convert(resource, Resource.class);
158 * @see org.openecomp.vid.asdc.AsdcClient#getResources()
160 public Collection<Resource> getResources() throws AsdcCatalogException {
161 final Collection<Resource> resources = new LinkedList<Resource>();
163 for (String key : getCatalog().getJSONObject("resources").keySet()) {
164 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
165 final Resource resource = convert(json, Resource.class);
166 resources.add(resource);
173 * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
175 public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
176 final Collection<Resource> resources = new LinkedList<Resource>();
178 for (String key : getCatalog().getJSONObject("resources").keySet()) {
179 final JSONObject json = getCatalog().getJSONObject("resources").getJSONObject(key);
181 boolean filterMatch = true;
183 for (Map.Entry<String, String[]> entry : filter.entrySet()) {
184 for (int i = 0; i < entry.getValue().length; i++) {
185 if (!json.getString(entry.getKey()).equals(entry.getValue()[i])) {
192 if (filterMatch) resources.add(convert(json, Resource.class));
199 * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
201 public Service getService(UUID uuid) throws AsdcCatalogException {
203 JSONObject serviceJsonObject = null;
204 final JSONArray categoryJsonArray = getCatalog().getJSONArray("services");
206 for (int i = 0; i < categoryJsonArray.length() ; i++) {
207 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
208 if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
209 serviceJsonObject = jsonServiceObject;
214 if (serviceJsonObject != null)
215 return convert(serviceJsonObject, Service.class);
220 * @see org.openecomp.vid.asdc.AsdcClient#getServices()
222 public Collection<Service> getServices() throws AsdcCatalogException {
223 final Collection<Service> services = new LinkedList<Service>();
225 JSONArray servicesArr = getCatalog().getJSONArray("services");
227 for (Object objService : servicesArr) {
228 JSONObject jsonServiceItem = (JSONObject) objService;
229 final Service service = convert(jsonServiceItem, Service.class);
230 services.add(service);
237 * @see org.openecompt.vid.asdc.AsdcClient#getServices(java.util.Map)
239 public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
240 final Collection<Service> services = new LinkedList<Service>();
242 JSONArray catalogServices = catalog.getJSONArray("services");
244 for (int i = 0; i < catalogServices.length(); i++) {
246 JSONObject serviceJson = catalogServices.getJSONObject(i);
248 boolean filterMatch = true;
250 for (Map.Entry<String, String[]> entry : filter.entrySet()) {
251 for (int j = 0; j < entry.getValue().length; j++) {
252 if (!serviceJson.getString(entry.getKey()).equals(entry.getValue()[j])) {
258 if (filterMatch) services.add(convert(serviceJson, Service.class));
264 * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
266 public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
267 final JSONArray artifacts = getCatalog().getJSONObject("resources")
268 .getJSONObject(resourceUuid.toString())
269 .getJSONArray("artifacts");
271 for (int i = 0; i < artifacts.length(); i++) {
272 final JSONObject artifact = artifacts.getJSONObject(i);
274 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
275 return convert(artifact, Artifact.class);
283 * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
285 public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
286 final JSONArray artifacts = getCatalog().getJSONObject("services")
287 .getJSONObject(serviceUuid.toString())
288 .getJSONArray("artifacts");
290 for (int i = 0; i < artifacts.length(); i++) {
291 final JSONObject artifact = artifacts.getJSONObject(i);
293 if (artifact.getString("artifactUUID").equals(artifactUuid.toString())) {
294 return convert(artifact, Artifact.class);
302 * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
304 public Path getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
305 final String toscaModelURL = getCatalog().getJSONObject("resources")
306 .getJSONObject(resourceUuid.toString())
307 .getString("toscaModelURL");
310 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
312 if (toscaModelStream == null) return null;
314 return null;//getToscaModel(toscaModelStream);
318 * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
320 public Path getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
322 String toscaModelURL = null;
324 final JSONArray categoryJsonArray = getCatalog().getJSONArray("services");
326 for (int i = 0; i < categoryJsonArray.length() ; i++) {
328 JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
329 if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
330 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
335 if (toscaModelURL==null){
339 final InputStream toscaModelStream = getClass().getClassLoader().getResourceAsStream(toscaModelURL);
341 ClassLoader classLoader = getClass().getClassLoader();
342 File file = new File(classLoader.getResource(toscaModelURL).getFile());
343 Path path = Paths.get(file.getPath());
345 if (toscaModelStream == null) return null;
351 * Gets the tosca model.
353 * @param csarInputStream the csar input stream
354 * @return the tosca model
355 * @throws AsdcCatalogException the asdc catalog exception
357 private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
361 csarFile = Files.createTempFile("csar", ".zip");
362 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
363 } catch (IOException e) {
364 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
367 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
369 final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
370 final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
371 final String entryDefinitions = toscaMeta.get("Entry-Definitions");
372 final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
374 final Yaml yaml = new Yaml();
375 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
377 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
379 for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
380 for (Map.Entry<String, Map<String, String>> entry : imports.entrySet()) {
381 final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + entry.getValue().get("file")));
382 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
383 csarBuilder.addVnf(childModel);
387 return csarBuilder.build();
388 } catch (IOException e) {
389 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);