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;
27 import org.onap.aai.modelloader.config.ModelLoaderConfig;
28 import org.onap.aai.modelloader.entity.ArtifactType;
29 import org.onap.aai.modelloader.restclient.AaiRestClient;
31 import org.onap.aai.modelloader.entity.Artifact;
32 import org.onap.aai.restclient.client.OperationResult;
35 public class NamedQueryArtifact extends AbstractModelArtifact {
37 private String namedQueryUuid;
39 public NamedQueryArtifact() {
40 super(ArtifactType.NAMED_QUERY);
43 public String getNamedQueryUuid() {
44 return namedQueryUuid;
47 public void setNamedQueryUuid(String namedQueryUuid) {
48 this.namedQueryUuid = namedQueryUuid;
52 public String getUniqueIdentifier() {
53 return getNamedQueryUuid();
57 public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<Artifact> completedArtifacts) {
58 if (config.useGizmo()) {
59 return pushToGizmo(aaiClient, config, distId);
62 return pushToResources(aaiClient, config, distId, completedArtifacts);
65 private boolean pushToResources(AaiRestClient aaiClient, ModelLoaderConfig config, String distId,
66 List<Artifact> completedArtifacts) {
67 OperationResult getResponse =
68 aaiClient.getResource(getNamedQueryUrl(config), distId, MediaType.APPLICATION_XML_TYPE);
69 if (getResponse == null || getResponse.getResultCode() != Response.Status.OK.getStatusCode()) {
70 // Only attempt the PUT if the model doesn't already exist
71 OperationResult putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId,
72 MediaType.APPLICATION_XML_TYPE);
73 if (putResponse != null && putResponse.getResultCode() == Response.Status.CREATED.getStatusCode()) {
74 completedArtifacts.add(this);
75 logInfoMsg(getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
77 logErrorMsg("Ingestion failed for " + getType().toString()
78 + " " + getUniqueIdentifier() + ". Rolling back distribution.");
82 logInfoMsg(getType().toString() + " " + getUniqueIdentifier() + " already exists. Skipping ingestion.");
89 public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
90 // Gizmo is resilient and doesn't require a rollback. A redistribution will work fine even if
91 // the model is partially loaded.
92 if (config.useGizmo()) {
96 // Best effort to delete. Nothing we can do in the event this fails.
97 aaiClient.getAndDeleteResource(getNamedQueryUrl(config), distId);
100 private String getNamedQueryUrl(ModelLoaderConfig config) {
101 String baseURL = config.getAaiBaseUrl().trim();
102 String subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
103 String instance = this.getNamedQueryUuid();
105 if (!baseURL.endsWith("/") && !subURL.startsWith("/")) {
106 baseURL = baseURL + "/";
109 if (baseURL.endsWith("/") && subURL.startsWith("/")) {
110 baseURL = baseURL.substring(0, baseURL.length() - 1);
113 if (!subURL.endsWith("/")) {
114 subURL = subURL + "/";
117 return baseURL + subURL + instance;