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