blob: beecb568fc3c8d8032549d253cbf053b97670131 [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"]
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
vinokuma926cb3e2023-03-29 11:41:06 +0530110 // Ignore if UNI port is not UP
Naveen Sampath04696f72022-06-13 15:19:14 +0530111 if vp.State != app.PortStateUp {
112 return true
113 }
114
vinokuma926cb3e2023-03-29 11:41:06 +0530115 // Obtain all VPVs associated with the port
Naveen Sampath04696f72022-06-13 15:19:14 +0530116 vnets, ok := va.VnetsByPort.Load(port)
117 if !ok {
118 return true
119 }
120
121 for _, vpv := range vnets.([]*app.VoltPortVnet) {
vinokuma926cb3e2023-03-29 11:41:06 +0530122 // When only device id is provided as argument
Naveen Sampath04696f72022-06-13 15:19:14 +0530123 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 Sampath04696f72022-06-13 15:19:14 +0530160}