// 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.

syntax = "proto3";
package policy;

import "context.proto";
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) {}
}

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
}

message PolicyRuleId {
  context.Uuid uuid = 1;
}

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;
}

// 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
  PolicyRuleId policyRuleId = 1;
  PolicyRuleType policyRuleType = 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

  // Affected service and devices
  context.ServiceId serviceId = 8;
  repeated context.DeviceId deviceList = 9;
}

// A list of policy rules
message PolicyRuleList {
  repeated PolicyRule policyRuleList = 1;
}
