blob: 6a0964dd2d67099f459e8dfa5670ac10ef57ee60 [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. Bainbridgef694f5a2016-06-10 16:21:27 -070014package main
15
16import (
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070017 "fmt"
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070018 "net/url"
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070019 "time"
20)
21
22func 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. Bainbridge97ee8052016-06-14 00:52:07 -070029 return NewMemoryStorage()
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070030 default:
31 }
32 return nil, fmt.Errorf("Unknown storage scheme specified, '%s'", u.Scheme)
33}
34
35type Storage interface {
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070036 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. Bainbridge97ee8052016-06-14 00:52:07 -070040 ClearProvisioned(mac string) error
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070041}
42
43type MemoryStorage struct {
David K. Bainbridge97ee8052016-06-14 00:52:07 -070044 Checks map[string]time.Time
45 Times map[string]time.Time
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070046}
47
David K. Bainbridge97ee8052016-06-14 00:52:07 -070048func NewMemoryStorage() (Storage, error) {
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070049
David K. Bainbridge97ee8052016-06-14 00:52:07 -070050 s := MemoryStorage{
51 Checks: make(map[string]time.Time),
52 Times: make(map[string]time.Time),
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070053 }
54 return &s, nil
55}
56
David K. Bainbridgef694f5a2016-06-10 16:21:27 -070057func (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
66func (s *MemoryStorage) MarkMACCheck(mac string, when *time.Time) error {
67 s.Checks[mac] = *when
68 return nil
69}
70
71func (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
80func (s *MemoryStorage) MarkProvisioned(mac string, when *time.Time) error {
81 s.Times[mac] = *when
82 return nil
83}
David K. Bainbridge97ee8052016-06-14 00:52:07 -070084
85func (s *MemoryStorage) ClearProvisioned(mac string) error {
86 delete(s.Times, mac)
87 return nil
88}