2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 European Software Marketing Ltd.
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;
26 import org.onap.aai.cl.api.Logger;
27 import org.onap.aai.cl.eelf.LoggerFactory;
28 import org.onap.aai.modelloader.config.ModelLoaderConfig;
29 import org.onap.aai.modelloader.entity.Artifact;
30 import org.onap.aai.modelloader.entity.ArtifactType;
31 import org.onap.aai.modelloader.restclient.AaiRestClient;
32 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
33 import org.onap.aai.restclient.client.OperationResult;
35 public class NamedQueryArtifact extends AbstractModelArtifact {
37 private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
39 private String namedQueryUuid;
41 public NamedQueryArtifact() {
42 super(ArtifactType.NAMED_QUERY);
45 public String getNamedQueryUuid() {
46 return namedQueryUuid;
49 public void setNamedQueryUuid(String namedQueryUuid) {
50 this.namedQueryUuid = namedQueryUuid;
54 public String getUniqueIdentifier() {
55 return getNamedQueryUuid();
59 public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId,
60 List<Artifact> completedArtifacts) {
61 OperationResult getResponse =
62 aaiClient.getResource(getNamedQueryUrl(config), distId, MediaType.APPLICATION_XML_TYPE);
63 if ((getResponse == null) || (getResponse.getResultCode() != Response.Status.OK.getStatusCode())) {
64 // Only attempt the PUT if the model doesn't already exist
65 OperationResult putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId,
66 MediaType.APPLICATION_XML_TYPE);
67 if ((putResponse != null) && (putResponse.getResultCode() == Response.Status.CREATED.getStatusCode())) {
68 completedArtifacts.add(this);
69 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
70 getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
72 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString()
73 + " " + getUniqueIdentifier() + ". Rolling back distribution.");
77 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
78 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();
92 String subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
93 String instance = this.getNamedQueryUuid();
95 if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {
96 baseURL = baseURL + "/";
99 if (baseURL.endsWith("/") && subURL.startsWith("/")) {
100 baseURL = baseURL.substring(0, baseURL.length() - 1);
103 if (!subURL.endsWith("/")) {
104 subURL = subURL + "/";
107 return baseURL + subURL + instance;