diff --git a/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java b/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java index b2da70d89408a97b558669accd942adfaf3e1406..6c57e6a1be45a1a8e3734f503d04dc4da1d17731 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java +++ b/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java @@ -65,6 +65,11 @@ public class PolicyServiceImpl implements PolicyService { private static final Logger LOGGER = Logger.getLogger(PolicyServiceImpl.class); private static final String INVALID_MESSAGE = "%s is invalid."; private static final String VALID_MESSAGE = "%s is valid."; + private static final int POLICY_EVALUATION_TIMEOUT = 5; + private static final int ACCEPTABLE_NUMBER_OF_ALARMS = 3; + private static final int NUMBER_OF_CONSTRAINT_CUSTOM_FIELDS = 2; + private static final int NUMBER_OF_CONFIGRULE_CUSTOM_FIELDS = 3; + private static final PolicyRuleState INSERTED_POLICYRULE_STATE = new PolicyRuleState( PolicyRuleStateEnum.POLICY_INSERTED, "Successfully set to INSERTED STATE"); @@ -243,6 +248,15 @@ public class PolicyServiceImpl implements PolicyService { policyRuleBasic.setPolicyRuleState(PROVISIONED_POLICYRULE_STATE); contextService.setPolicyRule(policyRuleBasic); + getAlarmResponseStream(policyRuleBasic, alarmDescriptorList, alarmSubscriptionList, isService); + } + + private void getAlarmResponseStream( + PolicyRuleBasic policyRuleBasic, + List<AlarmDescriptor> alarmDescriptorList, + List<AlarmSubscription> alarmSubscriptionList, + Boolean isService) { + List<Multi<AlarmResponse>> alarmResponseStreamList = new ArrayList<>(); for (AlarmSubscription alarmSubscription : alarmSubscriptionList) { alarmResponseStreamList.add(monitoringService.getAlarmResponseStream(alarmSubscription)); @@ -263,8 +277,14 @@ public class PolicyServiceImpl implements PolicyService { } }); - Long count = multi.collect().with(Collectors.counting()).await().atMost(Duration.ofMinutes(5)); - if (count > 3) { + Long count = + multi + .collect() + .with(Collectors.counting()) + .await() + .atMost(Duration.ofMinutes(POLICY_EVALUATION_TIMEOUT)); + + if (count > ACCEPTABLE_NUMBER_OF_ALARMS) { for (AlarmDescriptor alarmDescriptor : alarmDescriptorList) { monitoringService .deleteAlarm(alarmDescriptor.getAlarmId()) @@ -368,7 +388,8 @@ public class PolicyServiceImpl implements PolicyService { List<ConfigRule> newConfigRules = new ArrayList<>(); // ConfigeRuleTypeCustom has 3 elements - for (int i = 0; i < actionParameters.size() - 3; i += 3) { + int fieldsNumber = NUMBER_OF_CONFIGRULE_CUSTOM_FIELDS; + for (int i = 0; i < actionParameters.size() - fieldsNumber; i += fieldsNumber) { ConfigActionEnum configActionEnum; if (actionParameters.get(i) == "SET") { configActionEnum = ConfigActionEnum.SET; @@ -405,7 +426,8 @@ public class PolicyServiceImpl implements PolicyService { List<String> actionParameters = policyRuleAction.getPolicyRuleActionParameters(); List<Constraint> constraintList = new ArrayList<>(); - for (int i = 0; i < actionParameters.size() - 2; i += 2) { + int fieldsNumber = NUMBER_OF_CONSTRAINT_CUSTOM_FIELDS; + for (int i = 0; i < actionParameters.size() - fieldsNumber; i += fieldsNumber) { var constraintCustom = new ConstraintCustom(actionParameters.get(i), actionParameters.get(i + 1)); var constraintTypeCustom = new ConstraintTypeCustom(constraintCustom); @@ -451,26 +473,35 @@ public class PolicyServiceImpl implements PolicyService { private void validateDevice(PolicyRuleDevice policyRuleDevice) { final var deviceIds = policyRuleDevice.getDeviceIds(); final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic(); - final var policyRuleId = policyRuleBasic.getPolicyRuleId(); final var invalidDeviceIds = returnInvalidDeviceIds(deviceIds); if (!invalidDeviceIds.isEmpty()) { var policyRuleState = createFailedPolicyRuleState( - "The Devices of PolicyRuleDevice " + policyRuleId + " are not valid"); + "The Devices of PolicyRuleDevice " + + policyRuleBasic.getPolicyRuleId() + + " are not valid"); LOGGER.infof("Setting Policy Rule state to [%s]", policyRuleState.toString()); policyRuleBasic.setPolicyRuleState(policyRuleState); contextService.setPolicyRule(policyRuleBasic); return; } + createAlarmDescriptorsForDevices(policyRuleDevice); + } + + private void createAlarmDescriptorsForDevices(PolicyRuleDevice policyRuleDevice) { + final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic(); + List<AlarmDescriptor> alarmDescriptorList = parsePolicyRuleCondition(policyRuleDevice.getPolicyRuleBasic()); if (alarmDescriptorList.isEmpty()) { var policyRuleState = createFailedPolicyRuleState( - "The PolicyRuleConditions of PolicyRuleDevice " + policyRuleId + " are not valid"); + "The PolicyRuleConditions of PolicyRuleDevice " + + policyRuleBasic.getPolicyRuleId() + + " are not valid"); LOGGER.infof("Setting Policy Rule state to [%s]", policyRuleState.toString()); policyRuleBasic.setPolicyRuleState(policyRuleState); contextService.setPolicyRule(policyRuleBasic); @@ -481,6 +512,10 @@ public class PolicyServiceImpl implements PolicyService { policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE); contextService.setPolicyRule(policyRuleBasic); + for (AlarmDescriptor alarmDescriptor : alarmDescriptorList) { + alarmPolicyRuleDeviceMap.put(alarmDescriptor.getAlarmId(), policyRuleDevice); + } + provisionAlarm(policyRuleBasic, alarmDescriptorList, false); return; } @@ -576,11 +611,11 @@ public class PolicyServiceImpl implements PolicyService { policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE); contextService.setPolicyRule(policyRuleBasic); - - createAlarmDescriptors(policyRuleService); + + createAlarmDescriptorsForService(policyRuleService); } - private void createAlarmDescriptors(PolicyRuleService policyRuleService) { + private void createAlarmDescriptorsForService(PolicyRuleService policyRuleService) { final var policyRuleBasic = policyRuleService.getPolicyRuleBasic(); List<AlarmDescriptor> alarmDescriptorList = @@ -653,37 +688,6 @@ public class PolicyServiceImpl implements PolicyService { return alarmDescriptorList; } - private KpiValueRange convertPolicyRuleConditionToAlarmDescriptor( - PolicyRuleCondition policyRuleCondition) { - - switch (policyRuleCondition.getNumericalOperator()) { - case POLICY_RULE_CONDITION_NUMERICAL_EQUAL: - return new KpiValueRange( - policyRuleCondition.getKpiValue(), policyRuleCondition.getKpiValue(), true, true, true); - case POLICY_RULE_CONDITION_NUMERICAL_NOT_EQUAL: - return new KpiValueRange( - policyRuleCondition.getKpiValue(), - policyRuleCondition.getKpiValue(), - true, - false, - false); - - case POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN: - return new KpiValueRange(policyRuleCondition.getKpiValue(), null, false, false, false); - - case POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN_EQUAL: - return new KpiValueRange(policyRuleCondition.getKpiValue(), null, false, true, false); - - case POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN: - return new KpiValueRange(null, policyRuleCondition.getKpiValue(), false, false, false); - - case POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN_EQUAL: - return new KpiValueRange(null, policyRuleCondition.getKpiValue(), false, false, true); - default: - return null; - } - } - private AlarmDescriptor parsePolicyRuleConditionAnd(PolicyRuleBasic policyRuleBasic) { List<PolicyRuleCondition> policyRuleConditionList = policyRuleBasic.getPolicyRuleConditions(); @@ -695,23 +699,35 @@ public class PolicyServiceImpl implements PolicyService { Set<String> kpisSet = new HashSet<String>(kpisList); if (kpisSet.size() == kpisList.size()) { - List<String> kpiIdList = new ArrayList<>(); - List<KpiValueRange> kpiValueRangeList = new ArrayList<>(); - for (PolicyRuleCondition policyRuleCondition : policyRuleConditionList) { + return createAlarmDescriptorWithoutRange(policyRuleConditionList, kpisList); + } - kpisList.add(policyRuleCondition.getKpiId()); - kpiValueRangeList.add(convertPolicyRuleConditionToAlarmDescriptor(policyRuleCondition)); - } + return createAlarmDescriptorWithRange(policyRuleConditionList, kpisList); + } + + private AlarmDescriptor createAlarmDescriptorWithoutRange( + List<PolicyRuleCondition> policyRuleConditionList, List<String> kpisList) { - return new AlarmDescriptor( - "alarmId-" + gen(), - "alarmDescription", - "alarmName-" + gen(), - kpiIdList, - kpiValueRangeList, - getTimeStamp()); + List<String> kpiIdList = new ArrayList<>(); + List<KpiValueRange> kpiValueRangeList = new ArrayList<>(); + + for (PolicyRuleCondition policyRuleCondition : policyRuleConditionList) { + kpisList.add(policyRuleCondition.getKpiId()); + kpiValueRangeList.add(convertPolicyRuleConditionToAlarmDescriptor(policyRuleCondition)); } + return new AlarmDescriptor( + "alarmId-" + gen(), + "alarmDescription", + "alarmName-" + gen(), + kpiIdList, + kpiValueRangeList, + getTimeStamp()); + } + + private AlarmDescriptor createAlarmDescriptorWithRange( + List<PolicyRuleCondition> policyRuleConditionList, List<String> kpisList) { + HashMap<String, KpiValueRange> KpiValueRangeMap = new HashMap<>(); for (PolicyRuleCondition policyRuleCondition : policyRuleConditionList) { @@ -719,66 +735,57 @@ public class PolicyServiceImpl implements PolicyService { var kpiValueRange = KpiValueRangeMap.get(policyRuleCondition.getKpiId()); if (kpiValueRange.getInRange() == true) { - LOGGER.errorf("Received %s", policyRuleBasic); + // LOGGER.errorf("Received %s", policyRuleBasic); return null; } if ((kpiValueRange.getKpiMaxValue() != null) && (kpiValueRange.getKpiMinValue() != null)) { - LOGGER.errorf("Received %s", policyRuleBasic); + // LOGGER.errorf("Received %s", policyRuleBasic); return null; } + var kpiMinValue = kpiValueRange.getKpiMinValue(); + var kpiMaxValue = kpiValueRange.getKpiMaxValue(); + boolean inRange = false; + boolean includeMinValue = kpiValueRange.getIncludeMinValue(); + boolean includeMaxValue = kpiValueRange.getIncludeMaxValue(); + if (policyRuleCondition.getNumericalOperator() == NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN && kpiValueRange.getKpiMinValue() == null) { - KpiValueRangeMap.put( - policyRuleCondition.getKpiId(), - new KpiValueRange( - policyRuleCondition.getKpiValue(), - kpiValueRange.getKpiMaxValue(), - true, - false, - kpiValueRange.getIncludeMaxValue())); - } - if (policyRuleCondition.getNumericalOperator() + kpiMinValue = policyRuleCondition.getKpiValue(); + inRange = true; + includeMinValue = false; + + } else if (policyRuleCondition.getNumericalOperator() == NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN_EQUAL && kpiValueRange.getKpiMinValue() == null) { - KpiValueRangeMap.put( - policyRuleCondition.getKpiId(), - new KpiValueRange( - policyRuleCondition.getKpiValue(), - kpiValueRange.getKpiMaxValue(), - true, - true, - kpiValueRange.getIncludeMaxValue())); - } - if (policyRuleCondition.getNumericalOperator() + kpiMinValue = policyRuleCondition.getKpiValue(); + inRange = true; + includeMinValue = true; + } else if (policyRuleCondition.getNumericalOperator() == NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN && kpiValueRange.getKpiMaxValue() == null) { - KpiValueRangeMap.put( - policyRuleCondition.getKpiId(), - new KpiValueRange( - kpiValueRange.getKpiMinValue(), - policyRuleCondition.getKpiValue(), - true, - kpiValueRange.getIncludeMinValue(), - false)); - } - if (policyRuleCondition.getNumericalOperator() + kpiMaxValue = policyRuleCondition.getKpiValue(); + inRange = true; + includeMaxValue = false; + } else if (policyRuleCondition.getNumericalOperator() == NumericalOperator.POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN_EQUAL && kpiValueRange.getKpiMaxValue() == null) { - KpiValueRangeMap.put( - policyRuleCondition.getKpiId(), - new KpiValueRange( - kpiValueRange.getKpiMinValue(), - policyRuleCondition.getKpiValue(), - true, - kpiValueRange.getIncludeMinValue(), - true)); + + kpiMaxValue = policyRuleCondition.getKpiValue(); + inRange = true; + includeMaxValue = true; + } else { + return null; } + + KpiValueRangeMap.put( + policyRuleCondition.getKpiId(), + new KpiValueRange(kpiMinValue, kpiMaxValue, inRange, includeMinValue, includeMaxValue)); } } @@ -795,6 +802,37 @@ public class PolicyServiceImpl implements PolicyService { getTimeStamp()); } + private KpiValueRange convertPolicyRuleConditionToAlarmDescriptor( + PolicyRuleCondition policyRuleCondition) { + + switch (policyRuleCondition.getNumericalOperator()) { + case POLICY_RULE_CONDITION_NUMERICAL_EQUAL: + return new KpiValueRange( + policyRuleCondition.getKpiValue(), policyRuleCondition.getKpiValue(), true, true, true); + case POLICY_RULE_CONDITION_NUMERICAL_NOT_EQUAL: + return new KpiValueRange( + policyRuleCondition.getKpiValue(), + policyRuleCondition.getKpiValue(), + true, + false, + false); + + case POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN: + return new KpiValueRange(policyRuleCondition.getKpiValue(), null, false, false, false); + + case POLICY_RULE_CONDITION_NUMERICAL_GREATER_THAN_EQUAL: + return new KpiValueRange(policyRuleCondition.getKpiValue(), null, false, true, false); + + case POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN: + return new KpiValueRange(null, policyRuleCondition.getKpiValue(), false, false, false); + + case POLICY_RULE_CONDITION_NUMERICAL_LESS_THAN_EQUAL: + return new KpiValueRange(null, policyRuleCondition.getKpiValue(), false, false, true); + default: + return null; + } + } + private List<String> returnInvalidDeviceIds(List<String> deviceIds) { var invalidDeviceIds = new ArrayList<String>();