blob: 7e47db9a755cdad76b9c333585ae230d133dbbda [file] [log] [blame]
Zdravko Bozakov7401ff22019-05-28 22:45:12 +02001/*
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
17package flow
18
19import (
Zdravko Bozakov078a2712019-07-19 23:25:15 +020020 "math/rand"
21 "time"
22
23 "github.com/google/gopacket"
Zack Williams2abf3932019-08-05 14:07:05 -070024 "github.com/opencord/voltha-bbsim/common/logger"
Zdravko Bozakov078a2712019-07-19 23:25:15 +020025 "github.com/opencord/voltha-bbsim/device"
Matt Jeanneret7c9c5f22019-08-09 14:40:12 -040026 openolt "github.com/opencord/voltha-protos/go/openolt"
Matteo Scandolo67add0c2019-08-01 16:41:14 -070027 log "github.com/sirupsen/logrus"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020028)
29
Zdravko Bozakov078a2712019-07-19 23:25:15 +020030var flowManager Manager
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020031
Zdravko Bozakov078a2712019-07-19 23:25:15 +020032// Manager interface for common methods of controller
33type Manager interface {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020034 AddFlow(flow *openolt.Flow) error
35 DeleteFlow(flow *openolt.Flow) error
Zdravko Bozakov078a2712019-07-19 23:25:15 +020036 DeleteAllFlows() error
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020037 PortUp(portID uint32) error
38 PortDown(portID uint32) error
39 GetFlow(onuID uint32) ([]*openolt.Flow, error)
Zdravko Bozakov078a2712019-07-19 23:25:15 +020040 InitializePacketInStream(s openolt.Openolt_EnableIndicationServer)
41 PacketOut(packet gopacket.Packet, s string, u uint32) error
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020042}
43
44// DefaultFlowController empty struct
45type DefaultFlowController struct {
46}
47
48// InitializeFlowManager starts godc controller
49func 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 Bozakov078a2712019-07-19 23:25:15 +020056// InitializePacketInStream initializes the stream to send packets towards VOLTHA
57func InitializePacketInStream(s openolt.Openolt_EnableIndicationServer) {
58 flowManager.InitializePacketInStream(s)
59}
60
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020061// AddFlow abstracts actual implementation of flow addition
62func AddFlow(flow *openolt.Flow) error {
63 return flowManager.AddFlow(flow)
64}
65
66// DeleteFlow abstracts actual implementation of flow deletion
67func DeleteFlow(flow *openolt.Flow) error {
68 return flowManager.DeleteFlow(flow)
69}
70
Zdravko Bozakov078a2712019-07-19 23:25:15 +020071// DeleteAllFlows abstracts actual implementation of flow deletion
72func DeleteAllFlows() error {
73 return flowManager.DeleteAllFlows()
74}
75
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020076// PortUp abstracts actual implementation of port up
77func PortUp(portID uint32) error {
78 return flowManager.PortUp(portID)
79}
80
81// PortDown abstracts actual implementation of port down
82func PortDown(portID uint32) error {
83 return flowManager.PortDown(portID)
84}
85
Zdravko Bozakov078a2712019-07-19 23:25:15 +020086// PacketOut abstracts actual implementation of sending packet out
87func 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
92func 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 Bozakov7401ff22019-05-28 22:45:12 +0200121// InitializeDefaultFlowController method to initialize default controller
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200122func InitializeDefaultFlowController() Manager {
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200123 logger.Debug("Default controller initialized")
124 return new(DefaultFlowController)
125}
126
127// AddFlow method implemented for DefaultFlowController
128func (fc *DefaultFlowController) AddFlow(flow *openolt.Flow) error {
Matteo Scandolo67add0c2019-08-01 16:41:14 -0700129 logger.WithFields(log.Fields{
130 "flow_eth_type": flow.Classifier.EthType,
Matt Jeanneret7c9c5f22019-08-09 14:40:12 -0400131 "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 Scandolo67add0c2019-08-01 16:41:14 -0700136 }).Debugf("AddFlow invoked for onu %d", flow.OnuId)
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200137 return nil
138}
139
140// DeleteFlow implemented for DefaultFlowController
141func (fc *DefaultFlowController) DeleteFlow(flow *openolt.Flow) error {
142 logger.Debug("DeleteFlow invoked %v", flow)
143 return nil
144}
145
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200146// DeleteAllFlows implemented for DefaultFlowController
147func (fc *DefaultFlowController) DeleteAllFlows() error {
148 logger.Debug("DeleteAllFlows invoked")
149 return nil
150}
151
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200152// GetFlow implemented for DefaultFlowController
153func (fc *DefaultFlowController) GetFlow(onuID uint32) ([]*openolt.Flow, error) {
154 return nil, nil
155}
156
157// PortUp implemented for DefaultFlowController
158func (fc *DefaultFlowController) PortUp(portID uint32) error {
159 logger.Debug("PortUp invoked %d", portID)
160 return nil
161}
162
163// PortDown implemented for DefaultFlowController
164func (fc *DefaultFlowController) PortDown(portID uint32) error {
165 logger.Debug("PortDown invoked %d", portID)
166 return nil
167}
Zdravko Bozakov078a2712019-07-19 23:25:15 +0200168
169// InitializePacketInStream implemented for DefaultFlowController
170func (fc *DefaultFlowController) InitializePacketInStream(s openolt.Openolt_EnableIndicationServer) {
171 logger.Debug("Initialize Openolt stream")
172}
173
174// PacketOut implemented for DefaultFlowController
175func (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}