ia64/xen-unstable

view tools/python/xen/xend/XendNetwork.py @ 16511:d1e1db24bd5f

xend: Implement get_by_name_label for class XendNetwork

Although XenAPI c-bindings support this operation on Network class,
there is no implementation in xend. This patch provides one.

Signed-off-by: Jim Fehlig <jfehlig@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Dec 04 10:41:55 2007 +0000 (2007-12-04)
parents 16e38247dee8
children 44a98411d230
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (c) 2006 Xensource Inc.
16 #============================================================================
18 import os
19 import commands
20 import re
21 import struct
22 import socket
24 import XendDomain
25 import XendNode
26 from XendLogging import log
27 from xen.xend import uuid as genuuid
28 from xen.xend.XendBase import XendBase
29 from xen.xend.XendError import *
30 from xen.util import Brctl
31 from xen.xend import XendAPIStore
33 IP_ROUTE_RE = r'^default via ([\d\.]+) dev (\w+)'
35 def bridge_exists(name):
36 return name in Brctl.get_state().keys()
38 class XendNetwork(XendBase):
39 """We're going to assert that the name_label of this
40 network is just the name of the bridge"""
42 def getClass(self):
43 return "network"
45 def getAttrRW(self):
46 attrRW = ['name_label',
47 'name_description',
48 'other_config',
49 'default_gateway',
50 'default_netmask']
51 return XendBase.getAttrRW() + attrRW
53 def getAttrRO(self):
54 attrRO = ['VIFs',
55 'PIFs']
56 return XendBase.getAttrRO() + attrRO
58 def getAttrInst(self):
59 return XendBase.getAttrInst() + self.getAttrRW()
61 def getMethods(self):
62 methods = ['add_to_other_config',
63 'remove_from_other_config',
64 'destroy']
65 return XendBase.getMethods() + methods
67 def getFuncs(self):
68 funcs = ['create', 'get_by_name_label']
69 return XendBase.getFuncs() + funcs
71 getClass = classmethod(getClass)
72 getAttrRO = classmethod(getAttrRO)
73 getAttrRW = classmethod(getAttrRW)
74 getAttrInst = classmethod(getAttrInst)
75 getMethods = classmethod(getMethods)
76 getFuncs = classmethod(getFuncs)
78 def create_phy(self, name):
79 """
80 Called when a new bridge is found on xend start
81 """
82 # Create new uuids
83 uuid = genuuid.createString()
85 # Create instance
86 record = {
87 'name_label': name,
88 'name_description': '',
89 'other_config': {},
90 'default_gateway': '',
91 'default_netmask': ''
92 }
93 network = XendNetwork(record, uuid)
95 return uuid
97 def recreate(self, record, uuid):
98 """
99 Called on xend start / restart, or machine
100 restart, when read from saved config.
101 Needs to check network exists, create it otherwise
102 """
104 # Create instance (do this first, to check record)
105 network = XendNetwork(record, uuid)
107 # Create network if it doesn't already exist
108 if not bridge_exists(network.name_label):
109 Brctl.bridge_create(network.name_label)
111 return uuid
113 def create(self, record):
114 """
115 Called from API, to create a new network
116 """
117 # Create new uuids
118 uuid = genuuid.createString()
120 # Create instance (do this first, to check record)
121 network = XendNetwork(record, uuid)
123 # Check network doesn't already exist
124 name_label = network.name_label
125 if bridge_exists(name_label):
126 del network
127 raise UniqueNameError(name_label, "network")
129 # Create the bridge
130 Brctl.bridge_create(network.name_label)
132 XendNode.instance().save_networks()
134 return uuid
136 def get_by_name_label(cls, name):
137 return [inst.get_uuid()
138 for inst in XendAPIStore.get_all(cls.getClass())
139 if inst.get_name_label() == name]
141 create_phy = classmethod(create_phy)
142 recreate = classmethod(recreate)
143 create = classmethod(create)
144 get_by_name_label = classmethod(get_by_name_label)
146 def __init__(self, record, uuid):
147 XendBase.__init__(self, uuid, record)
149 #
150 # XenAPI Mehtods
151 #
153 def destroy(self):
154 # check no VIFs or PIFs attached
155 if len(self.get_VIFs()) > 0:
156 raise NetworkError("Cannot destroy network with VIFs attached",
157 self.get_name_label())
159 if len(self.get_PIFs()) > 0:
160 raise NetworkError("Cannot destroy network with PIFs attached",
161 self.get_name_label())
163 XendBase.destroy(self)
164 Brctl.bridge_del(self.get_name_label())
165 XendNode.instance().save_networks()
167 def get_name_label(self):
168 return self.name_label
170 def get_name_description(self):
171 return self.name_description
173 def set_name_label(self, new_name):
174 pass
176 def set_name_description(self, new_desc):
177 self.name_description = new_desc
178 XendNode.instance().save_networks()
180 def get_VIFs(self):
181 result = []
182 vms = XendDomain.instance().get_all_vms()
183 for vm in vms:
184 vifs = vm.get_vifs()
185 for vif in vifs:
186 vif_cfg = vm.get_dev_xenapi_config('vif', vif)
187 if vif_cfg.get('network') == self.get_uuid():
188 result.append(vif)
189 return result
191 def get_PIFs(self):
192 pifs = XendAPIStore.get_all("PIF")
193 return [pif.get_uuid() for pif in pifs
194 if pif.get_network() == self.get_uuid()]
196 def get_other_config(self):
197 return self.other_config
199 def set_other_config(self, value):
200 self.other_config = value
201 XendNode.instance().save_networks()
203 def add_to_other_config(self, key, value):
204 self.other_config[key] = value
205 XendNode.instance().save_networks()
207 def remove_from_other_config(self, key):
208 if key in self.other_config:
209 del self.other_config[key]
210 XendNode.instance().save_networks()
212 def get_default_gateway(self):
213 return self.default_gateway
215 def set_default_gateway(self, gateway):
216 self.default_gateway = gateway
217 XendNode.instance().save_networks()
219 def get_default_netmask(self):
220 return self.default_netmask
222 def set_default_netmask(self, netmask):
223 self.default_netmask = netmask
224 XendNode.instance().save_networks()