From 7712aebd58699838b4926c577b46536de1e25baa Mon Sep 17 00:00:00 2001 From: Michael Shitrit Date: Tue, 12 Dec 2017 08:51:19 +0200 Subject: Distribution-client Tenant Isolation Issue-ID: SDC-744 Change-Id: I965cb1ad1c9dae6aa38a3c3baced3bbbb1d8f5af Signed-off-by: Michael Shitrit --- pom.xml | 2 +- .../main/java/org/openecomp/test/ClientTest.java | 98 +++-- .../org/openecomp/test/SimpleConfiguration.java | 16 +- .../org/openecomp/sdc/api/IDistributionClient.java | 137 +++++-- .../sdc/api/asdc/RegistrationRequest.java | 29 +- .../api/consumer/IComponentDoneStatusMessage.java | 7 + .../openecomp/sdc/api/consumer/IConfiguration.java | 122 +++--- .../api/consumer/IDistributionStatusMessage.java | 28 +- .../consumer/IDistributionStatusMessageBasic.java | 28 ++ .../sdc/api/consumer/IFinalDistrStatusMessage.java | 9 + .../sdc/api/consumer/IStatusCallback.java | 35 ++ .../sdc/api/notification/IStatusData.java | 65 ++++ .../openecomp/sdc/http/AsdcConnectorClient.java | 414 --------------------- .../org/openecomp/sdc/http/HttpAsdcClient.java | 21 +- .../org/openecomp/sdc/http/SdcConnectorClient.java | 398 ++++++++++++++++++++ .../java/org/openecomp/sdc/impl/Configuration.java | 62 ++- .../openecomp/sdc/impl/DistributionClientImpl.java | 212 +++++++++-- .../sdc/impl/DistributionStatusMessageImpl.java | 70 ++-- ...istributionStatusMessageJsonBuilderFactory.java | 31 ++ .../org/openecomp/sdc/impl/StatusConsumer.java | 66 ++++ .../org/openecomp/sdc/impl/StatusDataImpl.java | 95 +++++ .../sdc/impl/mock/DistributionClientStubImpl.java | 40 +- .../sdc/utils/DistributionActionResultEnum.java | 3 + .../sdc/utils/DistributionStatusEnum.java | 27 +- .../java/org/openecomp/sdc/utils/GeneralUtils.java | 32 +- .../openecomp/sdc/http/SdcConnectorClientTest.java | 119 ++++++ .../openecomp/sdc/impl/DistributionClientTest.java | 103 +---- .../org/openecomp/sdc/utils/TestConfiguration.java | 12 +- 28 files changed, 1533 insertions(+), 748 deletions(-) create mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IComponentDoneStatusMessage.java create mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IDistributionStatusMessageBasic.java create mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IFinalDistrStatusMessage.java create mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IStatusCallback.java create mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/api/notification/IStatusData.java delete mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/http/AsdcConnectorClient.java create mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/http/SdcConnectorClient.java create mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/StatusConsumer.java create mode 100644 sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/StatusDataImpl.java create mode 100644 sdc-distribution-client/src/test/java/org/openecomp/sdc/http/SdcConnectorClientTest.java diff --git a/pom.xml b/pom.xml index 9355588..1214b7e 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ sdc-distribution-client - sdc-distribution-ci + diff --git a/sdc-distribution-ci/src/main/java/org/openecomp/test/ClientTest.java b/sdc-distribution-ci/src/main/java/org/openecomp/test/ClientTest.java index e44013c..872240b 100644 --- a/sdc-distribution-ci/src/main/java/org/openecomp/test/ClientTest.java +++ b/sdc-distribution-ci/src/main/java/org/openecomp/test/ClientTest.java @@ -21,43 +21,93 @@ package org.openecomp.test; import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.consumer.INotificationCallback; +import org.openecomp.sdc.api.consumer.IComponentDoneStatusMessage; +import org.openecomp.sdc.api.consumer.IStatusCallback; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.notification.IStatusData; import org.openecomp.sdc.api.results.IDistributionClientResult; import org.openecomp.sdc.impl.DistributionClientFactory; -import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper; -import org.openecomp.sdc.tosca.parser.impl.SdcToscaParserFactory; -import org.slf4j.LoggerFactory; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.LoggerContext; +import org.openecomp.sdc.utils.DistributionStatusEnum; public class ClientTest { public static void main(String[] args) throws Exception { - - LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); - lc.getLogger("org.apache.http").setLevel(Level.INFO); + soWdListner(); + clientSender(); + + } + + private static void clientSender() { IDistributionClient client = DistributionClientFactory.createDistributionClient(); - INotificationCallback callback; - Boolean download = SimpleConfiguration.toDownload(); - if( download ){ - callback = new AdvanceCallBack(client); - } - else{ - callback = new SimpleCallback(client); + IDistributionClientResult result = client.init(new SimpleConfiguration(), new SimpleCallback(client)); + System.err.println("Init Status: " + result.toString()); + + IDistributionClientResult start = client.start(); + + System.err.println("Start Status: " + start.toString()); + for( int i = 0; i < 2; i++ ){ + try { + Thread.sleep(10000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + client.sendComponentDoneStatus(new IComponentDoneStatusMessage() { + + @Override + public long getTimestamp() { + return System.currentTimeMillis(); + } + + @Override + public DistributionStatusEnum getStatus() { + return DistributionStatusEnum.COMPONENT_DONE_OK; + } + + @Override + public String getDistributionID() { + // TODO Auto-generated method stub + return ""; + } + + @Override + public String getConsumerID() { + return client.getConfiguration().getConsumerID(); + } + + @Override + public String getComponentName() { + return "MSO"; + } + }); } - IDistributionClientResult result = client.init(new SimpleConfiguration(), callback); + + } - System.out.println(result.getDistributionMessageResult()); + private static void soWdListner() { + IDistributionClient client = DistributionClientFactory.createDistributionClient(); + IDistributionClientResult result = client.init(new SimpleConfiguration() { + @Override + public boolean isConsumeProduceStatusTopic() { + return true; + } + }, new SimpleCallback(client) { + @Override + public void activateCallback(INotificationData data) { + System.err.println("Monitor Recieved Notification: " + data.toString()); - System.out.println("Starting client..."); - IDistributionClientResult startResult = client.start(); + } + }, new IStatusCallback() { - // Thread.sleep(10000); - // client.stop(); + @Override + public void activateCallback(IStatusData data) { + System.err.println("Monitor Recieved Status: " + data.toString()); - System.out.println(startResult.getDistributionMessageResult()); + } + }); + System.err.println("Init Status: " + result.toString()); + IDistributionClientResult start = client.start(); + System.err.println("Start Status: " + start.toString()); } } diff --git a/sdc-distribution-ci/src/main/java/org/openecomp/test/SimpleConfiguration.java b/sdc-distribution-ci/src/main/java/org/openecomp/test/SimpleConfiguration.java index 60bb7d4..417f564 100644 --- a/sdc-distribution-ci/src/main/java/org/openecomp/test/SimpleConfiguration.java +++ b/sdc-distribution-ci/src/main/java/org/openecomp/test/SimpleConfiguration.java @@ -20,8 +20,11 @@ package org.openecomp.test; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.api.consumer.IConfiguration; public class SimpleConfiguration implements IConfiguration{ @@ -161,10 +164,14 @@ public class SimpleConfiguration implements IConfiguration{ return "127.0.0.1:8443"; } + @Override + public List getMsgBusAddress() { + return new ArrayList<>(); + } + @Override public String getKeyStorePath() { - //return ""; - return "etc/asdc-client.jks"; + return StringUtils.EMPTY; } @Override @@ -189,4 +196,9 @@ public class SimpleConfiguration implements IConfiguration{ return true; } + @Override + public boolean isConsumeProduceStatusTopic() { + return false; + } + } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/IDistributionClient.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/IDistributionClient.java index 620babf..8caf109 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/IDistributionClient.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/IDistributionClient.java @@ -22,33 +22,34 @@ package org.openecomp.sdc.api; import java.util.List; -import org.openecomp.sdc.api.consumer.IConfiguration; -import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; -import org.openecomp.sdc.api.consumer.INotificationCallback; +import org.openecomp.sdc.api.consumer.*; +import org.openecomp.sdc.api.consumer.IStatusCallback; import org.openecomp.sdc.api.notification.IArtifactInfo; import org.openecomp.sdc.api.notification.IVfModuleMetadata; import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; import org.openecomp.sdc.api.results.IDistributionClientResult; public interface IDistributionClient { - + /** * Update the configuration of the distribution client
- * Updatable configuration parameters are: pollingInterval, pollingTimeout, consumerGroup and relevantArtifactTypes + * Updatable configuration parameters are: pollingInterval, pollingTimeout, + * consumerGroup and relevantArtifactTypes * - * @param newConf - contains updated configuration + * @param newConf + * - contains updated configuration * * @return IDistributionClientResult */ IDistributionClientResult updateConfiguration(IConfiguration newConf); - + /** * Retrieve the configuration of the distribution client
* * @return IConfiguration */ IConfiguration getConfiguration(); - + /** * Start distribution client
* - start polling notification topic
@@ -56,17 +57,17 @@ public interface IDistributionClient { * @return IDistributionClientResult */ IDistributionClientResult start(); - + /** * Stop distribution client
* - stop polling notification topic
* - unregister topics (via ASDC)
- * - delete keys from UEB + * - delete keys from UEB * * @return IDistributionClientResult */ IDistributionClientResult stop(); - + /** * Downloads an artifact from ASDC Catalog
* @@ -74,66 +75,136 @@ public interface IDistributionClient { * @return IDistributionClientDownloadResult */ IDistributionClientDownloadResult download(IArtifactInfo artifactInfo); - + /** - * Initialize the distribution client
- * - fetch the UEB server list from ASDC
- * - create keys in UEB
- * - register for topics (via ASDC)
- * - set the notification callback
- * - * Note: all configuration fields are mandatory.
- * Password must be in clear text and not encrypted
- * ECOMP-Component MUST store password as SHA-2 (256) hashed with dynamically generated salt value
+ * Initialize the distribution client
+ * - fetch the UEB server list from ASDC
+ * - create keys in UEB
+ * - register for topics (via ASDC)
+ * - set the notification callback
+ * + * Note: all configuration fields are mandatory.
+ * Password must be in clear text and not encrypted
+ * ONAP-Component MUST store password as SHA-2 (256) hashed with + * dynamically generated salt value
* * @param conf * @param callback * @return IDistributionClientResult */ IDistributionClientResult init(IConfiguration conf, INotificationCallback callback); - - + /** - * Build and publish Distribution Download Status event to Distribution Status Topic + * Initialize the distribution client
+ * - fetch the UEB server list from ASDC
+ * - create keys in UEB
+ * - register for topics (via ASDC)
+ * - set the notification callback
+ * + * Note: all configuration fields are mandatory.
+ * Password must be in clear text and not encrypted
+ * ONAP-Component MUST store password as SHA-2 (256) hashed with + * dynamically generated salt value
+ * + * @param conf + * @param notificationCallback + * @param statusCallback + * @return IDistributionClientResult + */ + IDistributionClientResult init(IConfiguration conf, INotificationCallback notificationCallback, + IStatusCallback statusCallback); + + /** + * Build and publish Distribution Download Status event to Distribution + * Status Topic * * @param statusMessage * @return IDistributionClientResult */ IDistributionClientResult sendDownloadStatus(IDistributionStatusMessage statusMessage); - + /** - * Build and publish Distribution Download Status event to Distribution Status Topic With Error Reason. + * Build and publish Distribution Download Status event to Distribution + * Status Topic With Error Reason. * * @param statusMessage * @param errorReason * @return IDistributionClientResult */ IDistributionClientResult sendDownloadStatus(IDistributionStatusMessage statusMessage, String errorReason); + + /** + * Build and publish Distribution Deployment Status event to Distribution + * Status Topic + * + * @param statusMessage + * @return IDistributionClientResult + */ + IDistributionClientResult sendDeploymentStatus(IDistributionStatusMessage statusMessage); + + /** + * Build and publish Distribution Deployment Status event to Distribution + * Status Topic With Error Reason. + * + * @param statusMessage + * @param errorReason + * @return IDistributionClientResult + */ + IDistributionClientResult sendDeploymentStatus(IDistributionStatusMessage statusMessage, String errorReason); + + /** + * Build and publish Distribution Component Status event to Distribution + * Status Topic + * + * @param statusMessage + * @return IDistributionClientResult + */ + IDistributionClientResult sendComponentDoneStatus(IComponentDoneStatusMessage statusMessage); + + /** + * Build and publish Distribution Component Status event to Distribution + * Status Topic With Error Reason. + * + * @param statusMessage + * @param errorReason + * @return IDistributionClientResult + */ + IDistributionClientResult sendComponentDoneStatus(IComponentDoneStatusMessage statusMessage, String errorReason); /** - * Build and publish Distribution Deployment Status event to Distribution Status Topic + * Build and publish Distribution Final Status event to Distribution + * Status Topic * * @param statusMessage * @return IDistributionClientResult */ - IDistributionClientResult sendDeploymentStatus(IDistributionStatusMessage statusMessage); + IDistributionClientResult sendFinalDistrStatus(IFinalDistrStatusMessage statusMessage); + /** - * Build and publish Distribution Deployment Status event to Distribution Status Topic With Error Reason. + * Build and publish Distribution Final Status event to Distribution + * Status Topic With Error Reason. * * @param statusMessage * @param errorReason * @return IDistributionClientResult */ - IDistributionClientResult sendDeploymentStatus(IDistributionStatusMessage statusMessage, String errorReason); + IDistributionClientResult sendFinalDistrStatus(IFinalDistrStatusMessage statusMessage, String errorReason); + - /**This method parses artifact of type VF_MODULES_METADATA payload data .
- * Method is deprecated due to VF Module changes. Only backward compatibility is supported.
+ /** + * This method parses artifact of type VF_MODULES_METADATA payload data + * .
+ * Method is deprecated due to VF Module changes. Only backward + * compatibility is supported.
+ * * @param artifactPayload * @return IVfModuleMetadata list */ - @Deprecated + @Deprecated List decodeVfModuleArtifact(byte[] artifactPayload); + + } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/asdc/RegistrationRequest.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/asdc/RegistrationRequest.java index f6ce949..1e398be 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/asdc/RegistrationRequest.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/asdc/RegistrationRequest.java @@ -20,14 +20,37 @@ package org.openecomp.sdc.api.asdc; +import java.util.List; + public class RegistrationRequest { String apiPublicKey; String distrEnvName; - - public RegistrationRequest(String apiPublicKey, String distrEnvName) { + Boolean isConsumerToSdcDistrStatusTopic; + List distEnvEndPoints; + + public RegistrationRequest(String apiPublicKey, String distrEnvName, boolean isConsumerToSdcDistrStatusTopic, List distEnvEndPoints) { this.apiPublicKey = apiPublicKey; this.distrEnvName = distrEnvName; + this.isConsumerToSdcDistrStatusTopic = isConsumerToSdcDistrStatusTopic; + this.distEnvEndPoints = distEnvEndPoints; + } + + public String getApiPublicKey() { + return apiPublicKey; + } + + public String getDistrEnvName() { + return distrEnvName; } - + + public Boolean getIsConsumerToSdcDistrStatusTopic() { + return isConsumerToSdcDistrStatusTopic; + } + + public List getDistEnvEndPoints() { + return distEnvEndPoints; + } + + } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IComponentDoneStatusMessage.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IComponentDoneStatusMessage.java new file mode 100644 index 0000000..25661bf --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IComponentDoneStatusMessage.java @@ -0,0 +1,7 @@ +package org.openecomp.sdc.api.consumer; + +public interface IComponentDoneStatusMessage extends IDistributionStatusMessageBasic { + String getComponentName(); + default String getArtifactURL(){return "";} + +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IConfiguration.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IConfiguration.java index 19f94df..51d219f 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IConfiguration.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IConfiguration.java @@ -24,98 +24,134 @@ import java.util.List; import org.openecomp.sdc.api.notification.INotificationData; public interface IConfiguration { - /**SDC Distribution Engine address. - * Value can be either hostname (with or without port), IP:port or FQDN (Fully Qualified Domain Name). - * * @return SDC Distribution Engine address. - */ - String getAsdcAddress(); - + /** + * SDC Distribution Engine address. Value can be either hostname (with or + * without port), IP:port or FQDN (Fully Qualified Domain Name). * @return + * SDC Distribution Engine address. + */ + String getAsdcAddress(); + + /**SDC Distribution Addresses from ONAP Component + * Values need to be set from impl + */ + List getMsgBusAddress(); + /** * User Name for SDC distribution consumer authentication. + * * @return User Name. */ - String getUser(); - + String getUser(); + /** * User Password for SDC distribution consumer authentication. + * * @return User Password. */ - String getPassword(); - + String getPassword(); + /** - * Distribution Client Polling Interval towards UEB in seconds. - * Can Be reconfigured in runtime. + * Distribution Client Polling Interval towards UEB in seconds. Can Be + * reconfigured in runtime. + * * @return Distribution Client Polling Interval. */ - int getPollingInterval(); - + int getPollingInterval(); + /** - * Distribution Client Timeout in seconds waiting to UEB server response in each fetch interval. - * Can Be reconfigured in runtime. + * Distribution Client Timeout in seconds waiting to UEB server response in + * each fetch interval. Can Be reconfigured in runtime. + * * @return Distribution Client Timeout in seconds. */ - int getPollingTimeout(); - - /** List of artifact types.
- * If the service contains any of the artifacts in the list, the callback will be activated. - * Can Be reconfigured in runtime. + int getPollingTimeout(); + + /** + * List of artifact types.
+ * If the service contains any of the artifacts in the list, the callback + * will be activated. Can Be reconfigured in runtime. + * * @return List of artifact types. */ List getRelevantArtifactTypes(); - + /** - * Returns the consumer group defined for this ECOMP component, if no consumer group is defined return null. + * Returns the consumer group defined for this ECOMP component, if no + * consumer group is defined return null. + * * @return Consumer group. */ String getConsumerGroup(); - + /** - * Returns the environment name (testing, production etc...). - * Can Be reconfigured in runtime. - * @return + * Returns the environment name (testing, production etc...). Can Be + * reconfigured in runtime. + * + * @return */ String getEnvironmentName(); - + /** * Unique ID of ECOMP component instance (e.x INSTAR name). + * * @return */ String getConsumerID(); - + /** - * Return full path to Client's Key Store that contains either CA certificate or the ASDC's public key (e.g /etc/keystore/asdc-client.jks) + * Return full path to Client's Key Store that contains either CA + * certificate or the ASDC's public key (e.g /etc/keystore/asdc-client.jks) * file will be deployed with sdc-distribution jar. + * * @return */ String getKeyStorePath(); - + /** * @return Returns client's Key Store password */ String getKeyStorePassword(); - + /** - * Sets whether SDC server TLS authentication is activated. - * If set to false, Key Store path and password are not needed to be set. + * Sets whether SDC server TLS authentication is activated. If set to false, + * Key Store path and password are not needed to be set. + * * @return */ boolean activateServerTLSAuth(); - + /** - * If set to true the method {@link INotificationData#getResources()} will return all found resources.
- * That means that metadata of resources that do not contain relevant artifacts types (artifacts that are defined in {@link #getRelevantArtifactTypes()} will be returned.
- * Setting the method to false will activate the legacy behavior, in which empty resources are not part of the notification.
+ * If set to true the method {@link INotificationData#getResources()} will + * return all found resources.
+ * That means that metadata of resources that do not contain relevant + * artifacts types (artifacts that are defined in + * {@link #getRelevantArtifactTypes()} will be returned.
+ * Setting the method to false will activate the legacy behavior, in which + * empty resources are not part of the notification.
+ * * @return */ boolean isFilterInEmptyResources(); /** - * By default, Distribution Client will use HTTPS (TLS 1.2) when connecting to DMAAP. - * This param can be null, then default (HTTPS) behavior will be applied. - * If set to false, distribution client will use HTTP when connecting to DMAAP. + * By default, Distribution Client will use HTTPS (TLS 1.2) when connecting + * to DMAAP. This param can be null, then default (HTTPS) behavior will be + * applied. If set to false, distribution client will use HTTP when + * connecting to DMAAP. + * * @return */ Boolean isUseHttpsWithDmaap(); -} - + /** + * By default, (false value) Distribution Client will trigger the regular registration + * towards SDC (register component as consumer to the SDC-DISTR-NOTIF-TOPIC-[ENV] topic and register component as producer to the SDC-DISTR-STATUS-TOPIC-[ENV]).
+ * If set to true, distribution client trigger Register to SDC indicating + * that this component request to be consumer and producer of the + * SDC-DISTR-STATUS-TOPIC-[ENV] topic.
+ * @return + */ + default boolean isConsumeProduceStatusTopic() { + return false; + } +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IDistributionStatusMessage.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IDistributionStatusMessage.java index 86251d1..ab3d6a7 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IDistributionStatusMessage.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IDistributionStatusMessage.java @@ -20,33 +20,9 @@ package org.openecomp.sdc.api.consumer; -import org.openecomp.sdc.utils.DistributionStatusEnum; +public interface IDistributionStatusMessage extends IDistributionStatusMessageBasic{ -public interface IDistributionStatusMessage { - /** - * Distribution ID published in the distribution notification.
- * Should be used to link the distribution status reports to the appropriate - * distribution activation.
- * Global Distribution Identifier: UUID generated by ASDC per each - * distribution activation.
- * Generated UUID is compliant with RFC 4122. It is a 128-bit value - * formatted into blocks of hexadecimal digits separated by a hyphen ("-"). - * Ex.: AA97B177-9383-4934-8543-0F91A7A02836 - */ - String getDistributionID(); - - /**Unique ID of ECOMP component instance (e.x INSTAR name)*/ - String getConsumerID(); - - /** - * Timestamp of the distribution status report creation.
- * The number of seconds that have elapsed since January 1, 1970. - */ - long getTimestamp(); - /**Resource URL of the downloaded/deployed artifact - URL specified in the distribution notification message*/ String getArtifactURL(); - - /**Download/Deployment status*/ - DistributionStatusEnum getStatus(); + } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IDistributionStatusMessageBasic.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IDistributionStatusMessageBasic.java new file mode 100644 index 0000000..fb1194d --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IDistributionStatusMessageBasic.java @@ -0,0 +1,28 @@ +package org.openecomp.sdc.api.consumer; + +import org.openecomp.sdc.utils.DistributionStatusEnum; + +public interface IDistributionStatusMessageBasic { + /** + * Distribution ID published in the distribution notification.
+ * Should be used to link the distribution status reports to the appropriate + * distribution activation.
+ * Global Distribution Identifier: UUID generated by SDC per each + * distribution activation.
+ * Generated UUID is compliant with RFC 4122. It is a 128-bit value + * formatted into blocks of hexadecimal digits separated by a hyphen ("-"). + * Ex.: AA97B177-9383-4934-8543-0F91A7A02836 + */ + String getDistributionID(); + + /**Unique ID of ONAP component instance (e.x INSTAR name)*/ + String getConsumerID(); + + /** + * Timestamp of the distribution status report creation.
+ * The number of seconds that have elapsed since January 1, 1970. + */ + long getTimestamp(); + /**Download/Deployment status*/ + DistributionStatusEnum getStatus(); +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IFinalDistrStatusMessage.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IFinalDistrStatusMessage.java new file mode 100644 index 0000000..42affd7 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IFinalDistrStatusMessage.java @@ -0,0 +1,9 @@ +package org.openecomp.sdc.api.consumer; + + + +public interface IFinalDistrStatusMessage extends IDistributionStatusMessageBasic{ + + default String getConsumerID(){return "";} + default String getComponentName(){return "";} +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IStatusCallback.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IStatusCallback.java new file mode 100644 index 0000000..a889f52 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/consumer/IStatusCallback.java @@ -0,0 +1,35 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.api.consumer; + +import org.openecomp.sdc.api.notification.IStatusData; + +/** + * When a relevant status will be found activateCallback method will be + * activated with the status data.
+ * Please implement it according to your desired callback logic. + */ +@FunctionalInterface +public interface IStatusCallback { + void activateCallback(IStatusData data); + + +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/notification/IStatusData.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/notification/IStatusData.java new file mode 100644 index 0000000..7a46686 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/api/notification/IStatusData.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.api.notification; + +import org.openecomp.sdc.utils.DistributionStatusEnum; + +public interface IStatusData { + /** + * Global Distribution Identifier: UUID generated by SDC per each + * distribution activation.
+ * Generated UUID is compliant with RFC 4122.
+ * It is a 128-bit value formatted into blocks of hexadecimal digits + * separated by a hyphen ("-").
+ * Ex.: AA97B177-9383-4934-8543-0F91A7A02836 + */ + String getDistributionID(); + + /** Unique ID of ONAP component instance (e.x INSTAR name). */ + String getConsumerID(); + + /** The predefined ONAP component name configured on the component. */ + String getComponentName(); + + /** + * Timestamp of the distribution status report creation. The number of + * seconds that have elapsed since January 1, 1970 + */ + Long getTimestamp(); + /** + * Resource URL of the downloaded/deployed artifact - URL specified in the + * distribution notification message. + */ + String getArtifactURL(); + /** + * Status Event type + */ + DistributionStatusEnum getStatus(); + /** + * Error Reason describing the Status Event. + */ + String getErrorReason(); + + + + + +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/AsdcConnectorClient.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/AsdcConnectorClient.java deleted file mode 100644 index 0ef1711..0000000 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/AsdcConnectorClient.java +++ /dev/null @@ -1,414 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * sdc-distribution-client - * ================================================================================ - * 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.sdc.http; - -import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpEntity; -import org.apache.http.HttpHeaders; -import org.apache.http.HttpStatus; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.openecomp.sdc.api.asdc.RegistrationRequest; -import org.openecomp.sdc.api.asdc.ServerListResponse; -import org.openecomp.sdc.api.consumer.IConfiguration; -import org.openecomp.sdc.api.notification.IArtifactInfo; -import org.openecomp.sdc.api.results.IDistributionClientResult; -import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl; -import org.openecomp.sdc.impl.DistributionClientResultImpl; -import org.openecomp.sdc.utils.DistributionActionResultEnum; -import org.openecomp.sdc.utils.DistributionClientConstants; -import org.openecomp.sdc.utils.GeneralUtils; -import org.openecomp.sdc.utils.Pair; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.att.nsa.apiClient.credentials.ApiCredential; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; - -import fj.data.Either; - -public class AsdcConnectorClient { - String contentDispositionHeader = "Content-Disposition"; - private static Logger log = LoggerFactory.getLogger(AsdcConnectorClient.class.getName()); - private IConfiguration configuration; - private HttpAsdcClient httpClient = null; - - public void init(IConfiguration configuraion) { - this.configuration = configuraion; - httpClient = new HttpAsdcClient(configuration); - } - - public void close() { - if (httpClient != null) { - httpClient.closeHttpClient(); - } - } - - public IConfiguration getConfiguration() { - return configuration; - } - - public void setConfiguration(IConfiguration configuration) { - this.configuration = configuration; - } - - public HttpAsdcClient getHttpClient() { - return httpClient; - } - - public void setHttpClient(HttpAsdcClient httpClient) { - this.httpClient = httpClient; - } - - public Either, IDistributionClientResult> getServerList() { - Pair getServersResponsePair = performAsdcServerRequest(AsdcUrls.GET_CLUSTER_SERVER_LIST); - HttpAsdcResponse getServersResponse = getServersResponsePair.getFirst(); - - Either, IDistributionClientResult> response; - if (getServersResponse.getStatus() == HttpStatus.SC_OK) { - response = parseGetServersResponse(getServersResponse); - } else { - IDistributionClientResult asdcError = handleAsdcError(getServersResponse); - response = Either.right(asdcError); - - } - handeAsdcConnectionClose(getServersResponsePair); - return response; - - } - - public Either, IDistributionClientResult> getValidArtifactTypesList() { - Pair getServersResponsePair = performAsdcServerRequest(AsdcUrls.GET_VALID_ARTIFACT_TYPES); - HttpAsdcResponse getArtifactTypeResponse = getServersResponsePair.getFirst(); - - Either, IDistributionClientResult> response; - if (getArtifactTypeResponse.getStatus() == HttpStatus.SC_OK) { - response = parseGetValidArtifactTypesResponse(getArtifactTypeResponse); - } else { - IDistributionClientResult asdcError = handleAsdcError(getArtifactTypeResponse); - response = Either.right(asdcError); - - } - handeAsdcConnectionClose(getServersResponsePair); - return response; - - } - - private void handeAsdcConnectionClose(Pair getServersResponsePair) { - if (getServersResponsePair.getSecond() != null) { - try { - getServersResponsePair.getSecond().close(); - - } catch (IOException e) { - log.error("failed to close http response"); - } - - } - } - - private Pair performAsdcServerRequest(final String url) { - String requestId = UUID.randomUUID().toString(); - Map requestHeaders = addHeadersToHttpRequest(requestId); - log.debug("about to perform getServerList. requestId= {} url= {}", requestId, url); - Pair getServersResponsePair = httpClient.getRequest(url, requestHeaders, false); - return getServersResponsePair; - } - - public Either registerAsdcTopics(ApiCredential credential) { - - Either response = null; - - String requestId = UUID.randomUUID().toString(); - Map requestHeaders = addHeadersToHttpRequest(requestId); - - RegistrationRequest registrationRequest = new RegistrationRequest(credential.getApiKey(), configuration.getEnvironmentName()); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String jsonRequest = gson.toJson(registrationRequest); - StringEntity body = new StringEntity(jsonRequest, ContentType.APPLICATION_JSON); - - log.debug("about to perform registerAsdcTopics. requestId= " + requestId + " url= " + AsdcUrls.POST_FOR_TOPIC_REGISTRATION); - Pair registerResponsePair = httpClient.postRequest(AsdcUrls.POST_FOR_TOPIC_REGISTRATION, body, requestHeaders, false); - HttpAsdcResponse registerResponse = registerResponsePair.getFirst(); - int status = registerResponse.getStatus(); - - if (status == HttpStatus.SC_OK) { - response = parseRegistrationResponse(registerResponse); - - } else { - DistributionClientResultImpl asdcError = handleAsdcError(registerResponse); - return Either.right(asdcError); - } - handeAsdcConnectionClose(registerResponsePair); - - log.debug("registerAsdcTopics response= " + status + ". requestId= " + requestId + " url= " + AsdcUrls.POST_FOR_TOPIC_REGISTRATION); - return response; - - } - - public IDistributionClientResult unregisterTopics(ApiCredential credential) { - - DistributionClientResultImpl response = null; - - String requestId = UUID.randomUUID().toString(); - HttpAsdcClient httpClient = new HttpAsdcClient(configuration); - Map requestHeaders = addHeadersToHttpRequest(requestId); - - RegistrationRequest registrationRequest = new RegistrationRequest(credential.getApiKey(), configuration.getEnvironmentName()); - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - String jsonRequest = gson.toJson(registrationRequest); - StringEntity body = new StringEntity(jsonRequest, ContentType.APPLICATION_JSON); - - log.debug("about to perform unregisterTopics. requestId= " + requestId + " url= " + AsdcUrls.POST_FOR_UNREGISTER); - Pair unRegisterResponsePair = httpClient.postRequest(AsdcUrls.POST_FOR_UNREGISTER, body, requestHeaders, false); - HttpAsdcResponse unRegisterResponse = unRegisterResponsePair.getFirst(); - int status = unRegisterResponse.getStatus(); - if (status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_OK) { - response = new DistributionClientResultImpl(DistributionActionResultEnum.SUCCESS, "unregistration successful"); - - } else { - response = handleAsdcError(unRegisterResponse); - } - - handeAsdcConnectionClose(unRegisterResponsePair); - - log.debug("unregisterTopics response = " + status + ". requestId= " + requestId + " url= " + AsdcUrls.POST_FOR_UNREGISTER); - - return response; - - } - - public DistributionClientDownloadResultImpl dowloadArtifact(IArtifactInfo artifactInfo) { - DistributionClientDownloadResultImpl response = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to download artifact from ASDC"); - - String requestId = UUID.randomUUID().toString(); - Map requestHeaders = new HashMap(); - requestHeaders.put(DistributionClientConstants.HEADER_REQUEST_ID, requestId); - requestHeaders.put(DistributionClientConstants.HEADER_INSTANCE_ID, configuration.getConsumerID()); - requestHeaders.put(HttpHeaders.ACCEPT, ContentType.APPLICATION_OCTET_STREAM.toString()); - String requestUrl = artifactInfo.getArtifactURL(); - Pair downloadPair = httpClient.getRequest(requestUrl, requestHeaders, false); - HttpAsdcResponse downloadResponse = downloadPair.getFirst(); - - int status = downloadResponse.getStatus(); - if (status == 200) { - - response = parseDownloadArtifactResponse(artifactInfo, downloadResponse); - } else { - response = handleAsdcDownloadArtifactError(downloadResponse); - - } - handeAsdcConnectionClose(downloadPair); - return response; - } - - /* **************************** private methods ********************************************/ - - private Either, IDistributionClientResult> parseGetServersResponse(HttpAsdcResponse getServersResponse) { - Either, IDistributionClientResult> result; - try { - String jsonMessage = IOUtils.toString(getServersResponse.getMessage().getContent()); - - Gson gson = new GsonBuilder().create(); - ServerListResponse serverListResponse = gson.fromJson(jsonMessage, ServerListResponse.class); - List serverList = serverListResponse.getUebServerList(); - result = Either.left(serverList); - - } catch (UnsupportedOperationException | IOException e) { - result = handleParsingError(e); - } - - return result; - } - - private Either, IDistributionClientResult> parseGetValidArtifactTypesResponse(HttpAsdcResponse getArtifactTypesResponse) { - Either, IDistributionClientResult> result; - try { - String jsonMessage = IOUtils.toString(getArtifactTypesResponse.getMessage().getContent()); - Type listType = new TypeToken>() { - }.getType(); - Gson gson = new GsonBuilder().create(); - List artifactTypesList = gson.fromJson(jsonMessage, listType); - result = Either.left(artifactTypesList); - - } catch (UnsupportedOperationException | IOException e) { - result = handleParsingError(e); - } - - return result; - } - - private Either, IDistributionClientResult> handleParsingError(Exception e) { - Either, IDistributionClientResult> result; - log.error("failed to parse response from ASDC. error: " + e.getMessage()); - IDistributionClientResult response = new DistributionClientResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to parse response from ASDC"); - result = Either.right(response); - return result; - } - - private Either parseRegistrationResponse(HttpAsdcResponse registerResponse) { - - String jsonMessage; - try { - jsonMessage = IOUtils.toString(registerResponse.getMessage().getContent()); - - Gson gson = new GsonBuilder().create(); - TopicRegistrationResponse registrationResponse = gson.fromJson(jsonMessage, TopicRegistrationResponse.class); - - if (registrationResponse.getDistrNotificationTopicName() == null) { - DistributionClientResultImpl response = new DistributionClientResultImpl(DistributionActionResultEnum.FAIL, "failed to receive notification topic from ASDC"); - return Either.right(response); - } - - if (registrationResponse.getDistrStatusTopicName() == null) { - DistributionClientResultImpl response = new DistributionClientResultImpl(DistributionActionResultEnum.FAIL, "failed to receive status topic from ASDC"); - return Either.right(response); - } - return Either.left(registrationResponse); - - } catch (UnsupportedOperationException | IOException e) { - log.error("failed to pars response from ASDC. error: " + e.getMessage()); - DistributionClientResultImpl response = new DistributionClientResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to parse response from ASDC"); - return Either.right(response); - } - } - - private Map addHeadersToHttpRequest(String requestId) { - Map requestHeaders = new HashMap(); - requestHeaders.put(DistributionClientConstants.HEADER_REQUEST_ID, requestId); - requestHeaders.put(DistributionClientConstants.HEADER_INSTANCE_ID, configuration.getConsumerID()); - requestHeaders.put(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); - - return requestHeaders; - } - - private DistributionClientResultImpl handleAsdcError(HttpAsdcResponse registerResponse) { - int status = registerResponse.getStatus(); - DistributionClientResultImpl errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to send request to ASDC"); - if (status == HttpStatus.SC_UNAUTHORIZED) { - errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_AUTHENTICATION_FAILED, "authentication to ASDC failed for user " + configuration.getUser()); - } else if (status == HttpStatus.SC_FORBIDDEN) { - errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_AUTHORIZATION_FAILED, "authorization failure for user " + configuration.getUser()); - } else if (status == HttpStatus.SC_BAD_REQUEST) { - errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.BAD_REQUEST, "ASDC call failed due to missing information"); - } else if (status == HttpStatus.SC_NOT_FOUND) { - errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_NOT_FOUND, "ASDC not found"); - } else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { - errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, "ASDC server problem"); - } else if (status == HttpStatus.SC_BAD_GATEWAY) { - errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, "ASDC server problem"); - } else if (status == HttpStatus.SC_GATEWAY_TIMEOUT) { - errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_SERVER_TIMEOUT, "ASDC server problem"); - } - log.error("status from ASDC is " + registerResponse); - log.error(errorResponse.toString()); - try { - String errorString = IOUtils.toString(registerResponse.getMessage().getContent()); - log.debug("error from ASDC is: " + errorString); - } catch (UnsupportedOperationException | IOException e) { - } - return errorResponse; - - } - - private DistributionClientDownloadResultImpl handleAsdcDownloadArtifactError(HttpAsdcResponse registerResponse) { - int status = registerResponse.getStatus(); - DistributionClientDownloadResultImpl errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to send request to ASDC"); - if (status == HttpStatus.SC_UNAUTHORIZED) { - errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.ASDC_AUTHENTICATION_FAILED, "authentication to ASDC failed for user " + configuration.getUser()); - } else if (status == HttpStatus.SC_FORBIDDEN) { - errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.ASDC_AUTHORIZATION_FAILED, "authorization failure for user " + configuration.getUser()); - } else if (status == HttpStatus.SC_BAD_REQUEST || status == HttpStatus.SC_NOT_FOUND) { - errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.ARTIFACT_NOT_FOUND, "Specified artifact is not found"); - // } else if (status == 404){ - // errorResponse = new DistributionClientDownloadResultImpl( - // DistributionActionResultEnum.ASDC_NOT_FOUND, - // "ASDC not found"); - } else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { - errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, "ASDC server problem"); - } - log.error("status from ASDC is " + registerResponse); - log.error(errorResponse.toString()); - try { - String errorString = IOUtils.toString(registerResponse.getMessage().getContent()); - log.debug("error from ASDC is: " + errorString); - } catch (UnsupportedOperationException | IOException e) { - } - return errorResponse; - - } - - private DistributionClientDownloadResultImpl parseDownloadArtifactResponse(IArtifactInfo artifactInfo, HttpAsdcResponse getServersResponse) { - HttpEntity entity = getServersResponse.getMessage(); - InputStream is; - try { - is = entity.getContent(); - String artifactName = ""; - if (getServersResponse.getHeadersMap().containsKey(contentDispositionHeader)) - artifactName = getServersResponse.getHeadersMap().get(contentDispositionHeader); - - byte[] payload = IOUtils.toByteArray(is); - if (artifactInfo.getArtifactChecksum() == null || artifactInfo.getArtifactChecksum().isEmpty()) { - return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.DATA_INTEGRITY_PROBLEM, "failed to get artifact from ASDC. Empty checksum"); - } - - DistributionClientDownloadResultImpl resResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS, "success", artifactName, payload); - return resResponse; - - //Validate checksum removed as fix to bug 293657 - /*if (validateChecksum(artifactInfo, payload)) { - DistributionClientDownloadResultImpl resResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS, "success", artifactName, payload); - return resResponse; - - } else { - - return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "Invalid checksum. ArtifactInfo checksum "); - }*/ - - } catch (UnsupportedOperationException | IOException e) { - log.error("failed to get artifact from response "); - return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "UnsupportedOperationException "); - } - - } - - private boolean validateChecksum(IArtifactInfo artifactInfo, byte[] payload) { - boolean bRes = false; - String calculatedMD5 = GeneralUtils.calculateMD5(payload); - if (artifactInfo.getArtifactChecksum().equals(calculatedMD5)) { - bRes = true; - } - - return bRes; - } - -} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/HttpAsdcClient.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/HttpAsdcClient.java index 7ace38f..c3a486a 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/HttpAsdcClient.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/HttpAsdcClient.java @@ -60,7 +60,6 @@ import org.apache.http.message.BasicHeader; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; import org.openecomp.sdc.api.consumer.IConfiguration; -import org.openecomp.sdc.impl.DistributionClientImpl; import org.openecomp.sdc.utils.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,19 +74,19 @@ public class HttpAsdcClient implements IHttpAsdcClient { private String serverFqdn = null; private String authHeaderValue = ""; - public HttpAsdcClient(IConfiguration configuraion/* String serverFqdn, String username, String password */) { + public HttpAsdcClient(IConfiguration configuraion) { this.serverFqdn = configuraion.getAsdcAddress(); + String username = configuraion.getUser(); String password = configuraion.getPassword(); - - initSSL(serverFqdn, username, password, configuraion.getKeyStorePath(), configuraion.getKeyStorePassword(), configuraion.activateServerTLSAuth()); + initSSL(username, password, configuraion.getKeyStorePath(), configuraion.getKeyStorePassword(), configuraion.activateServerTLSAuth()); String userNameAndPassword = username + ":" + password; this.authHeaderValue = "Basic " + Base64.encodeBase64String(userNameAndPassword.getBytes()); } // @SuppressWarnings("deprecation") - private void initSSL(String serverFqdn, String username, String password, String keyStorePath, String keyStoePass, boolean isSupportSSLVerification) { + private void initSSL(String username, String password, String keyStorePath, String keyStoePass, boolean isSupportSSLVerification) { try { HostnameVerifier hostnameVerifier = new HostnameVerifier() { @@ -245,9 +244,9 @@ public class HttpAsdcClient implements IHttpAsdcClient { log.error("failed to close http response"); } } - ret = new Pair(response, null); + ret = new Pair<>(response, null); } else { - ret = new Pair(response, httpResponse); + ret = new Pair<>(response, httpResponse); } } @@ -264,7 +263,7 @@ public class HttpAsdcClient implements IHttpAsdcClient { Pair ret; CloseableHttpResponse httpResponse = null; String url = HTTPS + serverFqdn + requestUrl; - log.debug("url to send " + url); + log.debug("url to send {}", url); HttpGet httpGet = new HttpGet(url); List
headers = addHeadersToHttpRequest(headersMap); for (Header header : headers) { @@ -277,16 +276,16 @@ public class HttpAsdcClient implements IHttpAsdcClient { try { httpResponse = httpClient.execute(httpGet); - log.debug("GET Response Status " + httpResponse.getStatusLine().getStatusCode()); + log.debug("GET Response Status {}", httpResponse.getStatusLine().getStatusCode()); Header[] headersRes = httpResponse.getAllHeaders(); - Map headersResMap = new HashMap(); + Map headersResMap = new HashMap<>(); for (Header header : headersRes) { headersResMap.put(header.getName(), header.getValue()); } response = new HttpAsdcResponse(httpResponse.getStatusLine().getStatusCode(), httpResponse.getEntity(), headersResMap); } catch (UnknownHostException | ConnectException e) { - log.error("failed to connect to url: " + requestUrl, e); + log.error("failed to connect to url: {}", requestUrl, e); StringEntity errorEntity = null; try { errorEntity = new StringEntity("failed to connect"); diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/SdcConnectorClient.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/SdcConnectorClient.java new file mode 100644 index 0000000..2ae78af --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/http/SdcConnectorClient.java @@ -0,0 +1,398 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.http; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.openecomp.sdc.api.asdc.RegistrationRequest; +import org.openecomp.sdc.api.asdc.ServerListResponse; +import org.openecomp.sdc.api.consumer.IConfiguration; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.results.IDistributionClientResult; +import org.openecomp.sdc.impl.DistributionClientDownloadResultImpl; +import org.openecomp.sdc.impl.DistributionClientResultImpl; +import org.openecomp.sdc.utils.DistributionActionResultEnum; +import org.openecomp.sdc.utils.DistributionClientConstants; +import org.openecomp.sdc.utils.GeneralUtils; +import org.openecomp.sdc.utils.Pair; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.att.nsa.apiClient.credentials.ApiCredential; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +import fj.data.Either; + +public class SdcConnectorClient { + String contentDispositionHeader = "Content-Disposition"; + private static Logger log = LoggerFactory.getLogger(SdcConnectorClient.class.getName()); + private IConfiguration configuration; + private HttpAsdcClient httpClient = null; + + public void init(IConfiguration configuraion) { + this.configuration = configuraion; + httpClient = new HttpAsdcClient(configuration); + } + + public void close() { + if (httpClient != null) { + httpClient.closeHttpClient(); + } + } + + public IConfiguration getConfiguration() { + return configuration; + } + + public void setConfiguration(IConfiguration configuration) { + this.configuration = configuration; + } + + public HttpAsdcClient getHttpClient() { + return httpClient; + } + + public void setHttpClient(HttpAsdcClient httpClient) { + this.httpClient = httpClient; + } + + + public Either, IDistributionClientResult> getValidArtifactTypesList() { + Pair getServersResponsePair = performAsdcServerRequest(AsdcUrls.GET_VALID_ARTIFACT_TYPES); + HttpAsdcResponse getArtifactTypeResponse = getServersResponsePair.getFirst(); + + Either, IDistributionClientResult> response; + if (getArtifactTypeResponse.getStatus() == HttpStatus.SC_OK) { + response = parseGetValidArtifactTypesResponse(getArtifactTypeResponse); + } else { + IDistributionClientResult asdcError = handleAsdcError(getArtifactTypeResponse); + response = Either.right(asdcError); + + } + handeAsdcConnectionClose(getServersResponsePair); + return response; + + } + + private void handeAsdcConnectionClose(Pair getServersResponsePair) { + if (getServersResponsePair.getSecond() != null) { + try { + getServersResponsePair.getSecond().close(); + + } catch (IOException e) { + log.error("failed to close http response"); + } + + } + } + + private Pair performAsdcServerRequest(final String url) { + String requestId = UUID.randomUUID().toString(); + Map requestHeaders = addHeadersToHttpRequest(requestId); + log.debug("about to perform getServerList. requestId= {} url= {}", requestId, url); + Pair getServersResponsePair = httpClient.getRequest(url, requestHeaders, false); + return getServersResponsePair; + } + + public Either registerAsdcTopics(ApiCredential credential) { + + Either response = null; + + String requestId = UUID.randomUUID().toString(); + Map requestHeaders = addHeadersToHttpRequest(requestId); + + RegistrationRequest registrationRequest = new RegistrationRequest(credential.getApiKey(), configuration.getEnvironmentName(), configuration.isConsumeProduceStatusTopic(), configuration.getMsgBusAddress()); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String jsonRequest = gson.toJson(registrationRequest); + StringEntity body = new StringEntity(jsonRequest, ContentType.APPLICATION_JSON); + + log.debug("about to perform registerAsdcTopics. requestId= " + requestId + " url= " + AsdcUrls.POST_FOR_TOPIC_REGISTRATION); + Pair registerResponsePair = httpClient.postRequest(AsdcUrls.POST_FOR_TOPIC_REGISTRATION, body, requestHeaders, false); + HttpAsdcResponse registerResponse = registerResponsePair.getFirst(); + int status = registerResponse.getStatus(); + + if (status == HttpStatus.SC_OK) { + response = parseRegistrationResponse(registerResponse); + + } else { + DistributionClientResultImpl asdcError = handleAsdcError(registerResponse); + return Either.right(asdcError); + } + handeAsdcConnectionClose(registerResponsePair); + + log.debug("registerAsdcTopics response= " + status + ". requestId= " + requestId + " url= " + AsdcUrls.POST_FOR_TOPIC_REGISTRATION); + return response; + + } + + public IDistributionClientResult unregisterTopics(ApiCredential credential) { + + DistributionClientResultImpl response = null; + + String requestId = UUID.randomUUID().toString(); + HttpAsdcClient httpClient = new HttpAsdcClient(configuration); + Map requestHeaders = addHeadersToHttpRequest(requestId); + + RegistrationRequest registrationRequest = new RegistrationRequest(credential.getApiKey(), configuration.getEnvironmentName(), configuration.isConsumeProduceStatusTopic(), configuration.getMsgBusAddress()); + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String jsonRequest = gson.toJson(registrationRequest); + StringEntity body = new StringEntity(jsonRequest, ContentType.APPLICATION_JSON); + + log.debug("about to perform unregisterTopics. requestId= " + requestId + " url= " + AsdcUrls.POST_FOR_UNREGISTER); + Pair unRegisterResponsePair = httpClient.postRequest(AsdcUrls.POST_FOR_UNREGISTER, body, requestHeaders, false); + HttpAsdcResponse unRegisterResponse = unRegisterResponsePair.getFirst(); + int status = unRegisterResponse.getStatus(); + if (status == HttpStatus.SC_NO_CONTENT || status == HttpStatus.SC_OK) { + response = new DistributionClientResultImpl(DistributionActionResultEnum.SUCCESS, "unregistration successful"); + + } else { + response = handleAsdcError(unRegisterResponse); + } + + handeAsdcConnectionClose(unRegisterResponsePair); + + log.debug("unregisterTopics response = " + status + ". requestId= " + requestId + " url= " + AsdcUrls.POST_FOR_UNREGISTER); + + return response; + + } + + public DistributionClientDownloadResultImpl dowloadArtifact(IArtifactInfo artifactInfo) { + DistributionClientDownloadResultImpl response = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to download artifact from ASDC"); + + String requestId = UUID.randomUUID().toString(); + Map requestHeaders = new HashMap(); + requestHeaders.put(DistributionClientConstants.HEADER_REQUEST_ID, requestId); + requestHeaders.put(DistributionClientConstants.HEADER_INSTANCE_ID, configuration.getConsumerID()); + requestHeaders.put(HttpHeaders.ACCEPT, ContentType.APPLICATION_OCTET_STREAM.toString()); + String requestUrl = artifactInfo.getArtifactURL(); + Pair downloadPair = httpClient.getRequest(requestUrl, requestHeaders, false); + HttpAsdcResponse downloadResponse = downloadPair.getFirst(); + + int status = downloadResponse.getStatus(); + if (status == 200) { + + response = parseDownloadArtifactResponse(artifactInfo, downloadResponse); + } else { + response = handleAsdcDownloadArtifactError(downloadResponse); + + } + handeAsdcConnectionClose(downloadPair); + return response; + } + + /* **************************** private methods ********************************************/ + + private Either, IDistributionClientResult> parseGetServersResponse(HttpAsdcResponse getServersResponse) { + Either, IDistributionClientResult> result; + try { + String jsonMessage = IOUtils.toString(getServersResponse.getMessage().getContent()); + + Gson gson = new GsonBuilder().create(); + ServerListResponse serverListResponse = gson.fromJson(jsonMessage, ServerListResponse.class); + List serverList = serverListResponse.getUebServerList(); + result = Either.left(serverList); + + } catch (UnsupportedOperationException | IOException e) { + result = handleParsingError(e); + } + + return result; + } + + private Either, IDistributionClientResult> parseGetValidArtifactTypesResponse(HttpAsdcResponse getArtifactTypesResponse) { + Either, IDistributionClientResult> result; + try { + String jsonMessage = IOUtils.toString(getArtifactTypesResponse.getMessage().getContent()); + Type listType = new TypeToken>() { + }.getType(); + Gson gson = new GsonBuilder().create(); + List artifactTypesList = gson.fromJson(jsonMessage, listType); + result = Either.left(artifactTypesList); + + } catch (UnsupportedOperationException | IOException e) { + result = handleParsingError(e); + } + + return result; + } + + private Either, IDistributionClientResult> handleParsingError(Exception e) { + Either, IDistributionClientResult> result; + log.error("failed to parse response from ASDC. error: " + e.getMessage()); + IDistributionClientResult response = new DistributionClientResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to parse response from ASDC"); + result = Either.right(response); + return result; + } + + Either parseRegistrationResponse(HttpAsdcResponse registerResponse) { + + String jsonMessage; + try { + jsonMessage = IOUtils.toString(registerResponse.getMessage().getContent()); + + Gson gson = new GsonBuilder().create(); + TopicRegistrationResponse registrationResponse = gson.fromJson(jsonMessage, TopicRegistrationResponse.class); + + if (registrationResponse.getDistrNotificationTopicName() == null) { + DistributionClientResultImpl response = new DistributionClientResultImpl(DistributionActionResultEnum.FAIL, "failed to receive notification topic from ASDC"); + return Either.right(response); + } + + if (registrationResponse.getDistrStatusTopicName() == null) { + DistributionClientResultImpl response = new DistributionClientResultImpl(DistributionActionResultEnum.FAIL, "failed to receive status topic from ASDC"); + return Either.right(response); + } + return Either.left(registrationResponse); + + } catch (UnsupportedOperationException | IOException e) { + log.error("failed to pars response from ASDC. error: " + e.getMessage()); + DistributionClientResultImpl response = new DistributionClientResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to parse response from ASDC"); + return Either.right(response); + } + } + + protected Map addHeadersToHttpRequest(String requestId) { + Map requestHeaders = new HashMap<>(); + requestHeaders.put(DistributionClientConstants.HEADER_REQUEST_ID, requestId); + requestHeaders.put(DistributionClientConstants.HEADER_INSTANCE_ID, configuration.getConsumerID()); + requestHeaders.put(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); + + return requestHeaders; + } + + private DistributionClientResultImpl handleAsdcError(HttpAsdcResponse registerResponse) { + int status = registerResponse.getStatus(); + DistributionClientResultImpl errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to send request to ASDC"); + if (status == HttpStatus.SC_UNAUTHORIZED) { + errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_AUTHENTICATION_FAILED, "authentication to ASDC failed for user " + configuration.getUser()); + } else if (status == HttpStatus.SC_FORBIDDEN) { + errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_AUTHORIZATION_FAILED, "authorization failure for user " + configuration.getUser()); + } else if (status == HttpStatus.SC_BAD_REQUEST) { + errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.BAD_REQUEST, "ASDC call failed due to missing information"); + } else if (status == HttpStatus.SC_NOT_FOUND) { + errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_NOT_FOUND, "ASDC not found"); + } else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { + errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, "ASDC server problem"); + } else if (status == HttpStatus.SC_BAD_GATEWAY) { + errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, "ASDC server problem"); + } else if (status == HttpStatus.SC_GATEWAY_TIMEOUT) { + errorResponse = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_SERVER_TIMEOUT, "ASDC server problem"); + } + log.error("status from ASDC is " + registerResponse); + log.error(errorResponse.toString()); + try { + String errorString = IOUtils.toString(registerResponse.getMessage().getContent()); + log.debug("error from ASDC is: " + errorString); + } catch (UnsupportedOperationException | IOException e) { + } + return errorResponse; + + } + + private DistributionClientDownloadResultImpl handleAsdcDownloadArtifactError(HttpAsdcResponse registerResponse) { + int status = registerResponse.getStatus(); + DistributionClientDownloadResultImpl errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "failed to send request to ASDC"); + if (status == HttpStatus.SC_UNAUTHORIZED) { + errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.ASDC_AUTHENTICATION_FAILED, "authentication to ASDC failed for user " + configuration.getUser()); + } else if (status == HttpStatus.SC_FORBIDDEN) { + errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.ASDC_AUTHORIZATION_FAILED, "authorization failure for user " + configuration.getUser()); + } else if (status == HttpStatus.SC_BAD_REQUEST || status == HttpStatus.SC_NOT_FOUND) { + errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.ARTIFACT_NOT_FOUND, "Specified artifact is not found"); + // } else if (status == 404){ + // errorResponse = new DistributionClientDownloadResultImpl( + // DistributionActionResultEnum.ASDC_NOT_FOUND, + // "ASDC not found"); + } else if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR) { + errorResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, "ASDC server problem"); + } + log.error("status from ASDC is " + registerResponse); + log.error(errorResponse.toString()); + try { + String errorString = IOUtils.toString(registerResponse.getMessage().getContent()); + log.debug("error from ASDC is: " + errorString); + } catch (UnsupportedOperationException | IOException e) { + } + return errorResponse; + + } + + private DistributionClientDownloadResultImpl parseDownloadArtifactResponse(IArtifactInfo artifactInfo, HttpAsdcResponse getServersResponse) { + HttpEntity entity = getServersResponse.getMessage(); + InputStream is; + try { + is = entity.getContent(); + String artifactName = ""; + if (getServersResponse.getHeadersMap().containsKey(contentDispositionHeader)) + artifactName = getServersResponse.getHeadersMap().get(contentDispositionHeader); + + byte[] payload = IOUtils.toByteArray(is); + if (artifactInfo.getArtifactChecksum() == null || artifactInfo.getArtifactChecksum().isEmpty()) { + return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.DATA_INTEGRITY_PROBLEM, "failed to get artifact from ASDC. Empty checksum"); + } + + DistributionClientDownloadResultImpl resResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS, "success", artifactName, payload); + return resResponse; + + //Validate checksum removed as fix to bug 293657 + /*if (validateChecksum(artifactInfo, payload)) { + DistributionClientDownloadResultImpl resResponse = new DistributionClientDownloadResultImpl(DistributionActionResultEnum.SUCCESS, "success", artifactName, payload); + return resResponse; + + } else { + + return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "Invalid checksum. ArtifactInfo checksum "); + }*/ + + } catch (UnsupportedOperationException | IOException e) { + log.error("failed to get artifact from response "); + return new DistributionClientDownloadResultImpl(DistributionActionResultEnum.GENERAL_ERROR, "UnsupportedOperationException "); + } + + } + + private boolean validateChecksum(IArtifactInfo artifactInfo, byte[] payload) { + boolean bRes = false; + String calculatedMD5 = GeneralUtils.calculateMD5(payload); + if (artifactInfo.getArtifactChecksum().equals(calculatedMD5)) { + bRes = true; + } + + return bRes; + } + +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/Configuration.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/Configuration.java index dff8a3b..851a198 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/Configuration.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/Configuration.java @@ -22,12 +22,15 @@ package org.openecomp.sdc.impl; import java.util.List; +import fj.data.Either; import org.openecomp.sdc.api.consumer.IConfiguration; +import org.openecomp.sdc.api.results.IDistributionClientResult; +import org.openecomp.sdc.utils.DistributionActionResultEnum; import org.openecomp.sdc.utils.DistributionClientConstants; -public class Configuration implements IConfiguration{ +public class Configuration implements IConfiguration { - + private List msgBusAddressList; private String asdcAddress; private String user; private String password; @@ -39,12 +42,14 @@ public class Configuration implements IConfiguration{ private String comsumerID; private String keyStorePath; private String keyStorePassword; - private boolean activateServerTLSAuth; + private boolean activateServerTLSAuth; private boolean filterInEmptyResources; private Boolean useHttpsWithDmaap; + private boolean consumeProduceStatusTopic; public Configuration(IConfiguration other) { this.asdcAddress = other.getAsdcAddress(); + this.msgBusAddressList = other.getMsgBusAddress(); this.comsumerID = other.getConsumerID(); this.consumerGroup = other.getConsumerGroup(); this.environmentName = other.getEnvironmentName(); @@ -58,6 +63,7 @@ public class Configuration implements IConfiguration{ this.activateServerTLSAuth = other.activateServerTLSAuth(); this.filterInEmptyResources = other.isFilterInEmptyResources(); this.useHttpsWithDmaap = other.isUseHttpsWithDmaap(); + this.consumeProduceStatusTopic = other.isConsumeProduceStatusTopic(); } @Override @@ -65,6 +71,11 @@ public class Configuration implements IConfiguration{ return asdcAddress; } + @Override + public List getMsgBusAddress() { + return msgBusAddressList; + } + @Override public String getUser() { return user; @@ -104,11 +115,11 @@ public class Configuration implements IConfiguration{ public String getConsumerID() { return comsumerID; } - + @Override public String getKeyStorePath() { return keyStorePath; - } + } @Override public String getKeyStorePassword() { @@ -154,7 +165,7 @@ public class Configuration implements IConfiguration{ public void setEnvironmentName(String environmentName) { this.environmentName = environmentName; } - + public void setKeyStorePath(String keyStorePath) { this.keyStorePath = keyStorePath; } @@ -163,7 +174,6 @@ public class Configuration implements IConfiguration{ this.keyStorePassword = keyStorePassword; } - public void setactivateServerTLSAuth(boolean activateServerTLSAuth) { this.activateServerTLSAuth = activateServerTLSAuth; } @@ -172,7 +182,7 @@ public class Configuration implements IConfiguration{ public boolean activateServerTLSAuth() { return this.activateServerTLSAuth; } - + @Override public boolean isFilterInEmptyResources() { return this.filterInEmptyResources; @@ -182,19 +192,37 @@ public class Configuration implements IConfiguration{ public Boolean isUseHttpsWithDmaap() { return this.useHttpsWithDmaap; } - - public void setUseHttpsWithDmaap(boolean useHttpsWithDmaap){ + + public void setUseHttpsWithDmaap(boolean useHttpsWithDmaap) { this.useHttpsWithDmaap = useHttpsWithDmaap; } + @Override + public boolean isConsumeProduceStatusTopic() { + return this.consumeProduceStatusTopic; + } + @Override public String toString() { - return "Configuration [asdcAddress=" + asdcAddress + ", user=" + user + ", password=" + password - + ", pollingInterval=" + pollingInterval + ", pollingTimeout=" + pollingTimeout - + ", relevantArtifactTypes=" + relevantArtifactTypes + ", consumerGroup=" + consumerGroup - + ", environmentName=" + environmentName + ", comsumerID=" + comsumerID + ", keyStorePath=" - + keyStorePath + ", keyStorePassword=" + keyStorePassword + ", activateServerTLSAuth=" - + activateServerTLSAuth + ", filterInEmptyResources=" + filterInEmptyResources + ", useHttpsWithDmaap=" - + useHttpsWithDmaap + "]"; + //@formatter:off + return "Configuration ["+ + "asdcAddress=" + asdcAddress + + ", user=" + user + + ", password=" + password + + ", pollingInterval=" + pollingInterval + + ", pollingTimeout=" + pollingTimeout + + ", relevantArtifactTypes=" + relevantArtifactTypes + + ", consumerGroup=" + consumerGroup + + ", environmentName=" + environmentName + + ", comsumerID=" + comsumerID + + ", keyStorePath=" + keyStorePath + + ", keyStorePassword=" + keyStorePassword + + ", activateServerTLSAuth=" + activateServerTLSAuth + + ", filterInEmptyResources=" + filterInEmptyResources + + ", useHttpsWithDmaap=" + useHttpsWithDmaap + + ", consumeProduceStatusTopic=" + consumeProduceStatusTopic + + "]"; + //@formatter:on } + } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionClientImpl.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionClientImpl.java index 1543256..a8778e5 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionClientImpl.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionClientImpl.java @@ -20,6 +20,8 @@ package org.openecomp.sdc.impl; +import static java.util.Objects.isNull; + import java.io.IOException; import java.lang.reflect.Type; import java.net.MalformedURLException; @@ -27,6 +29,7 @@ import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -35,17 +38,16 @@ import java.util.regex.Matcher; import org.openecomp.sdc.api.IDistributionClient; import org.openecomp.sdc.api.IDistributionStatusMessageJsonBuilder; -import org.openecomp.sdc.api.consumer.IConfiguration; -import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; -import org.openecomp.sdc.api.consumer.INotificationCallback; +import org.openecomp.sdc.api.consumer.*; import org.openecomp.sdc.api.notification.IArtifactInfo; import org.openecomp.sdc.api.notification.IVfModuleMetadata; import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; import org.openecomp.sdc.api.results.IDistributionClientResult; -import org.openecomp.sdc.http.AsdcConnectorClient; +import org.openecomp.sdc.http.SdcConnectorClient; import org.openecomp.sdc.http.TopicRegistrationResponse; import org.openecomp.sdc.utils.DistributionActionResultEnum; import org.openecomp.sdc.utils.DistributionClientConstants; +import org.openecomp.sdc.utils.GeneralUtils; import org.openecomp.sdc.utils.Wrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,13 +73,14 @@ public class DistributionClientImpl implements IDistributionClient { private static Logger log = LoggerFactory.getLogger(DistributionClientImpl.class.getName()); - protected AsdcConnectorClient asdcConnector = new AsdcConnectorClient(); + protected SdcConnectorClient asdcConnector = new SdcConnectorClient(); private ScheduledExecutorService executorPool = null; protected CambriaIdentityManager cambriaIdentityManager = null; private List brokerServers; protected ApiCredential credential; protected Configuration configuration; private INotificationCallback callback; + private IStatusCallback statusCallback; private String notificationTopic; private String statusTopic; private boolean isConsumerGroupGenerated = false; @@ -139,7 +142,8 @@ public class DistributionClientImpl implements IDistributionClient { public synchronized IDistributionClientResult start() { log.info("start DistributionClient"); - CambriaConsumer cambriaConsumer = null; + IDistributionClientResult startResult; + CambriaConsumer cambriaNotificationConsumer = null; Wrapper errorWrapper = new Wrapper<>(); validateRunReady(errorWrapper); if (errorWrapper.isEmpty()) { @@ -147,30 +151,49 @@ public class DistributionClientImpl implements IDistributionClient { } if (errorWrapper.isEmpty()) { try { - cambriaConsumer = new ConsumerBuilder().authenticatedBy(credential.getApiKey(), credential.getApiSecret()).knownAs(configuration.getConsumerGroup(), configuration.getConsumerID()).onTopic(notificationTopic).usingHttps(configuration.isUseHttpsWithDmaap()).usingHosts(brokerServers) + cambriaNotificationConsumer = new ConsumerBuilder().authenticatedBy(credential.getApiKey(), credential.getApiSecret()).knownAs(configuration.getConsumerGroup(), configuration.getConsumerID()).onTopic(notificationTopic).usingHttps(configuration.isUseHttpsWithDmaap()).usingHosts(brokerServers) .withSocketTimeout(configuration.getPollingTimeout() * 1000).build(); + } catch (MalformedURLException | GeneralSecurityException e) { handleCambriaInitFailure(errorWrapper, e); } } - + if (errorWrapper.isEmpty()) { + + List relevantArtifactTypes = configuration.getRelevantArtifactTypes(); + // Remove nulls from list - workaround for how configuration is built + while (relevantArtifactTypes.remove(null)); + + NotificationConsumer consumer = new NotificationConsumer(cambriaNotificationConsumer, callback, relevantArtifactTypes, this); + executorPool = Executors.newScheduledThreadPool(DistributionClientConstants.POOL_SIZE); + executorPool.scheduleAtFixedRate(consumer, 0, configuration.getPollingInterval(), TimeUnit.SECONDS); + + handleStatusConsumer(errorWrapper, executorPool); + } if (!errorWrapper.isEmpty()) { - return errorWrapper.getInnerElement(); + startResult = errorWrapper.getInnerElement(); + } + else{ + startResult = new DistributionClientResultImpl(DistributionActionResultEnum.SUCCESS, "distribution client started successfuly"); + isStarted = true; } - - List relevantArtifactTypes = configuration.getRelevantArtifactTypes(); - // Remove nulls from list - workaround for how configuration is built - while (relevantArtifactTypes.remove(null)); - - NotificationConsumer consumer = new NotificationConsumer(cambriaConsumer, callback, relevantArtifactTypes, this); - executorPool = Executors.newScheduledThreadPool(DistributionClientConstants.POOL_SIZE); - executorPool.scheduleAtFixedRate(consumer, 0, configuration.getPollingInterval(), TimeUnit.SECONDS); - - DistributionClientResultImpl startResult = new DistributionClientResultImpl(DistributionActionResultEnum.SUCCESS, "distribution client started successfuly"); - isStarted = true; return startResult; } + private void handleStatusConsumer(Wrapper errorWrapper, ScheduledExecutorService executorPool) { + if( configuration.isConsumeProduceStatusTopic()){ + CambriaConsumer cambriaStatusConsumer = null; + try { + cambriaStatusConsumer = new ConsumerBuilder().authenticatedBy(credential.getApiKey(), credential.getApiSecret()).knownAs(configuration.getConsumerGroup(), configuration.getConsumerID()).onTopic(statusTopic).usingHttps(configuration.isUseHttpsWithDmaap()).usingHosts(brokerServers) + .withSocketTimeout(configuration.getPollingTimeout() * 1000).build(); + StatusConsumer statusConsumer = new StatusConsumer(cambriaStatusConsumer, statusCallback); + executorPool.scheduleAtFixedRate(statusConsumer, 0, configuration.getPollingInterval(), TimeUnit.SECONDS); + } catch (MalformedURLException | GeneralSecurityException e) { + handleCambriaInitFailure(errorWrapper, e); + } + } + } + @Override /* see javadoc */ public synchronized IDistributionClientResult stop() { @@ -219,7 +242,24 @@ public class DistributionClientImpl implements IDistributionClient { } return asdcConnector.dowloadArtifact(artifactInfo); } + @Override + public synchronized IDistributionClientResult init(IConfiguration conf, INotificationCallback notificationCallback, + IStatusCallback statusCallback) { + IDistributionClientResult initResult; + if( !conf.isConsumeProduceStatusTopic() ){ + initResult = new DistributionClientResultImpl(DistributionActionResultEnum.CONF_INVALID_CONSUME_PRODUCE_STATUS_TOPIC_FALG, "configuration is invalid: isConsumeProduceStatusTopic() should be set to 'true'" ); + } + else if( isNull(statusCallback) ){ + initResult = new DistributionClientResultImpl(DistributionActionResultEnum.CONF_INVALID_CONSUME_PRODUCE_STATUS_TOPIC_FALG, "configuration is invalid: statusCallback is not defined" ); + } + else{ + this.statusCallback = statusCallback; + initResult = init(conf, notificationCallback); + } + return initResult; + } + @Override /* * see javadoc @@ -236,7 +276,7 @@ public class DistributionClientImpl implements IDistributionClient { if (errorWrapper.isEmpty()) { validateAndInitConfiguration(errorWrapper, conf); } - // 1. get servers list from ASDC + // 1. get ueb server list from configuration if (errorWrapper.isEmpty()) { initUebServerList(errorWrapper); } @@ -295,6 +335,7 @@ public class DistributionClientImpl implements IDistributionClient { } private void validateArtifactTypesWithAsdcServer(IConfiguration conf, Wrapper errorWrapper) { + asdcConnector.init(configuration); Either, IDistributionClientResult> eitherValidArtifactTypesList = asdcConnector.getValidArtifactTypesList(); if (eitherValidArtifactTypesList.isRight()) { DistributionActionResultEnum errorType = eitherValidArtifactTypesList.right().value().getDistributionActionResult(); @@ -319,15 +360,16 @@ public class DistributionClientImpl implements IDistributionClient { } private void initUebServerList(Wrapper errorWrapper) { - log.debug("get cluster server list from ASDC"); - asdcConnector.init(configuration); + log.debug("get ueb cluster server list from component(configuration file)"); - Either, IDistributionClientResult> serverListResponse = asdcConnector.getServerList(); + Either, IDistributionClientResult> serverListResponse = getUEBServerList(); if (serverListResponse.isRight()) { errorWrapper.setInnerElement(serverListResponse.right().value()); } else { + brokerServers = serverListResponse.left().value(); } + } private void validateNotInitilized(Wrapper errorWrapper) { @@ -457,32 +499,25 @@ public class DistributionClientImpl implements IDistributionClient { result = DistributionActionResultEnum.CONF_MISSING_USERNAME; } else if (conf.getPassword() == null || conf.getPassword().isEmpty()) { result = DistributionActionResultEnum.CONF_MISSING_PASSWORD; + } else if (conf.getMsgBusAddress() == null || conf.getMsgBusAddress().isEmpty()) { + result = DistributionActionResultEnum.CONF_MISSING_MSG_BUS_ADDRESS; } else if (conf.getAsdcAddress() == null || conf.getAsdcAddress().isEmpty()) { result = DistributionActionResultEnum.CONF_MISSING_ASDC_FQDN; } else if (!isValidFqdn(conf.getAsdcAddress())) { result = DistributionActionResultEnum.CONF_INVALID_ASDC_FQDN; + } else if (!isValidFqdns(conf.getMsgBusAddress())){ + result = DistributionActionResultEnum.CONF_INVALID_MSG_BUS_ADDRESS; } else if (conf.getEnvironmentName() == null || conf.getEnvironmentName().isEmpty()) { result = DistributionActionResultEnum.CONF_MISSING_ENVIRONMENT_NAME; } else if (conf.getRelevantArtifactTypes() == null || conf.getRelevantArtifactTypes().isEmpty()) { result = DistributionActionResultEnum.CONF_MISSING_ARTIFACT_TYPES; } + else if( conf.isConsumeProduceStatusTopic() && Objects.isNull(statusCallback) ){ + result = DistributionActionResultEnum.CONF_INVALID_CONSUME_PRODUCE_STATUS_TOPIC_FALG; + } // DistributionActionResultEnum.SUCCESS else { - this.configuration = new Configuration(conf); - if (!isPollingIntervalValid(conf.getPollingInterval())) { - configuration.setPollingInterval(DistributionClientConstants.MIN_POLLING_INTERVAL_SEC); - } - if (!isPollingTimeoutValid(conf.getPollingTimeout())) { - configuration.setPollingTimeout(DistributionClientConstants.POLLING_TIMEOUT_SEC); - } - if (conf.getConsumerGroup() == null) { - generateConsumerGroup(); - } - - //Default use HTTPS with DMAAP - if (conf.isUseHttpsWithDmaap() == null){ - configuration.setUseHttpsWithDmaap(true); - } + handleValidConf(conf); } if (result != DistributionActionResultEnum.SUCCESS) { @@ -495,6 +530,24 @@ public class DistributionClientImpl implements IDistributionClient { return result; } + private void handleValidConf(IConfiguration conf) { + this.configuration = new Configuration(conf); + if (!isPollingIntervalValid(conf.getPollingInterval())) { + configuration.setPollingInterval(DistributionClientConstants.MIN_POLLING_INTERVAL_SEC); + } + if (!isPollingTimeoutValid(conf.getPollingTimeout())) { + configuration.setPollingTimeout(DistributionClientConstants.POLLING_TIMEOUT_SEC); + } + if (conf.getConsumerGroup() == null) { + generateConsumerGroup(); + } + + //Default use HTTPS with DMAAP + if (conf.isUseHttpsWithDmaap() == null){ + configuration.setUseHttpsWithDmaap(true); + } + } + private void generateConsumerGroup() { String generatedConsumerGroup = UUID.randomUUID().toString(); configuration.setConsumerGroup(generatedConsumerGroup); @@ -509,6 +562,19 @@ public class DistributionClientImpl implements IDistributionClient { } return false; } + protected boolean isValidFqdns(List fqdns) { + if (fqdns != null && !fqdns.isEmpty()) { + for (String fqdn : fqdns) { + if (isValidFqdn(fqdn)) { + continue; + } else { + return false; + } + } + return true; + } + return false; + } private void shutdownExecutor() { if (executorPool == null) @@ -632,6 +698,31 @@ public class DistributionClientImpl implements IDistributionClient { } + @Override + public IDistributionClientResult sendComponentDoneStatus(IComponentDoneStatusMessage statusMessage) { + log.info("DistributionClient - sendComponentDone status"); + Wrapper errorWrapper = new Wrapper<>(); + validateRunReady(errorWrapper); + if (!errorWrapper.isEmpty()) { + return errorWrapper.getInnerElement(); + } + return sendStatus(DistributionStatusMessageJsonBuilderFactory.getSimpleBuilder(statusMessage)); + + } + + @Override + public IDistributionClientResult sendComponentDoneStatus(IComponentDoneStatusMessage statusMessage, + String errorReason) { + log.info("DistributionClient - sendComponentDone status with errorReason"); + Wrapper errorWrapper = new Wrapper<>(); + validateRunReady(errorWrapper); + if (!errorWrapper.isEmpty()) { + return errorWrapper.getInnerElement(); + } + return sendStatus(DistributionStatusMessageJsonBuilderFactory.getErrorReasonBuilder(statusMessage, errorReason)); + } + + @Override public List decodeVfModuleArtifact(byte[] artifactPayload) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); @@ -641,4 +732,47 @@ public class DistributionClientImpl implements IDistributionClient { List vfModules = gson.fromJson(vfModuleJsonString, type); return vfModules; } + + + public IDistributionClientResult sendFinalDistrStatus(IFinalDistrStatusMessage statusMessage) { + log.info("DistributionClient - sendFinalDistributionStatus status"); + Wrapper errorWrapper = new Wrapper<>(); + validateRunReady(errorWrapper); + if (!errorWrapper.isEmpty()) { + return errorWrapper.getInnerElement(); + } + return sendStatus(DistributionStatusMessageJsonBuilderFactory.getSimpleBuilder(statusMessage)); + + } + + + @Override + public IDistributionClientResult sendFinalDistrStatus(IFinalDistrStatusMessage statusMessage, + String errorReason) { + log.info("DistributionClient - sendFinalDistributionStatus status with errorReason"); + Wrapper errorWrapper = new Wrapper<>(); + validateRunReady(errorWrapper); + if (!errorWrapper.isEmpty()) { + return errorWrapper.getInnerElement(); + } + return sendStatus(DistributionStatusMessageJsonBuilderFactory.getErrorReasonBuilder(statusMessage, errorReason)); + + + } + + public Either,IDistributionClientResult> getUEBServerList() { + List msgBusAddresses = configuration.getMsgBusAddress(); + if(msgBusAddresses.isEmpty()){ + return Either.right(new DistributionClientResultImpl(DistributionActionResultEnum.CONF_MISSING_MSG_BUS_ADDRESS, "Message bus address was not found in the config file")); + } + else{ + return GeneralUtils.convertToValidHostName(msgBusAddresses); + } + } + + + + + + } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionStatusMessageImpl.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionStatusMessageImpl.java index d570142..d9ed654 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionStatusMessageImpl.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionStatusMessageImpl.java @@ -20,78 +20,80 @@ package org.openecomp.sdc.impl; +import org.openecomp.sdc.api.consumer.IComponentDoneStatusMessage; import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; +import org.openecomp.sdc.api.consumer.IFinalDistrStatusMessage; import org.openecomp.sdc.utils.DistributionStatusEnum; class DistributionStatusMessageImpl implements IDistributionStatusMessage { - - + String distributionID; String consumerID; long timestamp; String artifactURL; DistributionStatusEnum status; String errorReason; - - - public DistributionStatusMessageImpl(IDistributionStatusMessage message){ + private String componentName; + + public DistributionStatusMessageImpl(IDistributionStatusMessage message) { super(); distributionID = message.getDistributionID(); consumerID = message.getConsumerID(); timestamp = message.getTimestamp(); artifactURL = message.getArtifactURL(); status = message.getStatus(); - + + } + + public DistributionStatusMessageImpl(IComponentDoneStatusMessage message) { + super(); + distributionID = message.getDistributionID(); + consumerID = message.getConsumerID(); + timestamp = message.getTimestamp(); + artifactURL = message.getArtifactURL(); + status = message.getStatus(); + componentName = message.getComponentName(); + } + + public DistributionStatusMessageImpl(IFinalDistrStatusMessage message) { + super(); + distributionID = message.getDistributionID(); + consumerID = message.getConsumerID(); + timestamp = message.getTimestamp(); + + artifactURL = ""; + status = message.getStatus(); + componentName = message.getComponentName(); } @Override public String getDistributionID() { - + return distributionID; } @Override public String getConsumerID() { - + return consumerID; } @Override public long getTimestamp() { - + return timestamp; } @Override public String getArtifactURL() { - + return artifactURL; } @Override public DistributionStatusEnum getStatus() { - - return status; - } - - public void setDistributionID(String distributionID) { - this.distributionID = distributionID; - } - - public void setConsumerID(String consumerID) { - this.consumerID = consumerID; - } - - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } - - public void setArtifactURL(String artifactURL) { - this.artifactURL = artifactURL; - } - public void setStatus(DistributionStatusEnum status) { - this.status = status; + return status; } public String getErrorReason() { @@ -102,4 +104,10 @@ class DistributionStatusMessageImpl implements IDistributionStatusMessage { this.errorReason = errorReason; } + public String getComponentName() { + return componentName; + } + + + } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionStatusMessageJsonBuilderFactory.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionStatusMessageJsonBuilderFactory.java index 9d97805..e2f9f89 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionStatusMessageJsonBuilderFactory.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/DistributionStatusMessageJsonBuilderFactory.java @@ -21,7 +21,9 @@ package org.openecomp.sdc.impl; import org.openecomp.sdc.api.IDistributionStatusMessageJsonBuilder; +import org.openecomp.sdc.api.consumer.IComponentDoneStatusMessage; import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; +import org.openecomp.sdc.api.consumer.IFinalDistrStatusMessage; import org.openecomp.sdc.utils.DistributionStatusEnum; import com.google.gson.Gson; @@ -35,6 +37,18 @@ public class DistributionStatusMessageJsonBuilderFactory { return prepareBuilderFromImpl(message); } + + public static IDistributionStatusMessageJsonBuilder getSimpleBuilder(IComponentDoneStatusMessage statusMessage){ + DistributionStatusMessageImpl message = new DistributionStatusMessageImpl(statusMessage); + + return prepareBuilderFromImpl(message); + } + + public static IDistributionStatusMessageJsonBuilder getSimpleBuilder(IFinalDistrStatusMessage statusMessage){ + DistributionStatusMessageImpl message = new DistributionStatusMessageImpl(statusMessage); + + return prepareBuilderFromImpl(message); + } public static IDistributionStatusMessageJsonBuilder getErrorReasonBuilder(IDistributionStatusMessage statusMessage, String errorReason){ DistributionStatusMessageImpl message = new DistributionStatusMessageImpl(statusMessage); @@ -42,6 +56,20 @@ public class DistributionStatusMessageJsonBuilderFactory { return prepareBuilderFromImpl(message); } + + public static IDistributionStatusMessageJsonBuilder getErrorReasonBuilder(IComponentDoneStatusMessage statusMessage, + String errorReason) { + DistributionStatusMessageImpl message = new DistributionStatusMessageImpl(statusMessage); + message.setErrorReason(errorReason); + return prepareBuilderFromImpl(message); + } + + public static IDistributionStatusMessageJsonBuilder getErrorReasonBuilder(IFinalDistrStatusMessage statusMessage, + String errorReason) { + DistributionStatusMessageImpl message = new DistributionStatusMessageImpl(statusMessage); + message.setErrorReason(errorReason); + return prepareBuilderFromImpl(message); + } static IDistributionStatusMessageJsonBuilder prepareBuilderForNotificationStatus(final String consumerId, final long currentTimeMillis, final String distributionId, final ArtifactInfoImpl artifactInfo, boolean isNotified){ @@ -112,6 +140,9 @@ public class DistributionStatusMessageJsonBuilderFactory { private enum DistributionStatusNotificationEnum { NOTIFIED, NOT_NOTIFIED } + + + } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/StatusConsumer.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/StatusConsumer.java new file mode 100644 index 0000000..34d85ec --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/StatusConsumer.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.impl; + +import org.openecomp.sdc.api.consumer.IStatusCallback; +import org.openecomp.sdc.api.notification.IStatusData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.att.nsa.cambria.client.CambriaConsumer; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +class StatusConsumer implements Runnable { + + private static Logger log = LoggerFactory.getLogger(StatusConsumer.class.getName()); + + private CambriaConsumer cambriaConsumer; + private IStatusCallback clientCallback; + + public StatusConsumer(CambriaConsumer cambriaConsumer, IStatusCallback clientCallback) { + this.cambriaConsumer = cambriaConsumer; + this.clientCallback = clientCallback; + } + + @Override + public void run() { + + try { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + for (String statusMsg : cambriaConsumer.fetch()) { + log.debug("received message from topic"); + log.debug("recieved notification from broker: {}", statusMsg); + IStatusData statusData = gson.fromJson(statusMsg, StatusDataImpl.class); + clientCallback.activateCallback(statusData); + + + } + + } catch (Exception e) { + log.error("Error exception occured when fetching with Cambria Client:{}", e.getMessage()); + log.debug("Error exception occured when fetching with Cambria Client:{}", e.getMessage(), e); + } + } + + + +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/StatusDataImpl.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/StatusDataImpl.java new file mode 100644 index 0000000..119a7f3 --- /dev/null +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/StatusDataImpl.java @@ -0,0 +1,95 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * 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.sdc.impl; + +import org.openecomp.sdc.api.notification.IStatusData; +import org.openecomp.sdc.utils.DistributionStatusEnum; + +public class StatusDataImpl implements IStatusData{ + + String distributionID; + String consumerID; + long timestamp; + String artifactURL; + DistributionStatusEnum status; + String componentName; + String errorReason; + + @Override + public String getDistributionID() { + return distributionID; + } + + public void setDistributionID(String distributionId) { + this.distributionID = distributionId; + } + @Override + public String getConsumerID() { + return consumerID; + } + + public void setConsumerID(String consumerId) { + this.consumerID = consumerId; + } + @Override + public Long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + @Override + public String getArtifactURL() { + return artifactURL; + } + + public void setArtifactURL(String artifactURL) { + this.artifactURL = artifactURL; + } + @Override + public DistributionStatusEnum getStatus() { + return status; + } + + public void setStatus(DistributionStatusEnum status) { + this.status = status; + } + + + @Override + public String toString() { + return "StatusDataImpl [distributionId=" + distributionID + ", consumerId=" + consumerID + ", timestamp=" + timestamp + ", artifactURL=" + artifactURL + ", status=" + status +", errorReason=" + errorReason+ "]"; + } + @Override + public String getComponentName() { + return componentName; + } + + @Override + public String getErrorReason() { + return errorReason; + } + + + + +} diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/mock/DistributionClientStubImpl.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/mock/DistributionClientStubImpl.java index b8044cd..1730290 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/mock/DistributionClientStubImpl.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/impl/mock/DistributionClientStubImpl.java @@ -23,18 +23,14 @@ package org.openecomp.sdc.impl.mock; import java.util.List; import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.consumer.IConfiguration; -import org.openecomp.sdc.api.consumer.IDistributionStatusMessage; -import org.openecomp.sdc.api.consumer.INotificationCallback; +import org.openecomp.sdc.api.consumer.*; import org.openecomp.sdc.api.notification.IArtifactInfo; import org.openecomp.sdc.api.notification.IVfModuleMetadata; import org.openecomp.sdc.api.results.IDistributionClientDownloadResult; import org.openecomp.sdc.api.results.IDistributionClientResult; /** Mock Implementation */ public class DistributionClientStubImpl implements IDistributionClient{ - public DistributionClientStubImpl(){ - - } + public IDistributionClientResult updateConfiguration(IConfiguration newConf) { return new DistributionClientResultStubImpl(); @@ -66,6 +62,12 @@ public class DistributionClientStubImpl implements IDistributionClient{ return new DistributionClientResultStubImpl(); } + @Override + public IDistributionClientResult init(IConfiguration conf, INotificationCallback notificationCallback, + IStatusCallback statusCallback) { + return new DistributionClientResultStubImpl(); + } + @Override public IConfiguration getConfiguration() { // TODO Auto-generated method stub @@ -86,11 +88,37 @@ public class DistributionClientStubImpl implements IDistributionClient{ return null; } + @Override + public IDistributionClientResult sendComponentDoneStatus(IComponentDoneStatusMessage statusMessage) { + return null; + } + + @Override + public IDistributionClientResult sendFinalDistrStatus(IFinalDistrStatusMessage statusMessage) { + return null; + } + @Override public List decodeVfModuleArtifact(byte[] artifactPayload) { // TODO Auto-generated method stub return null; } + + @Override + public IDistributionClientResult sendComponentDoneStatus(IComponentDoneStatusMessage statusMessage, + String errorReason) { + // TODO Auto-generated method stub + return null; + } + + @Override + public IDistributionClientResult sendFinalDistrStatus(IFinalDistrStatusMessage statusMessage, String errorReason) { + // TODO Auto-generated method stub + return null; + } + + + diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/DistributionActionResultEnum.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/DistributionActionResultEnum.java index 47333d5..6ac4d35 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/DistributionActionResultEnum.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/DistributionActionResultEnum.java @@ -43,6 +43,9 @@ public enum DistributionActionResultEnum { CONF_MISSING_ENVIRONMENT_NAME, CONF_MISSING_CONSUMER_GROUP, CONF_INVALID_ASDC_FQDN, + CONF_INVALID_CONSUME_PRODUCE_STATUS_TOPIC_FALG, + CONF_MISSING_MSG_BUS_ADDRESS, + CONF_INVALID_MSG_BUS_ADDRESS, ASDC_AUTHENTICATION_FAILED, ASDC_AUTHORIZATION_FAILED, ASDC_NOT_FOUND, diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/DistributionStatusEnum.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/DistributionStatusEnum.java index 1d2d03c..13fcfcc 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/DistributionStatusEnum.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/DistributionStatusEnum.java @@ -21,21 +21,36 @@ package org.openecomp.sdc.utils; public enum DistributionStatusEnum { - /**Can be sent when ECOMP component successfully downloaded the specific artifact*/ + /**Can be sent when ONAP component successfully downloaded the specific artifact*/ DOWNLOAD_OK, - /**Can be sent when ECOMP component failed to download the specific artifact (corrupted file)*/ + /**Can be sent when ONAP component failed to download the specific artifact (corrupted file)*/ DOWNLOAD_ERROR, - /**Can be sent only if the repeated distribution notification event is sent when the ECOMP component already downloaded the artifact , but still not stored it in the local repository .*/ + /**Can be sent only if the repeated distribution notification event is sent when the ONAP component already downloaded the artifact , but still not stored it in the local repository .*/ ALREADY_DOWNLOADED, - /**Can be sent when ECOMP component successfully deployed the specific artifact in the local repository*/ + /**Can be sent when ONAP component successfully deployed the specific artifact in the local repository*/ DEPLOY_OK, - /**Can be sent when ECOMP component failed to store the downloaded artifact in the local repository*/ + /**Can be sent when ONAP component failed to store the downloaded artifact in the local repository*/ DEPLOY_ERROR, /**Sent when the repeated distribution notification event is sent for already stored in the local repository service artifact ( artifact's version and checksum match the one stored in the local repository)*/ - ALREADY_DEPLOYED + ALREADY_DEPLOYED, + /** + * ONAP component is requested to publish this status once component successfully complete downloading and storing all the data it needs from the service. + */ + COMPONENT_DONE_OK, + + /**ONAP component is requested to publish this status when component failed to download or failed to store one or more of the mandatory information it requires from the service model. + + It is recommended to populate the errorReason field with appropriate description of the error +*/ + COMPONENT_DONE_ERROR, + /** The DISTRIBUTION_COMPLETE_OK/ERROR status indicating the overall ONAP components status of retrieving and storing the information. +*/ + DISTRIBUTION_COMPLETE_OK, + + DISTRIBUTION_COMPLETE_ERROR } diff --git a/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/GeneralUtils.java b/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/GeneralUtils.java index 9d786d0..0f76574 100644 --- a/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/GeneralUtils.java +++ b/sdc-distribution-client/src/main/java/org/openecomp/sdc/utils/GeneralUtils.java @@ -20,12 +20,22 @@ package org.openecomp.sdc.utils; +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Pattern; import org.apache.commons.codec.binary.Base64; +import org.openecomp.sdc.api.results.IDistributionClientResult; +import org.openecomp.sdc.impl.DistributionClientResultImpl; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fj.data.Either; public class GeneralUtils { - + private static final Logger log = LoggerFactory.getLogger(GeneralUtils.class.getName()); public static String calculateMD5 (String data){ String calculatedMd5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data); // encode base-64 result @@ -59,4 +69,24 @@ public class GeneralUtils { return isEncoded; } + + public static Either, IDistributionClientResult> convertToValidHostName(List msgBusAddresses) { + List uebLocalHostsNames = new ArrayList<>(); + for(String name : msgBusAddresses){ + try { + uebLocalHostsNames.add(InetAddress.getByName(name).getHostName()); + } catch (UnknownHostException e) { + log.debug("UnknownHost: {}", e.getMessage(), e); + } + } + Either, IDistributionClientResult> response; + if( uebLocalHostsNames.isEmpty() ){ + response = Either.right(new DistributionClientResultImpl(DistributionActionResultEnum.CONF_INVALID_MSG_BUS_ADDRESS, "configuration is invalid: " + DistributionActionResultEnum.CONF_INVALID_MSG_BUS_ADDRESS.name())); + + } + else{ + response = Either.left(uebLocalHostsNames); + } + return response; + } } diff --git a/sdc-distribution-client/src/test/java/org/openecomp/sdc/http/SdcConnectorClientTest.java b/sdc-distribution-client/src/test/java/org/openecomp/sdc/http/SdcConnectorClientTest.java new file mode 100644 index 0000000..5ddc915 --- /dev/null +++ b/sdc-distribution-client/src/test/java/org/openecomp/sdc/http/SdcConnectorClientTest.java @@ -0,0 +1,119 @@ +package org.openecomp.sdc.http; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.openecomp.sdc.api.asdc.RegistrationRequest; +import org.openecomp.sdc.api.consumer.IConfiguration; +import org.openecomp.sdc.impl.DistributionClientResultImpl; +import org.openecomp.sdc.utils.Pair; + +import com.att.nsa.apiClient.credentials.ApiCredential; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import fj.data.Either; + +public class SdcConnectorClientTest { + private Gson gson = new GsonBuilder().create(); + private static final String MOCK_ENV = "MockEnv"; + private static final String MOCK_API_KEY = "MockApikey"; + private static HttpAsdcClient httpClient = Mockito.mock(HttpAsdcClient.class); + private static IConfiguration configuration = Mockito.mock(IConfiguration.class); + private static ApiCredential apiCredential = Mockito.mock(ApiCredential.class); + private static HttpAsdcResponse httpAsdcResponse = Mockito.mock(HttpAsdcResponse.class); + @SuppressWarnings("unchecked") + private static Either mockResponse = Mockito + .mock(Either.class); + private static Map mockHeaders = new HashMap<>(); + Pair mockPair = new Pair<>(httpAsdcResponse, null); + private HttpEntity lastHttpEntity = null; + + private static SdcConnectorClient asdcClient = Mockito.spy(new SdcConnectorClient()); + + @BeforeClass + public static void beforeClass() { + asdcClient.setConfiguration(configuration); + asdcClient.setHttpClient(httpClient); + when(apiCredential.getApiKey()).thenReturn(MOCK_API_KEY); + when(httpAsdcResponse.getStatus()).thenReturn(HttpStatus.SC_OK); + + doReturn(mockHeaders).when(asdcClient).addHeadersToHttpRequest(Mockito.anyString()); + doReturn(mockResponse).when(asdcClient).parseRegistrationResponse(httpAsdcResponse); + } + + @Before + public void beforeMethod() { + Mockito.reset(configuration, httpClient); + lastHttpEntity = null; + when(configuration.getEnvironmentName()).thenReturn(MOCK_ENV); + + + doAnswer(new Answer>() { + @Override + public Pair answer(InvocationOnMock invocation) throws Throwable { + lastHttpEntity = invocation.getArgumentAt(1, HttpEntity.class); + return mockPair; + } + }).when(httpClient).postRequest(Mockito.eq(AsdcUrls.POST_FOR_TOPIC_REGISTRATION), Mockito.any(HttpEntity.class), + Mockito.eq(mockHeaders), Mockito.eq(false)); + } + + @Test + public void testConsumeProduceStatusTopicFalse() throws UnsupportedOperationException, IOException { + + testConsumeProduceStatusTopic(false); + + } + + @Test + public void testConsumeProduceStatusTopicTrue() throws UnsupportedOperationException, IOException { + + testConsumeProduceStatusTopic(true); + + } + + private void testConsumeProduceStatusTopic(final boolean isConsumeProduceStatusFlag) throws IOException { + when(configuration.isConsumeProduceStatusTopic()).thenReturn(isConsumeProduceStatusFlag); + asdcClient.registerAsdcTopics(apiCredential); + verify(httpClient, times(1)).postRequest(Mockito.eq(AsdcUrls.POST_FOR_TOPIC_REGISTRATION), + Mockito.any(HttpEntity.class), Mockito.eq(mockHeaders), Mockito.eq(false)); + assertNotNull(lastHttpEntity); + RegistrationRequest actualRegRequest = gson.fromJson(IOUtils.toString(lastHttpEntity.getContent(), StandardCharsets.UTF_8), RegistrationRequest.class); + RegistrationRequest expectedRegRequest = gson.fromJson(excpectedStringBody(isConsumeProduceStatusFlag), RegistrationRequest.class); + + assertTrue(actualRegRequest.getApiPublicKey().equals(expectedRegRequest.getApiPublicKey())); + assertTrue(actualRegRequest.getDistrEnvName().equals(expectedRegRequest.getDistrEnvName())); + assertTrue(actualRegRequest.getIsConsumerToSdcDistrStatusTopic() + .equals(expectedRegRequest.getIsConsumerToSdcDistrStatusTopic())); + } + + + + private String excpectedStringBody(boolean isConsumeProduceStatusTopic) { + String stringBodyTemplate = "{\r\n" + " \"apiPublicKey\": \"MockApikey\",\r\n" + + " \"distrEnvName\": \"MockEnv\",\r\n" + " \"isConsumerToSdcDistrStatusTopic\": %s\r\n" + "}"; + return String.format(stringBodyTemplate, isConsumeProduceStatusTopic); + + } +} diff --git a/sdc-distribution-client/src/test/java/org/openecomp/sdc/impl/DistributionClientTest.java b/sdc-distribution-client/src/test/java/org/openecomp/sdc/impl/DistributionClientTest.java index cf380cb..81f9a17 100644 --- a/sdc-distribution-client/src/test/java/org/openecomp/sdc/impl/DistributionClientTest.java +++ b/sdc-distribution-client/src/test/java/org/openecomp/sdc/impl/DistributionClientTest.java @@ -22,16 +22,14 @@ package org.openecomp.sdc.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doReturn; import java.io.IOException; -import java.net.MalformedURLException; -import java.security.GeneralSecurityException; import java.util.ArrayList; import java.util.List; import org.junit.After; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.Test; import org.mockito.Mockito; import org.openecomp.sdc.api.IDistributionClient; @@ -39,7 +37,7 @@ import org.openecomp.sdc.api.consumer.IConfiguration; import org.openecomp.sdc.api.notification.IArtifactInfo; import org.openecomp.sdc.api.notification.IVfModuleMetadata; import org.openecomp.sdc.api.results.IDistributionClientResult; -import org.openecomp.sdc.http.AsdcConnectorClient; +import org.openecomp.sdc.http.SdcConnectorClient; import org.openecomp.sdc.http.TopicRegistrationResponse; import org.openecomp.sdc.utils.ArtifactTypeEnum; import org.openecomp.sdc.utils.ArtifactsUtils; @@ -51,28 +49,17 @@ import org.openecomp.sdc.utils.Wrapper; import com.att.nsa.apiClient.credentials.ApiCredential; import com.att.nsa.apiClient.http.HttpException; import com.att.nsa.cambria.client.CambriaClient.CambriaApiException; -import com.att.nsa.cambria.client.CambriaClientBuilders; import com.att.nsa.cambria.client.CambriaIdentityManager; -import com.att.nsa.cambria.client.CambriaTopicManager; import fj.data.Either; public class DistributionClientTest { static CambriaIdentityManager cc; - static List serverList; - DistributionClientImpl client = new DistributionClientImpl(); + DistributionClientImpl client = Mockito.spy(new DistributionClientImpl()); IConfiguration testConfiguration = new TestConfiguration(); - AsdcConnectorClient connector = Mockito.mock(AsdcConnectorClient.class); + SdcConnectorClient connector = Mockito.mock(SdcConnectorClient.class); - @BeforeClass - public static void setup() { - serverList = new ArrayList(); - serverList.add("uebsb91sfdc.it.open.com:3904"); - serverList.add("uebsb92sfdc.it.open.com:3904"); - serverList.add("uebsb93sfdc.it.open.com:3904"); - - } @After public void afterTest() { @@ -150,9 +137,6 @@ public class DistributionClientTest { @Test public void initWithMocksBadConfigurationTest() throws HttpException, CambriaApiException, IOException { - // connectorMock - Either, IDistributionClientResult> serversResult = Either.left(serverList); - Mockito.when(connector.getServerList()).thenReturn(serversResult); TopicRegistrationResponse topics = new TopicRegistrationResponse(); topics.setDistrNotificationTopicName("notificationTopic"); @@ -222,7 +206,7 @@ public class DistributionClientTest { validationResult = client.init(testEnv, new TestNotificationCallback()); Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_ENVIRONMENT_NAME, validationResult.getDistributionActionResult()); - Mockito.verify(connector, Mockito.times(0)).getServerList(); + Mockito.verify(client, Mockito.times(0)).getUEBServerList(); Mockito.verify(cambriaMock, Mockito.times(0)).createApiKey(Mockito.anyString(), Mockito.anyString()); Mockito.verify(connector, Mockito.times(0)).registerAsdcTopics(Mockito.any(ApiCredential.class)); } @@ -252,8 +236,6 @@ public class DistributionClientTest { @Test public void getConfigurationTest() throws HttpException, CambriaApiException, IOException { // connectorMock - Either, IDistributionClientResult> serversResult = Either.left(serverList); - Mockito.when(connector.getServerList()).thenReturn(serversResult); mockArtifactTypeList(); TopicRegistrationResponse topics = new TopicRegistrationResponse(); topics.setDistrNotificationTopicName("notificationTopic"); @@ -300,9 +282,6 @@ public class DistributionClientTest { @Test public void initWithMocksTest() throws HttpException, CambriaApiException, IOException { - // connectorMock - Either, IDistributionClientResult> serversResult = Either.left(serverList); - Mockito.when(connector.getServerList()).thenReturn(serversResult); mockArtifactTypeList(); TopicRegistrationResponse topics = new TopicRegistrationResponse(); @@ -323,7 +302,7 @@ public class DistributionClientTest { IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); assertEquals(DistributionActionResultEnum.SUCCESS, initResponse.getDistributionActionResult()); - Mockito.verify(connector, Mockito.times(1)).getServerList(); + Mockito.verify(client, Mockito.times(1)).getUEBServerList(); Mockito.verify(cambriaMock, Mockito.times(1)).createApiKey(Mockito.anyString(), Mockito.anyString()); Mockito.verify(connector, Mockito.times(1)).registerAsdcTopics(Mockito.any(ApiCredential.class)); System.out.println(initResponse); @@ -352,7 +331,7 @@ public class DistributionClientTest { // connectorMock IDistributionClientResult getServersResult = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, "problem"); Either, IDistributionClientResult> serversResult = Either.right(getServersResult); - Mockito.when(connector.getServerList()).thenReturn(serversResult); + doReturn(serversResult).when(client).getUEBServerList(); TopicRegistrationResponse topics = new TopicRegistrationResponse(); topics.setDistrNotificationTopicName("notificationTopic"); @@ -371,7 +350,7 @@ public class DistributionClientTest { IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); assertEquals(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, initResponse.getDistributionActionResult()); - Mockito.verify(connector, Mockito.times(1)).getServerList(); + Mockito.verify(client, Mockito.times(1)).getUEBServerList(); Mockito.verify(cambriaMock, Mockito.times(0)).createApiKey(Mockito.anyString(), Mockito.anyString()); Mockito.verify(connector, Mockito.times(0)).registerAsdcTopics(Mockito.any(ApiCredential.class)); @@ -382,8 +361,6 @@ public class DistributionClientTest { public void initCreateKeysFailedTest() throws HttpException, CambriaApiException, IOException { // connectorMock - Either, IDistributionClientResult> serversResult = Either.left(serverList); - Mockito.when(connector.getServerList()).thenReturn(serversResult); mockArtifactTypeList(); TopicRegistrationResponse topics = new TopicRegistrationResponse(); @@ -403,7 +380,7 @@ public class DistributionClientTest { IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); assertEquals(DistributionActionResultEnum.UEB_KEYS_CREATION_FAILED, initResponse.getDistributionActionResult()); - Mockito.verify(connector, Mockito.times(1)).getServerList(); + Mockito.verify(client, Mockito.times(1)).getUEBServerList(); Mockito.verify(cambriaMock, Mockito.times(1)).createApiKey(Mockito.anyString(), Mockito.anyString()); Mockito.verify(connector, Mockito.times(0)).registerAsdcTopics(Mockito.any(ApiCredential.class)); System.out.println(initResponse); @@ -413,8 +390,6 @@ public class DistributionClientTest { public void initRegistrationFailedTest() throws HttpException, CambriaApiException, IOException { // connectorMock - Either, IDistributionClientResult> serversResult = Either.left(serverList); - Mockito.when(connector.getServerList()).thenReturn(serversResult); mockArtifactTypeList(); DistributionClientResultImpl failureResult = new DistributionClientResultImpl(DistributionActionResultEnum.BAD_REQUEST, "Bad Request"); Either topicsResult = Either.right(failureResult); @@ -430,7 +405,7 @@ public class DistributionClientTest { IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); assertEquals(DistributionActionResultEnum.BAD_REQUEST, initResponse.getDistributionActionResult()); - Mockito.verify(connector, Mockito.times(1)).getServerList(); + Mockito.verify(client, Mockito.times(1)).getUEBServerList(); Mockito.verify(cambriaMock, Mockito.times(1)).createApiKey(Mockito.anyString(), Mockito.anyString()); Mockito.verify(connector, Mockito.times(1)).registerAsdcTopics(Mockito.any(ApiCredential.class)); System.out.println(initResponse); @@ -450,7 +425,7 @@ public class DistributionClientTest { } // ########### TESTS TO ADD TO CI START ########### - public void createKeysTestCI() throws MalformedURLException, GeneralSecurityException { + /*public void createKeysTestCI() throws MalformedURLException, GeneralSecurityException { validateConfigurationTest(); CambriaIdentityManager trueCambria = new CambriaClientBuilders.IdentityManagerBuilder().usingHttps().usingHosts(serverList).build(); client.cambriaIdentityManager = trueCambria; @@ -462,7 +437,7 @@ public class DistributionClientTest { System.out.println(keysResult); System.out.println("keys: public=" + client.credential.getApiKey() + " | secret=" + client.credential.getApiSecret()); } - +*/ public void initTestCI() { IDistributionClient distributionClient = DistributionClientFactory.createDistributionClient(); IDistributionClientResult init = distributionClient.init(testConfiguration, new TestNotificationCallback()); @@ -507,59 +482,9 @@ public class DistributionClientTest { } - // @Test - public void registerProducerCI() { - - try { - CambriaTopicManager topicManager = new CambriaClientBuilders.TopicManagerBuilder().usingHttps().usingHosts(serverList).authenticatedBy("sSJc5qiBnKy2qrlc", "4ZRPzNJfEUK0sSNBvccd2m7X").build(); - topicManager.allowProducer("ASDC-DISTR-STATUS-TOPIC-TESTER", "1FSVAA3bRjhSKNAI"); - } catch (HttpException | IOException | GeneralSecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // publish - // StringBuilder sb = new StringBuilder(); - // for (String s : serverList) - // { - // sb.append(s); - // sb.append(","); - // } - // CambriaBatchingPublisher pub = CambriaClientFactory.createSimplePublisher(sb.toString(), "ASDC-DISTR-STATUS-TOPIC-TESTER"); - // pub.setApiCredentials("yPMwjhmOgHUyJEeW", "3RYpgvBsjpA8Y2CHdA1PM8xK" ); - // - // - // try { - // pub.send("MyPartitionKey", "{\"artifactURL\":\"artifactURL_Val\", \"consumerID\" : \"123\", \"distributionID\" : \"AAA\", \"status\" : \"DOWNLOAD_OK\", \"timestamp\" : 1000}"); - // } catch (IOException e) { - // e.printStackTrace(); - // } - // - // finally{ - // - // - // try { - // List stuck = pub.close(15L, TimeUnit.SECONDS); - // assertTrue(stuck.isEmpty()); - // } catch (IOException | InterruptedException e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } - // } - - } - - public void connectorGetServersTestCI() { - AsdcConnectorClient connector = new AsdcConnectorClient(); - connector.init(testConfiguration); - - Either, IDistributionClientResult> serverListFromAsdc = connector.getServerList(); - assertTrue(serverListFromAsdc.isLeft()); - assertEquals(serverList, serverListFromAsdc.left().value()); - } public void connectorRegisterCI() { - AsdcConnectorClient connector = new AsdcConnectorClient(); + SdcConnectorClient connector = new SdcConnectorClient(); connector.init(testConfiguration); ApiCredential creds = new ApiCredential("publicKey", "secretKey"); @@ -569,7 +494,7 @@ public class DistributionClientTest { } public void downloadArtifactTestCI() { - AsdcConnectorClient connector = new AsdcConnectorClient(); + SdcConnectorClient connector = new SdcConnectorClient(); connector.init(testConfiguration); IArtifactInfo artifactInfo = initArtifactInfo(); connector.dowloadArtifact(artifactInfo); diff --git a/sdc-distribution-client/src/test/java/org/openecomp/sdc/utils/TestConfiguration.java b/sdc-distribution-client/src/test/java/org/openecomp/sdc/utils/TestConfiguration.java index 727f9a6..22814f4 100644 --- a/sdc-distribution-client/src/test/java/org/openecomp/sdc/utils/TestConfiguration.java +++ b/sdc-distribution-client/src/test/java/org/openecomp/sdc/utils/TestConfiguration.java @@ -24,8 +24,6 @@ import java.util.ArrayList; import java.util.List; import org.openecomp.sdc.api.consumer.IConfiguration; -import org.openecomp.sdc.utils.ArtifactTypeEnum; -import org.openecomp.sdc.utils.DistributionClientConstants; public class TestConfiguration implements IConfiguration { @@ -43,6 +41,7 @@ public class TestConfiguration implements IConfiguration { private boolean activateServerTLSAuth; private boolean isFilterInEmptyResources; private boolean useHttpsWithDmaap; + private List msgBusAddress; public TestConfiguration(IConfiguration other) { this.asdcAddress = other.getAsdcAddress(); @@ -75,6 +74,10 @@ public class TestConfiguration implements IConfiguration { this.keyStorePassword = "Aa123456"; this.activateServerTLSAuth = false; this.isFilterInEmptyResources = false; + msgBusAddress = new ArrayList(); + msgBusAddress.add("www.cnn.com"); + msgBusAddress.add("www.cnn.com"); + msgBusAddress.add("www.cnn.com"); } @Override @@ -82,6 +85,11 @@ public class TestConfiguration implements IConfiguration { return asdcAddress; } + @Override + public List getMsgBusAddress() { + return msgBusAddress; + } + @Override public String getUser() { return user; -- cgit 1.2.3-korg