Renaming openecomp to onap
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / entity / model / ModelV8Artifact.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.Artifact;
32 import org.onap.aai.modelloader.entity.ArtifactType;
33 import org.onap.aai.modelloader.restclient.AaiRestClient;
34 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
35 import org.onap.aai.cl.api.Logger;
36 import org.onap.aai.cl.eelf.LoggerFactory;
37
38 import com.sun.jersey.api.client.ClientResponse;
39
40 public class ModelV8Artifact extends AbstractModelArtifact {
41   private static String AAI_CONVERSION_URL = "/aai/tools/modeltransform";       
42
43   private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifact.class.getName());
44
45   private String modelNameVersionId; 
46         private ModelArtifact translatedModel;
47
48         public ModelV8Artifact() {
49           super(ArtifactType.MODEL_V8);
50         }
51
52         public String getModelNameVersionId() {
53           return modelNameVersionId;
54         }
55         
56         public void setModelNameVersionId(String modelNameVersionId) {
57                 this.modelNameVersionId = modelNameVersionId;
58         }
59         
60   @Override
61   public String getUniqueIdentifier() {
62     return getModelNameVersionId();
63   }     
64         
65   @Override
66   public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) {
67     // For a legacy model (version <= v8), we need to call out to an A&AI endpoint to convert to the proper format
68     ClientResponse response  = aaiClient.postResource(getConversionUrl(config), constructTransformPayload(), distId, AaiRestClient.MimeType.XML);
69     if ( (response == null) || (response.getStatus() != Response.Status.OK.getStatusCode()) ) {
70       logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + 
71           getType().toString() + " " + getModelNameVersionId() + ". Unable to convert model.  Rolling back distribution.");
72       return false;
73     }
74     
75     String translatedPayload = response.getEntity(String.class);
76     
77     logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Translated artifact payload:\n" + translatedPayload);
78     
79     ModelArtifactParser parser = new ModelArtifactParser();
80     
81     List<Artifact> parsedArtifacts = parser.parse(translatedPayload.getBytes(), "translated-payload");
82     if (parsedArtifacts == null || parsedArtifacts.isEmpty()) {
83       return false;
84     } 
85     
86     translatedModel = (ModelArtifact)parsedArtifacts.get(0);
87     return translatedModel.push(aaiClient, config, distId, addedModels);
88   }
89
90   @Override
91   public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
92     if (translatedModel != null) {
93       translatedModel.rollbackModel(aaiClient, config, distId);
94     }
95   }
96   
97
98   private String constructTransformPayload() {
99     // A&AI requires that to transform a legacy model, we need to use the v8 namespace (even 
100     // if the version < 8)
101     return getPayload().replaceFirst("aai.inventory/v.", "aai.inventory/v8");
102   }
103   
104   private String getConversionUrl(ModelLoaderConfig config) {
105     String baseUrl = config.getAaiBaseUrl().trim();
106     String subUrl = AAI_CONVERSION_URL;
107
108     if ( (!baseUrl.endsWith("/")) && (!subUrl.startsWith("/")) ) {
109       baseUrl = baseUrl + "/";
110     }
111
112     if ( baseUrl.endsWith("/") && subUrl.startsWith("/") ) {
113       baseUrl = baseUrl.substring(0, baseUrl.length()-1);
114     }
115
116     if (!subUrl.endsWith("/")) {
117       subUrl = subUrl + "/";
118     }
119
120     String url = baseUrl + subUrl;
121     return url;
122   }
123 }