1 {-| Implementation of the Ganeti network objects.
3 This is does not (yet) cover all methods that are provided in the
4 corresponding python implementation (network.py).
10 Copyright (C) 2011, 2012, 2013 Google Inc.
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 , bitStringToBitVector
41 import qualified Data.Vector.Unboxed as V
45 -- | An address pool, holding a network plus internal and external
47 data AddressPool = AddressPool { network :: Network,
48 reservations :: V.Vector Bool,
49 extReservations :: V.Vector Bool }
52 -- | Create an address pool from a network.
53 createAddressPool :: Network -> Maybe AddressPool
56 let res = maybeStr2BitVec $ networkReservations n
57 ext_res = maybeStr2BitVec $ networkExtReservations n
58 in Just AddressPool { reservations = res
59 , extReservations = ext_res
63 -- | Checks the consistency of the network object. So far, only checks the
64 -- length of the reservation strings.
65 networkIsValid :: Network -> Bool
67 sameLength (networkReservations n) (networkExtReservations n)
69 -- | Checks if two maybe strings are both nothing or of equal length.
70 sameLength :: Maybe String -> Maybe String -> Bool
71 sameLength Nothing Nothing = True
72 sameLength (Just s1) (Just s2) = length s1 == length s2
73 sameLength _ _ = False
75 -- | Converts a maybe bit string to a bit vector. Returns an empty bit vector on
77 maybeStr2BitVec :: Maybe String -> V.Vector Bool
78 maybeStr2BitVec (Just s) = bitStringToBitVector s
79 maybeStr2BitVec Nothing = V.fromList ([]::[Bool])
81 -- | Converts a string to a bit vector. The character '0' is interpreted
82 -- as 'False', all others as 'True'.
83 bitStringToBitVector :: String -> V.Vector Bool
84 bitStringToBitVector = V.fromList . map (/= '0')
86 -- | Get a bit vector of all reservations (internal and external) combined.
87 allReservations :: AddressPool -> V.Vector Bool
88 allReservations a = V.zipWith (||) (reservations a) (extReservations a)
90 -- | Get the count of reserved addresses.
91 getReservedCount :: AddressPool -> Int
92 getReservedCount = V.length . V.filter (== True) . allReservations
94 -- | Get the count of free addresses.
95 getFreeCount :: AddressPool -> Int
96 getFreeCount = V.length . V.filter (== False) . allReservations
98 -- | Check whether the network is full.
99 isFull :: AddressPool -> Bool
100 isFull = V.and . allReservations
102 -- | Return a textual representation of the network's occupation status.
103 getMap :: AddressPool -> String
104 getMap = V.toList . V.map mapPixel . allReservations
105 where mapPixel c = if c then 'X' else '.'