Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Network.hs @ 82b948e4

History | View | Annotate | Download (3.5 kB)

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