blob: a2a8db80d71a45df5e13d4837f4680d2a1bb06d7 [file] [log] [blame]
Brian O'Connor6a37ea92017-08-03 22:45:59 -07001// Copyright 2016 Open Networking Foundation
David K. Bainbridge732957f2016-10-06 22:36:59 -07002//
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.
14package main
15
16import (
17 "encoding/json"
18 "fmt"
19 "net"
20 "strings"
21 "time"
22)
23
24// BindingState type used to maintain lease state
25type BindingState uint
26
27// constant values of lease binding state
28const (
29 Unknown BindingState = 0
30 Free BindingState = 1
31 Active BindingState = 2
32 Expired BindingState = 3
33 Released BindingState = 4
34 Abandoned BindingState = 5
35 Reset BindingState = 6
36 Backup BindingState = 7
37 Reserved BindingState = 8
38 Bootp BindingState = 9
39)
40
41// String return a string value for a lease binding state
42func (s *BindingState) String() string {
43 switch *s {
44 case 1:
45 return "Free"
46 case 2:
47 return "Active"
48 case 3:
49 return "Expired"
50 case 4:
51 return "Released"
52 case 5:
53 return "Abandoned"
54 case 6:
55 return "Reset"
56 case 7:
57 return "Backup"
58 case 8:
59 return "Reserved"
60 case 9:
61 return "Bootp"
62 default:
63 return "Unknown"
64 }
65}
66
67// Lease DHCP lease information
68type Lease struct {
69 BindingState BindingState `json:"binding-state"`
70 IPAddress net.IP `json:"ip-address"`
71 ClientHostname string `json:"client-hostname"`
72 HardwareAddress net.HardwareAddr `json:"hardware-address"`
73 Starts time.Time `json:"starts"`
74 Ends time.Time `json:"ends"`
75}
76
77// MarshalJSON custom marshaller for DHCP lease
78func (l *Lease) MarshalJSON() ([]byte, error) {
79
80 // a custom marshaller is required because the net.Hardware marshals to a string
81 // that is not in the standard MAC address format by default as well as the
82 // binding state is marshalled to a human readable string
83 type Alias Lease
84 return json.Marshal(&struct {
85 HardwareAddress string `json:"hardware-address"`
86 BindingState string `json:"binding-state"`
87 *Alias
88 }{
89 HardwareAddress: l.HardwareAddress.String(),
90 BindingState: l.BindingState.String(),
91 Alias: (*Alias)(l),
92 })
93}
94
95// parseBindingState conversts from a string to a valid binding state constant
96func parseBindingState(bindingState string) (BindingState, error) {
97 switch strings.ToLower(bindingState) {
98 case "free":
99 return Free, nil
100 case "active":
101 return Active, nil
102 case "expired":
103 return Expired, nil
104 case "released":
105 return Released, nil
106 case "abandoned":
107 return Abandoned, nil
108 case "reset":
109 return Reset, nil
110 case "backup":
111 return Backup, nil
112 case "reserved":
113 return Reserved, nil
114 case "bootp":
115 return Bootp, nil
116 case "unknown":
117 fallthrough
118 default:
119 return Unknown, nil
120 }
121
122 return 0, fmt.Errorf("Unknown lease binding state '%s'", bindingState)
123}