blob: 13b21ed4618879106cfd0535a9b654626a79299a [file] [log] [blame]
David K. Bainbridgedf9df632016-07-07 18:47:46 -07001// Copyright 2016 Open Networking Laboratory
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
David K. Bainbridge8bc905c2016-05-31 14:07:10 -070014package main
15
16func Allocate(store Storage, mac string) (string, error) {
17 // Check to see if an IP address is already allocated and if so just
18 // return that value
19 ip, err := store.Get(mac)
20 if err != nil {
21 return "", err
22 }
23
24 if ip != "" {
25 return ip, nil
26 }
27
28 // This MAC does not already have an IP assigned, so pull then next
29 // one off the available queue and return it
30 ip, err = store.Dequeue()
31 if err != nil {
32 return "", err
33 }
34 err = store.Put(mac, ip)
35 if err != nil {
36 store.Enqueue(ip)
37 return "", err
38 }
39 return ip, nil
40}
41
42func Release(store Storage, mac string) error {
43 ip, err := store.Remove(mac)
44 if err != nil {
45 return err
46 }
47
48 if ip != "" {
49 err = store.Enqueue(ip)
50 if err != nil {
51 return err
52 }
53 }
54 return nil
55}