root / src / Ganeti / Network.hs @ 8c3b6093
History | View | Annotate | Download (3.8 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, 2013 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 |
import Data.Maybe (isJust) |
43 |
|
44 |
import Ganeti.Objects |
45 |
|
46 |
-- | An address pool, holding a network plus internal and external |
47 |
-- reservations. |
48 |
data AddressPool = AddressPool { network :: Network, |
49 |
reservations :: V.Vector Bool, |
50 |
extReservations :: V.Vector Bool } |
51 |
deriving (Show) |
52 |
|
53 |
-- | Create an address pool from a network. |
54 |
createAddressPool :: Network -> Maybe AddressPool |
55 |
createAddressPool n |
56 |
| networkHasPool n = |
57 |
let res = maybeStr2BitVec $ networkReservations n |
58 |
ext_res = maybeStr2BitVec $ networkExtReservations n |
59 |
in Just AddressPool { reservations = res |
60 |
, extReservations = ext_res |
61 |
, network = n } |
62 |
| otherwise = Nothing |
63 |
|
64 |
-- | Checks the consistency of the network object. So far, only checks the |
65 |
-- length of the reservation strings. |
66 |
networkHasPool :: Network -> Bool |
67 |
networkHasPool = isJust . networkNetwork |
68 |
|
69 |
-- | Checks the consistency of the network object. So far, only checks the |
70 |
-- length of the reservation strings. |
71 |
networkIsValid :: Network -> Bool |
72 |
networkIsValid n = |
73 |
sameLength (networkReservations n) (networkExtReservations n) |
74 |
|
75 |
-- | Checks if two maybe strings are both nothing or of equal length. |
76 |
sameLength :: Maybe String -> Maybe String -> Bool |
77 |
sameLength Nothing Nothing = True |
78 |
sameLength (Just s1) (Just s2) = length s1 == length s2 |
79 |
sameLength _ _ = False |
80 |
|
81 |
-- | Converts a maybe bit string to a bit vector. Returns an empty bit vector on |
82 |
-- nothing. |
83 |
maybeStr2BitVec :: Maybe String -> V.Vector Bool |
84 |
maybeStr2BitVec (Just s) = bitStringToBitVector s |
85 |
maybeStr2BitVec Nothing = V.fromList ([]::[Bool]) |
86 |
|
87 |
-- | Converts a string to a bit vector. The character '0' is interpreted |
88 |
-- as 'False', all others as 'True'. |
89 |
bitStringToBitVector :: String -> V.Vector Bool |
90 |
bitStringToBitVector = V.fromList . map (/= '0') |
91 |
|
92 |
-- | Get a bit vector of all reservations (internal and external) combined. |
93 |
allReservations :: AddressPool -> V.Vector Bool |
94 |
allReservations a = V.zipWith (||) (reservations a) (extReservations a) |
95 |
|
96 |
-- | Get the count of reserved addresses. |
97 |
getReservedCount :: AddressPool -> Int |
98 |
getReservedCount = V.length . V.filter (== True) . allReservations |
99 |
|
100 |
-- | Get the count of free addresses. |
101 |
getFreeCount :: AddressPool -> Int |
102 |
getFreeCount = V.length . V.filter (== False) . allReservations |
103 |
|
104 |
-- | Check whether the network is full. |
105 |
isFull :: AddressPool -> Bool |
106 |
isFull = V.and . allReservations |
107 |
|
108 |
-- | Return a textual representation of the network's occupation status. |
109 |
getMap :: AddressPool -> String |
110 |
getMap = V.toList . V.map mapPixel . allReservations |
111 |
where mapPixel c = if c then 'X' else '.' |