--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2018 Ericsson. 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.distribution.reception.handling.sdc;
+
+import java.util.Timer;
+import java.util.TimerTask;
+
+/**
+ * This class implements TimerTask for calling life cycle methods of SdcClient iteratively after specified interval
+ * until the operation is successful.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
+ */
+public class SdcClientHandler extends TimerTask {
+
+ private SdcReceptionHandler sdcReceptionHandler;
+ private SdcClientOperationType operationType;
+ private Timer timer;
+
+ public enum SdcClientOperationType {
+ START, STOP
+ }
+
+ /**
+ * Constructs an instance of {@link SdcClientHandler} class.
+ *
+ * @param sdcReceptionHandler the sdcReceptionHandler
+ */
+ public SdcClientHandler(final SdcReceptionHandler sdcReceptionHandler, final SdcClientOperationType operationType,
+ final long retryDelay) {
+ this.sdcReceptionHandler = sdcReceptionHandler;
+ this.operationType = operationType;
+ timer = new Timer(false);
+ timer.scheduleAtFixedRate(this, 0, retryDelay * 1000L);
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public void run() {
+ if (SdcClientOperationType.START.equals(operationType)) {
+ sdcReceptionHandler.initializeSdcClient();
+ sdcReceptionHandler.startSdcClient();
+ } else {
+ sdcReceptionHandler.stopSdcClient();
+ }
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public boolean cancel() {
+ timer.cancel();
+ return true;
+ }
+}
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.common.parameters.ParameterService;
import org.onap.policy.distribution.model.Csar;
-import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
-import org.onap.policy.distribution.reception.decoding.PluginTerminationException;
import org.onap.policy.distribution.reception.decoding.PolicyDecodingException;
import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler;
+import org.onap.policy.distribution.reception.handling.sdc.SdcClientHandler.SdcClientOperationType;
import org.onap.policy.distribution.reception.handling.sdc.exceptions.ArtifactDownloadException;
import org.onap.policy.distribution.reception.statistics.DistributionStatisticsManager;
import org.onap.sdc.api.IDistributionClient;
public class SdcReceptionHandler extends AbstractReceptionHandler implements INotificationCallback {
private static final Logger LOGGER = FlexLogger.getLogger(SdcReceptionHandler.class);
+ private static final String SECONDS = "Seconds";
private SdcReceptionHandlerStatus sdcReceptionHandlerStatus = SdcReceptionHandlerStatus.STOPPED;
- private SdcReceptionHandlerConfigurationParameterGroup handlerParameters;
private IDistributionClient distributionClient;
private SdcConfiguration sdcConfig;
private volatile int nbOfNotificationsOngoing = 0;
+ private int retryDelay;
+ private SdcClientHandler sdcClientHandler;
private enum DistributionStatusType {
DOWNLOAD, DEPLOY
}
@Override
- protected void initializeReception(final String parameterGroupName) throws PluginInitializationException {
- handlerParameters = ParameterService.get(parameterGroupName);
- initializeSdcClient();
- startSdcClient();
+ protected void initializeReception(final String parameterGroupName) {
+ final SdcReceptionHandlerConfigurationParameterGroup handlerParameters =
+ ParameterService.get(parameterGroupName);
+ retryDelay = handlerParameters.getRetryDelay() < 30 ? 30 : handlerParameters.getRetryDelay();
+ sdcConfig = new SdcConfiguration(handlerParameters);
+ distributionClient = createSdcDistributionClient();
+ sdcClientHandler = new SdcClientHandler(this, SdcClientOperationType.START, retryDelay);
}
@Override
- public void destroy() throws PluginTerminationException {
- LOGGER.debug("Going to stop the SDC Client...");
+ public void destroy() {
if (distributionClient != null) {
- final IDistributionClientResult clientResult = distributionClient.stop();
- if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
- final String message =
- "SDC client stop failed with reason:" + clientResult.getDistributionMessageResult();
- LOGGER.error(message);
- throw new PluginTerminationException(message);
- }
+ sdcClientHandler = new SdcClientHandler(this, SdcClientOperationType.STOP, retryDelay);
}
- changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
- LOGGER.debug("SDC Client is stopped successfully");
}
@Override
++nbOfNotificationsOngoing;
sdcReceptionHandlerStatus = newStatus;
break;
+ default:
+ break;
}
}
/**
* Method to initialize the SDC client.
*
- * @throws PluginInitializationException if the initialization of SDC Client fails
*/
- private void initializeSdcClient() throws PluginInitializationException {
+ protected void initializeSdcClient() {
LOGGER.debug("Initializing the SDC Client...");
if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.STOPPED) {
- final String message = "The SDC Client is already initialized";
- LOGGER.error(message);
- throw new PluginInitializationException(message);
+ LOGGER.error("The SDC Client is already initialized");
+ return;
}
- sdcConfig = new SdcConfiguration(handlerParameters);
- distributionClient = createSdcDistributionClient();
final IDistributionClientResult clientResult = distributionClient.init(sdcConfig, this);
if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
- changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
- final String message =
- "SDC client initialization failed with reason:" + clientResult.getDistributionMessageResult();
- LOGGER.error(message);
- throw new PluginInitializationException(message);
+ LOGGER.error("SDC client initialization failed with reason:" + clientResult.getDistributionMessageResult()
+ + ". Initialization will be retried after " + retryDelay + SECONDS);
+ return;
}
LOGGER.debug("SDC Client is initialized successfully");
- this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
+ changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.INIT);
}
/**
* Method to start the SDC client.
*
- * @param configParameter the configuration parameters
- * @throws PluginInitializationException if the start of SDC Client fails
*/
- private void startSdcClient() throws PluginInitializationException {
+ protected void startSdcClient() {
LOGGER.debug("Going to start the SDC Client...");
+ if (sdcReceptionHandlerStatus != SdcReceptionHandlerStatus.INIT) {
+ LOGGER.error("The SDC Client is not initialized");
+ return;
+ }
final IDistributionClientResult clientResult = distributionClient.start();
if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
- changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
- final String message = "SDC client start failed with reason:" + clientResult.getDistributionMessageResult();
- LOGGER.error(message);
- throw new PluginInitializationException(message);
+ LOGGER.error("SDC client start failed with reason:" + clientResult.getDistributionMessageResult()
+ + ". Start will be retried after " + retryDelay + SECONDS);
+ return;
}
LOGGER.debug("SDC Client is started successfully");
- this.changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
+ changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.IDLE);
+ sdcClientHandler.cancel();
+ }
+
+ /**
+ * Method to stop the SDC client.
+ *
+ */
+ protected void stopSdcClient() {
+ LOGGER.debug("Going to stop the SDC Client...");
+ final IDistributionClientResult clientResult = distributionClient.stop();
+ if (!clientResult.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
+ LOGGER.error("SDC client stop failed with reason:" + clientResult.getDistributionMessageResult()
+ + ". Stop will be retried after " + retryDelay + SECONDS);
+ return;
+ }
+ LOGGER.debug("SDC Client is stopped successfully");
+ changeSdcReceptionHandlerStatus(SdcReceptionHandlerStatus.STOPPED);
+ sdcClientHandler.cancel();
}
/**
private String keystorePassword;
private List<String> messageBusAddress;
private List<String> artifactTypes;
+ private int retryDelay;
/**
* Set activeserverTlsAuth to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance.
return this;
}
+ /**
+ * Set retryDelay to this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance.
+ *
+ * @param retryDelay the retryDelay
+ */
+ public SdcReceptionHandlerConfigurationParameterBuilder setRetryDelay(final int retryDelay) {
+ this.retryDelay = retryDelay;
+ return this;
+ }
+
/**
* Returns the active server TlsAuth of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance.
*
return artifactTypes;
}
+ /**
+ * Returns the retryDelay of this {@link SdcReceptionHandlerConfigurationParameterBuilder} instance.
+ *
+ * @return the retryDelay
+ */
+ public int getRetryDelay() {
+ return retryDelay;
+ }
+
}
package org.onap.policy.distribution.reception.handling.sdc;
import java.util.List;
+
import org.onap.policy.common.parameters.GroupValidationResult;
import org.onap.policy.common.parameters.ValidationStatus;
import org.onap.policy.common.utils.validation.ParameterValidationUtils;
import org.onap.policy.distribution.reception.parameters.ReceptionHandlerConfigurationParameterGroup;
/**
- * This class handles reading, parsing and validating of the Policy SDC Service Distribution
- * parameters from Json format, which strictly adheres to the interface:IConfiguration, defined by
- * SDC SDK.
+ * This class handles reading, parsing and validating of the Policy SDC Service Distribution parameters from Json
+ * format, which strictly adheres to the interface:IConfiguration, defined by SDC SDK.
*/
public class SdcReceptionHandlerConfigurationParameterGroup extends ReceptionHandlerConfigurationParameterGroup {
private String password;
private int pollingInterval;
private int pollingTimeout;
+ private int retryDelay;
private String consumerId;
private List<String> artifactTypes;
private String consumerGroup;
private boolean isUseHttpsWithDmaap;
/**
- * The constructor for instantiating {@link SdcReceptionHandlerConfigurationParameterGroup}
- * class.
+ * The constructor for instantiating {@link SdcReceptionHandlerConfigurationParameterGroup} class.
*
* @param builder the SDC configuration builder
*/
password = builder.getPassword();
pollingInterval = builder.getPollingInterval();
pollingTimeout = builder.getPollingTimeout();
+ retryDelay = builder.getRetryDelay();
consumerId = builder.getConsumerId();
artifactTypes = builder.getArtifactTypes();
consumerGroup = builder.getConsumerGroup();
return pollingTimeout;
}
+ public int getRetryDelay() {
+ return retryDelay;
+ }
+
public String getConsumerId() {
return consumerId;
}
validateStringElement(validationResult, keyStorePassword, "keyStorePassword");
validateIntElement(validationResult, pollingInterval, "pollingInterval");
validateIntElement(validationResult, pollingTimeout, "pollingTimeout");
+ validateIntElement(validationResult, retryDelay, "retryDelay");
validateStringListElement(validationResult, messageBusAddress, "messageBusAddress");
validateStringListElement(validationResult, artifactTypes, "artifactTypes");
return validationResult;
import org.onap.policy.distribution.forwarding.parameters.PolicyForwarderParameters;
import org.onap.policy.distribution.model.Csar;
import org.onap.policy.distribution.reception.decoding.PluginInitializationException;
-import org.onap.policy.distribution.reception.decoding.PluginTerminationException;
import org.onap.policy.distribution.reception.decoding.PolicyDecoder;
import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler;
import org.onap.policy.distribution.reception.handling.PluginHandler;
* Setup for the test cases.
*
* @throws IOException if it occurs
+ * @throws SecurityException if it occurs
+ * @throws NoSuchFieldException if it occurs
+ * @throws IllegalAccessException if it occurs
+ * @throws IllegalArgumentException if it occurs
*/
@Before
- public final void init() throws IOException {
+ public final void init() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException,
+ IllegalAccessException {
DistributionStatisticsManager.resetAllStatistics();
final Gson gson = new GsonBuilder().create();
pssdConfigParameters = gson.fromJson(new FileReader("src/test/resources/handling-sdc.json"),
public final void testInitializeSdcClient() {
try {
sypHandler.initializeReception(pssdConfigParameters.getName());
- } catch (final PluginInitializationException exp) {
+ } catch (final Exception exp) {
LOGGER.error(exp);
fail("Test should not throw any exception");
}
}
@Test
- public final void testInitializeSdcClient_Again() throws PluginInitializationException {
- sypHandler.initializeReception(pssdConfigParameters.getName());
- try {
- sypHandler.initializeReception(pssdConfigParameters.getName());
- fail("Test must throw an exception here");
- } catch (final Exception exp) {
- assertTrue(exp.getMessage().startsWith("The SDC Client is already initialized"));
- }
- }
-
- @Test
- public final void testInitializeSdcClient_Failure() throws PluginInitializationException {
+ public final void testInitializeSdcClient_Failure() {
Mockito.when(successfulClientInitResult.getDistributionActionResult())
- .thenReturn(DistributionActionResultEnum.FAIL);
+ .thenReturn(DistributionActionResultEnum.FAIL).thenReturn(DistributionActionResultEnum.SUCCESS);
try {
sypHandler.initializeReception(pssdConfigParameters.getName());
- fail("Test must throw an exception here");
} catch (final Exception exp) {
- assertTrue(exp.getMessage().startsWith("SDC client initialization failed with reason"));
+ LOGGER.error(exp);
+ fail("Test should not throw any exception");
}
}
@Test
- public final void testStartSdcClient_Failure() throws PluginInitializationException {
+ public final void testStartSdcClient_Failure() {
try {
- Mockito.when(distributionClient.start()).thenReturn(failureClientInitResult);
+ Mockito.when(distributionClient.start()).thenReturn(failureClientInitResult)
+ .thenReturn(successfulClientInitResult);
sypHandler.initializeReception(pssdConfigParameters.getName());
-
- fail("Test must throw an exception here");
} catch (final Exception exp) {
- assertTrue(exp.getMessage().startsWith("SDC client start failed with reason"));
+ LOGGER.error(exp);
+ fail("Test should not throw any exception");
}
}
try {
sypHandler.initializeReception(pssdConfigParameters.getName());
sypHandler.destroy();
- } catch (final PluginInitializationException | PluginTerminationException exp) {
+ } catch (final Exception exp) {
LOGGER.error(exp);
fail("Test should not throw any exception");
}
}
@Test
- public final void testStopSdcClientWithoutStart() {
+ public final void testStopSdcClient_Failure() throws PluginInitializationException {
+
+ sypHandler.initializeReception(pssdConfigParameters.getName());
+ Mockito.when(distributionClient.stop()).thenReturn(failureClientInitResult)
+ .thenReturn(successfulClientInitResult);
try {
sypHandler.destroy();
- } catch (final PluginTerminationException exp) {
+ } catch (final Exception exp) {
LOGGER.error(exp);
fail("Test should not throw any exception");
}
-
}
@Test
- public final void testStopSdcClient_Failure() throws PluginInitializationException {
-
- sypHandler.initializeReception(pssdConfigParameters.getName());
- Mockito.when(successfulClientInitResult.getDistributionActionResult())
- .thenReturn(DistributionActionResultEnum.FAIL);
+ public final void testStopSdcClientWithoutStart() {
try {
sypHandler.destroy();
- fail("Test must throw an exception here");
} catch (final Exception exp) {
- assertTrue(exp.getMessage().startsWith("SDC client stop failed with reason"));
+ LOGGER.error(exp);
+ fail("Test should not throw any exception");
}
+
}
@Test
new SdcReceptionHandlerConfigurationParameterBuilder().setAsdcAddress("localhost")
.setConsumerGroup("policy-group").setConsumerId("policy-id").setEnvironmentName("TEST")
.setKeystorePassword("password").setKeystorePath("dummyPath").setPassword("policy")
- .setPollingInterval(10).setPollingTimeout(20).setUser("policy").setUseHttpsWithDmaap(false)
- .setActiveserverTlsAuth(false).setFilterinEmptyResources(true)
+ .setPollingInterval(10).setPollingTimeout(20).setRetryDelay(30).setUser("policy")
+ .setUseHttpsWithDmaap(false).setActiveserverTlsAuth(false).setFilterinEmptyResources(true)
.setArtifactTypes(Arrays.asList("TOSCA_CSAR")).setMessageBusAddress(Arrays.asList("localhost"));
final SdcReceptionHandlerConfigurationParameterGroup configParameters =
new SdcReceptionHandlerConfigurationParameterGroup(builder);
assertEquals("policy", configParameters.getPassword());
assertEquals(10, configParameters.getPollingInterval());
assertEquals(20, configParameters.getPollingTimeout());
+ assertEquals(30, configParameters.getRetryDelay());
assertEquals("policy-id", configParameters.getConsumerId());
assertEquals("policy-group", configParameters.getConsumerGroup());
assertEquals("TEST", configParameters.getEnvironmentName());
"password": "policy",
"pollingInterval":20,
"pollingTimeout":30,
+ "retryDelay":30,
"consumerId": "policy-id",
"artifactTypes": [
"TOSCA_CSAR",