diff options
Diffstat (limited to 'src/main/java')
25 files changed, 2077 insertions, 1040 deletions
diff --git a/src/main/java/org/openecomp/modelloader/config/ModelLoaderConfig.java b/src/main/java/org/openecomp/modelloader/config/ModelLoaderConfig.java index 26f13ad..107010d 100644 --- a/src/main/java/org/openecomp/modelloader/config/ModelLoaderConfig.java +++ b/src/main/java/org/openecomp/modelloader/config/ModelLoaderConfig.java @@ -1,36 +1,36 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.config; import org.eclipse.jetty.util.security.Password; import org.openecomp.sdc.api.consumer.IConfiguration; -import java.io.File; -import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Properties; public class ModelLoaderConfig implements IConfiguration { - + // Configuration file structure public static final String PREFIX_MODEL_LOADER_CONFIG = "ml"; public static final String PREFIX_DISTRIBUTION_CLIENT = @@ -86,17 +86,9 @@ public class ModelLoaderConfig implements IConfiguration { * @param modelLoaderProperties properties needed to be configured for the model loader * @param certLocation location of the certificate */ - public ModelLoaderConfig(Properties modelLoaderProperties) { + public ModelLoaderConfig(Properties modelLoaderProperties, String certLocation) { this.modelLoaderProperties = modelLoaderProperties; - - String aaiKeystoreFile = modelLoaderProperties.getProperty(PROP_AAI_KEYSTORE_FILE); - if(aaiKeystoreFile != null){ - URL keystoreURL = this.getClass().getClassLoader().getResource(aaiKeystoreFile); - if(keystoreURL != null){ - File fKeystoreLocation = new File(keystoreURL.getPath()); - this.certLocation = fKeystoreLocation.getParent(); - } - } + this.certLocation = certLocation; // Get list of artifacts artifactTypes = new ArrayList<String>(); @@ -185,12 +177,12 @@ public class ModelLoaderConfig implements IConfiguration { return modelLoaderProperties.getProperty(PROP_AAI_BASE_URL); } - public String getAaiModelUrl() { - return modelLoaderProperties.getProperty(PROP_AAI_MODEL_RESOURCE_URL); + public String getAaiModelUrl(String version) { + return modelLoaderProperties.getProperty(PROP_AAI_MODEL_RESOURCE_URL).replace("v*", version); } - public String getAaiNamedQueryUrl() { - return modelLoaderProperties.getProperty(PROP_AAI_NAMED_QUERY_RESOURCE_URL); + public String getAaiNamedQueryUrl(String version) { + return modelLoaderProperties.getProperty(PROP_AAI_NAMED_QUERY_RESOURCE_URL).replace("v*", version); } public String getAaiVnfImageUrl() { @@ -232,4 +224,9 @@ public class ModelLoaderConfig implements IConfiguration { return false; } + + public boolean isFilterInEmptyResources() { + return false; + } + } diff --git a/src/main/java/org/openecomp/modelloader/entity/Artifact.java b/src/main/java/org/openecomp/modelloader/entity/Artifact.java index fb0ec9f..88c75eb 100644 --- a/src/main/java/org/openecomp/modelloader/entity/Artifact.java +++ b/src/main/java/org/openecomp/modelloader/entity/Artifact.java @@ -1,23 +1,25 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.entity; public abstract class Artifact { @@ -25,14 +27,14 @@ public abstract class Artifact { private String payload; private ArtifactType type; + public Artifact(ArtifactType type) { + this.type = type; + } + public ArtifactType getType() { return type; } - public void setType(ArtifactType type) { - this.type = type; - } - public String getPayload() { return payload; } diff --git a/src/main/java/org/openecomp/modelloader/entity/ArtifactHandler.java b/src/main/java/org/openecomp/modelloader/entity/ArtifactHandler.java index 16f2c87..bd6a861 100644 --- a/src/main/java/org/openecomp/modelloader/entity/ArtifactHandler.java +++ b/src/main/java/org/openecomp/modelloader/entity/ArtifactHandler.java @@ -1,37 +1,39 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.modelloader.entity; - -import org.openecomp.modelloader.config.ModelLoaderConfig; - -import java.util.List; - -public abstract class ArtifactHandler { - - protected ModelLoaderConfig config; - - public ArtifactHandler(ModelLoaderConfig config) { - this.config = config; - } - - public abstract boolean pushArtifacts(List<Artifact> artifacts, String distributionId); - -} +/**
+ * ============LICENSE_START=======================================================
+ * Model Loader
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ *
+ * ECOMP and OpenECOMP are trademarks
+ * and service marks of AT&T Intellectual Property.
+ */
+package org.openecomp.modelloader.entity;
+
+import org.openecomp.modelloader.config.ModelLoaderConfig;
+
+import java.util.List;
+
+public abstract class ArtifactHandler {
+
+ protected ModelLoaderConfig config;
+
+ public ArtifactHandler(ModelLoaderConfig config) {
+ this.config = config;
+ }
+
+ public abstract boolean pushArtifacts(List<Artifact> artifacts, String distributionId);
+
+}
diff --git a/src/main/java/org/openecomp/modelloader/entity/ArtifactType.java b/src/main/java/org/openecomp/modelloader/entity/ArtifactType.java index 8977e3c..381733b 100644 --- a/src/main/java/org/openecomp/modelloader/entity/ArtifactType.java +++ b/src/main/java/org/openecomp/modelloader/entity/ArtifactType.java @@ -1,25 +1,30 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.entity; public enum ArtifactType { - MODEL, NAMED_QUERY, VNF_CATALOG; + MODEL, + MODEL_V8, + NAMED_QUERY, + VNF_CATALOG; } diff --git a/src/main/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifact.java b/src/main/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifact.java index b2e1fdb..9b4c587 100644 --- a/src/main/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifact.java +++ b/src/main/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifact.java @@ -1,23 +1,25 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.entity.catalog; import org.openecomp.modelloader.entity.Artifact; @@ -25,7 +27,7 @@ import org.openecomp.modelloader.entity.ArtifactType; public class VnfCatalogArtifact extends Artifact { public VnfCatalogArtifact(String payload) { + super(ArtifactType.VNF_CATALOG); setPayload(payload); - setType(ArtifactType.VNF_CATALOG); } } diff --git a/src/main/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifactHandler.java b/src/main/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifactHandler.java index 189b069..5c526a6 100644 --- a/src/main/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifactHandler.java +++ b/src/main/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifactHandler.java @@ -1,184 +1,192 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.modelloader.entity.catalog; - -import com.sun.jersey.api.client.ClientResponse; - -import generated.VnfCatalog; -import generated.VnfCatalog.PartNumberList; - -import inventory.aai.openecomp.org.v8.VnfImage; - -import org.eclipse.persistence.jaxb.MarshallerProperties; -import org.openecomp.cl.api.Logger; -import org.openecomp.cl.eelf.LoggerFactory; -import org.openecomp.modelloader.config.ModelLoaderConfig; -import org.openecomp.modelloader.entity.Artifact; -import org.openecomp.modelloader.entity.ArtifactHandler; -import org.openecomp.modelloader.restclient.AaiRestClient; -import org.openecomp.modelloader.restclient.AaiRestClient.MimeType; -import org.openecomp.modelloader.service.ModelLoaderMsgs; - -import java.io.StringReader; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import javax.ws.rs.core.Response; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; - - -public class VnfCatalogArtifactHandler extends ArtifactHandler { - - private static Logger logger = LoggerFactory.getInstance() - .getLogger(VnfCatalogArtifactHandler.class.getName()); - - public VnfCatalogArtifactHandler(ModelLoaderConfig config) { - super(config); - } - - @Override - public boolean pushArtifacts(List<Artifact> artifacts, String distributionId) { - for (Artifact art : artifacts) { - VnfCatalogArtifact vnfCatalog = (VnfCatalogArtifact) art; - String artifactPayload = vnfCatalog.getPayload(); - - AaiRestClient restClient = new AaiRestClient(this.config); - List<VnfImage> putImages = new ArrayList<VnfImage>(); - - try { - JAXBContext inputContext = JAXBContext.newInstance(VnfCatalog.class); - Unmarshaller unmarshaller = inputContext.createUnmarshaller(); - StringReader reader = new StringReader(artifactPayload); - VnfCatalog cat = (VnfCatalog) unmarshaller.unmarshal(reader); - - int numParts = cat.getPartNumberList().size(); - - for (int i = 0; i < numParts; i++) { - - PartNumberList pnl = cat.getPartNumberList().get(i); - - String application = pnl.getVendorInfo().getVendorModel(); - String applicationVendor = pnl.getVendorInfo().getVendorName(); - - int numVersions = pnl.getSoftwareVersionList().size(); - - for (int j = 0; j < numVersions; j++) { - String applicationVersion = pnl.getSoftwareVersionList().get(j).getSoftwareVersion(); - - String imageId = "vnf image " + applicationVendor + " " + application + " " - + applicationVersion; - - String getUrl = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() - + "?application-vendor=" + applicationVendor + "&application=" + application - + "&application-version=" + applicationVersion; - - ClientResponse tryGet = restClient.getResource(getUrl, distributionId, MimeType.JSON); - if (tryGet == null) { - logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, - "Ingestion failed on " + imageId + ". Rolling back distribution."); - failureCleanup(putImages, restClient, distributionId); - return false; - } - if (tryGet.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) { - // this vnf-image not already in the db, need to add - // only do this on 404 bc other error responses could mean there - // are problems that - // you might not want to try to PUT against - - VnfImage image = new VnfImage(); - image.setApplication(application); - image.setApplicationVendor(applicationVendor); - image.setApplicationVersion(applicationVersion); - String uuid = UUID.randomUUID().toString(); - image.setUuid(uuid); // need to create uuid - - System.setProperty("javax.xml.bind.context.factory", - "org.eclipse.persistence.jaxb.JAXBContextFactory"); - JAXBContext jaxbContext = JAXBContext.newInstance(VnfImage.class); - Marshaller marshaller = jaxbContext.createMarshaller(); - marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); - marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false); - marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); - StringWriter writer = new StringWriter(); - marshaller.marshal(image, writer); - String payload = writer.toString(); - - String putUrl = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() + "/vnf-image/" - + uuid; - - ClientResponse putResp = restClient.putResource(putUrl, payload, distributionId, - MimeType.JSON); - if (putResp == null - || putResp.getStatus() != Response.Status.CREATED.getStatusCode()) { - logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, - "Ingestion failed on vnf-image " + imageId + ". Rolling back distribution."); - failureCleanup(putImages, restClient, distributionId); - return false; - } - putImages.add(image); - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, imageId + " successfully ingested."); - } else if (tryGet.getStatus() == Response.Status.OK.getStatusCode()) { - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, - imageId + " already exists. Skipping ingestion."); - } else { - // if other than 404 or 200, something went wrong - logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, - "Ingestion failed on vnf-image " + imageId + " with status " + tryGet.getStatus() - + ". Rolling back distribution."); - failureCleanup(putImages, restClient, distributionId); - return false; - } - } - } - - } catch (JAXBException e) { - logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, - "Ingestion failed. " + e.getMessage() + ". Rolling back distribution."); - failureCleanup(putImages, restClient, distributionId); - return false; - } - } - - return true; - } - - /* - * if something fails in the middle of ingesting the catalog we want to - * rollback any changes to the db - */ - private void failureCleanup(List<VnfImage> putImages, AaiRestClient restClient, String transId) { - for (VnfImage image : putImages) { - String url = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() + "/vnf-image/" - + image.getUuid(); - restClient.getAndDeleteResource(url, transId); // try to delete the image, - // if something goes wrong - // we can't really do - // anything here - } - } - -} +/**
+ * ============LICENSE_START=======================================================
+ * Model Loader
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ *
+ * ECOMP and OpenECOMP are trademarks
+ * and service marks of AT&T Intellectual Property.
+ */
+package org.openecomp.modelloader.entity.catalog;
+
+import com.sun.jersey.api.client.ClientResponse;
+
+import generated.VnfCatalog;
+import generated.VnfCatalog.PartNumberList;
+
+import inventory.aai.openecomp.org.v8.VnfImage;
+
+import org.eclipse.persistence.jaxb.MarshallerProperties;
+import org.openecomp.cl.api.Logger;
+import org.openecomp.cl.eelf.LoggerFactory;
+import org.openecomp.modelloader.config.ModelLoaderConfig;
+import org.openecomp.modelloader.entity.Artifact;
+import org.openecomp.modelloader.entity.ArtifactHandler;
+import org.openecomp.modelloader.restclient.AaiRestClient;
+import org.openecomp.modelloader.restclient.AaiRestClient.MimeType;
+import org.openecomp.modelloader.service.ModelLoaderMsgs;
+import org.springframework.web.util.UriUtils;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import javax.ws.rs.core.Response;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+
+public class VnfCatalogArtifactHandler extends ArtifactHandler {
+
+ private static Logger logger = LoggerFactory.getInstance()
+ .getLogger(VnfCatalogArtifactHandler.class.getName());
+
+ public VnfCatalogArtifactHandler(ModelLoaderConfig config) {
+ super(config);
+ }
+
+ @Override
+ public boolean pushArtifacts(List<Artifact> artifacts, String distributionId) {
+ for (Artifact art : artifacts) {
+ VnfCatalogArtifact vnfCatalog = (VnfCatalogArtifact) art;
+ String artifactPayload = vnfCatalog.getPayload();
+
+ AaiRestClient restClient = new AaiRestClient(this.config);
+ List<VnfImage> putImages = new ArrayList<VnfImage>();
+
+ try {
+ JAXBContext inputContext = JAXBContext.newInstance(VnfCatalog.class);
+ Unmarshaller unmarshaller = inputContext.createUnmarshaller();
+ StringReader reader = new StringReader(artifactPayload);
+ VnfCatalog cat = (VnfCatalog) unmarshaller.unmarshal(reader);
+
+ int numParts = cat.getPartNumberList().size();
+
+ for (int i = 0; i < numParts; i++) {
+
+ PartNumberList pnl = cat.getPartNumberList().get(i);
+
+ String application = pnl.getVendorInfo().getVendorModel();
+ String applicationVendor = pnl.getVendorInfo().getVendorName();
+
+ int numVersions = pnl.getSoftwareVersionList().size();
+
+ for (int j = 0; j < numVersions; j++) {
+ String applicationVersion = pnl.getSoftwareVersionList().get(j).getSoftwareVersion();
+
+ String imageId = "vnf image " + applicationVendor + " " + application + " "
+ + applicationVersion;
+
+ String queryURI = "application-vendor=" + applicationVendor + "&application=" + application + "&application-version=" + applicationVersion;
+
+ String getUrl = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() + "?" + UriUtils.encodePath(queryURI, "UTF-8");
+
+ ClientResponse tryGet = restClient.getResource(getUrl, distributionId, MimeType.JSON);
+ if (tryGet == null) {
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
+ "Ingestion failed on " + imageId + ". Rolling back distribution.");
+ failureCleanup(putImages, restClient, distributionId);
+ return false;
+ }
+ if (tryGet.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) {
+ // this vnf-image not already in the db, need to add
+ // only do this on 404 bc other error responses could mean there
+ // are problems that
+ // you might not want to try to PUT against
+
+ VnfImage image = new VnfImage();
+ image.setApplication(application);
+ image.setApplicationVendor(applicationVendor);
+ image.setApplicationVersion(applicationVersion);
+ String uuid = UUID.randomUUID().toString();
+ image.setUuid(uuid); // need to create uuid
+
+ System.setProperty("javax.xml.bind.context.factory",
+ "org.eclipse.persistence.jaxb.JAXBContextFactory");
+ JAXBContext jaxbContext = JAXBContext.newInstance(VnfImage.class);
+ Marshaller marshaller = jaxbContext.createMarshaller();
+ marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
+ marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
+ marshaller.setProperty(MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
+ StringWriter writer = new StringWriter();
+ marshaller.marshal(image, writer);
+ String payload = writer.toString();
+
+ String putUrl = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() + "/vnf-image/"
+ + uuid;
+
+ ClientResponse putResp = restClient.putResource(putUrl, payload, distributionId,
+ MimeType.JSON);
+ if (putResp == null
+ || putResp.getStatus() != Response.Status.CREATED.getStatusCode()) {
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
+ "Ingestion failed on vnf-image " + imageId + ". Rolling back distribution.");
+ failureCleanup(putImages, restClient, distributionId);
+ return false;
+ }
+ putImages.add(image);
+ logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, imageId + " successfully ingested.");
+ } else if (tryGet.getStatus() == Response.Status.OK.getStatusCode()) {
+ logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
+ imageId + " already exists. Skipping ingestion.");
+ } else {
+ // if other than 404 or 200, something went wrong
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
+ "Ingestion failed on vnf-image " + imageId + " with status " + tryGet.getStatus()
+ + ". Rolling back distribution.");
+ failureCleanup(putImages, restClient, distributionId);
+ return false;
+ }
+ }
+ }
+
+ } catch (JAXBException e) {
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
+ "Ingestion failed. " + e.getMessage() + ". Rolling back distribution.");
+ failureCleanup(putImages, restClient, distributionId);
+ return false;
+ } catch (UnsupportedEncodingException e) {
+ logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed. " + e.getMessage() + ". Rolling back distribution.");
+ failureCleanup(putImages, restClient, distributionId);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /*
+ * if something fails in the middle of ingesting the catalog we want to
+ * rollback any changes to the db
+ */
+ private void failureCleanup(List<VnfImage> putImages, AaiRestClient restClient, String transId) {
+ for (VnfImage image : putImages) {
+ String url = config.getAaiBaseUrl() + config.getAaiVnfImageUrl() + "/vnf-image/"
+ + image.getUuid();
+ restClient.getAndDeleteResource(url, transId); // try to delete the image,
+ // if something goes wrong
+ // we can't really do
+ // anything here
+ }
+ }
+
+}
diff --git a/src/main/java/org/openecomp/modelloader/entity/model/AbstractModelArtifact.java b/src/main/java/org/openecomp/modelloader/entity/model/AbstractModelArtifact.java new file mode 100644 index 0000000..2add53e --- /dev/null +++ b/src/main/java/org/openecomp/modelloader/entity/model/AbstractModelArtifact.java @@ -0,0 +1,85 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.openecomp.modelloader.config.ModelLoaderConfig; +import org.openecomp.modelloader.entity.Artifact; +import org.openecomp.modelloader.entity.ArtifactType; +import org.openecomp.modelloader.restclient.AaiRestClient; + +public abstract class AbstractModelArtifact extends Artifact { + + private String modelNamespace; + private String modelNamespaceVersion; + private Set<String> referencedModelIds = new HashSet<String>(); + + public AbstractModelArtifact(ArtifactType type) { + super(type); + } + + public Set<String> getDependentModelIds() { + return referencedModelIds; + } + + public void addDependentModelId(String dependentModelId) { + this.referencedModelIds.add(dependentModelId); + } + + public String getModelNamespace() { + return modelNamespace; + } + + public void setModelNamespace(String modelNamespace) { + this.modelNamespace = modelNamespace; + + // Get the version from the namespace (in format 'http://org.openecomp.aai.inventory/v9') + String[] parts = modelNamespace.split("/"); + modelNamespaceVersion = parts[parts.length-1].trim(); + } + + public String getModelNamespaceVersion() { + return modelNamespaceVersion; + } + + public abstract String getUniqueIdentifier(); + + public abstract boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels); + + public abstract void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId); + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("\nType=" + getType().toString() +"\nId=" + getUniqueIdentifier() +"\nVersion=" + getModelNamespaceVersion() + "\nDependant models: "); + for (String dep : referencedModelIds) { + sb.append(dep + " "); + } + + return sb.toString(); + } + + +} diff --git a/src/main/java/org/openecomp/modelloader/entity/model/IModelParser.java b/src/main/java/org/openecomp/modelloader/entity/model/IModelParser.java new file mode 100644 index 0000000..3a31756 --- /dev/null +++ b/src/main/java/org/openecomp/modelloader/entity/model/IModelParser.java @@ -0,0 +1,31 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import java.util.List; + +import org.openecomp.modelloader.entity.Artifact; + +public interface IModelParser { + public List<Artifact> parse(byte[] artifactPayload, String artifactName); +} diff --git a/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifact.java b/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifact.java index 904dba9..f316e28 100644 --- a/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifact.java +++ b/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifact.java @@ -1,60 +1,210 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.entity.model; -import org.openecomp.modelloader.entity.Artifact; +import java.io.StringWriter; +import java.util.List; -import java.util.HashSet; -import java.util.Set; +import javax.ws.rs.core.Response; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; -public class ModelArtifact extends Artifact { +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.modelloader.config.ModelLoaderConfig; +import org.openecomp.modelloader.entity.ArtifactType; +import org.openecomp.modelloader.restclient.AaiRestClient; +import org.openecomp.modelloader.service.ModelLoaderMsgs; +import org.w3c.dom.Node; - String nameVersionId; - Set<String> referencedModelIds = new HashSet<String>(); +import com.sun.jersey.api.client.ClientResponse; - public String getNameVersionId() { - return nameVersionId; - } +public class ModelArtifact extends AbstractModelArtifact { - public void setNameVersionId(String nameVersionId) { - this.nameVersionId = nameVersionId; - } + private static final String AAI_MODEL_VER_SUB_URL = "/model-vers/model-ver"; + + private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifact.class.getName()); + + private String modelVerId; + private String modelInvariantId; + private Node modelVer; + private boolean firstVersionOfModel = false; - public Set<String> getDependentModelIds() { - return referencedModelIds; - } + public ModelArtifact() { + super(ArtifactType.MODEL); + } - public void addDependentModelId(String dependentModelId) { - this.referencedModelIds.add(dependentModelId); + public String getModelVerId() { + return modelVerId; + } + + public void setModelVerId(String modelVerId) { + this.modelVerId = modelVerId; + } + + public String getModelInvariantId() { + return modelInvariantId; + } + + public void setModelInvariantId(String modelInvariantId) { + this.modelInvariantId = modelInvariantId; + } + + public Node getModelVer() { + return modelVer; + } + + public void setModelVer(Node modelVer) { + this.modelVer = modelVer; + } + + @Override + public String getUniqueIdentifier() { + return getModelInvariantId() + "|" + getModelVerId(); } @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("NameVersId=" + nameVersionId + "(" + getType().toString() + ") ==> "); - for (String dep : referencedModelIds) { - sb.append(dep + " "); + public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) { + ClientResponse getResponse = aaiClient.getResource(getModelUrl(config), distId, AaiRestClient.MimeType.XML); + if ( (getResponse == null) || (getResponse.getStatus() != Response.Status.OK.getStatusCode()) ) { + // Only attempt the PUT if the model doesn't already exist + ClientResponse putResponse = aaiClient.putResource(getModelUrl(config), getPayload(), distId, AaiRestClient.MimeType.XML); + if ( (putResponse != null) && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode()) ) { + addedModels.add(this); + + // Flag this as the first version of the model that has been added. + firstVersionOfModel = true; + + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " successfully ingested."); + } + else { + logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + getUniqueIdentifier() + + ". Rolling back distribution."); + return false; + } } + else { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getModelInvariantId() + " already exists. Skipping ingestion."); + getResponse = aaiClient.getResource(getModelVerUrl(config), distId, AaiRestClient.MimeType.XML); + if ( (getResponse == null) || (getResponse.getStatus() != Response.Status.OK.getStatusCode()) ) { + // Only attempt the PUT if the model-ver doesn't already exist + ClientResponse putResponse = null; - return sb.toString(); + try { + putResponse = aaiClient.putResource(getModelVerUrl(config), nodeToString(getModelVer()), distId, AaiRestClient.MimeType.XML); + } catch (TransformerException e) { + logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + getUniqueIdentifier() + + ": " + e.getMessage() + ". Rolling back distribution."); + return false; + } + if ( (putResponse != null) && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode()) ) { + addedModels.add(this); + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " successfully ingested."); + } + else { + logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + + getUniqueIdentifier() + ". Rolling back distribution."); + return false; + } + } + else { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " already exists. Skipping ingestion."); + } + } + + return true; } + + @Override + public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) { + String url = getModelVerUrl(config); + if (firstVersionOfModel) { + // If this was the first version of the model which was added, we want to remove the entire + // model rather than just the version. + url = getModelUrl(config); + } + + // Best effort to delete. Nothing we can do in the event this fails. + aaiClient.getAndDeleteResource(url, distId); + } + + private String getModelUrl(ModelLoaderConfig config) { + String baseURL = config.getAaiBaseUrl().trim(); + String subURL = null; + String instance = null; + + subURL = config.getAaiModelUrl(getModelNamespaceVersion()).trim(); + instance = getModelInvariantId(); + + if ( (!baseURL.endsWith("/")) && (!subURL.startsWith("/")) ) { + baseURL = baseURL + "/"; + } + + if ( baseURL.endsWith("/") && subURL.startsWith("/") ) { + baseURL = baseURL.substring(0, baseURL.length()-1); + } + if (!subURL.endsWith("/")) { + subURL = subURL + "/"; + } + + String url = baseURL + subURL + instance; + return url; + } + + private String getModelVerUrl(ModelLoaderConfig config) { + String baseURL = config.getAaiBaseUrl().trim(); + String subURL = null; + String instance = null; + + subURL = config.getAaiModelUrl(getModelNamespaceVersion()).trim() + getModelInvariantId() + AAI_MODEL_VER_SUB_URL; + instance = getModelVerId(); + + if ( (!baseURL.endsWith("/")) && (!subURL.startsWith("/")) ) { + baseURL = baseURL + "/"; + } + + if ( baseURL.endsWith("/") && subURL.startsWith("/") ) { + baseURL = baseURL.substring(0, baseURL.length()-1); + } + + if (!subURL.endsWith("/")) { + subURL = subURL + "/"; + } + + String url = baseURL + subURL + instance; + return url; + } + + private String nodeToString(Node node) throws TransformerException { + StringWriter sw = new StringWriter(); + Transformer t = TransformerFactory.newInstance().newTransformer(); + t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + t.transform(new DOMSource(node), new StreamResult(sw)); + return sw.toString(); + } } diff --git a/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifactHandler.java b/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifactHandler.java index fb269b1..13ef1b1 100644 --- a/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifactHandler.java +++ b/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifactHandler.java @@ -1,127 +1,79 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.entity.model; -import com.sun.jersey.api.client.ClientResponse; - import org.openecomp.cl.api.Logger; import org.openecomp.cl.eelf.LoggerFactory; import org.openecomp.modelloader.config.ModelLoaderConfig; import org.openecomp.modelloader.entity.Artifact; import org.openecomp.modelloader.entity.ArtifactHandler; -import org.openecomp.modelloader.entity.ArtifactType; import org.openecomp.modelloader.restclient.AaiRestClient; import org.openecomp.modelloader.service.ModelLoaderMsgs; import java.util.ArrayList; import java.util.List; -import javax.ws.rs.core.Response; public class ModelArtifactHandler extends ArtifactHandler { - private static Logger logger = LoggerFactory.getInstance() - .getLogger(ArtifactHandler.class.getName()); - + private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactHandler.class.getName()); + public ModelArtifactHandler(ModelLoaderConfig config) { super(config); } @Override - public boolean pushArtifacts(List<Artifact> artifacts, String distributionId) { + public boolean pushArtifacts(List<Artifact> artifacts, String distributionID) { ModelSorter modelSorter = new ModelSorter(); - List<Artifact> sortedModelArtifacts = modelSorter.sort(artifacts); - - // Push the ordered list of model artifacts to A&AI. If one fails, we need - // to roll back + List<Artifact> sortedModelArtifacts; + try { + sortedModelArtifacts = modelSorter.sort(artifacts); + } + catch (RuntimeException ex) { + logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve models: " + ex.getMessage()); + return false; + } + + // Push the ordered list of model artifacts to A&AI. If one fails, we need to roll back // the changes. - List<ModelArtifact> completedModels = new ArrayList<ModelArtifact>(); + List<AbstractModelArtifact> completedModels = new ArrayList<AbstractModelArtifact>(); AaiRestClient aaiClient = new AaiRestClient(config); for (Artifact art : sortedModelArtifacts) { - ModelArtifact model = (ModelArtifact) art; - ClientResponse getResponse = aaiClient.getResource(getUrl(model), distributionId, - AaiRestClient.MimeType.XML); - if ((getResponse == null) - || (getResponse.getStatus() != Response.Status.OK.getStatusCode())) { - // Only attempt the PUT if the model doesn't already exist - ClientResponse putResponse = aaiClient.putResource(getUrl(model), model.getPayload(), - distributionId, AaiRestClient.MimeType.XML); - if ((putResponse != null) - && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode())) { - completedModels.add(model); - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, model.getType().toString() + " " - + model.getNameVersionId() + " successfully ingested."); - } else { - logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, - "Ingestion failed for " + model.getType().toString() + " " + model.getNameVersionId() - + ". Rolling back distribution."); - - for (ModelArtifact modelToDelete : completedModels) { - // Best effort to delete. Nothing we can do in the event this fails. - aaiClient.getAndDeleteResource(getUrl(modelToDelete), distributionId); - } - - return false; + AbstractModelArtifact model = (AbstractModelArtifact)art; + if (model.push(aaiClient, config, distributionID, completedModels) != true) { + for (AbstractModelArtifact modelToDelete : completedModels) { + modelToDelete.rollbackModel(aaiClient, config, distributionID); } - } else { - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, model.getType().toString() + " " - + model.getNameVersionId() + " already exists. Skipping ingestion."); + + return false; } } return true; } - private String getUrl(ModelArtifact model) { - String baseUrl = config.getAaiBaseUrl().trim(); - String subUrl = null; - if (model.getType().equals(ArtifactType.MODEL)) { - subUrl = config.getAaiModelUrl().trim(); - } else { - subUrl = config.getAaiNamedQueryUrl().trim(); - } - - if ((!baseUrl.endsWith("/")) && (!subUrl.startsWith("/"))) { - baseUrl = baseUrl + "/"; - } - - if (baseUrl.endsWith("/") && subUrl.startsWith("/")) { - baseUrl = baseUrl.substring(0, baseUrl.length() - 1); - } - - if (!subUrl.endsWith("/")) { - subUrl = subUrl + "/"; - } - - String url = baseUrl + subUrl + model.getNameVersionId(); - return url; - } - - /** - * This method is used for the test REST interface to load models without an - * ASDC. - * - * @param payload content of the request - */ + // This method is used for the test REST interface to load models without an ASDC public void loadModelTest(byte[] payload) { List<Artifact> modelArtifacts = new ArrayList<Artifact>(); ModelArtifactParser parser = new ModelArtifactParser(); diff --git a/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifactParser.java b/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifactParser.java index 625145f..d81539c 100644 --- a/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifactParser.java +++ b/src/main/java/org/openecomp/modelloader/entity/model/ModelArtifactParser.java @@ -1,197 +1,194 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.entity.model; import org.openecomp.cl.api.Logger; import org.openecomp.cl.eelf.LoggerFactory; import org.openecomp.modelloader.entity.Artifact; -import org.openecomp.modelloader.entity.ArtifactType; import org.openecomp.modelloader.service.ModelLoaderMsgs; -import org.openecomp.modelloader.util.JsonXmlConverter; + import org.w3c.dom.Document; +import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import java.io.StringReader; -import java.io.StringWriter; import java.util.ArrayList; import java.util.List; +import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -public class ModelArtifactParser { - - private static String MODELS_ELEMENT = "models"; - private static String MODEL_ELEMENT = "model"; - private static String NAMED_QUERIES_ELEMENT = "named-queries"; - private static String NAMED_QUERY_ELEMENT = "named-query"; - private static String MODEL_NAME_VERSION_ID = "model-name-version-id"; - private static String NAMED_QUERY_VERSION_ID = "named-query-uuid"; - private static String RELATIONSHIP_DATA = "relationship-data"; - private static String RELATIONSHIP_KEY = "relationship-key"; - private static String RELATIONSHIP_VALUE = "relationship-value"; - private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-name-version-id"; - - private static Logger logger = LoggerFactory.getInstance() - .getLogger(ModelArtifactParser.class.getName()); - - /** - * This method parses the given artifact payload in byte array format and - * generates a list of model artifacts according to the content. - * - * @param artifactPayload - * artifact content to be parsed - * @param artifactName - * name of the artifact - * @return a list of model artifacts - */ - public List<Artifact> parse(byte[] artifactPayload, String artifactName) { - String payload = new String(artifactPayload); - List<Artifact> modelList = new ArrayList<Artifact>(); - - try { - // Artifact could be JSON or XML - if (JsonXmlConverter.isValidJson(payload)) { - payload = JsonXmlConverter.convertJsonToXml(payload); - } - - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - InputSource is = new InputSource(new StringReader(payload)); - Document doc = builder.parse(is); - - if ((doc.getDocumentElement().getNodeName().equalsIgnoreCase(MODEL_ELEMENT)) - || (doc.getDocumentElement().getNodeName().equalsIgnoreCase(NAMED_QUERY_ELEMENT))) { - ModelArtifact model = parseModel(doc.getDocumentElement(), payload); - if (model != null) { - modelList.add(model); - } else { - // TODO: A WARN message? - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, - "Unable to parse artifact " + artifactName); - } - } else if ((doc.getDocumentElement().getNodeName().equalsIgnoreCase(MODELS_ELEMENT)) - || (doc.getDocumentElement().getNodeName().equalsIgnoreCase(NAMED_QUERIES_ELEMENT))) { - // The complete set of models/named-queries were contained in this - // artifact - NodeList nodeList = doc.getDocumentElement().getChildNodes(); - for (int i = 0; i < nodeList.getLength(); i++) { - Node childNode = nodeList.item(i); - if ((childNode.getNodeName().equalsIgnoreCase(MODEL_ELEMENT)) - || (childNode.getNodeName().equalsIgnoreCase(NAMED_QUERY_ELEMENT))) { - String modelPayload = nodeToString(childNode); - ModelArtifact model = parseModel(childNode, modelPayload); - if (model != null) { - modelList.add(model); - } else { - // TODO: A WARN message? - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, - "Unable to parse artifact " + artifactName); - modelList.clear(); - break; - } - } - } - } - } catch (Exception ex) { - // This may not be an error. We may be receiving an artifact that is - // unrelated - // to models. In this case, we just ignore it. - // TODO: A WARN message? - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, - "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage()); - } - - return modelList; - } - - private ModelArtifact parseModel(Node modelNode, String payload) { - ModelArtifact model = new ModelArtifact(); - model.setPayload(payload); - - if (modelNode.getNodeName().equalsIgnoreCase(MODEL_ELEMENT)) { - model.setType(ArtifactType.MODEL); - } else { - model.setType(ArtifactType.NAMED_QUERY); - } - - parseNode(modelNode, model); - - if (model.getNameVersionId() == null) { - return null; - } - - return model; - } - - private void parseNode(Node node, ModelArtifact model) { - if (node.getNodeName().equalsIgnoreCase(MODEL_NAME_VERSION_ID)) { - model.setNameVersionId(node.getTextContent().trim()); - } else if (node.getNodeName().equalsIgnoreCase(NAMED_QUERY_VERSION_ID)) { - model.setNameVersionId(node.getTextContent().trim()); - } else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) { - parseRelationshipNode(node, model); - } else { - NodeList nodeList = node.getChildNodes(); - for (int i = 0; i < nodeList.getLength(); i++) { - Node childNode = nodeList.item(i); - parseNode(childNode, model); - } - } - } - - private void parseRelationshipNode(Node node, ModelArtifact model) { - String key = null; - String value = null; - - NodeList nodeList = node.getChildNodes(); - for (int i = 0; i < nodeList.getLength(); i++) { - Node childNode = nodeList.item(i); - if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) { - key = childNode.getTextContent().trim(); - } else if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) { - value = childNode.getTextContent().trim(); - } - } - - if ((key != null) && (key.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY))) { - if (value != null) { - model.addDependentModelId(value); - } - } - } - - private String nodeToString(Node node) throws TransformerException { - StringWriter sw = new StringWriter(); - Transformer transfomer = TransformerFactory.newInstance().newTransformer(); - transfomer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); - transfomer.transform(new DOMSource(node), new StreamResult(sw)); - return sw.toString(); - } + + +public class ModelArtifactParser implements IModelParser { + + private static String MODEL_VER = "model-ver"; + private static String MODEL_VERSION_ID = "model-version-id"; + private static String MODEL_INVARIANT_ID = "model-invariant-id"; + private static String RELATIONSHIP = "relationship"; + private static String RELATIONSHIP_DATA = "relationship-data"; + private static String RELATIONSHIP_KEY = "relationship-key"; + private static String RELATIONSHIP_VALUE = "relationship-value"; + private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id"; + private static String MODEL_VER_ELEMENT_RELATIONSHIP_KEY = "model-ver.model-version-id"; + + private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifactParser.class.getName()); + + public List<Artifact> parse(byte[] artifactPayload, String artifactName) { + String payload = new String(artifactPayload); + List<Artifact> modelList = new ArrayList<Artifact>(); + + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(payload)); + Document doc = builder.parse(is); + + ModelArtifact model = parseModel(doc.getDocumentElement(), payload); + + if (model != null) { + logger.info( ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> " + + "Model-invariant-Id: "+ model.getModelInvariantId() + + " Model-Version-Id: "+ model.getModelVerId()); + modelList.add(model); + } + else { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName); + return null; + } + } + catch (Exception ex) { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + ": " + ex.getLocalizedMessage()); + } + + return modelList; + } + + private ModelArtifact parseModel(Node modelNode, String payload) { + ModelArtifact model = new ModelArtifact(); + model.setPayload(payload); + + Element e = (Element)modelNode; + model.setModelNamespace(e.getAttribute("xmlns")); + + parseNode(modelNode, model); + + if ( (model.getModelInvariantId() == null) || (model.getModelVerId() == null) ){ + return null; + } + + return model; + } + + private void parseNode(Node node, ModelArtifact model) { + if (node.getNodeName().equalsIgnoreCase(MODEL_INVARIANT_ID)) { + model.setModelInvariantId(node.getTextContent().trim()); + } + else if (node.getNodeName().equalsIgnoreCase(MODEL_VERSION_ID)) { + model.setModelVerId(node.getTextContent().trim()); + } + else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP)) { + String dependentModelKey = parseRelationshipNode(node, model); + if (dependentModelKey != null) { + model.addDependentModelId(dependentModelKey); + } + } + else { + if (node.getNodeName().equalsIgnoreCase(MODEL_VER)) { + model.setModelVer(node); + if ( (model.getModelNamespace() != null) && (!model.getModelNamespace().isEmpty()) ) { + Element e = (Element) node; + e.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", model.getModelNamespace()); + } + } + + NodeList nodeList = node.getChildNodes(); + + for (int i = 0; i < nodeList.getLength(); i++) { + Node childNode = nodeList.item(i); + parseNode(childNode, model); + } + } + } + + private String parseRelationshipNode(Node node, ModelArtifact model) { + String currentKey = null; + String currentValue = null; + String modelVersionIdValue = null; + String modelInvariantIdValue = null; + + NodeList nodeList = node.getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node childNode = nodeList.item(i); + + if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) { + NodeList relDataChildList = childNode.getChildNodes(); + + for (int j = 0; j < relDataChildList.getLength(); j++) { + Node relDataChildNode = relDataChildList.item(j); + + if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) { + currentKey = relDataChildNode.getTextContent().trim(); + + if (currentValue != null) { + if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) { + modelVersionIdValue = currentValue; + } + else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) { + modelInvariantIdValue = currentValue; + } + + currentKey = null; + currentValue = null; + } + } + else if (relDataChildNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) { + currentValue = relDataChildNode.getTextContent().trim(); + + if (currentKey != null) { + if (currentKey.equalsIgnoreCase(MODEL_VER_ELEMENT_RELATIONSHIP_KEY)) { + modelVersionIdValue = currentValue; + } + else if (currentKey.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY)) { + modelInvariantIdValue = currentValue; + } + + currentKey = null; + currentValue = null; + } + } + } + } + } + + if ( (modelVersionIdValue != null) && (modelInvariantIdValue != null) ) { + return modelInvariantIdValue + "|" + modelVersionIdValue; + } + + return null; + + } + } diff --git a/src/main/java/org/openecomp/modelloader/entity/model/ModelParserFactory.java b/src/main/java/org/openecomp/modelloader/entity/model/ModelParserFactory.java new file mode 100644 index 0000000..aa3283f --- /dev/null +++ b/src/main/java/org/openecomp/modelloader/entity/model/ModelParserFactory.java @@ -0,0 +1,87 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.modelloader.service.ModelLoaderMsgs; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.xml.sax.InputSource; + +public class ModelParserFactory { + private static Logger logger = LoggerFactory.getInstance().getLogger(ModelParserFactory.class.getName()); + + private static String MODEL_ELEMENT = "model"; + private static String NAMED_QUERY_ELEMENT = "named-query"; + + public static IModelParser createModelParser(byte[] artifactPayload, String artifactName) { + Document doc = null; + + try { + String payload = new String(artifactPayload); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder; + builder = factory.newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(payload)); + doc = builder.parse(is); + } catch (Exception e) { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName); + return null; + } + + if (doc.getDocumentElement().getNodeName().equalsIgnoreCase(NAMED_QUERY_ELEMENT)) { + return new NamedQueryArtifactParser(); + } + + if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase(MODEL_ELEMENT)) { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + + ": Invalid root element: " + doc.getDocumentElement().getNodeName()); + return null; + } + + Element e = doc.getDocumentElement(); + String ns = e.getAttribute("xmlns"); + String[] parts = ns.split("/"); + + if (parts.length < 1) { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName + + ": Could not parse namespace version"); + return null; + } + + String modelNamespaceVersion = parts[parts.length-1].trim().replace("v", ""); + int version = Integer.parseInt(modelNamespaceVersion); + + if (version > 8) { + return new ModelArtifactParser(); + } + + return new ModelV8ArtifactParser(); + } +} diff --git a/src/main/java/org/openecomp/modelloader/entity/model/ModelSorter.java b/src/main/java/org/openecomp/modelloader/entity/model/ModelSorter.java index 4dcda71..274b18f 100644 --- a/src/main/java/org/openecomp/modelloader/entity/model/ModelSorter.java +++ b/src/main/java/org/openecomp/modelloader/entity/model/ModelSorter.java @@ -1,23 +1,25 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.entity.model; import jline.internal.Log; @@ -42,11 +44,11 @@ public class ModelSorter { * Wraps a Model object to form dependencies other Models using Edges. */ static class Node { - private final ModelArtifact model; + private final AbstractModelArtifact model; private final HashSet<Edge> inEdges; private final HashSet<Edge> outEdges; - public Node(ModelArtifact model) { + public Node(AbstractModelArtifact model) { this.model = model; inEdges = new HashSet<Edge>(); outEdges = new HashSet<Edge>(); @@ -61,19 +63,18 @@ public class ModelSorter { @Override public String toString() { - return model.getNameVersionId(); + return model.getUniqueIdentifier(); } @Override public boolean equals(Object other) { - ModelArtifact otherModel = ((Node) other).model; - return this.model.getNameVersionId().equals(otherModel.getNameVersionId()); + AbstractModelArtifact otherModel = ((Node) other).model; + return this.model.getUniqueIdentifier().equals(otherModel.getUniqueIdentifier()); } @Override public int hashCode() { - return this.model.getNameVersionId().hashCode(); - + return this.model.getUniqueIdentifier().hashCode(); } } @@ -111,7 +112,7 @@ public class ModelSorter { Collection<Node> nodes = createNodes(originalList); Collection<Node> sortedNodes = sortNodes(nodes); - + List<Artifact> sortedModelsList = new ArrayList<Artifact>(sortedNodes.size()); for (Node node : sortedNodes) { sortedModelsList.add(node.model); @@ -131,23 +132,24 @@ public class ModelSorter { // load list of models into a map, so we can later replace referenceIds with // real Models - HashMap<String, ModelArtifact> versionIdToModelMap = new HashMap<String, ModelArtifact>(); + HashMap<String, AbstractModelArtifact> versionIdToModelMap = new HashMap<String, AbstractModelArtifact>(); for (Artifact art : models) { - ModelArtifact ma = (ModelArtifact) art; - versionIdToModelMap.put(ma.getNameVersionId(), ma); + AbstractModelArtifact ma = (AbstractModelArtifact) art; + versionIdToModelMap.put(ma.getUniqueIdentifier(), ma); } HashMap<String, Node> nodes = new HashMap<String, Node>(); // create a node for each model and its referenced models for (Artifact art : models) { - ModelArtifact model = (ModelArtifact) art; + AbstractModelArtifact model = (AbstractModelArtifact) art; + // node might have been created by another model referencing it - Node node = nodes.get(model.getNameVersionId()); + Node node = nodes.get(model.getUniqueIdentifier()); if (null == node) { node = new Node(model); - nodes.put(model.getNameVersionId(), node); + nodes.put(model.getUniqueIdentifier(), node); } for (String referencedModelId : model.getDependentModelIds()) { @@ -156,7 +158,7 @@ public class ModelSorter { if (null == referencedNode) { // create node - ModelArtifact referencedModel = versionIdToModelMap.get(referencedModelId); + AbstractModelArtifact referencedModel = versionIdToModelMap.get(referencedModelId); if (referencedModel == null) { Log.debug("ignoring " + referencedModelId); continue; // referenced model not supplied, no need to sort it @@ -179,7 +181,6 @@ public class ModelSorter { * @return a sorted collection of the given nodes */ private Collection<Node> sortNodes(Collection<Node> unsortedNodes) { - // L <- Empty list that will contain the sorted elements ArrayList<Node> nodeList = new ArrayList<Node>(); @@ -230,4 +231,5 @@ public class ModelSorter { return nodeList; } + } diff --git a/src/main/java/org/openecomp/modelloader/entity/model/ModelV8Artifact.java b/src/main/java/org/openecomp/modelloader/entity/model/ModelV8Artifact.java new file mode 100644 index 0000000..847f880 --- /dev/null +++ b/src/main/java/org/openecomp/modelloader/entity/model/ModelV8Artifact.java @@ -0,0 +1,122 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import java.util.List; + +import javax.ws.rs.core.Response; + +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.modelloader.config.ModelLoaderConfig; +import org.openecomp.modelloader.entity.Artifact; +import org.openecomp.modelloader.entity.ArtifactType; +import org.openecomp.modelloader.restclient.AaiRestClient; +import org.openecomp.modelloader.service.ModelLoaderMsgs; + +import com.sun.jersey.api.client.ClientResponse; + +public class ModelV8Artifact extends AbstractModelArtifact { + private static String AAI_CONVERSION_URL = "/aai/tools/modeltransform"; + + private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifact.class.getName()); + + private String modelNameVersionId; + private ModelArtifact translatedModel; + + public ModelV8Artifact() { + super(ArtifactType.MODEL_V8); + } + + public String getModelNameVersionId() { + return modelNameVersionId; + } + + public void setModelNameVersionId(String modelNameVersionId) { + this.modelNameVersionId = modelNameVersionId; + } + + @Override + public String getUniqueIdentifier() { + return getModelNameVersionId(); + } + + @Override + public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) { + // For a legacy model (version <= v8), we need to call out to an A&AI endpoint to convert to the proper format + ClientResponse response = aaiClient.postResource(getConversionUrl(config), constructTransformPayload(), distId, AaiRestClient.MimeType.XML); + if ( (response == null) || (response.getStatus() != Response.Status.OK.getStatusCode()) ) { + logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + + getType().toString() + " " + getModelNameVersionId() + ". Unable to convert model. Rolling back distribution."); + return false; + } + + String translatedPayload = response.getEntity(String.class); + + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Translated artifact payload:\n" + translatedPayload); + + ModelArtifactParser parser = new ModelArtifactParser(); + + List<Artifact> parsedArtifacts = parser.parse(translatedPayload.getBytes(), "translated-payload"); + if (parsedArtifacts == null || parsedArtifacts.isEmpty()) { + return false; + } + + translatedModel = (ModelArtifact)parsedArtifacts.get(0); + return translatedModel.push(aaiClient, config, distId, addedModels); + } + + @Override + public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) { + if (translatedModel != null) { + translatedModel.rollbackModel(aaiClient, config, distId); + } + } + + + private String constructTransformPayload() { + // A&AI requires that to transform a legacy model, we need to use the v8 namespace (even + // if the version < 8) + return getPayload().replaceFirst("aai.inventory/v.", "aai.inventory/v8"); + } + + private String getConversionUrl(ModelLoaderConfig config) { + String baseUrl = config.getAaiBaseUrl().trim(); + String subUrl = AAI_CONVERSION_URL; + + if ( (!baseUrl.endsWith("/")) && (!subUrl.startsWith("/")) ) { + baseUrl = baseUrl + "/"; + } + + if ( baseUrl.endsWith("/") && subUrl.startsWith("/") ) { + baseUrl = baseUrl.substring(0, baseUrl.length()-1); + } + + if (!subUrl.endsWith("/")) { + subUrl = subUrl + "/"; + } + + String url = baseUrl + subUrl; + return url; + } +} diff --git a/src/main/java/org/openecomp/modelloader/entity/model/ModelV8ArtifactParser.java b/src/main/java/org/openecomp/modelloader/entity/model/ModelV8ArtifactParser.java new file mode 100644 index 0000000..f5ecb2b --- /dev/null +++ b/src/main/java/org/openecomp/modelloader/entity/model/ModelV8ArtifactParser.java @@ -0,0 +1,136 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.modelloader.entity.Artifact; +import org.openecomp.modelloader.service.ModelLoaderMsgs; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + + +public class ModelV8ArtifactParser implements IModelParser { + + private static String MODEL_NAME_VERSION_ID = "model-name-version-id"; + private static String RELATIONSHIP_DATA = "relationship-data"; + private static String RELATIONSHIP_KEY = "relationship-key"; + private static String RELATIONSHIP_VALUE = "relationship-value"; + private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-name-version-id"; + + + private static Logger logger = LoggerFactory.getInstance().getLogger(ModelV8ArtifactParser.class.getName()); + + public List<Artifact> parse(byte[] artifactPayload, String artifactName) { + String payload = new String(artifactPayload); + List<Artifact> modelList = new ArrayList<Artifact>(); + + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(payload)); + Document doc = builder.parse(is); + + ModelV8Artifact model = parseModel(doc.getDocumentElement(), payload); + + if (model != null) { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Model parsed =====>>>> " + "Model-Named-Version-Id: "+ model.getModelNameVersionId()); + modelList.add(model); + } + else { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse legacy model artifact " + artifactName); + return null; + } + } + catch (Exception ex) { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse legacy model artifact " + artifactName + ": " + ex.getLocalizedMessage()); + } + + return modelList; + } + + private ModelV8Artifact parseModel(Node modelNode, String payload) { + ModelV8Artifact model = new ModelV8Artifact(); + model.setPayload(payload); + + Element e = (Element)modelNode; + model.setModelNamespace(e.getAttribute("xmlns")); + + parseNode(modelNode, model); + + if (model.getModelNameVersionId() == null) { + return null; + } + + return model; + } + + private void parseNode(Node node, ModelV8Artifact model) { + if (node.getNodeName().equalsIgnoreCase(MODEL_NAME_VERSION_ID)) { + model.setModelNameVersionId(node.getTextContent().trim()); + } + else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) { + parseRelationshipNode(node, model); + } + else { + NodeList nodeList = node.getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node childNode = nodeList.item(i); + parseNode(childNode, model); + } + } + } + + private void parseRelationshipNode(Node node, ModelV8Artifact model) { + String key = null; + String value = null; + + NodeList nodeList = node.getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node childNode = nodeList.item(i); + if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) { + key = childNode.getTextContent().trim(); + } + else if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) { + value = childNode.getTextContent().trim(); + } + } + + if ( (key != null) && (key.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY )) ) { + if (value != null) { + model.addDependentModelId(value); + } + } + } +} diff --git a/src/main/java/org/openecomp/modelloader/entity/model/NamedQueryArtifact.java b/src/main/java/org/openecomp/modelloader/entity/model/NamedQueryArtifact.java new file mode 100644 index 0000000..1a4ceaa --- /dev/null +++ b/src/main/java/org/openecomp/modelloader/entity/model/NamedQueryArtifact.java @@ -0,0 +1,113 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import java.util.List; + +import javax.ws.rs.core.Response; + +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.modelloader.config.ModelLoaderConfig; +import org.openecomp.modelloader.entity.ArtifactType; +import org.openecomp.modelloader.restclient.AaiRestClient; +import org.openecomp.modelloader.service.ModelLoaderMsgs; + +import com.sun.jersey.api.client.ClientResponse; + +public class NamedQueryArtifact extends AbstractModelArtifact { + + private Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifact.class.getName()); + + private String namedQueryUuid; + + public NamedQueryArtifact() { + super(ArtifactType.NAMED_QUERY); + } + + public String getNamedQueryUuid() { + return namedQueryUuid; + } + + public void setNamedQueryUuid(String namedQueryUuid) { + this.namedQueryUuid = namedQueryUuid; + } + + @Override + public String getUniqueIdentifier() { + return getNamedQueryUuid(); + } + + @Override + public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId, List<AbstractModelArtifact> addedModels) { + ClientResponse getResponse = aaiClient.getResource(getNamedQueryUrl(config), distId, AaiRestClient.MimeType.XML); + if ( (getResponse == null) || (getResponse.getStatus() != Response.Status.OK.getStatusCode()) ) { + // Only attempt the PUT if the model doesn't already exist + ClientResponse putResponse = aaiClient.putResource(getNamedQueryUrl(config), getPayload(), distId, AaiRestClient.MimeType.XML); + if ( (putResponse != null) && (putResponse.getStatus() == Response.Status.CREATED.getStatusCode()) ) { + addedModels.add(this); + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " successfully ingested."); + } + else { + logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Ingestion failed for " + getType().toString() + " " + getUniqueIdentifier() + + ". Rolling back distribution."); + return false; + } + } + else { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, getType().toString() + " " + getUniqueIdentifier() + " already exists. Skipping ingestion."); + } + + return true; + } + + @Override + public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) { + // Best effort to delete. Nothing we can do in the event this fails. + aaiClient.getAndDeleteResource(getNamedQueryUrl(config), distId); + } + + private String getNamedQueryUrl(ModelLoaderConfig config) { + String baseURL = config.getAaiBaseUrl().trim(); + String subURL = null; + String instance = null; + + subURL = config.getAaiNamedQueryUrl(getModelNamespaceVersion()).trim(); + instance = this.getNamedQueryUuid(); + + if ( (!baseURL.endsWith("/")) && (!subURL.startsWith("/")) ) { + baseURL = baseURL + "/"; + } + + if ( baseURL.endsWith("/") && subURL.startsWith("/") ) { + baseURL = baseURL.substring(0, baseURL.length()-1); + } + + if (!subURL.endsWith("/")) { + subURL = subURL + "/"; + } + + String url = baseURL + subURL + instance; + return url; + } +} diff --git a/src/main/java/org/openecomp/modelloader/entity/model/NamedQueryArtifactParser.java b/src/main/java/org/openecomp/modelloader/entity/model/NamedQueryArtifactParser.java new file mode 100644 index 0000000..3643f18 --- /dev/null +++ b/src/main/java/org/openecomp/modelloader/entity/model/NamedQueryArtifactParser.java @@ -0,0 +1,136 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.modelloader.entity.Artifact; +import org.openecomp.modelloader.service.ModelLoaderMsgs; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + + +public class NamedQueryArtifactParser implements IModelParser { + + private static String NAMED_QUERY_VERSION_ID = "named-query-uuid"; + private static String RELATIONSHIP_DATA = "relationship-data"; + private static String RELATIONSHIP_KEY = "relationship-key"; + private static String RELATIONSHIP_VALUE = "relationship-value"; + private static String MODEL_ELEMENT_RELATIONSHIP_KEY = "model.model-invariant-id"; + + + private static Logger logger = LoggerFactory.getInstance().getLogger(NamedQueryArtifactParser.class.getName()); + + public List<Artifact> parse(byte[] artifactPayload, String artifactName) { + String payload = new String(artifactPayload); + List<Artifact> modelList = new ArrayList<Artifact>(); + + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + InputSource is = new InputSource(new StringReader(payload)); + Document doc = builder.parse(is); + + NamedQueryArtifact model = parseModel(doc.getDocumentElement(), payload); + + if (model != null) { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Named-Query parsed =====>>>> " + "Named-Query-UUID: "+ model.getNamedQueryUuid()); + modelList.add(model); + } + else { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName); + return null; + } + } + catch (Exception ex) { + logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse named-query artifact " + artifactName + ": " + ex.getLocalizedMessage()); + } + + return modelList; + } + + private NamedQueryArtifact parseModel(Node modelNode, String payload) { + NamedQueryArtifact model = new NamedQueryArtifact(); + model.setPayload(payload); + + Element e = (Element)modelNode; + model.setModelNamespace(e.getAttribute("xmlns")); + + parseNode(modelNode, model); + + if (model.getNamedQueryUuid() == null) { + return null; + } + + return model; + } + + private void parseNode(Node node, NamedQueryArtifact model) { + if (node.getNodeName().equalsIgnoreCase(NAMED_QUERY_VERSION_ID)) { + model.setNamedQueryUuid(node.getTextContent().trim()); + } + else if (node.getNodeName().equalsIgnoreCase(RELATIONSHIP_DATA)) { + parseRelationshipNode(node, model); + } + else { + NodeList nodeList = node.getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node childNode = nodeList.item(i); + parseNode(childNode, model); + } + } + } + + private void parseRelationshipNode(Node node, NamedQueryArtifact model) { + String key = null; + String value = null; + + NodeList nodeList = node.getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node childNode = nodeList.item(i); + if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_KEY)) { + key = childNode.getTextContent().trim(); + } + else if (childNode.getNodeName().equalsIgnoreCase(RELATIONSHIP_VALUE)) { + value = childNode.getTextContent().trim(); + } + } + + if ( (key != null) && (key.equalsIgnoreCase(MODEL_ELEMENT_RELATIONSHIP_KEY )) ) { + if (value != null) { + model.addDependentModelId(value); + } + } + } +} diff --git a/src/main/java/org/openecomp/modelloader/notification/DistributionStatusMsg.java b/src/main/java/org/openecomp/modelloader/notification/DistributionStatusMsg.java index 6079d5d..0ef2a42 100644 --- a/src/main/java/org/openecomp/modelloader/notification/DistributionStatusMsg.java +++ b/src/main/java/org/openecomp/modelloader/notification/DistributionStatusMsg.java @@ -1,75 +1,77 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.modelloader.notification; - -import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; -import org.openecomp.sdc.utils.DistributionStatusEnum; - -public class DistributionStatusMsg implements IDistributionStatusMessage { - private DistributionStatusEnum status; - private String distributionId; - private String consumerId; - private String artifactUrl; - - /** - * Creates a new DistributionStatusMsg instance. - * - * @param status - The distribution status to be reported. - * @param distributionId - The identifier of the distribution who's status is being rported on. - * @param consumerId - Identifier of the consumer associated with the distribution. - * @param artifactUrl - Resource identifier for the artifact. - */ - public DistributionStatusMsg(DistributionStatusEnum status, - String distributionId, - String consumerId, - String artifactUrl) { - this.status = status; - this.distributionId = distributionId; - this.consumerId = consumerId; - this.artifactUrl = artifactUrl; - } - - @Override - public long getTimestamp() { - long currentTimeMillis = System.currentTimeMillis(); - return currentTimeMillis; - } - - @Override - public DistributionStatusEnum getStatus() { - return status; - } - - @Override - public String getDistributionID() { - return distributionId; - } - - @Override - public String getConsumerID() { - return consumerId; - } - - @Override - public String getArtifactURL() { - return artifactUrl; - } -} +/**
+ * ============LICENSE_START=======================================================
+ * Model Loader
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ *
+ * ECOMP and OpenECOMP are trademarks
+ * and service marks of AT&T Intellectual Property.
+ */
+package org.openecomp.modelloader.notification;
+
+import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
+import org.openecomp.sdc.utils.DistributionStatusEnum;
+
+public class DistributionStatusMsg implements IDistributionStatusMessage {
+ private DistributionStatusEnum status;
+ private String distributionId;
+ private String consumerId;
+ private String artifactUrl;
+
+ /**
+ * Creates a new DistributionStatusMsg instance.
+ *
+ * @param status - The distribution status to be reported.
+ * @param distributionId - The identifier of the distribution who's status is being rported on.
+ * @param consumerId - Identifier of the consumer associated with the distribution.
+ * @param artifactUrl - Resource identifier for the artifact.
+ */
+ public DistributionStatusMsg(DistributionStatusEnum status,
+ String distributionId,
+ String consumerId,
+ String artifactUrl) {
+ this.status = status;
+ this.distributionId = distributionId;
+ this.consumerId = consumerId;
+ this.artifactUrl = artifactUrl;
+ }
+
+ @Override
+ public long getTimestamp() {
+ long currentTimeMillis = System.currentTimeMillis();
+ return currentTimeMillis;
+ }
+
+ @Override
+ public DistributionStatusEnum getStatus() {
+ return status;
+ }
+
+ @Override
+ public String getDistributionID() {
+ return distributionId;
+ }
+
+ @Override
+ public String getConsumerID() {
+ return consumerId;
+ }
+
+ @Override
+ public String getArtifactURL() {
+ return artifactUrl;
+ }
+}
diff --git a/src/main/java/org/openecomp/modelloader/notification/EventCallback.java b/src/main/java/org/openecomp/modelloader/notification/EventCallback.java index 3b32315..059964e 100644 --- a/src/main/java/org/openecomp/modelloader/notification/EventCallback.java +++ b/src/main/java/org/openecomp/modelloader/notification/EventCallback.java @@ -1,23 +1,25 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.notification; import org.openecomp.sdc.api.IDistributionClient; @@ -40,8 +42,9 @@ import org.openecomp.modelloader.config.ModelLoaderConfig; import org.openecomp.modelloader.entity.Artifact; import org.openecomp.modelloader.entity.catalog.VnfCatalogArtifact; import org.openecomp.modelloader.entity.catalog.VnfCatalogArtifactHandler; +import org.openecomp.modelloader.entity.model.IModelParser; import org.openecomp.modelloader.entity.model.ModelArtifactHandler; -import org.openecomp.modelloader.entity.model.ModelArtifactParser; +import org.openecomp.modelloader.entity.model.ModelParserFactory; import org.openecomp.modelloader.service.ModelLoaderMsgs; import org.slf4j.MDC; @@ -80,7 +83,6 @@ public class EventCallback implements INotificationCallback { List<IArtifactInfo> artifacts = getArtifacts(data); List<Artifact> modelArtifacts = new ArrayList<Artifact>(); List<Artifact> catalogArtifacts = new ArrayList<Artifact>(); - ModelArtifactParser modelArtParser = new ModelArtifactParser(); for (IArtifactInfo artifact : artifacts) { // Grab the current time so we can measure the download time for the @@ -102,16 +104,22 @@ public class EventCallback implements INotificationCallback { break; } - logger.debug("Artifact: " + artifact.getArtifactName() + " Payload:\n" + new String(downloadResult.getArtifactPayload())); + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, + "Downloaded artifact: " + artifact.getArtifactName() + " Payload:\n" + new String(downloadResult.getArtifactPayload())); publishDownloadSuccess(data, artifact, downloadResult); - if ((artifact.getArtifactType() - .compareToIgnoreCase(ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.toString()) == 0) - || (artifact.getArtifactType() - .compareToIgnoreCase(ArtifactTypeEnum.MODEL_QUERY_SPEC.toString()) == 0)) { - modelArtifacts.addAll(modelArtParser.parse(downloadResult.getArtifactPayload(), - downloadResult.getArtifactName())); + if ((artifact.getArtifactType().compareToIgnoreCase(ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.toString()) == 0) + || (artifact.getArtifactType().compareToIgnoreCase(ArtifactTypeEnum.MODEL_QUERY_SPEC.toString()) == 0)) { + IModelParser parser = ModelParserFactory.createModelParser(downloadResult.getArtifactPayload(), downloadResult.getArtifactName()); + List<Artifact> parsedArtifacts = parser.parse(downloadResult.getArtifactPayload(), downloadResult.getArtifactName()); + if (parsedArtifacts != null && !parsedArtifacts.isEmpty()) { + modelArtifacts.addAll(parsedArtifacts); + } else { + success = false; + publishDeployFailure(data, artifact); + break; + } } else if (artifact.getArtifactType() .compareToIgnoreCase(ArtifactTypeEnum.VNF_CATALOG.toString()) == 0) { catalogArtifacts @@ -150,6 +158,8 @@ public class EventCallback implements INotificationCallback { } } } + } else { + statusString = "FAILURE"; } auditLogger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, @@ -212,9 +222,6 @@ public class EventCallback implements INotificationCallback { metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "download success", artifact.getArtifactName(), sendDownloadStatus.getDistributionActionResult().toString()); - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, - "Downloaded artifact: " + artifact.getArtifactName()); - if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Failed to publish download success status: " diff --git a/src/main/java/org/openecomp/modelloader/restclient/AaiRestClient.java b/src/main/java/org/openecomp/modelloader/restclient/AaiRestClient.java index e13bdbd..4d5e487 100644 --- a/src/main/java/org/openecomp/modelloader/restclient/AaiRestClient.java +++ b/src/main/java/org/openecomp/modelloader/restclient/AaiRestClient.java @@ -1,23 +1,25 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.restclient; import com.sun.jersey.api.client.Client; @@ -292,6 +294,84 @@ public class AaiRestClient { } /** + * Send a POST request to the A&AI. + * + * @param url + * - the url + * @param transId + * - transaction ID + * @param payload + * - the XML or JSON payload for the request + * @param mimeType + * - the content type (XML or JSON) + * @return ClientResponse + */ + public ClientResponse postResource(String url, String payload, String transId, MimeType mimeType) { + ClientResponse result = null; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + long startTimeInMs = 0; + MdcOverride override = new MdcOverride(); + + try { + Client client = setupClient(); + + baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + if (logger.isDebugEnabled()) { + client.addFilter(new LoggingFilter(ps)); + } + + // Grab the current time so that we can use it for metrics purposes later. + startTimeInMs = System.currentTimeMillis(); + override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs)); + + if (useBasicAuth()) { + result = client.resource(url).header(HEADER_TRANS_ID, transId) + .header(HEADER_FROM_APP_ID, ML_APP_NAME) + .header(HEADER_AUTHORIZATION, getAuthenticationCredentials()) + .type(mimeType.getHttpHeaderType()).post(ClientResponse.class, payload); + } else { + result = client.resource(url).header(HEADER_TRANS_ID, transId) + .header(HEADER_FROM_APP_ID, ML_APP_NAME).type(mimeType.getHttpHeaderType()) + .post(ClientResponse.class, payload); + } + } catch (Exception ex) { + logger.error(ModelLoaderMsgs.AAI_REST_REQUEST_ERROR, "POST", url, ex.getLocalizedMessage()); + return null; + } finally { + if (logger.isDebugEnabled()) { + logger.debug(baos.toString()); + } + } + + if ((result != null) && ((result.getStatus() == Response.Status.CREATED.getStatusCode()) + || (result.getStatus() == Response.Status.OK.getStatusCode()))) { + logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_SUCCESS, "POST", url, + Integer.toString(result.getStatus())); + metricsLogger.info(ModelLoaderMsgs.AAI_REST_REQUEST_SUCCESS, + new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, result.getStatus()) + .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, + result.getResponseStatus().toString()), + override, "POST", url, Integer.toString(result.getStatus())); + } else { + // If response is not 200 OK, then additionally log the reason + String respMsg = result.getEntity(String.class); + if (respMsg == null) { + respMsg = result.getStatusInfo().getReasonPhrase(); + } + logger.info(ModelLoaderMsgs.AAI_REST_REQUEST_UNSUCCESSFUL, "POST", url, + Integer.toString(result.getStatus()), respMsg); + metricsLogger.info(ModelLoaderMsgs.AAI_REST_REQUEST_UNSUCCESSFUL, + new LogFields().setField(LogLine.DefinedFields.RESPONSE_CODE, result.getStatus()) + .setField(LogLine.DefinedFields.RESPONSE_DESCRIPTION, + result.getResponseStatus().toString()), + override, "POST", url, Integer.toString(result.getStatus()), respMsg); + } + + return result; + } + + /** * Does a GET on a resource to retrieve the resource version, and then DELETE * that version. * diff --git a/src/main/java/org/openecomp/modelloader/service/ModelLoaderInterface.java b/src/main/java/org/openecomp/modelloader/service/ModelLoaderInterface.java index 689115e..99c5735 100644 --- a/src/main/java/org/openecomp/modelloader/service/ModelLoaderInterface.java +++ b/src/main/java/org/openecomp/modelloader/service/ModelLoaderInterface.java @@ -1,53 +1,55 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.modelloader.service; - -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.Response; - -public interface ModelLoaderInterface { - - @GET - @Path("/loadModel/{modelid}") - public Response loadModel(@PathParam("modelid") String modelid); - - @PUT - @Path("/saveModel/{modelid}/{modelname}") - public Response saveModel(@PathParam("modelid") String modelid, - @PathParam("modelname") String modelname); - - @POST - @Consumes("application/xml") - @Produces("application/xml") - @Path("/ingestModel/{modelid}") - public Response ingestModel(@PathParam("modelid") String modelid, @Context HttpServletRequest req, - String payload) throws IOException; -} +/**
+ * ============LICENSE_START=======================================================
+ * Model Loader
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ *
+ * ECOMP and OpenECOMP are trademarks
+ * and service marks of AT&T Intellectual Property.
+ */
+package org.openecomp.modelloader.service;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+
+public interface ModelLoaderInterface {
+
+ @GET
+ @Path("/loadModel/{modelid}")
+ public Response loadModel(@PathParam("modelid") String modelid);
+
+ @PUT
+ @Path("/saveModel/{modelid}/{modelname}")
+ public Response saveModel(@PathParam("modelid") String modelid,
+ @PathParam("modelname") String modelname);
+
+ @POST
+ @Consumes("application/xml")
+ @Produces("application/xml")
+ @Path("/ingestModel/{modelid}")
+ public Response ingestModel(@PathParam("modelid") String modelid, @Context HttpServletRequest req,
+ String payload) throws IOException;
+}
diff --git a/src/main/java/org/openecomp/modelloader/service/ModelLoaderMsgs.java b/src/main/java/org/openecomp/modelloader/service/ModelLoaderMsgs.java index b81c541..2d3137b 100644 --- a/src/main/java/org/openecomp/modelloader/service/ModelLoaderMsgs.java +++ b/src/main/java/org/openecomp/modelloader/service/ModelLoaderMsgs.java @@ -1,102 +1,110 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.modelloader.service; - -import com.att.eelf.i18n.EELFResourceManager; - -import org.openecomp.cl.eelf.LogMessageEnum; - -public enum ModelLoaderMsgs implements LogMessageEnum { - - /** - * Arguments: None. - */ - LOADING_CONFIGURATION, - - /** - * Arguments: None. - */ - STOPPING_CLIENT, - - /** - * Arguments: {0} = message. - */ - INITIALIZING, - - /** - * Arguments: {0} = reason. - */ - ASDC_CONNECTION_ERROR, - - /** - * Arguments: {0} = message. - */ - DISTRIBUTION_EVENT, - - /** - * Arguments: {0} = error message. - */ - DISTRIBUTION_EVENT_ERROR, - - /** - * Arguments: {0} = request type. - * {1} = endpoint. - * {2} = result code. - */ - AAI_REST_REQUEST_SUCCESS, - - /** - * Arguments: {0} = request type. - * {1} = endpoint. - * {2} = result code. - * {3} = result. - * message - */ - AAI_REST_REQUEST_UNSUCCESSFUL, - - /** - * Arguments: {0} = request type. - * {1} = endpoint. - * {2} = error message. - */ - AAI_REST_REQUEST_ERROR, - - /** - * Arguments: {0} = artifact name. - * {1} = result. - */ - DOWNLOAD_COMPLETE, - - /** - * Arguments: {0} = event. - * {1} = artifact name. - * {2} = result. - */ - EVENT_PUBLISHED; - - /** - * Load message bundle (ModelLoaderMsgs.properties file) - */ - static { - EELFResourceManager.loadMessageBundle("org/openecomp/modelloader/service/ModelLoaderMsgs"); - } - -} +/**
+ * ============LICENSE_START=======================================================
+ * Model Loader
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ *
+ * ECOMP and OpenECOMP are trademarks
+ * and service marks of AT&T Intellectual Property.
+ */
+package org.openecomp.modelloader.service;
+
+import com.att.eelf.i18n.EELFResourceManager;
+
+import org.openecomp.cl.eelf.LogMessageEnum;
+
+public enum ModelLoaderMsgs implements LogMessageEnum {
+
+ /**
+ * Arguments: None.
+ */
+ LOADING_CONFIGURATION,
+
+ /**
+ * Arguments: None.
+ */
+ STOPPING_CLIENT,
+
+ /**
+ * Arguments: {0} = message.
+ */
+ INITIALIZING,
+
+ /**
+ * Arguments: {0} = reason.
+ */
+ ASDC_CONNECTION_ERROR,
+
+ /**
+ * Arguments: {0} = message.
+ */
+ DISTRIBUTION_EVENT,
+
+ /**
+ * Arguments: {0} = error message.
+ */
+ DISTRIBUTION_EVENT_ERROR,
+
+ /**
+ * Arguments: {0} = request type.
+ * {1} = endpoint.
+ * {2} = result code.
+ */
+ AAI_REST_REQUEST_SUCCESS,
+
+ /**
+ * Arguments: {0} = request type.
+ * {1} = endpoint.
+ * {2} = result code.
+ * {3} = result.
+ * message
+ */
+ AAI_REST_REQUEST_UNSUCCESSFUL,
+
+ /**
+ * Arguments: {0} = request type.
+ * {1} = endpoint.
+ * {2} = error message.
+ */
+ AAI_REST_REQUEST_ERROR,
+
+ /**
+ * Arguments:
+ * {0} = artifact name
+ */
+ ARTIFACT_PARSE_ERROR,
+
+ /**
+ * Arguments: {0} = artifact name.
+ * {1} = result.
+ */
+ DOWNLOAD_COMPLETE,
+
+ /**
+ * Arguments: {0} = event.
+ * {1} = artifact name.
+ * {2} = result.
+ */
+ EVENT_PUBLISHED;
+
+ /**
+ * Load message bundle (ModelLoaderMsgs.properties file)
+ */
+ static {
+ EELFResourceManager.loadMessageBundle("org/openecomp/modelloader/service/ModelLoaderMsgs");
+ }
+
+}
diff --git a/src/main/java/org/openecomp/modelloader/service/ModelLoaderService.java b/src/main/java/org/openecomp/modelloader/service/ModelLoaderService.java index 16aba7e..786f1a2 100644 --- a/src/main/java/org/openecomp/modelloader/service/ModelLoaderService.java +++ b/src/main/java/org/openecomp/modelloader/service/ModelLoaderService.java @@ -1,23 +1,25 @@ -/*- +/** * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE + * Model Loader * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. */ - package org.openecomp.modelloader.service; import org.openecomp.sdc.api.IDistributionClient; @@ -31,11 +33,13 @@ import org.openecomp.modelloader.config.ModelLoaderConfig; import org.openecomp.modelloader.entity.model.ModelArtifactHandler; import org.openecomp.modelloader.notification.EventCallback; +import java.io.FileInputStream; import java.io.IOException; +import java.util.Date; import java.util.Properties; +import java.util.Timer; +import java.util.TimerTask; -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Response; @@ -44,131 +48,157 @@ import javax.ws.rs.core.Response; * capabilities between AAI and an ASDC. */ public class ModelLoaderService implements ModelLoaderInterface { - - protected static String CONFIG_FILE = "model-loader.properties"; - - private IDistributionClient client; - private ModelLoaderConfig config; - - static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName()); - - /** - * Responsible for loading configuration files and calling initialization. - */ - @PostConstruct - protected void start() { - // Load model loader system configuration - logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION); - Properties configProperties = new Properties(); - try { - configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); - } catch (IOException e) { - String errorMsg = "Failed to load configuration: " + e.getMessage(); - logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg); - shutdown(); - } - - config = new ModelLoaderConfig(configProperties); - init(); - } - - /** - * Responsible for stopping the connection to the distribution client before - * the resource is destroyed. - */ - @PreDestroy - protected void preShutdownOperations() { - logger.info(ModelLoaderMsgs.STOPPING_CLIENT); - if (client != null) { - client.stop(); - } - } - - /** - * Responsible for loading configuration files, initializing model - * distribution clients, and starting them. - */ - protected void init() { - // Initialize distribution client - logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client..."); - client = DistributionClientFactory.createDistributionClient(); - IDistributionClientResult initResult = client.init(config, new EventCallback(client, config)); - if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { - String errorMsg = "Failed to initialize distribution client: " - + initResult.getDistributionMessageResult(); - logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg); - shutdown(); - } - - // Start distribution client - logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client..."); - IDistributionClientResult startResult = client.start(); - if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { - String errorMsg = "Failed to start distribution client: " - + startResult.getDistributionMessageResult(); - logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg); - shutdown(); - } - - logger.debug(ModelLoaderMsgs.INITIALIZING, - "Succcessfully loaded service: " + this.getClass().getSimpleName()); - } - - /** - * Shut down the process. - */ - private void shutdown() { - preShutdownOperations(); - - // TODO: Find a better way to shut down the model loader. - try { - // Give logs time to write to file - Thread.sleep(2000); - } catch (InterruptedException e) { - // Nothing we can do at this point - } - - Runtime.getRuntime().halt(1); - } - - /** (non-Javadoc) - * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String) - */ - @Override - public Response loadModel(String modelid) { - Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build(); - - return response; - } - - /** (non-Javadoc) - * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String) - */ - @Override - public Response saveModel(String modelid, String modelname) { - Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}") - .build(); - - return response; - } - - @Override - public Response ingestModel(String modelid, HttpServletRequest req, String payload) - throws IOException { - Response response; - - if (config.getIngestSimulatorEnabled()) { - logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact"); - - ModelArtifactHandler handler = new ModelArtifactHandler(config); - handler.loadModelTest(payload.getBytes()); - - response = Response.ok().build(); - } else { - logger.debug("Simulation interface disabled"); - response = Response.serverError().build(); - } - - return response; - } + + protected static final String FILESEP = (System.getProperty("file.separator") == null) ? "/" + : System.getProperty("file.separator"); + + protected static final String CONFIG_DIR = System.getProperty("CONFIG_HOME") + FILESEP; + protected static final String CONFIG_AUTH_LOCATION = CONFIG_DIR + "auth" + FILESEP; + protected static final String CONFIG_FILE = CONFIG_DIR + "model-loader.properties"; + + private IDistributionClient client; + private ModelLoaderConfig config; + private Timer timer = null; + + static Logger logger = LoggerFactory.getInstance().getLogger(ModelLoaderService.class.getName()); + + /** + * Responsible for loading configuration files and calling initialization. + */ + public ModelLoaderService() { + start(); + } + + protected void start() { + // Load model loader system configuration + logger.info(ModelLoaderMsgs.LOADING_CONFIGURATION); + Properties configProperties = new Properties(); + try { + configProperties.load(new FileInputStream(CONFIG_FILE)); + } catch (IOException e) { + String errorMsg = "Failed to load configuration: " + e.getMessage(); + logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg); + shutdown(); + } + + config = new ModelLoaderConfig(configProperties, CONFIG_AUTH_LOCATION); + init(); + + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){ + public void run() { + preShutdownOperations(); + } + })); + } + + /** + * Responsible for stopping the connection to the distribution client before + * the resource is destroyed. + */ + protected void preShutdownOperations() { + logger.info(ModelLoaderMsgs.STOPPING_CLIENT); + if (client != null) { + client.stop(); + } + } + + /** + * Responsible for loading configuration files, initializing model + * distribution clients, and starting them. + */ + protected void init() { + // Initialize distribution client + logger.debug(ModelLoaderMsgs.INITIALIZING, "Initializing distribution client..."); + client = DistributionClientFactory.createDistributionClient(); + EventCallback callback = new EventCallback(client, config); + + IDistributionClientResult initResult = client.init(config, callback); + + if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { + String errorMsg = "Failed to initialize distribution client: " + + initResult.getDistributionMessageResult(); + logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg); + + // Kick off a timer to retry the SDC connection + timer = new Timer(); + TimerTask task = new SdcConnectionJob(client, config, callback, timer); + timer.schedule(task, new Date(), 60000); + } + else { + // Start distribution client + logger.debug(ModelLoaderMsgs.INITIALIZING, "Starting distribution client..."); + IDistributionClientResult startResult = client.start(); + if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { + String errorMsg = "Failed to start distribution client: " + + startResult.getDistributionMessageResult(); + logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg); + + // Kick off a timer to retry the SDC connection + timer = new Timer(); + TimerTask task = new SdcConnectionJob(client, config, callback, timer); + timer.schedule(task, new Date(), 60000); + } + else { + logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established"); + } + } + } + + /** + * Shut down the process. + */ + private void shutdown() { + preShutdownOperations(); + + // TODO: Find a better way to shut down the model loader. + try { + // Give logs time to write to file + Thread.sleep(2000); + } catch (InterruptedException e) { + // Nothing we can do at this point + } + + Runtime.getRuntime().halt(1); + } + + /** (non-Javadoc) + * @see org.openecomp.modelloader.service.ModelLoaderInterface#loadModel(java.lang.String) + */ + @Override + public Response loadModel(String modelid) { + Response response = Response.ok("{\"model_loaded\":\"" + modelid + "\"}").build(); + + return response; + } + + /** (non-Javadoc) + * @see org.openecomp.modelloader.service.ModelLoaderInterface#saveModel(java.lang.String, java.lang.String) + */ + @Override + public Response saveModel(String modelid, String modelname) { + Response response = Response.ok("{\"model_saved\":\"" + modelid + "-" + modelname + "\"}") + .build(); + + return response; + } + + @Override + public Response ingestModel(String modelid, HttpServletRequest req, String payload) + throws IOException { + Response response; + + if (config.getIngestSimulatorEnabled()) { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Received test artifact"); + + ModelArtifactHandler handler = new ModelArtifactHandler(config); + handler.loadModelTest(payload.getBytes()); + + response = Response.ok().build(); + } else { + logger.debug("Simulation interface disabled"); + response = Response.serverError().build(); + } + + return response; + } } diff --git a/src/main/java/org/openecomp/modelloader/service/SdcConnectionJob.java b/src/main/java/org/openecomp/modelloader/service/SdcConnectionJob.java new file mode 100644 index 0000000..bb3741c --- /dev/null +++ b/src/main/java/org/openecomp/modelloader/service/SdcConnectionJob.java @@ -0,0 +1,79 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.service; + +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +import org.openecomp.cl.api.Logger; +import org.openecomp.cl.eelf.LoggerFactory; +import org.openecomp.modelloader.config.ModelLoaderConfig; +import org.openecomp.modelloader.notification.EventCallback; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.results.IDistributionClientResult; +import org.openecomp.sdc.utils.DistributionActionResultEnum; + +public class SdcConnectionJob extends TimerTask { + static Logger logger = LoggerFactory.getInstance().getLogger(SdcConnectionJob.class.getName()); + + private IDistributionClient client; + private ModelLoaderConfig config; + private EventCallback callback; + private Timer timer; + + public SdcConnectionJob(IDistributionClient client, + ModelLoaderConfig config, + EventCallback callback, + Timer timer) { + this.client = client; + this.timer = timer; + this.callback = callback; + this.config = config; + } + + @Override + public void run() { + + IDistributionClientResult initResult = client.init(config, callback); + + if (initResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { + String errorMsg = "Failed to initialize distribution client: " + + initResult.getDistributionMessageResult(); + logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg); + return; + } + + IDistributionClientResult startResult = client.start(); + if (startResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { + String errorMsg = "Failed to start distribution client: " + + startResult.getDistributionMessageResult(); + logger.error(ModelLoaderMsgs.ASDC_CONNECTION_ERROR, errorMsg); + return; + } + + // Success. Cancel the timer job + timer.cancel(); + logger.info(ModelLoaderMsgs.INITIALIZING, "Connection to SDC established"); + } +} diff --git a/src/main/java/org/openecomp/modelloader/util/JsonXmlConverter.java b/src/main/java/org/openecomp/modelloader/util/JsonXmlConverter.java index ca63b23..621f704 100644 --- a/src/main/java/org/openecomp/modelloader/util/JsonXmlConverter.java +++ b/src/main/java/org/openecomp/modelloader/util/JsonXmlConverter.java @@ -1,79 +1,81 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.modelloader.util; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.XML; - -public class JsonXmlConverter { - - /** - * Determines whether or not the supplied text string represents a valid - * JSON structure or not. - * - * @param text - The text to be evaluated. - * - * @return - true if the string represents a valid JSON object, - * false, otherwise. - */ - public static boolean isValidJson(String text) { - try { - new JSONObject(text); - } catch (JSONException ex) { - try { - new JSONArray(text); - } catch (JSONException ex1) { - return false; - } - } - - return true; - } - - /** - * Takes a text string representing a valid JSON structure and converts it to - * an equivalent XML string. - * - * @param jsonText - The JSON string to convert to XML. - * - * @return - An XML string representation of the supplied JSON string. - */ - public static String convertJsonToXml(String jsonText) { - JSONObject jsonObj = new JSONObject(jsonText); - String xmlText = XML.toString(jsonObj); - return xmlText; - } - - /** - * Takes a text string representing a valid XML structure and converts it to - * an equivalent JSON string. - * - * @param xmlText - The XML string to convert to JSON. - * - * @return - A JSON string representation of the supplied XML string. - */ - public static String convertXmlToJson(String xmlText) { - JSONObject jsonObj = XML.toJSONObject(xmlText); - return jsonObj.toString(); - } -} +/**
+ * ============LICENSE_START=======================================================
+ * Model Loader
+ * ================================================================================
+ * Copyright © 2017 AT&T Intellectual Property.
+ * Copyright © 2017 Amdocs
+ * All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ *
+ * ECOMP and OpenECOMP are trademarks
+ * and service marks of AT&T Intellectual Property.
+ */
+package org.openecomp.modelloader.util;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.XML;
+
+public class JsonXmlConverter {
+
+ /**
+ * Determines whether or not the supplied text string represents a valid
+ * JSON structure or not.
+ *
+ * @param text - The text to be evaluated.
+ *
+ * @return - true if the string represents a valid JSON object,
+ * false, otherwise.
+ */
+ public static boolean isValidJson(String text) {
+ try {
+ new JSONObject(text);
+ } catch (JSONException ex) {
+ try {
+ new JSONArray(text);
+ } catch (JSONException ex1) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ /**
+ * Takes a text string representing a valid JSON structure and converts it to
+ * an equivalent XML string.
+ *
+ * @param jsonText - The JSON string to convert to XML.
+ *
+ * @return - An XML string representation of the supplied JSON string.
+ */
+ public static String convertJsonToXml(String jsonText) {
+ JSONObject jsonObj = new JSONObject(jsonText);
+ String xmlText = XML.toString(jsonObj);
+ return xmlText;
+ }
+
+ /**
+ * Takes a text string representing a valid XML structure and converts it to
+ * an equivalent JSON string.
+ *
+ * @param xmlText - The XML string to convert to JSON.
+ *
+ * @return - A JSON string representation of the supplied XML string.
+ */
+ public static String convertXmlToJson(String xmlText) {
+ JSONObject jsonObj = XML.toJSONObject(xmlText);
+ return jsonObj.toString();
+ }
+}
|