David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame^] | 1 | // 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. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 17 | "fmt" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 18 | "net/url" |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 19 | "time" |
| 20 | ) |
| 21 | |
| 22 | func NewStorage(spec string) (Storage, error) { |
| 23 | u, err := url.Parse(spec) |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | switch u.Scheme { |
| 28 | case "memory": |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 29 | return NewMemoryStorage() |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 30 | default: |
| 31 | } |
| 32 | return nil, fmt.Errorf("Unknown storage scheme specified, '%s'", u.Scheme) |
| 33 | } |
| 34 | |
| 35 | type Storage interface { |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 36 | LastMACCheck(mac string) (*time.Time, error) |
| 37 | MarkMACCheck(mac string, when *time.Time) error |
| 38 | LastProvisioned(mac string) (*time.Time, error) |
| 39 | MarkProvisioned(mac string, when *time.Time) error |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 40 | ClearProvisioned(mac string) error |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | type MemoryStorage struct { |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 44 | Checks map[string]time.Time |
| 45 | Times map[string]time.Time |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 46 | } |
| 47 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 48 | func NewMemoryStorage() (Storage, error) { |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 49 | |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 50 | s := MemoryStorage{ |
| 51 | Checks: make(map[string]time.Time), |
| 52 | Times: make(map[string]time.Time), |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 53 | } |
| 54 | return &s, nil |
| 55 | } |
| 56 | |
David K. Bainbridge | f694f5a | 2016-06-10 16:21:27 -0700 | [diff] [blame] | 57 | func (s *MemoryStorage) LastMACCheck(mac string) (*time.Time, error) { |
| 58 | when, ok := s.Checks[mac] |
| 59 | if !ok { |
| 60 | return nil, nil |
| 61 | } |
| 62 | result := when |
| 63 | return &result, nil |
| 64 | } |
| 65 | |
| 66 | func (s *MemoryStorage) MarkMACCheck(mac string, when *time.Time) error { |
| 67 | s.Checks[mac] = *when |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | func (s *MemoryStorage) LastProvisioned(mac string) (*time.Time, error) { |
| 72 | when, ok := s.Times[mac] |
| 73 | if !ok { |
| 74 | return nil, nil |
| 75 | } |
| 76 | result := when |
| 77 | return &result, nil |
| 78 | } |
| 79 | |
| 80 | func (s *MemoryStorage) MarkProvisioned(mac string, when *time.Time) error { |
| 81 | s.Times[mac] = *when |
| 82 | return nil |
| 83 | } |
David K. Bainbridge | 97ee805 | 2016-06-14 00:52:07 -0700 | [diff] [blame] | 84 | |
| 85 | func (s *MemoryStorage) ClearProvisioned(mac string) error { |
| 86 | delete(s.Times, mac) |
| 87 | return nil |
| 88 | } |