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"] |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 100 | var dhcpData *DhcpSessionInfo |
| 101 | |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 102 | logger.Infow(ctx, "Received get Dhcp Session Info", log.Fields{"DeviceID": id}) |
| 103 | |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 104 | var voltAppIntr app.VoltAppInterface |
| 105 | voltApp := app.GetApplication() |
| 106 | voltAppIntr = voltApp |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 107 | dhcpSessionInfoResp := []*DhcpSessionInfo{} |
| 108 | |
| 109 | getPorts := func(key, value interface{}) bool { |
| 110 | port := key.(string) |
| 111 | vp := value.(*app.VoltPort) |
| 112 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 113 | // Ignore if UNI port is not UP |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 114 | if vp.State != app.PortStateUp { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 115 | logger.Warnw(ctx, "Ignore if UNI port is not UP", log.Fields{"VoltPort State": vp.State, "PortStateUp": app.PortStateUp}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 116 | return true |
| 117 | } |
| 118 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 119 | // Obtain all VPVs associated with the port |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 120 | vnets, ok := voltApp.VnetsByPort.Load(port) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 121 | if !ok { |
| 122 | return true |
| 123 | } |
| 124 | |
| 125 | for _, vpv := range vnets.([]*app.VoltPortVnet) { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 126 | // When only device id is provided as argument |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 127 | sv := strconv.Itoa(int(vpv.SVlan)) |
| 128 | cv := strconv.Itoa(int(vpv.CVlan)) |
| 129 | uv := strconv.Itoa(int(vpv.UniVlan)) |
| 130 | macAddr := (vpv.MacAddr).String() |
| 131 | |
| 132 | validData := validateArgs(sv, cv, macAddr, svlan, cvlan, mac) |
| 133 | |
| 134 | if validData { |
| 135 | dhcpData = getDhcpSessionFields(id, vpv.Port, sv, cv, uv, macAddr, vpv.Ipv4Addr, vpv.Ipv6Addr, vpv.RelayState, vpv.RelayStatev6, vpv.DhcpExpiryTime, vpv.Dhcp6ExpiryTime) |
| 136 | dhcpSessionInfoResp = append(dhcpSessionInfoResp, dhcpData) |
| 137 | } |
| 138 | } |
| 139 | return true |
| 140 | } |
| 141 | |
| 142 | if len(id) == 0 { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 143 | logger.Warnw(ctx, "No Device Id Provided for Dhcp session Info", log.Fields{"DeviceID": id}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 144 | return |
| 145 | } |
Akash Soni | 6f36945 | 2023-09-19 11:18:28 +0530 | [diff] [blame] | 146 | voltDevice := voltAppIntr.GetDevice(id) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 147 | if voltDevice != nil { |
| 148 | voltDevice.Ports.Range(getPorts) |
| 149 | } |
| 150 | |
| 151 | dhcpSessionInfoJSON, err := json.Marshal(dhcpSessionInfoResp) |
| 152 | if err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 153 | logger.Errorw(ctx, "Error occurred while marshaling dhcp session info response", log.Fields{"DeviceID": id, "DhcpSessionInfoResp": dhcpSessionInfoResp, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 154 | w.WriteHeader(http.StatusInternalServerError) |
| 155 | return |
| 156 | } |
| 157 | |
| 158 | w.Header().Add("Content-Type", "application/json") |
| 159 | _, err = w.Write(dhcpSessionInfoJSON) |
| 160 | if err != nil { |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 161 | logger.Errorw(ctx, "error in sending dhcp session info response", log.Fields{"DeviceID": id, "DhcpSessionInfo": dhcpSessionInfoResp, "Error": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 162 | w.WriteHeader(http.StatusInternalServerError) |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 163 | return |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 164 | } |
Hitesh Chhabra | 8c3f166 | 2023-07-19 13:15:16 +0530 | [diff] [blame] | 165 | logger.Debugw(ctx, "Fetching Dhcp Session Info", log.Fields{"DhcpSessionInfo": dhcpSessionInfoResp}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 166 | } |