Apache Mesos
utils.hpp
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef __CSI_UTILS_HPP__
18 #define __CSI_UTILS_HPP__
19 
20 #include <ostream>
21 #include <type_traits>
22 
23 #include <csi/spec.hpp>
24 
25 #include <google/protobuf/map.h>
26 
27 #include <google/protobuf/util/json_util.h>
28 
29 #include <mesos/mesos.hpp>
30 
31 #include <stout/foreach.hpp>
32 #include <stout/try.hpp>
33 #include <stout/unreachable.hpp>
34 
35 #include "csi/state.hpp"
36 
37 namespace csi {
38 namespace v0 {
39 
40 bool operator==(
41  const ControllerServiceCapability& left,
42  const ControllerServiceCapability& right);
43 
44 
45 bool operator==(const VolumeCapability& left, const VolumeCapability& right);
46 
47 
48 inline bool operator!=(
49  const VolumeCapability& left,
50  const VolumeCapability& right)
51 {
52  return !(left == right);
53 }
54 
55 
56 std::ostream& operator<<(
57  std::ostream& stream,
59 
60 
61 // Default imprementation for output protobuf messages in namespace
62 // `csi`. Note that any non-template overloading of the output operator
63 // would take precedence over this function template.
64 template <
65  typename Message,
66  typename std::enable_if<std::is_convertible<
67  Message*, google::protobuf::Message*>::value, int>::type = 0>
68 std::ostream& operator<<(std::ostream& stream, const Message& message)
69 {
70  // NOTE: We use Google's JSON utility functions for proto3.
71  std::string output;
72  google::protobuf::util::MessageToJsonString(message, &output);
73  return stream << output;
74 }
75 
76 } // namespace v0 {
77 } // namespace csi {
78 
79 
80 namespace mesos {
81 namespace csi {
82 namespace v0 {
83 
85 {
86  PluginCapabilities() = default;
87 
88  template <typename Iterable> PluginCapabilities(const Iterable& capabilities)
89  {
90  foreach (const auto& capability, capabilities) {
91  if (capability.has_service() &&
92  PluginCapability::Service::Type_IsValid(
93  capability.service().type())) {
94  switch (capability.service().type()) {
96  break;
97  case PluginCapability::Service::CONTROLLER_SERVICE:
98  controllerService = true;
99  break;
100  case google::protobuf::kint32min:
101  case google::protobuf::kint32max:
102  UNREACHABLE();
103  }
104  }
105  }
106  }
107 
108  bool controllerService = false;
109 };
110 
111 
113 {
114  ControllerCapabilities() = default;
115 
116  template <typename Iterable>
117  ControllerCapabilities(const Iterable& capabilities)
118  {
119  foreach (const auto& capability, capabilities) {
120  if (capability.has_rpc() &&
121  ControllerServiceCapability::RPC::Type_IsValid(
122  capability.rpc().type())) {
123  switch (capability.rpc().type()) {
125  break;
126  case ControllerServiceCapability::RPC::CREATE_DELETE_VOLUME:
127  createDeleteVolume = true;
128  break;
129  case ControllerServiceCapability::RPC::PUBLISH_UNPUBLISH_VOLUME:
130  publishUnpublishVolume = true;
131  break;
133  listVolumes = true;
134  break;
136  getCapacity = true;
137  break;
138  case google::protobuf::kint32min:
139  case google::protobuf::kint32max:
140  UNREACHABLE();
141  }
142  }
143  }
144  }
145 
146  bool createDeleteVolume = false;
147  bool publishUnpublishVolume = false;
148  bool listVolumes = false;
149  bool getCapacity = false;
150 };
151 
152 
154 {
155  NodeCapabilities() = default;
156 
157  template <typename Iterable> NodeCapabilities(const Iterable& capabilities)
158  {
159  foreach (const auto& capability, capabilities) {
160  if (capability.has_rpc() &&
161  NodeServiceCapability::RPC::Type_IsValid(capability.rpc().type())) {
162  switch (capability.rpc().type()) {
164  break;
165  case NodeServiceCapability::RPC::STAGE_UNSTAGE_VOLUME:
166  stageUnstageVolume = true;
167  break;
168  case google::protobuf::kint32min:
169  case google::protobuf::kint32max:
170  UNREACHABLE();
171  }
172  }
173  }
174  }
175 
176  bool stageUnstageVolume = false;
177 };
178 
179 } // namespace v0 {
180 } // namespace csi {
181 } // namespace mesos {
182 
183 #endif // __CSI_UTILS_HPP__
Definition: rpc.hpp:41
Definition: rpc.hpp:42
Definition: utils.hpp:37
std::ostream & operator<<(std::ostream &stream, const ControllerServiceCapability::RPC::Type &type)
Definition: utils.hpp:84
Definition: spec.hpp:26
bool operator!=(const VolumeCapability &left, const VolumeCapability &right)
Definition: utils.hpp:48
const int UNKNOWN
Definition: diagnosis.hpp:39
#define UNREACHABLE()
Definition: unreachable.hpp:22
Type
Definition: capabilities.hpp:79
ControllerCapabilities(const Iterable &capabilities)
Definition: utils.hpp:117
bool operator==(const ControllerServiceCapability &left, const ControllerServiceCapability &right)
Try< uint32_t > type(const std::string &path)
Definition: utils.hpp:153
PluginCapabilities(const Iterable &capabilities)
Definition: utils.hpp:88
NodeCapabilities(const Iterable &capabilities)
Definition: utils.hpp:157