public function check_consistency(EtvaNode $etva_node, $sync_node_vgs)
 {
     $errors = array();
     $etva_node->getId();
     $etva_cluster = $etva_node->getEtvaCluster();
     // get node database VGs
     $node_vgs = $etva_node->getEtvaNodeVolumegroups();
     // get shared database VGs
     $shared_vgs = $etva_cluster->getSharedVgs();
     $node_inconsistent = 0;
     foreach ($node_vgs as $rvg) {
         $vg = $rvg->getEtvaVolumegroup();
         $error = array();
         $uuid = $vg->getUuid();
         $vgname = $vg->getVg();
         // init
         $inconsistent = 0;
         // look at volume groups list
         $found_lvm = 0;
         foreach ($sync_node_vgs as $hvg) {
             $arr_hvg = (array) $hvg;
             if ($arr_hvg[EtvaVolumegroup::STORAGE_TYPE_MAP] == $vg->getStorageType()) {
                 if (isset($arr_hvg[EtvaVolumegroup::UUID_MAP])) {
                     if ($arr_hvg[EtvaVolumegroup::UUID_MAP] == $uuid) {
                         $found_lvm = 1;
                     }
                 } else {
                     if ($arr_hvg[EtvaVolumegroup::VG_MAP] == $vgname) {
                         $found_lvm = 1;
                     }
                 }
             }
         }
         $inconsistent = $inconsistent || !$found_lvm;
         if (!$found_lvm) {
             $error['not_found_lvm'] = 1;
         }
         $found_shared_lvm = 0;
         $inconsistent_shared_vgs = 0;
         foreach ($shared_vgs as $s_vg) {
             $s_uuid = $s_vg->getUuid();
             if ($s_uuid == $uuid) {
                 if ($s_vg->getStorageType() != $vg->getStorageType()) {
                     $inconsistent_shared_vgs = 1;
                 } else {
                     $found_shared_lvm = 1;
                 }
             } else {
                 if ($s_vg->getStorageType() == $vg->getStorageType()) {
                     if ($arr_hvg[EtvaVolumegroup::VG_MAP] == $vgname) {
                         $found_shared_lvm = 1;
                     }
                 }
             }
         }
         if ($vg->getStorageType() != EtvaVolumegroup::STORAGE_TYPE_LOCAL_MAP) {
             $inconsistent = $inconsistent || !$found_shared_lvm;
             if (!$found_shared_lvm) {
                 $error['not_found_shared_lvm'] = 1;
             }
         }
         $inconsistent = $inconsistent || $inconsistent_shared_vgs;
         if ($inconsistent_shared_vgs) {
             $error['diff_storage_type'] = 1;
         }
         // update data-base
         $etva_volumegroup = EtvaVolumegroupPeer::retrieveByNodeTypeUUIDVg($etva_node->getId(), $vg->getStorageType(), $vg->getUuid(), $vg->getVg());
         if ($etva_volumegroup) {
             $etva_volumegroup->setInconsistent($inconsistent);
             $etva_volumegroup->save();
         }
         $etva_node_volumegroup = EtvaNodeVolumegroupPeer::retrieveByPK($etva_node->getId(), $vg->getId());
         if ($etva_node_volumegroup) {
             $etva_node_volumegroup->setInconsistent($inconsistent);
             $etva_node_volumegroup->save();
         }
         if ($inconsistent) {
             $message = sfContext::getInstance()->getI18N()->__(EtvaVolumegroupPeer::_ERR_INCONSISTENT_, array('%info%' => sprintf('volume group "%s" with uuid "%s"', $vg->getVg(), $vg->getUuid())));
             if ($error['not_found_shared_lvm'] || $error['diff_storage_type']) {
                 $message = sfContext::getInstance()->getI18N()->__(EtvaVolumegroupPeer::_ERR_SHARED_INCONSISTENT_, array('%info%' => sprintf('volume group "%s" with uuid "%s"', $vg->getVg(), $vg->getUuid())));
             }
             sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($etva_node->getName(), 'event.log', array('message' => $message, 'priority' => EtvaEventLogger::ERR)));
             $error['node'] = array('name' => $etva_node->getName(), 'id' => $etva_node->getId(), 'uuid' => $etva_node->getUuid());
             $error['volumegroup'] = $vg->getVg();
             $error['uuid'] = $vg->getUuid();
             $error['message'] = $message;
         }
         if (!empty($error)) {
             $errors[] = $error;
         }
         $node_inconsistent = $node_inconsistent || $inconsistent;
     }
     // check consistency for shared Volume groups
     foreach ($shared_vgs as $vg) {
         $error = array();
         $uuid = $vg->getUuid();
         $vgname = $vg->getVg();
         // init
         $inconsistent = 0;
         // look at physical volumes list
         $found_lvm = 0;
         foreach ($sync_node_vgs as $hvg) {
             $arr_hvg = (array) $hvg;
             if ($arr_hvg[EtvaVolumegroup::STORAGE_TYPE_MAP] == $vg->getStorageType()) {
                 if (isset($arr_hvg[EtvaVolumegroup::UUID_MAP])) {
                     if ($arr_hvg[EtvaVolumegroup::UUID_MAP] == $uuid) {
                         $found_lvm = 1;
                     }
                 } else {
                     if ($arr_hvg[EtvaVolumegroup::VG_MAP] == $vgname) {
                         $found_lvm = 1;
                     }
                 }
             }
         }
         $inconsistent = $inconsistent || !$found_lvm;
         if (!$found_lvm) {
             $error['not_found_lvm'] = 1;
         }
         // update data-base
         $etva_volumegroup = EtvaVolumegroupPeer::retrieveByNodeTypeUUIDVg($etva_node->getId(), $vg->getStorageType(), $vg->getUuid(), $vg->getVg());
         if ($etva_volumegroup) {
             $etva_volumegroup->setInconsistent($inconsistent);
             $etva_volumegroup->save();
         }
         $etva_node_volumegroup = EtvaNodeVolumegroupPeer::retrieveByPK($etva_node->getId(), $vg->getId());
         if ($etva_node_volumegroup) {
             $etva_node_volumegroup->setInconsistent($inconsistent);
             $etva_node_volumegroup->save();
         }
         if ($inconsistent) {
             $message = sfContext::getInstance()->getI18N()->__(EtvaVolumegroupPeer::_ERR_SHARED_INCONSISTENT_, array('%info%' => sprintf('volumegroup "%s" with uuid "%s"', $vg->getVg(), $vg->getUuid())));
             sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($etva_node->getName(), 'event.log', array('message' => $message, 'priority' => EtvaEventLogger::ERR)));
             $error['node'] = array('name' => $etva_node->getName(), 'id' => $etva_node->getId(), 'uuid' => $etva_node->getUuid());
             $error['volumegroup'] = $vg->getVg();
             $error['uuid'] = $vg->getUuid();
             $error['message'] = $message;
         }
         if (!empty($error)) {
             $errors[] = $error;
         }
         $node_inconsistent = $node_inconsistent || $inconsistent;
     }
     $return = array();
     if ($node_inconsistent) {
         $etva_node->setErrorMessage(self::VGINIT);
         $return = array('success' => false, 'errors' => $errors);
     } else {
         $etva_node->clearErrorMessage(self::VGINIT);
         $return = array('success' => true);
     }
     return $return;
 }
 public function initialize(EtvaNode $etva_node, $lvs, $dtable, $bulk_dtable)
 {
     $etva_cluster = $etva_node->getEtvaCluster();
     $logical_names = array();
     $errors = array();
     /*
      * check lv consistency
      */
     $check_res = $this->check_consistency($etva_node, $lvs, $dtable, $bulk_dtable);
     if (!$check_res['success']) {
         $errors = $check_res['errors'];
         $inconsistent_message = sfContext::getInstance()->getI18N()->__(EtvaLogicalvolumePeer::_ERR_INCONSISTENT_, array('%info%' => ''));
         $etva_node->setErrorMessage(self::LVINIT);
         $message = Etva::getLogMessage(array('info' => $inconsistent_message), EtvaLogicalvolumePeer::_ERR_SOAPUPDATE_);
         sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($etva_node->getName(), 'event.log', array('message' => $message, 'priority' => EtvaEventLogger::ERR)));
         // dont stop process
         //return array('success'=>false,'error'=>$errors);
     }
     /*
      * check shared lvs consistency
      */
     /*$consist = $this->check_shared_consistency($etva_node,$lvs);        
             $consist_dtable = $this->check_shared_devicetable_consistency($etva_node,$dtable,$bulk_dtable);
     
             if(!$consist || !$consist_dtable){
                 $errors = $this->get_missing_lv_devices($etva_node);
                 $inconsistent_message = $errors ? $errors['message'] :
                                 sfContext::getInstance()->getI18N()->__(EtvaLogicalvolumePeer::_ERR_INCONSISTENT_,array('%info%'=>'initialize'));
     
                 $etva_node->setErrorMessage(self::LVINIT);
     
                 $message = Etva::getLogMessage(array('info'=>$inconsistent_message), EtvaLogicalvolumePeer::_ERR_SOAPUPDATE_);
                 sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($etva_node->getName(),'event.log',array('message' =>$message,'priority'=>EtvaEventLogger::ERR)));
     
                 // dont stop process
                 //return array('success'=>false,'error'=>$errors);
             }*/
     foreach ($lvs as $lvInfo) {
         if (!empty($lvInfo)) {
             $lv_info = (array) $lvInfo;
             // set mounted 0 when not mounted
             if (!isset($lv_info[EtvaLogicalvolume::MOUNTED_MAP])) {
                 $lv_info[EtvaLogicalvolume::MOUNTED_MAP] = 0;
             }
             //error_log("device " . $lv_info[EtvaLogicalvolume::LVDEVICE_MAP] . " EtvaLogicalvolume::MOUNTED_MAP " . $lv_info[EtvaLogicalvolume::MOUNTED_MAP]);
             $lv_dev = $lv_info[EtvaLogicalvolume::LVDEVICE_MAP];
             $lv_type = $lv_info[EtvaLogicalvolume::STORAGE_TYPE_MAP];
             $lv_uuid = isset($lv_info[EtvaLogicalvolume::UUID_MAP]) ? $lv_info[EtvaLogicalvolume::UUID_MAP] : '';
             // vg info
             $vg_info = (array) $lv_info[EtvaLogicalvolume::VOLUMEGROUP_MAP];
             $vg_name = $vg_info[EtvaVolumegroup::VG_MAP];
             $vg_type = $vg_info[EtvaVolumegroup::STORAGE_TYPE_MAP];
             $vg_uuid = isset($vg_info[EtvaVolumegroup::UUID_MAP]) ? $vg_info[EtvaVolumegroup::UUID_MAP] : '';
             //get volume group based on node, type, uuid and vg
             $etva_volgroup = EtvaVolumegroupPeer::retrieveByNodeTypeUUIDVg($etva_node->getId(), $vg_type, $vg_uuid, $vg_name);
             if ($etva_volgroup) {
                 if ($lv_type == EtvaLogicalvolume::STORAGE_TYPE_LOCAL_MAP) {
                     $etva_logicalvol = EtvaLogicalvolumePeer::retrieveByNodeTypeLvDevice($etva_node->getId(), $lv_type, $lv_dev);
                 } else {
                     $etva_logicalvol = EtvaLogicalvolumePeer::retrieveByUUID($lv_uuid);
                 }
                 if (!$etva_logicalvol) {
                     // no lv in db...so create new one
                     $etva_node_logicalvol = new EtvaNodeLogicalvolume();
                     $etva_logicalvol = new EtvaLogicalvolume();
                 } else {
                     //if lv already in DB we need to make sure if already exists association with node. if not create new one
                     $etva_node_logicalvol = EtvaNodeLogicalvolumePeer::retrieveByPK($etva_node->getId(), $etva_logicalvol->getId());
                     if (!$etva_node_logicalvol) {
                         $etva_node_logicalvol = new EtvaNodeLogicalvolume();
                     }
                 }
                 $etva_logicalvol->initData($lv_info);
                 $etva_logicalvol->setEtvaVolumegroup($etva_volgroup);
                 $etva_logicalvol->setEtvaCluster($etva_cluster);
                 $etva_logicalvol->save();
                 $etva_node_logicalvol->setEtvaLogicalvolume($etva_logicalvol);
                 $etva_node_logicalvol->setEtvaNode($etva_node);
                 $etva_node_logicalvol->save();
                 $logical_names[] = $etva_logicalvol->getLv();
             }
         }
     }
     if (!empty($errors)) {
         // if have some errors, return it
         return array('success' => false, 'error' => $errors);
     } else {
         /*
          * check if is an appliance restore operation...
          */
         $apli = new Appliance();
         $action = $apli->getStage(Appliance::RESTORE_STAGE);
         if ($action) {
             $apli->setStage(Appliance::RESTORE_STAGE, Appliance::VA_UPDATE_LVS);
         }
         $etva_node->clearErrorMessage(self::LVINIT);
         $message = Etva::getLogMessage(array('info' => implode(', ', $logical_names)), EtvaLogicalvolumePeer::_OK_SOAPUPDATE_);
         sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($etva_node->getName(), 'event.log', array('message' => $message)));
         return array('success' => true, 'response' => $logical_names);
     }
 }