Global replace of Nicira Networks.
[sliver-openvswitch.git] / tests / MockXenAPI.py
1 # Copyright (c) 2011 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 def xapi_local():
16     return Session()
17
18
19 class Session(object):
20     def __init__(self):
21         self.xenapi = XenAPI()
22
23
24 class Failure(Exception):
25     pass
26
27
28 class XenAPI(object):
29     def __init__(self):
30         self.network = Network()
31         self.pool = Pool()
32         self.VIF = VIF()
33         self.VM = VM()
34
35     def login_with_password(self, unused_username, unused_password):
36         pass
37
38
39 class RecordRef(object):
40     def __init__(self, attrs):
41         self.attrs = attrs
42
43
44 class Table(object):
45     def __init__(self, records):
46         self.records = records
47
48     def get_all(self):
49         return [RecordRef(rec) for rec in self.records]
50
51     def get_by_uuid(self, uuid):
52         recs = [rec for rec in self.records if rec["uuid"] == uuid]
53         if len(recs) != 1:
54             raise Failure("No record with UUID %s" % uuid)
55         return RecordRef(recs[0])
56
57     def get_record(self, record_ref):
58         return record_ref.attrs
59
60
61 class Network(Table):
62     __records = ({"uuid": "9b66c68b-a74e-4d34-89a5-20a8ab352d1e",
63                   "bridge": "xenbr0",
64                   "other_config":
65                       {"vswitch-controller-fail-mode": "secure",
66                        "nicira-bridge-id": "custom bridge ID"}},
67                  {"uuid": "e1c9019d-375b-45ac-a441-0255dd2247de",
68                   "bridge": "xenbr1",
69                   "other_config":
70                       {"vswitch-disable-in-band": "true"}})
71
72     def __init__(self):
73         Table.__init__(self, Network.__records)
74
75
76 class Pool(Table):
77     __records = ({"uuid": "7a793edf-e5f4-4994-a0f9-cee784c0cda3",
78                   "other_config":
79                       {"vswitch-controller-fail-mode": "secure"}},)
80
81     def __init__(self):
82         Table.__init__(self, Pool.__records)
83
84 class VIF(Table):
85     __records = ({"uuid": "6ab1b260-398e-49ba-827b-c7696108964c",
86                   "other_config":
87                       {"nicira-iface-id": "custom iface ID"}},)
88
89     def __init__(self):
90         Table.__init__(self, VIF.__records)
91
92 class VM(Table):
93     __records = ({"uuid": "fcb8a3f6-dc04-41d2-8b8a-55afd2b755b8",
94                   "other_config":
95                       {"nicira-vm-id": "custom vm ID"}},)
96
97     def __init__(self):
98         Table.__init__(self, VM.__records)