1e1f3a288702ac9844523106314cc0732fa5ef2d
[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
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 com.sun.jersey.api.client.ClientResponse;
35
36 public class NamedQueryArtifact extends AbstractModelArtifact {
37                 
38   private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
39   
40         private String namedQueryUuid; 
41         
42         public NamedQueryArtifact() {
43           super(ArtifactType.NAMED_QUERY);
44         }
45         
46         public String getNamedQueryUuid() {
47                 return namedQueryUuid;
48         }
49         
50         public void setNamedQueryUuid(String namedQueryUuid) {
51                 this.namedQueryUuid = namedQueryUuid;
52         }
53         
54   @Override
55   public String getUniqueIdentifier() {
56     return getNamedQueryUuid();
57   }     
58
59   @Override
60   public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) {
61     ClientResponse getResponse  = aaiClient.getResource(getNamedQueryUrl(config), distId, AaiRestClient.MimeType.XML);
62     if ( (getResponse == null) || (getResponse.getStatus() != Response.Status.OK.getStatusCode()) ) {
63       // Only attempt the PUT if the model doesn't already exist
64       ClientResponse putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId, AaiRestClient.MimeType.XML);
65       if ( (putResponse != null) && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode()) ) {
66         addedModels.add(this);
67         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
68       }
69       else {
70         logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + getUniqueIdentifier() +
71             ". Rolling back distribution.");
72         return false;
73       }
74     }
75     else {
76       logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " already exists.  Skipping ingestion.");
77     }
78     
79     return true;
80   }
81
82   @Override
83   public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
84     // Best effort to delete.  Nothing we can do in the event this fails.
85     aaiClient.getAndDeleteResource(getNamedQueryUrl(config), distId);
86   }
87
88   private String getNamedQueryUrl(ModelLoaderConfig config) {
89     String baseURL = config.getAaiBaseUrl().trim();
90     String subURL = null;
91     String instance = null;
92
93     subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
94     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     String url = baseURL + subURL + instance;
109     return url;
110   }
111 }