AcsMessageHandler.cpp 16.6 KB
Newer Older
1
2
3
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  CERN
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
4
 *
5
6
7
8
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
9
10
11
12
13
14
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
15
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
18

19
20
#include "common/Constants.hpp"
#include "mediachanger/acs/daemon/Constants.hpp"
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
21
22
23
24
25
26
27
28
#include "AcsMessageHandler.hpp"
#include "AcsDismountTape.hpp"
#include "AcsForceDismountTape.hpp"
#include "AcsMountTapeReadOnly.hpp"
#include "AcsMountTapeReadWrite.hpp"
#include "mediachanger/acs/Acs.hpp"
#include "mediachanger/acs/AcsImpl.hpp"
#include "common/log/log.hpp"
29
#include "common/log/SyslogLogger.hpp"
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
30
#include "mediachanger/messages.hpp"
31
32
33
34
35
36
37
#include "mediachanger/ReturnValue.pb.h"
#include "mediachanger/AcsMountTapeReadOnly.pb.h"
#include "mediachanger/AcsMountTapeReadWrite.pb.h"
#include "mediachanger/AcsDismountTape.pb.h"
#include "mediachanger/AcsForceDismountTape.pb.h"
#include "mediachanger/Exception.pb.h"
#include "mediachanger/reactor/ZMQPollEventHandler.hpp"
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
38
39
40
41
#include "mediachanger/reactor/ZMQReactor.hpp"
#include "mediachanger/ZmqSocket.hpp"
#include "errno.h"

42
#include <iostream>
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
43
#include <unistd.h>
44

Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
45
46
47
48
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::mediachanger::acs::daemon::AcsMessageHandler::AcsMessageHandler(
49
  cta::log::Logger &log,
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
50
51
  cta::mediachanger::reactor::ZMQReactor &reactor,
  void *const zmqContext,
52
  const std::string &hostName,
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
53
54
  const AcsdConfiguration &ctaConf,
  AcsPendingRequests &acsPendingRequests):
55
  m_log(log),
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
56
57
58
59
60
61
62
  m_reactor(reactor),
  m_socket(zmqContext, ZMQ_ROUTER),
  m_hostName(hostName),
  m_ctaConf(ctaConf),
  m_acsPendingRequests(acsPendingRequests) { 

  std::ostringstream endpoint;
63
  endpoint << "tcp://127.0.0.1:" << m_ctaConf.port.value();
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  
  try {
    m_socket.bind(endpoint.str().c_str());
    std::list<log::Param> params = {log::Param("endpoint", endpoint.str())};
    m_log(LOG_INFO, "Bound the ZMQ socket of the AcsMessageHandler",
      params);
  } catch(cta::exception::Exception &ne){
    cta::exception::Exception ex;
    ex.getMessage() <<
      "Failed to bind the ZMQ socket of the AcsMessageHandler"
      ": endpoint=" << endpoint.str() << ": " << ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::mediachanger::acs::daemon::AcsMessageHandler::~AcsMessageHandler()
83
  {
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
84
85
86
87
88
89
}

//------------------------------------------------------------------------------
// getName
//------------------------------------------------------------------------------
std::string cta::mediachanger::acs::daemon::AcsMessageHandler::getName()
90
  const {
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
91
92
93
94
95
96
97
  return "AcsMessageHandler";
}

//------------------------------------------------------------------------------
// fillPollFd
//------------------------------------------------------------------------------
void cta::mediachanger::acs::daemon::AcsMessageHandler::fillPollFd(
98
  zmq_pollitem_t &fd) {
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
99
100
101
102
103
104
105
106
107
108
  fd.events = ZMQ_POLLIN;
  fd.revents = 0;
  fd.socket = m_socket.getZmqSocket();
  fd.fd = -1;
}

//------------------------------------------------------------------------------
// handleEvent
//------------------------------------------------------------------------------
bool cta::mediachanger::acs::daemon::AcsMessageHandler::handleEvent(
109
  const zmq_pollitem_t &fd) {
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
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
  // Try to receive a request, simply giving up if an exception is raised
  cta::mediachanger::Frame rqst;

  //for handling zeroMQ's router socket type specific elements 
  //ie first frame = identity of the sender
  //   second one  =  empty
  //   third and following = actual data frames
 
  //The ZmqMsg address data can be dump as string and used as key for storing 
  //the identity (for clients who need a late answer)
  cta::mediachanger::ZmqMsg address;
  cta::mediachanger::ZmqMsg empty;
  try {
    checkSocket(fd);
    m_socket.recv(address);
    m_socket.recv(empty);
    rqst = recvFrame(m_socket);
  } catch(cta::exception::Exception &ex) {
    std::list<log::Param> params = {log::Param("message", ex.getMessage().str())};
    m_log(LOG_ERR, "AcsMessageHandler failed to handle event", params);
    return false; // Give up and stay registered with the reactor
  }
  std::list<log::Param> params = {
      log::Param("sender identity", 
              utils::hexDump(address.getData(),address.size()))
     };
  m_log(LOG_DEBUG, "handling event in AcsMessageHandler", params);
 
  // From this point on any exception thrown should be converted into an
  // Exception message and sent back to the client 
  cta::mediachanger::Frame reply;
  
  try {
    // if there are any problems we need to send the replay to the client.
    reply = dispatchMsgHandler(rqst);
  } catch(cta::exception::Exception &ex) {
146
    reply = createExceptionFrame(ECANCELED, ex.getMessage().str()); 
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
147
148
    m_log(LOG_ERR, ex.getMessage().str());
  } catch(std::exception &se) {
149
    reply = createExceptionFrame(ECANCELED, se.what());
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
150
151
    m_log(LOG_ERR, se.what());
  } catch(...) {
152
    reply = createExceptionFrame(ECANCELED, "Caught an unknown exception"); 
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
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
    m_log(LOG_ERR, "Caught an unknown exception");
  }

  // Send the reply to the client
  try {
    //we need to prepend our frames the same way we received them
    // ie identity + empty frames 
    m_socket.send(address,ZMQ_SNDMORE);
    m_socket.send(empty,ZMQ_SNDMORE);

    cta::mediachanger::sendFrame(m_socket, reply);
  } catch(cta::exception::Exception &ex) {
    std::list<log::Param> params = {log::Param("message", ex.getMessage().str())};
    m_log(LOG_ERR, "AcsMessageHandler failed to send reply to client", params);
  }

  return false; // Stay registered with the reactor
}

//------------------------------------------------------------------------------
// checkSocket
//------------------------------------------------------------------------------
void cta::mediachanger::acs::daemon::AcsMessageHandler::checkSocket(
  const zmq_pollitem_t &fd) const{
  void* underlyingSocket = m_socket.getZmqSocket();
  if(fd.socket != underlyingSocket){
    cta::exception::Exception ex;
    ex.getMessage() << "AcsMessageHandler passed wrong poll item";
    throw ex;
  }
}

//------------------------------------------------------------------------------
// dispatchMsgHandler
//------------------------------------------------------------------------------
cta::mediachanger::Frame cta::mediachanger::acs::daemon::AcsMessageHandler::
  dispatchMsgHandler(const cta::mediachanger::Frame &rqst) {
  m_log(LOG_DEBUG, "AcsMessageHandler dispatching message handler");
  
192
  const cta::mediachanger::acs::daemon::MsgType msgType = (cta::mediachanger::acs::daemon::MsgType)rqst.header.msgtype();
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  switch(msgType) {
  case cta::mediachanger::MSG_TYPE_ACSMOUNTTAPEREADONLY:
    return handleAcsMountTapeReadOnly(rqst);
      
  case cta::mediachanger::MSG_TYPE_ACSMOUNTTAPEREADWRITE:
    return handleAcsMountTapeReadWrite(rqst);  

  case cta::mediachanger::MSG_TYPE_ACSDISMOUNTTAPE:
    return handleAcsDismountTape(rqst);

  case cta::mediachanger::MSG_TYPE_ACSFORCEDISMOUNTTAPE:
    return handleAcsForceDismountTape(rqst);

  default:
    {
208
      const std::string msgTypeStr = cta::mediachanger::acs::daemon::msgTypeToString(msgType);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
      cta::exception::Exception ex;
      ex.getMessage() << "Failed to dispatch message handler"
        ": Unexpected request type: msgType=" << msgType << " msgTypeStr=" <<
        msgTypeStr;
      throw ex;
    }
  }
}

//------------------------------------------------------------------------------
// handleAcsMountTapeReadOnly
//------------------------------------------------------------------------------
cta::mediachanger::Frame cta::mediachanger::acs::daemon::AcsMessageHandler::
  handleAcsMountTapeReadOnly(const cta::mediachanger::Frame &rqst) {
  m_log(LOG_DEBUG, "Handling AcsMountTapeReadOnly message");

  try {
226
    cta::mediachanger::AcsMountTapeReadOnly rqstBody;
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
227
228
229
230
231
232
233
234
    rqst.parseBodyIntoProtocolBuffer(rqstBody);
    
    const std::string vid = rqstBody.vid();
    const uint32_t acs    = rqstBody.acs();
    const uint32_t lsm    = rqstBody.lsm();
    const uint32_t panel  = rqstBody.panel();
    const uint32_t drive  = rqstBody.drive();
    
235
    std::list<log::Param> params = {log::Param("tapeVid", vid),
236
237
238
239
      log::Param("acs", acs),
      log::Param("lsm", lsm),
      log::Param("panel", panel),
      log::Param("drive", drive)};
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
240
241
242
243
    m_log(LOG_INFO, "Mount tape for read-only access", params);

    cta::mediachanger::acs::AcsImpl acsWrapper;
    cta::mediachanger::acs::daemon::AcsMountTapeReadOnly acsMountTapeReadOnly(vid, acs, lsm, 
244
      panel, drive, acsWrapper, m_log, m_ctaConf);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
245
246
247
248
249
    try {
      acsMountTapeReadOnly.execute();
      m_log(LOG_INFO,"Tape successfully mounted for read-only access", params);
    } catch (cta::exception::Exception &ne) {
      m_log(LOG_ERR,"Tape mount for read-only access failed: "
250
        + ne.getMessage().str(), params);  
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
      throw;  
    }     
    const cta::mediachanger::Frame reply = createReturnValueFrame(0);
    return reply;
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to handle AcsMountTapeReadOnly message: " <<
      ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// handleAcsMountTapeReadWrite
//------------------------------------------------------------------------------
cta::mediachanger::Frame cta::mediachanger::acs::daemon::AcsMessageHandler::
  handleAcsMountTapeReadWrite(const cta::mediachanger::Frame &rqst) {
  m_log(LOG_DEBUG, "Handling AcsMountTapeReadWrite message");

  try {
271
    cta::mediachanger::AcsMountTapeReadWrite rqstBody;
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
272
273
274
275
276
277
278
279
    rqst.parseBodyIntoProtocolBuffer(rqstBody);
     
    const std::string vid = rqstBody.vid();
    const uint32_t acs    = rqstBody.acs();
    const uint32_t lsm    = rqstBody.lsm();
    const uint32_t panel  = rqstBody.panel();
    const uint32_t drive  = rqstBody.drive();
    
280
    std::list<log::Param> params = {log::Param("tapeVid", vid),
281
282
283
284
285
      log::Param("acs", acs),
      log::Param("lsm", lsm),
      log::Param("panel", panel),
      log::Param("drive", drive)};
    m_log(LOG_INFO, "Mount tape for read/write access",params);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
286
287

    cta::mediachanger::acs::AcsImpl acsWrapper;
288
289
    cta::mediachanger::acs::daemon::AcsMountTapeReadWrite acsMountTapeReadWrite(vid, acs,
      lsm, panel, drive, acsWrapper, m_log, m_ctaConf);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
290
291
    try {
      acsMountTapeReadWrite.execute();   
292
      m_log(LOG_INFO,"Tape successfully mounted for read/write access", params);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
293
294
    } catch (cta::exception::Exception &ne) {
      m_log(LOG_ERR,"Tape mount for read/write access failed: "
295
        + ne.getMessage().str(), params);  
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
      throw;  
    }     
    const cta::mediachanger::Frame reply = createReturnValueFrame(0);
    return reply;
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to handle AcsMountTapeReadWrite message: " <<
      ne.getMessage().str();
    throw ex;
  }
}

//------------------------------------------------------------------------------
// handleAcsDismountTape
//------------------------------------------------------------------------------
311
cta::mediachanger::Frame cta::mediachanger::acs::daemon::AcsMessageHandler::
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
312
  handleAcsDismountTape(const cta::mediachanger::Frame& rqst) {
313
  m_log(LOG_DEBUG, "Handling AcsDismountTape message");
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
314
315

  try {
316
    cta::mediachanger::AcsDismountTape rqstBody;
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
317
318
319
320
321
322
323
324
    rqst.parseBodyIntoProtocolBuffer(rqstBody);

    const std::string vid = rqstBody.vid();
    const uint32_t acs    = rqstBody.acs();
    const uint32_t lsm    = rqstBody.lsm();
    const uint32_t panel  = rqstBody.panel();
    const uint32_t drive  = rqstBody.drive();
    
325
    std::list<log::Param> params = {log::Param("tapeVid", vid),
326
327
328
329
      log::Param("acs", acs),
      log::Param("lsm", lsm),
      log::Param("panel", panel),
      log::Param("drive", drive)};
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
330
331
332
333
    m_log(LOG_INFO, "Dismount tape",params);

    cta::mediachanger::acs::AcsImpl acsWrapper;
    cta::mediachanger::acs::daemon::AcsDismountTape acsDismountTape(vid, acs, lsm, panel, drive,
334
      acsWrapper, m_log, m_ctaConf);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
    try {
      acsDismountTape.execute();
      m_log(LOG_INFO,"Tape successfully dismounted", params);
    } catch (cta::exception::Exception &ne) {
      m_log(LOG_ERR,"Tape dismount failed: "+ne.getMessage().str(), params);  
      throw;  
    }    
    const cta::mediachanger::Frame reply = createReturnValueFrame(0);
    return reply;
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to handle AcsDismountTape message: " <<
      ne.getMessage().str();
    throw ex;
  } catch(...) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to handle AcsDismountTape message: " 
                    << "Caught an unknown exception";
    throw ex;
  }
}

//------------------------------------------------------------------------------
// handleAcsForceDismountTape
//------------------------------------------------------------------------------
cta::mediachanger::Frame cta::mediachanger::acs::daemon::AcsMessageHandler::
  handleAcsForceDismountTape(const cta::mediachanger::Frame& rqst) {
  m_log(LOG_DEBUG, "Handling AcsDismountTape message");

  try {
365
    cta::mediachanger::AcsForceDismountTape rqstBody;
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
366
367
368
369
370
371
372
373
    rqst.parseBodyIntoProtocolBuffer(rqstBody);

    const std::string vid = rqstBody.vid();
    const uint32_t acs    = rqstBody.acs();
    const uint32_t lsm    = rqstBody.lsm();
    const uint32_t panel  = rqstBody.panel();
    const uint32_t drive  = rqstBody.drive();

374
    std::list<log::Param> params = {log::Param("tapeVid", vid),
375
376
377
378
379
      log::Param("acs", acs),
      log::Param("lsm", lsm),
      log::Param("panel", panel),
      log::Param("drive", drive)};
    m_log(LOG_INFO, "Force dismount tape", params);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
380
381

    cta::mediachanger::acs::AcsImpl acsWrapper;
382
383
    cta::mediachanger::acs::daemon::AcsForceDismountTape acsForceDismountTape(vid, acs, lsm,
      panel, drive, acsWrapper, m_log, m_ctaConf);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
384
385
    try {
      acsForceDismountTape.execute();
386
      m_log(LOG_INFO,"Tape successfully force dismounted", params);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
387
388
    } catch (cta::exception::Exception &ne) {
      m_log(LOG_ERR,"Tape force dismount failed: "+ne.getMessage().str(),
389
        params);
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
      throw;
    }
    const cta::mediachanger::Frame reply = createReturnValueFrame(0);
    return reply;
  } catch(cta::exception::Exception &ne) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to handle AcsForceDismountTape message: " <<
      ne.getMessage().str();
    throw ex;
  } catch(...) {
    cta::exception::Exception ex;
    ex.getMessage() << "Failed to handle AcsForceDismountTape message: "
                    << "Caught an unknown exception";
    throw ex;
  }
}

//------------------------------------------------------------------------------
// createReturnValueFrame
//------------------------------------------------------------------------------
cta::mediachanger::Frame cta::mediachanger::acs::daemon::AcsMessageHandler::
  createReturnValueFrame(const int value) {
  cta::mediachanger::Frame frame;

  frame.header = cta::mediachanger::protoTapePreFillHeader();
  frame.header.set_msgtype(cta::mediachanger::MSG_TYPE_RETURNVALUE);
  frame.header.set_bodyhashvalue(cta::mediachanger::computeSHA1Base64(frame.body));
  frame.header.set_bodysignature("PIPO");

419
  cta::mediachanger::ReturnValue body;
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
  body.set_value(value);
  frame.serializeProtocolBufferIntoBody(body);

  return frame;
}

//------------------------------------------------------------------------------
// createExceptionFrame
//------------------------------------------------------------------------------
cta::mediachanger::Frame cta::mediachanger::acs::daemon::AcsMessageHandler::
  createExceptionFrame(const int code, const std::string& msg) {
  cta::mediachanger::Frame frame;

  frame.header = cta::mediachanger::protoTapePreFillHeader();
  frame.header.set_msgtype(cta::mediachanger::MSG_TYPE_EXCEPTION);
  frame.header.set_bodyhashvalue(cta::mediachanger::computeSHA1Base64(frame.body));
  frame.header.set_bodysignature("PIPO");

438
  cta::mediachanger::Exception body;
Anastasia Karachaliou's avatar
Anastasia Karachaliou committed
439
440
441
442
443
444
  body.set_code(code);
  body.set_message(msg);
  frame.serializeProtocolBufferIntoBody(body);

  return frame;
}