Commit 7a2ce809 authored by Vasilis Katopodis's avatar Vasilis Katopodis
Browse files

Merge branch 'policy/implement-PolicyAddService-grpc-as-state-machine' into 'develop'

feat(policy): policy rule add/update/delete RPCs

See merge request !3
parents 7d3e613d 7f981c76
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ import "policy.proto";
service ContextPolicyService {
  rpc ListPolicyRuleIds(context.Empty         ) returns (policy.PolicyRuleIdList) {}
  rpc ListPolicyRules  (context.Empty         ) returns (policy.PolicyRuleList  ) {}
  rpc GetPolicyRule    (policy.PolicyRuleId   ) returns (policy.PolicyRuleBasic ) {}
  rpc SetPolicyRule    (policy.PolicyRuleBasic) returns (policy.PolicyRuleId    ) {}
  rpc GetPolicyRule    (policy.PolicyRuleId   ) returns (policy.PolicyRule      ) {}
  rpc SetPolicyRule    (policy.PolicyRule     ) returns (policy.PolicyRuleId    ) {}
  rpc RemovePolicyRule (policy.PolicyRuleId   ) returns (context.Empty          ) {}
}
+11 −2
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ service PolicyService {
  rpc GetPolicyByServiceId (context.ServiceId) returns (PolicyRuleServiceList) {}
}

enum RuleState {
enum PolicyRuleStateEnum {
  POLICY_UNDEFINED = 0;     // Undefined rule state
  POLICY_FAILED = 1;        // Rule failed
  POLICY_INSERTED = 2;      // Rule is just inserted
@@ -49,7 +49,8 @@ message PolicyRuleId {
}

message PolicyRuleState {
  RuleState policyRuleState = 1;
  PolicyRuleStateEnum policyRuleState = 1;
  string policyRuleStateMessage = 2;
}

// Basic policy rule attributes
@@ -83,6 +84,14 @@ message PolicyRuleDevice {
  repeated context.DeviceId deviceList = 2;
}

// Wrapper policy rule object
message PolicyRule {
  oneof policy_rule {
    PolicyRuleService service = 1;
    PolicyRuleDevice device = 2;
  }
}

// A list of policy rule IDs
message PolicyRuleIdList {
  repeated PolicyRuleId policyRuleIdList = 1;
+12 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ package policy;
// Action
message PolicyRuleAction {
  PolicyRuleActionEnum action = 1;
  repeated string parameters = 2;
  repeated PolicyRuleActionConfig action_config = 2;
}

enum PolicyRuleActionEnum {
@@ -27,3 +27,14 @@ enum PolicyRuleActionEnum {
  POLICYRULE_ACTION_ADD_SERVICE_CONFIGRULE = 2;
  POLICYRULE_ACTION_ADD_SERVICE_CONSTRAINT = 3;
}

// Action configuration
message PolicyRuleActionConfig {
  string action_key = 1;
  string action_value = 2;
}

// message PolicyRuleAction {
//   PolicyRuleActionEnum action = 1;
//   repeated string parameters = 2;
// }
 No newline at end of file
+19 −25
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import policy.Policy.PolicyRuleId;
import policy.Policy.PolicyRuleService;
import policy.Policy.PolicyRuleServiceList;
import policy.Policy.PolicyRuleState;
import policy.Policy.RuleState;

@GrpcService
public class PolicyGatewayImpl implements PolicyGateway {
@@ -52,45 +51,40 @@ public class PolicyGatewayImpl implements PolicyGateway {
    }

    @Override
    public Uni<PolicyRuleState> policyAddDevice(PolicyRuleDevice request) {
        final var policyRuleDevice = serializer.deserialize(request);
    public Uni<PolicyRuleState> policyUpdateService(PolicyRuleService request) {
        final var policyRuleService = serializer.deserialize(request);

        return policyService
                .addPolicyDevice(policyRuleDevice)
                .updatePolicyService(policyRuleService)
                .onItem()
                .transform(serializer::serialize);
    }

    @Override
    public Uni<PolicyRuleState> policyUpdateService(PolicyRuleService request) {
        return Uni.createFrom()
                .item(
                        () ->
                                Policy.PolicyRuleState.newBuilder()
                                        .setPolicyRuleState(
                                                request.getPolicyRuleBasic().getPolicyRuleState().getPolicyRuleState())
                                        .build());
    public Uni<PolicyRuleState> policyAddDevice(PolicyRuleDevice request) {
        final var policyRuleDevice = serializer.deserialize(request);

        return policyService
                .addPolicyDevice(policyRuleDevice)
                .onItem()
                .transform(serializer::serialize);
    }

    @Override
    public Uni<PolicyRuleState> policyUpdateDevice(PolicyRuleDevice request) {
        return Uni.createFrom()
                .item(
                        () ->
                                Policy.PolicyRuleState.newBuilder()
                                        .setPolicyRuleState(
                                                request.getPolicyRuleBasic().getPolicyRuleState().getPolicyRuleState())
                                        .build());
        final var policyRuleDevice = serializer.deserialize(request);

        return policyService
                .updatePolicyDevice(policyRuleDevice)
                .onItem()
                .transform(serializer::serialize);
    }

    @Override
    public Uni<PolicyRuleState> policyDelete(PolicyRuleId request) {
        return Uni.createFrom()
                .item(
                        () ->
                                Policy.PolicyRuleState.newBuilder()
                                        .setPolicyRuleState(RuleState.POLICY_REMOVED)
                                        .build());
        final var policyRuleId = serializer.deserialize(request);

        return policyService.deletePolicy(policyRuleId).onItem().transform(serializer::serialize);
    }

    @Override
+48 −0
Original line number Diff line number Diff line
@@ -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;
@@ -70,6 +73,21 @@ public class PolicyRuleConditionValidator {
        return isServiceIdValid;
    }

    public Uni<Boolean> validateUpdatedPolicyRuleId(String updatedPolicyRuleId) {
        final var isUpdatedPolicyRuleIdValid = isUpdatedPolicyRuleIdValid(updatedPolicyRuleId);

        isUpdatedPolicyRuleIdValid
                .subscribe()
                .with(
                        serviceIdBooleanValue -> {
                            if (Boolean.FALSE.equals(serviceIdBooleanValue)) {
                                LOGGER.errorf(INVALID_MESSAGE, updatedPolicyRuleId);
                            }
                        });

        return isUpdatedPolicyRuleIdValid;
    }

    private Uni<Boolean> isDeviceIdValid(String deviceId) {
        return contextService
                .getDevice(deviceId)
@@ -98,4 +116,34 @@ public class PolicyRuleConditionValidator {
        return serviceServiceIdContextId.equals(serviceId.getContextId())
                && 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)
                .onItem()
                .ifNotNull()
                .transform(
                        id -> {
                            return true;
                        })
                .onItem()
                .ifNull()
                .continueWith(false);
    }
}
Loading