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 a40fc88db5a7dd30a6da38c7704eb813e885ce91..b9fb351c84c97249a90a311a9483fdacf52e5ef7 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java +++ b/src/policy/src/main/java/eu/teraflow/policy/PolicyServiceImpl.java @@ -145,8 +145,15 @@ public class PolicyServiceImpl implements PolicyService { LOGGER.infof("Received %s", policyRuleService); final var policyRuleBasic = policyRuleService.getPolicyRuleBasic(); - policyRuleBasic.setPolicyRuleState(INSERTED_POLICYRULE_STATE); + if (!policyRuleBasic.areArgumentsValid()) { + setState( + policyRuleBasic, + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, policyRuleBasic.getExeceptionMessage())); + return Uni.createFrom().item(policyRuleBasic.getPolicyRuleState()); + } + policyRuleBasic.setPolicyRuleState(INSERTED_POLICYRULE_STATE); contextService .setPolicyRule(policyRuleBasic) .subscribe() @@ -160,8 +167,15 @@ public class PolicyServiceImpl implements PolicyService { LOGGER.infof("Received %s", policyRuleService); final var policyRuleBasic = policyRuleService.getPolicyRuleBasic(); - policyRuleBasic.setPolicyRuleState(UPDATED_POLICYRULE_STATE); + if (!policyRuleBasic.areArgumentsValid()) { + setState( + policyRuleBasic, + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, policyRuleBasic.getExeceptionMessage())); + return Uni.createFrom().item(policyRuleBasic.getPolicyRuleState()); + } + policyRuleBasic.setPolicyRuleState(UPDATED_POLICYRULE_STATE); contextService .setPolicyRule(policyRuleBasic) .subscribe() @@ -175,8 +189,15 @@ public class PolicyServiceImpl implements PolicyService { LOGGER.infof("Received %s", policyRuleDevice); final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic(); - policyRuleBasic.setPolicyRuleState(INSERTED_POLICYRULE_STATE); + if (!policyRuleBasic.areArgumentsValid()) { + setState( + policyRuleBasic, + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, policyRuleBasic.getExeceptionMessage())); + return Uni.createFrom().item(policyRuleBasic.getPolicyRuleState()); + } + policyRuleBasic.setPolicyRuleState(INSERTED_POLICYRULE_STATE); contextService .setPolicyRule(policyRuleBasic) .subscribe() @@ -189,8 +210,15 @@ public class PolicyServiceImpl implements PolicyService { LOGGER.infof("Received %s", policyRuleDevice); final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic(); - policyRuleBasic.setPolicyRuleState(UPDATED_POLICYRULE_STATE); + if (!policyRuleBasic.areArgumentsValid()) { + setState( + policyRuleBasic, + new PolicyRuleState( + PolicyRuleStateEnum.POLICY_FAILED, policyRuleBasic.getExeceptionMessage())); + return Uni.createFrom().item(policyRuleBasic.getPolicyRuleState()); + } + policyRuleBasic.setPolicyRuleState(UPDATED_POLICYRULE_STATE); contextService .setPolicyRule(policyRuleBasic) .subscribe() @@ -203,7 +231,6 @@ public class PolicyServiceImpl implements PolicyService { public Uni<PolicyRuleState> deletePolicy(String policyRuleId) { LOGGER.infof("Received %s", policyRuleId); - // TODO: Specify timeout in case of connectivity issue PolicyRuleBasic policyRuleBasic = contextService.getPolicyRule(policyRuleId).await().indefinitely(); List<PolicyRuleCondition> policyRuleConditions = policyRuleBasic.getPolicyRuleConditions(); @@ -220,8 +247,7 @@ public class PolicyServiceImpl implements PolicyService { .subscribe() .with(emptyMessage -> LOGGER.infof("Policy [%s] has been removed.\n", policyRuleId)); - policyRuleBasic.setPolicyRuleState(REMOVED_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + setState(policyRuleBasic, REMOVED_POLICYRULE_STATE); return Uni.createFrom().item(policyRuleBasic.getPolicyRuleState()); } @@ -459,7 +485,8 @@ public class PolicyServiceImpl implements PolicyService { if (!invalidDeviceIds.isEmpty()) { String message = String.format( - "he Devices of PolicyRuleDevice %s are not valid", policyRuleBasic.getPolicyRuleId()); + "The Devices of PolicyRuleDevice %s are not valid", + policyRuleBasic.getPolicyRuleId()); setState(policyRuleBasic, new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message)); return; } @@ -467,12 +494,6 @@ public class PolicyServiceImpl implements PolicyService { 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(); @@ -581,8 +602,7 @@ public class PolicyServiceImpl implements PolicyService { return; } - policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE); - contextService.setPolicyRule(policyRuleBasic); + setState(policyRuleBasic, VALIDATED_POLICYRULE_STATE); createAlarmDescriptorsForService(policyRuleService); } @@ -825,4 +845,10 @@ public class PolicyServiceImpl implements PolicyService { return invalidDeviceIds; } + + private void setState(PolicyRuleBasic policyRuleBasic, PolicyRuleState policyRuleState) { + LOGGER.infof("Setting Policy Rule state to [%s]", policyRuleState.toString()); + policyRuleBasic.setPolicyRuleState(policyRuleState); + contextService.setPolicyRule(policyRuleBasic); + } } diff --git a/src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleBasic.java b/src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleBasic.java index 1157bc220f75db05c02c050c00af6c928a29dea0..2fc1c5c26578b444b045ee14fb490d49b3ac76b1 100644 --- a/src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleBasic.java +++ b/src/policy/src/main/java/eu/teraflow/policy/model/PolicyRuleBasic.java @@ -17,7 +17,6 @@ package eu.teraflow.policy.model; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; import eu.teraflow.policy.common.Util; import java.util.ArrayList; @@ -32,6 +31,7 @@ public class PolicyRuleBasic { private final BooleanOperator booleanOperator; private final List<PolicyRuleAction> policyRuleActions; private final Boolean isValid; + private final String exceptionMessage; public PolicyRuleBasic( String policyRuleId, @@ -47,22 +47,23 @@ public class PolicyRuleBasic { BooleanOperator booleanOperator_val; List<PolicyRuleAction> policyRuleActions_val; Boolean isValid_val; + String exceptionMessage_val = ""; try { - checkNotNull(policyRuleId, "Policy rule ID must not be null."); + // checkNotNull(policyRuleId, "Policy rule ID must not be null."); checkArgument(!policyRuleId.isBlank(), "Policy rule ID must not be empty."); policyRuleId_val = policyRuleId; this.policyRuleState = policyRuleState; checkArgument(priority >= 0, "Priority value must be greater or equal than zero."); priority_val = priority; - checkNotNull(policyRuleConditions, "Policy Rule conditions cannot be null."); + // checkNotNull(policyRuleConditions, "Policy Rule conditions cannot be null."); checkArgument(!policyRuleConditions.isEmpty(), "Policy Rule conditions cannot be empty."); policyRuleConditions_val = policyRuleConditions; checkArgument( booleanOperator != BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED, "Boolean operator cannot be undefined"); booleanOperator_val = booleanOperator; - checkNotNull(policyRuleActions, "Policy Rule actions cannot be null."); + // checkNotNull(policyRuleActions, "Policy Rule actions cannot be null."); checkArgument(!policyRuleActions.isEmpty(), "Policy Rule actions cannot be empty."); policyRuleActions_val = policyRuleActions; isValid_val = true; @@ -74,6 +75,7 @@ public class PolicyRuleBasic { booleanOperator_val = BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED; policyRuleActions_val = new ArrayList<PolicyRuleAction>(); isValid_val = false; + exceptionMessage_val = e.toString(); } this.isValid = isValid_val; @@ -82,12 +84,17 @@ public class PolicyRuleBasic { this.policyRuleConditions = policyRuleConditions_val; this.booleanOperator = booleanOperator_val; this.policyRuleActions = policyRuleActions_val; + this.exceptionMessage = exceptionMessage_val; } - public boolean isValid() { + public boolean areArgumentsValid() { return isValid; } + public String getExeceptionMessage() { + return exceptionMessage; + } + public String getPolicyRuleId() { return policyRuleId; } diff --git a/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java b/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java index 1669966aba2fe0392a4fd49cfe71ba0fc809fc52..53b0f47198fa2ab708b2f55f10dc36ab4a7fefd8 100644 --- a/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java +++ b/src/policy/src/test/java/eu/teraflow/policy/PolicyServiceTest.java @@ -183,7 +183,7 @@ class PolicyServiceTest { final var expectedPolicyRuleState = Policy.PolicyRuleState.newBuilder() - .setPolicyRuleState(PolicyRuleStateEnum.POLICY_UPDATED) + .setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED) .build(); final var policyRuleBasic = @@ -213,7 +213,7 @@ class PolicyServiceTest { final var expectedPolicyRuleState = Policy.PolicyRuleState.newBuilder() - .setPolicyRuleState(PolicyRuleStateEnum.POLICY_UPDATED) + .setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED) .build(); final var policyRuleBasic =