blob: 8b122947bfbe24b32d7dbb4014865dded5ee0128 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
2 Copyright 2017 the original author or authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17package openflow
18
19import (
20 "context"
21 "encoding/json"
Don Newtone0d34a82019-11-14 10:58:06 -050022
Don Newton98fd8812019-09-23 15:15:02 -040023 "net"
Don Newton7577f072020-01-06 12:41:11 -050024
Don Newton98fd8812019-09-23 15:15:02 -040025 "unsafe"
26
Don Newton7577f072020-01-06 12:41:11 -050027 "github.com/opencord/ofagent-go/settings"
28 l "github.com/opencord/voltha-lib-go/v2/pkg/log"
Don Newtonb437c6f2019-12-18 11:51:57 -050029 "github.com/opencord/voltha-protos/v2/go/openflow_13"
Don Newtone0d34a82019-11-14 10:58:06 -050030
31 "github.com/donNewtonAlpha/goloxi"
32 ofp "github.com/donNewtonAlpha/goloxi/of13"
Don Newtonb437c6f2019-12-18 11:51:57 -050033 "github.com/opencord/voltha-protos/v2/go/common"
Don Newton98fd8812019-09-23 15:15:02 -040034)
35
Don Newton7577f072020-01-06 12:41:11 -050036func handleStatsRequest(request ofp.IHeader, statType uint16, DeviceID string, client *Client) error {
37 if settings.GetDebug(DeviceID) {
38 js, _ := json.Marshal(request)
39 logger.Debugw("handleStatsRequest called", l.Fields{"DeviceID": DeviceID, "StatType": statType, "rquest": js})
40 }
41 var id = common.ID{Id: DeviceID}
Don Newton98fd8812019-09-23 15:15:02 -040042 switch statType {
Don Newtone0d34a82019-11-14 10:58:06 -050043 case ofp.OFPSTDesc:
Don Newton98fd8812019-09-23 15:15:02 -040044 statsReq := request.(*ofp.DescStatsRequest)
45 response, err := handleDescStatsRequest(statsReq, id)
46 if err != nil {
47 return err
48 }
Don Newton7577f072020-01-06 12:41:11 -050049 if settings.GetDebug(DeviceID) {
50 reqJs, _ := json.Marshal(statsReq)
51 resJs, _ := json.Marshal(response)
52 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
53 }
54 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -050055 case ofp.OFPSTFlow:
Don Newton98fd8812019-09-23 15:15:02 -040056 statsReq := request.(*ofp.FlowStatsRequest)
Don Newton7577f072020-01-06 12:41:11 -050057 response, err := handleFlowStatsRequest(statsReq, id, DeviceID)
Don Newtonb437c6f2019-12-18 11:51:57 -050058 if err != nil {
59 return err
60 }
Don Newtone0d34a82019-11-14 10:58:06 -050061 response.Length = uint16(unsafe.Sizeof(*response))
Don Newton7577f072020-01-06 12:41:11 -050062 if settings.GetDebug(DeviceID) {
63 reqJs, _ := json.Marshal(statsReq)
64 resJs, _ := json.Marshal(response)
65 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
Don Newton98fd8812019-09-23 15:15:02 -040066 }
Don Newton7577f072020-01-06 12:41:11 -050067 return client.SendMessage(response)
Don Newton98fd8812019-09-23 15:15:02 -040068
Don Newtone0d34a82019-11-14 10:58:06 -050069 case ofp.OFPSTAggregate:
Don Newton98fd8812019-09-23 15:15:02 -040070 statsReq := request.(*ofp.AggregateStatsRequest)
Don Newton7577f072020-01-06 12:41:11 -050071 response, err := handleAggregateStatsRequest(statsReq, id)
Don Newton98fd8812019-09-23 15:15:02 -040072 if err != nil {
73 return err
74 }
Don Newton7577f072020-01-06 12:41:11 -050075 if settings.GetDebug(DeviceID) {
76 reqJs, _ := json.Marshal(statsReq)
77 resJs, _ := json.Marshal(response)
78 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
79 }
80 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -050081 case ofp.OFPSTTable:
Don Newton98fd8812019-09-23 15:15:02 -040082 statsReq := request.(*ofp.TableStatsRequest)
Don Newton7577f072020-01-06 12:41:11 -050083 response, e := handleTableStatsRequest(statsReq, id)
84 if settings.GetDebug(DeviceID) {
85 reqJs, _ := json.Marshal(statsReq)
86 resJs, _ := json.Marshal(response)
87 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
88 }
Don Newton98fd8812019-09-23 15:15:02 -040089 if e != nil {
90 return e
91 }
Don Newton7577f072020-01-06 12:41:11 -050092 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -050093 case ofp.OFPSTPort:
Don Newton98fd8812019-09-23 15:15:02 -040094 statsReq := request.(*ofp.PortStatsRequest)
95 response, err := handlePortStatsRequest(statsReq, id)
96 if err != nil {
97 return err
98 }
Don Newton7577f072020-01-06 12:41:11 -050099 if settings.GetDebug(DeviceID) {
100 reqJs, _ := json.Marshal(statsReq)
101 resJs, _ := json.Marshal(response)
102 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
103 }
104 return client.SendMessage(response)
Don Newton98fd8812019-09-23 15:15:02 -0400105
Don Newtone0d34a82019-11-14 10:58:06 -0500106 case ofp.OFPSTQueue:
Don Newton98fd8812019-09-23 15:15:02 -0400107 statsReq := request.(*ofp.QueueStatsRequest)
108 response, err := handleQueueStatsRequest(statsReq, id)
109 if err != nil {
110 return err
111 }
Don Newton7577f072020-01-06 12:41:11 -0500112 if settings.GetDebug(DeviceID) {
113 reqJs, _ := json.Marshal(statsReq)
114 resJs, _ := json.Marshal(response)
115 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
116 }
117 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -0500118 case ofp.OFPSTGroup:
Don Newton98fd8812019-09-23 15:15:02 -0400119 statsReq := request.(*ofp.GroupStatsRequest)
120 response, err := handleGroupStatsRequest(statsReq, id)
121 if err != nil {
122 return err
123 }
Don Newton7577f072020-01-06 12:41:11 -0500124 if settings.GetDebug(DeviceID) {
125 reqJs, _ := json.Marshal(statsReq)
126 resJs, _ := json.Marshal(response)
127 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
128 }
Don Newton98fd8812019-09-23 15:15:02 -0400129 client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -0500130 case ofp.OFPSTGroupDesc:
Don Newton98fd8812019-09-23 15:15:02 -0400131 statsReq := request.(*ofp.GroupDescStatsRequest)
132 response, err := handleGroupStatsDescRequest(statsReq, id)
133 if err != nil {
134 return err
135 }
Don Newton7577f072020-01-06 12:41:11 -0500136 if settings.GetDebug(DeviceID) {
137 reqJs, _ := json.Marshal(statsReq)
138 resJs, _ := json.Marshal(response)
139 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
140 }
141 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -0500142
143 case ofp.OFPSTGroupFeatures:
Don Newton98fd8812019-09-23 15:15:02 -0400144 statsReq := request.(*ofp.GroupFeaturesStatsRequest)
145 response, err := handleGroupFeatureStatsRequest(statsReq, id)
146 if err != nil {
147 return err
148 }
Don Newton7577f072020-01-06 12:41:11 -0500149 if settings.GetDebug(DeviceID) {
150 reqJs, _ := json.Marshal(statsReq)
151 resJs, _ := json.Marshal(response)
152 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
153 }
154 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -0500155 case ofp.OFPSTMeter:
Don Newton98fd8812019-09-23 15:15:02 -0400156 statsReq := request.(*ofp.MeterStatsRequest)
157 response, err := handleMeterStatsRequest(statsReq, id)
158 if err != nil {
159 return err
160 }
Don Newton7577f072020-01-06 12:41:11 -0500161 if settings.GetDebug(DeviceID) {
162 reqJs, _ := json.Marshal(statsReq)
163 resJs, _ := json.Marshal(response)
164 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
165 }
166 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -0500167 case ofp.OFPSTMeterConfig:
Don Newton98fd8812019-09-23 15:15:02 -0400168 statsReq := request.(*ofp.MeterConfigStatsRequest)
169 response, err := handleMeterConfigStatsRequest(statsReq, id)
170 if err != nil {
171 return err
172 }
Don Newton7577f072020-01-06 12:41:11 -0500173 if settings.GetDebug(DeviceID) {
174 reqJs, _ := json.Marshal(statsReq)
175 resJs, _ := json.Marshal(response)
176 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
177 }
178 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -0500179 case ofp.OFPSTMeterFeatures:
Don Newton98fd8812019-09-23 15:15:02 -0400180 statsReq := request.(*ofp.MeterFeaturesStatsRequest)
181 response, err := handleMeterFeatureStatsRequest(statsReq)
182 if err != nil {
183 return err
184 }
Don Newton7577f072020-01-06 12:41:11 -0500185 if settings.GetDebug(DeviceID) {
186 reqJs, _ := json.Marshal(statsReq)
187 resJs, _ := json.Marshal(response)
188 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
189 }
190 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -0500191 case ofp.OFPSTTableFeatures:
Don Newton98fd8812019-09-23 15:15:02 -0400192 statsReq := request.(*ofp.TableFeaturesStatsRequest)
193 response, err := handleTableFeaturesStatsRequest(statsReq, id)
194 if err != nil {
195 return err
196 }
Don Newton7577f072020-01-06 12:41:11 -0500197 if settings.GetDebug(DeviceID) {
198 reqJs, _ := json.Marshal(statsReq)
199 resJs, _ := json.Marshal(response)
200 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
201 }
202 return client.SendMessage(response)
Don Newtone0d34a82019-11-14 10:58:06 -0500203 case ofp.OFPSTPortDesc:
Don Newton98fd8812019-09-23 15:15:02 -0400204 statsReq := request.(*ofp.PortDescStatsRequest)
Don Newton7577f072020-01-06 12:41:11 -0500205 response, err := handlePortDescStatsRequest(statsReq, DeviceID)
Don Newton98fd8812019-09-23 15:15:02 -0400206 if err != nil {
207 return err
208 }
Don Newton7577f072020-01-06 12:41:11 -0500209 if settings.GetDebug(DeviceID) {
210 reqJs, _ := json.Marshal(statsReq)
211 resJs, _ := json.Marshal(response)
212 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
213 }
214 return client.SendMessage(response)
Don Newton98fd8812019-09-23 15:15:02 -0400215
Don Newtone0d34a82019-11-14 10:58:06 -0500216 case ofp.OFPSTExperimenter:
Don Newton98fd8812019-09-23 15:15:02 -0400217 statsReq := request.(*ofp.ExperimenterStatsRequest)
218 response, err := handleExperimenterStatsRequest(statsReq, id)
219 if err != nil {
220 return err
221 }
Don Newton7577f072020-01-06 12:41:11 -0500222 if settings.GetDebug(DeviceID) {
223 reqJs, _ := json.Marshal(statsReq)
224 resJs, _ := json.Marshal(response)
225 logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
226 }
227 return client.SendMessage(response)
Don Newton98fd8812019-09-23 15:15:02 -0400228 }
229 return nil
230}
231
232func handleDescStatsRequest(request *ofp.DescStatsRequest, id common.ID) (*ofp.DescStatsReply, error) {
233 response := ofp.NewDescStatsReply()
234 response.SetXid(request.GetXid())
235 response.SetVersion(request.GetVersion())
Don Newtone0d34a82019-11-14 10:58:06 -0500236 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
237
Don Newton98fd8812019-09-23 15:15:02 -0400238 client := *getGrpcClient()
239 resp, err := client.GetLogicalDevice(context.Background(), &id)
240 if err != nil {
241 return nil, err
242 }
243 desc := resp.GetDesc()
244
245 response.SetMfrDesc(PadString(desc.GetMfrDesc(), 256))
246 response.SetHwDesc(PadString(desc.GetHwDesc(), 256))
247 response.SetSwDesc(PadString(desc.GetSwDesc(), 256))
248 response.SetSerialNum(PadString(desc.GetSerialNum(), 32))
249 response.SetDpDesc(PadString(desc.GetDpDesc(), 256))
Don Newton98fd8812019-09-23 15:15:02 -0400250 return response, nil
251}
Don Newton7577f072020-01-06 12:41:11 -0500252func handleFlowStatsRequest(request *ofp.FlowStatsRequest, id common.ID, DeviceID string) (*ofp.FlowStatsReply, error) {
Don Newton98fd8812019-09-23 15:15:02 -0400253 response := ofp.NewFlowStatsReply()
254 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500255 response.SetVersion(4)
256 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400257 client := *getGrpcClient()
258 resp, err := client.ListLogicalDeviceFlows(context.Background(), &id)
259 if err != nil {
Don Newton98fd8812019-09-23 15:15:02 -0400260 return nil, err
261 }
Don Newton98fd8812019-09-23 15:15:02 -0400262 var flow []*ofp.FlowStatsEntry
263 items := resp.GetItems()
Don Newton98fd8812019-09-23 15:15:02 -0400264 for i := 0; i < len(items); i++ {
Don Newtone0d34a82019-11-14 10:58:06 -0500265 item := items[i]
266 entry := ofp.NewFlowStatsEntry()
Don Newtone0d34a82019-11-14 10:58:06 -0500267 entry.SetTableId(uint8(item.GetTableId()))
Don Newton98fd8812019-09-23 15:15:02 -0400268 entry.SetDurationSec(item.GetDurationSec())
269 entry.SetDurationNsec(item.GetDurationNsec())
270 entry.SetPriority(uint16(item.GetPriority()))
271 entry.SetIdleTimeout(uint16(item.GetIdleTimeout()))
272 entry.SetHardTimeout(uint16(item.GetHardTimeout()))
273 entry.SetFlags(ofp.FlowModFlags(item.GetFlags()))
274 entry.SetCookie(item.GetCookie())
275 entry.SetPacketCount(item.GetPacketCount())
276 entry.SetByteCount(item.GetByteCount())
Don Newtone0d34a82019-11-14 10:58:06 -0500277 var entrySize uint16
278 entrySize = 48
279 match := ofp.NewMatchV3()
Don Newton98fd8812019-09-23 15:15:02 -0400280 pbMatch := item.GetMatch()
Don Newton98fd8812019-09-23 15:15:02 -0400281 var fields []goloxi.IOxm
282 match.SetType(uint16(pbMatch.GetType()))
283 oxFields := pbMatch.GetOxmFields()
Don Newtone0d34a82019-11-14 10:58:06 -0500284 var size uint16
285 size = 4
Don Newton98fd8812019-09-23 15:15:02 -0400286 for i := 0; i < len(oxFields); i++ {
Don Newton98fd8812019-09-23 15:15:02 -0400287 oxmField := oxFields[i]
288 field := oxmField.GetField()
289 ofbField := field.(*openflow_13.OfpOxmField_OfbField).OfbField
Don Newton7577f072020-01-06 12:41:11 -0500290 iOxm, oxmSize := parseOxm(ofbField, DeviceID)
Don Newtone0d34a82019-11-14 10:58:06 -0500291 fields = append(fields, iOxm)
292 if oxmSize > 0 {
293 size += 4 //header for oxm
Don Newton98fd8812019-09-23 15:15:02 -0400294 }
Don Newtone0d34a82019-11-14 10:58:06 -0500295 size += oxmSize
Don Newton98fd8812019-09-23 15:15:02 -0400296 }
Don Newtone0d34a82019-11-14 10:58:06 -0500297
Don Newton98fd8812019-09-23 15:15:02 -0400298 match.OxmList = fields
Don Newtone0d34a82019-11-14 10:58:06 -0500299 match.Length = uint16(size)
300 //account for 8 byte alignment
Don Newtone0d34a82019-11-14 10:58:06 -0500301 if size%8 != 0 {
302 size = ((size / 8) + 1) * 8
303 }
Don Newtone0d34a82019-11-14 10:58:06 -0500304 entrySize += size
305 entry.SetMatch(*match)
Don Newton98fd8812019-09-23 15:15:02 -0400306 var instructions []ofp.IInstruction
307 ofpInstructions := item.Instructions
308 for i := 0; i < len(ofpInstructions); i++ {
Don Newton7577f072020-01-06 12:41:11 -0500309 instruction, size := parseInstructions(ofpInstructions[i], DeviceID)
Don Newtone0d34a82019-11-14 10:58:06 -0500310 instructions = append(instructions, instruction)
311 entrySize += size
Don Newton98fd8812019-09-23 15:15:02 -0400312 }
313 entry.Instructions = instructions
Don Newtone0d34a82019-11-14 10:58:06 -0500314 entry.Length = entrySize
315 entrySize = 0
316 flow = append(flow, entry)
Don Newton98fd8812019-09-23 15:15:02 -0400317 }
318 response.SetEntries(flow)
319 return response, nil
320}
321func handleAggregateStatsRequest(request *ofp.AggregateStatsRequest, id common.ID) (*ofp.AggregateStatsReply, error) {
322 response := ofp.NewAggregateStatsReply()
323 response.SetVersion(request.GetVersion())
324 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500325 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400326 response.SetFlowCount(0)
327 //TODO wire this to voltha core when it implements
328 return response, nil
329}
330func handleGroupStatsRequest(request *ofp.GroupStatsRequest, id common.ID) (*ofp.GroupStatsReply, error) {
331 response := ofp.NewGroupStatsReply()
332 response.SetVersion(request.GetVersion())
333 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500334 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400335 client := *getGrpcClient()
336 reply, err := client.ListLogicalDeviceFlowGroups(context.Background(), &id)
337 if err != nil {
338 return nil, err
339 }
340
341 var groupStatsEntries []*ofp.GroupStatsEntry
342 items := reply.GetItems()
343 for i := 0; i < len(items); i++ {
344 item := items[i].GetStats()
345 var entry ofp.GroupStatsEntry
346 entry.SetByteCount(item.GetByteCount())
347 entry.SetPacketCount(item.GetPacketCount())
348 entry.SetDurationNsec(item.GetDurationNsec())
349 entry.SetDurationSec(item.GetDurationSec())
350 entry.SetRefCount(item.GetRefCount())
351 entry.SetGroupId(item.GetGroupId())
352 bucketStats := item.GetBucketStats()
353 var bucketStatsList []*ofp.BucketCounter
354 for j := 0; j < len(bucketStats); j++ {
355 bucketStat := bucketStats[i]
356 var bucketCounter ofp.BucketCounter
357 bucketCounter.SetPacketCount(bucketStat.GetPacketCount())
358 bucketCounter.SetByteCount(bucketStat.GetByteCount())
359 bucketStatsList = append(bucketStatsList, &bucketCounter)
360 }
361 entry.SetBucketStats(bucketStatsList)
362 groupStatsEntries = append(groupStatsEntries, &entry)
363 }
364 response.SetEntries(groupStatsEntries)
365 return response, nil
366}
367func handleGroupStatsDescRequest(request *ofp.GroupDescStatsRequest, id common.ID) (*ofp.GroupDescStatsReply, error) {
368 response := ofp.NewGroupDescStatsReply()
369 response.SetVersion(request.GetVersion())
370 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500371 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400372 client := *getGrpcClient()
373 reply, err := client.ListLogicalDeviceFlowGroups(context.Background(), &id)
374 if err != nil {
375 return nil, err
376 }
377 entries := reply.GetItems()
378 var groupDescStatsEntries []*ofp.GroupDescStatsEntry
379 for i := 0; i < len(entries); i++ {
380 item := entries[i].GetStats()
381 var groupDesc ofp.GroupDescStatsEntry
382 groupDesc.SetGroupId(item.GetGroupId())
383 /*
384 buckets := item.g
385 var bucketList []*ofp.Bucket
386 for j:=0;j<len(buckets);j++{
387
388 }
389
390 groupDesc.SetBuckets(bucketList)
391 */
392 groupDescStatsEntries = append(groupDescStatsEntries, &groupDesc)
393 }
394 response.SetEntries(groupDescStatsEntries)
395 return response, nil
396}
397func handleGroupFeatureStatsRequest(request *ofp.GroupFeaturesStatsRequest, id common.ID) (*ofp.GroupFeaturesStatsReply, error) {
398 response := ofp.NewGroupFeaturesStatsReply()
399 response.SetVersion(request.GetVersion())
400 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500401 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400402 //TODO wire this to voltha core when it implements
403 return response, nil
404}
405func handleMeterStatsRequest(request *ofp.MeterStatsRequest, id common.ID) (*ofp.MeterStatsReply, error) {
406 response := ofp.NewMeterStatsReply()
407 response.SetVersion(request.GetVersion())
408 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500409 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
410 client := *getGrpcClient()
411 resp, err := client.ListLogicalDeviceMeters(context.Background(), &id)
412 if err != nil {
413 return nil, err
414 }
415 size := uint16(40)
416 items := resp.Items
417 var meterStats []*ofp.MeterStats
418 for i := 0; i < len(items); i++ {
419 meterStat := ofp.NewMeterStats()
420 item := items[i]
421 stats := item.Stats
422 meterStat.DurationNsec = stats.DurationNsec
423 meterStat.DurationSec = stats.DurationSec
424 meterStat.ByteInCount = stats.ByteInCount
425 meterStat.FlowCount = stats.FlowCount
426 meterStat.MeterId = stats.MeterId
427 var bandStats []*ofp.MeterBandStats
428 bStats := stats.BandStats
429 for j := 0; j < len(bStats); j++ {
430 bStat := bStats[j]
431 bandStat := ofp.NewMeterBandStats()
432 bandStat.ByteBandCount = bStat.ByteBandCount
433 bandStat.PacketBandCount = bStat.PacketBandCount
434 bandStats = append(bandStats, bandStat)
435 size += 16
436 }
437 meterStat.SetBandStats(bandStats)
438 meterStat.Len = size
439 meterStats = append(meterStats, meterStat)
440 }
441 response.SetEntries(meterStats)
Don Newton98fd8812019-09-23 15:15:02 -0400442 return response, nil
443}
Don Newton98fd8812019-09-23 15:15:02 -0400444func handleMeterConfigStatsRequest(request *ofp.MeterConfigStatsRequest, id common.ID) (*ofp.MeterConfigStatsReply, error) {
445 response := ofp.NewMeterConfigStatsReply()
446 response.SetVersion(request.GetVersion())
447 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500448 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400449 //TODO wire this to voltha core when it implements
450 return response, nil
451}
Don Newton98fd8812019-09-23 15:15:02 -0400452func handleTableFeaturesStatsRequest(request *ofp.TableFeaturesStatsRequest, id common.ID) (*ofp.TableFeaturesStatsReply, error) {
453 response := ofp.NewTableFeaturesStatsReply()
454 response.SetVersion(request.GetVersion())
455 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500456 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400457 //TODO wire this to voltha core when it implements
458 return response, nil
459}
460func handleTableStatsRequest(request *ofp.TableStatsRequest, id common.ID) (*ofp.TableStatsReply, error) {
Don Newtone0d34a82019-11-14 10:58:06 -0500461 var response = ofp.NewTableStatsReply()
462 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
463 response.SetVersion(request.GetVersion())
464 response.SetXid(request.GetXid())
465 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
466 return response, nil
Don Newton98fd8812019-09-23 15:15:02 -0400467}
468func handleQueueStatsRequest(request *ofp.QueueStatsRequest, id common.ID) (*ofp.QueueStatsReply, error) {
469 response := ofp.NewQueueStatsReply()
470 response.SetVersion(request.GetVersion())
471 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500472 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400473 //TODO wire this to voltha core when it implements
474 return response, nil
475}
476func handlePortStatsRequest(request *ofp.PortStatsRequest, id common.ID) (*ofp.PortStatsReply, error) {
Don Newton98fd8812019-09-23 15:15:02 -0400477 response := ofp.NewPortStatsReply()
478 response.SetXid(request.GetXid())
479 response.SetVersion(request.GetVersion())
Don Newtone0d34a82019-11-14 10:58:06 -0500480 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400481 client := *getGrpcClient()
482 reply, err := client.ListLogicalDevicePorts(context.Background(), &id)
483 //reply,err := client.GetLogicalDevicePort(context.Background(),&id)
484 if err != nil {
Don Newton98fd8812019-09-23 15:15:02 -0400485 return nil, err
486 }
Don Newton98fd8812019-09-23 15:15:02 -0400487 ports := reply.GetItems()
488 var entries []*ofp.PortStatsEntry
489 if request.GetPortNo() == 0xffffffff { //all ports
490 for i := 0; i < len(ports); i++ {
491 port := ports[i]
492 entry := parsePortStats(port)
493 entries = append(entries, &entry)
494 }
495 } else { //find right port that is requested
496 for i := 0; i < len(ports); i++ {
497 if ports[i].GetOfpPortStats().GetPortNo() == uint32(request.GetPortNo()) {
498 entry := parsePortStats(ports[i])
499 entries = append(entries, &entry)
500 }
501 }
502 }
503 response.SetEntries(entries)
Don Newton98fd8812019-09-23 15:15:02 -0400504 return response, nil
Don Newton98fd8812019-09-23 15:15:02 -0400505}
Don Newtone0d34a82019-11-14 10:58:06 -0500506
Don Newton7577f072020-01-06 12:41:11 -0500507func handlePortDescStatsRequest(request *ofp.PortDescStatsRequest, DeviceID string) (*ofp.PortDescStatsReply, error) {
Don Newton98fd8812019-09-23 15:15:02 -0400508 response := ofp.NewPortDescStatsReply()
509 response.SetVersion(request.GetVersion())
510 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500511 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400512 var grpcClient = *getGrpcClient()
Don Newton7577f072020-01-06 12:41:11 -0500513 var id = common.ID{Id: DeviceID}
Don Newton98fd8812019-09-23 15:15:02 -0400514 logicalDevice, err := grpcClient.GetLogicalDevice(context.Background(), &id)
515 if err != nil {
516 return nil, err
517 }
518 ports := logicalDevice.GetPorts()
519 var entries []*ofp.PortDesc
520 for i := 0; i < len(ports); i++ {
521 var entry ofp.PortDesc
522 port := ports[i].GetOfpPort()
523 entry.SetPortNo(ofp.Port(port.GetPortNo()))
524
525 intArray := port.GetHwAddr()
526 var octets []byte
527 for i := 0; i < len(intArray); i++ {
528 octets = append(octets, byte(intArray[i]))
529 }
530 hwAddr := net.HardwareAddr(octets)
531 entry.SetHwAddr(hwAddr)
532 entry.SetName(PadString(port.GetName(), 16))
533 entry.SetConfig(ofp.PortConfig(port.GetConfig()))
534 entry.SetState(ofp.PortState(port.GetState()))
535 entry.SetCurr(ofp.PortFeatures(port.GetCurr()))
536 entry.SetAdvertised(ofp.PortFeatures(port.GetAdvertised()))
537 entry.SetSupported(ofp.PortFeatures(port.GetSupported()))
538 entry.SetPeer(ofp.PortFeatures(port.GetPeer()))
539 entry.SetCurrSpeed(port.GetCurrSpeed())
540 entry.SetMaxSpeed(port.GetMaxSpeed())
541
542 entries = append(entries, &entry)
543 }
544
545 response.SetEntries(entries)
546 //TODO call voltha and get port descriptions etc
547 return response, nil
548
549}
550func handleMeterFeatureStatsRequest(request *ofp.MeterFeaturesStatsRequest) (*ofp.MeterFeaturesStatsReply, error) {
551 response := ofp.NewMeterFeaturesStatsReply()
552 response.SetXid(request.GetXid())
553 response.SetVersion(request.GetVersion())
Don Newtone0d34a82019-11-14 10:58:06 -0500554 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
555 meterFeatures := ofp.NewMeterFeatures()
556 meterFeatures.Capabilities = ofp.OFPMFKbps
557 meterFeatures.BandTypes = ofp.OFPMBTDrop
558 meterFeatures.MaxMeter = 0xffffffff
559 meterFeatures.MaxBands = 0xff
560 meterFeatures.MaxColor = 0xff
561 response.Features = *meterFeatures
Don Newton98fd8812019-09-23 15:15:02 -0400562 return response, nil
563}
564func handleExperimenterStatsRequest(request *ofp.ExperimenterStatsRequest, id common.ID) (*ofp.ExperimenterStatsReply, error) {
565 response := ofp.NewExperimenterStatsReply(request.GetExperimenter())
566 response.SetVersion(request.GetVersion())
567 response.SetXid(request.GetXid())
Don Newtone0d34a82019-11-14 10:58:06 -0500568 response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
Don Newton98fd8812019-09-23 15:15:02 -0400569 //TODO wire this to voltha core when it implements
570 return response, nil
571}