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\.|^::1$|^fe80:|^ff').search
112
113 - def __getattr__(self, name):
114 """
115 Allow access to getNextHopIp() though the nexthopip attribute
116 """
117 if name == 'nexthopip':
118 return self.getNextHopIp()
119 else:
120 raise AttributeError( name )
121
122
123 security.declareProtected('View', 'getNextHopDeviceLink')
125 """
126 Figure out which hop to return and if it's a relation build link
127 """
128 ipobj = self.nexthop()
129 retval = ""
130 if ipobj:
131 retval = ipobj.getDeviceLink()
132 if not retval: retval = "None"
133 return retval
134
135
137 """
138 Return an <a> link to our next hop ip.
139 """
140 ipobj = self.nexthop()
141 if not ipobj: return ""
142 return ipobj.getPrimaryLink()
143
144
145 security.declareProtected('View', 'getNextHopIp')
146 - def getNextHopIp(self):
147 """
148 Return our next hop ip (as string) if stored as object or locally.
149 """
150 ip = self._nexthop
151 ipobj = self.nexthop()
152 if ipobj: ip = ipobj.id
153 return ip
154
155
157 """
158 Return the device to which this route points.
159 """
160 ipobj = self.nexthop()
161 if ipobj: return ipobj.device()
162
163
164 security.declareProtected('View', 'getInterfaceName')
166 """
167 Return the interface name for this route as a string.
168 If no interface is found return 'No Interface'.
169 """
170 if self._ifname is not None:
171 return self._ifname
172 elif self.interface():
173 return self.interface().name()
174 return "No Interface"
175
176
177 security.declareProtected('Change Device', 'setNextHopIp')
178 - def setNextHopIp(self, nextHopIp):
179 """
180 If the nexthop is a 127. or 0. address store locally
181 else link to it in the network hierarchy
182 """
183 if localIpCheck(self, nextHopIp) or not nextHopIp:
184 self._nexthop = nextHopIp
185 else:
186 networks = self.device().getNetworkRoot()
187 ip = networks.findIp(nextHopIp)
188 if not ip:
189 netmask = 24
190 int = self.interface()
191 if int:
192 intip = int.getIpAddressObj()
193 if intip: netmask = intip.netmask
194 ip = networks.createIp(nextHopIp, netmask)
195 self.addRelation('nexthop', ip)
196
197
198 - def matchTarget(self, ip):
199 """
200 Does this route target match the ip passed.
201 """
202 if self.target(): return self.target().hasIp(ip)
203
204
205 - def setTarget(self, netip):
206 """
207 Set this route target netip in the form 10.0.0.0/24.
208 """
209 netid, netmask = netip.split('/')
210 if localIpCheck(self, netip) or netmask == '0':
211 self._target = netip
212 else:
213 networks = self.device().getNetworkRoot()
214 net = networks.createNet(netid, netmask)
215 self.target.addRelation(net)
216
217
218 - def getTarget(self):
219 """
220 Return the route target ie 0.0.0.0/0.
221 """
222 if self.target():
223 return self.target().getNetworkName()
224 else:
225 return self._target
226
227
228 - def getTargetIp(self):
229 """
230 Return the target network Ip ie: 10.2.1.0
231 """
232 return self.getTarget().split("/")[0]
233
234
235 - def getTargetLink(self):
236 """
237 Return an <a> link to our target network.
238 """
239 if self.target():
240 return self.target().urlLink()
241 else:
242 return self._target
243
244
245 security.declareProtected('Change Device', 'setInterfaceIndex')
246 - def setInterfaceIndex(self, ifindex):
247 """
248 Set the interface relationship to the interface specified by the given
249 index. See also setInterfaceName()
250 """
251 self._ifindex = ifindex
252 for int in self.os().interfaces():
253 if int.ifindex == ifindex: break
254 else:
255 int = None
256 if int: self.interface.addRelation(int)
257 else: log.warn("interface index:%s not found", ifindex)
258
259
261 """
262 Return the index of the associated interface or None if no
263 interface is found.
264 """
265 if self._ifindex is not None:
266 return self._ifindex
267 else:
268 int = self.interface()
269 if int:
270 return int.ifindex
271
272
273 security.declareProtected('Change Device', 'setInterfaceName')
274 - def setInterfaceName(self, intname):
275 """
276 Set the interface relationship to the interface specified by the given
277 name. See also setInterfaceIndex()
278 """
279 self._ifname = intname
280 try:
281 int = filter(lambda i: i.name() == intname,
282 self.os().interfaces())[0]
283 self.interface.addRelation(int)
284 except IndexError:
285 log.warn("interface '%s' not found", intname)
286
287
288 - def getInterfaceIp(self):
289 """
290 Retrieve ip of the associated interface
291 """
292 int = self.interface()
293 if int: return int.getIp()
294 return ""
295
296
297 InitializeClass(IpRouteEntry)
298