summaryrefslogtreecommitdiffstats
path: root/src/rgw/driver/rados/rgw_otp.cc
blob: 07cc14f113b5fa432ab6f2f397d7ac69778e7f97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

#include <errno.h>

#include <string>
#include <map>
#include <boost/algorithm/string.hpp>

#include "common/errno.h"
#include "common/Formatter.h"
#include "common/ceph_json.h"
#include "rgw_otp.h"
#include "rgw_zone.h"
#include "rgw_metadata.h"

#include "include/types.h"

#include "rgw_common.h"
#include "rgw_tools.h"

#include "services/svc_zone.h"
#include "services/svc_meta.h"
#include "services/svc_meta_be.h"
#include "services/svc_meta_be_otp.h"
#include "services/svc_otp.h"

#define dout_subsys ceph_subsys_rgw

using namespace std;


class RGWOTPMetadataHandler;

class RGWOTPMetadataObject : public RGWMetadataObject {
  friend class RGWOTPMetadataHandler;

  otp_devices_list_t devices;
public:
  RGWOTPMetadataObject() {}
  RGWOTPMetadataObject(otp_devices_list_t&& _devices, const obj_version& v, const real_time m) {
    devices = std::move(_devices);
    objv = v;
    mtime = m;
  }

  void dump(Formatter *f) const override {
    encode_json("devices", devices, f);
  }

  otp_devices_list_t& get_devs() {
    return devices;
  }
};


class RGWOTPMetadataHandler : public RGWOTPMetadataHandlerBase {
  friend class RGWOTPCtl;

  struct Svc {
    RGWSI_Zone *zone;
    RGWSI_MetaBackend *meta_be;
    RGWSI_OTP *otp;
  } svc;

  int init(RGWSI_Zone *zone,
           RGWSI_MetaBackend *_meta_be,
           RGWSI_OTP *_otp) {
    base_init(zone->ctx(), _otp->get_be_handler().get());
    svc.zone = zone;
    svc.meta_be = _meta_be;
    svc.otp = _otp;
    return 0;
  }

  int call(std::function<int(RGWSI_OTP_BE_Ctx& ctx)> f) {
    return be_handler->call([&](RGWSI_MetaBackend_Handler::Op *op) {
      RGWSI_OTP_BE_Ctx ctx(op->ctx());
      return f(ctx);
    });
  }

  RGWMetadataObject *get_meta_obj(JSONObj *jo, const obj_version& objv, const ceph::real_time& mtime) override {
    otp_devices_list_t devices;
    try {
      JSONDecoder::decode_json("devices", devices, jo);
    } catch (JSONDecoder::err& e) {
      return nullptr;
    }

    return new RGWOTPMetadataObject(std::move(devices), objv, mtime);
  }

  int do_get(RGWSI_MetaBackend_Handler::Op *op, string& entry, RGWMetadataObject **obj, optional_yield y, const DoutPrefixProvider *dpp) override {
    RGWObjVersionTracker objv_tracker;

    std::unique_ptr<RGWOTPMetadataObject> mdo(new RGWOTPMetadataObject);

    
    RGWSI_OTP_BE_Ctx be_ctx(op->ctx());

    int ret = svc.otp->read_all(be_ctx,
                                entry,
                                &mdo->get_devs(),
                                &mdo->get_mtime(),
                                &objv_tracker,
                                y,
                                dpp);
    if (ret < 0) {
      return ret;
    }

    mdo->objv = objv_tracker.read_version;

    *obj = mdo.release();

    return 0;
  }

  int do_put(RGWSI_MetaBackend_Handler::Op *op, string& entry,
             RGWMetadataObject *_obj, RGWObjVersionTracker& objv_tracker,
             optional_yield y,
             const DoutPrefixProvider *dpp,
             RGWMDLogSyncType type, bool from_remote_zone) override {
    RGWOTPMetadataObject *obj = static_cast<RGWOTPMetadataObject *>(_obj);

    RGWSI_OTP_BE_Ctx be_ctx(op->ctx());

    int ret = svc.otp->store_all(dpp, be_ctx,
                                 entry,
                                 obj->devices,
                                 obj->mtime,
                                 &objv_tracker,
                                 y);
    if (ret < 0) {
      return ret;
    }

    return STATUS_APPLIED;
  }

  int do_remove(RGWSI_MetaBackend_Handler::Op *op, string& entry, RGWObjVersionTracker& objv_tracker,
                optional_yield y, const DoutPrefixProvider *dpp) override {
    RGWSI_MBOTP_RemoveParams params;

    RGWSI_OTP_BE_Ctx be_ctx(op->ctx());

    return svc.otp->remove_all(dpp, be_ctx,
                               entry,
                               &objv_tracker,
                               y);
  }

public:
  RGWOTPMetadataHandler() {}

  string get_type() override { return "otp"; }
};


RGWOTPCtl::RGWOTPCtl(RGWSI_Zone *zone_svc,
		     RGWSI_OTP *otp_svc)
{
  svc.zone = zone_svc;
  svc.otp = otp_svc;
}


void RGWOTPCtl::init(RGWOTPMetadataHandler *_meta_handler)
{
  meta_handler = _meta_handler;
  be_handler = meta_handler->get_be_handler();
}

int RGWOTPCtl::read_all(const rgw_user& uid,
                        RGWOTPInfo *info,
                        optional_yield y,
                        const DoutPrefixProvider *dpp,
                        const GetParams& params)
{
  info->uid = uid;
  return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
    return svc.otp->read_all(ctx, uid, &info->devices, params.mtime, params.objv_tracker, y, dpp);
  });
}

int RGWOTPCtl::store_all(const DoutPrefixProvider *dpp, 
                         const RGWOTPInfo& info,
                         optional_yield y,
                         const PutParams& params)
{
  return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
    return svc.otp->store_all(dpp, ctx, info.uid, info.devices, params.mtime, params.objv_tracker, y);
  });
}

int RGWOTPCtl::remove_all(const DoutPrefixProvider *dpp,
                          const rgw_user& uid,
                          optional_yield y,
                          const RemoveParams& params)
{
  return meta_handler->call([&](RGWSI_OTP_BE_Ctx& ctx) {
    return svc.otp->remove_all(dpp, ctx, uid, params.objv_tracker, y);
  });
}


RGWMetadataHandler *RGWOTPMetaHandlerAllocator::alloc()
{
  return new RGWOTPMetadataHandler();
}