1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""RouteEntry
15
16 RouteEntry represents a group of devices
17 """
18
19 import re
20
21 from Globals import DTMLFile
22 from Globals import InitializeClass
23 from AccessControl import ClassSecurityInfo
24
25 from Products.ZenUtils.Utils import localIpCheck, prepId
26 from Products.ZenRelations.RelSchema import *
27
28
29 from OSComponent import OSComponent
30
31 import logging
32 log = logging.getLogger("zen.IpRouteEntry")
33
34 -def manage_addIpRouteEntry(context, dest, routemask, nexthopid, interface,
35 routeproto, routetype, userCreated=None, REQUEST = None):
36 """
37 Make a IpRouteEntry from the ZMI
38 """
39 if not routemask:
40 routemask = 0
41 else:
42 routemask = int(routemask)
43 dest = '%s/%s' % (dest, routemask)
44 id = prepId(dest)
45 d = IpRouteEntry(id)
46 context._setObject(id, d)
47 d = context._getOb(id)
48 d.setTarget(dest)
49 d.setNextHopIp(nexthopid)
50 d.setInterfaceName(interface)
51 if userCreated: d.setUserCreateFlag()
52 d.routeproto = routeproto
53 d.routetype = routetype
54 d.routemask = routemask
55
56 if REQUEST is not None:
57 REQUEST['RESPONSE'].redirect(context.absolute_url()
58 +'/manage_main')
59
60 addIpRouteEntry = DTMLFile('dtml/addIpRouteEntry',globals())
61
62
63 -class IpRouteEntry(OSComponent):
64 """
65 IpRouteEntry object
66 """
67
68 meta_type = 'IpRouteEntry'
69
70
71 monitor = False
72
73 _nexthop = ""
74 _target = ""
75 _targetobj = None
76 routetype = ""
77 routeproto = ""
78 routemask = 0
79 routeage = 0
80 metric1 = 0
81 metric2 = 0
82 metric3 = 0
83 metric4 = 0
84 metric5 = 0
85
86 _ifindex = None
87 _ifname = None
88
89 _properties = (
90 {'id':'routemask', 'type':'string', 'mode':''},
91 {'id':'nexthopip', 'type':'string',
92 'mode':'', 'setter':'setNextHopIp'},
93 {'id':'routeproto', 'type':'string', 'mode':''},
94 {'id':'routeage', 'type':'string', 'mode':''},
95 {'id':'routetype', 'type':'string', 'mode':''},
96 {'id':'metric1', 'type':'int', 'mode':''},
97 {'id':'metric2', 'type':'int', 'mode':''},
98 {'id':'metric3', 'type':'int', 'mode':''},
99 {'id':'metric4', 'type':'int', 'mode':''},
100 {'id':'metric5', 'type':'int', 'mode':''},
101 )
102 _relations = OSComponent._relations + (
103 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","routes")),
104 ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","iproutes")),
105 ("nexthop", ToOne(ToMany,"Products.ZenModel.IpAddress","clientroutes")),
106 ("target", ToOne(ToMany,"Products.ZenModel.IpNetwork","clientroutes")),
107 )
108
109 security = ClassSecurityInfo()
110
111 ipcheck = re.compile(r'^127\.|^0\.0\.|^169\.254\.|^224\.').search
112
113
114 - def __getattr__(self, name):
115 """
116 Allow access to getNextHopIp() though the nexthopip attribute
117 """
118 if name == 'nexthopip':
119 return self.getNextHopIp()
120 else:
121 raise AttributeError( name )
122
123
124 security.declareProtected('View', 'getNextHopDeviceLink')
126 """
127 Figure out which hop to return and if its a relation build link
128 """
129 ipobj = self.nexthop()
130 retval = ""
131 if ipobj:
132 retval = ipobj.getDeviceLink()
133 if not retval: retval = "None"
134 return retval
135
136
138 """
139 Return an <a> link to our next hop ip.
140 """
141 ipobj = self.nexthop()
142 if not ipobj: return ""
143 return ipobj.getPrimaryLink()
144
145
146 security.declareProtected('View', 'getNextHopIp')
147 - def getNextHopIp(self):
148 """
149 Return our next hop ip (as string) if stored as object or locally.
150 """
151 ip = self._nexthop
152 ipobj = self.nexthop()
153 if ipobj: ip = ipobj.id
154 return ip
155
156
158 """
159 Return the device to which this route points.
160 """
161 ipobj = self.nexthop()
162 if ipobj: return ipobj.device()
163
164
165 security.declareProtected('View', 'getInterfaceName')
167 """
168 Return the interface name for this route as a string.
169 If no interface is found return 'No Interface'.
170 """
171 if self._ifname is not None:
172 return self._ifname
173 elif self.interface():
174 return self.interface().name()
175 return "No Interface"
176
177
178 security.declareProtected('Change Device', 'setNextHopIp')
179 - def setNextHopIp(self, nextHopIp):
180 """
181 If the nexthop is a 127. or 0. address store locally
182 else link to it in the network hierarchy
183 """
184 if localIpCheck(self, nextHopIp) or not nextHopIp:
185 self._nexthop = nextHopIp
186 else:
187 networks = self.device().getNetworkRoot()
188 ip = networks.findIp(nextHopIp)
189 if not ip:
190 netmask = 24
191 int = self.interface()
192 if int:
193 intip = int.getIpAddressObj()
194 if intip: netmask = intip.netmask
195 ip = networks.createIp(nextHopIp, netmask)
196 self.addRelation('nexthop', ip)
197
198
199 - def matchTarget(self, ip):
200 """
201 Does this route target match the ip passed.
202 """
203 if self.target(): return self.target().hasIp(ip)
204
205
206 - def setTarget(self, netip):
207 """
208 Set this route target netip in the form 10.0.0.0/24.
209 """
210 netid, netmask = netip.split('/')
211 if localIpCheck(self, netip) or netmask == '0':
212 self._target = netip
213 else:
214 networks = self.device().getNetworkRoot()
215 net = networks.createNet(netid, netmask)
216 self.target.addRelation(net)
217
218
219 - def getTarget(self):
220 """
221 Return the route target ie 0.0.0.0/0.
222 """
223 if self.target():
224 return self.target().getNetworkName()
225 else:
226 return self._target
227
228
229 - def getTargetIp(self):
230 """
231 Return the target network Ip ie: 10.2.1.0
232 """
233 return self.getTarget().split("/")[0]
234
235
236 - def getTargetLink(self):
237 """
238 Return an <a> link to our target network.
239 """
240 if self.target():
241 return self.target().urlLink()
242 else:
243 return self._target
244
245
246 security.declareProtected('Change Device', 'setInterfaceIndex')
247 - def setInterfaceIndex(self, ifindex):
248 """
249 Set the interface relationship to the interface specified by the given
250 index. See also setInterfaceName()
251 """
252 self._ifindex = ifindex
253 for int in self.os().interfaces():
254 if int.ifindex == ifindex: break
255 else:
256 int = None
257 if int: self.interface.addRelation(int)
258 else: log.warn("interface index:%s not found", ifindex)
259
260
262 """
263 Return the index of the associated interface or None if no
264 interface is found.
265 """
266 if self._ifindex is not None:
267 return self._ifindex
268 else:
269 int = self.interface()
270 if int:
271 return int.ifindex
272
273
274 security.declareProtected('Change Device', 'setInterfaceName')
275 - def setInterfaceName(self, intname):
276 """
277 Set the interface relationship to the interface specified by the given
278 name. See also setInterfaceIndex()
279 """
280 self._ifname = intname
281 try:
282 int = filter(lambda i: i.name() == intname,
283 self.os().interfaces())[0]
284 self.interface.addRelation(int)
285 except IndexError:
286 log.warn("interface '%s' not found", intname)
287
288
289 - def getInterfaceIp(self):
290 """
291 Retrieve ip of the associated interface
292 """
293 int = self.interface()
294 if int: return int.getIp()
295 return ""
296
297
298 InitializeClass(IpRouteEntry)
299