Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • tfs/controller
1 result
Show changes
Commits on Source (2)
......@@ -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));
......@@ -149,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()
......@@ -164,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()
......@@ -179,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()
......@@ -193,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()
......@@ -207,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();
......@@ -224,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());
}
......@@ -244,10 +266,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 +313,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 +342,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 +360,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 +455,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 +464,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,14 +483,11 @@ 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(
"The Devices of PolicyRuleDevice %s are not valid",
policyRuleBasic.getPolicyRuleId());
setState(policyRuleBasic, new PolicyRuleState(PolicyRuleStateEnum.POLICY_FAILED, message));
return;
}
......@@ -497,21 +501,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 +529,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 +555,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 +577,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,19 +593,16 @@ 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;
}
policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE);
contextService.setPolicyRule(policyRuleBasic);
setState(policyRuleBasic, VALIDATED_POLICYRULE_STATE);
createAlarmDescriptorsForService(policyRuleService);
}
......@@ -622,19 +614,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);
......@@ -857,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 =
......