Commit 2bfdc93b authored by Fotis Soldatos's avatar Fotis Soldatos
Browse files

refactor(policy): update policy.proto

PolicyAdd rpc replaced by PolicyAddService & PolicyAddDevice
parent ff0c2adf
Loading
Loading
Loading
Loading
+57 −40
Original line number Diff line number Diff line
@@ -20,23 +20,28 @@ import "policy-condition.proto";
import "policy-action.proto";

service PolicyService {
  rpc PolicyAdd (PolicyRule) returns (PolicyRuleState) {}
  rpc PolicyUpdate (PolicyRule) returns (PolicyRuleState) {}
  rpc PolicyDelete (PolicyRule) returns (PolicyRuleState) {}
  rpc GetPolicy (PolicyRuleId) returns (PolicyRule) {}
  rpc GetPolicyByDeviceId (context.DeviceId) returns (PolicyRuleList) {}
  rpc GetPolicyByServiceId (context.ServiceId) returns (PolicyRuleList) {}
  rpc PolicyAddService (PolicyRuleService) returns (PolicyRuleState) {}
  rpc PolicyAddDevice (PolicyRuleDevice) returns (PolicyRuleState) {}
  rpc PolicyUpdateService (PolicyRuleService) returns (PolicyRuleState) {}
  rpc PolicyUpdateDevice (PolicyRuleDevice) returns (PolicyRuleState) {}
  rpc PolicyDelete (PolicyRuleId) returns (PolicyRuleState) {}
  rpc GetPolicyService (PolicyRuleId) returns (PolicyRuleService) {}
  rpc GetPolicyDevice (PolicyRuleId) returns (PolicyRuleDevice) {}
  rpc GetPolicyByServiceId (context.ServiceId) returns (PolicyRuleServiceList) {}
}

enum RuleState {
  POLICY_INACTIVE = 0;    // Rule is currently inactive
  POLICY_PLANNED = 1;     // Rule installation planned
  POLICY_ACTIVE = 2;      // Rule is currently active
}

enum PolicyRuleType {
  POLICYTYPE_DEVICE = 0;  // Device-level
  POLICYTYPE_NETWORK = 1; // Network-wide
  POLICY_UNDEFINED = 0;     // Undefined rule state
  POLICY_FAILED = 1;        // Rule failed
  POLICY_INSERTED = 2;      // Rule is just inserted
  POLICY_VALIDATED = 3;     // Rule content is correct
  POLICY_PROVISIONED = 4;   // Rule subscribed to Monitoring
  POLICY_ACTIVE = 5;        // Rule is currently active (alarm is just thrown by Monitoring)
  POLICY_ENFORCED = 6;      // Rule action is successfully enforced
  POLICY_INEFFECTIVE = 7;   // The applied rule action did not work as expected
  POLICY_EFFECTIVE = 8;     // The applied rule action did work as expected
  POLICY_UPDATED = 9;       // Operator requires a policy to change
  POLICY_REMOVED = 10;      // Operator requires to remove a policy
}

message PolicyRuleId {
@@ -44,39 +49,51 @@ message PolicyRuleId {
}

message PolicyRuleState {
  context.Uuid policyRuleId = 1;
  RuleState policyRuleState = 2;
}

// IETF draft: Framework for Use of ECA (Event Condition Action) in Network Self Management
//     Source: https://datatracker.ietf.org/doc/draft-bwd-netmod-eca-framework/
// Event
message PolicyRuleEvent {
  context.Event event = 1;
  RuleState policyRuleState = 1;
}

// Policy rule partially complies with IETF’s:
//     RFC 3060: https://datatracker.ietf.org/doc/html/rfc3060
//     RFC 3460: https://datatracker.ietf.org/doc/html/rfc3460
// Enhanced with a policy rule event according to the ECA model
message PolicyRule {
// Basic policy rule attributes
message PolicyRuleBasic {
  PolicyRuleId policyRuleId = 1;
  PolicyRuleType policyRuleType = 2;
  optional PolicyRuleState policyRuleState = 2;
  uint32 priority = 3;

  // Event-Condition-Action model
  PolicyRuleEvent event = 4;                         // A single event triggers the policy
  repeated PolicyRuleCondition conditionList = 5;    // One or more conditions must be met
  BooleanOperator booleanOperator = 6;               // Evaluation operator to be used
  repeated PolicyRuleAction actionList = 7;          // One or more actions should be applied
  // Event-Condition-Action (ECA) model
  repeated PolicyRuleCondition conditionList = 4;  // When these policy conditions are met, an event is automatically thrown
  BooleanOperator booleanOperator = 5;             // Evaluation operator to be used
  repeated PolicyRuleAction actionList = 6;        // One or more actions should be applied
}

// Service-oriented policy rule
message PolicyRuleService {
  // Basic policy rule attributes
  PolicyRuleBasic policyRuleBasic = 1;

  // Affected service and (some of) its device(s)
  context.ServiceId serviceId = 2;
  repeated context.DeviceId deviceList = 3;  // List of devices this service is traversing (not exhaustive)
}

// Device-oriented policy rule
message PolicyRuleDevice {
  // Basic policy rule attributes
  PolicyRuleBasic policyRuleBasic = 1;

  // Affected device(s)
  repeated context.DeviceId deviceList = 2;
}

// A list of policy rule IDs
message PolicyRuleIdList {
  repeated PolicyRuleId policyRuleIdList = 1;
}

  // Affected service and devices
  context.ServiceId serviceId = 8;
  repeated context.DeviceId deviceList = 9;
// A list of service-oriented policy rules
message PolicyRuleServiceList {
  repeated PolicyRuleService policyRuleServiceList = 1;
}

// A list of policy rules
message PolicyRuleList {
  repeated PolicyRule policyRuleList = 1;
// A list of device-oriented policy rules
message PolicyRuleDeviceList {
  repeated PolicyRuleDevice policyRuleDeviceList = 1;
}
+47 −13
Original line number Diff line number Diff line
@@ -16,11 +16,18 @@

package eu.teraflow.policy;

import context.ContextOuterClass;
import context.ContextOuterClass.ServiceId;
import io.quarkus.grpc.GrpcService;
import io.smallrye.mutiny.Uni;
import javax.inject.Inject;
import policy.Policy;
import policy.Policy.PolicyRuleBasic;
import policy.Policy.PolicyRuleDevice;
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 {
@@ -33,50 +40,77 @@ public class PolicyGatewayImpl implements PolicyGateway {
    }

    @Override
    public Uni<Policy.PolicyRuleState> policyAdd(Policy.PolicyRule request) {
    public Uni<PolicyRuleState> policyAddService(PolicyRuleService request) {
        return Uni.createFrom()
                .item(
                        () ->
                                Policy.PolicyRuleState.newBuilder()
                                        .setPolicyRuleId(request.getPolicyRuleId().getUuid())
                                        .setPolicyRuleState(
                                                request.getPolicyRuleBasic().getPolicyRuleState().getPolicyRuleState())
                                        .build());
    }

    @Override
    public Uni<Policy.PolicyRuleState> policyUpdate(Policy.PolicyRule request) {
    public Uni<PolicyRuleState> policyAddDevice(PolicyRuleDevice request) {
        return Uni.createFrom()
                .item(
                        () ->
                                Policy.PolicyRuleState.newBuilder()
                                        .setPolicyRuleId(request.getPolicyRuleId().getUuid())
                                        .setPolicyRuleState(
                                                request.getPolicyRuleBasic().getPolicyRuleState().getPolicyRuleState())
                                        .build());
    }

    @Override
    public Uni<Policy.PolicyRuleState> policyDelete(Policy.PolicyRule request) {
    public Uni<PolicyRuleState> policyUpdateService(PolicyRuleService request) {
        return Uni.createFrom()
                .item(
                        () ->
                                Policy.PolicyRuleState.newBuilder()
                                        .setPolicyRuleId(request.getPolicyRuleId().getUuid())
                                        .setPolicyRuleState(
                                                request.getPolicyRuleBasic().getPolicyRuleState().getPolicyRuleState())
                                        .build());
    }

    @Override
    public Uni<Policy.PolicyRule> getPolicy(Policy.PolicyRuleId request) {
    public Uni<PolicyRuleState> policyUpdateDevice(PolicyRuleDevice request) {
        return Uni.createFrom()
                .item(() -> Policy.PolicyRule.newBuilder().setPolicyRuleId(request).build());
                .item(
                        () ->
                                Policy.PolicyRuleState.newBuilder()
                                        .setPolicyRuleState(
                                                request.getPolicyRuleBasic().getPolicyRuleState().getPolicyRuleState())
                                        .build());
    }

    @Override
    public Uni<Policy.PolicyRuleList> getPolicyByDeviceId(ContextOuterClass.DeviceId request) {
    public Uni<PolicyRuleState> policyDelete(PolicyRuleId request) {
        return Uni.createFrom()
                .item(
                        () ->
                                Policy.PolicyRuleState.newBuilder()
                                        .setPolicyRuleState(RuleState.POLICY_REMOVED)
                                        .build());
    }

    @Override
    public Uni<PolicyRuleService> getPolicyService(PolicyRuleId request) {
        final var policyRuleBasic = PolicyRuleBasic.newBuilder().setPolicyRuleId(request).build();

        return Uni.createFrom().item(() -> Policy.PolicyRuleList.newBuilder().build());
        return Uni.createFrom()
                .item(() -> PolicyRuleService.newBuilder().setPolicyRuleBasic(policyRuleBasic).build());
    }

    @Override
    public Uni<Policy.PolicyRuleList> getPolicyByServiceId(ContextOuterClass.ServiceId request) {
    public Uni<PolicyRuleDevice> getPolicyDevice(PolicyRuleId request) {
        final var policyRuleBasic = PolicyRuleBasic.newBuilder().setPolicyRuleId(request).build();

        return Uni.createFrom().item(() -> Policy.PolicyRuleList.newBuilder().build());
        return Uni.createFrom()
                .item(() -> PolicyRuleDevice.newBuilder().setPolicyRuleBasic(policyRuleBasic).build());
    }

    @Override
    public Uni<PolicyRuleServiceList> getPolicyByServiceId(ServiceId request) {
        return Uni.createFrom().item(() -> Policy.PolicyRuleServiceList.newBuilder().build());
    }
}
+5 −2
Original line number Diff line number Diff line
@@ -16,11 +16,14 @@

package eu.teraflow.policy;

import eu.teraflow.policy.model.PolicyRule;
import eu.teraflow.policy.model.PolicyRuleState;
import io.smallrye.mutiny.Uni;
import policy.Policy.PolicyRuleDevice;
import policy.Policy.PolicyRuleService;

public interface PolicyService {

    Uni<PolicyRuleState> addPolicy(PolicyRule policyRule);
    Uni<PolicyRuleState> addPolicyService(PolicyRuleService policyRuleService);

    Uni<PolicyRuleState> addPolicyDevice(PolicyRuleDevice policyRuleDevice);
}
+8 −2
Original line number Diff line number Diff line
@@ -16,19 +16,25 @@

package eu.teraflow.policy;

import eu.teraflow.policy.model.PolicyRule;
import eu.teraflow.policy.model.PolicyRuleState;
import io.smallrye.mutiny.Uni;
import javax.enterprise.context.ApplicationScoped;
import org.jboss.logging.Logger;
import policy.Policy.PolicyRuleDevice;
import policy.Policy.PolicyRuleService;

@ApplicationScoped
public class PolicyServiceImpl implements PolicyService {

    private static final Logger LOGGER = Logger.getLogger(PolicyServiceImpl.class);

    public Uni<PolicyRuleState> addPolicy(PolicyRule policyRule) {
    @Override
    public Uni<PolicyRuleState> addPolicyService(PolicyRuleService policyRuleService) {
        return null;
    }

    @Override
    public Uni<PolicyRuleState> addPolicyDevice(PolicyRuleDevice policyRuleDevice) {
        return null;
    }
}
+0 −111
Original line number Diff line number Diff line
/*
* Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package eu.teraflow.policy.model;

import eu.teraflow.policy.context.model.ServiceId;
import java.util.List;
import java.util.stream.Collectors;

public class PolicyRule {

    private final String policyRuleId;
    private final PolicyRuleType policyRuleType;
    private final int priority;
    private final PolicyRuleEvent event;
    private final List<PolicyRuleCondition> policyRuleConditions;
    private final BooleanOperator booleanOperator;
    private final List<PolicyRuleAction> policyRuleActions;
    private final ServiceId serviceId;
    private final List<String> deviceIds;

    public PolicyRule(
            String policyRuleId,
            PolicyRuleType policyRuleType,
            int priority,
            PolicyRuleEvent event,
            List<PolicyRuleCondition> policyRuleConditions,
            BooleanOperator booleanOperator,
            List<PolicyRuleAction> policyRuleActions,
            ServiceId serviceId,
            List<String> deviceIds) {
        this.policyRuleId = policyRuleId;
        this.policyRuleType = policyRuleType;
        this.priority = priority;
        this.event = event;
        this.policyRuleConditions = policyRuleConditions;
        this.booleanOperator = booleanOperator;
        this.policyRuleActions = policyRuleActions;
        this.serviceId = serviceId;
        this.deviceIds = deviceIds;
    }

    public String getPolicyRuleId() {
        return policyRuleId;
    }

    public PolicyRuleType getPolicyRuleType() {
        return policyRuleType;
    }

    public int getPriority() {
        return priority;
    }

    public PolicyRuleEvent getEvent() {
        return event;
    }

    public List<PolicyRuleCondition> getPolicyRuleConditions() {
        return policyRuleConditions;
    }

    public BooleanOperator getBooleanOperator() {
        return booleanOperator;
    }

    public List<PolicyRuleAction> getPolicyRuleActions() {
        return policyRuleActions;
    }

    public ServiceId getServiceId() {
        return serviceId;
    }

    public List<String> getDeviceIds() {
        return deviceIds;
    }

    @Override
    public String toString() {
        return String.format(
                "%s:{policyRuleId:\"%s\", policyRuleType:\"%s\", priority:%d, %s, [%s], booleanOperator:\"%s\", [%s], %s, [%s]}",
                getClass().getSimpleName(),
                policyRuleId,
                policyRuleType.toString(),
                priority,
                event,
                toString(policyRuleConditions),
                booleanOperator.toString(),
                toString(policyRuleActions),
                serviceId,
                toString(deviceIds));
    }

    private <T> String toString(List<T> list) {
        return list.stream().map(T::toString).collect(Collectors.joining(", "));
    }
}
Loading