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