Fix Sonar code smells
[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
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
31 import org.onap.aai.modelloader.entity.Artifact;
32 import org.onap.aai.restclient.client.OperationResult;
33
34
35 public class NamedQueryArtifact extends AbstractModelArtifact {
36
37     private String namedQueryUuid;
38
39     public NamedQueryArtifact() {
40         super(ArtifactType.NAMED_QUERY);
41     }
42
43     public String getNamedQueryUuid() {
44         return namedQueryUuid;
45     }
46
47     public void setNamedQueryUuid(String namedQueryUuid) {
48         this.namedQueryUuid = namedQueryUuid;
49     }
50
51     @Override
52     public String getUniqueIdentifier() {
53         return getNamedQueryUuid();
54     }
55
56     @Override
57     public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<Artifact> completedArtifacts) {
58         if (config.useGizmo()) {
59             return pushToGizmo(aaiClient, config, distId);
60         }
61
62         return pushToResources(aaiClient, config, distId, completedArtifacts);
63     }
64
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.");
76             } else {
77                 logErrorMsg("Ingestion failed for " + getType().toString()
78                         + " " + getUniqueIdentifier() + ". Rolling back distribution.");
79                 return false;
80             }
81         } else {
82             logInfoMsg(getType().toString() + " " + getUniqueIdentifier() + " already exists.  Skipping ingestion.");
83         }
84
85         return true;
86     }
87
88     @Override
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()) {
93             return;
94         }
95         
96         // Best effort to delete. Nothing we can do in the event this fails.
97         aaiClient.getAndDeleteResource(getNamedQueryUrl(config), distId);
98     }
99
100     private String getNamedQueryUrl(ModelLoaderConfig config) {
101         String baseURL = config.getAaiBaseUrl().trim();
102         String subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
103         String instance = this.getNamedQueryUuid();
104
105         if (!baseURL.endsWith("/") && !subURL.startsWith("/")) {
106             baseURL = baseURL + "/";
107         }
108
109         if (baseURL.endsWith("/") && subURL.startsWith("/")) {
110             baseURL = baseURL.substring(0, baseURL.length() - 1);
111         }
112
113         if (!subURL.endsWith("/")) {
114             subURL = subURL + "/";
115         }
116
117         return baseURL + subURL + instance;
118     }
119 }