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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.aai.modelloader.entity.model;
26 import java.util.List;
28 import javax.ws.rs.core.Response;
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;
38 import com.sun.jersey.api.client.ClientResponse;
40 public class ModelV8Artifact extends AbstractModelArtifact {
41 private static String AAI_CONVERSION_URL = "/aai/tools/modeltransform";
43 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifact.class.getName());
45 private String modelNameVersionId;
46 private ModelArtifact translatedModel;
48 public ModelV8Artifact() {
49 super(ArtifactType.MODEL_V8);
52 public String getModelNameVersionId() {
53 return modelNameVersionId;
56 public void setModelNameVersionId(String modelNameVersionId) {
57 this.modelNameVersionId = modelNameVersionId;
61 public String getUniqueIdentifier() {
62 return getModelNameVersionId();
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.");
75 String translatedPayload = response.getEntity(String.class);
77 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Translated artifact payload:\n" + translatedPayload);
79 ModelArtifactParser parser = new ModelArtifactParser();
81 List<Artifact> parsedArtifacts = parser.parse(translatedPayload.getBytes(), "translated-payload");
82 if (parsedArtifacts == null || parsedArtifacts.isEmpty()) {
86 translatedModel = (ModelArtifact)parsedArtifacts.get(0);
87 return translatedModel.push(aaiClient, config, distId, addedModels);
91 public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
92 if (translatedModel != null) {
93 translatedModel.rollbackModel(aaiClient, config, distId);
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");
104 private String getConversionUrl(ModelLoaderConfig config) {
105 String baseUrl = config.getAaiBaseUrl().trim();
106 String subUrl = AAI_CONVERSION_URL;
108 if ( (!baseUrl.endsWith("/")) && (!subUrl.startsWith("/")) ) {
109 baseUrl = baseUrl + "/";
112 if ( baseUrl.endsWith("/") && subUrl.startsWith("/") ) {
113 baseUrl = baseUrl.substring(0, baseUrl.length()-1);
116 if (!subUrl.endsWith("/")) {
117 subUrl = subUrl + "/";
120 String url = baseUrl + subUrl;