--- /dev/null
+/**\r
+ * ============LICENSE_START=======================================================\r
+ * org.onap.aai\r
+ * ================================================================================\r
+ * Copyright © 2017 AT&T Intellectual Property.\r
+ * Copyright © 2017 Amdocs\r
+ * All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ *\r
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ */\r
+package org.onap.aai.modelloader.entity.catalog;\r
+\r
+import com.sun.jersey.api.client.ClientResponse;\r
+\r
+import generated.VnfCatalog;\r
+import generated.VnfCatalog.PartNumberList;\r
+\r
+import inventory.aai.openecomp.org.v8.VnfImage;\r
+\r
+import org.eclipse.persistence.jaxb.MarshallerProperties;\r
+import org.onap.aai.modelloader.config.ModelLoaderConfig;\r
+import org.onap.aai.modelloader.entity.Artifact;\r
+import org.onap.aai.modelloader.entity.ArtifactHandler;\r
+import org.onap.aai.modelloader.restclient.AaiRestClient;\r
+import org.onap.aai.modelloader.restclient.AaiRestClient.MimeType;\r
+import org.onap.aai.modelloader.service.ModelLoaderMsgs;\r
+import org.openecomp.cl.api.Logger;\r
+import org.openecomp.cl.eelf.LoggerFactory;\r
+import org.springframework.web.util.UriUtils;\r
+\r
+import java.io.StringReader;\r
+import java.io.StringWriter;\r
+import java.io.UnsupportedEncodingException;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.UUID;\r
+\r
+import javax.ws.rs.core.Response;\r
+import javax.xml.bind.JAXBContext;\r
+import javax.xml.bind.JAXBException;\r
+import javax.xml.bind.Marshaller;\r
+import javax.xml.bind.Unmarshaller;\r
+\r
+\r
+public class VnfCatalogArtifactHandler extends ArtifactHandler {\r
+\r
+ private static Logger logger = LoggerFactory.getInstance()\r
+ .getLogger(VnfCatalogArtifactHandler.class.getName());\r
+\r
+ public VnfCatalogArtifactHandler(ModelLoaderConfig config) {\r
+ super(config);\r
+ }\r
+\r
+ @Override\r
+ public boolean pushArtifacts(List<Artifact> artifacts, String distributionId) {\r
+ for (Artifact art : artifacts) {\r
+ VnfCatalogArtifact vnfCatalog = (VnfCatalogArtifact) art;\r
+ String artifactPayload = vnfCatalog.getPayload();\r
+\r
+ AaiRestClient restClient = new AaiRestClient(this.config);\r
+ List<VnfImage> putImages = new ArrayList<VnfImage>();\r
+\r
+ try {\r
+ JAXBContext inputContext = JAXBContext.newInstance(VnfCatalog.class);\r
+ Unmarshaller unmarshaller = inputContext.createUnmarshaller();\r
+ StringReader reader = new StringReader(artifactPayload);\r
+ VnfCatalog cat = (VnfCatalog) unmarshaller.unmarshal(reader);\r
+\r
+ int numParts = cat.getPartNumberList().size();\r
+\r
+ for (int i = 0; i < numParts; i++) {\r
+\r
+ PartNumberList pnl = cat.getPartNumberList().get(i);\r
+\r
+ String application = pnl.getVendorInfo().getVendorModel();\r
+ String applicationVendor = pnl.getVendorInfo().getVendorName();\r
+\r
+ int numVersions = pnl.getSoftwareVersionList().size();\r
+\r
+ for (int j = 0; j < numVersions; j++) {\r
+ String applicationVersion = pnl.getSoftwareVersionList().get(j).getSoftwareVersion();\r
+\r
+ String imageId = "vnf image " + applicationVendor + " " + application + " "\r
+ + applicationVersion;\r
+\r
+ String queryURI = "application-vendor=" + applicationVendor + "&application=" + application + "&application-version=" + applicationVersion;\r
+ \r
+ String getUrl = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() + "?" + UriUtils.encodePath(queryURI, "UTF-8");\r
+\r
+ ClientResponse tryGet = restClient.getResource(getUrl, distributionId, MimeType.JSON);\r
+ if (tryGet == null) {\r
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,\r
+ "Ingestion failed on " + imageId + ". Rolling back distribution.");\r
+ failureCleanup(putImages, restClient, distributionId);\r
+ return false;\r
+ }\r
+ if (tryGet.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) {\r
+ // this vnf-image not already in the db, need to add\r
+ // only do this on 404 bc other error responses could mean there\r
+ // are problems that\r
+ // you might not want to try to PUT against\r
+\r
+ VnfImage image = new VnfImage();\r
+ image.setApplication(application);\r
+ image.setApplicationVendor(applicationVendor);\r
+ image.setApplicationVersion(applicationVersion);\r
+ String uuid = UUID.randomUUID().toString();\r
+ image.setUuid(uuid); // need to create uuid\r
+\r
+ System.setProperty("javax.xml.bind.context.factory",\r
+ "org.eclipse.persistence.jaxb.JAXBContextFactory");\r
+ JAXBContext jaxbContext = JAXBContext.newInstance(VnfImage.class);\r
+ Marshaller marshaller = jaxbContext.createMarshaller();\r
+ marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");\r
+ marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);\r
+ marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);\r
+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);\r
+ StringWriter writer = new StringWriter();\r
+ marshaller.marshal(image, writer);\r
+ String payload = writer.toString();\r
+\r
+ String putUrl = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() + "/vnf-image/"\r
+ + uuid;\r
+\r
+ ClientResponse putResp = restClient.putResource(putUrl, payload, distributionId,\r
+ MimeType.JSON);\r
+ if (putResp == null\r
+ || putResp.getStatus() != Response.Status.CREATED.getStatusCode()) {\r
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,\r
+ "Ingestion failed on vnf-image " + imageId + ". Rolling back distribution.");\r
+ failureCleanup(putImages, restClient, distributionId);\r
+ return false;\r
+ }\r
+ putImages.add(image);\r
+ logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, imageId + " successfully ingested.");\r
+ } else if (tryGet.getStatus() == Response.Status.OK.getStatusCode()) {\r
+ logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,\r
+ imageId + " already exists. Skipping ingestion.");\r
+ } else {\r
+ // if other than 404 or 200, something went wrong\r
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,\r
+ "Ingestion failed on vnf-image " + imageId + " with status " + tryGet.getStatus()\r
+ + ". Rolling back distribution.");\r
+ failureCleanup(putImages, restClient, distributionId);\r
+ return false;\r
+ }\r
+ }\r
+ }\r
+\r
+ } catch (JAXBException e) {\r
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,\r
+ "Ingestion failed. " + e.getMessage() + ". Rolling back distribution.");\r
+ failureCleanup(putImages, restClient, distributionId);\r
+ return false;\r
+ } catch (UnsupportedEncodingException e) {\r
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed. " + e.getMessage() + ". Rolling back distribution.");\r
+ failureCleanup(putImages, restClient, distributionId);\r
+ return false;\r
+ }\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ /*\r
+ * if something fails in the middle of ingesting the catalog we want to\r
+ * rollback any changes to the db\r
+ */\r
+ private void failureCleanup(List<VnfImage> putImages, AaiRestClient restClient, String transId) {\r
+ for (VnfImage image : putImages) {\r
+ String url = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() + "/vnf-image/"\r
+ + image.getUuid();\r
+ restClient.getAndDeleteResource(url, transId); // try to delete the image,\r
+ // if something goes wrong\r
+ // we can't really do\r
+ // anything here\r
+ }\r
+ }\r
+\r
+}\r