Convert project from AJSC to Spring Boot
[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 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
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 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;
34
35 public class NamedQueryArtifact extends AbstractModelArtifact {
36
37     private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
38
39     private String namedQueryUuid;
40
41     public NamedQueryArtifact() {
42         super(ArtifactType.NAMED_QUERY);
43     }
44
45     public String getNamedQueryUuid() {
46         return namedQueryUuid;
47     }
48
49     public void setNamedQueryUuid(String namedQueryUuid) {
50         this.namedQueryUuid = namedQueryUuid;
51     }
52
53     @Override
54     public String getUniqueIdentifier() {
55         return getNamedQueryUuid();
56     }
57
58     @Override
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.");
71             } else {
72                 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString()
73                         + " " + getUniqueIdentifier() + ". Rolling back distribution.");
74                 return false;
75             }
76         } else {
77             logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
78                     getType().toString() + " " + getUniqueIdentifier() + " already exists.  Skipping ingestion.");
79         }
80
81         return true;
82     }
83
84     @Override
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);
88     }
89
90     private String getNamedQueryUrl(ModelLoaderConfig config) {
91         String baseURL = config.getAaiBaseUrl().trim();
92         String subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
93         String instance = this.getNamedQueryUuid();
94
95         if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) {
96             baseURL = baseURL + "/";
97         }
98
99         if (baseURL.endsWith("/") && subURL.startsWith("/")) {
100             baseURL = baseURL.substring(0, baseURL.length() - 1);
101         }
102
103         if (!subURL.endsWith("/")) {
104             subURL = subURL + "/";
105         }
106
107         return baseURL + subURL + instance;
108     }
109 }