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.rest;
\r
23 import java.io.IOException;
\r
24 import java.io.InputStream;
\r
25 import java.io.FileInputStream;
\r
26 import java.io.File;
\r
27 import java.net.URI;
\r
28 import java.nio.file.Files;
\r
29 import java.nio.file.Path;
\r
30 import java.nio.file.StandardCopyOption;
\r
31 import java.text.DateFormat;
\r
32 import java.text.SimpleDateFormat;
\r
33 import java.util.Collection;
\r
34 import java.util.Collections;
\r
35 import java.util.Map;
\r
36 import java.util.Map.Entry;
\r
37 import java.util.UUID;
\r
38 import java.util.zip.ZipFile;
\r
40 import javax.ws.rs.NotFoundException;
\r
41 import javax.ws.rs.ProcessingException;
\r
42 import javax.ws.rs.WebApplicationException;
\r
43 import javax.ws.rs.client.Client;
\r
44 import javax.ws.rs.client.ResponseProcessingException;
\r
45 import javax.ws.rs.client.WebTarget;
\r
46 import javax.ws.rs.core.GenericType;
\r
47 import javax.ws.rs.core.MediaType;
\r
48 import javax.ws.rs.core.MultivaluedHashMap;
\r
50 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
51 import org.openecomp.vid.asdc.AsdcCatalogException;
\r
52 import org.openecomp.vid.asdc.AsdcClient;
\r
53 import org.openecomp.vid.asdc.beans.Artifact;
\r
54 import org.openecomp.vid.asdc.beans.Resource;
\r
55 import org.openecomp.vid.asdc.beans.Service;
\r
56 import org.openecomp.vid.asdc.beans.tosca.ToscaCsar;
\r
57 import org.openecomp.vid.asdc.beans.tosca.ToscaMeta;
\r
58 import org.openecomp.vid.asdc.beans.tosca.ToscaModel;
\r
59 import org.openecomp.vid.model.ModelConstants;
\r
60 import org.yaml.snakeyaml.Yaml;
\r
61 import org.yaml.snakeyaml.error.YAMLException;
\r
63 import org.openecomp.vid.properties.VidProperties;
\r
64 import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
\r
67 * The Class RestfulAsdcClient.
\r
69 public class RestfulAsdcClient implements AsdcClient {
\r
71 /** The Constant LOG. */
\r
72 private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(RestfulAsdcClient.class);
\r
74 /** The Constant dateFormat. */
\r
75 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
\r
78 private final Client client;
\r
81 private final URI uri;
\r
83 /** The common headers. */
\r
84 private final MultivaluedHashMap<String, Object> commonHeaders;
\r
87 private final String auth;
\r
90 * The Class Builder.
\r
92 public static class Builder {
\r
95 private final Client client;
\r
98 private final URI uri;
\r
101 private String auth = null;
\r
104 * Instantiates a new builder.
\r
106 * @param client the client
\r
107 * @param uri the uri
\r
109 public Builder(Client client, URI uri) {
\r
110 this.client = client;
\r
111 this.client.register(JacksonJsonProvider.class);
\r
118 * @param auth the auth
\r
119 * @return the builder
\r
121 public Builder auth(String auth) {
\r
129 * @return the restful asdc client
\r
131 public RestfulAsdcClient build() {
\r
132 return new RestfulAsdcClient(this);
\r
137 * Instantiates a new restful asdc client.
\r
139 * @param builder the builder
\r
141 private RestfulAsdcClient(Builder builder) {
\r
142 client = builder.client;
\r
144 auth = builder.auth;
\r
146 commonHeaders = new MultivaluedHashMap<String, Object> ();
\r
147 commonHeaders.put("X-ECOMP-InstanceID", Collections.singletonList((Object) "VID"));
\r
148 commonHeaders.put("Authorization", Collections.singletonList((Object) (auth)));
\r
154 * @return the client
\r
156 private Client getClient() { return client; }
\r
159 * @see org.openecomp.vid.asdc.AsdcClient#getResource(java.util.UUID)
\r
161 public Resource getResource(UUID uuid) throws AsdcCatalogException {
\r
162 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
\r
166 .path(path + "/" + uuid.toString() + "/metadata")
\r
167 .request(MediaType.APPLICATION_JSON_TYPE)
\r
168 .headers(commonHeaders)
\r
169 .header("Content-Type", MediaType.APPLICATION_JSON)
\r
170 .get(Resource.class);
\r
171 } catch (ResponseProcessingException e) {
\r
172 //Couldn't convert response to Java type
\r
173 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
174 } catch (ProcessingException e) {
\r
175 //IO problems during request
\r
176 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
177 } catch (WebApplicationException e) {
\r
178 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
179 throw new AsdcCatalogException(e);
\r
184 * @see org.openecomp.vid.asdc.AsdcClient#getResources()
\r
186 public Collection<Resource> getResources() throws AsdcCatalogException {
\r
187 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
\r
192 .request(MediaType.APPLICATION_JSON_TYPE)
\r
193 .headers(commonHeaders)
\r
194 .header("Content-Type", MediaType.APPLICATION_JSON)
\r
195 .get(new GenericType<Collection<Resource>> () {});
\r
196 } catch (ResponseProcessingException e) {
\r
197 //Couldn't convert response to Java type
\r
198 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
199 } catch (ProcessingException e) {
\r
200 //IO problems during request
\r
201 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
202 } catch (WebApplicationException e) {
\r
203 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
204 throw new AsdcCatalogException(e);
\r
209 * @see org.openecomp.vid.asdc.AsdcClient#getResources(java.util.Map)
\r
211 public Collection<Resource> getResources(Map<String, String[]> filter) throws AsdcCatalogException {
\r
212 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
\r
213 WebTarget target = getClient()
\r
217 for (Entry<String, String[]> filterEntry : filter.entrySet()) {
\r
218 target = target.queryParam(filterEntry.getKey(), (Object []) filterEntry.getValue());
\r
222 return target.request()
\r
223 .accept(MediaType.APPLICATION_JSON_TYPE)
\r
224 .headers(commonHeaders)
\r
225 .header("Content-Type", MediaType.APPLICATION_JSON)
\r
226 .get(new GenericType<Collection<Resource>> () {});
\r
227 } catch (ResponseProcessingException e) {
\r
228 //Couldn't convert response to Java type
\r
229 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
230 } catch (ProcessingException e) {
\r
231 //IO problems during request
\r
232 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
233 } catch (NotFoundException e) {
\r
235 } catch (WebApplicationException e) {
\r
236 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
237 throw new AsdcCatalogException(e);
\r
242 * @see org.openecomp.vid.asdc.AsdcClient#getResourceArtifact(java.util.UUID, java.util.UUID)
\r
244 public Artifact getResourceArtifact(UUID resourceUuid, UUID artifactUuid) throws AsdcCatalogException {
\r
245 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
\r
249 .path(path + "/" + resourceUuid + "/artifacts/" + artifactUuid)
\r
250 .request(MediaType.APPLICATION_JSON_TYPE)
\r
251 .headers(commonHeaders)
\r
252 .header("Content-Type", MediaType.APPLICATION_JSON)
\r
253 .get(Artifact.class);
\r
254 } catch (ResponseProcessingException e) {
\r
255 //Couldn't convert response to Java type
\r
256 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
257 } catch (ProcessingException e) {
\r
258 //IO problems during request
\r
259 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
260 } catch (WebApplicationException e) {
\r
261 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
262 throw new AsdcCatalogException(e);
\r
267 * @see org.openecomp.vid.asdc.AsdcClient#getService(java.util.UUID)
\r
269 public Service getService(UUID uuid) throws AsdcCatalogException {
\r
271 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
\r
275 .path( path + "/" + uuid.toString() + "/metadata")
\r
276 .request(MediaType.APPLICATION_JSON)
\r
277 .headers(commonHeaders)
\r
278 .get(Service.class);
\r
279 } catch (ResponseProcessingException e) {
\r
280 //Couldn't convert response to Java type
\r
281 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
282 } catch (ProcessingException e) {
\r
283 //IO problems during request
\r
284 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
285 } catch (WebApplicationException e) {
\r
286 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
287 throw new AsdcCatalogException(e);
\r
292 * @see org.openecomp.vid.asdc.AsdcClient#getServices()
\r
294 public Collection<Service> getServices() throws AsdcCatalogException {
\r
295 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
\r
301 .accept(MediaType.APPLICATION_JSON_TYPE)
\r
302 .headers(commonHeaders)
\r
303 .header("Content-Type", MediaType.APPLICATION_JSON)
\r
304 .get(new GenericType<Collection<Service>> () {});
\r
305 } catch (ResponseProcessingException e) {
\r
306 //Couldn't convert response to Java type
\r
307 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
308 } catch (ProcessingException e) {
\r
309 //IO problems during request
\r
310 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
311 } catch (WebApplicationException e) {
\r
312 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
313 throw new AsdcCatalogException(e);
\r
318 * @see org.openecomp.vid.asdc.AsdcClient#getServices(java.util.Map)
\r
320 public Collection<Service> getServices(Map<String, String[]> filter) throws AsdcCatalogException {
\r
322 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
\r
323 WebTarget target = getClient()
\r
328 for (Entry<String, String[]> filterEntry : filter.entrySet()) {
\r
329 target = target.queryParam(filterEntry.getKey(), (Object []) filterEntry.getValue());
\r
333 return target.request()
\r
334 .accept(MediaType.APPLICATION_JSON_TYPE)
\r
335 .headers(commonHeaders)
\r
336 .header("Content-Type", MediaType.APPLICATION_JSON)
\r
337 .get(new GenericType<Collection<Service>> () {});
\r
338 } catch (ResponseProcessingException e) {
\r
339 //Couldn't convert response to Java type
\r
340 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
341 } catch (ProcessingException e) {
\r
342 //IO problems during request
\r
343 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
344 } catch (NotFoundException e) {
\r
346 } catch (WebApplicationException e) {
\r
347 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
348 throw new AsdcCatalogException(e);
\r
353 * @see org.openecomp.vid.asdc.AsdcClient#getServiceArtifact(java.util.UUID, java.util.UUID)
\r
355 public Artifact getServiceArtifact(UUID serviceUuid, UUID artifactUuid) throws AsdcCatalogException {
\r
356 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
\r
360 .path(path + "/" + serviceUuid + "/artifacts/" + artifactUuid)
\r
361 .request(MediaType.APPLICATION_JSON_TYPE)
\r
362 .headers(commonHeaders)
\r
363 .header("Content-Type", MediaType.APPLICATION_JSON)
\r
364 .get(Artifact.class);
\r
365 } catch (ResponseProcessingException e) {
\r
366 //Couldn't convert response to Java type
\r
367 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
368 } catch (ProcessingException e) {
\r
369 //IO problems during request
\r
370 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
371 } catch (WebApplicationException e) {
\r
372 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
373 throw new AsdcCatalogException(e);
\r
378 * @see org.openecomp.vid.asdc.AsdcClient#getResourceToscaModel(java.util.UUID)
\r
380 public ToscaCsar getResourceToscaModel(UUID resourceUuid) throws AsdcCatalogException {
\r
381 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_RESOURCE_API_PATH, ModelConstants.DEFAULT_ASDC_RESOURCE_API_PATH);
\r
382 try (final InputStream csarInputStream = (InputStream) getClient()
\r
384 .path(path + "/" + resourceUuid + "/toscaModel")
\r
385 .request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
\r
386 .headers(commonHeaders)
\r
387 .header("Content-Type", MediaType.APPLICATION_OCTET_STREAM)
\r
388 .get(InputStream.class)) {
\r
390 return getToscaModel(csarInputStream);
\r
391 } catch (IOException e) {
\r
392 throw new AsdcCatalogException("Failed to retrieve resource TOSCA model from ASDC", e);
\r
397 * @see org.openecomp.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
\r
399 public ToscaCsar getServiceToscaModel(UUID serviceUuid) throws AsdcCatalogException {
\r
400 String path = VidProperties.getPropertyWithDefault(ModelConstants.ASDC_SVC_API_PATH, ModelConstants.DEFAULT_ASDC_SVC_API_PATH);
\r
402 final InputStream csarInputStream = (InputStream) getClient()
\r
404 .path(path + "/" + serviceUuid + "/toscaModel")
\r
405 .request(MediaType.APPLICATION_OCTET_STREAM_TYPE)
\r
406 .headers(commonHeaders)
\r
407 .header("Content-Type", MediaType.APPLICATION_OCTET_STREAM)
\r
408 .get(InputStream.class);
\r
410 return getToscaModel(csarInputStream);
\r
411 } catch (ResponseProcessingException e) {
\r
412 //Couldn't convert response to Java type
\r
413 throw new AsdcCatalogException("ASDC response could not be processed", e);
\r
414 } catch (ProcessingException e) {
\r
415 //IO problems during request
\r
416 throw new AsdcCatalogException("Failed to get a response from ASDC service", e);
\r
417 } catch (WebApplicationException e) {
\r
418 //Web service returned data, but the response status wasn't a good one (i.e. non 2xx)
\r
419 throw new AsdcCatalogException(e);
\r
424 * Gets the tosca model.
\r
426 * @param csarInputStream the csar input stream
\r
427 * @return the tosca model
\r
428 * @throws AsdcCatalogException the asdc catalog exception
\r
430 private ToscaCsar getToscaModel(InputStream csarInputStream) throws AsdcCatalogException {
\r
431 final Path csarFile;
\r
433 csarFile = Files.createTempFile("csar", ".zip");
\r
434 Files.copy(csarInputStream, csarFile, StandardCopyOption.REPLACE_EXISTING);
\r
435 } catch (IOException e) {
\r
436 throw new AsdcCatalogException("Caught IOException while creating CSAR", e);
\r
439 try (final ZipFile csar = new ZipFile(csarFile.toFile())) {
\r
441 final InputStream toscaMetaStream = csar.getInputStream(csar.getEntry("TOSCA-Metadata/TOSCA.meta"));
\r
442 final ToscaMeta toscaMeta = new ToscaMeta.Builder(toscaMetaStream).build();
\r
443 final String entryDefinitions = toscaMeta.get("Entry-Definitions");
\r
444 final InputStream toscaParentEntryYamlStream = csar.getInputStream(csar.getEntry(entryDefinitions));
\r
447 final Yaml yaml = new Yaml();
\r
448 final ToscaModel parentModel = yaml.loadAs(toscaParentEntryYamlStream, ToscaModel.class);
\r
450 final ToscaCsar.Builder csarBuilder = new ToscaCsar.Builder(parentModel);
\r
452 for (Map<String, Map<String, String>> imports : parentModel.getImports()) {
\r
453 LOG.debug("imports = " + imports.toString());
\r
454 for (Entry<String, Map<String, String>> entry : imports.entrySet()) {
\r
455 if ( entry.getValue() != null) {
\r
456 String fname = entry.getValue().get("file");
\r
457 if ( ( fname != null ) && (fname.startsWith("service") || fname.startsWith("resource")) ) {
\r
458 LOG.debug("fname = " + fname);
\r
459 final InputStream toscaChildEntryYamlStream = csar.getInputStream(csar.getEntry("Definitions/" + fname ));
\r
460 final ToscaModel childModel = yaml.loadAs(toscaChildEntryYamlStream, ToscaModel.class);
\r
461 csarBuilder.addVnf(childModel);
\r
467 return csarBuilder.build();
\r
468 } catch (YAMLException e) {
\r
469 throw new AsdcCatalogException("Caught exception while processing TOSCA YAML", e);
\r
471 } catch (IOException e) {
\r
472 throw new AsdcCatalogException("Caught IOException while processing CSAR", e);
\r