Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Network.hs @ cefd4a4a

History | View | Annotate | Download (3.5 kB)

1
{-| Implementation of the Ganeti network objects.
2

    
3
This is does not (yet) cover all methods that are provided in the
4
corresponding python implementation (network.py).
5

    
6
-}
7

    
8
{-
9

    
10
Copyright (C) 2011, 2012 Google Inc.
11

    
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.
16

    
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.
21

    
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
25
02110-1301, USA.
26

    
27
-}
28

    
29
module Ganeti.Network
30
  ( AddressPool(..)
31
  , createAddressPool
32
  , bitStringToBitVector
33
  , allReservations
34
  , getReservedCount
35
  , getFreeCount
36
  , isFull
37
  , getMap
38
  , networkIsValid
39
  ) where
40

    
41
import qualified Data.Vector.Unboxed as V
42

    
43
import Ganeti.Objects
44

    
45
data AddressPool = AddressPool { network :: Network,
46
                                 reservations :: V.Vector Bool,
47
                                 extReservations :: V.Vector Bool }
48
                                 deriving (Show)
49

    
50
-- | Create an address pool from a network.
51
createAddressPool :: Network -> Maybe AddressPool
52
createAddressPool n
53
  | networkIsValid n =
54
      let res = maybeStr2BitVec $ networkReservations n
55
          ext_res = maybeStr2BitVec $ networkExtReservations n
56
      in  Just AddressPool { reservations = res
57
                           , extReservations = ext_res
58
                           , network = n }
59
  | otherwise = Nothing
60

    
61
-- | Checks the consistency of the network object. So far, only checks the
62
-- length of the reservation strings.
63
networkIsValid :: Network -> Bool
64
networkIsValid n = sameLength (networkReservations n) (networkExtReservations n)
65

    
66
-- | Checks if two maybe strings are both nothing or of equal length.
67
sameLength :: Maybe String -> Maybe String -> Bool
68
sameLength Nothing Nothing = True
69
sameLength (Just s1) (Just s2) = length s1 == length s2
70
sameLength _ _ = False
71

    
72
-- | Converts a maybe bit string to a bit vector. Returns an empty bit vector on
73
-- nothing.
74
maybeStr2BitVec :: Maybe String -> V.Vector Bool
75
maybeStr2BitVec (Just s) = bitStringToBitVector s
76
maybeStr2BitVec Nothing = V.fromList ([]::[Bool])
77

    
78
-- | Converts a string to a bit vector. The character '0' is interpreted
79
-- as 'False', all others as 'True'.
80
bitStringToBitVector :: String -> V.Vector Bool
81
bitStringToBitVector = V.fromList . map (/= '0')
82

    
83
-- | Get a bit vector of all reservations (internal and external) combined.
84
allReservations :: AddressPool -> V.Vector Bool
85
allReservations a = V.zipWith (||) (reservations a) (extReservations a)
86

    
87
-- | Get the count of reserved addresses.
88
getReservedCount :: AddressPool -> Int
89
getReservedCount = V.length . V.filter (== True) . allReservations
90

    
91
-- | Get the count of free addresses.
92
getFreeCount :: AddressPool -> Int
93
getFreeCount = V.length . V.filter (== False) . allReservations
94

    
95
-- | Check whether the network is full.
96
isFull :: AddressPool -> Bool
97
isFull = V.and . allReservations
98

    
99
-- | Return a textual representation of the network's occupation status.
100
getMap :: AddressPool -> String
101
getMap = V.toList . V.map mapPixel . allReservations
102
  where mapPixel c = if c then 'X' else '.'
103