Imported Upstream version 1.27.0
[platform/upstream/grpc.git] / src / core / ext / filters / client_channel / xds / xds_bootstrap.h
1 //
2 // Copyright 2019 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_XDS_XDS_BOOTSTRAP_H
18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_XDS_XDS_BOOTSTRAP_H
19
20 #include <grpc/support/port_platform.h>
21
22 #include <vector>
23
24 #include <grpc/impl/codegen/slice.h>
25
26 #include "src/core/lib/gprpp/inlined_vector.h"
27 #include "src/core/lib/gprpp/map.h"
28 #include "src/core/lib/gprpp/memory.h"
29 #include "src/core/lib/iomgr/error.h"
30 #include "src/core/lib/json/json.h"
31
32 namespace grpc_core {
33
34 class XdsBootstrap {
35  public:
36   struct MetadataValue {
37     enum class Type { MD_NULL, DOUBLE, STRING, BOOL, STRUCT, LIST };
38     Type type = Type::MD_NULL;
39     // TODO(roth): Once we can use C++17, these can be in a std::variant.
40     double double_value;
41     const char* string_value;
42     bool bool_value;
43     std::map<const char*, MetadataValue, StringLess> struct_value;
44     std::vector<MetadataValue> list_value;
45   };
46
47   struct Node {
48     const char* id = nullptr;
49     const char* cluster = nullptr;
50     const char* locality_region = nullptr;
51     const char* locality_zone = nullptr;
52     const char* locality_subzone = nullptr;
53     std::map<const char*, MetadataValue, StringLess> metadata;
54   };
55
56   struct ChannelCreds {
57     const char* type = nullptr;
58     grpc_json* config = nullptr;
59   };
60
61   struct XdsServer {
62     const char* server_uri = nullptr;
63     InlinedVector<ChannelCreds, 1> channel_creds;
64   };
65
66   // If *error is not GRPC_ERROR_NONE after returning, then there was an
67   // error reading the file.
68   static std::unique_ptr<XdsBootstrap> ReadFromFile(grpc_error** error);
69
70   // Do not instantiate directly -- use ReadFromFile() above instead.
71   XdsBootstrap(grpc_slice contents, grpc_error** error);
72   ~XdsBootstrap();
73
74   // TODO(roth): We currently support only one server. Fix this when we
75   // add support for fallback for the xds channel.
76   const XdsServer& server() const { return servers_[0]; }
77   const Node* node() const { return node_.get(); }
78
79  private:
80   grpc_error* ParseXdsServerList(grpc_json* json);
81   grpc_error* ParseXdsServer(grpc_json* json, size_t idx);
82   grpc_error* ParseChannelCredsArray(grpc_json* json, XdsServer* server);
83   grpc_error* ParseChannelCreds(grpc_json* json, size_t idx, XdsServer* server);
84   grpc_error* ParseNode(grpc_json* json);
85   grpc_error* ParseLocality(grpc_json* json);
86
87   InlinedVector<grpc_error*, 1> ParseMetadataStruct(
88       grpc_json* json,
89       std::map<const char*, MetadataValue, StringLess>* result);
90   InlinedVector<grpc_error*, 1> ParseMetadataList(
91       grpc_json* json, std::vector<MetadataValue>* result);
92   grpc_error* ParseMetadataValue(grpc_json* json, size_t idx,
93                                  MetadataValue* result);
94
95   grpc_slice contents_;
96   grpc_json* tree_ = nullptr;
97
98   InlinedVector<XdsServer, 1> servers_;
99   std::unique_ptr<Node> node_;
100 };
101
102 }  // namespace grpc_core
103
104 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_XDS_XDS_BOOTSTRAP_H */