From 38713207b817ed4700f630748c681116338d32f0 Mon Sep 17 00:00:00 2001 From: Patrick Brady Date: Fri, 28 Jul 2017 11:29:23 -0700 Subject: Merging in bug fixes Change-Id: I85da90b9ba0d03149f42035a820c76ef0285e413 Signed-off-by: Patrick Brady Issue: APPC-100 --- .../appc-asdc-listener-bundle/pom.xml | 55 ++++- .../appc/sdc/artifacts/ArtifactProcessor.java | 41 ++++ .../artifacts/helper/ArtifactStorageService.java | 180 ++++++++++++++++ .../artifacts/impl/AbstractArtifactProcessor.java | 196 ++++++++++++++++++ .../artifacts/impl/ArtifactProcessorFactory.java | 89 ++++++++ .../artifacts/impl/ConfigArtifactProcessor.java | 76 +++++++ .../artifacts/impl/LicenseArtifactProcessor.java | 92 +++++++++ .../artifacts/impl/ToscaCsarArtifactProcessor.java | 210 +++++++++++++++++++ .../appc/sdc/artifacts/object/ArtifactType.java | 46 +++++ .../appc/sdc/artifacts/object/Resource.java | 87 ++++++++ .../appc/sdc/artifacts/object/SDCArtifact.java | 215 +++++++++++++++++++ .../openecomp/appc/sdc/listener/AsdcCallback.java | 26 ++- .../openecomp/appc/sdc/listener/AsdcConfig.java | 6 +- .../appc/sdc/listener/DownloadAndStoreOp.java | 229 --------------------- .../appc/sdc/listener/TestAsdcListener.java | 194 +++++++++++++++++ .../resources/csar/service-ServiceAppc-csar.csar | Bin 0 -> 9782 bytes appc-asdc-listener/appc-yang-generator/pom.xml | 34 ++- .../appc/yang/impl/YANGGeneratorImpl.java | 4 +- 18 files changed, 1537 insertions(+), 243 deletions(-) create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/ArtifactProcessor.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/helper/ArtifactStorageService.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/AbstractArtifactProcessor.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ArtifactProcessorFactory.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ConfigArtifactProcessor.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/LicenseArtifactProcessor.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ToscaCsarArtifactProcessor.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/ArtifactType.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/Resource.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/SDCArtifact.java delete mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/DownloadAndStoreOp.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/TestAsdcListener.java create mode 100644 appc-asdc-listener/appc-asdc-listener-bundle/src/test/resources/csar/service-ServiceAppc-csar.csar (limited to 'appc-asdc-listener') diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/pom.xml b/appc-asdc-listener/appc-asdc-listener-bundle/pom.xml index 607ab7745..35b9b3180 100644 --- a/appc-asdc-listener/appc-asdc-listener-bundle/pom.xml +++ b/appc-asdc-listener/appc-asdc-listener-bundle/pom.xml @@ -27,11 +27,33 @@ json compile + + org.mockito + mockito-all + test + + + + org.mockito + mockito-core + test + + + org.powermock + powermock-module-junit4 + 1.6.2 + test + + + org.powermock + powermock-api-mockito + test + - org.openecomp.sdc + org.openecomp.sdc.sdc-distribution-client sdc-distribution-client - 1.0.0 + ${sdc-client.version} @@ -40,6 +62,12 @@ ${project.version} + + org.openecomp.appc + appc-message-adapter-factory + ${project.version} + + org.yaml snakeyaml @@ -91,7 +119,7 @@ org.openecomp.appc appc-license-manager-api - 1.1.0-SNAPSHOT + ${project.version} @@ -99,11 +127,11 @@ gson - - org.openecomp.sdc - openecomp-tosca-lib - ${toscalib.version} - + + org.openecomp.sdc.common + openecomp-tosca-datatype + ${toscalib.version} + org.apache.velocity @@ -127,7 +155,16 @@ ietf-yang-types ${odl.ietf-yang-types.version} - + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + ${jackson.yml.version} + + + org.mockito + mockito-core + + diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/ArtifactProcessor.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/ArtifactProcessor.java new file mode 100644 index 000000000..c0f52206f --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/ArtifactProcessor.java @@ -0,0 +1,41 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts; + +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; + +/** + * This interface provides api processArtifact which cab have multiple implementation + * for type of artifact app-c receives from sdc + */ +public interface ArtifactProcessor extends Runnable { + /** + * Processes the artifact received from sdc + * @param result an instance of IDistributionClientDownloadResult + * @throws APPCException + */ + void processArtifact(IDistributionClientDownloadResult result) throws APPCException; +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/helper/ArtifactStorageService.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/helper/ArtifactStorageService.java new file mode 100644 index 000000000..6bae9695a --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/helper/ArtifactStorageService.java @@ -0,0 +1,180 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.helper; + +import org.openecomp.appc.exceptions.APPCException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.openecomp.sdnc.sli.resource.dblib.DbLibService; +import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; + +import javax.sql.rowset.CachedRowSet; +import java.sql.SQLException; +import java.util.ArrayList; + +import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS; +import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS.*; + +/** + * Provides methods for storing sdc artifacts into app-c database + */ +public class ArtifactStorageService { + + private DbLibService dbLibService; + + private static final String COMMA = " , "; + private static final String QUERY_PLACEHOLDER = " = ? "; + + private static final String SCHEMA = "sdnctl"; + + private static final String SELECT_QUERY = "SELECT * FROM " + ASDC_ARTIFACTS + + " WHERE " + RESOURCE_NAME + QUERY_PLACEHOLDER + + " AND " + RESOURCE_VERSION + QUERY_PLACEHOLDER + + " AND " + ARTIFACT_TYPE + QUERY_PLACEHOLDER; + + private static final String INSERT_QUERY = "INSERT INTO " + ASDC_ARTIFACTS + + " ( " + SERVICE_UUID + COMMA + + DISTRIBUTION_ID + COMMA + + SERVICE_NAME + COMMA + + SERVICE_DESCRIPTION + COMMA + + RESOURCE_UUID + COMMA + + RESOURCE_INSTANCE_NAME + COMMA + + RESOURCE_NAME + COMMA + + RESOURCE_VERSION + COMMA + + RESOURCE_TYPE + COMMA + + ARTIFACT_UUID + COMMA + + ARTIFACT_TYPE + COMMA + + ARTIFACT_VERSION + COMMA + + ARTIFACT_DESCRIPTION + COMMA + + CREATION_DATE + COMMA + + ARTIFACT_NAME +COMMA + + ARTIFACT_CONTENT + " ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; + + + private final EELFLogger logger = EELFManager.getInstance().getLogger(ArtifactStorageService.class); + + /** + * Stores Artifact received from SDC into APP-C database + * @param artifact + * @throws APPCException + */ + public void storeASDCArtifact(SDCArtifact artifact) throws APPCException { + if(logger.isDebugEnabled()){ + logger.debug("Entering storeASDCArtifact with : " + artifact.toString()); + } + try { + initializeDBLibService(); + ArrayList arguments = prepareArguments(artifact); + dbLibService.writeData(INSERT_QUERY,arguments,SCHEMA); + } catch (SQLException e) { + logger.error("Error storing artifact in database : " +artifact.toString(),e); + throw new APPCException(e.getMessage(),e); + } + if(logger.isDebugEnabled()){ + logger.debug("Exiting storeASDCArtifact"); + } + } + + private void initializeDBLibService() { + if(dbLibService == null){ + BundleContext context = FrameworkUtil.getBundle(DbLibService.class).getBundleContext(); + ServiceReference serviceReference = context.getServiceReference(DbLibService.class.getName()); + dbLibService = (DbLibService)context.getService(serviceReference); + } + } + + private ArrayList prepareArguments(SDCArtifact artifact) { + ArrayList arguments = new ArrayList<>(); + arguments.add(artifact.getServiceUUID()); + arguments.add(artifact.getDistributionId()); + arguments.add(artifact.getServiceName()); + arguments.add(artifact.getServiceDescription()); + arguments.add(artifact.getResourceUUID()); + arguments.add(artifact.getResourceInstanceName()); + arguments.add(artifact.getResourceName()); + arguments.add(artifact.getResourceVersion()); + arguments.add(artifact.getResourceType()); + arguments.add(artifact.getArtifactUUID()); + arguments.add(artifact.getArtifactType()); + arguments.add(artifact.getArtifactVersion()); + arguments.add(artifact.getArtifactDescription()); + arguments.add(artifact.getCreationDate()); + arguments.add(artifact.getArtifactName()); + arguments.add(artifact.getArtifactContent()); + return arguments; + } + + /** + * reads the SDC artifact from APP-C database + * @param resourceName + * @param resourceVersion + * @param artifactType + * @return + * @throws APPCException + */ + public SDCArtifact retrieveSDCArtifact(String resourceName, String resourceVersion, String artifactType) throws APPCException { + SDCArtifact artifact = null; + try { + initializeDBLibService(); + ArrayList arguments = new ArrayList<>(); + arguments.add(resourceName); + arguments.add(resourceVersion); + arguments.add(artifactType); + CachedRowSet rowSet = dbLibService.getData(SELECT_QUERY, arguments, SCHEMA); + if (rowSet.first()) { + artifact = new SDCArtifact(); + artifact.setArtifactUUID(rowSet.getString(ARTIFACT_UUID.toString())); + artifact.setArtifactName(rowSet.getString(ARTIFACT_NAME.toString())); + artifact.setArtifactType(rowSet.getString(ARTIFACT_TYPE.toString())); + artifact.setArtifactVersion(rowSet.getString(ARTIFACT_VERSION.toString())); + artifact.setArtifactDescription(rowSet.getString(ARTIFACT_DESCRIPTION.toString())); + artifact.setArtifactContent(rowSet.getString(ARTIFACT_CONTENT.toString())); + + artifact.setResourceUUID(rowSet.getString(RESOURCE_UUID.toString())); + artifact.setResourceName(rowSet.getString(RESOURCE_NAME.toString())); + artifact.setResourceType(rowSet.getString(RESOURCE_TYPE.toString())); + artifact.setResourceVersion(rowSet.getString(RESOURCE_VERSION.toString())); + artifact.setResourceInstanceName(rowSet.getString(RESOURCE_INSTANCE_NAME.toString())); + + artifact.setServiceUUID(rowSet.getString(SERVICE_UUID.toString())); + artifact.setServiceName(rowSet.getString(SERVICE_NAME.toString())); + artifact.setServiceDescription(rowSet.getString(SERVICE_DESCRIPTION.toString())); + + artifact.setCreationDate(rowSet.getString(CREATION_DATE.toString())); + artifact.setDistributionId(rowSet.getString(DISTRIBUTION_ID.toString())); + } + + } catch (SQLException e) { + logger.error("Error query artifact for " + RESOURCE_NAME + " = " + resourceName + + RESOURCE_VERSION + " = " + resourceVersion + + ARTIFACT_TYPE + " = " + artifactType, e); + throw new APPCException(e); + } + return artifact; + } +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/AbstractArtifactProcessor.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/AbstractArtifactProcessor.java new file mode 100644 index 000000000..12fd260d0 --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/AbstractArtifactProcessor.java @@ -0,0 +1,196 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.impl; + +import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.adapter.message.MessageDestination; +import org.openecomp.appc.adapter.message.event.EventHeader; +import org.openecomp.appc.adapter.message.event.EventMessage; +import org.openecomp.appc.adapter.message.event.EventStatus; +import org.openecomp.appc.sdc.listener.Util; +import org.openecomp.appc.exceptions.APPCException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.openecomp.appc.sdc.artifacts.ArtifactProcessor; +import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService; +import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; +import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; +import org.openecomp.sdc.utils.DistributionActionResultEnum; +import org.openecomp.sdc.utils.DistributionStatusEnum; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Provides abstrace implementation for SDC artifact processor + */ +public abstract class AbstractArtifactProcessor implements ArtifactProcessor { + + public static final String PAYLOAD_CHARSET = "UTF-8"; + private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss"; + + protected IDistributionClient client; + protected EventSender eventSender; + + protected INotificationData notification; + protected IResourceInstance resource; + protected IArtifactInfo artifact; + protected URI storeUri; + + private final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractArtifactProcessor.class); + + protected ArtifactStorageService artifactStorageService; + + private AbstractArtifactProcessor(){ + artifactStorageService = new ArtifactStorageService(); + } + + AbstractArtifactProcessor(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource, + IArtifactInfo artifact, URI storeUri){ + + this(); + this.client = client; + this.eventSender = eventSender; + this.notification = notification; + this.resource = resource; + this.artifact = artifact; + this.storeUri = storeUri; + } + + @Override + public void run(){ + + try{ + logger.info(String.format("Attempting to download artifact %s", artifact)); + // Download artifact + IDistributionClientDownloadResult download = client.download(artifact); + + logger.info(String.format("Download of artifact %s completed with status %s", artifact.getArtifactUUID(), download)); + + // Notify of download status + if (download.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { + client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact, + DistributionStatusEnum.DOWNLOAD_ERROR), download.getDistributionMessageResult()); + sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), "Download is failed."); + return; + } + + client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact, DistributionStatusEnum.DOWNLOAD_OK)); + + processArtifact(download); + + client.sendDeploymentStatus( + Util.buildDistributionStatusMessage(client, notification, this.artifact, DistributionStatusEnum.DEPLOY_OK)); + } + catch (Exception e){ + logger.error("Error processing artifact " + this.artifact.toString() ,e); + + client.sendDeploymentStatus(Util.buildDistributionStatusMessage(client, notification, artifact, + DistributionStatusEnum.DEPLOY_ERROR), e.getMessage()); + sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), e.getMessage()); + } + } + + + @Override + public void processArtifact(IDistributionClientDownloadResult download) throws APPCException { + String data = null; + if(logger.isDebugEnabled()){ + logger.debug("Entry processArtifact in AbstractArtifactProcessor"); + } + try { + if (download.getArtifactPayload() != null) { + data = new String(download.getArtifactPayload(), PAYLOAD_CHARSET); + } + } catch (UnsupportedEncodingException e) { + logger.error("Error reading artifact with " + PAYLOAD_CHARSET + " encoding" + new String(download.getArtifactPayload()) ,e); + throw new APPCException(e); + } + + SDCArtifact sdcArtifact = getArtifactObject(data); + logger.debug("Constructed SDCArtifact = " + sdcArtifact); + processArtifact(sdcArtifact); + + if(logger.isDebugEnabled()){ + logger.debug("Exit processArtifact in AbstractArtifactProcessor"); + } + } + + protected abstract void processArtifact(SDCArtifact artifact) throws APPCException; + + protected SDCArtifact getArtifactObject(String data){ + + SDCArtifact sdcArtifact = new SDCArtifact(); + + sdcArtifact.setArtifactUUID(this.artifact.getArtifactUUID()); + sdcArtifact.setArtifactName(this.artifact.getArtifactName()); + sdcArtifact.setArtifactType(this.artifact.getArtifactType()); + sdcArtifact.setArtifactVersion(this.artifact.getArtifactVersion()); + sdcArtifact.setArtifactDescription(this.artifact.getArtifactDescription()); + sdcArtifact.setArtifactContent(data); + sdcArtifact.setCreationDate(getCurrentDateTime()); + + sdcArtifact.setDistributionId(this.notification.getDistributionID()); + sdcArtifact.setServiceUUID(this.notification.getServiceUUID()); + sdcArtifact.setServiceName(this.notification.getServiceName()); + sdcArtifact.setServiceDescription(this.notification.getServiceDescription()); + + sdcArtifact.setResourceName(this.resource.getResourceName()); + sdcArtifact.setResourceType(this.resource.getResourceType()); + sdcArtifact.setResourceVersion(this.resource.getResourceVersion()); + sdcArtifact.setResourceUUID(this.resource.getResourceUUID()); + sdcArtifact.setResourceInstanceName(this.resource.getResourceInstanceName()); + + return sdcArtifact; + } + + protected String getCurrentDateTime() { + DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); + Date date = new Date(); + return dateFormat.format(date); + } + + private void sendDCAEEvent(String distributionID, String serviceName, String serviceVersion, String errorMessage) { + if (null == eventSender){ + return; + } + String errorDescription = String.format("ASDC distribution of service '%s', version '%s' is failed with reason: '%s'", + serviceName, serviceVersion, errorMessage); + + EventMessage eventMessage = new EventMessage( + new EventHeader((new Date()).toString(), serviceVersion, distributionID), + new EventStatus(401, errorDescription)); + + eventSender.sendEvent(MessageDestination.DCAE, eventMessage); + } + +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ArtifactProcessorFactory.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ArtifactProcessorFactory.java new file mode 100644 index 000000000..aa9d0e04b --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ArtifactProcessorFactory.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.impl; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.sdc.artifacts.ArtifactProcessor; +import org.openecomp.appc.sdc.artifacts.object.ArtifactType; +import org.openecomp.appc.sdc.listener.AsdcListener; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; + +import java.net.URI; + +/** + * Factory class for creating instance of Artifact Processor + */ +public class ArtifactProcessorFactory { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(ArtifactProcessorFactory.class); + + private ArtifactProcessorFactory (){ + + } + + /** + * Provides and instance of Artifact Processor + * @param client an instance of IDistributionClient + * @param eventSender an instance of EventSender + * @param notification an instance of INotificationData + * @param resource an instance of IResourceInstance + * @param artifact an instance of IArtifactInfo + * @param storeUri + * @return + */ + public static ArtifactProcessor getArtifactProcessor(IDistributionClient client, EventSender eventSender, + INotificationData notification, IResourceInstance resource, + IArtifactInfo artifact, URI storeUri) { + + logger.debug("Creating artifact processor for artifact type = " + artifact.getArtifactType()); + ArtifactType artifactType = ArtifactType.getArtifactType(artifact.getArtifactType()); + if(artifactType == null){ + return null; + } + ArtifactProcessor artifactProcessor = null; + switch (artifactType){ + case APPC_CONFIG : + artifactProcessor = new ConfigArtifactProcessor(client, eventSender, notification, resource, + artifact, storeUri); + break; + case VF_LICENSE: + artifactProcessor = new LicenseArtifactProcessor(client,eventSender,notification,resource, + artifact,storeUri); + break; + case TOSCA_CSAR: + artifactProcessor = new ToscaCsarArtifactProcessor(client,eventSender,notification,resource, + artifact,storeUri); + default: + break; + } + return artifactProcessor; + } + +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ConfigArtifactProcessor.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ConfigArtifactProcessor.java new file mode 100644 index 000000000..3667b6b2a --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ConfigArtifactProcessor.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.impl; + +import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.sdc.listener.ProviderOperations; +import org.openecomp.appc.sdc.listener.ProviderResponse; +import org.openecomp.appc.sdc.listener.Util; +import org.openecomp.appc.exceptions.APPCException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; + +import java.net.MalformedURLException; +import java.net.URI; + +/** + * Artifact processor for config artifact type + */ +public class ConfigArtifactProcessor extends AbstractArtifactProcessor { + + private final EELFLogger logger = EELFManager.getInstance().getLogger(ConfigArtifactProcessor.class); + + /** + * returns an instance of ConfigArtifactProcessor + * @param client an instance of IDistributionClient + * @param eventSender an instance of EventSender + * @param notification an instance of INotificationData + * @param resource an instance of IResourceInstance + * @param artifact an instance of IArtifactInfo + * @param storeUri an instance of URI + */ + public ConfigArtifactProcessor(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource, IArtifactInfo artifact, URI storeUri) { + super(client,eventSender,notification,resource,artifact,storeUri); + } + + @Override + public void processArtifact(SDCArtifact artifact) throws APPCException { + String postData = Util.toAsdcStoreDocumentInput(notification, resource, super.artifact, artifact.getArtifactContent()); + try { + ProviderResponse result = ProviderOperations.post(storeUri.toURL(), postData, null); + if (result.getStatus() == 200) { + Util.parseResponse(result.getBody()); + } + } catch (MalformedURLException | APPCException e) { + logger.error("Error processing artifact : " + this.artifact.toString(),e); + throw new APPCException(e.getMessage(),e); + } + } +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/LicenseArtifactProcessor.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/LicenseArtifactProcessor.java new file mode 100644 index 000000000..9cbc56276 --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/LicenseArtifactProcessor.java @@ -0,0 +1,92 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.impl; + +import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.appc.licmgr.Constants; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.apache.commons.lang.StringUtils; +import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; + +import java.net.URI; + +/** + * Artifact processor for VNF license artifact type + */ +public class LicenseArtifactProcessor extends AbstractArtifactProcessor { + + private final EELFLogger logger = EELFManager.getInstance().getLogger(LicenseArtifactProcessor.class); + + /** + * returns an instance of ConfigArtifactProcessor + * @param client an instance of IDistributionClient + * @param eventSender an instance of EventSender + * @param notification an instance of INotificationData + * @param resource an instance of IResourceInstance + * @param artifact an instance of IArtifactInfo + * @param storeUri an instance of URI + */ + public LicenseArtifactProcessor(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource, IArtifactInfo artifact, URI storeUri) { + super(client,eventSender,notification,resource,artifact,storeUri); + } + + @Override + public void processArtifact(SDCArtifact artifact) throws APPCException { + + String vnfType = artifact.getResourceName(); + String version = artifact.getResourceVersion(); + String packageArtifactID = artifact.getArtifactUUID(); + + if (StringUtils.isEmpty(vnfType) || + StringUtils.isEmpty(version) || + StringUtils.isEmpty(packageArtifactID)) { + String errStr = String.format("Missing information in ASDC request. Details: resource_type='%s', resource_version='%s', artifactID='%s'", vnfType, version, packageArtifactID); + logger.error(errStr); + throw new APPCException(errStr); + } + + try { + SDCArtifact existingArtifact = artifactStorageService.retrieveSDCArtifact(vnfType, version,artifact.getArtifactType()); + + if (existingArtifact ==null) { // new resource + logger.debug("Artifact not found from database for vnfType = " + vnfType + " , version = " + version + " , artifactType = " + artifact.getArtifactType()); + artifactStorageService.storeASDCArtifact(artifact); + } else { // duplicate + logger.debug("Artifact retrieved from database = " + existingArtifact); + logger.warn(String.format("Artifact of type '%s' already deployed for resource_type='%s' and resource_version='%s'", Constants.VF_LICENSE, vnfType, version)); + } + + } catch (Exception e) { + logger.error("Error processing artifact : " + artifact.toString(),e); + throw new APPCException(e.getMessage(),e); + } + } +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ToscaCsarArtifactProcessor.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ToscaCsarArtifactProcessor.java new file mode 100644 index 000000000..5379d463e --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/impl/ToscaCsarArtifactProcessor.java @@ -0,0 +1,210 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.impl; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import org.apache.commons.lang.StringUtils; +import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.exceptions.APPCException; +import org.openecomp.appc.licmgr.Constants; +import org.openecomp.appc.sdc.artifacts.object.Resource; +import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; +import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; + +import javax.json.Json; +import java.io.*; +import java.net.URI; +import java.util.*; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +public class ToscaCsarArtifactProcessor extends AbstractArtifactProcessor{ + + private final EELFLogger logger = EELFManager.getInstance().getLogger(ToscaCsarArtifactProcessor.class); + + public ToscaCsarArtifactProcessor(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource, + IArtifactInfo artifact, URI storeUri){ + super(client,eventSender,notification,resource,artifact,storeUri); + } + + @Override + public void processArtifact(IDistributionClientDownloadResult download) throws APPCException { + logger.debug("processing artifact " + super.artifact.getArtifactType()); + byte[] byteArray = download.getArtifactPayload(); + String serviceFileName = ""; + String serviceTemplateContent = ""; + List resources = null; + Map csarFiles = new HashMap<>(); + try (ZipInputStream inputStream = new ZipInputStream(new ByteArrayInputStream(byteArray))) { + ZipEntry entry = inputStream.getNextEntry(); + logger.debug("First Entry = " +entry); + while(entry!= null){ + String filename = entry.getName(); + logger.debug("Next Entry = "+ filename); + + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String str = null; + StringBuilder sb = new StringBuilder(); + while((str = bufferedReader.readLine()) != null){ + sb.append(new String(str)).append(System.getProperty("line.separator")); + } + csarFiles.put(filename,sb.toString()); + entry = inputStream.getNextEntry(); + } + + } catch (IOException e) { + logger.error("Error Reading TOSCA.meta from CSAR",e); + throw new APPCException(e); + } + serviceFileName = readServiceFileName(csarFiles.get("TOSCA-Metadata/TOSCA.meta")); + logger.debug("Service File Name = " + serviceFileName); + serviceTemplateContent = csarFiles.get(serviceFileName); + + try { + resources = readResources (serviceTemplateContent); + } catch (Exception e) { + logger.error("Error reading resources from " + ", serviceFileName = " + serviceFileName + + ", TOSCA Metadata = " + csarFiles.get("TOSCA-Metadata/TOSCA.meta"),e); + throw new APPCException(e); + } + + for(Resource resource:resources){ + String resourceTemplate = csarFiles.get("Definitions/resource-" + resource.getFileNameTag() + "-template.yml"); + SDCArtifact artifact = this.getArtifactObject(resource,resourceTemplate); + processArtifact(artifact); + } + } + + private String readServiceFileName(String toscaMetadata) { + toscaMetadata = toscaMetadata.substring(toscaMetadata.indexOf("Entry-Definitions"), toscaMetadata.indexOf(System.getProperty("line.separator"),toscaMetadata.indexOf("Entry-Definitions"))); + toscaMetadata =toscaMetadata.split(":")[1].trim(); + return toscaMetadata; + } + + protected SDCArtifact getArtifactObject(Resource resource, String data){ + + SDCArtifact sdcArtifact = new SDCArtifact(); + + sdcArtifact.setArtifactUUID(this.artifact.getArtifactUUID()); + sdcArtifact.setArtifactName(this.artifact.getArtifactName()); + sdcArtifact.setArtifactType(this.artifact.getArtifactType()); + sdcArtifact.setArtifactVersion(this.artifact.getArtifactVersion()); + sdcArtifact.setArtifactDescription(this.artifact.getArtifactDescription()); + sdcArtifact.setArtifactContent(data); + sdcArtifact.setCreationDate(super.getCurrentDateTime()); + + sdcArtifact.setDistributionId(this.notification.getDistributionID()); + sdcArtifact.setServiceUUID(this.notification.getServiceUUID()); + sdcArtifact.setServiceName(this.notification.getServiceName()); + sdcArtifact.setServiceDescription(this.notification.getServiceDescription()); + + sdcArtifact.setResourceName(resource.getName()); + sdcArtifact.setResourceType(resource.getType()); + sdcArtifact.setResourceVersion(resource.getVersion()); + sdcArtifact.setResourceUUID(resource.getUuid()); + sdcArtifact.setResourceInstanceName(resource.getInstanceName()); + + return sdcArtifact; + } + + private List readResources(String serviceTemplateContent) throws IOException { + List resources = new LinkedList<>(); + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + JsonNode root = mapper.readTree(serviceTemplateContent); + JsonNode topologyTemplate = root.get("topology_template"); + JsonNode nodeTemplates = topologyTemplate.get("node_templates"); + Iterator> itr = nodeTemplates.fields(); + while(itr.hasNext()){ + Map.Entry entry = itr.next(); + String instanceName = entry.getKey(); + JsonNode nodeTemplate = entry.getValue(); + + String fileNameTag = nodeTemplate.get("type").asText(); + logger.debug("Resource type in Service Template = " + fileNameTag); + fileNameTag = fileNameTag.substring(fileNameTag.lastIndexOf(".")+1,fileNameTag.length()); + String version = nodeTemplate.get("metadata").get("version").asText(); + String uuid = nodeTemplate.get("metadata").get("UUID").asText(); + String name = nodeTemplate.get("metadata").get("name").asText(); + String type = nodeTemplate.get("metadata").get("type").asText(); + + if(!"VF".equalsIgnoreCase(type)){ + continue; + } + + Resource resource = new Resource(); + resource.setFileNameTag(fileNameTag); + resource.setVersion(version); + resource.setUuid(uuid); + resource.setInstanceName(instanceName); + resource.setName(name); + resource.setType(type); + + resources.add(resource); + } + return resources; + } + + + @Override + protected void processArtifact(SDCArtifact artifact) throws APPCException { + String vnfType = artifact.getResourceName(); + String version = artifact.getResourceVersion(); + String packageArtifactID = artifact.getArtifactUUID(); + + if (StringUtils.isEmpty(vnfType) || + StringUtils.isEmpty(version) || + StringUtils.isEmpty(packageArtifactID)) { + String errStr = String.format("Missing information in SDC request. Details: resource_type='%s', resource_version='%s', artifactID='%s'", vnfType, version, packageArtifactID); + logger.error(errStr); + throw new APPCException(errStr); + } + + try { + SDCArtifact existingArtifact = artifactStorageService.retrieveSDCArtifact(vnfType, version,artifact.getArtifactType()); + + if (existingArtifact ==null) { // new resource + logger.debug("Artifact not found for vnfType = " + vnfType + " , version = " + version + " , artifactType = " + artifact.getArtifactType()); + artifactStorageService.storeASDCArtifact(artifact); + } else { // duplicate + logger.debug("Artifact retrieved from database = " + existingArtifact); + logger.warn(String.format("Artifact of type '%s' already deployed for resource_type='%s' and resource_version='%s'", Constants.VF_LICENSE, vnfType, version)); + } + + } catch (Exception e) { + logger.error("Error processing artifact : " + artifact.toString() ); + throw new APPCException(e.getMessage(),e); + } + } + + +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/ArtifactType.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/ArtifactType.java new file mode 100644 index 000000000..1ff62385d --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/ArtifactType.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.object; + +/** + * Enummration listing SDC artifact types + */ +public enum ArtifactType { + APPC_CONFIG,VF_LICENSE,TOSCA_CSAR; + + /** + * returns ArtifactType for the input string type + * @param artifactTypeStr + * @return + */ + public static ArtifactType getArtifactType(String artifactTypeStr){ + for(ArtifactType artifactType: ArtifactType.values()){ + if(artifactType.name().equals(artifactTypeStr)){ + return artifactType; + } + } + return null; + } +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/Resource.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/Resource.java new file mode 100644 index 000000000..42716dc47 --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/Resource.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.object; + +public class Resource { + private String name; + private String type; + private String version; + private String uuid; + private String instanceName; + private String fileNameTag; + + public Resource(){ + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + public String getInstanceName() { + return instanceName; + } + + public void setInstanceName(String instanceName) { + this.instanceName = instanceName; + } + + + public String getFileNameTag() { + return fileNameTag; + } + + public void setFileNameTag(String fileNameTag) { + this.fileNameTag = fileNameTag; + } +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/SDCArtifact.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/SDCArtifact.java new file mode 100644 index 000000000..6cba5b2a2 --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/object/SDCArtifact.java @@ -0,0 +1,215 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.artifacts.object; + +/** + * POJO containing metadata about SDC artifact + */ +public class SDCArtifact { + + private String artifactUUID; + + private String artifactName; + + private String artifactType; + + private String artifactVersion; + + private String artifactContent; + + private String artifactDescription; + + private String creationDate; + + private String distributionId; + + + + private String resourceUUID; + + private String resourceName; + + private String resourceType; + + private String resourceVersion; + + private String resourceInstanceName; + + + private String serviceUUID; + + private String serviceName; + + private String serviceDescription; + + public String getArtifactUUID() { + return artifactUUID; + } + + public void setArtifactUUID(String artifactUUID) { + this.artifactUUID = artifactUUID; + } + + public String getArtifactContent() { + return artifactContent; + } + + public void setArtifactContent(String artifactContent) { + this.artifactContent = artifactContent; + } + + public String getArtifactDescription() { + return artifactDescription; + } + + public void setArtifactDescription(String artifactDescription) { + this.artifactDescription = artifactDescription; + } + + public String getArtifactName() { + return artifactName; + } + + public void setArtifactName(String artifactName) { + this.artifactName = artifactName; + } + + public String getArtifactType() { + return artifactType; + } + + public void setArtifactType(String artifactType) { + this.artifactType = artifactType; + } + + public String getArtifactVersion() { + return artifactVersion; + } + + public void setArtifactVersion(String artifactVersion) { + this.artifactVersion = artifactVersion; + } + + public String getCreationDate() { + return creationDate; + } + + public void setCreationDate(String creationDate) { + this.creationDate = creationDate; + } + + public String getDistributionId() { + return distributionId; + } + + public void setDistributionId(String distributionId) { + this.distributionId = distributionId; + } + + public String getResourceInstanceName() { + return resourceInstanceName; + } + + public void setResourceInstanceName(String resourceInstanceName) { + this.resourceInstanceName = resourceInstanceName; + } + + public String getResourceName() { + return resourceName; + } + + public void setResourceName(String resourceName) { + this.resourceName = resourceName; + } + + public String getResourceType() { + return resourceType; + } + + public void setResourceType(String resourceType) { + this.resourceType = resourceType; + } + + public String getResourceUUID() { + return resourceUUID; + } + + public void setResourceUUID(String resourceUUID) { + this.resourceUUID = resourceUUID; + } + + public String getResourceVersion() { + return resourceVersion; + } + + public void setResourceVersion(String resourceVersion) { + this.resourceVersion = resourceVersion; + } + + public String getServiceDescription() { + return serviceDescription; + } + + public void setServiceDescription(String serviceDescription) { + this.serviceDescription = serviceDescription; + } + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public String getServiceUUID() { + return serviceUUID; + } + + public void setServiceUUID(String serviceUUID) { + this.serviceUUID = serviceUUID; + } + + + @Override + public String toString() { + return "artifactUUID = " + artifactUUID + + " , artifactName = " + artifactName + + " , artifactType = " + artifactType + + " , artifactVersion = " + artifactVersion + + " , artifactContent = " + artifactContent + + " , artifactDescription = " + artifactDescription + + " , creationDate = " + creationDate + + " , distributionId = " +distributionId + + " , resourceUUID = " + resourceUUID + + " , resourceName = " + resourceName + + " , resourceType = " + resourceType + + " , resourceVersion = " + resourceVersion + + " , resourceInstanceName = " + resourceInstanceName + + " , serviceUUID = " + serviceUUID + + " , serviceName = " + serviceName + + " , serviceDescription = " + serviceDescription; + } +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcCallback.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcCallback.java index 6b36e2e45..345149686 100644 --- a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcCallback.java +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcCallback.java @@ -27,12 +27,15 @@ package org.openecomp.appc.sdc.listener; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.sdc.artifacts.ArtifactProcessor; +import org.openecomp.appc.sdc.artifacts.impl.ArtifactProcessorFactory; import org.openecomp.sdc.api.IDistributionClient; import org.openecomp.sdc.api.consumer.INotificationCallback; import org.openecomp.sdc.api.notification.IArtifactInfo; import org.openecomp.sdc.api.notification.INotificationData; import org.openecomp.sdc.api.notification.IResourceInstance; import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.openecomp.sdc.utils.DistributionStatusEnum; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; @@ -86,13 +89,34 @@ public class AsdcCallback implements INotificationCallback { } if (isRunning.get()) { + + for(IArtifactInfo artifact:data.getServiceArtifacts()){ + ArtifactProcessor artifactProcessor = ArtifactProcessorFactory.getArtifactProcessor(client, eventSender, data, null, artifact, storeUri); + if(artifactProcessor!=null){ + executor.submit(artifactProcessor); + } + } + for (IResourceInstance resource : data.getResources()) { for (IArtifactInfo artifact : resource.getArtifacts()) { logger.info(Util.toAsdcStoreDocumentInput(data, resource, artifact, "abc")); if (executor.getQueue().size() >= threadCount) { // log warning about job backlog } - executor.submit(new DownloadAndStoreOp(client, eventSender, data, resource, artifact, storeUri)); + ArtifactProcessor artifactProcessor = ArtifactProcessorFactory.getArtifactProcessor(client, eventSender, data, resource, artifact, storeUri); + if(artifactProcessor != null){ + executor.submit(artifactProcessor); + } + else{ + /* Before refactoring of the DownloadAndStoreOp class, the approach was to download all the + artifacts, send the download status, and then perform the processing of artifact if it is + required. Now that we are downloading the artifacts only when its processing is required, + we are sending the download status as positive just to have the same behaviour as before + refactoring. + */ + client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, data, artifact, DistributionStatusEnum.DOWNLOAD_OK)); + logger.error("Artifact type not supported : " + artifact.getArtifactType()); + } } } } else { diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcConfig.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcConfig.java index 7a176a981..448a7f365 100644 --- a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcConfig.java +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/AsdcConfig.java @@ -41,7 +41,7 @@ public class AsdcConfig implements IConfiguration { private String keystorePass; private int pollingInterval; // Time between listening sessions private int pollingTimeout; // Time to listen for (dmaap timeout url param)/1000 - private List types = new ArrayList<>(1); + private List types = new ArrayList<>(); private String user; private String pass; @@ -94,6 +94,10 @@ public class AsdcConfig implements IConfiguration { // Download type types.add("APPC_CONFIG"); types.add("VF_LICENSE"); + types.add("TOSCA_CSAR"); + /* + This types seems redundant, as it looks from the code that they are not being used anywhere + */ storeOp = new URI(props.getProperty("appc.asdc.provider.url")); } diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/DownloadAndStoreOp.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/DownloadAndStoreOp.java deleted file mode 100644 index 8fcf1fb81..000000000 --- a/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/DownloadAndStoreOp.java +++ /dev/null @@ -1,229 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : APPC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Copyright (C) 2017 Amdocs - * ============================================================================= - * 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. - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.appc.sdc.listener; - -import org.openecomp.appc.adapter.message.EventSender; -import org.openecomp.appc.adapter.message.MessageDestination; -import org.openecomp.appc.adapter.message.event.EventHeader; -import org.openecomp.appc.adapter.message.event.EventMessage; -import org.openecomp.appc.adapter.message.event.EventStatus; -import org.openecomp.appc.exceptions.APPCException; -import org.openecomp.appc.licmgr.Constants; -import org.openecomp.appc.licmgr.LicenseManager; -import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.notification.IArtifactInfo; -import org.openecomp.sdc.api.notification.INotificationData; -import org.openecomp.sdc.api.notification.IResourceInstance; -import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; -import org.openecomp.sdc.utils.DistributionActionResultEnum; -import org.openecomp.sdc.utils.DistributionStatusEnum; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.osgi.framework.BundleContext; -import org.osgi.framework.FrameworkUtil; -import org.osgi.framework.ServiceReference; - -import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS.*; - -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.URI; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -@SuppressWarnings("JavaDoc") -public class DownloadAndStoreOp implements Runnable { - - public static final String PAYLOAD_CHARSET = "UTF-8"; - - private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss"; - - private final EELFLogger logger = EELFManager.getInstance().getLogger(DownloadAndStoreOp.class); - - private IDistributionClient client; - private EventSender eventSender; - - private INotificationData notification; - private IResourceInstance resource; - private IArtifactInfo artifact; - - private URI storeUri; - - public DownloadAndStoreOp(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource, - IArtifactInfo artifact, URI storeUri) { - this.client = client; - this.eventSender = eventSender; - this.notification = notification; - this.resource = resource; - this.artifact = artifact; - this.storeUri = storeUri; - } - - @Override - public void run() { - logger.info(String.format("Attempting to download artifact %s", artifact)); - // Download artifact - IDistributionClientDownloadResult download = client.download(artifact); - logger.info(String.format("Download of artifact %s completed with status %s", artifact.getArtifactUUID(), download)); - - // Notify of download status - if (download.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) { - client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact, - DistributionStatusEnum.DOWNLOAD_ERROR), download.getDistributionMessageResult()); - sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), "Download is failed."); - return; - } - - client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact, DistributionStatusEnum.DOWNLOAD_OK)); - - String data = null; - try { - if (download.getArtifactPayload() != null) { - data = new String(download.getArtifactPayload(), PAYLOAD_CHARSET); - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - - boolean providerSuccess = false; - String providerReason = "Unknown Error"; - // Send data to provider - if (data != null && artifact != null) { - switch(artifact.getArtifactType()) { - case "APPC_CONFIG": - String postData = Util.toAsdcStoreDocumentInput(notification, resource, artifact, data); - try { - ProviderResponse result = ProviderOperations.post(storeUri.toURL(), postData, null); - if (result.getStatus() == 200) { - providerSuccess = Util.parseResponse(result.getBody()); - providerReason = "Success"; - } - } catch (MalformedURLException | APPCException e) { - providerReason = e.getMessage(); - e.printStackTrace(); - } - break; - - case "VF_LICENSE": - BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); - ServiceReference srefLicenseService = bctx.getServiceReference(LicenseManager.class); - LicenseManager licenseService = (LicenseManager) bctx.getService(srefLicenseService); - - Map artifactPayload = prepareArtifactPayloadParamsMap(data); - - String vnfType = artifactPayload.get(RESOURCE_NAME.name()); - String version = artifactPayload.get(RESOURCE_VERSION.name()); - String packageArtifactID = artifactPayload.get(ARTIFACT_UUID.name()); - - try { - if (null == vnfType || null == version || null == packageArtifactID || vnfType.isEmpty() || version.isEmpty() || packageArtifactID.isEmpty()) { - throw new APPCException(String.format("Missing information in ASDC request. Details: resource_type='%s', resource_version='%s', artifactID='%s'", vnfType, version, packageArtifactID)); - } - - Map existingArtifactPayload = licenseService.retrieveLicenseModelData(vnfType, version); - - if (existingArtifactPayload.isEmpty()) { // new resource - licenseService.storeArtifactPayload(artifactPayload); - } else { // duplicate - logger.warn(String.format("Artifact of type '%s' already deployed for resource_type='%s' and resource_version='%s'", Constants.VF_LICENSE, vnfType, version)); - } - - providerSuccess = true; - - } catch (Exception e) { - providerSuccess = false; - providerReason = e.getMessage(); - } - break; - - default: - throw new UnsupportedOperationException("Artifact type " + artifact.getArtifactType() + " is not supported"); - } - - } - - // Notify of provider's response - if (providerSuccess) { - client.sendDeploymentStatus( - Util.buildDistributionStatusMessage(client, notification, artifact, DistributionStatusEnum.DEPLOY_OK)); - } else { - client.sendDeploymentStatus(Util.buildDistributionStatusMessage(client, notification, artifact, - DistributionStatusEnum.DEPLOY_ERROR), providerReason); - sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), providerReason); - } - - } - - /** - * Prepares Artifact Payload params map - * @param data - * @return Map - */ - private Map prepareArtifactPayloadParamsMap(String data) { - Map paramsMap = new HashMap<>(); - - paramsMap.put(SERVICE_UUID.name(), this.notification.getServiceUUID()); - paramsMap.put(DISTRIBUTION_ID.name(), this.notification.getDistributionID()); - paramsMap.put(SERVICE_NAME.name(), this.notification.getServiceName()); - paramsMap.put(SERVICE_DESCRIPTION.name(), this.notification.getServiceDescription()); - paramsMap.put(RESOURCE_UUID.name(), this.resource.getResourceUUID()); - paramsMap.put(RESOURCE_INSTANCE_NAME.name(), this.resource.getResourceInstanceName()); - paramsMap.put(RESOURCE_NAME.name(), this.resource.getResourceName()); - paramsMap.put(RESOURCE_VERSION.name(), this.resource.getResourceVersion()); - paramsMap.put(RESOURCE_TYPE.name(), this.resource.getResourceType()); - paramsMap.put(ARTIFACT_UUID.name(), this.artifact.getArtifactUUID()); - paramsMap.put(ARTIFACT_TYPE.name(), this.artifact.getArtifactType()); - paramsMap.put(ARTIFACT_VERSION.name(), this.artifact.getArtifactVersion()); - paramsMap.put(ARTIFACT_DESCRIPTION.name(), this.artifact.getArtifactDescription()); - paramsMap.put(CREATION_DATE.name(), getCurrentDateTime()); - paramsMap.put(ARTIFACT_NAME.name(), this.artifact.getArtifactName()); - paramsMap.put(ARTIFACT_CONTENT.name(), data); - - return paramsMap; - } - - - private String getCurrentDateTime() { - DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); - Date date = new Date(); - return dateFormat.format(date); - } - - private void sendDCAEEvent(String distributionID, String serviceName, String serviceVersion, String errorMessage) { - if (null == eventSender) return; - String errorDescription = String.format("ASDC distribution of service '%s', version '%s' is failed with reason: '%s'", - serviceName, serviceVersion, errorMessage); - - EventMessage eventMessage = new EventMessage( - new EventHeader((new java.util.Date()).toString(), serviceVersion, distributionID), - new EventStatus(401, errorDescription)); - - eventSender.sendEvent(MessageDestination.DCAE, eventMessage); - } - -} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/TestAsdcListener.java b/appc-asdc-listener/appc-asdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/TestAsdcListener.java new file mode 100644 index 000000000..e6f913a6a --- /dev/null +++ b/appc-asdc-listener/appc-asdc-listener-bundle/src/test/java/org/openecomp/appc/sdc/listener/TestAsdcListener.java @@ -0,0 +1,194 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2017 Amdocs + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.appc.sdc.listener; + + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Matchers; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.openecomp.appc.adapter.message.EventSender; +import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService; +import org.openecomp.appc.sdc.artifacts.impl.ArtifactProcessorFactory; +import org.openecomp.appc.sdc.artifacts.impl.ToscaCsarArtifactProcessor; +import org.openecomp.appc.sdc.artifacts.object.SDCArtifact; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; +import org.openecomp.sdc.api.consumer.INotificationCallback; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IResourceInstance; +import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; +import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl; +import org.openecomp.sdc.utils.DistributionActionResultEnum; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({IDistributionClient.class, + EventSender.class, + ArtifactStorageService.class, + ToscaCsarArtifactProcessor.class, + ArtifactProcessorFactory.class}) +public class TestAsdcListener { + + IDistributionClient client; + private EventSender eventSender; + private INotificationCallback asdcCallback; + private ArtifactStorageService storageService; + private ToscaCsarArtifactProcessor artifactProcessor; + + + @Before + public void setup() throws Exception { + client = PowerMockito.mock(IDistributionClient.class); + eventSender = PowerMockito.mock(EventSender.class); + asdcCallback = new AsdcCallback(null,client); + + artifactProcessor = Mockito.spy(new ToscaCsarArtifactProcessor(client,eventSender,getNotificationData(),getResources().get(0) + ,getServiceArtifacts().get(0),null)); + storageService = PowerMockito.mock(ArtifactStorageService.class); + Whitebox.setInternalState(artifactProcessor,"artifactStorageService", storageService); + + PowerMockito.doCallRealMethod().when(artifactProcessor).processArtifact((IDistributionClientDownloadResult) Matchers.anyObject()); + PowerMockito.doCallRealMethod().when(artifactProcessor).run(); + + + PowerMockito.mockStatic(ArtifactProcessorFactory.class); + PowerMockito.when(ArtifactProcessorFactory.getArtifactProcessor((IDistributionClient)Matchers.anyObject(), (EventSender)Matchers.anyObject(), + (INotificationData)Matchers.anyObject(), (IResourceInstance)Matchers.anyObject(), + (IArtifactInfo)Matchers.anyObject(), (URI)Matchers.anyObject())).thenReturn(artifactProcessor); + + Whitebox.setInternalState(asdcCallback,"eventSender", eventSender); + PowerMockito.doReturn(readDownloadResult()).when(client).download((IArtifactInfo) Matchers.anyObject()); + PowerMockito.doReturn(null).when(client).sendDownloadStatus((IDistributionStatusMessage) Matchers.anyObject()); + + PowerMockito.doReturn(null).when(storageService).retrieveSDCArtifact(Matchers.anyString(),Matchers.anyString(),Matchers.anyString()); + + PowerMockito.doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocationOnMock) throws Throwable { + System.out.print(invocationOnMock.getArguments()[0].toString()); + return null; + } + }).when(storageService).storeASDCArtifact((SDCArtifact)Matchers.anyObject()); + } + + private IDistributionClientDownloadResult readDownloadResult() throws IOException, URISyntaxException { + DistributionClientDownloadResultImpl downloadResult = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS,"Download success"); + File file = new File(this.getClass().getResource("/csar/service-ServiceAppc-csar.csar").toURI()); + + byte[] bFile = new byte[(int) file.length()]; + FileInputStream fileInputStream = new FileInputStream(file); + fileInputStream.read(bFile); + fileInputStream.close(); + + downloadResult.setArtifactPayload(bFile); + return downloadResult; + } + + +// @Test + public void testASDCListener() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { + + + INotificationData notificationData = getNotificationData(); + asdcCallback.activateCallback(notificationData); + +// pause(); + } + +// private void pause(){ +// try { +// Thread.sleep(50000000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// } + + private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { + + INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl"); + + List serviceArtifacts = getServiceArtifacts(); + + invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts); + return notificationData; + } + + private List getResources() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException { + List resources = new ArrayList<>(); + IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance"); + + List serviceArtifacts = getServiceArtifacts(); + invokeMethod(resource,"setArtifacts",serviceArtifacts); + invokeMethod(resource,"setResourceName","Vnf"); + invokeMethod(resource,"setResourceVersion","1.0"); + + resources.add(resource); + return resources; + } + + private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException { + Method[] methods = object.getClass().getDeclaredMethods(); + for(Method method:methods){ + if(methodName.equalsIgnoreCase(method.getName())){ + method.setAccessible(true); + method.invoke(object,arguments); + } + } + } + + private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException { + Constructor constructor = Class.forName(fqcn).getDeclaredConstructors()[0]; + constructor.setAccessible(true); + return constructor.newInstance(); + } + + private List getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException, InstantiationException, IllegalAccessException { + List serviceArtifacts = new ArrayList<>(); + IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl"); + invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR"); + invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl"); + serviceArtifacts.add(artifactInfo); + return serviceArtifacts; + } +} diff --git a/appc-asdc-listener/appc-asdc-listener-bundle/src/test/resources/csar/service-ServiceAppc-csar.csar b/appc-asdc-listener/appc-asdc-listener-bundle/src/test/resources/csar/service-ServiceAppc-csar.csar new file mode 100644 index 000000000..a30ca2f6f Binary files /dev/null and b/appc-asdc-listener/appc-asdc-listener-bundle/src/test/resources/csar/service-ServiceAppc-csar.csar differ diff --git a/appc-asdc-listener/appc-yang-generator/pom.xml b/appc-asdc-listener/appc-yang-generator/pom.xml index 6834bd85d..473524104 100644 --- a/appc-asdc-listener/appc-yang-generator/pom.xml +++ b/appc-asdc-listener/appc-yang-generator/pom.xml @@ -41,7 +41,39 @@ openecomp-tosca-lib ${toscalib.version} + + org.openecomp.sdc.common + openecomp-tosca-datatype + ${toscalib.version} + - + + + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + + + ${artifactId}-${version} + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 1.8 + 1.8 + + + + \ No newline at end of file diff --git a/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java index bc5273252..246563932 100644 --- a/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java +++ b/appc-asdc-listener/appc-yang-generator/src/main/java/org/openecomp/appc/yang/impl/YANGGeneratorImpl.java @@ -42,7 +42,7 @@ import org.openecomp.appc.yang.type.YangTypes; import org.openecomp.sdc.tosca.datatypes.model.NodeType; import org.openecomp.sdc.tosca.datatypes.model.PropertyDefinition; import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate; -import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil; +import org.openecomp.sdc.tosca.services.YamlUtil; import java.io.*; import java.util.HashMap; @@ -127,7 +127,7 @@ public class YANGGeneratorImpl implements YANGGenerator { private Map parseTosca(String tosca) throws YANGGenerationException { Log.info("Entered into parseTosca."); - ServiceTemplate serviceTemplate = new ToscaExtensionYamlUtil().yamlToObject(tosca, ServiceTemplate.class); + ServiceTemplate serviceTemplate = new YamlUtil().yamlToObject(tosca, ServiceTemplate.class); Map nodeTypeMap = serviceTemplate.getNode_types(); String kind = nodeTypeMap.keySet().toArray(new String[0])[0]; NodeType nodeType = nodeTypeMap.get(kind); -- cgit 1.2.3-korg