1 25 package org.jrobin.mrtg.server; 26 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 import java.io.IOException ; 33 import java.util.Hashtable ; 34 import java.util.Map ; 35 import java.util.Vector ; 36 37 class Device { 38 private String host = ""; 39 private String community = ""; 40 private String descr = ""; 41 private boolean active = true; 42 private Vector links = new Vector (); 43 44 Device() { } 45 46 Device(Node routerNode) { 47 NodeList nodes = routerNode.getChildNodes(); 48 for(int i = 0; i < nodes.getLength(); i++) { 49 Node node = nodes.item(i); 50 String name = node.getNodeName(); 51 Node firstChild = node.getFirstChild(); 52 String value = (firstChild != null)? firstChild.getNodeValue().trim(): null; 53 if(name.equals("host")) { 54 setHost(value); 55 } 56 else if(name.equals("community")) { 57 setCommunity(value); 58 } 59 else if(name.equals("description")) { 60 setDescr(value); 61 } 62 else if(name.equals("active")) { 63 setActive(new Boolean (node.getFirstChild().getNodeValue().trim()).booleanValue()); 64 } 65 else if(name.equals("interface")) { 66 links.add(new Port(node)); 67 } 68 } 69 } 70 71 String getHost() { 72 return host; 73 } 74 75 void setHost(String host) { 76 if(host != null) { 77 this.host = host; 78 } 79 } 80 81 String getCommunity() { 82 return community; 83 } 84 85 void setCommunity(String community) { 86 if(community != null) { 87 this.community = community; 88 } 89 } 90 91 String getDescr() { 92 return descr; 93 } 94 95 void setDescr(String descr) { 96 if(descr != null) { 97 this.descr = descr; 98 } 99 } 100 101 boolean isActive() { 102 return active; 103 } 104 105 boolean getActive() { 106 return active; 107 } 108 109 void setActive(boolean active) { 110 this.active = active; 111 } 112 113 Vector getLinks() { 114 return links; 115 } 116 117 void setLinks(Vector links) { 118 this.links = links; 119 } 120 121 public String toString() { 122 String buff = new String (); 123 buff += "Router: " + host + " -- " + "community=" + community + ", "; 124 buff += "descr=" + descr + ", "; 125 buff += "active=" + active + "\n"; 126 for(int i = 0; i < links.size(); i++) { 128 Port link = (Port) links.get(i); 129 buff += " Link: " + link + "\n"; 130 } 131 return buff; 132 } 133 134 Port getLinkByIfDescr(String ifDescr) { 135 for(int i = 0; i < links.size(); i++) { 136 Port link = (Port) links.get(i); 137 if(link.getIfDescr().equalsIgnoreCase(ifDescr)) { 138 return link; 139 } 140 } 141 return null; 142 } 143 144 void addLink(Port link) { 145 links.add(link); 146 } 147 148 void removeLink(Port link) { 149 links.remove(link); 150 } 151 152 int getLinkCount() { 153 return links.size(); 154 } 155 156 String [] getAvailableLinks() throws IOException { 157 Poller comm = null; 158 try { 159 comm = new Poller(host, community); 160 Map links = comm.walkIfDescr(); 161 return (String []) links.values().toArray(new String [0]); 162 } 163 finally { 164 if(comm != null) { 165 comm.close(); 166 } 167 } 168 } 169 170 Hashtable getRouterInfo() { 171 Hashtable table = new Hashtable (); 172 table.put("host", host); 173 table.put("community", community); 174 table.put("descr", descr); 175 table.put("active", new Boolean (active)); 176 Vector linkData = new Vector (); 178 for (int i = 0; i < links.size(); i++) { 179 Port link = (Port) links.get(i); 180 linkData.add(link.getLinkInfo()); 181 } 182 table.put("links", linkData); 183 return table; 184 } 185 186 void appendXml(Element root) { 187 Document doc = root.getOwnerDocument(); 188 Element routerElem = doc.createElement("router"); 189 root.appendChild(routerElem); 190 Element hostElem = doc.createElement("host"); 191 hostElem.appendChild(doc.createTextNode(host)); 192 routerElem.appendChild(hostElem); 193 Element commElem = doc.createElement("community"); 194 commElem.appendChild(doc.createTextNode(community)); 195 routerElem.appendChild(commElem); 196 Element descrElem = doc.createElement("description"); 197 descrElem.appendChild(doc.createTextNode(descr)); 198 routerElem.appendChild(descrElem); 199 Element activeElem = doc.createElement("active"); 200 activeElem.appendChild(doc.createTextNode("" + active)); 201 routerElem.appendChild(activeElem); 202 for (int i = 0; i < links.size(); i++) { 203 Port link = (Port) links.elementAt(i); 204 link.appendXml(routerElem); 205 } 206 } 207 } | Popular Tags |