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/src/main | |
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/src/main')
9 files changed, 451 insertions, 17 deletions
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; +} |