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 {
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);
}
}
......@@ -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;
}
......
......@@ -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 =
......
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