Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mon / MonCommand.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2017 John Spray <john.spray@redhat.com>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  */
13
14 #pragma once
15
16 #include <string>
17 #include "include/encoding.h"
18
19 struct MonCommand {
20   std::string cmdstring;
21   std::string helpstring;
22   std::string module;
23   std::string req_perms;
24   std::string availability;
25   uint64_t flags;
26
27   // MonCommand flags
28   static const uint64_t FLAG_NONE       = 0;
29   static const uint64_t FLAG_NOFORWARD  = 1 << 0;
30   static const uint64_t FLAG_OBSOLETE   = 1 << 1;
31   static const uint64_t FLAG_DEPRECATED = 1 << 2;
32   static const uint64_t FLAG_MGR        = 1 << 3;
33
34   bool has_flag(uint64_t flag) const { return (flags & flag) != 0; }
35   void set_flag(uint64_t flag) { flags |= flag; }
36   void unset_flag(uint64_t flag) { flags &= ~flag; }
37
38   void encode(bufferlist &bl) const {
39     ENCODE_START(1, 1, bl);
40     encode_bare(bl);
41     ::encode(flags, bl);
42     ENCODE_FINISH(bl);
43   }
44
45   void decode(bufferlist::iterator &bl) {
46     DECODE_START(1, bl);
47     decode_bare(bl);
48     ::decode(flags, bl);
49     DECODE_FINISH(bl);
50   }
51
52   /**
53    * Unversioned encoding for use within encode_array.
54    */
55   void encode_bare(bufferlist &bl) const {
56     ::encode(cmdstring, bl);
57     ::encode(helpstring, bl);
58     ::encode(module, bl);
59     ::encode(req_perms, bl);
60     ::encode(availability, bl);
61   }
62   void decode_bare(bufferlist::iterator &bl) {
63     ::decode(cmdstring, bl);
64     ::decode(helpstring, bl);
65     ::decode(module, bl);
66     ::decode(req_perms, bl);
67     ::decode(availability, bl);
68   }
69   bool is_compat(const MonCommand* o) const {
70     return cmdstring == o->cmdstring &&
71         module == o->module && req_perms == o->req_perms &&
72         availability == o->availability;
73   }
74
75   bool is_noforward() const {
76     return has_flag(MonCommand::FLAG_NOFORWARD);
77   }
78
79   bool is_obsolete() const {
80     return has_flag(MonCommand::FLAG_OBSOLETE);
81   }
82
83   bool is_deprecated() const {
84     return has_flag(MonCommand::FLAG_DEPRECATED);
85   }
86
87   bool is_mgr() const {
88     return has_flag(MonCommand::FLAG_MGR);
89   }
90
91   static void encode_array(const MonCommand *cmds, int size, bufferlist &bl) {
92     ENCODE_START(2, 1, bl);
93     uint16_t s = size;
94     ::encode(s, bl);
95     for (int i = 0; i < size; ++i) {
96       cmds[i].encode_bare(bl);
97     }
98     for (int i = 0; i < size; i++) {
99       ::encode(cmds[i].flags, bl);
100     }
101     ENCODE_FINISH(bl);
102   }
103   static void decode_array(MonCommand **cmds, int *size,
104                            bufferlist::iterator &bl) {
105     DECODE_START(2, bl);
106     uint16_t s = 0;
107     ::decode(s, bl);
108     *size = s;
109     *cmds = new MonCommand[*size];
110     for (int i = 0; i < *size; ++i) {
111       (*cmds)[i].decode_bare(bl);
112     }
113     if (struct_v >= 2) {
114       for (int i = 0; i < *size; i++)
115         ::decode((*cmds)[i].flags, bl);
116     } else {
117       for (int i = 0; i < *size; i++)
118         (*cmds)[i].flags = 0;
119     }
120     DECODE_FINISH(bl);
121   }
122
123   // this uses a u16 for the count, so we need a special encoder/decoder.
124   static void encode_vector(const std::vector<MonCommand>& cmds,
125                             bufferlist &bl) {
126     ENCODE_START(2, 1, bl);
127     uint16_t s = cmds.size();
128     ::encode(s, bl);
129     for (unsigned i = 0; i < s; ++i) {
130       cmds[i].encode_bare(bl);
131     }
132     for (unsigned i = 0; i < s; i++) {
133       ::encode(cmds[i].flags, bl);
134     }
135     ENCODE_FINISH(bl);
136   }
137   static void decode_vector(std::vector<MonCommand> &cmds,
138                             bufferlist::iterator &bl) {
139     DECODE_START(2, bl);
140     uint16_t s = 0;
141     ::decode(s, bl);
142     cmds.resize(s);
143     for (unsigned i = 0; i < s; ++i) {
144       cmds[i].decode_bare(bl);
145     }
146     if (struct_v >= 2) {
147       for (unsigned i = 0; i < s; i++)
148         ::decode(cmds[i].flags, bl);
149     } else {
150       for (unsigned i = 0; i < s; i++)
151         cmds[i].flags = 0;
152     }
153     DECODE_FINISH(bl);
154   }
155
156   bool requires_perm(char p) const {
157     return (req_perms.find(p) != std::string::npos);
158   }
159 };
160 WRITE_CLASS_ENCODER(MonCommand)