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;
24 import javax.ws.rs.core.MediaType;
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 org.onap.aai.modelloader.entity.Artifact;
35 import org.onap.aai.restclient.client.OperationResult;
37 public class NamedQueryArtifact extends AbstractModelArtifact {
39 private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
41 private String namedQueryUuid;
43 public NamedQueryArtifact() {
44 super(ArtifactType.NAMED_QUERY);
47 public String getNamedQueryUuid() {
48 return namedQueryUuid;
51 public void setNamedQueryUuid(String namedQueryUuid) {
52 this.namedQueryUuid = namedQueryUuid;
56 public String getUniqueIdentifier() {
57 return getNamedQueryUuid();
61 public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<Artifact> completedArtifacts) {
62 OperationResult getResponse =
63 aaiClient.getResource(getNamedQueryUrl(config), distId, MediaType.APPLICATION_XML_TYPE);
64 if ((getResponse == null) || (getResponse.getResultCode() != Response.Status.OK.getStatusCode())) {
65 // Only attempt the PUT if the model doesn't already exist
66 OperationResult putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId,
67 MediaType.APPLICATION_XML_TYPE);
68 if ((putResponse != null) && (putResponse.getResultCode() == Response.Status.CREATED.getStatusCode())) {
69 completedArtifacts.add(this);
70 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
71 getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
73 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString()
74 + " " + getUniqueIdentifier() + ". Rolling back distribution.");
78 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
79 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();
93 String subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
94 String 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 return baseURL + subURL + instance;