Brian O'Connor | 6a37ea9 | 2017-08-03 22:45:59 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Foundation |
David K. Bainbridge | 732957f | 2016-10-06 22:36:59 -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. |
| 14 | package main |
| 15 | |
| 16 | import ( |
| 17 | "encoding/json" |
| 18 | "github.com/gorilla/mux" |
| 19 | "net/http" |
| 20 | "strings" |
| 21 | "time" |
| 22 | ) |
| 23 | |
| 24 | // listLeaseHandler returns a list of all known leases |
| 25 | func (app *application) listLeasesHandler(w http.ResponseWriter, r *http.Request) { |
| 26 | |
| 27 | // convert data map of leases to a slice |
| 28 | app.interchange.RLock() |
| 29 | leases := make([]Lease, len(app.leases)) |
| 30 | i := 0 |
| 31 | for _, lease := range app.leases { |
| 32 | leases[i] = *lease |
| 33 | i += 1 |
| 34 | } |
| 35 | app.interchange.RUnlock() |
| 36 | |
| 37 | w.Header().Set("Content-Type", "application/json") |
| 38 | encoder := json.NewEncoder(w) |
| 39 | encoder.Encode(leases) |
| 40 | } |
| 41 | |
| 42 | // getLeaseHandler return a single known lease |
| 43 | func (app *application) getLeaseHandler(w http.ResponseWriter, r *http.Request) { |
| 44 | vars := mux.Vars(r) |
| 45 | ip, ok := vars["ip"] |
| 46 | if !ok || strings.TrimSpace(ip) == "" { |
| 47 | w.WriteHeader(http.StatusBadRequest) |
| 48 | return |
| 49 | } |
| 50 | app.interchange.RLock() |
| 51 | lease, ok := app.leases[ip] |
| 52 | app.interchange.RUnlock() |
| 53 | if !ok { |
| 54 | w.WriteHeader(http.StatusNotFound) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | w.Header().Set("Content-Type", "application/json") |
| 59 | encoder := json.NewEncoder(w) |
| 60 | encoder.Encode(lease) |
| 61 | } |
| 62 | |
| 63 | // getLeaseByHardware return a single known lease by its MAC address |
| 64 | func (app *application) getLeaseByHardware(w http.ResponseWriter, r *http.Request) { |
| 65 | vars := mux.Vars(r) |
| 66 | mac, ok := vars["mac"] |
| 67 | if !ok || strings.TrimSpace(mac) == "" { |
| 68 | w.WriteHeader(http.StatusBadRequest) |
| 69 | return |
| 70 | } |
| 71 | app.interchange.RLock() |
| 72 | lease, ok := app.byHardware[mac] |
| 73 | app.interchange.RUnlock() |
| 74 | if !ok { |
| 75 | w.WriteHeader(http.StatusNotFound) |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | w.Header().Set("Content-Type", "application/json") |
| 80 | encoder := json.NewEncoder(w) |
| 81 | encoder.Encode(lease) |
| 82 | } |
| 83 | |
| 84 | // getLeaseByHostname return a single known lease by its hostname |
| 85 | func (app *application) getLeaseByHostname(w http.ResponseWriter, r *http.Request) { |
| 86 | vars := mux.Vars(r) |
| 87 | name, ok := vars["name"] |
| 88 | if !ok || strings.TrimSpace(name) == "" { |
| 89 | w.WriteHeader(http.StatusBadRequest) |
| 90 | return |
| 91 | } |
| 92 | app.interchange.RLock() |
| 93 | lease, ok := app.byHostname[name] |
| 94 | app.interchange.RUnlock() |
| 95 | if !ok { |
| 96 | w.WriteHeader(http.StatusNotFound) |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | w.Header().Set("Content-Type", "application/json") |
| 101 | encoder := json.NewEncoder(w) |
| 102 | encoder.Encode(lease) |
| 103 | } |
| 104 | |
| 105 | // doHarvestHandler request a harvest of lease information and return if it was completed or during the quiet period |
| 106 | func (app *application) doHarvestHandler(w http.ResponseWriter, r *http.Request) { |
| 107 | app.log.Info("Manual harvest invocation") |
| 108 | responseChan := make(chan uint) |
| 109 | app.requests <- &responseChan |
| 110 | select { |
| 111 | case response := <-responseChan: |
| 112 | switch response { |
| 113 | case responseOK: |
| 114 | w.Header().Set("Content-Type", "application/json") |
| 115 | encoder := json.NewEncoder(w) |
| 116 | encoder.Encode(struct { |
| 117 | Response string `json:"response"` |
| 118 | }{ |
| 119 | Response: "OK", |
| 120 | }) |
| 121 | case responseQuiet: |
| 122 | w.Header().Set("Content-Type", "application/json") |
| 123 | encoder := json.NewEncoder(w) |
| 124 | encoder.Encode(struct { |
| 125 | Response string `json:"response"` |
| 126 | }{ |
| 127 | Response: "QUIET", |
| 128 | }) |
| 129 | default: |
| 130 | w.WriteHeader(http.StatusInternalServerError) |
| 131 | } |
| 132 | case <-time.After(app.RequestTimeout): |
| 133 | app.log.Error("Request to process DHCP lease file timed out") |
| 134 | w.WriteHeader(http.StatusInternalServerError) |
| 135 | } |
| 136 | } |