1
2
3
4
5
6
7
8
9
10
11
12
13
14 __doc__="""IpAddress
15
16 IpAddress represents a device residing on an IP network.
17
18 $Id: IpAddress.py,v 1.42 2004/04/15 00:54:14 edahl Exp $"""
19
20 __version__ = "$Revision: 1.42 $"[11:-2]
21
22 import socket
23 import logging
24 log = logging.getLogger("zen.IpAddress")
25
26
27 from ManagedEntity import ManagedEntity
28
29
30 from AccessControl import ClassSecurityInfo
31 from Globals import DTMLFile
32 from Globals import InitializeClass
33 from OFS.FindSupport import FindSupport
34 from Acquisition import aq_parent
35
36 from Products.ZenRelations.RelSchema import *
37
38 from Products.ZenUtils.IpUtil import *
39 from Products.ZenUtils.Utils import getObjByPath
40
41 from Products.ZenModel.Exceptions import *
42
50
51
53 """find an ip from base. base should be Networks root found through aq"""
54 searchCatalog = context.Networks.ipSearch
55 ret = searchCatalog({'id':ip})
56 if len(ret) > 1:
57 raise IpAddressConflict, "IP address conflict for IP: %s" % ip
58 if ret:
59 ipobj = getObjByPath(searchCatalog.getPhysicalRoot(),
60 ret[0].getPrimaryId)
61 return ipobj
62
63
64 addIpAddress = DTMLFile('dtml/addIpAddress',globals())
65
66
68 """IpAddress object"""
69 event_key = portal_type = meta_type = 'IpAddress'
70
71 default_catalog = 'ipSearch'
72
73 _properties = (
74 {'id':'netmask', 'type':'string', 'mode':'w', 'setter':'setNetmask'},
75 {'id':'ptrName', 'type':'string', 'mode':'w'},
76 )
77 _relations = ManagedEntity._relations + (
78 ("network", ToOne(ToManyCont,"Products.ZenModel.IpNetwork","ipaddresses")),
79 ("interface", ToOne(ToMany,"Products.ZenModel.IpInterface","ipaddresses")),
80 ("clientroutes", ToMany(ToOne,"Products.ZenModel.IpRouteEntry","nexthop")),
81 )
82
83 factory_type_information = (
84 {
85 'id' : 'IpAddress',
86 'meta_type' : 'IpAddress',
87 'description' : """Ip Address Class""",
88 'icon' : 'IpAddress_icon.gif',
89 'product' : 'ZenModel',
90 'factory' : 'manage_addIpAddress',
91 'immediate_view' : 'viewIpAddressOverview',
92 'actions' :
93 (
94 { 'id' : 'overview'
95 , 'name' : 'Overview'
96 , 'action' : 'viewIpAddressOverview'
97 , 'permissions' : ( "View", )
98 },
99 )
100 },
101 )
102
103 security = ClassSecurityInfo()
104
110
111
112
120
121
122 security.declareProtected('View', 'primarySortKey')
124 """make sure that networks sort correctly"""
125 return numbip(self.id)
126
127
130
132 """override from PerpertyManager to handle checks and ip creation"""
133 self._wrapperCheck(value)
134 if id == 'netmask':
135 self.setNetmask(value)
136 else:
137 setattr(self,id,value)
138
139
145
146
147 security.declareProtected('Change Device', 'setIpAddress')
149 """set the ipaddress can be in the form 1.1.1.1/24 to also set mask"""
150 iparray = ip.split("/")
151 if len(iparray) > 1:
152 ip = iparray[0]
153 self._netmask = maskToBits(iparray[1])
154 checkip(ip)
155 aqself = self.primaryAq()
156 network = aqself.aq_parent
157 netip = getnetstr(ip, network.netmask)
158 if netip == network.id:
159 network._renameObject(aqs.id, ip)
160 else:
161 raise WrongSubnetError, \
162 "Ip %s is in a different subnet than %s" % (ip, self.id)
163
164
165
166 security.declareProtected('View', 'getIp')
168 """return only the ip"""
169 return self.id
170
171
172 security.declareProtected('View', 'getIpAddress')
174 """return the ip with its netmask in the form 1.1.1.1/24"""
175 return self.id + "/" + str(self._netmask)
176
177
180
181
182 security.declareProtected('View', 'getInterfaceName')
184 if self.interface():
185 return self.interface().name()
186 return "No Interface"
187
188
189 security.declareProtected('View', 'getNetworkName')
191 if self.network():
192 return self.network().getNetworkName()
193 return "No Network"
194
195
196 security.declareProtected('View', 'getNetworkUrl')
198 if self.network():
199 return self.network().absolute_url()
200 return ""
201
202
203 security.declareProtected('View', 'getDeviceUrl')
205 """Get the primary url path of the device in which this ip
206 is associated with. If no device return url to the ip itself.
207 """
208 d = self.device()
209 if d:
210 return d.getPrimaryUrlPath()
211 else:
212 return self.getPrimaryUrlPath()
213
214
216 """Reuturn the device for this ip for DeviceResultInt"""
217 int = self.interface()
218 if int: return int.device()
219 return None
220
221
228
229
234
235
237 """
238 Device only propagates beforeDelete if we are being deleted or copied.
239 Moving and renaming don't propagate.
240 """
241 super(IpAddress,self).manage_beforeDelete(item, container)
242 self.unindex_object()
243
244
245 InitializeClass(IpAddress)
246