Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package flow |
| 18 | |
| 19 | import ( |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 20 | "math/rand" |
| 21 | "time" |
| 22 | |
| 23 | "github.com/google/gopacket" |
Zack Williams | 2abf393 | 2019-08-05 14:07:05 -0700 | [diff] [blame] | 24 | "github.com/opencord/voltha-bbsim/common/logger" |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 25 | "github.com/opencord/voltha-bbsim/device" |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 26 | openolt "github.com/opencord/voltha-protos/go/openolt" |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 27 | log "github.com/sirupsen/logrus" |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 28 | ) |
| 29 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 30 | var flowManager Manager |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 31 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 32 | // Manager interface for common methods of controller |
| 33 | type Manager interface { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 34 | AddFlow(flow *openolt.Flow) error |
| 35 | DeleteFlow(flow *openolt.Flow) error |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 36 | DeleteAllFlows() error |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 37 | PortUp(portID uint32) error |
| 38 | PortDown(portID uint32) error |
| 39 | GetFlow(onuID uint32) ([]*openolt.Flow, error) |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 40 | InitializePacketInStream(s openolt.Openolt_EnableIndicationServer) |
| 41 | PacketOut(packet gopacket.Packet, s string, u uint32) error |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // DefaultFlowController empty struct |
| 45 | type DefaultFlowController struct { |
| 46 | } |
| 47 | |
| 48 | // InitializeFlowManager starts godc controller |
| 49 | func InitializeFlowManager(OltID uint32) { |
| 50 | // Initialize flow controller as per custom implementation |
| 51 | logger.Debug("InitializeFlowManager for OLT %d", OltID) |
| 52 | flowManager = InitializeDefaultFlowController() |
| 53 | return |
| 54 | } |
| 55 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 56 | // InitializePacketInStream initializes the stream to send packets towards VOLTHA |
| 57 | func InitializePacketInStream(s openolt.Openolt_EnableIndicationServer) { |
| 58 | flowManager.InitializePacketInStream(s) |
| 59 | } |
| 60 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 61 | // AddFlow abstracts actual implementation of flow addition |
| 62 | func AddFlow(flow *openolt.Flow) error { |
| 63 | return flowManager.AddFlow(flow) |
| 64 | } |
| 65 | |
| 66 | // DeleteFlow abstracts actual implementation of flow deletion |
| 67 | func DeleteFlow(flow *openolt.Flow) error { |
| 68 | return flowManager.DeleteFlow(flow) |
| 69 | } |
| 70 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 71 | // DeleteAllFlows abstracts actual implementation of flow deletion |
| 72 | func DeleteAllFlows() error { |
| 73 | return flowManager.DeleteAllFlows() |
| 74 | } |
| 75 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 76 | // PortUp abstracts actual implementation of port up |
| 77 | func PortUp(portID uint32) error { |
| 78 | return flowManager.PortUp(portID) |
| 79 | } |
| 80 | |
| 81 | // PortDown abstracts actual implementation of port down |
| 82 | func PortDown(portID uint32) error { |
| 83 | return flowManager.PortDown(portID) |
| 84 | } |
| 85 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 86 | // PacketOut abstracts actual implementation of sending packet out |
| 87 | func PacketOut(packet gopacket.Packet, intfType string, intfID uint32) error { |
| 88 | return flowManager.PacketOut(packet, intfType, intfID) |
| 89 | } |
| 90 | |
| 91 | // GetPortStats return stats for specified interface |
| 92 | func GetPortStats(portStats *device.PortStats) *openolt.PortStatistics { |
| 93 | |
| 94 | // increment current packet count by random number |
| 95 | pkts := portStats.Packets + uint64((rand.Intn(50)+1)*10) |
| 96 | portStats.Packets = pkts |
| 97 | logger.Info("Packet count %d", portStats.Packets) |
| 98 | |
| 99 | // fill all other stats based on packet count |
| 100 | nextPortStats := &openolt.PortStatistics{ |
| 101 | RxBytes: pkts * 64, |
| 102 | RxPackets: pkts, |
| 103 | RxUcastPackets: pkts * 40 / 100, |
| 104 | RxMcastPackets: pkts * 30 / 100, |
| 105 | RxBcastPackets: pkts * 30 / 100, |
| 106 | RxErrorPackets: 0, |
| 107 | TxBytes: pkts * 64, |
| 108 | TxPackets: pkts, |
| 109 | TxUcastPackets: pkts * 40 / 100, |
| 110 | TxMcastPackets: pkts * 30 / 100, |
| 111 | TxBcastPackets: pkts * 30 / 100, |
| 112 | TxErrorPackets: 0, |
| 113 | RxCrcErrors: 0, |
| 114 | BipErrors: 0, |
| 115 | Timestamp: uint32(time.Now().Unix()), |
| 116 | } |
| 117 | |
| 118 | return nextPortStats |
| 119 | } |
| 120 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 121 | // InitializeDefaultFlowController method to initialize default controller |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 122 | func InitializeDefaultFlowController() Manager { |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 123 | logger.Debug("Default controller initialized") |
| 124 | return new(DefaultFlowController) |
| 125 | } |
| 126 | |
| 127 | // AddFlow method implemented for DefaultFlowController |
| 128 | func (fc *DefaultFlowController) AddFlow(flow *openolt.Flow) error { |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 129 | logger.WithFields(log.Fields{ |
| 130 | "flow_eth_type": flow.Classifier.EthType, |
Matt Jeanneret | 7c9c5f2 | 2019-08-09 14:40:12 -0400 | [diff] [blame] | 131 | "ovid": flow.Classifier.OVid, |
| 132 | "ivid": flow.Classifier.IVid, |
| 133 | "onu_id": flow.OnuId, |
| 134 | "flow_id": flow.FlowId, |
| 135 | "flow_type": flow.FlowType, |
Matteo Scandolo | 67add0c | 2019-08-01 16:41:14 -0700 | [diff] [blame] | 136 | }).Debugf("AddFlow invoked for onu %d", flow.OnuId) |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 137 | return nil |
| 138 | } |
| 139 | |
| 140 | // DeleteFlow implemented for DefaultFlowController |
| 141 | func (fc *DefaultFlowController) DeleteFlow(flow *openolt.Flow) error { |
| 142 | logger.Debug("DeleteFlow invoked %v", flow) |
| 143 | return nil |
| 144 | } |
| 145 | |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 146 | // DeleteAllFlows implemented for DefaultFlowController |
| 147 | func (fc *DefaultFlowController) DeleteAllFlows() error { |
| 148 | logger.Debug("DeleteAllFlows invoked") |
| 149 | return nil |
| 150 | } |
| 151 | |
Zdravko Bozakov | 7401ff2 | 2019-05-28 22:45:12 +0200 | [diff] [blame] | 152 | // GetFlow implemented for DefaultFlowController |
| 153 | func (fc *DefaultFlowController) GetFlow(onuID uint32) ([]*openolt.Flow, error) { |
| 154 | return nil, nil |
| 155 | } |
| 156 | |
| 157 | // PortUp implemented for DefaultFlowController |
| 158 | func (fc *DefaultFlowController) PortUp(portID uint32) error { |
| 159 | logger.Debug("PortUp invoked %d", portID) |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | // PortDown implemented for DefaultFlowController |
| 164 | func (fc *DefaultFlowController) PortDown(portID uint32) error { |
| 165 | logger.Debug("PortDown invoked %d", portID) |
| 166 | return nil |
| 167 | } |
Zdravko Bozakov | 078a271 | 2019-07-19 23:25:15 +0200 | [diff] [blame] | 168 | |
| 169 | // InitializePacketInStream implemented for DefaultFlowController |
| 170 | func (fc *DefaultFlowController) InitializePacketInStream(s openolt.Openolt_EnableIndicationServer) { |
| 171 | logger.Debug("Initialize Openolt stream") |
| 172 | } |
| 173 | |
| 174 | // PacketOut implemented for DefaultFlowController |
| 175 | func (fc *DefaultFlowController) PacketOut(pkt gopacket.Packet, intfType string, intfID uint32) error { |
| 176 | logger.Debug("PacketOut invoked intfType: %s, intfID: %d", intfType, intfID) |
| 177 | return nil |
| 178 | } |