2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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=========================================================
20 * ECOMP and OpenECOMP are trademarks
21 * and service marks of AT&T Intellectual Property.
23 package org.openecomp.modelloader.entity.model;
25 import java.util.List;
27 import javax.ws.rs.core.Response;
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;
37 import com.sun.jersey.api.client.ClientResponse;
39 public class ModelV8Artifact extends AbstractModelArtifact {
40 private static String AAI_CONVERSION_URL = "/aai/tools/modeltransform";
42 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifact.class.getName());
44 private String modelNameVersionId;
45 private ModelArtifact translatedModel;
47 public ModelV8Artifact() {
48 super(ArtifactType.MODEL_V8);
51 public String getModelNameVersionId() {
52 return modelNameVersionId;
55 public void setModelNameVersionId(String modelNameVersionId) {
56 this.modelNameVersionId = modelNameVersionId;
60 public String getUniqueIdentifier() {
61 return getModelNameVersionId();
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.");
74 String translatedPayload = response.getEntity(String.class);
76 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Translated artifact payload:\n" + translatedPayload);
78 ModelArtifactParser parser = new ModelArtifactParser();
80 List<Artifact> parsedArtifacts = parser.parse(translatedPayload.getBytes(), "translated-payload");
81 if (parsedArtifacts == null || parsedArtifacts.isEmpty()) {
85 translatedModel = (ModelArtifact)parsedArtifacts.get(0);
86 return translatedModel.push(aaiClient, config, distId, addedModels);
90 public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
91 if (translatedModel != null) {
92 translatedModel.rollbackModel(aaiClient, config, distId);
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");
103 private String getConversionUrl(ModelLoaderConfig config) {
104 String baseUrl = config.getAaiBaseUrl().trim();
105 String subUrl = AAI_CONVERSION_URL;
107 if ( (!baseUrl.endsWith("/")) && (!subUrl.startsWith("/")) ) {
108 baseUrl = baseUrl + "/";
111 if ( baseUrl.endsWith("/") && subUrl.startsWith("/") ) {
112 baseUrl = baseUrl.substring(0, baseUrl.length()-1);
115 if (!subUrl.endsWith("/")) {
116 subUrl = subUrl + "/";
119 String url = baseUrl + subUrl;