2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.aai.modelloader.entity.model;
23 import java.util.List;
25 import javax.ws.rs.core.Response;
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;
35 import com.sun.jersey.api.client.ClientResponse;
37 public class ModelV8Artifact extends AbstractModelArtifact {
38 private static String AAI_CONVERSION_URL = "/aai/tools/modeltransform";
40 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifact.class.getName());
42 private String modelNameVersionId;
43 private ModelArtifact translatedModel;
45 public ModelV8Artifact() {
46 super(ArtifactType.MODEL_V8);
49 public String getModelNameVersionId() {
50 return modelNameVersionId;
53 public void setModelNameVersionId(String modelNameVersionId) {
54 this.modelNameVersionId = modelNameVersionId;
58 public String getUniqueIdentifier() {
59 return getModelNameVersionId();
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.");
72 String translatedPayload = response.getEntity(String.class);
74 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Translated artifact payload:\n" + translatedPayload);
76 ModelArtifactParser parser = new ModelArtifactParser();
78 List<Artifact> parsedArtifacts = parser.parse(translatedPayload.getBytes(), "translated-payload");
79 if (parsedArtifacts == null || parsedArtifacts.isEmpty()) {
83 translatedModel = (ModelArtifact)parsedArtifacts.get(0);
84 return translatedModel.push(aaiClient, config, distId, addedModels);
88 public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
89 if (translatedModel != null) {
90 translatedModel.rollbackModel(aaiClient, config, distId);
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");
101 private String getConversionUrl(ModelLoaderConfig config) {
102 String baseUrl = config.getAaiBaseUrl().trim();
103 String subUrl = AAI_CONVERSION_URL;
105 if ( (!baseUrl.endsWith("/")) && (!subUrl.startsWith("/")) ) {
106 baseUrl = baseUrl + "/";
109 if ( baseUrl.endsWith("/") && subUrl.startsWith("/") ) {
110 baseUrl = baseUrl.substring(0, baseUrl.length()-1);
113 if (!subUrl.endsWith("/")) {
114 subUrl = subUrl + "/";
117 String url = baseUrl + subUrl;