Package Products :: Package Zuul :: Package routers :: Module network
[hide private]
[frames] | no frames]

Source Code for Module Products.Zuul.routers.network

  1  ############################################################################## 
  2  #  
  3  # Copyright (C) Zenoss, Inc. 2010, all rights reserved. 
  4  #  
  5  # This content is made available according to terms specified in 
  6  # License.zenoss under the directory where your Zenoss product is installed. 
  7  #  
  8  ############################################################################## 
  9   
 10   
 11  """ 
 12  Operations for Networks. 
 13   
 14  Available at:  /zport/dmd/network_router 
 15  """ 
 16   
 17  import logging 
 18  from Products.ZenUtils.Ext import DirectResponse 
 19  from Products.ZenUtils.IpUtil import IpAddressError 
 20  from Products.Zuul.decorators import require 
 21  from Products.Zuul.interfaces import ITreeNode 
 22  from Products.ZenUtils.jsonutils import unjson 
 23  from Products.ZenUtils.Utils import getDisplayType 
 24  from Products import Zuul 
 25  from Products.Zuul.decorators import serviceConnectionError 
 26  from Products.Zuul.routers import TreeRouter 
 27  from Products.ZenModel.IpAddress import IpAddress 
 28  from Products.ZenMessaging.audit import audit 
 29   
 30  log = logging.getLogger('zen.NetworkRouter') 
31 32 -class NetworkRouter(TreeRouter):
33 """ 34 A JSON/ExtDirect interface to operations on networks 35 """ 36
37 - def __init__(self, context, request):
38 super(NetworkRouter, self).__init__(context, request) 39 self.api = Zuul.getFacade('network')
40
41 - def _getFacade(self):
42 return Zuul.getFacade('network', self.context)
43 44 @require('Manage DMD')
45 - def discoverDevices(self, uid):
46 """ 47 Discover devices on a network. 48 49 @type uid: string 50 @param uid: Unique identifier of the network to discover 51 @rtype: DirectResponse 52 @return: B{Properties}: 53 - jobId: (integer) The id of the discovery job 54 """ 55 jobStatus = self.api.discoverDevices(uid) 56 if jobStatus: 57 audit('UI.Network.DiscoverDevices', uid) 58 return DirectResponse.succeed(new_jobs=Zuul.marshal([jobStatus], 59 keys=('uuid', 'description','started'))) 60 else: 61 return DirectResponse.fail()
62 63 @require('Manage DMD')
64 - def addNode(self, newSubnet, contextUid):
65 """ 66 Add a new subnet. 67 68 @type newSubnet: string 69 @param newSubnet: New subnet to add 70 @type contextUid: string 71 @param contextUid: Unique identifier of the network parent of the new subnet 72 @rtype: DirectResponse 73 @return: B{Properties}: 74 - newNode: (dictionary) An object representing the new subnet node 75 """ 76 # If the user doesn't include a mask, reject the request. 77 if '/' not in newSubnet: 78 response = DirectResponse.fail('You must include a subnet mask.') 79 else: 80 try: 81 netip, netmask = newSubnet.split('/') 82 netmask = int(netmask) 83 foundSubnet = self.api.findSubnet(netip, netmask, contextUid) 84 85 if foundSubnet is not None: 86 response = DirectResponse.fail('Did not add duplicate subnet: %s (%s/%s)' % 87 (newSubnet, foundSubnet.id, foundSubnet.netmask)) 88 else: 89 newNet = self.api.addSubnet(newSubnet, contextUid) 90 node = ITreeNode(newNet) 91 audit('UI.Network.AddSubnet', contextUid, subnet=newSubnet) 92 response = DirectResponse.succeed(newNode=Zuul.marshal(node)) 93 94 except IpAddressError as error: 95 response = DirectResponse.exception(error, 'Error adding subnet.') 96 97 except Exception as error: 98 log.exception("Error adding subnet.") 99 response = DirectResponse.exception(error, 'Error adding subnet.') 100 101 return response
102 103 @require('Manage DMD')
104 - def deleteNode(self, uid):
105 """ 106 Delete a subnet. 107 108 @type uid: string 109 @param uid: Unique identifier of the subnet to delete 110 @rtype: DirectResponse 111 @return: B{Properties}: 112 - tree: (dictionary) An object representing the new network tree 113 """ 114 self.api.deleteSubnet(uid) 115 audit('UI.Network.DeleteSubnet', subnet=uid) 116 return DirectResponse.succeed(tree=self.getTree())
117 118
119 - def getTree(self, id='/zport/dmd/Networks'):
120 """ 121 Returns the tree structure of an organizer hierarchy where 122 the root node is the organizer identified by the id parameter. 123 124 @type id: string 125 @param id: Id of the root node of the tree to be returned. Defaults to 126 the Networks tree root. 127 @rtype: [dictionary] 128 @return: Object representing the tree 129 """ 130 tree = self.api.getTree(id) 131 data = Zuul.marshal(tree) 132 return [data]
133
134 - def getInfo(self, uid, keys=None):
135 """ 136 Returns a dictionary of the properties of an object 137 138 @type uid: string 139 @param uid: Unique identifier of an object 140 @type keys: list 141 @param keys: (optional) List of keys to include in the returned 142 dictionary. If None then all keys will be returned 143 @rtype: DirectResponse 144 @return: B{Properties} 145 - data: (dictionary) Object properties 146 """ 147 network = self.api.getInfo(uid) 148 data = Zuul.marshal(network, keys) 149 disabled = not Zuul.checkPermission('Manage DMD') 150 return DirectResponse.succeed(data=data, disabled=disabled)
151 152 @require('Manage DMD')
153 - def setInfo(self, **data):
154 """ 155 Main method for setting attributes on a network or network organizer. 156 This method accepts any keyword argument for the property that you wish 157 to set. The only required property is "uid". 158 159 @type uid: string 160 @keyword uid: Unique identifier of an object 161 @rtype: DirectResponse 162 """ 163 network = self.api.getInfo(data['uid']) 164 Zuul.unmarshal(data, network) 165 audit(['UI', getDisplayType(network), 'Edit'], network, data_=data) 166 return DirectResponse.succeed()
167 168 @serviceConnectionError
169 - def getIpAddresses(self, uid, start=0, params=None, limit=50, sort='ipAddressAsInt', 170 page=None, dir='ASC'):
171 """ 172 Given a subnet, get a list of IP addresses and their relations. 173 174 @type uid: string 175 @param uid: Unique identifier of a subnet 176 @type start: integer 177 @param start: Offset to return the results from; used in pagination 178 @type params: string 179 @param params: Not used 180 @type limit: integer 181 @param limit: Number of items to return; used in pagination 182 @type sort: string 183 @param sort: (optional) Key on which to sort the return results; 184 defaults to 'name' 185 @type order: string 186 @param order: Sort order; can be either 'ASC' or 'DESC' 187 @rtype: DirectResponse 188 """ 189 if isinstance(params, basestring): 190 params = unjson(params) 191 instances = self.api.getIpAddresses(uid=uid, start=start, params=params, 192 limit=limit, sort=sort, dir=dir) 193 194 keys = ['name', 'netmask', 'pingstatus', 'snmpstatus', 'uid', 195 'device', 'interface', 'macAddress', 196 'interfaceDescription'] 197 data = Zuul.marshal(instances.results, keys) 198 return DirectResponse.succeed(data=data, totalCount=instances.total, 199 hash=instances.hash_)
200
201 - def removeIpAddresses(self, uids=None):
202 """ 203 Removes every ip address specified by uids that are 204 not attached to any device 205 @type uids: Array of Strings 206 @param uids: unique identfiers of the ip addresses to delete 207 """ 208 if uids: 209 removedCount, errorCount = self.api.removeIpAddresses(uids) 210 audit('UI.IPAddress.Remove', ips=uids, numremoved=removedCount, 211 numerrors=errorCount) 212 return DirectResponse.succeed(removedCount=removedCount, 213 errorCount=errorCount)
214
215 216 -class Network6Router(NetworkRouter):
217 """ 218 A JSON/ExtDirect interface to operations on IPv6 networks 219 """ 220
221 - def __init__(self, context, request):
222 super(NetworkRouter, self).__init__(context, request) 223 self.api = Zuul.getFacade('network6')
224
225 - def _getFacade(self):
226 return Zuul.getFacade('network6', self.context)
227