Skip to content
Snippets Groups Projects
Commit 83d036d8 authored by Vasilis Katopodis's avatar Vasilis Katopodis
Browse files

Handle exception in PolicyRuleBasic constructor

parent 9dd92558
No related branches found
No related tags found
1 merge request!3feat(policy): policy rule add/update/delete RPCs
...@@ -145,8 +145,15 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -145,8 +145,15 @@ public class PolicyServiceImpl implements PolicyService {
LOGGER.infof("Received %s", policyRuleService); LOGGER.infof("Received %s", policyRuleService);
final var policyRuleBasic = policyRuleService.getPolicyRuleBasic(); 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 contextService
.setPolicyRule(policyRuleBasic) .setPolicyRule(policyRuleBasic)
.subscribe() .subscribe()
...@@ -160,8 +167,15 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -160,8 +167,15 @@ public class PolicyServiceImpl implements PolicyService {
LOGGER.infof("Received %s", policyRuleService); LOGGER.infof("Received %s", policyRuleService);
final var policyRuleBasic = policyRuleService.getPolicyRuleBasic(); 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 contextService
.setPolicyRule(policyRuleBasic) .setPolicyRule(policyRuleBasic)
.subscribe() .subscribe()
...@@ -175,8 +189,15 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -175,8 +189,15 @@ public class PolicyServiceImpl implements PolicyService {
LOGGER.infof("Received %s", policyRuleDevice); LOGGER.infof("Received %s", policyRuleDevice);
final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic(); 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 contextService
.setPolicyRule(policyRuleBasic) .setPolicyRule(policyRuleBasic)
.subscribe() .subscribe()
...@@ -189,8 +210,15 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -189,8 +210,15 @@ public class PolicyServiceImpl implements PolicyService {
LOGGER.infof("Received %s", policyRuleDevice); LOGGER.infof("Received %s", policyRuleDevice);
final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic(); 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 contextService
.setPolicyRule(policyRuleBasic) .setPolicyRule(policyRuleBasic)
.subscribe() .subscribe()
...@@ -203,7 +231,6 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -203,7 +231,6 @@ public class PolicyServiceImpl implements PolicyService {
public Uni<PolicyRuleState> deletePolicy(String policyRuleId) { public Uni<PolicyRuleState> deletePolicy(String policyRuleId) {
LOGGER.infof("Received %s", policyRuleId); LOGGER.infof("Received %s", policyRuleId);
// TODO: Specify timeout in case of connectivity issue
PolicyRuleBasic policyRuleBasic = PolicyRuleBasic policyRuleBasic =
contextService.getPolicyRule(policyRuleId).await().indefinitely(); contextService.getPolicyRule(policyRuleId).await().indefinitely();
List<PolicyRuleCondition> policyRuleConditions = policyRuleBasic.getPolicyRuleConditions(); List<PolicyRuleCondition> policyRuleConditions = policyRuleBasic.getPolicyRuleConditions();
...@@ -220,8 +247,7 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -220,8 +247,7 @@ public class PolicyServiceImpl implements PolicyService {
.subscribe() .subscribe()
.with(emptyMessage -> LOGGER.infof("Policy [%s] has been removed.\n", policyRuleId)); .with(emptyMessage -> LOGGER.infof("Policy [%s] has been removed.\n", policyRuleId));
policyRuleBasic.setPolicyRuleState(REMOVED_POLICYRULE_STATE); setState(policyRuleBasic, REMOVED_POLICYRULE_STATE);
contextService.setPolicyRule(policyRuleBasic);
return Uni.createFrom().item(policyRuleBasic.getPolicyRuleState()); return Uni.createFrom().item(policyRuleBasic.getPolicyRuleState());
} }
...@@ -459,7 +485,8 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -459,7 +485,8 @@ public class PolicyServiceImpl implements PolicyService {
if (!invalidDeviceIds.isEmpty()) { if (!invalidDeviceIds.isEmpty()) {
String message = String message =
String.format( 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)); setState(policyRuleBasic, new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message));
return; return;
} }
...@@ -467,12 +494,6 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -467,12 +494,6 @@ public class PolicyServiceImpl implements PolicyService {
createAlarmDescriptorsForDevices(policyRuleDevice); 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) { private void createAlarmDescriptorsForDevices(PolicyRuleDevice policyRuleDevice) {
final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic(); final var policyRuleBasic = policyRuleDevice.getPolicyRuleBasic();
...@@ -581,8 +602,7 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -581,8 +602,7 @@ public class PolicyServiceImpl implements PolicyService {
return; return;
} }
policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE); setState(policyRuleBasic, VALIDATED_POLICYRULE_STATE);
contextService.setPolicyRule(policyRuleBasic);
createAlarmDescriptorsForService(policyRuleService); createAlarmDescriptorsForService(policyRuleService);
} }
...@@ -825,4 +845,10 @@ public class PolicyServiceImpl implements PolicyService { ...@@ -825,4 +845,10 @@ public class PolicyServiceImpl implements PolicyService {
return invalidDeviceIds; 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);
}
} }
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package eu.teraflow.policy.model; package eu.teraflow.policy.model;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import eu.teraflow.policy.common.Util; import eu.teraflow.policy.common.Util;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -32,6 +31,7 @@ public class PolicyRuleBasic { ...@@ -32,6 +31,7 @@ public class PolicyRuleBasic {
private final BooleanOperator booleanOperator; private final BooleanOperator booleanOperator;
private final List<PolicyRuleAction> policyRuleActions; private final List<PolicyRuleAction> policyRuleActions;
private final Boolean isValid; private final Boolean isValid;
private final String exceptionMessage;
public PolicyRuleBasic( public PolicyRuleBasic(
String policyRuleId, String policyRuleId,
...@@ -47,22 +47,23 @@ public class PolicyRuleBasic { ...@@ -47,22 +47,23 @@ public class PolicyRuleBasic {
BooleanOperator booleanOperator_val; BooleanOperator booleanOperator_val;
List<PolicyRuleAction> policyRuleActions_val; List<PolicyRuleAction> policyRuleActions_val;
Boolean isValid_val; Boolean isValid_val;
String exceptionMessage_val = "";
try { 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."); checkArgument(!policyRuleId.isBlank(), "Policy rule ID must not be empty.");
policyRuleId_val = policyRuleId; policyRuleId_val = policyRuleId;
this.policyRuleState = policyRuleState; this.policyRuleState = policyRuleState;
checkArgument(priority >= 0, "Priority value must be greater or equal than zero."); checkArgument(priority >= 0, "Priority value must be greater or equal than zero.");
priority_val = priority; 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."); checkArgument(!policyRuleConditions.isEmpty(), "Policy Rule conditions cannot be empty.");
policyRuleConditions_val = policyRuleConditions; policyRuleConditions_val = policyRuleConditions;
checkArgument( checkArgument(
booleanOperator != BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED, booleanOperator != BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED,
"Boolean operator cannot be undefined"); "Boolean operator cannot be undefined");
booleanOperator_val = booleanOperator; 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."); checkArgument(!policyRuleActions.isEmpty(), "Policy Rule actions cannot be empty.");
policyRuleActions_val = policyRuleActions; policyRuleActions_val = policyRuleActions;
isValid_val = true; isValid_val = true;
...@@ -74,6 +75,7 @@ public class PolicyRuleBasic { ...@@ -74,6 +75,7 @@ public class PolicyRuleBasic {
booleanOperator_val = BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED; booleanOperator_val = BooleanOperator.POLICYRULE_CONDITION_BOOLEAN_UNDEFINED;
policyRuleActions_val = new ArrayList<PolicyRuleAction>(); policyRuleActions_val = new ArrayList<PolicyRuleAction>();
isValid_val = false; isValid_val = false;
exceptionMessage_val = e.toString();
} }
this.isValid = isValid_val; this.isValid = isValid_val;
...@@ -82,12 +84,17 @@ public class PolicyRuleBasic { ...@@ -82,12 +84,17 @@ public class PolicyRuleBasic {
this.policyRuleConditions = policyRuleConditions_val; this.policyRuleConditions = policyRuleConditions_val;
this.booleanOperator = booleanOperator_val; this.booleanOperator = booleanOperator_val;
this.policyRuleActions = policyRuleActions_val; this.policyRuleActions = policyRuleActions_val;
this.exceptionMessage = exceptionMessage_val;
} }
public boolean isValid() { public boolean areArgumentsValid() {
return isValid; return isValid;
} }
public String getExeceptionMessage() {
return exceptionMessage;
}
public String getPolicyRuleId() { public String getPolicyRuleId() {
return policyRuleId; return policyRuleId;
} }
......
...@@ -183,7 +183,7 @@ class PolicyServiceTest { ...@@ -183,7 +183,7 @@ class PolicyServiceTest {
final var expectedPolicyRuleState = final var expectedPolicyRuleState =
Policy.PolicyRuleState.newBuilder() Policy.PolicyRuleState.newBuilder()
.setPolicyRuleState(PolicyRuleStateEnum.POLICY_UPDATED) .setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED)
.build(); .build();
final var policyRuleBasic = final var policyRuleBasic =
...@@ -213,7 +213,7 @@ class PolicyServiceTest { ...@@ -213,7 +213,7 @@ class PolicyServiceTest {
final var expectedPolicyRuleState = final var expectedPolicyRuleState =
Policy.PolicyRuleState.newBuilder() Policy.PolicyRuleState.newBuilder()
.setPolicyRuleState(PolicyRuleStateEnum.POLICY_UPDATED) .setPolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED)
.build(); .build();
final var policyRuleBasic = final var policyRuleBasic =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment