Конвертер: шлюзы at-8000 в устройства INET
Материал из BiTel WiKi
(Различия между версиями)
Stark (Обсуждение | вклад)
(Новая страница: «Изначально шлюзы расположены были в виде дерева Локальные Шлюзы Папка улицы комм…»)
(Новая страница: «Изначально шлюзы расположены были в виде дерева Локальные Шлюзы Папка улицы комм…»)
Текущая версия на 10:39, 20 декабря 2012
Изначально шлюзы расположены были в виде дерева
Локальные Шлюзы Папка улицы коммутатор 1 коммутатор 2
Примерно такую же структуру имеют полученные устройства inet. Конвертер переносит также адрес шлюза в атрибут устройства inet.
Код конвертера:
package ru.maglan.bgbilling.script.global; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import com.sun.tools.ws.wsdl.framework.Entity; import ru.bitel.bgbilling.common.BGException; import ru.bitel.bgbilling.kernel.container.managed.ServerContext; import ru.bitel.bgbilling.kernel.script.server.dev.GlobalScriptBase; import ru.bitel.bgbilling.modules.inet.api.common.bean.InetDevice; import ru.bitel.bgbilling.modules.inet.api.common.bean.InetDeviceType; import ru.bitel.bgbilling.modules.inet.api.common.service.InetDeviceService; import ru.bitel.bgbilling.server.util.ServerUtils; import ru.bitel.bgbilling.server.util.Setup; import ru.bitel.common.Preferences; import ru.bitel.common.sql.ConnectionSet; import ru.bitel.oss.kernel.entity.common.bean.EntityAttr; import ru.bitel.oss.kernel.entity.common.bean.EntityAttrAddress; import ru.bitel.oss.systems.inventory.resource.common.DeviceInterfaceService; import ru.bitel.oss.systems.inventory.resource.common.bean.Device; import ru.bitel.oss.systems.inventory.resource.common.bean.DeviceInterface; public class ConvertIPNGatesToInet extends GlobalScriptBase { //код модуля IPN private static int IPN_MODULE_ID = 1; //корень устройства в IPN private static int IPN_ROOT_DEVICE_ID = 4; //тип шлюза dlink в IPN private static int IPN_DLINK_GATE_TYPE_ID = 24; //код модуля Inet private static int INET_MODULE_ID = 18; //код атрбута для адреса в модуле inet private static int INET_DEVICE_ENTITY_SPEC_ID = 1; //код устройства Dlink в Inet private static int INET_DLINK_DEVICE_TYPE_ID = 3; //private static int INET_AT8000_383_PORT_DEVICE_TYPE_ID = 7; //private static int INET_AT8000_5009_PORT_DEVICE_TYPE_ID = 8; //код корня дерева устройств (Адреса локальных шлюзов в модуле Inet) private static int INET_ROOT_DEVICE_ID = 5; private InetDeviceService wsDevice = null;; private DeviceInterfaceService wsDevInterService = null; Logger logger = Logger.getLogger( ConvertIPNGatesToInet.class ); @Override public void execute( Setup setup, ConnectionSet connectionSet ) throws Exception { print ( "connectionSet.getAutoCommit()=" + connectionSet.getAutoCommit() ); ServerContext context = ServerContext.get(); ; wsDevice = context.getService( InetDeviceService.class, INET_MODULE_ID ); wsDevInterService = context.getService( DeviceInterfaceService.class, INET_MODULE_ID ); createDevices( connectionSet ); //connectionSet.commit(); print ( "Ok" ); } private void createDevices( ConnectionSet connectionSet ) throws BGException, SQLException { Map<Integer, Integer> ipnInetFolderIdMap = new HashMap<Integer, Integer>(); createFolders( connectionSet, ipnInetFolderIdMap ); createDlinkDevices( connectionSet, ipnInetFolderIdMap ); } private void createDlinkDevices( ConnectionSet connectionSet, Map<Integer, Integer> ipnInetFolderIdMap ) throws BGException, SQLException { List<InetDevice> inetDevicelist = new ArrayList<InetDevice>(); inetDevicelist.addAll( wsDevice.deviceList( INET_DLINK_DEVICE_TYPE_ID ) ); //inetDevicelist.addAll( wsDevice.deviceList( INET_AT8000_383_PORT_DEVICE_TYPE_ID ) ); //inetDevicelist.addAll( wsDevice.deviceList( INET_AT8000_5009_PORT_DEVICE_TYPE_ID ) ); Map<String, InetDevice> dlinkDeviceMap = new HashMap<String, InetDevice>(); for ( InetDevice device : inetDevicelist ) { dlinkDeviceMap.put( device.getHost(), device ); } Connection con = connectionSet.getConnection(); String tableName = "ipn_gate_" + IPN_MODULE_ID; String query = " SELECT gate.*, parent.id as parentId, address.hid, address.address, address.pod, address.floor, address.frac FROM " + tableName + " as gate " + " LEFT JOIN " + tableName + " as parent ON parent.id=gate.parent_id" + " LEFT JOIN " + tableName + " as parentParent ON parentParent.id=parent.parent_id" + " LEFT JOIN ipn_gate_address_" + IPN_MODULE_ID + " as address ON address.gid=gate.id " + " WHERE parentParent.id = " + IPN_ROOT_DEVICE_ID + " AND gate.type =" + IPN_DLINK_GATE_TYPE_ID; PreparedStatement ps = con.prepareStatement( query ); ResultSet rs = ps.executeQuery(); int count =0; while ( rs.next() ) { int gid = rs.getInt( "id" ); String host = rs.getString( "host" ); int port = rs.getInt( "port" ); String config = rs.getString( "config" ); String keyword = rs.getString( "keyword" ); int ipnParentId = rs.getInt( "parentId" ); int hid = rs.getInt( "address.hid" ); String address = rs.getString( "address.address" ); int pod = rs.getInt( "address.pod" ); int floor = rs.getInt( "address.floor" ); //String frac = rs.getString( "address.frac" ); logger.info( "gid=" + gid ); /*if ( gid != 52 ) { continue; } */ //print( "gid=" + gid ); String hostPort = host + ":" + port; InetDevice device = dlinkDeviceMap.get( hostPort ); if ( device == null ) { print( "add new device " + host ); device = new InetDevice(); dlinkDeviceMap.put( hostPort, device ); } //print( "device id =" + device.getId() ); device.setHost( hostPort ); device.setSecret( keyword ); int typeId = INET_DLINK_DEVICE_TYPE_ID; //85-7(тип), 352- 8 //private static int INET_AT8000_383_PORT_DEVICE_TYPE_ID = 7; //private static int INET_AT8000_5009_PORT_DEVICE_TYPE_ID = 8; /* switch ( gid ) { case 85 : typeId = INET_AT8000_383_PORT_DEVICE_TYPE_ID; break; case 352 : typeId = INET_AT8000_5009_PORT_DEVICE_TYPE_ID; break; default : typeId = INET_DLINK_DEVICE_TYPE_ID; } */ device.setDeviceTypeId( typeId ); device.setDeviceGroupIds( new HashSet<Integer>() ); //print( "hostPort=" + hostPort ); //print( "ipnParentId=" + ipnParentId ); int parentId = ipnInetFolderIdMap.get( ipnParentId ); device.setParentId( parentId ); device.setPassword( "" ); device.setUsername( "" ); device.setConfig(""); device.setIdentifier( "" ); device.setComment( "" ); device.setDeviceGroupIds( new HashSet<Integer>() ); InetDeviceType type = wsDevice.deviceTypeGet( typeId ); device.setTitle( Device.generateTitle( device, type ) ); //print( "device.getId()=" + device.getId() ); int id = wsDevice.deviceUpdate( device ); connectionSet.commit(); if ( device.getId() <= 0 ) { device.setId( id ); } //print( "111=" + 111 ); //Находим зарезервированный порты Preferences gateSetup = new Preferences( config, "\r\n" ); String portRange = gateSetup.get( "port.range", "1-47" ); //print( "portRange=" + portRange ); List<List<Integer>> rangeList = ServerUtils.getRanges( 0, portRange ); List<DeviceInterface> devicePortList = wsDevInterService.devicePortList( device.getId() ); //print( "2222=" + 111 ); Set<Integer> portMap = new HashSet<Integer>(); for ( DeviceInterface iface : devicePortList ) { portMap.add( iface.getPort() ); } m1: for ( List<Integer> list : rangeList ) { boolean found = false; int startPort = list.get( 0 ); int stopPort = list.get( 1 ); //print( "startPort=" + startPort ); //print( "stopPort=" + stopPort ); for ( int i = startPort; i <= stopPort; i++ ) { if ( !portMap.contains( i ) ) { print ( "port out of range " + host + ";" + startPort + "-" + stopPort); break m1; } } } for ( DeviceInterface iface : devicePortList ) { boolean found = false; //print( "iface=" + iface.getPort() ); for ( List<Integer> list : rangeList ) { int startPort = list.get( 0 ); int stopPort = list.get( 1 ); //print( "startPort=" + startPort ); //print( "stopPort=" + stopPort ); if ( startPort <= iface.getPort() && stopPort >= iface.getPort() ) { found = true; //print( "found" ); break; } } //порт зарезервирован if ( !found && iface.getStatus() != DeviceInterface.STATUS_RESERVED ) { print( "reserve port = " + iface.getPort() + ";host = " + host ); iface.setStatus( DeviceInterface.STATUS_RESERVED ); wsDevInterService.devicePortUpdate( iface ); connectionSet.commit(); } else if ( found && iface.getStatus() != DeviceInterface.STATUS_ACTIVE ) { print( "activated port = " + iface.getPort() + ";host = " + host ); iface.setStatus( DeviceInterface.STATUS_ACTIVE ); wsDevInterService.devicePortUpdate( iface ); connectionSet.commit(); } } //print( "address=" + address ); //адрес EntityAttrAddress addressEnt = new EntityAttrAddress( device.getEntityId(), INET_DEVICE_ENTITY_SPEC_ID, hid, pod, floor, null, "", "", "0", address ); Map<Integer, EntityAttr> entityAttributes = new HashMap<Integer, EntityAttr>(); entityAttributes.put( INET_DEVICE_ENTITY_SPEC_ID, addressEnt ); device.setEntityAttributes( entityAttributes ); //print( "device.getId()=" + device.getId() ); wsDevice.deviceUpdate( device ); connectionSet.commit(); } ps.close(); } private void createFolders( ConnectionSet connectionSet, Map<Integer, Integer> ipnInetFolderIdMap ) throws BGException, SQLException { List<InetDevice> inetDevicelist = new ArrayList<InetDevice>(); inetDevicelist.addAll( wsDevice.deviceList( 0 ) ); Map<String, InetDevice> inetFolderMap = new HashMap<String, InetDevice>(); for ( InetDevice device : inetDevicelist ) { inetFolderMap.put( device.getIdentifier(), device ); } Connection con = connectionSet.getConnection(); String query = " SELECT * FROM ipn_gate_" + IPN_MODULE_ID + " WHERE parent_id = " + IPN_ROOT_DEVICE_ID; PreparedStatement ps = con.prepareStatement( query ); ResultSet rs = ps.executeQuery(); while ( rs.next() ) { int ipnDeviceid = rs.getInt( "id" ); String title = rs.getString( "host" ); InetDevice device = inetFolderMap.get( title ); if ( device == null ) { print( "add new folder " + title ); device = new InetDevice(); inetFolderMap.put( title, device ); } device.setIdentifier( title ); device.setDeviceTypeId( 0 ); device.setParentId( INET_ROOT_DEVICE_ID ); device.setSecret( "" ); device.setHost( "" ); device.setUsername( "" ); device.setPassword( "" ); device.setConfig( "" ); device.setComment( "" ); device.setDeviceGroupIds( new HashSet<Integer>() ); InetDeviceType type = new InetDeviceType( 0, "Папка" ); device.setTitle( Device.generateTitle( device, type ) ); int id = wsDevice.deviceUpdate( device ); if ( device.getId() <= 0 ) { device.setId( id ); } ipnInetFolderIdMap.put( ipnDeviceid, id ); connectionSet.commit(); } ps.close(); } }