/*
 * openi_data_api
 * openi-ict.eu
 *
 * Copyright (c) 2013 dmccarthy
 * Licensed under the MIT license.
 */

'use strict';

var jwt    = require('jsonwebtoken');
var zmq    = require('m2nodehandler');
var helper = require('./helper.js');
var rrd    = require('openi_rrd');


var processMessage = function(config, msg, senderToClient, senderToDao){
   var daoMsg = helper.processMongrel2Message(msg, senderToClient);

   if ( null === daoMsg){
      return
   }

   if (undefined !== daoMsg && undefined !== daoMsg.error){
      senderToClient.send(msg.uuid, msg.connId, zmq.status.BAD_REQUEST_400, zmq.standard_headers.json, daoMsg);
   }
   else {
      daoMsg.mongrel_sink = config.mongrel_handler.sink;
      senderToDao.send(daoMsg);
   }
};


var cloudletApi = function(config){

   helper.init(config.logger_params);
   zmq.addPreProcessFilter(rrd.msgfilter);

   var senderToDao    = zmq.sender(config.dao_sink);
   var senderToClient = zmq.sender(config.mongrel_handler.sink);

   zmq.receiver(config.mongrel_handler.source, config.mongrel_handler.sink, function(msg) {

      if ( msg.headers.METHOD === 'GET' && msg.path === '/api/v1/cloudlets') {

         if (undefined === msg.headers.authorization || null == msg.headers.authorization){
            senderToClient.send(msg.uuid, msg.connId, zmq.status.BAD_REQUEST_400, zmq.standard_headers.json, {"error":"Auth token required" });
            return
         }

         var tokenB64 = msg.headers.authorization.replace("Bearer ", "");

         jwt.verify(tokenB64, config.trusted_security_framework_public_key, function(err, token) {

            if (undefined !== err && null !== err){
               senderToClient.send(msg.uuid, msg.connId, zmq.status.BAD_REQUEST_400, zmq.standard_headers.json, {"error":"Invalid token: " + err });
            }
            else {
               if (token){
                  msg.token = JSON.parse(token);
               }
               processMessage(config, msg, senderToClient, senderToDao);
            }
         });
      }
      else {
         processMessage(config, msg, senderToClient, senderToDao);
      }
   });
};


module.exports = cloudletApi;
@@ -819,16 +819,51 @@
 arams);%0A
+   rrd.startAPI(config.monitoring)%0A
    zmq.a

Fix for openi_rrd issue