Brian O'Connor | 6a37ea9 | 2017-08-03 22:45:59 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Foundation |
David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 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 | 8bc905c | 2016-05-31 14:07:10 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "encoding/json" |
| 18 | "github.com/gorilla/mux" |
| 19 | "net/http" |
| 20 | ) |
| 21 | |
| 22 | type ErrorMsg struct { |
| 23 | Error string |
| 24 | } |
| 25 | |
| 26 | type AllocationMsg struct { |
| 27 | Mac string |
| 28 | Ip string |
| 29 | } |
| 30 | |
| 31 | func (c *Context) release(mac string, w http.ResponseWriter) { |
| 32 | err := Release(c.storage, mac) |
| 33 | if err != nil { |
| 34 | msg := ErrorMsg{ |
| 35 | Error: err.Error(), |
| 36 | } |
| 37 | bytes, err := json.Marshal(&msg) |
| 38 | if err != nil { |
| 39 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 40 | return |
| 41 | } |
| 42 | http.Error(w, string(bytes), http.StatusInternalServerError) |
| 43 | return |
| 44 | } |
| 45 | w.WriteHeader(http.StatusOK) |
| 46 | } |
| 47 | |
| 48 | func (c *Context) ReleaseAllocationHandler(w http.ResponseWriter, r *http.Request) { |
| 49 | vars := mux.Vars(r) |
| 50 | mac := vars["mac"] |
| 51 | c.release(mac, w) |
| 52 | } |
| 53 | |
| 54 | func (c *Context) AllocationHandler(w http.ResponseWriter, r *http.Request) { |
| 55 | vars := mux.Vars(r) |
| 56 | mac := vars["mac"] |
| 57 | |
| 58 | ip, err := Allocate(c.storage, mac) |
| 59 | if err != nil { |
| 60 | msg := ErrorMsg{ |
| 61 | Error: err.Error(), |
| 62 | } |
| 63 | bytes, err := json.Marshal(&msg) |
| 64 | if err != nil { |
| 65 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 66 | return |
| 67 | } |
| 68 | http.Error(w, string(bytes), http.StatusInternalServerError) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | msg := AllocationMsg{ |
| 73 | Mac: mac, |
| 74 | Ip: ip, |
| 75 | } |
| 76 | bytes, err := json.Marshal(&msg) |
| 77 | if err != nil { |
| 78 | msg := ErrorMsg{ |
| 79 | Error: err.Error(), |
| 80 | } |
| 81 | bytes, err := json.Marshal(&msg) |
| 82 | if err != nil { |
| 83 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 84 | return |
| 85 | } |
| 86 | http.Error(w, string(bytes), http.StatusInternalServerError) |
| 87 | return |
| 88 | } |
| 89 | w.Write(bytes) |
| 90 | } |
| 91 | |
| 92 | func (c *Context) ListAllocationsHandler(w http.ResponseWriter, r *http.Request) { |
| 93 | all := c.storage.GetAll() |
| 94 | |
| 95 | list := make([]AllocationMsg, len(all)) |
| 96 | i := 0 |
| 97 | for k, v := range all { |
| 98 | list[i].Mac = k |
| 99 | list[i].Ip = v |
| 100 | i += 1 |
| 101 | } |
| 102 | |
| 103 | bytes, err := json.Marshal(&list) |
| 104 | if err != nil { |
| 105 | msg := ErrorMsg{ |
| 106 | Error: err.Error(), |
| 107 | } |
| 108 | bytes, err := json.Marshal(&msg) |
| 109 | if err != nil { |
| 110 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 111 | return |
| 112 | } |
| 113 | http.Error(w, string(bytes), http.StatusInternalServerError) |
| 114 | return |
| 115 | } |
| 116 | w.Write(bytes) |
| 117 | } |
| 118 | |
| 119 | func (c *Context) FreeAddressHandler(w http.ResponseWriter, r *http.Request) { |
| 120 | vars := mux.Vars(r) |
| 121 | ip := vars["ip"] |
| 122 | |
| 123 | all := c.storage.GetAll() |
| 124 | for k, v := range all { |
| 125 | if v == ip { |
| 126 | c.release(k, w) |
| 127 | return |
| 128 | } |
| 129 | } |
| 130 | http.Error(w, "", http.StatusNotFound) |
| 131 | } |