2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP and OpenECOMP are trademarks
21 * and service marks of AT&T Intellectual Property.
23 package org.openecomp.modelloader.entity.model;
25 import java.util.List;
27 import javax.ws.rs.core.Response;
29 import org.openecomp.cl.api.Logger;
30 import org.openecomp.cl.eelf.LoggerFactory;
31 import org.openecomp.modelloader.config.ModelLoaderConfig;
32 import org.openecomp.modelloader.entity.ArtifactType;
33 import org.openecomp.modelloader.restclient.AaiRestClient;
34 import org.openecomp.modelloader.service.ModelLoaderMsgs;
36 import com.sun.jersey.api.client.ClientResponse;
38 public class NamedQueryArtifact extends AbstractModelArtifact {
40 private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
42 private String namedQueryUuid;
44 public NamedQueryArtifact() {
45 super(ArtifactType.NAMED_QUERY);
48 public String getNamedQueryUuid() {
49 return namedQueryUuid;
52 public void setNamedQueryUuid(String namedQueryUuid) {
53 this.namedQueryUuid = namedQueryUuid;
57 public String getUniqueIdentifier() {
58 return getNamedQueryUuid();
62 public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) {
63 ClientResponse getResponse = aaiClient.getResource(getNamedQueryUrl(config), distId, AaiRestClient.MimeType.XML);
64 if ( (getResponse == null) || (getResponse.getStatus() != Response.Status.OK.getStatusCode()) ) {
65 // Only attempt the PUT if the model doesn't already exist
66 ClientResponse putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId, AaiRestClient.MimeType.XML);
67 if ( (putResponse != null) && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode()) ) {
68 addedModels.add(this);
69 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
72 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + getUniqueIdentifier() +
73 ". Rolling back distribution.");
78 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " already exists. Skipping ingestion.");
85 public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
86 // Best effort to delete. Nothing we can do in the event this fails.
87 aaiClient.getAndDeleteResource(getNamedQueryUrl(config), distId);
90 private String getNamedQueryUrl(ModelLoaderConfig config) {
91 String baseURL = config.getAaiBaseUrl().trim();
93 String instance = null;
95 subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
96 instance = this.getNamedQueryUuid();
98 if ( (!baseURL.endsWith("/")) && (!subURL.startsWith("/")) ) {
99 baseURL = baseURL + "/";
102 if ( baseURL.endsWith("/") && subURL.startsWith("/") ) {
103 baseURL = baseURL.substring(0, baseURL.length()-1);
106 if (!subURL.endsWith("/")) {
107 subURL = subURL + "/";
110 String url = baseURL + subURL + instance;