668a75126354e7e2510026b3ab63c3962d241a71
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / entity / model / NamedQueryArtifact.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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============================================
20  */
21 package org.onap.aai.modelloader.entity.model;
22
23 import java.util.List;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
26
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;
33
34 import org.onap.aai.modelloader.entity.Artifact;
35 import org.onap.aai.restclient.client.OperationResult;
36
37 public class NamedQueryArtifact extends AbstractModelArtifact {
38
39     private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
40
41     private String namedQueryUuid;
42
43     public NamedQueryArtifact() {
44         super(ArtifactType.NAMED_QUERY);
45     }
46
47     public String getNamedQueryUuid() {
48         return namedQueryUuid;
49     }
50
51     public void setNamedQueryUuid(String namedQueryUuid) {
52         this.namedQueryUuid = namedQueryUuid;
53     }
54
55     @Override
56     public String getUniqueIdentifier() {
57         return getNamedQueryUuid();
58     }
59
60     @Override
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.");
72             } else {
73                 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString()
74                         + " " + getUniqueIdentifier() + ". Rolling back distribution.");
75                 return false;
76             }
77         } else {
78             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
79                     getType().toString() + " " + getUniqueIdentifier() + " already exists.  Skipping ingestion.");
80         }
81
82         return true;
83     }
84
85     @Override
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);
89     }
90
91     private String getNamedQueryUrl(ModelLoaderConfig config) {
92         String baseURL = config.getAaiBaseUrl().trim();
93         String subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
94         String instance = this.getNamedQueryUuid();
95
96         if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {
97             baseURL = baseURL + "/";
98         }
99
100         if (baseURL.endsWith("/") && subURL.startsWith("/")) {
101             baseURL = baseURL.substring(0, baseURL.length() - 1);
102         }
103
104         if (!subURL.endsWith("/")) {
105             subURL = subURL + "/";
106         }
107
108         return baseURL + subURL + instance;
109     }
110 }