blob: e83c818a9d8fd62edb4b609215c54726ff280f1b [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# Copyright 2017-present Open Networking Foundation
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
Zack Williams5c2ea232019-01-30 15:23:01 -070016from __future__ import absolute_import
17
Scott Baker2c465a62017-06-09 14:21:11 -070018from xosapi.orm import ORMWrapper, register_convenience_wrapper
19
Zack Williams045b63d2019-01-22 16:30:57 -070020
Scott Baker2c465a62017-06-09 14:21:11 -070021class ORMWrapperAddressPool(ORMWrapper):
22 def get_address(self):
23 ap = self
24 if ap.addresses:
25 avail_ips = ap.addresses.split()
26 else:
27 avail_ips = []
28
29 if ap.inuse:
30 inuse_ips = ap.inuse.split()
31 else:
32 inuse_ips = []
33
34 while avail_ips:
35 addr = avail_ips.pop(0)
36
37 if addr in inuse_ips:
38 # This may have happened if someone re-ran the tosca
39 # recipe and 'refilled' the AddressPool while some addresses
40 # were still in use.
41 continue
42
43 inuse_ips.insert(0, addr)
44
45 ap.inuse = " ".join(inuse_ips)
46 ap.addresses = " ".join(avail_ips)
47 return addr
48
49 return None
50
Scott Baker2c465a62017-06-09 14:21:11 -070051 def put_address(self, addr):
52 ap = self
53 addresses = ap.addresses or ""
54 parts = addresses.split()
55 if addr not in parts:
56 parts.insert(0, addr)
57 ap.addresses = " ".join(parts)
58
59 inuse = ap.inuse or ""
60 parts = inuse.split()
61 if addr in parts:
62 parts.remove(addr)
63 ap.inuse = " ".join(parts)
64
Zack Williams045b63d2019-01-22 16:30:57 -070065
Scott Baker2c465a62017-06-09 14:21:11 -070066register_convenience_wrapper("AddressPool", ORMWrapperAddressPool)