--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 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.clamp.controlloop.models.controlloop.concepts;
+
+import java.util.Map;
+import javax.ws.rs.core.Response;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.onap.policy.common.utils.coder.Coder;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ParticipantUtils {
+
+ private static final Coder CODER = new StandardCoder();
+ private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
+
+ /**
+ * Finds participantType from a map of properties.
+ *
+ * @param properties Map of properties
+ * @return participantType
+ */
+ public static ToscaConceptIdentifier findParticipantType(Map<String, Object> properties) {
+ var objParticipantType = properties.get("participantType");
+ if (objParticipantType != null) {
+ try {
+ return CODER.decode(objParticipantType.toString(), ToscaConceptIdentifier.class);
+ } catch (CoderException e) {
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, e.getMessage(), e);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Checks If NodeTemplate Is ControlLoopElement.
+ *
+ * @param nodeTemplate the ToscaNodeTemplate
+ * @param toscaServiceTemplate the ToscaServiceTemplate
+ * @return true if NodeTemplate Is ControlLoopElement
+ */
+ public static boolean checkIfNodeTemplateIsControlLoopElement(ToscaNodeTemplate nodeTemplate,
+ ToscaServiceTemplate toscaServiceTemplate) {
+ if (nodeTemplate.getType().contains(CONTROL_LOOP_ELEMENT)) {
+ return true;
+ } else {
+ var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType());
+ if (nodeType != null) {
+ var derivedFrom = nodeType.getDerivedFrom();
+ if (derivedFrom != null) {
+ return derivedFrom.contains(CONTROL_LOOP_ELEMENT);
+ }
+ }
+ }
+ return false;
+ }
+}
import java.util.List;
import java.util.function.Supplier;
import javax.ws.rs.core.Response;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import org.onap.policy.common.parameters.BeanValidationResult;
import org.onap.policy.models.base.PfAuthorative;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfModelRuntimeException;
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ProviderUtils {
- private ProviderUtils() {
- // Utility class has no instances
- }
protected static <A, J extends PfConcept & PfAuthorative<A>> List<J> getJpaAndValidate(
List<A> authorativeConceptList, Supplier<J> jpaSupplier, String conceptDescription) {
participantClUpdateIntervalSec: 1000
participantClStateChangeIntervalSec: 1000
participantParameters:
- heartBeatMs: 120000
- maxMessageAgeMs: 600000
+ heartBeatMs: 20000
+ maxStatusWaitMs: 100000
updateParameters:
maxRetryCount: 3
- maxWaitMs: 100000
+ maxWaitMs: 20000
databaseProviderParameters:
name: PolicyProviderParameterGroup
implementation: org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl
package utils;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
/**
* Util class for Test scope.
*/
-public class ToscaUtils {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ToscaUtils {
private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
private static final String TOSCA_TEMPLATE_YAML = "src/test/resources/HttpParticipantConfig.yaml";
package org.onap.policy.clamp.controlloop.participant.kubernetes.utils;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-public class TestUtils {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class TestUtils {
private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
private static final String TOSCA_TEMPLATE_YAML = "src/test/resources/servicetemplates/KubernetesHelm.yaml";
import java.util.Map;
import java.util.Set;
import java.util.UUID;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class TestListenerUtils {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class TestListenerUtils {
private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
private static final Coder CODER = new StandardCoder();
static CommonTestData commonTestData = new CommonTestData();
private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
- private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
private static final String POLICY_TYPE_ID = "policy_type_id";
private static final String POLICY_ID = "policy_id";
- private TestListenerUtils() {}
-
/**
* Method to create a controlLoop from a yaml file.
*
*/
public static ControlLoopUpdate createControlLoopUpdateMsg() {
final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
- ToscaConceptIdentifier controlLoopId =
- new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
- ToscaConceptIdentifier participantId =
- new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
+ ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
+ ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
clUpdateMsg.setControlLoopId(controlLoopId);
clUpdateMsg.setParticipantId(participantId);
Map<String, ToscaNodeTemplate> nodeTemplatesMap =
toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
- if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) {
+ if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
+ toscaServiceTemplate)) {
ControlLoopElement clElement = new ControlLoopElement();
clElement.setId(UUID.randomUUID());
- ToscaConceptIdentifier clParticipantType;
- try {
- clParticipantType = CODER.decode(
- toscaInputEntry.getValue().getProperties().get("participantType").toString(),
- ToscaConceptIdentifier.class);
- } catch (CoderException e) {
- throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
- }
+ var clParticipantType =
+ ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
clElement.setParticipantId(clParticipantType);
clElement.setParticipantType(clParticipantType);
- clElement.setDefinition(new ToscaConceptIdentifier(toscaInputEntry.getKey(),
- toscaInputEntry.getValue().getVersion()));
+ clElement.setDefinition(
+ new ToscaConceptIdentifier(toscaInputEntry.getKey(), toscaInputEntry.getValue().getVersion()));
clElement.setState(ControlLoopState.UNINITIALISED);
clElement.setDescription(toscaInputEntry.getValue().getDescription());
clElement.setOrderedState(ControlLoopOrderedState.PASSIVE);
return clUpdateMsg;
}
- private static boolean checkIfNodeTemplateIsControlLoopElement(ToscaNodeTemplate nodeTemplate,
- ToscaServiceTemplate toscaServiceTemplate) {
- if (nodeTemplate.getType().contains(CONTROL_LOOP_ELEMENT)) {
- return true;
- } else {
- var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType());
- if (nodeType != null) {
- var derivedFrom = nodeType.getDerivedFrom();
- if (derivedFrom != null) {
- return derivedFrom.contains(CONTROL_LOOP_ELEMENT) ? true : false;
- }
- }
- }
- return false;
- }
-
private static void populateToscaNodeTemplateFragment(ControlLoopElement clElement,
ToscaServiceTemplate toscaServiceTemplate) {
- ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate
- .getToscaTopologyTemplate().getNodeTemplates().get(clElement.getDefinition().getName());
+ ToscaNodeTemplate toscaNodeTemplate = toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates()
+ .get(clElement.getDefinition().getName());
// If the ControlLoopElement has policy_type_id or policy_id, identify it as a PolicyControlLoopElement
// and pass respective PolicyTypes or Policies as part of toscaServiceTemplateFragment
if ((toscaNodeTemplate.getProperties().get(POLICY_TYPE_ID) != null)
}
private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement,
- List<ParticipantUpdates> participantUpdates) {
+ List<ParticipantUpdates> participantUpdates) {
if (participantUpdates.isEmpty()) {
participantUpdates.add(getControlLoopElementList(clElement));
} else {
public static ParticipantUpdate createParticipantUpdateMsg() {
final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
- ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
- "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
+ ToscaConceptIdentifier participantType =
+ new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
participantUpdateMsg.setParticipantId(participantId);
participantUpdateMsg.setTimestamp(Instant.now());
TestListenerUtils.addPoliciesToToscaServiceTemplate(toscaServiceTemplate);
List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
- for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry :
- toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
- if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) {
- ToscaConceptIdentifier clParticipantType;
- try {
- clParticipantType = CODER.decode(
- toscaInputEntry.getValue().getProperties().get("participantType").toString(),
- ToscaConceptIdentifier.class);
- } catch (CoderException e) {
- throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
- }
+ for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
+ .getNodeTemplates().entrySet()) {
+ if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
+ toscaServiceTemplate)) {
+ var clParticipantType =
+ ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
- toscaInputEntry.getValue(), participantDefinitionUpdates);
+ toscaInputEntry.getValue(), participantDefinitionUpdates);
}
}
}
private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
- ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
+ ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
var clDefinition = new ControlLoopElementDefinition();
- clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
- entryKey, entryValue.getVersion()));
+ clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
if (participantDefinitionUpdates.isEmpty()) {
- participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType,
- controlLoopElementDefinitionList));
+ participantDefinitionUpdates
+ .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
} else {
boolean participantExists = false;
for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
}
}
if (!participantExists) {
- participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType,
- controlLoopElementDefinitionList));
+ participantDefinitionUpdates.add(
+ getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
}
}
}
private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
- ToscaConceptIdentifier clParticipantType,
- List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
+ ToscaConceptIdentifier clParticipantType,
+ List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
ParticipantDefinition participantDefinition = new ParticipantDefinition();
participantDefinition.setParticipantType(clParticipantType);
controlLoopElementDefinitionList.add(clDefinition);
return toscaServiceTemplate;
}
- private static void addPolicyTypesToToscaServiceTemplate(
- ToscaServiceTemplate toscaServiceTemplate) {
+ private static void addPolicyTypesToToscaServiceTemplate(ToscaServiceTemplate toscaServiceTemplate) {
Set<String> policyTypeDirectoryContents = ResourceUtils.getDirectoryContents("policytypes");
for (String policyTypeFilePath : policyTypeDirectoryContents) {
for (String policiesFilePath : policiesDirectoryContents) {
String policiesString = ResourceUtils.getResourceAsString(policiesFilePath);
- ToscaServiceTemplate foundPoliciesSt =
- yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
- toscaServiceTemplate.getToscaTopologyTemplate().setPolicies(
- foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
+ ToscaServiceTemplate foundPoliciesSt = yamlTranslator.fromYaml(policiesString, ToscaServiceTemplate.class);
+ toscaServiceTemplate.getToscaTopologyTemplate()
+ .setPolicies(foundPoliciesSt.getToscaTopologyTemplate().getPolicies());
}
}
throw new FileNotFoundException(controlLoopFilePath);
}
- ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
- controlLoopString, ToscaServiceTemplate.class);
+ ToscaServiceTemplate serviceTemplate =
+ yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
return serviceTemplate;
} catch (FileNotFoundException e) {
LOGGER.error("cannot find YAML file", controlLoopFilePath);
package org.onap.policy.clamp.controlloop.participant.simulator.config;
+import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
+import org.onap.policy.clamp.controlloop.participant.simulator.main.handler.ControlLoopElementHandler;
import org.onap.policy.clamp.controlloop.participant.simulator.main.rest.RequestResponseLoggingFilter;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
return registrationBean;
}
+
+ /**
+ * Register ControlLoopElementListener.
+ *
+ * @param intermediaryApi the ParticipantIntermediaryApi
+ * @param clElementHandler the ControlLoop Element Handler
+ */
+ @Autowired
+ public void registerControlLoopElementListener(ParticipantIntermediaryApi intermediaryApi,
+ ControlLoopElementHandler clElementHandler) {
+ intermediaryApi.registerControlLoopElementListener(clElementHandler);
+ clElementHandler.setIntermediaryApi(intermediaryApi);
+ }
}
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 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.clamp.controlloop.participant.simulator.main.handler;
+
+import java.time.Instant;
+import java.util.UUID;
+import lombok.Setter;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopOrderedState;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
+import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
+import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
+import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ * This class handles implementation of controlLoopElement updates.
+ */
+@Component
+public class ControlLoopElementHandler implements ControlLoopElementListener {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(ControlLoopElementHandler.class);
+
+ @Setter
+ private ParticipantIntermediaryApi intermediaryApi;
+
+ /**
+ * Callback method to handle a control loop element state change.
+ *
+ * @param controlLoopElementId the ID of the control loop element
+ * @param currentState the current state of the control loop element
+ * @param newState the state to which the control loop element is changing to
+ * @throws PfModelException in case of an exception
+ */
+ @Override
+ public void controlLoopElementStateChange(ToscaConceptIdentifier controlLoopId,
+ UUID controlLoopElementId, ControlLoopState currentState,
+ ControlLoopOrderedState newState) throws PfModelException {
+ switch (newState) {
+ case UNINITIALISED:
+ intermediaryApi.updateControlLoopElementState(controlLoopId,
+ controlLoopElementId, newState, ControlLoopState.UNINITIALISED,
+ ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
+ break;
+ case PASSIVE:
+ intermediaryApi.updateControlLoopElementState(controlLoopId,
+ controlLoopElementId, newState, ControlLoopState.PASSIVE,
+ ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
+ break;
+ case RUNNING:
+ intermediaryApi.updateControlLoopElementState(controlLoopId,
+ controlLoopElementId, newState, ControlLoopState.RUNNING,
+ ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
+ break;
+ default:
+ LOGGER.debug("Unknown orderedstate {}", newState);
+ break;
+ }
+ }
+
+ /**
+ * Callback method to handle an update on a control loop element.
+ *
+ * @param element the information on the control loop element
+ * @param clElementDefinition toscaNodeTemplate
+ * @throws PfModelException in case of an exception
+ */
+ @Override
+ public void controlLoopElementUpdate(ToscaConceptIdentifier controlLoopId, ControlLoopElement element,
+ ToscaNodeTemplate clElementDefinition)
+ throws PfModelException {
+ intermediaryApi.updateControlLoopElementState(controlLoopId, element.getId(), element.getOrderedState(),
+ ControlLoopState.PASSIVE, ParticipantMessageType.CONTROL_LOOP_UPDATE);
+ }
+
+ @Override
+ public void handleStatistics(UUID controlLoopElementId) throws PfModelException {
+ var clElement = intermediaryApi.getControlLoopElement(controlLoopElementId);
+ if (clElement != null) {
+ var clElementStatistics = new ClElementStatistics();
+ clElementStatistics.setControlLoopState(clElement.getState());
+ clElementStatistics.setTimeStamp(Instant.now());
+ intermediaryApi.updateControlLoopElementStatistics(controlLoopElementId, clElementStatistics);
+ }
+ }
+
+}
participant:
intermediaryParameters:
- reportingTimeIntervalMs: 120000
+ reportingTimeIntervalMs: 60000
description: Participant Description
participantId:
name: org.onap.PM_CDS_Blueprint
import java.util.Map;
import java.util.Set;
import java.util.UUID;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopState;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUpdates;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopStateChange;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ControlLoopUpdate;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class TestListenerUtils {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class TestListenerUtils {
private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
private static final Coder CODER = new StandardCoder();
static CommonTestData commonTestData = new CommonTestData();
private static final Logger LOGGER = LoggerFactory.getLogger(TestListenerUtils.class);
- private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
-
- private TestListenerUtils() {}
/**
* Method to create a controlLoop from a yaml file.
*/
public static ControlLoopUpdate createControlLoopUpdateMsg() {
final ControlLoopUpdate clUpdateMsg = new ControlLoopUpdate();
- ToscaConceptIdentifier controlLoopId =
- new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
- ToscaConceptIdentifier participantId =
- new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
+ ToscaConceptIdentifier controlLoopId = new ToscaConceptIdentifier("PMSHInstance0", "1.0.0");
+ ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "0.0.0");
clUpdateMsg.setControlLoopId(controlLoopId);
clUpdateMsg.setParticipantId(participantId);
Map<String, ToscaNodeTemplate> nodeTemplatesMap =
toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates();
for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : nodeTemplatesMap.entrySet()) {
- if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) {
+ if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
+ toscaServiceTemplate)) {
ControlLoopElement clElement = new ControlLoopElement();
clElement.setId(UUID.randomUUID());
- ToscaConceptIdentifier clParticipantType;
- try {
- clParticipantType = CODER.decode(
- toscaInputEntry.getValue().getProperties().get("participantType").toString(),
- ToscaConceptIdentifier.class);
- } catch (CoderException e) {
- throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
- }
+ var clParticipantType =
+ ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
clElement.setParticipantId(clParticipantType);
clElement.setParticipantType(clParticipantType);
- clElement.setDefinition(new ToscaConceptIdentifier(toscaInputEntry.getKey(),
- toscaInputEntry.getValue().getVersion()));
+ clElement.setDefinition(
+ new ToscaConceptIdentifier(toscaInputEntry.getKey(), toscaInputEntry.getValue().getVersion()));
clElement.setState(ControlLoopState.UNINITIALISED);
clElement.setDescription(toscaInputEntry.getValue().getDescription());
clElement.setOrderedState(ControlLoopOrderedState.PASSIVE);
return clUpdateMsg;
}
- private static boolean checkIfNodeTemplateIsControlLoopElement(ToscaNodeTemplate nodeTemplate,
- ToscaServiceTemplate toscaServiceTemplate) {
- if (nodeTemplate.getType().contains(CONTROL_LOOP_ELEMENT)) {
- return true;
- } else {
- var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType());
- if (nodeType != null) {
- var derivedFrom = nodeType.getDerivedFrom();
- if (derivedFrom != null) {
- return derivedFrom.contains(CONTROL_LOOP_ELEMENT) ? true : false;
- }
- }
- }
- return false;
- }
-
private static void prepareParticipantUpdateForControlLoop(ControlLoopElement clElement,
- List<ParticipantUpdates> participantUpdates) {
+ List<ParticipantUpdates> participantUpdates) {
if (participantUpdates.isEmpty()) {
participantUpdates.add(getControlLoopElementList(clElement));
} else {
public static ParticipantUpdate createParticipantUpdateMsg() {
final ParticipantUpdate participantUpdateMsg = new ParticipantUpdate();
ToscaConceptIdentifier participantId = new ToscaConceptIdentifier("org.onap.PM_Policy", "1.0.0");
- ToscaConceptIdentifier participantType = new ToscaConceptIdentifier(
- "org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
+ ToscaConceptIdentifier participantType =
+ new ToscaConceptIdentifier("org.onap.policy.controlloop.PolicyControlLoopParticipant", "2.3.1");
participantUpdateMsg.setParticipantId(participantId);
participantUpdateMsg.setTimestamp(Instant.now());
// Add policies to the toscaServiceTemplate
List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
- for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry :
- toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
- if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) {
- ToscaConceptIdentifier clParticipantType;
- try {
- clParticipantType = CODER.decode(
- toscaInputEntry.getValue().getProperties().get("participantType").toString(),
- ToscaConceptIdentifier.class);
- } catch (CoderException e) {
- throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
- }
+ for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
+ .getNodeTemplates().entrySet()) {
+ if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
+ toscaServiceTemplate)) {
+ var clParticipantType =
+ ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
- toscaInputEntry.getValue(), participantDefinitionUpdates);
+ toscaInputEntry.getValue(), participantDefinitionUpdates);
}
}
}
private static void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
- ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
+ ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
var clDefinition = new ControlLoopElementDefinition();
- clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
- entryKey, entryValue.getVersion()));
+ clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
if (participantDefinitionUpdates.isEmpty()) {
- participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType,
- controlLoopElementDefinitionList));
+ participantDefinitionUpdates
+ .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
} else {
boolean participantExists = false;
for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
}
}
if (!participantExists) {
- participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType,
- controlLoopElementDefinitionList));
+ participantDefinitionUpdates.add(
+ getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
}
}
}
private static ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
- ToscaConceptIdentifier clParticipantType,
- List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
+ ToscaConceptIdentifier clParticipantType,
+ List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
ParticipantDefinition participantDefinition = new ParticipantDefinition();
participantDefinition.setParticipantType(clParticipantType);
controlLoopElementDefinitionList.add(clDefinition);
* @return ControlLoopUpdate message
* @throws CoderException exception while reading the file to object
*/
- public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath)
- throws CoderException {
- ControlLoopUpdate controlLoopUpdateMsg =
- CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
+ public static ControlLoopUpdate createParticipantClUpdateMsgFromJson(String jsonFilePath) throws CoderException {
+ ControlLoopUpdate controlLoopUpdateMsg = CODER.decode(new File(jsonFilePath), ControlLoopUpdate.class);
return controlLoopUpdateMsg;
}
throw new FileNotFoundException(controlLoopFilePath);
}
- ToscaServiceTemplate serviceTemplate = yamlTranslator.fromYaml(
- controlLoopString, ToscaServiceTemplate.class);
+ ToscaServiceTemplate serviceTemplate =
+ yamlTranslator.fromYaml(controlLoopString, ToscaServiceTemplate.class);
return serviceTemplate;
} catch (FileNotFoundException e) {
LOGGER.error("cannot find YAML file", controlLoopFilePath);
*/
void registerControlLoopElementListener(ControlLoopElementListener controlLoopElementListener);
- /**
- * Send participant register message to controlloop runtime.
- */
- void sendParticipantRegister();
-
- /**
- * Send participant deregister message to controlloop runtime.
- */
- void sendParticipantDeregister();
-
/**
* Get participants loops from the intermediary API.
*
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
+import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ControlLoopHandler;
import org.onap.policy.clamp.controlloop.participant.intermediary.handler.ParticipantHandler;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.springframework.stereotype.Component;
public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryApi {
// The handler for the participant intermediary
- private ParticipantHandler participantHandler;
+ private final ParticipantHandler participantHandler;
+
+ // The handler for the controlLoop intermediary
+ private final ControlLoopHandler controlLoopHandler;
/**
* Constructor.
*
* @param participantHandler ParticipantHandler
+ * @param controlLoopHandler ControlLoopHandler
*/
- public ParticipantIntermediaryApiImpl(ParticipantHandler participantHandler) {
+ public ParticipantIntermediaryApiImpl(ParticipantHandler participantHandler,
+ ControlLoopHandler controlLoopHandler) {
this.participantHandler = participantHandler;
+ this.controlLoopHandler = controlLoopHandler;
}
@Override
public void registerControlLoopElementListener(ControlLoopElementListener controlLoopElementListener) {
- participantHandler.getControlLoopHandler().registerControlLoopElementListener(controlLoopElementListener);
- }
-
- @Override
- public void sendParticipantRegister() {
- participantHandler.sendParticipantRegister();
- }
-
- @Override
- public void sendParticipantDeregister() {
- participantHandler.sendParticipantDeregister();
+ controlLoopHandler.registerControlLoopElementListener(controlLoopElementListener);
}
@Override
@Override
public ControlLoops getControlLoops(String name, String version) {
- return participantHandler.getControlLoopHandler().getControlLoops();
+ return controlLoopHandler.getControlLoops();
}
@Override
public Map<UUID, ControlLoopElement> getControlLoopElements(String name, String version) {
- List<ControlLoop> controlLoops =
- participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList();
+ List<ControlLoop> controlLoops = controlLoopHandler.getControlLoops().getControlLoopList();
for (ControlLoop controlLoop : controlLoops) {
if (name.equals(controlLoop.getDefinition().getName())) {
@Override
public ControlLoopElement getControlLoopElement(UUID id) {
- List<ControlLoop> controlLoops =
- participantHandler.getControlLoopHandler().getControlLoops().getControlLoopList();
+ List<ControlLoop> controlLoops = controlLoopHandler.getControlLoops().getControlLoopList();
for (ControlLoop controlLoop : controlLoops) {
ControlLoopElement clElement = controlLoop.getElements().get(id);
public ControlLoopElement updateControlLoopElementState(ToscaConceptIdentifier controlLoopId,
UUID id, ControlLoopOrderedState currentState,
ControlLoopState newState, ParticipantMessageType messageType) {
- return participantHandler.getControlLoopHandler().updateControlLoopElementState(controlLoopId,
+ return controlLoopHandler.updateControlLoopElementState(controlLoopId,
id, currentState, newState);
}
@Override
public void updateControlLoopElementStatistics(UUID id, ClElementStatistics elementStatistics) {
- participantHandler.getControlLoopHandler().updateControlLoopElementStatistics(id, elementStatistics);
+ controlLoopHandler.updateControlLoopElementStatistics(id, elementStatistics);
}
}
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.Getter;
-import org.apache.commons.collections4.CollectionUtils;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoop;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
import org.onap.policy.clamp.controlloop.participant.intermediary.api.ControlLoopElementListener;
import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantMessagePublisher;
import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
-import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
* @param newState the ordered state
* @return controlLoopElement the updated controlloop element
*/
- public ControlLoopElement updateControlLoopElementState(ToscaConceptIdentifier controlLoopId,
- UUID id, ControlLoopOrderedState orderedState,
- ControlLoopState newState) {
+ public ControlLoopElement updateControlLoopElementState(ToscaConceptIdentifier controlLoopId, UUID id,
+ ControlLoopOrderedState orderedState, ControlLoopState newState) {
if (id == null) {
LOGGER.warn("Cannot update Control loop element state, id is null");
+ return null;
}
- ControlLoopElement clElement = elementsOnThisParticipant.get(id);
for (var controlLoop : controlLoopMap.values()) {
var element = controlLoop.getElements().get(id);
if (element != null) {
element.setState(newState);
}
+ var checkOpt = controlLoop.getElements().values().stream()
+ .filter(clElement -> !newState.equals(clElement.getState())).findAny();
+ if (checkOpt.isEmpty()) {
+ controlLoop.setState(newState);
+ controlLoop.setOrderedState(orderedState);
+ }
}
+ var clElement = elementsOnThisParticipant.get(id);
if (clElement != null) {
- var controlLoopStateChangeAck =
- new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK);
+ var controlLoopStateChangeAck = new ControlLoopAck(ParticipantMessageType.CONTROLLOOP_STATECHANGE_ACK);
controlLoopStateChangeAck.setParticipantId(participantId);
controlLoopStateChangeAck.setParticipantType(participantType);
controlLoopStateChangeAck.setControlLoopId(controlLoopId);
clElement.setOrderedState(orderedState);
clElement.setState(newState);
- controlLoopStateChangeAck.getControlLoopResultMap().put(clElement.getId(),
- new ControlLoopElementAck(newState, true,
- "Control loop element {} state changed to {}\", id, newState)"));
+ controlLoopStateChangeAck.getControlLoopResultMap().put(clElement.getId(), new ControlLoopElementAck(
+ newState, true, "Control loop element {} state changed to {}\", id, newState)"));
LOGGER.debug("Control loop element {} state changed to {}", id, newState);
controlLoopStateChangeAck.setMessage("ControlLoopElement state changed to {} " + newState);
controlLoopStateChangeAck.setResult(true);
* @param elementStatistics control loop element Statistics
*/
public void updateControlLoopElementStatistics(UUID id, ClElementStatistics elementStatistics) {
- ControlLoopElement clElement = elementsOnThisParticipant.get(id);
+ var clElement = elementsOnThisParticipant.get(id);
if (clElement != null) {
elementStatistics.setParticipantId(participantId);
elementStatistics.setId(id);
controlLoopAck.setParticipantId(participantId);
controlLoopAck.setParticipantType(participantType);
controlLoopAck.setMessage("Control loop " + stateChangeMsg.getControlLoopId()
- + " does not use this participant " + participantId);
+ + " does not use this participant " + participantId);
controlLoopAck.setResult(false);
controlLoopAck.setResponseTo(stateChangeMsg.getMessageId());
controlLoopAck.setControlLoopId(stateChangeMsg.getControlLoopId());
* @param updateMsg the update message
*/
public void handleControlLoopUpdate(ControlLoopUpdate updateMsg,
- List<ControlLoopElementDefinition> clElementDefinitions) {
+ List<ControlLoopElementDefinition> clElementDefinitions) {
if (!updateMsg.appliesTo(participantType, participantId)) {
return;
controlLoopUpdateAck.setParticipantType(participantType);
controlLoopUpdateAck.setMessage("Control loop " + updateMsg.getControlLoopId()
- + " already defined on participant " + participantId);
+ + " already defined on participant " + participantId);
controlLoopUpdateAck.setResult(false);
controlLoopUpdateAck.setResponseTo(updateMsg.getMessageId());
controlLoopUpdateAck.setControlLoopId(updateMsg.getControlLoopId());
return;
}
- List<ControlLoopElement> clElements = storeElementsOnThisParticipant(updateMsg.getParticipantUpdatesList());
+ var clElements = storeElementsOnThisParticipant(updateMsg.getParticipantUpdatesList());
try {
- for (ControlLoopElement element : clElements) {
- ToscaNodeTemplate clElementNodeTemplate = getClElementNodeTemplate(
- clElementDefinitions, element.getDefinition());
- for (ControlLoopElementListener clElementListener : listeners) {
- clElementListener.controlLoopElementUpdate(updateMsg.getControlLoopId(),
- element, clElementNodeTemplate);
+ for (var element : clElements) {
+ var clElementNodeTemplate =
+ getClElementNodeTemplate(clElementDefinitions, element.getDefinition());
+ for (var clElementListener : listeners) {
+ clElementListener.controlLoopElementUpdate(updateMsg.getControlLoopId(), element,
+ clElementNodeTemplate);
}
}
} catch (PfModelException e) {
LOGGER.debug("Control loop element update failed {}", updateMsg.getControlLoopId());
}
- Map<UUID, ControlLoopElement> clElementMap = prepareClElementMap(clElements);
+ var clElementMap = prepareClElementMap(clElements);
controlLoop = new ControlLoop();
controlLoop.setDefinition(updateMsg.getControlLoopId());
controlLoop.setElements(clElementMap);
}
private ToscaNodeTemplate getClElementNodeTemplate(List<ControlLoopElementDefinition> clElementDefinitions,
- ToscaConceptIdentifier clElementDefId) {
- for (ControlLoopElementDefinition clElementDefinition : clElementDefinitions) {
+ ToscaConceptIdentifier clElementDefId) {
+ for (var clElementDefinition : clElementDefinitions) {
if (clElementDefinition.getClElementDefinitionId().equals(clElementDefId)) {
return clElementDefinition.getControlLoopElementToscaNodeTemplate();
}
}
private List<ControlLoopElement> storeElementsOnThisParticipant(List<ParticipantUpdates> participantUpdates) {
- var clElementMap =
- participantUpdates.stream()
+ var clElementMap = participantUpdates.stream()
.flatMap(participantUpdate -> participantUpdate.getControlLoopElementList().stream())
- .filter(element -> participantType.equals(element.getParticipantType()))
- .collect(Collectors.toList());
+ .filter(element -> participantType.equals(element.getParticipantType())).collect(Collectors.toList());
for (var element : clElementMap) {
elementsOnThisParticipant.put(element.getId(), element);
private Map<UUID, ControlLoopElement> prepareClElementMap(List<ControlLoopElement> clElements) {
Map<UUID, ControlLoopElement> clElementMap = new LinkedHashMap<>();
- for (ControlLoopElement element : clElements) {
+ for (var element : clElements) {
clElementMap.put(element.getId(), element);
}
return clElementMap;
* @param orderedState orderedState
*/
private void handleUninitialisedState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) {
- handleStateChange(controlLoop, orderedState, ControlLoopState.UNINITIALISED);
- controlLoopMap.remove(controlLoop.getKey().asIdentifier());
-
- for (ControlLoopElementListener clElementListener : listeners) {
- try {
- for (ControlLoopElement element : controlLoop.getElements().values()) {
- clElementListener.controlLoopElementStateChange(controlLoop.getDefinition(),
- element.getId(), element.getState(), orderedState);
- }
- } catch (PfModelException e) {
- LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition());
- }
- }
+ handleStateChange(controlLoop, orderedState);
+ controlLoopMap.remove(controlLoop.getDefinition());
+ controlLoop.getElements().values().forEach(element -> elementsOnThisParticipant.remove(element.getId()));
}
/**
* @param orderedState orderedState
*/
private void handlePassiveState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) {
- handleStateChange(controlLoop, orderedState, ControlLoopState.PASSIVE);
+ handleStateChange(controlLoop, orderedState);
}
/**
* @param orderedState orderedState
*/
private void handleRunningState(final ControlLoop controlLoop, final ControlLoopOrderedState orderedState) {
- handleStateChange(controlLoop, orderedState, ControlLoopState.RUNNING);
+ handleStateChange(controlLoop, orderedState);
}
/**
*
* @param controlLoop participant status in memory
* @param orderedState orderedState the new ordered state the participant should have
- * @param newState new state of the control loop elements
*/
- private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState,
- ControlLoopState newState) {
+ private void handleStateChange(ControlLoop controlLoop, final ControlLoopOrderedState orderedState) {
if (orderedState.equals(controlLoop.getOrderedState())) {
var controlLoopAck = new ControlLoopAck(ParticipantMessageType.CONTROL_LOOP_STATE_CHANGE);
controlLoopAck.setParticipantId(participantId);
controlLoopAck.setParticipantType(participantType);
- controlLoopAck.setMessage("Control loop is already in state" + orderedState);
+ controlLoopAck.setMessage("Control loop is already in state " + orderedState);
controlLoopAck.setResult(false);
controlLoopAck.setControlLoopId(controlLoop.getDefinition());
publisher.sendControlLoopAck(controlLoopAck);
return;
}
- if (!CollectionUtils.isEmpty(controlLoop.getElements().values())) {
- controlLoop.getElements().values().forEach(element -> {
- element.setState(newState);
- element.setOrderedState(orderedState);
- });
+ for (var clElementListener : listeners) {
+ try {
+ for (var element : controlLoop.getElements().values()) {
+ clElementListener.controlLoopElementStateChange(controlLoop.getDefinition(), element.getId(),
+ element.getState(), orderedState);
+ }
+ } catch (PfModelException e) {
+ LOGGER.debug("Control loop element update failed {}", controlLoop.getDefinition());
+ }
}
-
- controlLoop.setOrderedState(orderedState);
}
/**
import java.io.Closeable;
import java.io.IOException;
import java.util.List;
-import org.onap.policy.clamp.controlloop.participant.intermediary.api.ParticipantIntermediaryApi;
import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
private List<TopicSink> topicSinks;
private List<TopicSource> topicSources;
- ParticipantIntermediaryApi participantIntermediaryApi;
+ private ParticipantHandler participantHandler;
private final MessageTypeDispatcher msgDispatcher;
* Instantiate the activator for participant.
*
* @param parameters the ParticipantParameters
+ * @param participantHandler the ParticipantHandler
* @param publishers list of Publishers
* @param listeners list of Listeners
*/
public <T> IntermediaryActivator(final ParticipantParameters parameters,
- ParticipantIntermediaryApi participantIntermediaryApi, List<Publisher> publishers,
+ ParticipantHandler participantHandler, List<Publisher> publishers,
List<Listener<T>> listeners) {
- this.participantIntermediaryApi = participantIntermediaryApi;
+ this.participantHandler = participantHandler;
topicSinks = TopicEndpointManager.getManager()
.addTopicSinks(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSinks());
}
private void sendParticipantRegister() {
- participantIntermediaryApi.sendParticipantRegister();
+ participantHandler.sendParticipantRegister();
}
private void sendParticipantDeregister() {
- participantIntermediaryApi.sendParticipantDeregister();
+ participantHandler.sendParticipantDeregister();
}
/**
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
+import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
/**
* This class is responsible for managing the state of a participant.
*/
-@Getter
@Component
public class ParticipantHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantHandler.class);
+ @Getter
private final ToscaConceptIdentifier participantType;
+
+ @Getter
private final ToscaConceptIdentifier participantId;
+
private final ControlLoopHandler controlLoopHandler;
private final ParticipantStatistics participantStatistics;
private final ParticipantMessagePublisher publisher;
* @param participantStatusReqMsg participant participantStatusReq message
*/
public void handleParticipantStatusReq(final ParticipantStatusReq participantStatusReqMsg) {
- var controlLoops = controlLoopHandler.getControlLoops();
- for (ControlLoopElementListener clElementListener : controlLoopHandler.getListeners()) {
- updateClElementStatistics(controlLoops, clElementListener);
- }
-
var participantStatus = makeHeartbeat(true);
publisher.sendParticipantStatus(participantStatus);
}
* Method to send heartbeat to controlloop runtime.
*/
public ParticipantStatus makeHeartbeat(boolean responseToParticipantStatusReq) {
+ if (!responseToParticipantStatusReq) {
+ var controlLoops = controlLoopHandler.getControlLoops();
+ for (var clElementListener : controlLoopHandler.getListeners()) {
+ updateClElementStatistics(controlLoops, clElementListener);
+ }
+ }
this.participantStatistics.setState(state);
this.participantStatistics.setHealthStatus(healthStatus);
this.participantStatistics.setTimeStamp(Instant.now());
clStatitistics.setControlLoopId(entry.getKey());
ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
clElementStatisticsList
- .setClElementStatistics(entry.getValue().getControlLoopElementStatisticsList(entry.getValue()));
+ .setClElementStatistics(entry.getValue().getElements().values()
+ .stream()
+ .map(ControlLoopElement::getClElementStatistics)
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList()));
clStatitistics.setClElementStatisticsList(clElementStatisticsList);
clInfo.setControlLoopStatistics(clStatitistics);
clInfo.setState(entry.getValue().getState());
+ controlLoopInfoList.add(clInfo);
}
return controlLoopInfoList;
}
package org.onap.policy.clamp.controlloop.runtime.main.parameters;
-import java.util.concurrent.TimeUnit;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@Validated
public class ParticipantParameters {
- /**
- * Default maximum message age, in milliseconds, that should be examined. Any message
- * older than this is discarded.
- */
- public static final long DEFAULT_MAX_AGE_MS = TimeUnit.MILLISECONDS.convert(10, TimeUnit.MINUTES);
-
- @Min(1)
+ @Min(100)
private long heartBeatMs;
- @Min(1)
- private long maxMessageAgeMs = DEFAULT_MAX_AGE_MS;
+ @Min(100)
+ private long maxStatusWaitMs;
@Valid
@NotNull
/**
* Maximum number of times to re-send a request to a PDP.
*/
- @Min(value = 0)
+ @Min(value = 1)
private int maxRetryCount;
/**
* Maximum time to wait, in milliseconds, for a PDP response.
*/
- @Min(value = 0)
+ @Min(value = 100)
private long maxWaitMs;
}
private final ParticipantStatusReqPublisher participantStatusReqPublisher;
private final ParticipantUpdatePublisher participantUpdatePublisher;
- private final long maxWaitMs;
-
/**
* Constructor for instantiating SupervisionScanner.
*
controlLoopCounter.setMaxRetryCount(
clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxRetryCount());
- controlLoopCounter
- .setMaxWaitMs(clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxWaitMs());
+ controlLoopCounter.setMaxWaitMs(clRuntimeParameterGroup.getParticipantParameters().getMaxStatusWaitMs());
participantUpdateCounter.setMaxRetryCount(
clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxRetryCount());
participantStatusCounter.setMaxRetryCount(
clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxRetryCount());
- participantStatusCounter
- .setMaxWaitMs(clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxWaitMs());
-
- maxWaitMs = clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().getMaxWaitMs();
+ participantStatusCounter.setMaxWaitMs(clRuntimeParameterGroup.getParticipantParameters().getMaxStatusWaitMs());
}
/**
} catch (PfModelException pfme) {
LOGGER.warn("error reading control loops from database", pfme);
}
+
if (counterCheck) {
scanParticipantUpdate();
}
if (participantUpdateCounter.isFault(id)) {
LOGGER.debug("report Participant Update fault");
- } else if (participantUpdateCounter.getDuration(id) > maxWaitMs) {
+ } else if (participantUpdateCounter.getDuration(id) > participantUpdateCounter.getMaxWaitMs()) {
if (participantUpdateCounter.count(id)) {
LOGGER.debug("retry message ParticipantUpdate");
LOGGER.debug("report Participant fault");
return;
}
- if (participantStatusCounter.getDuration(id) > maxWaitMs) {
+ if (participantStatusCounter.getDuration(id) > participantStatusCounter.getMaxWaitMs()) {
if (participantStatusCounter.count(id)) {
LOGGER.debug("retry message ParticipantStatusReq");
participantStatusReqPublisher.send(id);
return;
}
- if (controlLoopCounter.count(id)) {
- if (ControlLoopState.UNINITIALISED2PASSIVE.equals(controlLoop.getState())) {
- LOGGER.debug("retry message ControlLoopUpdate");
- controlLoopUpdatePublisher.send(controlLoop);
+ if (controlLoopCounter.getDuration(id) > controlLoopCounter.getMaxWaitMs()) {
+ if (controlLoopCounter.count(id)) {
+ if (ControlLoopState.UNINITIALISED2PASSIVE.equals(controlLoop.getState())) {
+ LOGGER.debug("retry message ControlLoopUpdate");
+ controlLoopUpdatePublisher.send(controlLoop);
+ } else {
+ LOGGER.debug("retry message ControlLoopStateChange");
+ controlLoopStateChangePublisher.send(controlLoop);
+ }
} else {
- LOGGER.debug("retry message ControlLoopStateChange");
- controlLoopStateChangePublisher.send(controlLoop);
+ LOGGER.debug("report ControlLoop fault");
+ controlLoopCounter.setFault(id);
}
- } else {
- LOGGER.debug("report ControlLoop fault");
- controlLoopCounter.setFault(id);
}
}
}
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
-import java.util.Map;
import lombok.AllArgsConstructor;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdate;
-import org.onap.policy.common.utils.coder.Coder;
-import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
public class ParticipantUpdatePublisher extends AbstractParticipantPublisher<ParticipantUpdate> {
private static final Logger LOGGER = LoggerFactory.getLogger(ParticipantUpdatePublisher.class);
- private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
- private static final Coder CODER = new StandardCoder();
+
private final PolicyModelsProvider modelsProvider;
/**
message.setParticipantType(participantType);
message.setTimestamp(Instant.now());
- ToscaServiceTemplate toscaServiceTemplate;
+ ToscaServiceTemplate toscaServiceTemplate = null;
try {
- toscaServiceTemplate = modelsProvider.getServiceTemplateList(null, null).get(0);
+ var list = modelsProvider.getServiceTemplateList(null, null);
+ if (!list.isEmpty()) {
+ toscaServiceTemplate = list.get(0);
+ }
} catch (PfModelException pfme) {
LOGGER.warn("Get of tosca service template failed, cannot send participantupdate", pfme);
return;
}
List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
- for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
- .getNodeTemplates().entrySet()) {
- if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) {
- ToscaConceptIdentifier clParticipantType;
- try {
- clParticipantType =
- CODER.decode(toscaInputEntry.getValue().getProperties().get("participantType").toString(),
- ToscaConceptIdentifier.class);
- } catch (CoderException e) {
- throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
+ if (toscaServiceTemplate != null) {
+ for (var toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
+ if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
+ toscaServiceTemplate)) {
+ var clParticipantType =
+ ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
+ prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
+ toscaInputEntry.getValue(), participantDefinitionUpdates);
}
- prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
- toscaInputEntry.getValue(), participantDefinitionUpdates);
}
}
participantDefinition.setControlLoopElementDefinitionList(controlLoopElementDefinitionList);
return participantDefinition;
}
-
- private static boolean checkIfNodeTemplateIsControlLoopElement(ToscaNodeTemplate nodeTemplate,
- ToscaServiceTemplate toscaServiceTemplate) {
- if (nodeTemplate.getType().contains(CONTROL_LOOP_ELEMENT)) {
- return true;
- } else {
- var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType());
- if (nodeType != null) {
- var derivedFrom = nodeType.getDerivedFrom();
- if (derivedFrom != null) {
- return derivedFrom.contains(CONTROL_LOOP_ELEMENT) ? true : false;
- }
- }
- }
- return false;
- }
}
participantClUpdateIntervalSec: 1000
participantClStateChangeIntervalSec: 1000
participantParameters:
- heartBeatMs: 120000
- maxMessageAgeMs: 600000
+ heartBeatMs: 20000
+ maxStatusWaitMs: 100000
updateParameters:
maxRetryCount: 3
- maxWaitMs: 100000
+ maxWaitMs: 20000
databaseProviderParameters:
name: PolicyProviderParameterGroup
implementation: org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl
when(controlLoopProvider.getControlLoops(null, null)).thenReturn(List.of(controlLoop));
var clRuntimeParameterGroup = CommonTestData.geParameterGroup("dbScanParticipant");
- clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().setMaxWaitMs(0);
+ clRuntimeParameterGroup.getParticipantParameters().getUpdateParameters().setMaxWaitMs(-1);
+ clRuntimeParameterGroup.getParticipantParameters().setMaxStatusWaitMs(-1);
var participant = new Participant();
participant.setName("Participant0");
List<Participant> participants = participantProvider.getParticipants(null, null);
assertThat(participants.get(0).getHealthStatus()).isEqualTo(ParticipantHealthStatus.NOT_HEALTHY);
- supervisionScanner.run(true);
supervisionScanner.run(true);
participants = participantProvider.getParticipants(null, null);
assertThat(participants.get(0).getHealthStatus()).isEqualTo(ParticipantHealthStatus.OFF_LINE);
import org.mockito.Mockito;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElementDefinition;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantDefinition;
+import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantUtils;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantProvider;
import org.onap.policy.clamp.controlloop.runtime.util.rest.CommonRestController;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
-import org.onap.policy.common.utils.coder.Coder;
-import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.YamlJsonTranslator;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
private static PolicyModelsProvider modelsProvider;
private static ParticipantProvider participantProvider;
private static final YamlJsonTranslator yamlTranslator = new YamlJsonTranslator();
- private static final String CONTROL_LOOP_ELEMENT = "org.onap.policy.clamp.controlloop.ControlLoopElement";
- private static final Coder CODER = new StandardCoder();
/**
* setup Db Provider Parameters.
// List<ToscaNodeTemplate> listOfTemplates = commissioningProvider.getControlLoopDefinitions(null, null);
commissioningProvider.createControlLoopDefinitions(serviceTemplate);
assertThatCode(() -> participantRegisterListener.onTopicEvent(INFRA, TOPIC, null, participantRegisterMsg))
- .doesNotThrowAnyException();
+ .doesNotThrowAnyException();
}
}
synchronized (lockit) {
ParticipantDeregisterListener participantDeregisterListener =
- new ParticipantDeregisterListener(supervisionHandler);
- assertThatCode(() -> participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null,
- participantDeregisterMsg)).doesNotThrowAnyException();
+ new ParticipantDeregisterListener(supervisionHandler);
+ assertThatCode(
+ () -> participantDeregisterListener.onTopicEvent(INFRA, TOPIC, null, participantDeregisterMsg))
+ .doesNotThrowAnyException();
}
}
ToscaServiceTemplate toscaServiceTemplate = commissioningProvider.getToscaServiceTemplate(null, null);
List<ParticipantDefinition> participantDefinitionUpdates = new ArrayList<>();
- for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry :
- toscaServiceTemplate.getToscaTopologyTemplate().getNodeTemplates().entrySet()) {
- if (checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(), toscaServiceTemplate)) {
- ToscaConceptIdentifier clParticipantType;
- try {
- clParticipantType = CODER.decode(
- toscaInputEntry.getValue().getProperties().get("participantType").toString(),
- ToscaConceptIdentifier.class);
- } catch (CoderException e) {
- throw new RuntimeException("cannot get ParticipantType from toscaNodeTemplate", e);
- }
+ for (Map.Entry<String, ToscaNodeTemplate> toscaInputEntry : toscaServiceTemplate.getToscaTopologyTemplate()
+ .getNodeTemplates().entrySet()) {
+ if (ParticipantUtils.checkIfNodeTemplateIsControlLoopElement(toscaInputEntry.getValue(),
+ toscaServiceTemplate)) {
+ var clParticipantType =
+ ParticipantUtils.findParticipantType(toscaInputEntry.getValue().getProperties());
prepareParticipantDefinitionUpdate(clParticipantType, toscaInputEntry.getKey(),
- toscaInputEntry.getValue(), participantDefinitionUpdates);
+ toscaInputEntry.getValue(), participantDefinitionUpdates);
}
}
participantUpdateMsg.setParticipantDefinitionUpdates(participantDefinitionUpdates);
synchronized (lockit) {
- ParticipantUpdatePublisher participantUpdatePublisher =
- new ParticipantUpdatePublisher(modelsProvider);
+ ParticipantUpdatePublisher participantUpdatePublisher = new ParticipantUpdatePublisher(modelsProvider);
participantUpdatePublisher.active(Collections.singletonList(Mockito.mock(TopicSink.class)));
assertThatCode(() -> participantUpdatePublisher.send(participantUpdateMsg)).doesNotThrowAnyException();
}
}
- private static boolean checkIfNodeTemplateIsControlLoopElement(ToscaNodeTemplate nodeTemplate,
- ToscaServiceTemplate toscaServiceTemplate) {
- if (nodeTemplate.getType().contains(CONTROL_LOOP_ELEMENT)) {
- return true;
- } else {
- var nodeType = toscaServiceTemplate.getNodeTypes().get(nodeTemplate.getType());
- if (nodeType != null) {
- var derivedFrom = nodeType.getDerivedFrom();
- if (derivedFrom != null) {
- return derivedFrom.contains(CONTROL_LOOP_ELEMENT) ? true : false;
- }
- }
- }
- return false;
- }
-
private void prepareParticipantDefinitionUpdate(ToscaConceptIdentifier clParticipantType, String entryKey,
- ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
+ ToscaNodeTemplate entryValue, List<ParticipantDefinition> participantDefinitionUpdates) {
var clDefinition = new ControlLoopElementDefinition();
- clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(
- entryKey, entryValue.getVersion()));
+ clDefinition.setClElementDefinitionId(new ToscaConceptIdentifier(entryKey, entryValue.getVersion()));
clDefinition.setControlLoopElementToscaNodeTemplate(entryValue);
List<ControlLoopElementDefinition> controlLoopElementDefinitionList = new ArrayList<>();
if (participantDefinitionUpdates.isEmpty()) {
- participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType,
- controlLoopElementDefinitionList));
+ participantDefinitionUpdates
+ .add(getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
} else {
boolean participantExists = false;
for (ParticipantDefinition participantDefinitionUpdate : participantDefinitionUpdates) {
}
}
if (!participantExists) {
- participantDefinitionUpdates.add(getParticipantDefinition(clDefinition, clParticipantType,
- controlLoopElementDefinitionList));
+ participantDefinitionUpdates.add(
+ getParticipantDefinition(clDefinition, clParticipantType, controlLoopElementDefinitionList));
}
}
}
private ParticipantDefinition getParticipantDefinition(ControlLoopElementDefinition clDefinition,
- ToscaConceptIdentifier clParticipantType,
- List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
+ ToscaConceptIdentifier clParticipantType,
+ List<ControlLoopElementDefinition> controlLoopElementDefinitionList) {
ParticipantDefinition participantDefinition = new ParticipantDefinition();
participantDefinition.setParticipantType(clParticipantType);
controlLoopElementDefinitionList.add(clDefinition);
synchronized (lockit) {
ParticipantUpdateAckListener participantUpdateAckListener =
- new ParticipantUpdateAckListener(supervisionHandler);
+ new ParticipantUpdateAckListener(supervisionHandler);
assertThatCode(() -> participantUpdateAckListener.onTopicEvent(INFRA, TOPIC, null, participantUpdateAckMsg))
- .doesNotThrowAnyException();
+ .doesNotThrowAnyException();
}
}
runtime.supervisionScannerIntervalSec=1000
runtime.participantClUpdateIntervalSec=1000
runtime.participantClStateChangeIntervalSec=1000
-runtime.participantParameters.heartBeatMs=120000
-runtime.participantParameters.updateParameters.maxRetryCount=1
-runtime.participantParameters.updateParameters.maxWaitMs=30000
-runtime.participantParameters.stateChangeParameters.maxRetryCount=1
-runtime.participantParameters.stateChangeParameters.maxWaitMs=30000
+runtime.participantParameters.heartBeatMs=20000
+runtime.participantParameters.maxStatusWaitMs=100000
+runtime.participantParameters.updateParameters.maxRetryCount=3
+runtime.participantParameters.updateParameters.maxWaitMs=20000
runtime.databaseProviderParameters.name=PolicyProviderParameterGroup
runtime.databaseProviderParameters.implementation=org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl
runtime.databaseProviderParameters.databaseDriver=org.h2.Driver
"participantClStateChangeIntervalSec": 1000,
"participantParameters": {
"heartBeatMs": 120000,
+ "maxStatusWaitMs": 100000,
"updateParameters": {
"maxRetryCount": 1,
"maxWaitMs": 30000
- },
- "stateChangeParameters": {
- "maxRetryCount": 1,
- "maxWaitMs": 30000
}
},
"databaseProviderParameters": {