Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 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. |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 14 | */ |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 15 | |
| 16 | package nbi |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
| 20 | "net" |
| 21 | "net/http" |
| 22 | "strconv" |
| 23 | "time" |
| 24 | |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 25 | app "voltha-go-controller/internal/pkg/application" |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 26 | "voltha-go-controller/log" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 27 | |
| 28 | "github.com/gorilla/mux" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 29 | ) |
| 30 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 31 | // DHCPSessionInfoHandle handle dhcp session Requests |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 32 | type DHCPSessionInfoHandle struct { |
| 33 | } |
| 34 | |
| 35 | // DhcpSessionInfo Information |
| 36 | type DhcpSessionInfo struct { |
| 37 | DeviceID string |
| 38 | Uniport string |
| 39 | Svlan string |
| 40 | Cvlan string |
| 41 | UniVlan string |
| 42 | MacAddress string |
| 43 | IPAddress string |
| 44 | Ipv6Address string |
| 45 | State string |
| 46 | Statev6 string |
| 47 | LeaseTime string |
| 48 | LeaseTimev6 string |
| 49 | } |
| 50 | |
| 51 | // getDhcpSessionFields returns dhcp session information |
| 52 | func getDhcpSessionFields(id string, port string, svlan string, cvlan string, univlan string, macAddr string, ipAddr net.IP, ipv6Addr net.IP, rState app.DhcpRelayState, rStatev6 app.Dhcpv6RelayState, lTime time.Time, l6Time time.Time) *DhcpSessionInfo { |
| 53 | ip := ipAddr.String() |
| 54 | ipv6 := ipv6Addr.String() |
| 55 | relayState := strconv.Itoa(int(rState)) |
| 56 | relayStatev6 := strconv.Itoa(int(rStatev6)) |
| 57 | leaseTime := (lTime.Format(time.RubyDate)) |
| 58 | leasev6Time := (l6Time.Format(time.RubyDate)) |
| 59 | dInfo := &DhcpSessionInfo{DeviceID: id, Uniport: port, Svlan: svlan, Cvlan: cvlan, UniVlan: univlan, MacAddress: macAddr, IPAddress: ip, Ipv6Address: ipv6, State: relayState, Statev6: relayStatev6, LeaseTime: leaseTime, LeaseTimev6: leasev6Time} |
| 60 | return dInfo |
| 61 | } |
| 62 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 63 | // validateArgs validate the arguments |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 64 | func validateArgs(sv string, cv string, macAddr string, svlan string, cvlan string, mac string) bool { |
| 65 | var vlanFlag bool |
| 66 | var macFlag bool |
| 67 | |
| 68 | if ((sv == svlan) || (len(svlan) == 0)) && ((cv == cvlan) || (len(cvlan) == 0)) { |
| 69 | vlanFlag = true |
| 70 | } |
| 71 | |
| 72 | if mac == macAddr || len(mac) == 0 { |
| 73 | macFlag = true |
| 74 | } |
| 75 | |
| 76 | if macFlag && vlanFlag { |
| 77 | return true |
| 78 | } |
| 79 | return false |
| 80 | } |
| 81 | |
| 82 | // serveHTTP for actions performed on API. |
| 83 | func (dh *DHCPSessionInfoHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 84 | logger.Infow(ctx, "Received-northbound-request", log.Fields{"Method": r.Method, "URL": r.URL}) |
| 85 | switch r.Method { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 86 | case cGet: |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 87 | dh.getDhcpSessionInfo(w, r) |
| 88 | default: |
| 89 | logger.Warnw(ctx, "Unsupported Method", log.Fields{"Method": r.Method}) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // getDhcpSessionInfo to retrieve dhcp session information. |
| 94 | func (dh *DHCPSessionInfoHandle) getDhcpSessionInfo(w http.ResponseWriter, r *http.Request) { |
| 95 | vars := mux.Vars(r) |
| 96 | id := vars["id"] |
| 97 | mac := vars["mac"] |
| 98 | svlan := vars["svlan"] |
| 99 | cvlan := vars["cvlan"] |
| 100 | |
| 101 | var dhcpData *DhcpSessionInfo |
| 102 | |
| 103 | va := app.GetApplication() |
| 104 | dhcpSessionInfoResp := []*DhcpSessionInfo{} |
| 105 | |
| 106 | getPorts := func(key, value interface{}) bool { |
| 107 | port := key.(string) |
| 108 | vp := value.(*app.VoltPort) |
| 109 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 110 | // Ignore if UNI port is not UP |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 111 | if vp.State != app.PortStateUp { |
| 112 | return true |
| 113 | } |
| 114 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 115 | // Obtain all VPVs associated with the port |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 116 | vnets, ok := va.VnetsByPort.Load(port) |
| 117 | if !ok { |
| 118 | return true |
| 119 | } |
| 120 | |
| 121 | for _, vpv := range vnets.([]*app.VoltPortVnet) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame^] | 122 | // When only device id is provided as argument |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 123 | sv := strconv.Itoa(int(vpv.SVlan)) |
| 124 | cv := strconv.Itoa(int(vpv.CVlan)) |
| 125 | uv := strconv.Itoa(int(vpv.UniVlan)) |
| 126 | macAddr := (vpv.MacAddr).String() |
| 127 | |
| 128 | validData := validateArgs(sv, cv, macAddr, svlan, cvlan, mac) |
| 129 | |
| 130 | if validData { |
| 131 | dhcpData = getDhcpSessionFields(id, vpv.Port, sv, cv, uv, macAddr, vpv.Ipv4Addr, vpv.Ipv6Addr, vpv.RelayState, vpv.RelayStatev6, vpv.DhcpExpiryTime, vpv.Dhcp6ExpiryTime) |
| 132 | dhcpSessionInfoResp = append(dhcpSessionInfoResp, dhcpData) |
| 133 | } |
| 134 | } |
| 135 | return true |
| 136 | } |
| 137 | |
| 138 | if len(id) == 0 { |
| 139 | logger.Errorw(ctx, "No Device Id Provided for Dhcp session Info", log.Fields{"DeviceID": id}) |
| 140 | return |
| 141 | } |
| 142 | voltDevice := va.GetDevice(id) |
| 143 | if voltDevice != nil { |
| 144 | voltDevice.Ports.Range(getPorts) |
| 145 | } |
| 146 | |
| 147 | dhcpSessionInfoJSON, err := json.Marshal(dhcpSessionInfoResp) |
| 148 | if err != nil { |
| 149 | logger.Errorw(ctx, "Error occurred while marshaling dhcp session info response", log.Fields{"Error": err}) |
| 150 | w.WriteHeader(http.StatusInternalServerError) |
| 151 | return |
| 152 | } |
| 153 | |
| 154 | w.Header().Add("Content-Type", "application/json") |
| 155 | _, err = w.Write(dhcpSessionInfoJSON) |
| 156 | if err != nil { |
| 157 | logger.Errorw(ctx, "error in sending dhcp session info response", log.Fields{"Error": err}) |
| 158 | w.WriteHeader(http.StatusInternalServerError) |
| 159 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 160 | } |