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 _properties = (
87 {'id':'routemask', 'type':'string', 'mode':''},
88 {'id':'nexthopip', 'type':'string',
89 'mode':'', 'setter':'setNextHopIp'},
90 {'id':'routeproto', 'type':'string', 'mode':''},
91 {'id':'routeage', 'type':'string', 'mode':''},
92 {'id':'routetype', 'type':'string', 'mode':''},
93 {'id':'metric1', 'type':'int', 'mode':''},
94 {'id':'metric2', 'type':'int', 'mode':''},
95 {'id':'metric3', 'type':'int', 'mode':''},
96 {'id':'metric4', 'type':'int', 'mode':''},
97 {'id':'metric5', 'type':'int', 'mode':''},
98 )
99 _relations = OSComponent._relations + (
100 ("os", ToOne(ToManyCont,"Products.ZenModel.OperatingSystem","routes")),
101 ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","iproutes")),
102 ("nexthop", ToOne(ToMany,"Products.ZenModel.IpAddress","clientroutes")),
103 ("target", ToOne(ToMany,"Products.ZenModel.IpNetwork","clientroutes")),
104 )
105
106 security = ClassSecurityInfo()
107
108 ipcheck = re.compile(r'^127\.|^0\.0\.|^169\.254\.|^224\.').search
109
110
111 - def __getattr__(self, name):
112 """
113 Allow access to getNextHopIp() though the nexthopip attribute
114 """
115 if name == 'nexthopip':
116 return self.getNextHopIp()
117 else:
118 raise AttributeError( name )
119
120
121 security.declareProtected('View', 'getNextHopDeviceLink')
123 """
124 Figure out which hop to return and if its a relation build link
125 """
126 ipobj = self.nexthop()
127 retval = ""
128 if ipobj:
129 retval = ipobj.getDeviceLink()
130 if not retval: retval = "None"
131 return retval
132
133
135 """
136 Return an <a> link to our next hop ip.
137 """
138 ipobj = self.nexthop()
139 if not ipobj: return ""
140 return ipobj.getPrimaryLink()
141
142
143 security.declareProtected('View', 'getNextHopIp')
144 - def getNextHopIp(self):
145 """
146 Return our next hop ip (as string) if stored as object or locally.
147 """
148 ip = self._nexthop
149 ipobj = self.nexthop()
150 if ipobj: ip = ipobj.id
151 return ip
152
153
155 """
156 Return the device to which this route points.
157 """
158 ipobj = self.nexthop()
159 if ipobj: return ipobj.device()
160
161
162 security.declareProtected('View', 'getInterfaceName')
164 """
165 Return the interface name for this route as a string.
166 If no interface is found return 'No Interface'.
167 """
168 if self.interface():
169 return self.interface().name()
170 return "No Interface"
171
172
173 security.declareProtected('Change Device', 'setNextHopIp')
174 - def setNextHopIp(self, nextHopIp):
175 """
176 If the nexthop is a 127. or 0. address store locally
177 else link to it in the network hierarchy
178 """
179 if localIpCheck(self, nextHopIp) or not nextHopIp:
180 self._nexthop = nextHopIp
181 else:
182 networks = self.device().getNetworkRoot()
183 ip = networks.findIp(nextHopIp)
184 if not ip:
185 netmask = 24
186 int = self.interface()
187 if int:
188 intip = int.getIpAddressObj()
189 if intip: netmask = intip.netmask
190 ip = networks.createIp(nextHopIp, netmask)
191 self.addRelation('nexthop', ip)
192
193
194 - def matchTarget(self, ip):
195 """
196 Does this route target match the ip passed.
197 """
198 if self.target(): return self.target().hasIp(ip)
199
200
201 - def setTarget(self, netip):
202 """
203 Set this route target netip in the form 10.0.0.0/24.
204 """
205 netid, netmask = netip.split('/')
206 if localIpCheck(self, netip) or netmask == '0':
207 self._target = netip
208 else:
209 networks = self.device().getNetworkRoot()
210 net = networks.createNet(netid, netmask)
211 self.target.addRelation(net)
212
213
214 - def getTarget(self):
215 """
216 Return the route target ie 0.0.0.0/0.
217 """
218 if self.target():
219 return self.target().getNetworkName()
220 else:
221 return self._target
222
223
224 - def getTargetIp(self):
225 """
226 Return the target network Ip ie: 10.2.1.0
227 """
228 return self.getTarget().split("/")[0]
229
230
231 - def getTargetLink(self):
232 """
233 Return an <a> link to our target network.
234 """
235 if self.target():
236 return self.target().urlLink()
237 else:
238 return self._target
239
240
241 security.declareProtected('Change Device', 'setInterfaceIndex')
242 - def setInterfaceIndex(self, ifindex):
243 """
244 Set the interface relationship to the interface specified by the given
245 index. See also setInterfaceName()
246 """
247 for int in self.os().interfaces():
248 if int.ifindex == ifindex: break
249 else:
250 int = None
251 if int: self.interface.addRelation(int)
252 else: log.warn("interface index:%s not found", ifindex)
253
254
256 """
257 Return the index of the associated interface or None if no
258 interface is found.
259 """
260 int = self.interface()
261 if int: return int.ifindex
262
263
264 security.declareProtected('Change Device', 'setInterfaceName')
265 - def setInterfaceName(self, intname):
266 """
267 Set the interface relationship to the interface specified by the given
268 name. See also setInterfaceIndex()
269 """
270 try:
271 int = filter(lambda i: i.name() == intname,
272 self.os().interfaces())[0]
273 self.interface.addRelation(int)
274 except IndexError:
275 log.warn("interface '%s' not found", intname)
276
277
278 - def getInterfaceIp(self):
279 """
280 Retrieve ip of the associated interface
281 """
282 int = self.interface()
283 if int: return int.getIp()
284 return ""
285
286
287 InitializeClass(IpRouteEntry)
288