/********************************************************************************
 * Copyright (c) 2022 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Apache License 2.0 which is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * SPDX-License-Identifier: Apache-2.0
 ********************************************************************************/

syntax = "proto3";

package sdv.databroker.v1;

import "sdv/databroker/v1/types.proto";

service Broker {
  // Request a set of datapoints (values)
  //
  // Returns a list of requested data points.
  //
  // InvalidArgument is returned if the request is malformed.
  rpc GetDatapoints(GetDatapointsRequest) returns (GetDatapointsReply);

  // Set a datapoint (values)
  rpc SetDatapoints(SetDatapointsRequest) returns (SetDatapointsReply);

  // Subscribe to a set of data points or conditional expressions
  // using the Data Broker Query Syntax (described in QUERY.md)
  //
  // Returns a stream of replies.
  //
  // InvalidArgument is returned if the request is malformed.
  rpc Subscribe(SubscribeRequest) returns (stream SubscribeReply);

  // Request the metadata of a set of datapoints
  //
  // Returns metadata of the requested data points that exist.
  rpc GetMetadata(GetMetadataRequest) returns (GetMetadataReply);
}

message GetDatapointsRequest {
  // A list of requested data points.
  repeated string datapoints = 1;
}

message GetDatapointsReply {
  // Contains the values of the requested data points.
  // If a requested data point is not available, the corresponding Datapoint
  // will have the respective failure value set.
  map<string, Datapoint> datapoints = 1;
}

message SetDatapointsRequest {
  // A map of data points to set
  map<string, Datapoint> datapoints = 1;
}

message SetDatapointsReply {
  // A map of errors (if any)
  map<string, DatapointError> errors = 1;
}

message SubscribeRequest {
  // Subscribe to a set of data points (or expressions) described
  // by the provided query.
  // The query syntax is a subset of SQL and is described in more
  // detail in the QUERY.md file.
  string query = 2;
}

message SubscribeReply {
  // Contains the fields specified by the query.
  // If a requested data point value is not available, the corresponding
  // Datapoint will have it's respective failure value set.
  map<string, Datapoint> fields = 1;
}

message GetMetadataRequest {
  // Request metadata for a list of data points referenced by their names.
  // e.g. "Vehicle.Cabin.Seat.Row1.Pos1.Position" or "Vehicle.Speed".
  //
  // If no names are provided, metadata for all known data points will be
  // returned.
  repeated string names = 1;
}

message GetMetadataReply {
  // Contains metadata of the requested data points. If a data point
  // doesn't exist (i.e. not known to the Data Broker) the corresponding
  // Metadata isn't part of the returned list.
  repeated Metadata list = 1;
}
