⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gwporthandler.java

📁 Network Administration Visualized 网络管理可视化源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
									"vlanid", vl.getVlanidS(),								};								prefixid = Database.insert("prefix", ins, null);								oldp = getPrefix(p.getCidr());								newPrefixCnt++;							} else {								prefixid = oldp.getPrefixidS();							}							p.setPrefixid(prefixid);							p.addGwports(oldp);														Gwportprefix oldgp = getGwportprefix(gp.getGwip());							if (oldgp == null) {								// The prefix exists, but there is no old gwportprefix with the same gwip								Log.d("NEW GWPORTPREFIX", "Gwportprefix: " + gp);															String[] ins = {									"gwportid", gwportid,									"prefixid", p.getPrefixidS(),									"gwip", gp.getGwip(),									"hsrp", gp.getHsrp()?"t":"f"								};								Database.insert("gwportprefix", ins);							} else {								if (gp != oldgp || p != oldp) {									// gwip moved to different gwport / prefix changed									String[] set = {										"gwportid", gwportid,										"prefixid", prefixid,										"hsrp", gp.getHsrp()?"t":"f",									};									String[] where = {										"gwip", gp.getGwip()									};									Database.update("gwportprefix", set, where);								} else {																		if (!gp.equalsGwportprefix(oldgp)) {										// Update gwportprefix										Log.d("UPDATE_GWPORTPREFIX", "Update gwportprefix " + gp);										String[] set = {											"hsrp", gp.getHsrp()?"t":"f",										};										String[] where = {											"gwip", gp.getGwip()										};										Database.update("gwportprefix", set, where);										fixupPrefix = true;									}								}							}							boolean unknownNettype = vl.getNettype().equals(Vlan.UNKNOWN_NETTYPE);														Vlan dbvl = oldp.getVlan();							if (vl.getVlan() == null || vl.getVlan().equals(dbvl.getVlan())) {								if (!vl.equalsVlan(dbvl)) {									// Update vlan and dbvl object									if (!unknownNettype || !vl.equalsDataVlan(dbvl)) {										if (!vl.equalsOrgid(dbvl) || !vl.equalsUsageid(dbvl)) {											if (!vl.equalsOrgid(dbvl)) reportMissingOrgid(vl);											if (!vl.equalsUsageid(dbvl)) reportMissingUsageid(vl);										} else {											Log.d("UPDATE_VLAN", "Update vlan " + vl + " db: " + dbvl);										}										vl.setVlanid(dbvl.getVlanid());										updateVlan(vl, unknownNettype);										fixupPrefix = true;									}									dbvl.setEqual(vl);								}								vl = dbvl;								p.setVlan(vl);							} else {								// We must create a new vlan								String vlKey = nb.getNetboxid()+":"+vl.getVlanS();								if (gwVlanMap.containsKey(vlKey)) {									Vlan gwvl = (Vlan)gwVlanMap.get(vlKey);									vl.setVlanid(gwvl.getVlanid());								} else {									createVlan(vl);									gwVlanMap.put(vlKey, vl);									fixupPrefix = true;								}							}							if (!p.equalsPrefix(oldp)) {								// Update prefix								Log.d("UPDATE_PREFIX", "Update prefix " + p + " (db: " + oldp + ")");								String[] set = {									"vlanid", vl.getVlanidS(),								};								String[] where = {									"prefixid", prefixid,								};								Database.update("prefix", set, where);								fixupPrefix = true;							}							// This gwport now points to this prefix							p.addGwport(gwportid);													// Add prefix for autodetermination of nettype							if (unknownNettype) {								prefixMap.put(p.getCidr(), p);								prefixUpdateSet.add(p.getCidr());							} else if (vl.getNettype().equals("elink")) {								setElinkNetident(nb, p);								updateVlan(vl, false);							}						}					}				}				// Remove gwports missing				if (!gwportMap.isEmpty()) {					StringBuffer sb = new StringBuffer();					for (Iterator it = gwportMap.values().iterator(); it.hasNext();) {						Gwport gwp = (Gwport)it.next();						sb.append(gwp.getGwportidS()+",");					}					sb.deleteCharAt(sb.length()-1);					Database.update("DELETE FROM gwport WHERE gwportid IN ("+sb.toString()+")");					fixupPrefix = true;				}				if (!removeGwipMap.isEmpty()) {					fixupPrefix = true;					removeGwips(removeGwipMap, gc.isStaticCommited());				}				// Do autodetermination of nettype				if (!prefixUpdateSet.isEmpty()) {					// gwportid -> netboxid + interface					Map gwportidMap = new HashMap();					// Fetch info for all required gwportids					Set gwportidUpdateSet = new HashSet();					for (Iterator it = prefixUpdateSet.iterator(); it.hasNext();) {						Prefix p = (Prefix)getPrefix((String)it.next());						p.addGwportsTo(gwportidUpdateSet);					}					ResultSet rs = Database.query("SELECT gwport.gwportid,sysname,ifindex,interface,hsrp FROM netbox JOIN module USING(netboxid) JOIN gwport USING(moduleid) LEFT JOIN gwportprefix ON (gwport.gwportid=gwportprefix.gwportid AND hsrp='t') WHERE gwport.gwportid IN (" + join(gwportidUpdateSet) + ")");					while (rs.next()) {						gwportidMap.put(rs.getString("gwportid"), new String[] { rs.getString("sysname"), rs.getString("interface"), String.valueOf(rs.getBoolean("hsrp")), rs.getString("ifindex") });					}					for (Iterator it = prefixUpdateSet.iterator(); it.hasNext();) {						String DOMAIN_SUFFIX = navCp.get("DOMAIN_SUFFIX");						Prefix p = (Prefix)getPrefix((String)it.next());						Vlan vl = p.getVlan();						String nettype, netident = null;						int numGwp = p.gwportCount();											Set sysnameSet = new HashSet();						int hsrpCnt = 0;						for (Iterator gwIt = p.getGwportidIterator(); gwIt.hasNext();) {							String[] s = (String[])gwportidMap.get(gwIt.next());							sysnameSet.add(s[0]);							boolean hsrp = s[2].startsWith("t");							if (hsrp) hsrpCnt++;						}						//System.err.println("Prefix: " + p + ", num: " + numGwp + ", hsrpCnt: " + hsrpCnt + ", sysnames: " + sysnameSet);												if (numGwp == 0) {							// This can't happen							System.err.println("Prefix without any gwports: " + p);							Log.e("HANDLE", "Prefix without any gwports, this cannot happen, contact nav support!");							Log.d("HANDLE", "Prefix without any gwports: " + p + ", vlan: " + vl);							continue;						}						// Only one gwport = loopback, elink or lan (default)						else if (numGwp == 1) {							String interf = ((String[])gwportidMap.get(p.getGwportidIterator().next()))[1];							if (interf.toLowerCase().indexOf("loopback") >= 0) {								nettype = "loopback";							} else if (p.getMasklen() == 30) {								nettype = "elink";								setElinkNetident(nb, p);							} else {								nettype = "lan";							}						}						// Two different routers + hsrp = lan						else if (hsrpCnt > 0 && sysnameSet.size() == 2) {							nettype = "lan";						}						// Two gwports from different routers = link						else if (numGwp == 2 && sysnameSet.size() == 2 && hsrpCnt == 0) {							nettype = "link";							Iterator sIt = sysnameSet.iterator();							if (vl.getConvention() == Vlan.CONVENTION_NTNU) {								netident = util.remove((String)sIt.next(), DOMAIN_SUFFIX) + "," + util.remove((String)sIt.next(), DOMAIN_SUFFIX);							}						}						// More than two gwports without hsrp = core						else {							nettype = "core";						}						/*						// More than one gwport + hsrp = lan						else if (hsrpCnt > 0 && numGwp > 1) {						nettype = "lan";						}						// Two gwports from different routers = link						else if (numGwp == 2 && sysnameSet.size() == 2) {						nettype = "link";						Iterator sIt = sysnameSet.iterator();						netident = sIt.next()+","+sIt.next();						}						// More than two gwports without hsrp = core						else {						nettype = "core";						}						*/						Log.d("AUTO_NETTYPE", "Autodetermination of nettype: " + vl + " New: " + nettype);						if (!Vlan.equals(vl.getNettype(), nettype) ||							(netident != null && !netident.equals(vl.getNetident()))) {							vl.setNettype(nettype);							if (netident != null) vl.setNetident(netident);												updateVlan(vl, false);							fixupPrefix = true;							//System.err.println("Update(" + vl.getVlanid() + "): " + vl);						}					}				}				if (newPrefixCnt > 0) {					// Update netbox with new prefixids					// Check overlapping prefices					// select * from prefix a join prefix b on (a.netaddr << b.netaddr) join vlan on (b.vlanid=vlan.vlanid) and nettype not in ('scope','netaddr');					// select netboxid,count(*) from netbox join prefix on (ip << netaddr) natural join vlan where nettype not in ('static','scope') group by netboxid having count(*) > 1;					Database.update("UPDATE netbox SET prefixid = (SELECT prefixid FROM prefix JOIN vlan USING(vlanid) WHERE ip << netaddr AND nettype NOT IN ('scope','static') ORDER BY masklen(netaddr) DESC LIMIT 1) WHERE prefixid IS NULL");				}				if (fixupPrefix) {					int delPrefix = Database.update("DELETE FROM prefix WHERE prefixid NOT IN (SELECT prefixid FROM gwportprefix) AND vlanid NOT IN (SELECT vlanid FROM vlan JOIN swportvlan USING(vlanid) UNION SELECT vlanid FROM vlan WHERE nettype='scope')");					int delVlan = Database.update("DELETE FROM vlan WHERE vlanid NOT IN (SELECT vlanid FROM prefix UNION SELECT vlanid FROM swportvlan UNION SELECT vlanid FROM vlan WHERE nettype='scope')");					Log.d("FIXUP_PREFIX", "Deleted " + delPrefix + " prefices, " + delVlan + " VLANs no longer in use");				}			} catch (SQLException e) {				Log.e("HANDLE", "SQLException: " + e.getMessage());				e.printStackTrace(System.err);			}		}	}	private String join(Collection c) {		StringBuffer sb = new StringBuffer();		for (Iterator it = c.iterator(); it.hasNext();) {			sb.append("'" + it.next() + "',");		}		if (sb.length() > 0) sb.deleteCharAt(sb.length()-1);		return sb.toString();	}	private void reportMissingOrgid(Vlan vl) throws SQLException {		ResultSet rs = Database.query("SELECT orgid FROM org WHERE orgid='"+vl.getOrgid()+"'");		if (!rs.next()) {			Log.d("MISSING_ORGID", "Orgid " + vl.getOrgid() + " is missing for vlan " + vl.getVlan());		}	}	private void reportMissingUsageid(Vlan vl) throws SQLException {		ResultSet rs = Database.query("SELECT usageid FROM usage WHERE usageid='"+vl.getUsageid()+"'");		if (!rs.next()) {			Log.d("MISSING_USAGEID", "Usageid " + vl.getUsageid() + " is missing for vlan " + vl.getVlan());		}	}	private void setElinkNetident(Netbox nb, Prefix p) throws SQLException {		// Try to find elink name from CDP so we can set netident		String DOMAIN_SUFFIX = navCp.get("DOMAIN_SUFFIX");		Vlan vl = p.getVlan();		if (vl.getVlan() != null) {			ResultSet myrs = Database.query("SELECT ifindex FROM swport JOIN module USING(moduleid) WHERE netboxid="+nb.getNetboxid()+" and vlan='"+vl.getVlan()+"'");			if (myrs.next()) {				String remoteCdp = NetboxInfo.getSingleton(nb.getNetboxidS(), "unrecognizedCDP", myrs.getString("ifindex"));				if (remoteCdp != null) vl.setNetident(util.remove(nb.getSysname(), DOMAIN_SUFFIX) + "," + util.remove(remoteCdp, DOMAIN_SUFFIX));			}		}	}	private static void removeGwips(Map removeGwipMap, boolean isStaticCommited) throws SQLException {		// Remove all gwips from gwports where they no longer exist.		for (Iterator it = removeGwipMap.entrySet().iterator(); it.hasNext();) {			Map.Entry me = (Map.Entry)it.next();			String gwportid = (String)me.getKey();			StringBuffer sb = new StringBuffer();			for (Iterator gwipIt = ((Set)me.getValue()).iterator(); gwipIt.hasNext();) {				String rgwip = (String)gwipIt.next();				sb.append("'"+rgwip+"',");			}			sb.deleteCharAt(sb.length()-1);			Database.update("DELETE FROM gwportprefix WHERE gwportid='"+gwportid+"' AND gwip IN ("+sb.toString()+") AND gwip NOT IN (SELECT gwip FROM gwportprefix JOIN prefix USING(prefixid) JOIN vlan USING(vlanid) WHERE nettype='static')");		}		/*		if (hasDeleted) {			Database.update("DELETE FROM gwport WHERE masterindex IS NOT NULL AND gwportid NOT IN (SELECT gwportid FROM gwportprefix)");			Database.update("DELETE FROM gwport WHERE masterindex IS NULL AND ifindex NOT IN (SELECT masterindex FROM gwport WHERE masterindex IS NOT NULL AND moduleid = gwport.moduleid) AND gwportid NOT IN (SELECT gwportid FROM gwportprefix)");		}		*/	}		private static void createVlan(Vlan vl) throws SQLException {		String[] ins = {			"vlanid", "",			"vlan", vl.getVlanS(),			"nettype", vl.getNettype(),			"orgid", "(SELECT orgid FROM org WHERE orgid='"+vl.getOrgid()+"')",			"usageid", "(SELECT usageid FROM usage WHERE usageid='"+vl.getUsageid()+"')",			"netident", vl.getNetident(),			"description", vl.getDescription()		};		String vlanid = Database.insert("vlan", ins, null);		vl.setVlanid(vlanid);	}	private static void updateVlan(Vlan vl, boolean unknownNettype) throws SQLException {		String[] set = {			"nettype", unknownNettype ? null : vl.getNettype(),			"orgid", "(SELECT orgid FROM org WHERE orgid='"+vl.getOrgid()+"')",			"usageid", "(SELECT usageid FROM usage WHERE usageid='"+vl.getUsageid()+"')",			"netident", vl.getNetident(),			"description", vl.getDescription()		};		String[] where = {			"vlanid", vl.getVlanidS()		};		Database.update("vlan", set, where);	}	private static void errl(Object o) {		if (DEBUG_OUT) System.err.println(o);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -