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
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.modelloader.entity.model;
26 import java.util.List;
28 import javax.ws.rs.core.Response;
30 import org.openecomp.cl.api.Logger;
31 import org.openecomp.cl.eelf.LoggerFactory;
32 import org.openecomp.modelloader.config.ModelLoaderConfig;
33 import org.openecomp.modelloader.entity.ArtifactType;
34 import org.openecomp.modelloader.restclient.AaiRestClient;
35 import org.openecomp.modelloader.service.ModelLoaderMsgs;
37 import com.sun.jersey.api.client.ClientResponse;
39 public class NamedQueryArtifact extends AbstractModelArtifact {
41 private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
43 private String namedQueryUuid;
45 public NamedQueryArtifact() {
46 super(ArtifactType.NAMED_QUERY);
49 public String getNamedQueryUuid() {
50 return namedQueryUuid;
53 public void setNamedQueryUuid(String namedQueryUuid) {
54 this.namedQueryUuid = namedQueryUuid;
58 public String getUniqueIdentifier() {
59 return getNamedQueryUuid();
63 public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) {
64 ClientResponse getResponse = aaiClient.getResource(getNamedQueryUrl(config), distId, AaiRestClient.MimeType.XML);
65 if ( (getResponse == null) || (getResponse.getStatus() != Response.Status.OK.getStatusCode()) ) {
66 // Only attempt the PUT if the model doesn't already exist
67 ClientResponse putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId, AaiRestClient.MimeType.XML);
68 if ( (putResponse != null) && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode()) ) {
69 addedModels.add(this);
70 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
73 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + getUniqueIdentifier() +
74 ". Rolling back distribution.");
79 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " already exists. Skipping ingestion.");
86 public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
87 // Best effort to delete. Nothing we can do in the event this fails.
88 aaiClient.getAndDeleteResource(getNamedQueryUrl(config), distId);
91 private String getNamedQueryUrl(ModelLoaderConfig config) {
92 String baseURL = config.getAaiBaseUrl().trim();
94 String instance = null;
96 subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
97 instance = this.getNamedQueryUuid();
99 if ( (!baseURL.endsWith("/")) && (!subURL.startsWith("/")) ) {
100 baseURL = baseURL + "/";
103 if ( baseURL.endsWith("/") && subURL.startsWith("/") ) {
104 baseURL = baseURL.substring(0, baseURL.length()-1);
107 if (!subURL.endsWith("/")) {
108 subURL = subURL + "/";
111 String url = baseURL + subURL + instance;