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

Refactor validateService method

parent b449507d
No related branches found
No related tags found
1 merge request!3feat(policy): policy rule add/update/delete RPCs
......@@ -18,9 +18,12 @@ package eu.teraflow.policy;
import eu.teraflow.policy.context.ContextService;
import eu.teraflow.policy.context.model.Device;
import eu.teraflow.policy.context.model.EndPointId;
import eu.teraflow.policy.context.model.Service;
import eu.teraflow.policy.context.model.ServiceId;
import io.smallrye.mutiny.Uni;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.jboss.logging.Logger;
......@@ -114,6 +117,22 @@ public class PolicyRuleConditionValidator {
&& serviceServiceIdId.equals(serviceId.getId());
}
public Uni<Boolean> isServicesDeviceIdsValid(ServiceId serviceId, List<String> deviceIds) {
return contextService
.getService(serviceId)
.onItem()
.transform(service -> checkIfServicesDeviceIdsExist(service, deviceIds));
}
private boolean checkIfServicesDeviceIdsExist(Service service, List<String> deviceIds) {
List<String> serviceDeviceIds = new ArrayList<>();
for (EndPointId serviceEndPointId : service.getServiceEndPointIds()) {
serviceDeviceIds.add(serviceEndPointId.getDeviceId());
}
return deviceIds.containsAll(serviceDeviceIds);
}
private Uni<Boolean> isUpdatedPolicyRuleIdValid(String updatedPolicyRuleId) {
return contextService
.getPolicyRule(updatedPolicyRuleId)
......
......@@ -541,57 +541,48 @@ public class PolicyServiceImpl implements PolicyService {
final var deviceIds = policyRuleService.getDeviceIds();
final var policyRuleBasic = policyRuleService.getPolicyRuleBasic();
final var isServiceIdValid = policyRuleConditionValidator.validateServiceId(serviceId);
isServiceIdValid
.subscribe()
.with(
serviceIdBooleanValue -> {
if (Boolean.FALSE.equals(serviceIdBooleanValue)) {
LOGGER.errorf(INVALID_MESSAGE, serviceId);
final var invalidDeviceIds = returnInvalidDeviceIds(deviceIds);
if (invalidDeviceIds.isEmpty()) {
LOGGER.info("All Device Ids are valid.");
}
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);
return;
} else {
LOGGER.infof(VALID_MESSAGE, serviceId);
final var invalidDeviceIds = returnInvalidDeviceIds(deviceIds);
if (!invalidDeviceIds.isEmpty()) {
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);
return;
} else {
LOGGER.infof("All deviceIds are valid");
}
Boolean isServiceIdValid =
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);
return;
}
policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE);
contextService.setPolicyRule(policyRuleBasic);
return;
}
});
Boolean isServicesDeviceIdsValid =
policyRuleConditionValidator
.isServicesDeviceIdsValid(serviceId, deviceIds)
.await()
.indefinitely();
if (policyRuleBasic.getPolicyRuleState() != VALIDATED_POLICYRULE_STATE) {
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);
return;
}
policyRuleBasic.setPolicyRuleState(VALIDATED_POLICYRULE_STATE);
contextService.setPolicyRule(policyRuleBasic);
createAlarmDescriptors(policyRuleService);
}
private void createAlarmDescriptors(PolicyRuleService policyRuleService) {
final var policyRuleBasic = policyRuleService.getPolicyRuleBasic();
List<AlarmDescriptor> alarmDescriptorList =
parsePolicyRuleCondition(policyRuleService.getPolicyRuleBasic());
......
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