Package ZenModel :: Module LinkManager
[hide private]
[frames] | no frames]

Source Code for Module ZenModel.LinkManager

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2007, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14   
 15  from Globals import InitializeClass 
 16  from AccessControl import ClassSecurityInfo 
 17  from AccessControl import getSecurityManager 
 18  from AccessControl import Permissions as permissions 
 19   
 20  from sets import Set 
 21   
 22  import Products.ZenUtils.guid as guid 
 23  from Products.AdvancedQuery import MatchRegexp, Or, In, Eq 
 24   
 25  import simplejson 
 26   
 27  from Products.ZenModel.ZenModelRM import ZenModelRM 
 28  from Products.ZenRelations.RelSchema import * 
 29  from Products.ZenUtils.Search import makeCaseInsensitiveFieldIndex 
 30  from Products.ZenUtils.Search import makeCaseInsensitiveKeywordIndex 
 31   
 32  from Products.ZenModel.Link import Link 
 33  from Products.ZenModel.Linkable import Linkable 
 34   
 35  from Products.ZenUtils.NetworkTree import NetworkLink 
 36   
 37   
38 -def manage_addLinkManager(context, id=None):
39 """ Make a LinkManager """ 40 if not id: 41 id = "ZenLinkManager" 42 mgr = LinkManager(id) 43 context._setObject(mgr.id, mgr) 44 mgr = context._getOb(id) 45 mgr.createCatalog() 46 mgr.buildRelations()
47 48
49 -class LinkManager(ZenModelRM):
50 """ An object that manages links. 51 """ 52 53 default_catalog = "linkSearch" 54 55 _properties = ( 56 {'id':'link_type','type':'string','mode':'w'}, 57 {'id':'OSI_layer', 'type':'int', 'mode':'w'}, 58 {'id':'entry_type', 'type':'string', 'mode':'w'}, 59 ) 60 61 _relations = ( 62 ("links", ToManyCont(ToOne, "Products.ZenModel.Link", "linkManager")), 63 ) 64 65 factory_type_information = ( 66 { 'immediate_view' : 'viewLinkManager', 67 'factory' : 'manage_addLinkManager', 68 'actions' : ( 69 { 'id' : 'viewLinkManager' 70 , 'name' : 'Link Manager' 71 , 'action' : 'viewLinkManager' 72 , 'permissions' : ( "Manage DMD", ) 73 },) 74 }, 75 ) 76 77 security = ClassSecurityInfo() 78
79 - def __init__(self, id):
80 self.id = id
81
82 - def createCatalog(self):
83 """ Creates the catalog for link searching """ 84 85 from Products.ZCatalog.ZCatalog import manage_addZCatalog 86 87 manage_addZCatalog(self, self.default_catalog, 88 self.default_catalog) 89 zcat = self._getOb(self.default_catalog) 90 cat = zcat._catalog 91 for idxname in ['link_type','OSI_layer']: 92 cat.addIndex(idxname, 93 makeCaseInsensitiveFieldIndex(idxname, 'string')) 94 cat.addIndex('getEndpointNames', 95 makeCaseInsensitiveKeywordIndex('getEndpointNames')) 96 zcat.addColumn('id')
97
98 - def _getCatalog(self):
99 """ Return the ZCatalog under the default_catalog attribute """ 100 return getattr(self, self.default_catalog, None)
101 102 security.declareProtected('Change Device', 'manage_addLink') 115 116 security.declareProtected('Change Device', 'manage_removeLink') 122 123 security.declareProtected('Change Device', 'getNodeLinks') 130 131 security.declareProtected('View', 'getLinkedNodes')
132 - def getLinkedNodes(self, node):
133 """ Returns all nodes linked to a given Linkable """ 134 nlinks = self.getNodeLinks(node) 135 return map(lambda x:x.getOtherEndpoint(node), nlinks)
136
137 - def query_catalog(self, indxname, querystr=""):
138 zcat = self._getCatalog() 139 if not querystr or not zcat: return [] 140 query = MatchRegexp(indxname, querystr) 141 brains = zcat.evalAdvancedQuery(query) 142 return [x.getObject() for x in brains]
143
144 - def searchLinksByType(self, linktype):
145 return self.query_catalog('link_type', linktype)
146
147 - def searchLinksByLayer(self, layernum):
148 return self.query_catalog('OSI_layer', linktype)
149
150 - def searchLinksByEndpoint(self, epointname):
151 return self.query_catalog('getEndpointNames', epointname)
152 167
168 - def getJSONLinkInfo(self, offset=0, count=50, filter='', 169 orderby='OSI_layer', orderdir='asc'):
170 """ Returns a batch of links in JSON format """ 171 totalCount, linkList = self.getAdvancedQueryLinkList( 172 offset, count, filter, orderby, orderdir) 173 results = [x.getObject().getDataForJSON() + ['odd'] 174 for x in linkList] 175 return simplejson.dumps((results, totalCount))
176 199 for ip in net.ipaddresses.objectValuesGen(): 200 iface = ip.interface() 201 if iface: addToDict(iface) 202 if len(locdict)<=1: continue 203 locgroups = locdict.values() 204 while locgroups: 205 lg = locgroups.pop() 206 targets = [] 207 for g in locgroups: targets.extend(g) 208 for l in lg: 209 for t in targets: 210 n = NetworkLink() 211 n.setEndpoints(l, t) 212 result.add(n) 213 return result
214 228 links = filter(hasForeignEndpoint, links) 229 for x in links: 230 geomapdata = x.getGeomapData(sibling) 231 severities[geomapdata] = max( 232 x.getStatus(), 233 severities.get(geomapdata, 0) 234 ) 235 result.add(geomapdata) 236 addresses = [x for x in list(result) if x] 237 severities = [severities[x] for x in addresses] 238 return map(list, zip(map(list, addresses), severities)) 239 255 256 257 258 InitializeClass(LinkManager) 259