blob: 9305d0839bb72ce88d376804ff9fe153dcd8b26f [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
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.
vinokuma926cb3e2023-03-29 11:41:06 +053014 */
Naveen Sampath04696f72022-06-13 15:19:14 +053015
16package nbi
17
18import (
19 "encoding/json"
20 "net"
21 "net/http"
22 "strconv"
23 "time"
24
Naveen Sampath04696f72022-06-13 15:19:14 +053025 app "voltha-go-controller/internal/pkg/application"
Tinoj Joseph1d108322022-07-13 10:07:39 +053026 "voltha-go-controller/log"
vinokuma926cb3e2023-03-29 11:41:06 +053027
28 "github.com/gorilla/mux"
Naveen Sampath04696f72022-06-13 15:19:14 +053029)
30
vinokuma926cb3e2023-03-29 11:41:06 +053031// DHCPSessionInfoHandle handle dhcp session Requests
Naveen Sampath04696f72022-06-13 15:19:14 +053032type DHCPSessionInfoHandle struct {
33}
34
35// DhcpSessionInfo Information
36type 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
52func 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
vinokuma926cb3e2023-03-29 11:41:06 +053063// validateArgs validate the arguments
Naveen Sampath04696f72022-06-13 15:19:14 +053064func 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.
83func (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 {
vinokuma926cb3e2023-03-29 11:41:06 +053086 case cGet:
Naveen Sampath04696f72022-06-13 15:19:14 +053087 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.
94func (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 Sampath04696f72022-06-13 15:19:14 +0530100 var dhcpData *DhcpSessionInfo
101
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +0530102 logger.Infow(ctx, "Received get Dhcp Session Info", log.Fields{"DeviceID": id})
103
Akash Soni6f369452023-09-19 11:18:28 +0530104 var voltAppIntr app.VoltAppInterface
105 voltApp := app.GetApplication()
106 voltAppIntr = voltApp
Naveen Sampath04696f72022-06-13 15:19:14 +0530107 dhcpSessionInfoResp := []*DhcpSessionInfo{}
108
109 getPorts := func(key, value interface{}) bool {
110 port := key.(string)
111 vp := value.(*app.VoltPort)
112
vinokuma926cb3e2023-03-29 11:41:06 +0530113 // Ignore if UNI port is not UP
Naveen Sampath04696f72022-06-13 15:19:14 +0530114 if vp.State != app.PortStateUp {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +0530115 logger.Warnw(ctx, "Ignore if UNI port is not UP", log.Fields{"VoltPort State": vp.State, "PortStateUp": app.PortStateUp})
Naveen Sampath04696f72022-06-13 15:19:14 +0530116 return true
117 }
118
vinokuma926cb3e2023-03-29 11:41:06 +0530119 // Obtain all VPVs associated with the port
Akash Soni6f369452023-09-19 11:18:28 +0530120 vnets, ok := voltApp.VnetsByPort.Load(port)
Naveen Sampath04696f72022-06-13 15:19:14 +0530121 if !ok {
122 return true
123 }
124
125 for _, vpv := range vnets.([]*app.VoltPortVnet) {
vinokuma926cb3e2023-03-29 11:41:06 +0530126 // When only device id is provided as argument
Naveen Sampath04696f72022-06-13 15:19:14 +0530127 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 Chhabra8c3f1662023-07-19 13:15:16 +0530143 logger.Warnw(ctx, "No Device Id Provided for Dhcp session Info", log.Fields{"DeviceID": id})
Naveen Sampath04696f72022-06-13 15:19:14 +0530144 return
145 }
Akash Soni6f369452023-09-19 11:18:28 +0530146 voltDevice := voltAppIntr.GetDevice(id)
Naveen Sampath04696f72022-06-13 15:19:14 +0530147 if voltDevice != nil {
148 voltDevice.Ports.Range(getPorts)
149 }
150
151 dhcpSessionInfoJSON, err := json.Marshal(dhcpSessionInfoResp)
152 if err != nil {
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +0530153 logger.Errorw(ctx, "Error occurred while marshaling dhcp session info response", log.Fields{"DeviceID": id, "DhcpSessionInfoResp": dhcpSessionInfoResp, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530154 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 Chhabra8c3f1662023-07-19 13:15:16 +0530161 logger.Errorw(ctx, "error in sending dhcp session info response", log.Fields{"DeviceID": id, "DhcpSessionInfo": dhcpSessionInfoResp, "Error": err})
Naveen Sampath04696f72022-06-13 15:19:14 +0530162 w.WriteHeader(http.StatusInternalServerError)
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +0530163 return
Naveen Sampath04696f72022-06-13 15:19:14 +0530164 }
Hitesh Chhabra8c3f1662023-07-19 13:15:16 +0530165 logger.Debugw(ctx, "Fetching Dhcp Session Info", log.Fields{"DhcpSessionInfo": dhcpSessionInfoResp})
Naveen Sampath04696f72022-06-13 15:19:14 +0530166}