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 6c57e6a1be45a1a8e3734f503d04dc4da1d17731..a40fc88db5a7dd30a6da38c7704eb813e885ce91 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java +++ b/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java @@ -130,10 +130,6 @@ public class PolicyServiceImpl implements PolicyService { this.policyRuleConditionFieldsGetter = policyRuleConditionFieldsGetter; } - private static PolicyRuleState createFailedPolicyRuleState(String message) { - return new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message); - } - private static String gen() { Random r = new Random(System.currentTimeMillis()); return String.valueOf((1 + r.nextInt(2)) * 10000 + r.nextInt(10000)); @@ -244,10 +240,7 @@ public class PolicyServiceImpl implements PolicyService { .with(alarmId -> alarmSubscriptionList.add(new AlarmSubscription(alarmId, 0, 0))); } - LOGGER.infof("Setting Policy Rule state to [%s]", PROVISIONED_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(PROVISIONED_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); - + setState(policyRuleBasic, PROVISIONED_POLICYRULE_STATE); getAlarmResponseStream(policyRuleBasic, alarmDescriptorList, alarmSubscriptionList, isService); } @@ -294,13 +287,11 @@ public class PolicyServiceImpl implements PolicyService { LOGGER.infof("Alarm [%s] has been deleted.\n", alarmDescriptor.getAlarmId())); } - LOGGER.infof("Setting Policy Rule state to [%s]", INEFFECTIVE_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(INEFFECTIVE_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + setState(policyRuleBasic, INEFFECTIVE_POLICYRULE_STATE); + } else { - LOGGER.infof("Setting Policy Rule state to [%s]", EFFECTIVE_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(EFFECTIVE_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + + setState(policyRuleBasic, EFFECTIVE_POLICYRULE_STATE); multi .subscribe() @@ -325,9 +316,7 @@ public class PolicyServiceImpl implements PolicyService { // In case additional PolicyRuleAction for Devices will be added } - LOGGER.infof("Setting Policy Rule state to [%s]", ACTIVE_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(ACTIVE_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + setState(policyRuleBasic, ACTIVE_POLICYRULE_STATE); List<String> deviceIds = policyRuleDevice.getDeviceIds(); List<String> actionParameters = @@ -345,9 +334,7 @@ public class PolicyServiceImpl implements PolicyService { activateDevice(deviceIds.get(i), deviceActions); } - LOGGER.infof("Setting Policy Rule state to [%s]", ENFORCED_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(ENFORCED_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + setState(policyRuleBasic, ENFORCED_POLICYRULE_STATE); } private void activateDevice(String deviceId, List<String> actionParameters) { @@ -442,11 +429,7 @@ public class PolicyServiceImpl implements PolicyService { deserializedService -> { deserializedService.appendServiceConstraints(constraintList); serviceService.updateService(deserializedService); - - LOGGER.infof( - "Setting Policy Rule state to [%s]", ENFORCED_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(ENFORCED_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + setState(policyRuleBasic, ENFORCED_POLICYRULE_STATE); }); } @@ -455,9 +438,7 @@ public class PolicyServiceImpl implements PolicyService { PolicyRuleBasic policyRuleBasic = policyRuleService.getPolicyRuleBasic(); PolicyRuleAction policyRuleAction = policyRuleActionMap.get(alarmId); - LOGGER.infof("Setting Policy Rule state to [%s]", ACTIVE_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(ACTIVE_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + setState(policyRuleBasic, ACTIVE_POLICYRULE_STATE); switch (policyRuleAction.getPolicyRuleActionEnum()) { case POLICY_RULE_ACTION_ADD_SERVICE_CONSTRAINT: @@ -476,20 +457,22 @@ public class PolicyServiceImpl implements PolicyService { final var invalidDeviceIds = returnInvalidDeviceIds(deviceIds); if (!invalidDeviceIds.isEmpty()) { - var policyRuleState = - createFailedPolicyRuleState( - "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); + String message = + String.format( + "he Devices of PolicyRuleDevice %s are not valid", policyRuleBasic.getPolicyRuleId()); + setState(policyRuleBasic, new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message)); return; } createAlarmDescriptorsForDevices(policyRuleDevice); } + private void setState(PolicyRuleBasic policyRuleBasic, PolicyRuleState policyRuleState) { + LOGGER.infof("Setting Policy Rule state to [%s]", policyRuleState.toString()); + policyRuleBasic.setPolicyRuleState(policyRuleState); + contextService.setPolicyRule(policyRuleBasic); + } + private void createAlarmDescriptorsForDevices(PolicyRuleDevice policyRuleDevice) { final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic(); @@ -497,21 +480,15 @@ public class PolicyServiceImpl implements PolicyService { parsePolicyRuleCondition(policyRuleDevice.getPolicyRuleBasic()); if (alarmDescriptorList.isEmpty()) { - var policyRuleState = - createFailedPolicyRuleState( - "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); + String message = + String.format( + "The Devices of PolicyRuleDevice %s are not valid", + policyRuleBasic.getPolicyRuleId()); + setState(policyRuleBasic, new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message)); return; } - LOGGER.infof("Setting Policy Rule state to [%s]", VALIDATED_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); - + setState(policyRuleBasic, VALIDATED_POLICYRULE_STATE); for (AlarmDescriptor alarmDescriptor : alarmDescriptorList) { alarmPolicyRuleDeviceMap.put(alarmDescriptor.getAlarmId(), policyRuleDevice); } @@ -531,14 +508,14 @@ public class PolicyServiceImpl implements PolicyService { .with( policyRuleBoolean -> { if (Boolean.FALSE.equals(isUpdatedPolicyRuleValid)) { - var policyRuleState = - createFailedPolicyRuleState( - "The requested PolicyRuleService to update: " - + policyRuleBasic.getPolicyRuleId() - + " is not valid"); - LOGGER.infof("Setting Policy Rule state to [%s]", policyRuleState.toString()); - policyRuleBasic.setPolicyRuleState(policyRuleState); - contextService.setPolicyRule(policyRuleBasic); + + String message = + String.format( + "The requested PolicyRuleService to update: %s is not valid", + policyRuleBasic.getPolicyRuleId()); + setState( + policyRuleBasic, + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message)); return; } @@ -557,14 +534,13 @@ public class PolicyServiceImpl implements PolicyService { .with( policyRuleBoolean -> { if (Boolean.FALSE.equals(isUpdatedPolicyRuleValid)) { - var policyRuleState = - createFailedPolicyRuleState( - "The requested PolicyRuleDevice to update: " - + policyRuleBasic.getPolicyRuleId() - + " is not valid"); - LOGGER.infof("Setting Policy Rule state to [%s]", policyRuleState.toString()); - policyRuleBasic.setPolicyRuleState(policyRuleState); - contextService.setPolicyRule(policyRuleBasic); + String message = + String.format( + "The requested PolicyRuleDevice to update: %s is not valid", + policyRuleBasic.getPolicyRuleId()); + setState( + policyRuleBasic, + new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message)); return; } validateDevice(policyRuleDevice); @@ -580,14 +556,12 @@ public class PolicyServiceImpl implements PolicyService { policyRuleConditionValidator.validateServiceId(serviceId).await().indefinitely(); if (!isServiceIdValid) { LOGGER.errorf(INVALID_MESSAGE, serviceId); - var policyRuleState = - createFailedPolicyRuleState( - "The Service of PolicyRuleService " - + policyRuleBasic.getPolicyRuleId() - + " is not valid"); - LOGGER.infof("Setting Policy Rule state to [%s]", policyRuleState.toString()); - policyRuleBasic.setPolicyRuleState(policyRuleState); - contextService.setPolicyRule(policyRuleBasic); + + String message = + String.format( + "The Service of PolicyRuleService %s is not valid", + policyRuleBasic.getPolicyRuleId()); + setState(policyRuleBasic, new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message)); return; } @@ -598,14 +572,12 @@ public class PolicyServiceImpl implements PolicyService { .indefinitely(); if (!isServicesDeviceIdsValid) { - var policyRuleState = - createFailedPolicyRuleState( - "The Devices of PolicyRuleService " - + policyRuleBasic.getPolicyRuleId() - + " are not valid"); - LOGGER.infof("Setting Policy Rule state to [%s]", policyRuleState.toString()); - policyRuleBasic.setPolicyRuleState(policyRuleState); - contextService.setPolicyRule(policyRuleBasic); + + String message = + String.format( + "he Devices of PolicyRuleService %s are not valid", + policyRuleBasic.getPolicyRuleId()); + setState(policyRuleBasic, new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message)); return; } @@ -622,19 +594,15 @@ public class PolicyServiceImpl implements PolicyService { parsePolicyRuleCondition(policyRuleService.getPolicyRuleBasic()); if (alarmDescriptorList.isEmpty()) { - var policyRuleState = - createFailedPolicyRuleState( - "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); + String message = + String.format( + "The PolicyRuleConditions of PolicyRuleDevice %s are not valid", + policyRuleBasic.getPolicyRuleId()); + setState(policyRuleBasic, new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message)); return; } - LOGGER.infof("Setting Policy Rule state to [%s]", VALIDATED_POLICYRULE_STATE.toString()); - policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + + setState(policyRuleBasic, VALIDATED_POLICYRULE_STATE); for (AlarmDescriptor alarmDescriptor : alarmDescriptorList) { alarmPolicyRuleServiceMap.put(alarmDescriptor.getAlarmId(), policyRuleService);