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