/**
  * @param $plugin_ocsinventoryng_ocsservers_id
  * @param $itemtype
  * @param int $ID
  * @param $ocsSnmp
  * @param $loc_id
  * @param $dom_id
  * @param $action
  * @param bool $linked
  * @return int
  */
 static function addOrUpdateNetworkEquipment($plugin_ocsinventoryng_ocsservers_id, $itemtype, $ID = 0, $ocsSnmp, $loc_id, $dom_id, $action, $linked = false)
 {
     global $DB;
     $snmpDevice = new $itemtype();
     $cfg_ocs = PluginOcsinventoryngOcsServer::getConfig($plugin_ocsinventoryng_ocsservers_id);
     $input = array("is_dynamic" => 1, "entities_id" => isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0, "is_recursive" => 0);
     if ($cfg_ocs['importsnmp_name'] && $action == "add" || $cfg_ocs['linksnmp_name'] && $linked || $action == "update" && $cfg_ocs['importsnmp_name'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_name'] && $linked) {
         if ($ocsSnmp['META']['NAME'] != "N/A") {
             $input["name"] = $ocsSnmp['META']['NAME'];
         } else {
             $input["name"] = $ocsSnmp['META']['DESCRIPTION'];
         }
     }
     if ($cfg_ocs['importsnmp_contact'] && $action == "add" || $cfg_ocs['linksnmp_contact'] && $linked || $action == "update" && $cfg_ocs['importsnmp_contact'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_contact'] && $linked) {
         $input["contact"] = $ocsSnmp['META']['CONTACT'];
     }
     if ($cfg_ocs['importsnmp_comment'] && $action == "add" || $cfg_ocs['linksnmp_comment'] && $linked || $action == "update" && $cfg_ocs['importsnmp_comment'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_comment'] && $linked) {
         $input["comment"] = $ocsSnmp['META']['DESCRIPTION'];
     }
     if ($loc_id > 0) {
         $input["locations_id"] = $loc_id;
     }
     if ($dom_id > 0) {
         $input["domains_id"] = $dom_id;
     }
     //if($ocsSnmp['META']['TYPE'] == null){
     //   $type_id = self::checkIfExist("NetworkEquipmentType", "Network Device");
     //} else {
     //   $type_id = self::checkIfExist("network", $ocsSnmp['META']['TYPE']);
     //}
     if (!empty($ocsSnmp['SWITCH'])) {
         if ($cfg_ocs['importsnmp_manufacturer'] && $action == "add" || $cfg_ocs['linksnmp_manufacturer'] && $linked || $action == "update" && $cfg_ocs['importsnmp_manufacturer'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_manufacturer'] && $linked) {
             $man_id = Dropdown::importExternal('Manufacturer', PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $ocsSnmp['SWITCH'][0]['MANUFACTURER']));
             $input['manufacturers_id'] = $man_id;
         }
         if ($cfg_ocs['importsnmp_firmware'] && $action == "add" || $cfg_ocs['linksnmp_firmware'] && $linked || $action == "update" && $cfg_ocs['importsnmp_firmware'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_firmware'] && $linked) {
             $firm_id = Dropdown::importExternal('NetworkEquipmentFirmware', PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $ocsSnmp['SWITCH'][0]['FIRMVERSION']));
             $input['networkequipmentfirmwares_id'] = $firm_id;
         }
         if ($cfg_ocs['importsnmp_serial'] && $action == "add" || $cfg_ocs['linksnmp_serial'] && $linked || $action == "update" && $cfg_ocs['importsnmp_serial'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_serial'] && $linked) {
             $input['serial'] = $ocsSnmp['SWITCH'][0]['SERIALNUMBER'];
         }
         //TODOSNMP = chassis ??
         //$mod_id = Dropdown::importExternal('NetworkEquipmentModel',
         //PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'],
         //$ocsSnmp['SWITCH'][0]['REFERENCE']));
         //$input['networkequipmentmodels_id'] = $mod_id;
         // TODOSNMP ?
         //$input['networkequipmenttypes_id'] = self::checkIfExist("NetworkEquipmentType", "Switch");
     }
     if (!empty($ocsSnmp['FIREWALLS'])) {
         if ($cfg_ocs['importsnmp_serial'] && $action == "add" || $cfg_ocs['linksnmp_serial'] && $action == $linked || $action == "update" && $cfg_ocs['importsnmp_serial'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_serial'] && $linked) {
             $input['serial'] = $ocsSnmp['FIREWALLS']['SERIALNUMBER'];
         }
         // TODOSNMP ?
         //$input['networkequipmenttypes_id'] = self::checkIfExist("NetworkEquipmentType", "Firewall");
     }
     $id_network = 0;
     if ($action == "add") {
         $id_network = $snmpDevice->add($input, array('unicity_error_message' => true), $cfg_ocs['history_hardware']);
     } else {
         $input["id"] = $ID;
         $id_network = $ID;
         if ($snmpDevice->getFromDB($id_network)) {
             $input["entities_id"] = $snmpDevice->fields['entities_id'];
         }
         $snmpDevice->update($input, $cfg_ocs['history_hardware'], array('unicity_error_message' => false, '_no_history' => !$cfg_ocs['history_hardware']));
     }
     if ($id_network > 0) {
         if (isset($ocsSnmp['POWERSUPPLIES']) && ($cfg_ocs['importsnmp_power'] && $action == "add" || $cfg_ocs['linksnmp_power'] && $linked || $action == "update" && $cfg_ocs['importsnmp_power'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_power'] && $linked) && count($ocsSnmp['POWERSUPPLIES']) > 0) {
             $man_id = Dropdown::importExternal('Manufacturer', PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $ocsSnmp['POWERSUPPLIES'][0]['MANUFACTURER']));
             $pow['manufacturers_id'] = $man_id;
             $pow['designation'] = $ocsSnmp['POWERSUPPLIES'][0]['REFERENCE'];
             $pow['comment'] = $ocsSnmp['POWERSUPPLIES'][0]['DESCRIPTION'];
             $item = new $itemtype();
             $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
             if ($item->getFromDB($id_network)) {
                 $entity = $item->fields['entities_id'];
             }
             $pow['entities_id'] = $entity;
             $power = new DevicePowerSupply();
             $power_id = $power->import($pow);
             if ($power_id) {
                 $serial = $ocsSnmp['POWERSUPPLIES'][0]['SERIALNUMBER'];
                 $CompDevice = new Item_DevicePowerSupply();
                 if ($cfg_ocs['history_devices']) {
                     $table = getTableForItemType("Item_DevicePowerSupply");
                     $query = "DELETE\n                            FROM `" . $table . "`\n                            WHERE `items_id` = '" . $id_network . "'\n                            AND `itemtype` = '" . $itemtype . "'";
                     $DB->query($query);
                 }
                 //            CANNOT USE BEFORE 9.1.2 - for _no_history problem
                 //               $CompDevice->deleteByCriteria(array('items_id' => $id_network,
                 //                  'itemtype' => $itemtype), 1);
                 $CompDevice->add(array('items_id' => $id_network, 'itemtype' => $itemtype, 'entities_id' => $entity, 'serial' => $serial, 'devicepowersupplies_id' => $power_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
             }
         }
         if (isset($ocsSnmp['FANS']) && ($cfg_ocs['importsnmp_fan'] && $action == "add" || $cfg_ocs['linksnmp_fan'] && $linked || $action == "update" && $cfg_ocs['importsnmp_fan'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_fan'] && $linked) && count($ocsSnmp['FANS']) > 0) {
             $man_id = Dropdown::importExternal('Manufacturer', PluginOcsinventoryngOcsServer::encodeOcsDataInUtf8($cfg_ocs['ocs_db_utf8'], $ocsSnmp['FANS'][0]['MANUFACTURER']));
             $dev['manufacturers_id'] = $man_id;
             $dev['designation'] = $ocsSnmp['FANS'][0]['REFERENCE'];
             $dev['comment'] = $ocsSnmp['FANS'][0]['DESCRIPTION'];
             $item = new $itemtype();
             $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
             if ($item->getFromDB($id_network)) {
                 $entity = $item->fields['entities_id'];
             }
             $dev['entities_id'] = $entity;
             $device = new DevicePci();
             $device_id = $device->import($dev);
             if ($device_id) {
                 $CompDevice = new Item_DevicePci();
                 if ($cfg_ocs['history_devices']) {
                     $table = getTableForItemType("Item_DevicePci");
                     $query = "DELETE\n                            FROM `" . $table . "`\n                            WHERE `items_id` = '" . $id_network . "'\n                            AND `itemtype` = '" . $itemtype . "'";
                     $DB->query($query);
                 }
                 //            CANNOT USE BEFORE 9.1.2 - for _no_history problem
                 //               $CompDevice->deleteByCriteria(array('items_id' => $id_network,
                 //                  'itemtype' => $itemtype), 1);
                 $CompDevice->add(array('items_id' => $id_network, 'itemtype' => $itemtype, 'entities_id' => $entity, 'devicepcis_id' => $device_id, 'is_dynamic' => 1), array(), $cfg_ocs['history_devices']);
             }
         }
     }
     if ($id_network > 0 && ($cfg_ocs['importsnmp_createport'] && $action == "add" || $cfg_ocs['linksnmp_createport'] && $linked || $action == "update" && $cfg_ocs['importsnmp_createport'] && !$linked || $action == "update" && $cfg_ocs['linksnmp_createport'] && $linked)) {
         //Add network port
         $ip = $ocsSnmp['META']['IPADDR'];
         $mac = $ocsSnmp['META']['MACADDR'];
         $np = new NetworkPort();
         $np->getFromDBByQuery("WHERE `mac` LIKE '{$mac}' AND `items_id` = '{$id_network}' AND `itemtype` LIKE '{$itemtype}' ");
         if (count($np->fields) < 1) {
             $item = new $itemtype();
             $entity = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : 0;
             if ($item->getFromDB($id_network)) {
                 $entity = $item->fields['entities_id'];
             }
             $port_input = array('name' => $ocsSnmp['META']['NAME'], 'mac' => $mac, 'items_id' => $id_network, 'itemtype' => $itemtype, 'instantiation_type' => "NetworkPortEthernet", "entities_id" => $entity, "NetworkName__ipaddresses" => array("-100" => $ip), '_create_children' => 1, 'is_deleted' => 0);
             $np->add($port_input, array(), $cfg_ocs['history_network']);
         }
     }
     return $id_network;
 }