Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # 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 | |
Scott Baker | 2c465a6 | 2017-06-09 14:21:11 -0700 | [diff] [blame] | 16 | from xosapi.orm import ORMWrapper, register_convenience_wrapper |
| 17 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 18 | |
Scott Baker | 2c465a6 | 2017-06-09 14:21:11 -0700 | [diff] [blame] | 19 | class ORMWrapperAddressPool(ORMWrapper): |
| 20 | def get_address(self): |
| 21 | ap = self |
| 22 | if ap.addresses: |
| 23 | avail_ips = ap.addresses.split() |
| 24 | else: |
| 25 | avail_ips = [] |
| 26 | |
| 27 | if ap.inuse: |
| 28 | inuse_ips = ap.inuse.split() |
| 29 | else: |
| 30 | inuse_ips = [] |
| 31 | |
| 32 | while avail_ips: |
| 33 | addr = avail_ips.pop(0) |
| 34 | |
| 35 | if addr in inuse_ips: |
| 36 | # This may have happened if someone re-ran the tosca |
| 37 | # recipe and 'refilled' the AddressPool while some addresses |
| 38 | # were still in use. |
| 39 | continue |
| 40 | |
| 41 | inuse_ips.insert(0, addr) |
| 42 | |
| 43 | ap.inuse = " ".join(inuse_ips) |
| 44 | ap.addresses = " ".join(avail_ips) |
| 45 | return addr |
| 46 | |
| 47 | return None |
| 48 | |
Scott Baker | 2c465a6 | 2017-06-09 14:21:11 -0700 | [diff] [blame] | 49 | def put_address(self, addr): |
| 50 | ap = self |
| 51 | addresses = ap.addresses or "" |
| 52 | parts = addresses.split() |
| 53 | if addr not in parts: |
| 54 | parts.insert(0, addr) |
| 55 | ap.addresses = " ".join(parts) |
| 56 | |
| 57 | inuse = ap.inuse or "" |
| 58 | parts = inuse.split() |
| 59 | if addr in parts: |
| 60 | parts.remove(addr) |
| 61 | ap.inuse = " ".join(parts) |
| 62 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 63 | |
Scott Baker | 2c465a6 | 2017-06-09 14:21:11 -0700 | [diff] [blame] | 64 | register_convenience_wrapper("AddressPool", ORMWrapperAddressPool) |