syntax = "proto3";

option go_package = "v1";
option java_package = "io.stackrox.proto.api.v1";

import "google/api/annotations.proto";
import "api/v1/network_graph.proto";
import "api/v1/common.proto";
import "api/v1/empty.proto";
import "storage/network_policy.proto";

package v1;

// API specific objects.
////////////////////////

message GetNetworkPoliciesRequest {
    string cluster_id = 1;
}

message NetworkPoliciesResponse {
    repeated storage.NetworkPolicy network_policies = 1;
}

message SendNetworkPolicyYamlRequest {
    string  notifier_id = 1;
    string  yaml        = 2;
    string  cluster_id  = 3;
}

message GetNetworkGraphRequest {
    string cluster_id = 1;
    string query      = 2;
}

message SimulateNetworkGraphRequest {
    string cluster_id               = 1;
    string query                    = 2;
    string simulation_yaml          = 3;
}

message NetworkPolicyInSimulation {
    enum Status {
        INVALID = 0;
        UNCHANGED = 1;
        MODIFIED = 2;
        ADDED = 3;
    }

    storage.NetworkPolicy policy = 1;
    Status        status = 2;
    storage.NetworkPolicy old_policy = 3;  // if status is MODIFIED, this contains the previous network policy.
}

message NetworkNodeDiff {
    repeated string policy_ids = 1;
    map<int32, NetworkEdgePropertiesBundle> out_edges = 2;
}

message NetworkGraphDiff {
    map<int32, NetworkNodeDiff> node_diffs = 1;
}

message SimulateNetworkGraphResponse {
    NetworkGraph simulated_graph                = 1;
    repeated NetworkPolicyInSimulation policies = 2;

    NetworkGraphDiff added = 3;
    NetworkGraphDiff removed = 4;
}

message NetworkGraphEpoch {
    uint32 epoch = 1;
}

service NetworkPolicyService {

    rpc GetNetworkPolicy (ResourceByID) returns (storage.NetworkPolicy) {
        option (google.api.http) = {
            get: "/v1/networkpolicies/{id}"
        };
    }

    rpc GetNetworkPolicies (GetNetworkPoliciesRequest) returns (NetworkPoliciesResponse) {
        option (google.api.http) = {
            get: "/v1/networkpolicies"
        };
    }

    rpc GetNetworkGraph (GetNetworkGraphRequest) returns (NetworkGraph) {
        option (google.api.http) = {
            get: "/v1/networkpolicies/cluster/{cluster_id}"
        };
    }

    rpc GetNetworkGraphEpoch (Empty) returns (NetworkGraphEpoch) {
        option (google.api.http) = {
            get: "/v1/networkpolicies/graph/epoch"
        };
    }

    rpc SimulateNetworkGraph (SimulateNetworkGraphRequest) returns (SimulateNetworkGraphResponse) {
        option (google.api.http) = {
            post: "/v1/networkpolicies/simulate/{cluster_id}"
            body: "simulation_yaml"
        };
    }

    rpc SendNetworkPolicyYAML (SendNetworkPolicyYamlRequest) returns (Empty) {
        option (google.api.http) = {
            post: "/v1/networkpolicies/simulate/{cluster_id}/notify"
            body: "yaml"
        };
    }
}
