Renaming openecomp to onap
[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 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.aai.modelloader.entity.model;
25
26 import java.util.List;
27
28 import javax.ws.rs.core.Response;
29
30 import org.onap.aai.modelloader.config.ModelLoaderConfig;
31 import org.onap.aai.modelloader.entity.ArtifactType;
32 import org.onap.aai.modelloader.restclient.AaiRestClient;
33 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
34 import org.onap.aai.cl.api.Logger;
35 import org.onap.aai.cl.eelf.LoggerFactory;
36
37 import com.sun.jersey.api.client.ClientResponse;
38
39 public class NamedQueryArtifact extends AbstractModelArtifact {
40                 
41   private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName());
42   
43         private String namedQueryUuid; 
44         
45         public NamedQueryArtifact() {
46           super(ArtifactType.NAMED_QUERY);
47         }
48         
49         public String getNamedQueryUuid() {
50                 return namedQueryUuid;
51         }
52         
53         public void setNamedQueryUuid(String namedQueryUuid) {
54                 this.namedQueryUuid = namedQueryUuid;
55         }
56         
57   @Override
58   public String getUniqueIdentifier() {
59     return getNamedQueryUuid();
60   }     
61
62   @Override
63   public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) {
64     ClientResponse getResponse  = aaiClient.getResource(getNamedQueryUrl(config), distId, AaiRestClient.MimeType.XML);
65     if ( (getResponse == null) || (getResponse.getStatus() != Response.Status.OK.getStatusCode()) ) {
66       // Only attempt the PUT if the model doesn't already exist
67       ClientResponse putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId, AaiRestClient.MimeType.XML);
68       if ( (putResponse != null) && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode()) ) {
69         addedModels.add(this);
70         logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
71       }
72       else {
73         logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + getUniqueIdentifier() +
74             ". Rolling back distribution.");
75         return false;
76       }
77     }
78     else {
79       logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, 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 = null;
94     String instance = null;
95
96     subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim();
97     instance = this.getNamedQueryUuid();
98
99     if ( (!baseURL.endsWith("/")) && (!subURL.startsWith("/")) ) {
100       baseURL = baseURL + "/";
101     }
102
103     if ( baseURL.endsWith("/") && subURL.startsWith("/") ) {
104       baseURL = baseURL.substring(0, baseURL.length()-1);
105     }
106
107     if (!subURL.endsWith("/")) {
108       subURL = subURL + "/";
109     }
110
111     String url = baseURL + subURL + instance;
112     return url;
113   }
114 }