diff options
author | a.sreekumar <ajith.sreekumar@est.tech> | 2019-04-02 09:09:51 +0000 |
---|---|---|
committer | a.sreekumar <ajith.sreekumar@est.tech> | 2019-04-02 09:09:51 +0000 |
commit | 50b84774797677791fb2111cf97b9f2ff65cb3e2 (patch) | |
tree | 2162cff82f7cdfa1305786a6ed5e9916cdbf7006 /services/services-onappf | |
parent | ab4cba7d37f41eddbf3ab040f04afe18c6630777 (diff) |
Changes to send pdp status messages
1) Adding handlers to create and send pdp status messages from apex-pdp.
2) Changes to read initial pdp status parameters from configuration
file.
3) Adding test cases.
Change-Id: Id2d1caa8abc124865a9ef8fbfe4d9f751e4491b5
Issue-ID: POLICY-1452
Signed-off-by: a.sreekumar <ajith.sreekumar@est.tech>
Diffstat (limited to 'services/services-onappf')
18 files changed, 642 insertions, 54 deletions
diff --git a/services/services-onappf/pom.xml b/services/services-onappf/pom.xml index dd7afdc10..af3dab0ab 100644 --- a/services/services-onappf/pom.xml +++ b/services/services-onappf/pom.xml @@ -45,11 +45,6 @@ </dependency> <dependency> <groupId>org.onap.policy.common</groupId> - <artifactId>pdp-common</artifactId> - <version>${version.policy.common}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.common</groupId> <artifactId>policy-endpoints</artifactId> <version>${version.policy.common}</version> </dependency> @@ -59,9 +54,19 @@ <version>${version.policy.common}</version> </dependency> <dependency> + <groupId>org.onap.policy.models</groupId> + <artifactId>policy-models-pdp</artifactId> + <version>${version.policy.models}</version> + </dependency> + <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.powermock</groupId> + <artifactId>powermock-api-mockito</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java index e1fb88a0a..5058e7cd4 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java @@ -20,15 +20,22 @@ package org.onap.policy.apex.starter; +import java.util.List; import java.util.Properties; import lombok.Getter; import lombok.Setter; import org.onap.policy.apex.starter.exception.ApexStarterException; +import org.onap.policy.apex.starter.handler.CommunicationHandler; +import org.onap.policy.apex.starter.handler.PdpMessageHandler; import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.starter.parameters.PdpStatusParameters; import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.event.comm.TopicSource; import org.onap.policy.common.parameters.ParameterService; +import org.onap.policy.common.utils.services.Registry; import org.onap.policy.common.utils.services.ServiceManager; import org.onap.policy.common.utils.services.ServiceManagerException; import org.slf4j.Logger; @@ -43,12 +50,7 @@ public class ApexStarterActivator { private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterActivator.class); private final ApexStarterParameterGroup apexStarterParameterGroup; - - /** - * The current activator. - */ - @Getter - private static volatile ApexStarterActivator current = null; + private CommunicationHandler communicationHandler; /** * Used to manage the services. @@ -62,21 +64,35 @@ public class ApexStarterActivator { public ApexStarterActivator(final ApexStarterParameterGroup apexStarterParameterGroup, final Properties topicProperties) { - TopicEndpoint.manager.addTopicSinks(topicProperties); - TopicEndpoint.manager.addTopicSources(topicProperties); - + final List<TopicSink> topicSinks = TopicEndpoint.manager.addTopicSinks(topicProperties); + final List<TopicSource> topicSources = TopicEndpoint.manager.addTopicSources(topicProperties); this.apexStarterParameterGroup = apexStarterParameterGroup; + + // TODO: instanceId currently set as a random string, could be fetched from actual deployment + final int random = (int) (Math.random() * 100); + final String instanceId = "apex_" + random; + // @formatter:off this.manager = new ServiceManager() .addAction("topics", - () -> TopicEndpoint.manager.start(), () -> TopicEndpoint.manager.shutdown()) + () -> TopicEndpoint.manager.start(), + () -> TopicEndpoint.manager.shutdown()) .addAction("set alive", - () -> setAlive(true), () -> setAlive(false)) + () -> setAlive(true), + () -> setAlive(false)) + .addAction("register context map", + () -> Registry.register(ApexStarterConstants.REG_PDP_STATUS_OBJECT, + new PdpMessageHandler().createPdpStatusFromParameters(instanceId, + apexStarterParameterGroup.getPdpStatusParameters())), + () -> Registry.unregister(ApexStarterConstants.REG_PDP_STATUS_OBJECT)) .addAction("register parameters", () -> registerToParameterService(apexStarterParameterGroup), - () -> deregisterToParameterService(apexStarterParameterGroup)); + () -> deregisterToParameterService(apexStarterParameterGroup)) + .addAction("Communication handler", + () -> startCommunicationHandler(topicSinks, topicSources, + apexStarterParameterGroup.getPdpStatusParameters()), + () -> communicationHandler.stop()); // @formatter:on - current = this; } /** @@ -144,4 +160,21 @@ public class ApexStarterActivator { public void deregisterToParameterService(final ApexStarterParameterGroup apexStarterParameterGroup) { ParameterService.deregister(apexStarterParameterGroup.getName()); } + + /** + * Starts the communication handler which handles the communication between apex pdp and pap. + * + * @param pdpStatusParameters + * @param topicSources + * @param topicSinks + * + * @throws ApexStarterException if the handler start fails + */ + public void startCommunicationHandler(final List<TopicSink> topicSinks, final List<TopicSource> topicSources, + final PdpStatusParameters pdpStatusParameters) throws ApexStarterException { + communicationHandler = new CommunicationHandler(topicSinks, topicSources, pdpStatusParameters); + if (!communicationHandler.start()) { + throw new ApexStarterException("Failed to start the communication handler for ApexStarter"); + } + } } diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterConstants.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterConstants.java new file mode 100644 index 000000000..c2d3dce05 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterConstants.java @@ -0,0 +1,30 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.apex.starter; + +/** + * Names of various items contained in the Registry. + */ +public class ApexStarterConstants { + // Registry keys + public static final String REG_APEX_STARTER_ACTIVATOR = "object:activator/apex_starter"; + public static final String REG_PDP_STATUS_OBJECT = "object:pdp/status"; +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterMain.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterMain.java index f576bdfd4..7f11de713 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterMain.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterMain.java @@ -27,6 +27,7 @@ import java.util.Properties; import org.onap.policy.apex.starter.exception.ApexStarterException; import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; +import org.onap.policy.common.utils.services.Registry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -88,12 +89,13 @@ public class ApexStarterMain { // create the activator activator = new ApexStarterActivator(parameterGroup, topicProperties); - + Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); // Start the activator try { activator.initialize(); } catch (final ApexStarterException e) { LOGGER.error("start of ApexStarter failed, used parameters are {}", Arrays.toString(args), e); + Registry.unregister(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR); return; } diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/CommunicationHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/CommunicationHandler.java new file mode 100644 index 000000000..197b34ef5 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/CommunicationHandler.java @@ -0,0 +1,94 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.apex.starter.handler; + +import java.util.List; + +import org.onap.policy.apex.starter.parameters.PdpStatusParameters; +import org.onap.policy.common.capabilities.Startable; +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.event.comm.TopicSource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class manages the communication between apex pdp and pap. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class CommunicationHandler implements Startable { + + private static final Logger LOGGER = LoggerFactory.getLogger(CommunicationHandler.class); + + private List<TopicSink> topicSinks; // topics to which apex-pdp sends pdp status + private List<TopicSource> topicSources; // topics to which apex-pdp listens to for messages from pap. + + private PdpStatusPublisher pdpStatusPublisher; + private PdpStatusParameters pdpStatusParameters; + + /** + * Constructor for instantiating CommunicationHandler + * + * @param topicSinks topics to which apex-pdp sends pdp status + * @param topicSources topics to which apex-pdp listens to for messages from pap. + * @param pdpStatusParameters pdp status parameters read from the configuration file + */ + public CommunicationHandler(final List<TopicSink> topicSinks, final List<TopicSource> topicSources, + final PdpStatusParameters pdpStatusParameters) { + this.topicSinks = topicSinks; + this.topicSources = topicSources; + this.pdpStatusParameters = pdpStatusParameters; + } + + @Override + public boolean start() { + try { + pdpStatusPublisher = new PdpStatusPublisher(topicSinks, pdpStatusParameters.getTimeInterval()); + } catch (final Exception e) { + LOGGER.error("Failed to start communication handler for apex-pdp", e); + return false; + } + return true; + } + + @Override + public boolean stop() { + try { + pdpStatusPublisher.terminate(); + } catch (final Exception e) { + LOGGER.error("Failed to stop communication handler for apex-pdp", e); + return false; + } + return true; + } + + @Override + public void shutdown() { + stop(); + } + + @Override + public boolean isAlive() { + return pdpStatusPublisher.isAlive(); + } + +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpMessageHandler.java new file mode 100644 index 000000000..a0f4f5863 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpMessageHandler.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.apex.starter.handler; + +import java.util.ArrayList; +import java.util.List; + +import org.onap.policy.apex.starter.ApexStarterConstants; +import org.onap.policy.apex.starter.parameters.PdpStatusParameters; +import org.onap.policy.apex.starter.parameters.PolicyTypeIdentParameters; +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.concepts.PolicyTypeIdent; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpState; + +/** + * This class supports the handling of pdp messages. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class PdpMessageHandler { + + /** + * Method to create PdpStatus message from the parameters which will be saved to the context + * + * @param instanceId instance id of apex pdp + * @param pdpStatusParameters pdp status parameters read from the configuration file + * + * @return PdpStatus the pdp status message + */ + public PdpStatus createPdpStatusFromParameters(final String instanceId, + final PdpStatusParameters pdpStatusParameters) { + final PdpStatus pdpStatus = new PdpStatus(); + pdpStatus.setName(pdpStatusParameters.getPdpName()); + pdpStatus.setVersion(pdpStatusParameters.getVersion()); + pdpStatus.setPdpType(pdpStatusParameters.getPdpType()); + pdpStatus.setState(PdpState.PASSIVE); + pdpStatus.setHealthy(PdpHealthStatus.HEALTHY); + pdpStatus.setDescription(pdpStatusParameters.getDescription()); + pdpStatus.setInstance(instanceId); + final List<PolicyTypeIdent> supportedPolicyTypes = new ArrayList<PolicyTypeIdent>(); + for (final PolicyTypeIdentParameters policyTypeIdentParameters : pdpStatusParameters + .getSupportedPolicyTypes()) { + supportedPolicyTypes.add( + new PolicyTypeIdent(policyTypeIdentParameters.getName(), policyTypeIdentParameters.getVersion())); + } + pdpStatus.setSupportedPolicyTypes(supportedPolicyTypes); + return pdpStatus; + } + + /** + * Method to create PdpStatus message from the context, which is to be sent by apex-pdp to pap + * + * @return PdpStatus the pdp status message + */ + public PdpStatus createPdpStatusFromContext() { + final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class); + final PdpStatus pdpStatus = new PdpStatus(); + pdpStatus.setName(pdpStatusContext.getName()); + pdpStatus.setVersion(pdpStatusContext.getVersion()); + pdpStatus.setPdpType(pdpStatusContext.getPdpType()); + pdpStatus.setState(pdpStatusContext.getState()); + pdpStatus.setHealthy(pdpStatusContext.getHealthy()); + pdpStatus.setDescription(pdpStatusContext.getDescription()); + pdpStatus.setInstance(pdpStatusContext.getInstance()); + pdpStatus.setSupportedPolicyTypes(pdpStatusContext.getSupportedPolicyTypes()); + return pdpStatus; + } +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpStatusPublisher.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpStatusPublisher.java new file mode 100644 index 000000000..2853ae798 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpStatusPublisher.java @@ -0,0 +1,91 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.apex.starter.handler; + +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +import lombok.Getter; +import lombok.Setter; + +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is used to send pdp status messages to pap using TopicSinkClient. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class PdpStatusPublisher extends TimerTask { + + private static final Logger LOGGER = LoggerFactory.getLogger(CommunicationHandler.class); + + private List<TopicSink> topicSinks; + private TopicSinkClient topicSinkClient; + private Timer timer; + private PdpMessageHandler pdpMessageHandler; + + @Getter + @Setter(lombok.AccessLevel.PRIVATE) + private volatile boolean alive = false; + + /** + * Constructor for instantiating PdpStatusPublisher + * + * @param pdpStatus + * @param topicSinks + * @param apexStarterParameterGroup + */ + public PdpStatusPublisher(final List<TopicSink> topicSinks, final int interval) { + this.topicSinks = topicSinks; + this.topicSinkClient = new TopicSinkClient(topicSinks.get(0)); + this.pdpMessageHandler = new PdpMessageHandler(); + timer = new Timer(false); + timer.scheduleAtFixedRate(this, 0, interval * 1000L); + setAlive(true); + } + + @Override + public void run() { + topicSinkClient.send(pdpMessageHandler.createPdpStatusFromContext()); + LOGGER.info("Sent heartbeat to PAP"); + } + + public void terminate() { + timer.cancel(); + timer.purge(); + setAlive(false); + } + + public PdpStatusPublisher updateInterval(final int interval) { + terminate(); + return new PdpStatusPublisher(topicSinks, interval); + } + + public void send() { + topicSinkClient.send(pdpMessageHandler.createPdpStatusFromContext()); + LOGGER.info("Sent pdp status response message to PAP"); + } + +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/ApexStarterParameterGroup.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/ApexStarterParameterGroup.java index 25001330c..68299480e 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/ApexStarterParameterGroup.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/ApexStarterParameterGroup.java @@ -23,7 +23,6 @@ package org.onap.policy.apex.starter.parameters; import lombok.Getter; import org.onap.policy.common.parameters.ParameterGroupImpl; -import org.onap.policy.common.parameters.annotations.Min; import org.onap.policy.common.parameters.annotations.NotBlank; import org.onap.policy.common.parameters.annotations.NotNull; @@ -36,8 +35,8 @@ import org.onap.policy.common.parameters.annotations.NotNull; @NotBlank @Getter public class ApexStarterParameterGroup extends ParameterGroupImpl { - @Min(value = 1) - private int timeInterval; + + private PdpStatusParameters pdpStatusParameters; /** * Create the apex starter parameter group. diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/PdpStatusParameters.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/PdpStatusParameters.java new file mode 100644 index 000000000..64545492d --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/PdpStatusParameters.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.apex.starter.parameters; + +import java.util.List; + +import lombok.Getter; + +import org.onap.policy.common.parameters.ParameterGroupImpl; +import org.onap.policy.common.parameters.annotations.Min; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * Class to hold all parameters needed for pdpstatus. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +@NotNull +@NotBlank +@Getter +public class PdpStatusParameters extends ParameterGroupImpl { + + @Min(value = 1) + private int timeInterval; + + private String pdpName; + private String version; + private String pdpType; + private String description; + private List<PolicyTypeIdentParameters> supportedPolicyTypes; + + public PdpStatusParameters() { + super(PdpStatusParameters.class.getSimpleName()); + } +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/PolicyTypeIdentParameters.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/PolicyTypeIdentParameters.java new file mode 100644 index 000000000..b6760a023 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/PolicyTypeIdentParameters.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.apex.starter.parameters; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; + +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * Class to hold all parameters needed for PolicyTypeIdent. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +@NotNull +@NotBlank +@Getter +@Setter +@EqualsAndHashCode +public class PolicyTypeIdentParameters { + private String name; + private String version; +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java index 3b7df8321..6da3a5eb6 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java @@ -23,7 +23,8 @@ package org.onap.policy.apex.starter; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.io.FileInputStream; @@ -36,8 +37,8 @@ import org.onap.policy.apex.starter.exception.ApexStarterException; import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; import org.onap.policy.apex.starter.parameters.CommonTestData; - - +import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpStatus; /** * Class to perform unit test of {@link ApexStarterActivator}}. @@ -55,6 +56,7 @@ public class TestApexStarterActivator { */ @Before public void setUp() throws Exception { + Registry.newRegistry(); final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json", "-p", "src/test/resources/topic.properties" }; final ApexStarterCommandLineArguments arguments = @@ -90,6 +92,9 @@ public class TestApexStarterActivator { assertTrue(activator.getParameterGroup().isValid()); assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, activator.getParameterGroup().getName()); + // ensure items were added to the registry + assertNotNull(Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class)); + // repeat - should throw an exception assertThatIllegalStateException().isThrownBy(() -> activator.initialize()); assertTrue(activator.isAlive()); @@ -97,16 +102,14 @@ public class TestApexStarterActivator { } @Test - public void testGetCurrent_testSetCurrent() { - assertSame(activator, ApexStarterActivator.getCurrent()); - } - - @Test public void testTerminate() throws Exception { activator.initialize(); activator.terminate(); assertFalse(activator.isAlive()); + // ensure items have been removed from the registry + assertNull(Registry.getOrDefault(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class, null)); + // repeat - should throw an exception assertThatIllegalStateException().isThrownBy(() -> activator.terminate()); assertFalse(activator.isAlive()); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java new file mode 100644 index 000000000..08e56cdf8 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.apex.starter; + +import org.junit.Test; +import org.powermock.reflect.Whitebox; + +/** + * Class to perform unit test of {@link ApexStarterConstants}}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestApexStarterConstants { + @Test + public void test() throws Exception { + // verify that constructor does not throw an exception + Whitebox.invokeConstructor(ApexStarterConstants.class); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java index 5d52d85c5..3005e320a 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java @@ -21,12 +21,15 @@ package org.onap.policy.apex.starter; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.onap.policy.apex.starter.exception.ApexStarterException; import org.onap.policy.apex.starter.parameters.CommonTestData; +import org.onap.policy.common.utils.services.Registry; /** * Class to perform unit test of {@link ApexStarterMain}}. @@ -37,6 +40,14 @@ public class TestApexStarterMain { private ApexStarterMain apexStarter; /** + * Set up. + */ + @Before + public void setUp() { + Registry.newRegistry(); + } + + /** * Shuts "main" down. * * @throws Exception if an error occurs @@ -44,7 +55,8 @@ public class TestApexStarterMain { @After public void tearDown() throws Exception { // shut down activator - final ApexStarterActivator activator = ApexStarterActivator.getCurrent(); + final ApexStarterActivator activator = Registry.getOrDefault(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, + ApexStarterActivator.class, null); if (activator != null && activator.isAlive()) { activator.terminate(); } @@ -58,6 +70,9 @@ public class TestApexStarterMain { assertTrue(apexStarter.getParameters().isValid()); assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName()); + // ensure items were added to the registry + assertNotNull(Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class)); + apexStarter.shutdown(); } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java index 08a33b94c..03f6ec2c5 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java @@ -20,6 +20,8 @@ package org.onap.policy.apex.starter.parameters; +import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -36,9 +38,29 @@ import org.onap.policy.common.utils.coder.StandardCoder; public class CommonTestData { public static final String APEX_STARTER_GROUP_NAME = "ApexStarterParameterGroup"; - public static final int APEX_STARTER_TIME_INTERVAL = 5; + public static final int TIME_INTERVAL = 2; + public static final String PDP_NAME = "apex-pdp"; + public static final String VERSION = "0.0.1"; + public static final String PDP_TYPE = "apex"; + public static final String DESCRIPTION = "Pdp status for HealthCheck"; + public static final String POLICY_NAME = "onap.controllloop.operational.apex.BBS"; + public static final String POLICY_VERSION = "0.0.1"; + public static final List<PolicyTypeIdentParameters> SUPPORTED_POLICY_TYPES = + Arrays.asList(getSupportedPolicyTypes(POLICY_NAME, POLICY_VERSION)); + + public static final Coder coder = new StandardCoder(); - private static final Coder coder = new StandardCoder(); + /** + * Returns supported policy types for test cases. + * + * @return supported policy types + */ + public static PolicyTypeIdentParameters getSupportedPolicyTypes(final String name, final String version) { + final PolicyTypeIdentParameters policyTypeIdentParameters = new PolicyTypeIdentParameters(); + policyTypeIdentParameters.setName(name); + policyTypeIdentParameters.setVersion(version); + return policyTypeIdentParameters; + } /** * Converts the contents of a map to a parameter class. @@ -67,26 +89,28 @@ public class CommonTestData { final Map<String, Object> map = new TreeMap<>(); map.put("name", name); - map.put("timeInterval", getTimeInterval(false)); + map.put("pdpStatusParameters", getPdpStatusParametersMap(false)); return map; } - - /** - * Determines whether to return null or a valid time interval + * Returns a property map for a PdpStatusParameters map for test cases. * - * @param isNullField flag to determine what to return - * @return time interval based on the flag + * @param isEmpty boolean value to represent that object created should be empty or not + * @return a property map suitable for constructing an object */ - public Object getTimeInterval(final boolean isNullField) { - if (isNullField) { - return null; - } else { - return APEX_STARTER_TIME_INTERVAL; + public Map<String, Object> getPdpStatusParametersMap(final boolean isEmpty) { + final Map<String, Object> map = new TreeMap<>(); + if (!isEmpty) { + map.put("timeInterval", TIME_INTERVAL); + map.put("pdpName", PDP_NAME); + map.put("version", VERSION); + map.put("pdpType", PDP_TYPE); + map.put("description", DESCRIPTION); + map.put("supportedPolicyTypes", SUPPORTED_POLICY_TYPES); } + return map; } - } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java index d22455214..2ba575ea6 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java @@ -48,10 +48,16 @@ public class TestApexStarterParameterGroup { final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject( commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME), ApexStarterParameterGroup.class); + final PdpStatusParameters pdpStatusParameters = apexStarterParameters.getPdpStatusParameters(); final GroupValidationResult validationResult = apexStarterParameters.validate(); assertTrue(validationResult.isValid()); assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarterParameters.getName()); - assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval()); + assertEquals(CommonTestData.TIME_INTERVAL, pdpStatusParameters.getTimeInterval()); + assertEquals(CommonTestData.PDP_NAME, pdpStatusParameters.getPdpName()); + assertEquals(CommonTestData.PDP_TYPE, pdpStatusParameters.getPdpType()); + assertEquals(CommonTestData.VERSION, pdpStatusParameters.getVersion()); + assertEquals(CommonTestData.DESCRIPTION, pdpStatusParameters.getDescription()); + assertEquals(CommonTestData.SUPPORTED_POLICY_TYPES, pdpStatusParameters.getSupportedPolicyTypes()); } @Test @@ -61,7 +67,6 @@ public class TestApexStarterParameterGroup { final GroupValidationResult validationResult = apexStarterParameters.validate(); assertFalse(validationResult.isValid()); assertEquals(null, apexStarterParameters.getName()); - assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval()); assertTrue(validationResult.getResult().contains("is null")); } @@ -72,7 +77,6 @@ public class TestApexStarterParameterGroup { final GroupValidationResult validationResult = apexStarterParameters.validate(); assertFalse(validationResult.isValid()); assertEquals("", apexStarterParameters.getName()); - assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, apexStarterParameters.getTimeInterval()); assertTrue(validationResult.getResult().contains( "field \"name\" type \"java.lang.String\" value \"\" INVALID, " + "must be a non-blank string")); } @@ -89,16 +93,16 @@ public class TestApexStarterParameterGroup { } @Test - public void testApexStarterParameterGroup_EmptyTimeInterval() { + public void testApexStarterParameterGroup_EmptyPdpStatusParameters() { final Map<String, Object> map = commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME); - map.put("timeInterval", commonTestData.getTimeInterval(true)); + map.put("pdpStatusParameters", commonTestData.getPdpStatusParametersMap(true)); final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject(map, ApexStarterParameterGroup.class); final GroupValidationResult validationResult = apexStarterParameters.validate(); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() - .contains("field \"timeInterval\" type \"int\" value \"0\" INVALID, must be >= 1") - && validationResult.getResult().contains("parameter group has status INVALID")); + .contains("\"org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup\" INVALID, " + + "parameter group has status INVALID")); } } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java index 3889b2aa5..baa91d23a 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java @@ -109,7 +109,6 @@ public class TestApexStarterParameterHandler { final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments); assertTrue(arguments.checkSetConfigurationFilePath()); assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, parGroup.getName()); - assertEquals(CommonTestData.APEX_STARTER_TIME_INTERVAL, parGroup.getTimeInterval()); } @Test diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java new file mode 100644 index 000000000..c5e061e1b --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java @@ -0,0 +1,60 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.apex.starter.parameters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.common.parameters.GroupValidationResult; + +/** + * Class to perform unit test of {@link PdpStatusParameters}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestPdpStatusParameters { + private static CommonTestData testData = new CommonTestData(); + + @Test + public void test() throws Exception { + final PdpStatusParameters pdpStatusParameters = + testData.toObject(testData.getPdpStatusParametersMap(false), PdpStatusParameters.class); + final GroupValidationResult validationResult = pdpStatusParameters.validate(); + assertTrue(validationResult.isValid()); + assertEquals(CommonTestData.TIME_INTERVAL, pdpStatusParameters.getTimeInterval()); + assertEquals(CommonTestData.PDP_NAME, pdpStatusParameters.getPdpName()); + assertEquals(CommonTestData.PDP_TYPE, pdpStatusParameters.getPdpType()); + assertEquals(CommonTestData.VERSION, pdpStatusParameters.getVersion()); + assertEquals(CommonTestData.DESCRIPTION, pdpStatusParameters.getDescription()); + assertEquals(CommonTestData.SUPPORTED_POLICY_TYPES, pdpStatusParameters.getSupportedPolicyTypes()); + } + + @Test + public void testValidate() throws Exception { + final PdpStatusParameters pdpStatusParameters = + testData.toObject(testData.getPdpStatusParametersMap(false), PdpStatusParameters.class); + final GroupValidationResult result = pdpStatusParameters.validate(); + assertNull(result.getResult()); + assertTrue(result.isValid()); + } +} diff --git a/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json b/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json index 2720cdefd..dad7408dc 100644 --- a/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json +++ b/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json @@ -1,4 +1,11 @@ { "name":"ApexStarterParameterGroup", - "timeInterval": 5 + "pdpStatusParameters":{ + "timeInterval": 2, + "pdpName":"apex-pdp", + "version":"0.0.1", + "pdpType":"apex", + "description":"Pdp status for HealthCheck", + "supportedPolicyTypes":[{"name":"policy1","version":"1.0"},{"name":"policy2","version":"1.0"}] + } } |