blob: 917313e9db6e56d8f50590c934e27f1e65eb507f [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001/*
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 */
npujar1d86a522019-11-14 17:11:16 +053016
Kent Hagerman2b216042020-04-03 18:28:56 -040017package device
khenaidoob9203542018-09-17 22:56:37 -040018
19import (
20 "context"
Matteo Scandolo360605d2019-11-05 18:29:17 -080021 "encoding/hex"
David Bainbridged1afd662020-03-26 18:27:41 -070022 "sync"
23 "time"
24
khenaidoob9203542018-09-17 22:56:37 -040025 "github.com/gogo/protobuf/proto"
sbarbari17d7e222019-11-05 10:02:29 -050026 "github.com/opencord/voltha-go/db/model"
Kent Hagerman433a31a2020-05-20 19:04:48 -040027 "github.com/opencord/voltha-go/rw_core/core/device/flow"
28 "github.com/opencord/voltha-go/rw_core/core/device/group"
Kent Hagermanfa9d6d42020-05-25 11:49:40 -040029 "github.com/opencord/voltha-go/rw_core/core/device/logical_port"
Kent Hagerman433a31a2020-05-20 19:04:48 -040030 "github.com/opencord/voltha-go/rw_core/core/device/meter"
npujar1d86a522019-11-14 17:11:16 +053031 fd "github.com/opencord/voltha-go/rw_core/flowdecomposition"
khenaidoo442e7c72020-03-10 16:13:48 -040032 "github.com/opencord/voltha-go/rw_core/route"
Scott Bakerb671a862019-10-24 10:53:40 -070033 coreutils "github.com/opencord/voltha-go/rw_core/utils"
Maninderdfadc982020-10-28 14:04:33 +053034 fu "github.com/opencord/voltha-lib-go/v4/pkg/flows"
35 "github.com/opencord/voltha-lib-go/v4/pkg/log"
36 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
37 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
38 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040039 "google.golang.org/grpc/codes"
40 "google.golang.org/grpc/status"
khenaidoo442e7c72020-03-10 16:13:48 -040041)
42
Kent Hagerman2b216042020-04-03 18:28:56 -040043// LogicalAgent represent attributes of logical device agent
44type LogicalAgent struct {
Kent Hagermanfa9d6d42020-05-25 11:49:40 -040045 logicalDeviceID string
46 serialNumber string
47 rootDeviceID string
48 deviceMgr *Manager
49 ldeviceMgr *LogicalManager
50 ldProxy *model.Proxy
51 stopped bool
52 deviceRoutes *route.DeviceRoutes
53 flowDecomposer *fd.FlowDecomposer
54 defaultTimeout time.Duration
55 logicalDevice *voltha.LogicalDevice
56 requestQueue *coreutils.RequestQueue
57 orderedEvents orderedEvents
58 startOnce sync.Once
59 stopOnce sync.Once
Mahir Gunyeladdb66a2020-04-29 18:08:50 -070060
Kent Hagerman433a31a2020-05-20 19:04:48 -040061 flowLoader *flow.Loader
62 meterLoader *meter.Loader
63 groupLoader *group.Loader
Kent Hagermanfa9d6d42020-05-25 11:49:40 -040064 portLoader *port.Loader
Mahir Gunyeladdb66a2020-04-29 18:08:50 -070065}
66
Rohan Agrawal31f21802020-06-12 05:38:46 +000067func newLogicalAgent(ctx context.Context, id string, sn string, deviceID string, ldeviceMgr *LogicalManager,
Kent Hagermanf5a67352020-04-30 15:15:26 -040068 deviceMgr *Manager, dbProxy *model.Path, ldProxy *model.Proxy, defaultTimeout time.Duration) *LogicalAgent {
Kent Hagerman2a07b862020-06-19 15:23:07 -040069 return &LogicalAgent{
Kent Hagermanf5a67352020-04-30 15:15:26 -040070 logicalDeviceID: id,
71 serialNumber: sn,
72 rootDeviceID: deviceID,
73 deviceMgr: deviceMgr,
74 ldProxy: ldProxy,
75 ldeviceMgr: ldeviceMgr,
Kent Hagerman2a07b862020-06-19 15:23:07 -040076 deviceRoutes: route.NewDeviceRoutes(id, deviceID, deviceMgr.listDevicePorts),
Kent Hagerman6031aad2020-07-29 16:36:33 -040077 flowDecomposer: fd.NewFlowDecomposer(deviceMgr.getDeviceReadOnly),
Kent Hagermanf5a67352020-04-30 15:15:26 -040078 defaultTimeout: defaultTimeout,
79 requestQueue: coreutils.NewRequestQueue(),
Kent Hagerman433a31a2020-05-20 19:04:48 -040080
Kent Hagermanf5a67352020-04-30 15:15:26 -040081 flowLoader: flow.NewLoader(dbProxy.SubPath("logical_flows").Proxy(id)),
82 groupLoader: group.NewLoader(dbProxy.SubPath("logical_groups").Proxy(id)),
83 meterLoader: meter.NewLoader(dbProxy.SubPath("logical_meters").Proxy(id)),
Kent Hagermanfa9d6d42020-05-25 11:49:40 -040084 portLoader: port.NewLoader(dbProxy.SubPath("logical_ports").Proxy(id)),
Kent Hagerman433a31a2020-05-20 19:04:48 -040085 }
khenaidoob9203542018-09-17 22:56:37 -040086}
87
khenaidoo4d4802d2018-10-04 21:59:49 -040088// start creates the logical device and add it to the data model
Kent Hagerman2b216042020-04-03 18:28:56 -040089func (agent *LogicalAgent) start(ctx context.Context, loadFromDB bool) error {
khenaidoo442e7c72020-03-10 16:13:48 -040090 needToStart := false
91 if agent.startOnce.Do(func() { needToStart = true }); !needToStart {
92 return nil
93 }
94
Himani Chawlab4c25912020-11-12 17:16:38 +053095 logger.Infow(ctx, "starting-logical-device-agent", log.Fields{"logical-device-id": agent.logicalDeviceID, "load-from-db": loadFromDB})
khenaidoo442e7c72020-03-10 16:13:48 -040096
97 var startSucceeded bool
98 defer func() {
99 if !startSucceeded {
100 if err := agent.stop(ctx); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000101 logger.Errorw(ctx, "failed-to-cleanup-after-unsuccessful-start", log.Fields{"logical-device-id": agent.logicalDeviceID, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400102 }
103 }
104 }()
105
khenaidoo297cd252019-02-07 22:10:23 -0500106 var ld *voltha.LogicalDevice
khenaidoo442e7c72020-03-10 16:13:48 -0400107 if !loadFromDB {
khenaidoo7e3d8f12019-08-02 16:06:30 -0400108 //Build the logical device based on information retrieved from the device adapter
109 var switchCap *ic.SwitchCapability
khenaidoo297cd252019-02-07 22:10:23 -0500110 var err error
npujar1d86a522019-11-14 17:11:16 +0530111 if switchCap, err = agent.deviceMgr.getSwitchCapability(ctx, agent.rootDeviceID); err != nil {
khenaidoo7e3d8f12019-08-02 16:06:30 -0400112 return err
113 }
npujar1d86a522019-11-14 17:11:16 +0530114 ld = &voltha.LogicalDevice{Id: agent.logicalDeviceID, RootDeviceId: agent.rootDeviceID}
khenaidoo297cd252019-02-07 22:10:23 -0500115
116 // Create the datapath ID (uint64) using the logical device ID (based on the MAC Address)
117 var datapathID uint64
Kent Hagerman2b216042020-04-03 18:28:56 -0400118 if datapathID, err = coreutils.CreateDataPathID(agent.serialNumber); err != nil {
khenaidoo297cd252019-02-07 22:10:23 -0500119 return err
120 }
121 ld.DatapathId = datapathID
khenaidoo7e3d8f12019-08-02 16:06:30 -0400122 ld.Desc = (proto.Clone(switchCap.Desc)).(*ofp.OfpDesc)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000123 logger.Debugw(ctx, "Switch-capability", log.Fields{"Desc": ld.Desc, "fromAd": switchCap.Desc})
khenaidoo7e3d8f12019-08-02 16:06:30 -0400124 ld.SwitchFeatures = (proto.Clone(switchCap.SwitchFeatures)).(*ofp.OfpSwitchFeatures)
khenaidoo297cd252019-02-07 22:10:23 -0500125
khenaidoo297cd252019-02-07 22:10:23 -0500126 // Save the logical device
Kent Hagermanf5a67352020-04-30 15:15:26 -0400127 if err := agent.ldProxy.Set(ctx, ld.Id, ld); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000128 logger.Errorw(ctx, "failed-to-add-logical-device", log.Fields{"logical-device-id": agent.logicalDeviceID})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530129 return err
130 }
Himani Chawlab4c25912020-11-12 17:16:38 +0530131 logger.Debugw(ctx, "logical-device-created", log.Fields{"logical-device-id": agent.logicalDeviceID, "root-id": ld.RootDeviceId})
khenaidoo6e55d9e2019-12-12 18:26:26 -0500132
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400133 agent.logicalDevice = ld
khenaidoofc1314d2019-03-14 09:34:21 -0400134
khenaidoo442e7c72020-03-10 16:13:48 -0400135 // Setup the logicalports - internal processing, no need to propagate the client context
npujar1d86a522019-11-14 17:11:16 +0530136 go func() {
Himani Chawlab4c25912020-11-12 17:16:38 +0530137 subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx)
138 err := agent.setupLogicalPorts(subCtx)
npujar1d86a522019-11-14 17:11:16 +0530139 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000140 logger.Errorw(ctx, "unable-to-setup-logical-ports", log.Fields{"error": err})
npujar1d86a522019-11-14 17:11:16 +0530141 }
142 }()
khenaidoo297cd252019-02-07 22:10:23 -0500143 } else {
144 // load from dB - the logical may not exist at this time. On error, just return and the calling function
145 // will destroy this agent.
Kent Hagerman4f355f52020-03-30 16:01:33 -0400146 ld := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400147 have, err := agent.ldProxy.Get(ctx, agent.logicalDeviceID, ld)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530148 if err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400149 return err
Kent Hagerman4f355f52020-03-30 16:01:33 -0400150 } else if !have {
khenaidoo6e55d9e2019-12-12 18:26:26 -0500151 return status.Errorf(codes.NotFound, "logical_device-%s", agent.logicalDeviceID)
khenaidoo297cd252019-02-07 22:10:23 -0500152 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400153
khenaidoo8c3303d2019-02-13 14:59:39 -0500154 // Update the root device Id
npujar1d86a522019-11-14 17:11:16 +0530155 agent.rootDeviceID = ld.RootDeviceId
khenaidoo3d3b8c22019-05-22 18:10:39 -0400156
khenaidoo6e55d9e2019-12-12 18:26:26 -0500157 // Update the last data
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400158 agent.logicalDevice = ld
khenaidoo6e55d9e2019-12-12 18:26:26 -0500159
Kent Hagerman2a07b862020-06-19 15:23:07 -0400160 // now that the root device is known, create DeviceRoutes with it
161 agent.deviceRoutes = route.NewDeviceRoutes(agent.logicalDeviceID, agent.rootDeviceID, agent.deviceMgr.listDevicePorts)
162
Mahir Gunyeladdb66a2020-04-29 18:08:50 -0700163 // load the flows, meters and groups from KV to cache
Kent Hagerman433a31a2020-05-20 19:04:48 -0400164 agent.flowLoader.Load(ctx)
165 agent.meterLoader.Load(ctx)
166 agent.groupLoader.Load(ctx)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400167 agent.portLoader.Load(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400168 }
khenaidoofc1314d2019-03-14 09:34:21 -0400169
khenaidoo820197c2020-02-13 16:35:33 -0500170 // Setup the device routes. Building routes may fail if the pre-conditions are not satisfied (e.g. no PON ports present)
khenaidoo442e7c72020-03-10 16:13:48 -0400171 if loadFromDB {
khenaidoo820197c2020-02-13 16:35:33 -0500172 go func() {
Himani Chawlab4c25912020-11-12 17:16:38 +0530173 subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx)
174 if err := agent.buildRoutes(subCtx); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000175 logger.Warn(ctx, "routes-not-ready", log.Fields{"logical-device-id": agent.logicalDeviceID, "error": err})
khenaidoo820197c2020-02-13 16:35:33 -0500176 }
177 }()
khenaidoo4c9e5592019-09-09 16:20:41 -0400178 }
khenaidoo442e7c72020-03-10 16:13:48 -0400179 startSucceeded = true
180
khenaidoob9203542018-09-17 22:56:37 -0400181 return nil
182}
183
khenaidoo442e7c72020-03-10 16:13:48 -0400184// stop stops the logical device agent. This removes the logical device from the data model.
Kent Hagerman2b216042020-04-03 18:28:56 -0400185func (agent *LogicalAgent) stop(ctx context.Context) error {
khenaidoo442e7c72020-03-10 16:13:48 -0400186 var returnErr error
187 agent.stopOnce.Do(func() {
Himani Chawlab4c25912020-11-12 17:16:38 +0530188 logger.Info(ctx, "stopping-logical-device-agent")
khenaidoo8c3303d2019-02-13 14:59:39 -0500189
khenaidoo442e7c72020-03-10 16:13:48 -0400190 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
191 // This should never happen - an error is returned only if the agent is stopped and an agent is only stopped once.
192 returnErr = err
193 return
194 }
195 defer agent.requestQueue.RequestComplete()
Himani Chawla40af2702021-01-27 15:06:30 +0530196 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
197 // Before deletion of the logical agent, make sure all events for ldagent are sent to avoid race conditions
198 if err := agent.orderedEvents.waitForAllEventsToBeSent(subCtx, cancel); err != nil {
199 //Log the error here
200 logger.Errorw(ctx, "failed-to-send-all-events-on-the-logical-device-before-deletion",
201 log.Fields{"error": err, "logical-device-id": agent.logicalDeviceID})
202 }
khenaidoo442e7c72020-03-10 16:13:48 -0400203 //Remove the logical device from the model
Kent Hagermanf5a67352020-04-30 15:15:26 -0400204 if err := agent.ldProxy.Remove(ctx, agent.logicalDeviceID); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400205 returnErr = err
khenaidoo442e7c72020-03-10 16:13:48 -0400206 } else {
Himani Chawlab4c25912020-11-12 17:16:38 +0530207 logger.Debugw(ctx, "logical-device-removed", log.Fields{"logical-device-id": agent.logicalDeviceID})
khenaidoo442e7c72020-03-10 16:13:48 -0400208 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400209 // TODO: remove all entries from all loaders
210 // TODO: don't allow any more modifications to flows/groups/meters/ports or to any logical device field
khenaidoo442e7c72020-03-10 16:13:48 -0400211
Kent Hagerman4f355f52020-03-30 16:01:33 -0400212 agent.stopped = true
khenaidoo442e7c72020-03-10 16:13:48 -0400213
Himani Chawlab4c25912020-11-12 17:16:38 +0530214 logger.Info(ctx, "logical-device-agent-stopped")
khenaidoo442e7c72020-03-10 16:13:48 -0400215 })
216 return returnErr
khenaidoo4d4802d2018-10-04 21:59:49 -0400217}
218
Kent Hagermancba2f302020-07-28 13:37:36 -0400219// GetLogicalDeviceReadOnly returns the latest logical device data
220func (agent *LogicalAgent) GetLogicalDeviceReadOnly(ctx context.Context) (*voltha.LogicalDevice, error) {
khenaidoo442e7c72020-03-10 16:13:48 -0400221 if err := agent.requestQueue.WaitForGreenLight(ctx); err != nil {
222 return nil, err
223 }
224 defer agent.requestQueue.RequestComplete()
Kent Hagermancba2f302020-07-28 13:37:36 -0400225 return agent.logicalDevice, nil
khenaidoo92e62c52018-10-03 14:02:54 -0400226}
227
Rohan Agrawal31f21802020-06-12 05:38:46 +0000228func (agent *LogicalAgent) addFlowsAndGroupsToDevices(ctx context.Context, deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata) []coreutils.Response {
Himani Chawlab4c25912020-11-12 17:16:38 +0530229 logger.Debugw(ctx, "send-add-flows-to-device-manager", log.Fields{"logical-device-id": agent.logicalDeviceID, "device-rules": deviceRules, "flow-metadata": flowMetadata})
khenaidoo19d7b632018-10-30 10:49:50 -0400230
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500231 responses := make([]coreutils.Response, 0)
npujar1d86a522019-11-14 17:11:16 +0530232 for deviceID, value := range deviceRules.GetRules() {
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500233 response := coreutils.NewResponse()
234 responses = append(responses, response)
235 go func(deviceId string, value *fu.FlowsAndGroups) {
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000236 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Himani Chawlab4c25912020-11-12 17:16:38 +0530237 subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx)
238
khenaidoo442e7c72020-03-10 16:13:48 -0400239 defer cancel()
khenaidoo0db4c812020-05-27 15:27:30 -0400240 start := time.Now()
241 if err := agent.deviceMgr.addFlowsAndGroups(subCtx, deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil {
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700242 logger.Errorw(ctx, "flow-add-failed", log.Fields{
divyadesaicb8b59d2020-08-18 09:55:47 +0000243 "device-id": deviceId,
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700244 "error": err,
245 "wait-time": time.Since(start),
246 "flows": value.ListFlows(),
247 "groups": value.ListGroups(),
248 })
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500249 response.Error(status.Errorf(codes.Internal, "flow-add-failed: %s", deviceId))
khenaidoo0458db62019-06-20 08:50:36 -0400250 }
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500251 response.Done()
npujar1d86a522019-11-14 17:11:16 +0530252 }(deviceID, value)
khenaidoo19d7b632018-10-30 10:49:50 -0400253 }
khenaidoo442e7c72020-03-10 16:13:48 -0400254 // Return responses (an array of channels) for the caller to wait for a response from the far end.
255 return responses
khenaidoo0458db62019-06-20 08:50:36 -0400256}
khenaidoo19d7b632018-10-30 10:49:50 -0400257
Rohan Agrawal31f21802020-06-12 05:38:46 +0000258func (agent *LogicalAgent) deleteFlowsAndGroupsFromDevices(ctx context.Context, deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata, mod *ofp.OfpFlowMod) []coreutils.Response {
divyadesaicb8b59d2020-08-18 09:55:47 +0000259 logger.Debugw(ctx, "send-delete-flows-to-device-manager", log.Fields{"logical-device-id": agent.logicalDeviceID})
khenaidoo0458db62019-06-20 08:50:36 -0400260
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500261 responses := make([]coreutils.Response, 0)
npujar1d86a522019-11-14 17:11:16 +0530262 for deviceID, value := range deviceRules.GetRules() {
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500263 response := coreutils.NewResponse()
264 responses = append(responses, response)
265 go func(deviceId string, value *fu.FlowsAndGroups) {
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000266 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Himani Chawlab4c25912020-11-12 17:16:38 +0530267 subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx)
268
khenaidoo442e7c72020-03-10 16:13:48 -0400269 defer cancel()
khenaidoo0db4c812020-05-27 15:27:30 -0400270 start := time.Now()
271 if err := agent.deviceMgr.deleteFlowsAndGroups(subCtx, deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000272 logger.Errorw(ctx, "flows-and-groups-delete-failed", log.Fields{
Matteo Scandolo367162b2020-06-22 15:07:33 -0700273 "device-id": deviceId,
274 "error": err,
275 "flow-cookie": mod.Cookie,
276 "wait-time": time.Since(start),
277 })
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500278 response.Error(status.Errorf(codes.Internal, "flow-delete-failed: %s", deviceId))
khenaidoo0458db62019-06-20 08:50:36 -0400279 }
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500280 response.Done()
npujar1d86a522019-11-14 17:11:16 +0530281 }(deviceID, value)
khenaidoo0458db62019-06-20 08:50:36 -0400282 }
khenaidoo442e7c72020-03-10 16:13:48 -0400283 return responses
khenaidoo0458db62019-06-20 08:50:36 -0400284}
285
Rohan Agrawal31f21802020-06-12 05:38:46 +0000286func (agent *LogicalAgent) updateFlowsAndGroupsOfDevice(ctx context.Context, deviceRules *fu.DeviceRules, flowMetadata *voltha.FlowMetadata) []coreutils.Response {
divyadesaicb8b59d2020-08-18 09:55:47 +0000287 logger.Debugw(ctx, "send-update-flows-to-device-manager", log.Fields{"logical-device-id": agent.logicalDeviceID})
khenaidoo0458db62019-06-20 08:50:36 -0400288
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500289 responses := make([]coreutils.Response, 0)
npujar1d86a522019-11-14 17:11:16 +0530290 for deviceID, value := range deviceRules.GetRules() {
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500291 response := coreutils.NewResponse()
292 responses = append(responses, response)
293 go func(deviceId string, value *fu.FlowsAndGroups) {
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000294 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Himani Chawlab4c25912020-11-12 17:16:38 +0530295 subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx)
296
khenaidoo442e7c72020-03-10 16:13:48 -0400297 defer cancel()
khenaidoo0db4c812020-05-27 15:27:30 -0400298 if err := agent.deviceMgr.updateFlowsAndGroups(subCtx, deviceId, value.ListFlows(), value.ListGroups(), flowMetadata); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000299 logger.Errorw(ctx, "flow-update-failed", log.Fields{"device-id": deviceId, "error": err})
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500300 response.Error(status.Errorf(codes.Internal, "flow-update-failed: %s", deviceId))
khenaidoo0458db62019-06-20 08:50:36 -0400301 }
Kent Hagerman8da2f1e2019-11-25 17:28:09 -0500302 response.Done()
npujar1d86a522019-11-14 17:11:16 +0530303 }(deviceID, value)
khenaidoo0458db62019-06-20 08:50:36 -0400304 }
khenaidoo442e7c72020-03-10 16:13:48 -0400305 return responses
khenaidoo19d7b632018-10-30 10:49:50 -0400306}
307
Rohan Agrawal31f21802020-06-12 05:38:46 +0000308func (agent *LogicalAgent) deleteFlowsFromParentDevice(ctx context.Context, flows map[uint64]*ofp.OfpFlowStats, metadata *voltha.FlowMetadata, mod *ofp.OfpFlowMod) []coreutils.Response {
309 logger.Debugw(ctx, "deleting-flows-from-parent-device", log.Fields{"logical-device-id": agent.logicalDeviceID, "flows": flows})
khenaidoo787224a2020-04-16 18:08:47 -0400310 responses := make([]coreutils.Response, 0)
Kent Hagerman433a31a2020-05-20 19:04:48 -0400311 for _, flow := range flows {
khenaidoo787224a2020-04-16 18:08:47 -0400312 response := coreutils.NewResponse()
313 responses = append(responses, response)
314 uniPort, err := agent.getUNILogicalPortNo(flow)
315 if err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000316 logger.Error(ctx, "no-uni-port-in-flow", log.Fields{"device-id": agent.rootDeviceID, "flow": flow, "error": err})
khenaidoo787224a2020-04-16 18:08:47 -0400317 response.Error(err)
318 response.Done()
319 continue
320 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000321 logger.Debugw(ctx, "uni-port", log.Fields{"flows": flows, "uni-port": uniPort})
khenaidoo787224a2020-04-16 18:08:47 -0400322 go func(uniPort uint32, metadata *voltha.FlowMetadata) {
Rohan Agrawalcf12f202020-08-03 04:42:01 +0000323 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout)
Himani Chawlab4c25912020-11-12 17:16:38 +0530324 subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx)
325
khenaidoo787224a2020-04-16 18:08:47 -0400326 defer cancel()
khenaidoo0db4c812020-05-27 15:27:30 -0400327 if err := agent.deviceMgr.deleteParentFlows(subCtx, agent.rootDeviceID, uniPort, metadata); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000328 logger.Error(ctx, "flow-delete-failed-from-parent-device", log.Fields{
Matteo Scandolo367162b2020-06-22 15:07:33 -0700329 "device-id": agent.rootDeviceID,
330 "error": err,
331 "flow-cookie": mod.Cookie,
332 })
khenaidoo787224a2020-04-16 18:08:47 -0400333 response.Error(status.Errorf(codes.Internal, "flow-delete-failed: %s %v", agent.rootDeviceID, err))
334 }
335 response.Done()
336 }(uniPort, metadata)
337 }
338 return responses
339}
340
Kent Hagerman2b216042020-04-03 18:28:56 -0400341func (agent *LogicalAgent) packetOut(ctx context.Context, packet *ofp.OfpPacketOut) {
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700342 if logger.V(log.InfoLevel) {
343 logger.Infow(ctx, "packet-out", log.Fields{
Himani Chawlab4c25912020-11-12 17:16:38 +0530344 "packet": hex.EncodeToString(packet.Data),
345 "in-port": packet.GetInPort(),
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700346 })
347 }
khenaidoo68c930b2019-05-13 11:46:51 -0400348 outPort := fu.GetPacketOutPort(packet)
khenaidoofdbad6e2018-11-06 22:26:38 -0500349 //frame := packet.GetData()
350 //TODO: Use a channel between the logical agent and the device agent
npujar467fe752020-01-16 20:17:45 +0530351 if err := agent.deviceMgr.packetOut(ctx, agent.rootDeviceID, outPort, packet); err != nil {
Himani Chawlab4c25912020-11-12 17:16:38 +0530352 logger.Error(ctx, "packet-out-failed", log.Fields{"logical-device-id": agent.rootDeviceID})
khenaidooca301322019-01-09 23:06:32 -0500353 }
khenaidoofdbad6e2018-11-06 22:26:38 -0500354}
355
Rohan Agrawal31f21802020-06-12 05:38:46 +0000356func (agent *LogicalAgent) packetIn(ctx context.Context, port uint32, transactionID string, packet []byte) {
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700357 if logger.V(log.InfoLevel) {
Andrea Campanella9d7ddcb2021-03-03 10:34:15 +0100358 logger.Debugw(ctx, "packet-in", log.Fields{
Matteo Scandolo45e514a2020-08-05 15:27:10 -0700359 "port": port,
360 "packet": hex.EncodeToString(packet),
361 "transactionId": transactionID,
362 })
363 }
364
khenaidoo68c930b2019-05-13 11:46:51 -0400365 packetIn := fu.MkPacketIn(port, packet)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000366 agent.ldeviceMgr.SendPacketIn(ctx, agent.logicalDeviceID, transactionID, packetIn)
367 logger.Debugw(ctx, "sending-packet-in", log.Fields{"packet": hex.EncodeToString(packetIn.Data)})
khenaidoofdbad6e2018-11-06 22:26:38 -0500368}