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