module Ganeti.Network
( AddressPool(..)
, createAddressPool
, bitStringToBitVector
, allReservations
, getReservedCount
, getFreeCount
, isFull
, getMap
, networkIsValid
) where
import qualified Data.Vector.Unboxed as V
import Ganeti.Objects
data AddressPool = AddressPool { network :: Network,
reservations :: V.Vector Bool,
extReservations :: V.Vector Bool }
deriving (Show)
createAddressPool :: Network -> Maybe AddressPool
createAddressPool n
| networkIsValid n =
let res = maybeStr2BitVec $ networkReservations n
ext_res = maybeStr2BitVec $ networkExtReservations n
in Just AddressPool { reservations = res
, extReservations = ext_res
, network = n }
| otherwise = Nothing
networkIsValid :: Network -> Bool
networkIsValid n = sameLength (networkReservations n) (networkExtReservations n)
sameLength :: Maybe String -> Maybe String -> Bool
sameLength Nothing Nothing = True
sameLength (Just s1) (Just s2) = length s1 == length s2
sameLength _ _ = False
maybeStr2BitVec :: Maybe String -> V.Vector Bool
maybeStr2BitVec (Just s) = bitStringToBitVector s
maybeStr2BitVec Nothing = V.fromList ([]::[Bool])
bitStringToBitVector :: String -> V.Vector Bool
bitStringToBitVector = V.fromList . map (/= '0')
allReservations :: AddressPool -> V.Vector Bool
allReservations a = V.zipWith (||) (reservations a) (extReservations a)
getReservedCount :: AddressPool -> Int
getReservedCount = V.length . V.filter (== True) . allReservations
getFreeCount :: AddressPool -> Int
getFreeCount = V.length . V.filter (== False) . allReservations
isFull :: AddressPool -> Bool
isFull = V.and . allReservations
getMap :: AddressPool -> String
getMap = V.toList . V.map mapPixel . allReservations
where mapPixel c = if c then 'X' else '.'