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 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 16 | from __future__ import absolute_import |
| 17 | |
Scott Baker | 2c465a6 | 2017-06-09 14:21:11 -0700 | [diff] [blame] | 18 | from xosapi.orm import ORMWrapper, register_convenience_wrapper |
| 19 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 20 | |
Scott Baker | 2c465a6 | 2017-06-09 14:21:11 -0700 | [diff] [blame] | 21 | class 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 Baker | 2c465a6 | 2017-06-09 14:21:11 -0700 | [diff] [blame] | 51 | 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 65 | |
Scott Baker | 2c465a6 | 2017-06-09 14:21:11 -0700 | [diff] [blame] | 66 | register_convenience_wrapper("AddressPool", ORMWrapperAddressPool) |