2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ===================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
21 package org.onap.aai.modelloader.entity.model;
23 import java.util.List;
25 import javax.ws.rs.core.Response;
27 import org.onap.aai.modelloader.config.ModelLoaderConfig;
28 import org.onap.aai.modelloader.entity.ArtifactType;
29 import org.onap.aai.modelloader.restclient.AaiRestClient;
30 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
34 import com.sun.jersey.api.client.ClientResponse;
36 public class NamedQueryArtifact extends AbstractModelArtifact {
38 private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
40 private String namedQueryUuid;
42 public NamedQueryArtifact() {
43 super(ArtifactType.NAMED_QUERY);
46 public String getNamedQueryUuid() {
47 return namedQueryUuid;
50 public void setNamedQueryUuid(String namedQueryUuid) {
51 this.namedQueryUuid = namedQueryUuid;
55 public String getUniqueIdentifier() {
56 return getNamedQueryUuid();
60 public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) {
61 ClientResponse getResponse = aaiClient.getResource(getNamedQueryUrl(config), distId, AaiRestClient.MimeType.XML);
62 if ( (getResponse == null) || (getResponse.getStatus() != Response.Status.OK.getStatusCode()) ) {
63 // Only attempt the PUT if the model doesn't already exist
64 ClientResponse putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId, AaiRestClient.MimeType.XML);
65 if ( (putResponse != null) && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode()) ) {
66 addedModels.add(this);
67 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
70 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + getUniqueIdentifier() +
71 ". Rolling back distribution.");
76 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " already exists. Skipping ingestion.");
83 public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
84 // Best effort to delete. Nothing we can do in the event this fails.
85 aaiClient.getAndDeleteResource(getNamedQueryUrl(config), distId);
88 private String getNamedQueryUrl(ModelLoaderConfig config) {
89 String baseURL = config.getAaiBaseUrl().trim();
91 String instance = null;
93 subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
94 instance = this.getNamedQueryUuid();
96 if ( (!baseURL.endsWith("/")) && (!subURL.startsWith("/")) ) {
97 baseURL = baseURL + "/";
100 if ( baseURL.endsWith("/") && subURL.startsWith("/") ) {
101 baseURL = baseURL.substring(0, baseURL.length()-1);
104 if (!subURL.endsWith("/")) {
105 subURL = subURL + "/";
108 String url = baseURL + subURL + instance;