blob: 9a38d9cf3e9cb0d1fc459e04159c7d3ec4ba232e [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"
21 "errors"
khenaidoo7585a962021-06-10 16:15:38 -040022 "github.com/opencord/voltha-lib-go/v4/pkg/probe"
Kent Hagerman45a13e42020-04-13 12:23:50 -040023 "io"
Kent Hagermanfa9d6d42020-05-25 11:49:40 -040024 "strconv"
David Bainbridged1afd662020-03-26 18:27:41 -070025 "strings"
26 "sync"
27 "time"
28
Kent Hagerman433a31a2020-05-20 19:04:48 -040029 "github.com/golang/protobuf/ptypes/empty"
sbarbari17d7e222019-11-05 10:02:29 -050030 "github.com/opencord/voltha-go/db/model"
Kent Hagerman433a31a2020-05-20 19:04:48 -040031 "github.com/opencord/voltha-go/rw_core/core/device/event"
32 "github.com/opencord/voltha-go/rw_core/utils"
Maninderdfadc982020-10-28 14:04:33 +053033 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
34 "github.com/opencord/voltha-lib-go/v4/pkg/log"
35 "github.com/opencord/voltha-protos/v4/go/openflow_13"
36 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040037 "google.golang.org/grpc/codes"
38 "google.golang.org/grpc/status"
khenaidoob9203542018-09-17 22:56:37 -040039)
40
Kent Hagerman2b216042020-04-03 18:28:56 -040041// LogicalManager represent logical device manager attributes
42type LogicalManager struct {
Kent Hagerman45a13e42020-04-13 12:23:50 -040043 *event.Manager
khenaidoo4c9e5592019-09-09 16:20:41 -040044 logicalDeviceAgents sync.Map
Kent Hagerman2b216042020-04-03 18:28:56 -040045 deviceMgr *Manager
npujar467fe752020-01-16 20:17:45 +053046 kafkaICProxy kafka.InterContainerProxy
Mahir Gunyel03de0d32020-06-03 01:36:59 -070047 dbPath *model.Path
Kent Hagermanf5a67352020-04-30 15:15:26 -040048 ldProxy *model.Proxy
khenaidoo442e7c72020-03-10 16:13:48 -040049 defaultTimeout time.Duration
khenaidoo4c9e5592019-09-09 16:20:41 -040050 logicalDevicesLoadingLock sync.RWMutex
51 logicalDeviceLoadingInProgress map[string][]chan int
khenaidoob9203542018-09-17 22:56:37 -040052}
53
khenaidoo7585a962021-06-10 16:15:38 -040054func (ldMgr *LogicalManager) Start(ctx context.Context) {
55 logger.Info(ctx, "starting-logical-device-manager")
56 probe.UpdateStatusFromContext(ctx, "logical-device-manager", probe.ServiceStatusPreparing)
57
58 // Load all the logical devices from the dB
59 var logicalDevices []*voltha.LogicalDevice
60 if err := ldMgr.ldProxy.List(ctx, &logicalDevices); err != nil {
61 logger.Fatalw(ctx, "failed-to-list-logical-devices-from-cluster-proxy", log.Fields{"error": err})
62 }
63 for _, lDevice := range logicalDevices {
64 // Create an agent for each device
65 agent := newLogicalAgent(ctx, lDevice.Id, "", "", ldMgr, ldMgr.deviceMgr, ldMgr.dbPath, ldMgr.ldProxy, ldMgr.defaultTimeout)
66 if err := agent.start(ctx, true, lDevice); err != nil {
67 logger.Warnw(ctx, "failure-starting-logical-agent", log.Fields{"logical-device-id": lDevice.Id})
68 } else {
69 ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent)
70 }
71 }
72
73 probe.UpdateStatusFromContext(ctx, "logical-device-manager", probe.ServiceStatusRunning)
74 logger.Info(ctx, "logical-device-manager-started")
75}
76
Kent Hagerman2b216042020-04-03 18:28:56 -040077func (ldMgr *LogicalManager) addLogicalDeviceAgentToMap(agent *LogicalAgent) {
npujar1d86a522019-11-14 17:11:16 +053078 if _, exist := ldMgr.logicalDeviceAgents.Load(agent.logicalDeviceID); !exist {
79 ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent)
khenaidoob9203542018-09-17 22:56:37 -040080 }
81}
82
khenaidoo8c3303d2019-02-13 14:59:39 -050083// getLogicalDeviceAgent returns the logical device agent. If the device is not in memory then the device will
84// be loaded from dB and a logical device agent created to managed it.
Kent Hagerman2b216042020-04-03 18:28:56 -040085func (ldMgr *LogicalManager) getLogicalDeviceAgent(ctx context.Context, logicalDeviceID string) *LogicalAgent {
Rohan Agrawal31f21802020-06-12 05:38:46 +000086 logger.Debugw(ctx, "get-logical-device-agent", log.Fields{"logical-device-id": logicalDeviceID})
npujar1d86a522019-11-14 17:11:16 +053087 agent, ok := ldMgr.logicalDeviceAgents.Load(logicalDeviceID)
88 if ok {
Kent Hagerman2b216042020-04-03 18:28:56 -040089 lda := agent.(*LogicalAgent)
khenaidoo93d5a3d2020-01-15 12:37:05 -050090 if lda.logicalDevice == nil {
91 // This can happen when an agent for the logical device has been created but the logical device
92 // itself is not ready for action as it is waiting for switch and port capabilities from the
93 // relevant adapter. In such a case prevent any request aimed at that logical device.
Himani Chawlab4c25912020-11-12 17:16:38 +053094 logger.Debugf(ctx, "logical-device-%s-is-not-ready-to-serve-requests", logicalDeviceID)
khenaidoo93d5a3d2020-01-15 12:37:05 -050095 return nil
96 }
97 return lda
npujar1d86a522019-11-14 17:11:16 +053098 }
99 // Try to load into memory - loading will also create the logical device agent
npujar467fe752020-01-16 20:17:45 +0530100 if err := ldMgr.load(ctx, logicalDeviceID); err == nil {
npujar1d86a522019-11-14 17:11:16 +0530101 if agent, ok = ldMgr.logicalDeviceAgents.Load(logicalDeviceID); ok {
Kent Hagerman2b216042020-04-03 18:28:56 -0400102 return agent.(*LogicalAgent)
khenaidoo8c3303d2019-02-13 14:59:39 -0500103 }
khenaidoob9203542018-09-17 22:56:37 -0400104 }
105 return nil
106}
107
Kent Hagerman2b216042020-04-03 18:28:56 -0400108func (ldMgr *LogicalManager) deleteLogicalDeviceAgent(logicalDeviceID string) {
npujar1d86a522019-11-14 17:11:16 +0530109 ldMgr.logicalDeviceAgents.Delete(logicalDeviceID)
khenaidoo92e62c52018-10-03 14:02:54 -0400110}
111
khenaidoo8c3303d2019-02-13 14:59:39 -0500112// GetLogicalDevice provides a cloned most up to date logical device. If device is not in memory
113// it will be fetched from the dB
Kent Hagerman45a13e42020-04-13 12:23:50 -0400114func (ldMgr *LogicalManager) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530115 ctx = utils.WithRPCMetadataContext(ctx, "GetLogicalDevice")
116 logger.Debugw(ctx, "get-logical-device", log.Fields{"logical-device-id": id})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400117 if agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id); agent != nil {
Kent Hagermancba2f302020-07-28 13:37:36 -0400118 return agent.GetLogicalDeviceReadOnly(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400119 }
120 return nil, status.Errorf(codes.NotFound, "%s", id)
121}
122
Kent Hagerman2b216042020-04-03 18:28:56 -0400123//ListLogicalDevices returns the list of all logical devices
Kent Hagerman45a13e42020-04-13 12:23:50 -0400124func (ldMgr *LogicalManager) ListLogicalDevices(ctx context.Context, _ *empty.Empty) (*voltha.LogicalDevices, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530125 ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDevices")
126 logger.Debug(ctx, "list-all-logical-devices")
Kent Hagerman4f355f52020-03-30 16:01:33 -0400127
128 var logicalDevices []*voltha.LogicalDevice
Esin Karamana4e4e002021-03-02 10:42:16 +0000129 ldMgr.logicalDeviceAgents.Range(func(key, value interface{}) bool {
130 if ld, err := value.(*LogicalAgent).GetLogicalDeviceReadOnly(ctx); err == nil {
131 logicalDevices = append(logicalDevices, ld)
132 } else {
133 logger.Errorw(ctx, "unable-to-get-logical-device", log.Fields{"err": err})
134 }
135 return true
136 })
khenaidoo7585a962021-06-10 16:15:38 -0400137 logger.Debugw(ctx, "list-all-logical-devices", log.Fields{"num-logical-devices": len(logicalDevices)})
138
Kent Hagerman4f355f52020-03-30 16:01:33 -0400139 return &voltha.LogicalDevices{Items: logicalDevices}, nil
khenaidoob9203542018-09-17 22:56:37 -0400140}
141
Kent Hagerman2b216042020-04-03 18:28:56 -0400142func (ldMgr *LogicalManager) createLogicalDevice(ctx context.Context, device *voltha.Device) (*string, error) {
divyadesaicb8b59d2020-08-18 09:55:47 +0000143 logger.Debugw(ctx, "creating-logical-device", log.Fields{"device-id": device.Id})
khenaidoob9203542018-09-17 22:56:37 -0400144 // Sanity check
145 if !device.Root {
khenaidoo2c6a0992019-04-29 13:46:56 -0400146 return nil, errors.New("device-not-root")
khenaidoob9203542018-09-17 22:56:37 -0400147 }
148
149 // Create a logical device agent - the logical device Id is based on the mac address of the device
150 // For now use the serial number - it may contain any combination of alphabetic characters and numbers,
151 // with length varying from eight characters to a maximum of 14 characters. Mac Address is part of oneof
152 // in the Device model. May need to be moved out.
Kent Hagerman2b216042020-04-03 18:28:56 -0400153 id := utils.CreateLogicalDeviceID()
David Bainbridged1afd662020-03-26 18:27:41 -0700154 sn := strings.Replace(device.MacAddress, ":", "", -1)
khenaidoo92e62c52018-10-03 14:02:54 -0400155 if id == "" {
divyadesaicb8b59d2020-08-18 09:55:47 +0000156 logger.Errorw(ctx, "mac-address-not-set", log.Fields{"device-id": device.Id, "serial-number": sn})
khenaidoo92e62c52018-10-03 14:02:54 -0400157 return nil, errors.New("mac-address-not-set")
158 }
David Bainbridged1afd662020-03-26 18:27:41 -0700159
divyadesaicb8b59d2020-08-18 09:55:47 +0000160 logger.Debugw(ctx, "logical-device-id", log.Fields{"logical-device-id": id})
khenaidoob9203542018-09-17 22:56:37 -0400161
Rohan Agrawal31f21802020-06-12 05:38:46 +0000162 agent := newLogicalAgent(ctx, id, sn, device.Id, ldMgr, ldMgr.deviceMgr, ldMgr.dbPath, ldMgr.ldProxy, ldMgr.defaultTimeout)
khenaidoob9203542018-09-17 22:56:37 -0400163 ldMgr.addLogicalDeviceAgentToMap(agent)
khenaidooad06fd72019-10-28 12:26:05 -0400164
165 // Update the root device with the logical device Id reference
npujar467fe752020-01-16 20:17:45 +0530166 if err := ldMgr.deviceMgr.setParentID(ctx, device, id); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000167 logger.Errorw(ctx, "failed-setting-parent-id", log.Fields{"logical-device-id": id, "device-id": device.Id})
khenaidooad06fd72019-10-28 12:26:05 -0400168 return nil, err
169 }
170
npujar1d86a522019-11-14 17:11:16 +0530171 go func() {
Kent Hagermanf5a67352020-04-30 15:15:26 -0400172 //TODO: either wait for the agent to be started before returning, or
173 // implement locks in the agent to ensure request are not processed before start() is complete
Himani Chawlab4c25912020-11-12 17:16:38 +0530174 ldCtx := utils.WithSpanAndRPCMetadataFromContext(ctx)
khenaidoo7585a962021-06-10 16:15:38 -0400175 err := agent.start(ldCtx, false, nil)
npujar1d86a522019-11-14 17:11:16 +0530176 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000177 logger.Errorw(ctx, "unable-to-create-the-logical-device", log.Fields{"error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400178 ldMgr.deleteLogicalDeviceAgent(id)
npujar1d86a522019-11-14 17:11:16 +0530179 }
180 }()
khenaidoob9203542018-09-17 22:56:37 -0400181
Rohan Agrawal31f21802020-06-12 05:38:46 +0000182 logger.Debug(ctx, "creating-logical-device-ends")
khenaidoob9203542018-09-17 22:56:37 -0400183 return &id, nil
184}
185
khenaidoo6d62c002019-05-15 21:57:03 -0400186// stopManagingLogicalDeviceWithDeviceId stops the management of the logical device. This implies removal of any
187// reference of this logical device in cache. The device Id is passed as param because the logical device may already
188// have been removed from the model. This function returns the logical device Id if found
Kent Hagerman2b216042020-04-03 18:28:56 -0400189func (ldMgr *LogicalManager) stopManagingLogicalDeviceWithDeviceID(ctx context.Context, id string) string {
divyadesaicb8b59d2020-08-18 09:55:47 +0000190 logger.Infow(ctx, "stop-managing-logical-device", log.Fields{"device-id": id})
khenaidoo6d62c002019-05-15 21:57:03 -0400191 // Go over the list of logical device agents to find the one which has rootDeviceId as id
npujar1d86a522019-11-14 17:11:16 +0530192 var ldID = ""
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400193 ldMgr.logicalDeviceAgents.Range(func(key, value interface{}) bool {
Kent Hagerman2b216042020-04-03 18:28:56 -0400194 ldAgent := value.(*LogicalAgent)
npujar1d86a522019-11-14 17:11:16 +0530195 if ldAgent.rootDeviceID == id {
divyadesaicb8b59d2020-08-18 09:55:47 +0000196 logger.Infow(ctx, "stopping-logical-device-agent", log.Fields{"logical-device-id": key})
npujar467fe752020-01-16 20:17:45 +0530197 if err := ldAgent.stop(ctx); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000198 logger.Errorw(ctx, "failed-to-stop-LDAgent", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530199 return false
200 }
npujar1d86a522019-11-14 17:11:16 +0530201 ldID = key.(string)
202 ldMgr.logicalDeviceAgents.Delete(ldID)
khenaidoo6d62c002019-05-15 21:57:03 -0400203 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400204 return true
205 })
npujar1d86a522019-11-14 17:11:16 +0530206 return ldID
khenaidoo6d62c002019-05-15 21:57:03 -0400207}
208
209//getLogicalDeviceFromModel retrieves the logical device data from the model.
Kent Hagerman2b216042020-04-03 18:28:56 -0400210func (ldMgr *LogicalManager) getLogicalDeviceFromModel(ctx context.Context, lDeviceID string) (*voltha.LogicalDevice, error) {
Kent Hagerman4f355f52020-03-30 16:01:33 -0400211 logicalDevice := &voltha.LogicalDevice{}
Kent Hagermanf5a67352020-04-30 15:15:26 -0400212 if have, err := ldMgr.ldProxy.Get(ctx, lDeviceID, logicalDevice); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000213 logger.Errorw(ctx, "failed-to-get-logical-devices-from-cluster-proxy", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530214 return nil, err
Kent Hagerman4f355f52020-03-30 16:01:33 -0400215 } else if !have {
216 return nil, status.Error(codes.NotFound, lDeviceID)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530217 }
Kent Hagerman4f355f52020-03-30 16:01:33 -0400218
219 return logicalDevice, nil
khenaidoo6d62c002019-05-15 21:57:03 -0400220}
221
khenaidoo297cd252019-02-07 22:10:23 -0500222// load loads a logical device manager in memory
Kent Hagerman2b216042020-04-03 18:28:56 -0400223func (ldMgr *LogicalManager) load(ctx context.Context, lDeviceID string) error {
npujar1d86a522019-11-14 17:11:16 +0530224 if lDeviceID == "" {
khenaidoo4c9e5592019-09-09 16:20:41 -0400225 return nil
khenaidoo297cd252019-02-07 22:10:23 -0500226 }
khenaidoo4c9e5592019-09-09 16:20:41 -0400227 // Add a lock to prevent two concurrent calls from loading the same device twice
228 ldMgr.logicalDevicesLoadingLock.Lock()
npujar1d86a522019-11-14 17:11:16 +0530229 if _, exist := ldMgr.logicalDeviceLoadingInProgress[lDeviceID]; !exist {
230 if ldAgent, _ := ldMgr.logicalDeviceAgents.Load(lDeviceID); ldAgent == nil {
231 ldMgr.logicalDeviceLoadingInProgress[lDeviceID] = []chan int{make(chan int, 1)}
khenaidoo4c9e5592019-09-09 16:20:41 -0400232 ldMgr.logicalDevicesLoadingLock.Unlock()
npujar467fe752020-01-16 20:17:45 +0530233 if _, err := ldMgr.getLogicalDeviceFromModel(ctx, lDeviceID); err == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000234 logger.Debugw(ctx, "loading-logical-device", log.Fields{"lDeviceId": lDeviceID})
235 agent := newLogicalAgent(ctx, lDeviceID, "", "", ldMgr, ldMgr.deviceMgr, ldMgr.dbPath, ldMgr.ldProxy, ldMgr.defaultTimeout)
khenaidoo7585a962021-06-10 16:15:38 -0400236 if err := agent.start(ctx, true, nil); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400237 return err
khenaidoo4c9e5592019-09-09 16:20:41 -0400238 }
khenaidoo442e7c72020-03-10 16:13:48 -0400239 ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent)
khenaidoo4c9e5592019-09-09 16:20:41 -0400240 } else {
Himani Chawlab4c25912020-11-12 17:16:38 +0530241 logger.Debugw(ctx, "logical-device-not-in-model", log.Fields{"logical-device-id": lDeviceID})
khenaidoo4c9e5592019-09-09 16:20:41 -0400242 }
243 // announce completion of task to any number of waiting channels
244 ldMgr.logicalDevicesLoadingLock.Lock()
npujar1d86a522019-11-14 17:11:16 +0530245 if v, ok := ldMgr.logicalDeviceLoadingInProgress[lDeviceID]; ok {
khenaidoo4c9e5592019-09-09 16:20:41 -0400246 for _, ch := range v {
247 close(ch)
248 }
npujar1d86a522019-11-14 17:11:16 +0530249 delete(ldMgr.logicalDeviceLoadingInProgress, lDeviceID)
khenaidoo4c9e5592019-09-09 16:20:41 -0400250 }
251 ldMgr.logicalDevicesLoadingLock.Unlock()
252 } else {
253 ldMgr.logicalDevicesLoadingLock.Unlock()
254 }
255 } else {
256 ch := make(chan int, 1)
npujar1d86a522019-11-14 17:11:16 +0530257 ldMgr.logicalDeviceLoadingInProgress[lDeviceID] = append(ldMgr.logicalDeviceLoadingInProgress[lDeviceID], ch)
khenaidoo4c9e5592019-09-09 16:20:41 -0400258 ldMgr.logicalDevicesLoadingLock.Unlock()
259 // Wait for the channel to be closed, implying the process loading this device is done.
260 <-ch
261 }
npujar1d86a522019-11-14 17:11:16 +0530262 if _, exist := ldMgr.logicalDeviceAgents.Load(lDeviceID); exist {
khenaidoo4c9e5592019-09-09 16:20:41 -0400263 return nil
264 }
npujar1d86a522019-11-14 17:11:16 +0530265 return status.Errorf(codes.Aborted, "Error loading logical device %s", lDeviceID)
khenaidoo297cd252019-02-07 22:10:23 -0500266}
267
Kent Hagerman2b216042020-04-03 18:28:56 -0400268func (ldMgr *LogicalManager) deleteLogicalDevice(ctx context.Context, device *voltha.Device) error {
divyadesaicb8b59d2020-08-18 09:55:47 +0000269 logger.Debugw(ctx, "deleting-logical-device", log.Fields{"device-id": device.Id})
khenaidoo92e62c52018-10-03 14:02:54 -0400270 // Sanity check
271 if !device.Root {
khenaidoo2c6a0992019-04-29 13:46:56 -0400272 return errors.New("device-not-root")
khenaidoo92e62c52018-10-03 14:02:54 -0400273 }
npujar1d86a522019-11-14 17:11:16 +0530274 logDeviceID := device.ParentId
npujar467fe752020-01-16 20:17:45 +0530275 if agent := ldMgr.getLogicalDeviceAgent(ctx, logDeviceID); agent != nil {
khenaidoo92e62c52018-10-03 14:02:54 -0400276 // Stop the logical device agent
Thomas Lee Se5a44012019-11-07 20:32:24 +0530277 if err := agent.stop(ctx); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000278 logger.Errorw(ctx, "failed-to-stop-agent", log.Fields{"error": err})
Thomas Lee Se5a44012019-11-07 20:32:24 +0530279 return err
280 }
khenaidoo92e62c52018-10-03 14:02:54 -0400281 //Remove the logical device agent from the Map
npujar1d86a522019-11-14 17:11:16 +0530282 ldMgr.deleteLogicalDeviceAgent(logDeviceID)
khenaidoo92e62c52018-10-03 14:02:54 -0400283 }
284
Rohan Agrawal31f21802020-06-12 05:38:46 +0000285 logger.Debug(ctx, "deleting-logical-device-ends")
khenaidoo92e62c52018-10-03 14:02:54 -0400286 return nil
287}
288
Kent Hagerman2b216042020-04-03 18:28:56 -0400289func (ldMgr *LogicalManager) getLogicalDeviceID(ctx context.Context, device *voltha.Device) (*string, error) {
khenaidoo92e62c52018-10-03 14:02:54 -0400290 // Device can either be a parent or a child device
291 if device.Root {
292 // Parent device. The ID of a parent device is the logical device ID
293 return &device.ParentId, nil
294 }
295 // Device is child device
296 // retrieve parent device using child device ID
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400297 // TODO: return (string, have) instead of *string
298 // also: If not root device, just return device.parentID instead of loading the parent device.
npujar467fe752020-01-16 20:17:45 +0530299 if parentDevice := ldMgr.deviceMgr.getParentDevice(ctx, device); parentDevice != nil {
khenaidoo92e62c52018-10-03 14:02:54 -0400300 return &parentDevice.ParentId, nil
301 }
302 return nil, status.Errorf(codes.NotFound, "%s", device.Id)
303}
304
Kent Hagerman2b216042020-04-03 18:28:56 -0400305func (ldMgr *LogicalManager) getLogicalDeviceIDFromDeviceID(ctx context.Context, deviceID string) (*string, error) {
khenaidoo3ab34882019-05-02 21:33:30 -0400306 // Get the device
307 var device *voltha.Device
308 var err error
Kent Hagermancba2f302020-07-28 13:37:36 -0400309 if device, err = ldMgr.deviceMgr.getDeviceReadOnly(ctx, deviceID); err != nil {
khenaidoo3ab34882019-05-02 21:33:30 -0400310 return nil, err
311 }
npujar467fe752020-01-16 20:17:45 +0530312 return ldMgr.getLogicalDeviceID(ctx, device)
khenaidoo3ab34882019-05-02 21:33:30 -0400313}
314
npujar1d86a522019-11-14 17:11:16 +0530315// ListLogicalDeviceFlows returns the flows of logical device
Kent Hagerman45a13e42020-04-13 12:23:50 -0400316func (ldMgr *LogicalManager) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530317 ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDeviceFlows")
318 logger.Debugw(ctx, "list-logical-device-flows", log.Fields{"logical-device-id": id.Id})
Kent Hagerman433a31a2020-05-20 19:04:48 -0400319 agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id)
320 if agent == nil {
321 return nil, status.Errorf(codes.NotFound, "%s", id.Id)
khenaidoodd237172019-05-27 16:37:17 -0400322 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400323
324 flows := agent.listLogicalDeviceFlows()
325 ctr, ret := 0, make([]*openflow_13.OfpFlowStats, len(flows))
326 for _, flow := range flows {
327 ret[ctr] = flow
328 ctr++
329 }
khenaidoo7585a962021-06-10 16:15:38 -0400330 logger.Debugw(ctx, "list-logical-device-flows", log.Fields{"logical-device-id": id.Id, "num-flows": len(flows)})
Kent Hagerman433a31a2020-05-20 19:04:48 -0400331 return &openflow_13.Flows{Items: ret}, nil
khenaidoodd237172019-05-27 16:37:17 -0400332}
333
npujar1d86a522019-11-14 17:11:16 +0530334// ListLogicalDeviceFlowGroups returns logical device flow groups
Kent Hagerman45a13e42020-04-13 12:23:50 -0400335func (ldMgr *LogicalManager) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530336 ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDeviceFlowGroups")
337 logger.Debugw(ctx, "list-logical-device-flow-groups", log.Fields{"logical-device-id": id.Id})
Kent Hagerman433a31a2020-05-20 19:04:48 -0400338 agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id)
339 if agent == nil {
340 return nil, status.Errorf(codes.NotFound, "%s", id.Id)
khenaidoodd237172019-05-27 16:37:17 -0400341 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400342
343 groups := agent.listLogicalDeviceGroups()
344 ctr, ret := 0, make([]*openflow_13.OfpGroupEntry, len(groups))
345 for _, group := range groups {
346 ret[ctr] = group
347 ctr++
348 }
khenaidoo7585a962021-06-10 16:15:38 -0400349 logger.Debugw(ctx, "list-logical-device-flow-groups", log.Fields{"logical-device-id": id.Id, "num-groups": len(groups)})
Kent Hagerman433a31a2020-05-20 19:04:48 -0400350 return &openflow_13.FlowGroups{Items: ret}, nil
khenaidoodd237172019-05-27 16:37:17 -0400351}
352
npujar1d86a522019-11-14 17:11:16 +0530353// ListLogicalDevicePorts returns logical device ports
Kent Hagerman45a13e42020-04-13 12:23:50 -0400354func (ldMgr *LogicalManager) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530355 ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDevicePorts")
356 logger.Debugw(ctx, "list-logical-device-ports", log.Fields{"logical-device-id": id.Id})
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400357 agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id)
358 if agent == nil {
359 return nil, status.Errorf(codes.NotFound, "%s", id.Id)
khenaidoo19d7b632018-10-30 10:49:50 -0400360 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400361
Rohan Agrawal31f21802020-06-12 05:38:46 +0000362 ports := agent.listLogicalDevicePorts(ctx)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400363 ctr, ret := 0, make([]*voltha.LogicalPort, len(ports))
364 for _, port := range ports {
365 ret[ctr] = port
366 ctr++
367 }
khenaidoo7585a962021-06-10 16:15:38 -0400368 logger.Debugw(ctx, "list-logical-device-ports", log.Fields{"logical-device-id": id.Id, "num-ports": len(ports)})
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400369 return &voltha.LogicalPorts{Items: ret}, nil
khenaidoo19d7b632018-10-30 10:49:50 -0400370}
371
Kent Hagerman45a13e42020-04-13 12:23:50 -0400372// GetLogicalDevicePort returns logical device port details
373func (ldMgr *LogicalManager) GetLogicalDevicePort(ctx context.Context, lPortID *voltha.LogicalPortId) (*voltha.LogicalPort, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530374 ctx = utils.WithRPCMetadataContext(ctx, "GetLogicalDevicePort")
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400375 // Get the logical device where this port is attached
376 agent := ldMgr.getLogicalDeviceAgent(ctx, lPortID.Id)
377 if agent == nil {
378 return nil, status.Errorf(codes.NotFound, "%s", lPortID.Id)
khenaidoo19d7b632018-10-30 10:49:50 -0400379 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400380
Rohan Agrawal31f21802020-06-12 05:38:46 +0000381 for _, port := range agent.listLogicalDevicePorts(ctx) {
npujar1d86a522019-11-14 17:11:16 +0530382 if port.Id == lPortID.PortId {
khenaidoo19d7b632018-10-30 10:49:50 -0400383 return port, nil
384 }
385 }
npujar1d86a522019-11-14 17:11:16 +0530386 return nil, status.Errorf(codes.NotFound, "%s-%s", lPortID.Id, lPortID.PortId)
khenaidoo19d7b632018-10-30 10:49:50 -0400387}
388
khenaidoo2c6a0992019-04-29 13:46:56 -0400389// updateLogicalPort sets up a logical port on the logical device based on the device port
390// information, if needed
Kent Hagerman2a07b862020-06-19 15:23:07 -0400391func (ldMgr *LogicalManager) updateLogicalPort(ctx context.Context, device *voltha.Device, devicePorts map[uint32]*voltha.Port, port *voltha.Port) error {
npujar467fe752020-01-16 20:17:45 +0530392 ldID, err := ldMgr.getLogicalDeviceID(ctx, device)
npujar1d86a522019-11-14 17:11:16 +0530393 if err != nil || *ldID == "" {
khenaidoo2c6a0992019-04-29 13:46:56 -0400394 // This is not an error as the logical device may not have been created at this time. In such a case,
395 // the ports will be created when the logical device is ready.
396 return nil
khenaidoo2c6a0992019-04-29 13:46:56 -0400397 }
npujar467fe752020-01-16 20:17:45 +0530398 if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil {
Kent Hagerman2a07b862020-06-19 15:23:07 -0400399 if err := agent.updateLogicalPort(ctx, device, devicePorts, port); err != nil {
npujar1d86a522019-11-14 17:11:16 +0530400 return err
khenaidoofc1314d2019-03-14 09:34:21 -0400401 }
402 }
403 return nil
404}
405
khenaidoo0a822f92019-05-08 15:15:57 -0400406// deleteLogicalPort removes the logical port associated with a child device
Kent Hagerman2b216042020-04-03 18:28:56 -0400407func (ldMgr *LogicalManager) deleteLogicalPorts(ctx context.Context, deviceID string) error {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000408 logger.Debugw(ctx, "deleting-logical-ports", log.Fields{"device-id": deviceID})
khenaidoo0a822f92019-05-08 15:15:57 -0400409 // Get logical port
npujar467fe752020-01-16 20:17:45 +0530410 ldID, err := ldMgr.getLogicalDeviceIDFromDeviceID(ctx, deviceID)
npujar1d86a522019-11-14 17:11:16 +0530411 if err != nil {
khenaidoo0a822f92019-05-08 15:15:57 -0400412 return err
npujar1d86a522019-11-14 17:11:16 +0530413 }
npujar467fe752020-01-16 20:17:45 +0530414 if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil {
415 if err = agent.deleteLogicalPorts(ctx, deviceID); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000416 logger.Warnw(ctx, "delete-logical-ports-failed", log.Fields{"logical-device-id": *ldID})
npujar1d86a522019-11-14 17:11:16 +0530417 return err
khenaidoo0a822f92019-05-08 15:15:57 -0400418 }
419 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000420 logger.Debug(ctx, "deleting-logical-ports-ends")
khenaidoo0a822f92019-05-08 15:15:57 -0400421 return nil
422}
423
Kent Hagerman2a07b862020-06-19 15:23:07 -0400424func (ldMgr *LogicalManager) setupUNILogicalPorts(ctx context.Context, childDevice *voltha.Device, childDevicePorts map[uint32]*voltha.Port) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530425 logger.Debugw(ctx, "setup-uni-logical-ports", log.Fields{"child-device-id": childDevice.Id, "parent-device-id": childDevice.ParentId, "current-data": childDevice})
khenaidoob9203542018-09-17 22:56:37 -0400426 // Sanity check
427 if childDevice.Root {
428 return errors.New("Device-root")
429 }
430
431 // Get the logical device id parent device
npujar1d86a522019-11-14 17:11:16 +0530432 parentID := childDevice.ParentId
npujar467fe752020-01-16 20:17:45 +0530433 logDeviceID := ldMgr.deviceMgr.GetParentDeviceID(ctx, parentID)
khenaidoob9203542018-09-17 22:56:37 -0400434
Himani Chawlab4c25912020-11-12 17:16:38 +0530435 logger.Debugw(ctx, "setup-uni-logical-ports", log.Fields{"logical-device-id": logDeviceID, "parentId": parentID})
khenaidoob9203542018-09-17 22:56:37 -0400436
npujar1d86a522019-11-14 17:11:16 +0530437 if parentID == "" || logDeviceID == "" {
khenaidoo2c6a0992019-04-29 13:46:56 -0400438 return errors.New("device-in-invalid-state")
khenaidoo5e677ae2019-02-28 17:26:29 -0500439 }
440
npujar467fe752020-01-16 20:17:45 +0530441 if agent := ldMgr.getLogicalDeviceAgent(ctx, logDeviceID); agent != nil {
Kent Hagerman2a07b862020-06-19 15:23:07 -0400442 if err := agent.setupUNILogicalPorts(ctx, childDevice, childDevicePorts); err != nil {
khenaidoobcf205b2019-01-25 22:21:14 -0500443 return err
444 }
khenaidoob9203542018-09-17 22:56:37 -0400445 }
khenaidoo21d51152019-02-01 13:48:37 -0500446 return nil
khenaidoob9203542018-09-17 22:56:37 -0400447}
khenaidoo19d7b632018-10-30 10:49:50 -0400448
Kent Hagerman2b216042020-04-03 18:28:56 -0400449func (ldMgr *LogicalManager) deleteAllLogicalPorts(ctx context.Context, device *voltha.Device) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530450 logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"device-id": device.Id})
khenaidoo0a822f92019-05-08 15:15:57 -0400451
npujar1d86a522019-11-14 17:11:16 +0530452 var ldID *string
khenaidoo0a822f92019-05-08 15:15:57 -0400453 var err error
454 //Get the logical device Id for this device
npujar467fe752020-01-16 20:17:45 +0530455 if ldID, err = ldMgr.getLogicalDeviceID(ctx, device); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000456 logger.Warnw(ctx, "no-logical-device-found", log.Fields{"device-id": device.Id, "error": err})
khenaidoo0a822f92019-05-08 15:15:57 -0400457 return err
458 }
npujar467fe752020-01-16 20:17:45 +0530459 if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil {
Girish Gowdra408cd962020-03-11 14:31:31 -0700460 if err := agent.deleteAllLogicalPorts(ctx); err != nil {
khenaidoo0a822f92019-05-08 15:15:57 -0400461 return err
462 }
463 }
464 return nil
465}
Hardik Windlassc704def2020-02-26 18:23:19 +0000466
Kent Hagerman2b216042020-04-03 18:28:56 -0400467func (ldMgr *LogicalManager) updatePortState(ctx context.Context, deviceID string, portNo uint32, state voltha.OperStatus_Types) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530468 logger.Debugw(ctx, "update-Port-state", log.Fields{"device-id": deviceID, "state": state, "port-no": portNo})
khenaidoo171b98e2019-10-31 11:48:15 -0400469
npujar1d86a522019-11-14 17:11:16 +0530470 var ldID *string
khenaidoo171b98e2019-10-31 11:48:15 -0400471 var err error
472 //Get the logical device Id for this device
npujar467fe752020-01-16 20:17:45 +0530473 if ldID, err = ldMgr.getLogicalDeviceIDFromDeviceID(ctx, deviceID); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000474 logger.Warnw(ctx, "no-logical-device-found", log.Fields{"device-id": deviceID, "error": err})
khenaidoo171b98e2019-10-31 11:48:15 -0400475 return err
476 }
npujar467fe752020-01-16 20:17:45 +0530477 if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil {
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400478 if err := agent.updatePortState(ctx, portNo, state); err != nil {
khenaidoo171b98e2019-10-31 11:48:15 -0400479 return err
480 }
481 }
482 return nil
483}
khenaidoo0a822f92019-05-08 15:15:57 -0400484
Kent Hagerman45a13e42020-04-13 12:23:50 -0400485// UpdateLogicalDeviceFlowTable updates logical device flow table
486func (ldMgr *LogicalManager) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530487 ctx = utils.WithRPCMetadataContext(ctx, "UpdateLogicalDeviceFlowTable")
488 logger.Debugw(ctx, "update-logical-device-flow-table", log.Fields{"logical-device-id": flow.Id})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400489 agent := ldMgr.getLogicalDeviceAgent(ctx, flow.Id)
490 if agent == nil {
491 return nil, status.Errorf(codes.NotFound, "%s", flow.Id)
khenaidoo19d7b632018-10-30 10:49:50 -0400492 }
Maninderf421da62020-12-04 11:44:58 +0530493 return &empty.Empty{}, agent.updateFlowTable(ctx, flow)
khenaidoo19d7b632018-10-30 10:49:50 -0400494}
495
Kent Hagerman45a13e42020-04-13 12:23:50 -0400496// UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response
497func (ldMgr *LogicalManager) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530498 ctx = utils.WithRPCMetadataContext(ctx, "UpdateLogicalDeviceMeterTable")
499 logger.Debugw(ctx, "update-logical-device-meter-table", log.Fields{"logical-device-id": meter.Id})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400500 agent := ldMgr.getLogicalDeviceAgent(ctx, meter.Id)
501 if agent == nil {
502 return nil, status.Errorf(codes.NotFound, "%s", meter.Id)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400503 }
Kent Hagerman45a13e42020-04-13 12:23:50 -0400504 return &empty.Empty{}, agent.updateMeterTable(ctx, meter.MeterMod)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400505}
506
npujar1d86a522019-11-14 17:11:16 +0530507// ListLogicalDeviceMeters returns logical device meters
Kent Hagerman45a13e42020-04-13 12:23:50 -0400508func (ldMgr *LogicalManager) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530509 ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDeviceMeters")
510 logger.Debugw(ctx, "list-logical-device-meters", log.Fields{"logical-device-id": id.Id})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400511 agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id)
512 if agent == nil {
513 return nil, status.Errorf(codes.NotFound, "%s", id.Id)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400514 }
Kent Hagerman433a31a2020-05-20 19:04:48 -0400515 meters := agent.listLogicalDeviceMeters()
516 ctr, ret := 0, make([]*openflow_13.OfpMeterEntry, len(meters))
517 for _, meter := range meters {
518 ret[ctr] = meter
519 ctr++
520 }
521 return &openflow_13.Meters{Items: ret}, nil
khenaidoo19d7b632018-10-30 10:49:50 -0400522}
523
Kent Hagerman45a13e42020-04-13 12:23:50 -0400524// UpdateLogicalDeviceFlowGroupTable updates logical device flow group table
525func (ldMgr *LogicalManager) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530526 ctx = utils.WithRPCMetadataContext(ctx, "UpdateLogicalDeviceFlowGroupTable")
527 logger.Debugw(ctx, "update-group-table", log.Fields{"logical-device-id": flow.Id})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400528 agent := ldMgr.getLogicalDeviceAgent(ctx, flow.Id)
529 if agent == nil {
530 return nil, status.Errorf(codes.NotFound, "%s", flow.Id)
khenaidoo19d7b632018-10-30 10:49:50 -0400531 }
Kent Hagerman45a13e42020-04-13 12:23:50 -0400532 return &empty.Empty{}, agent.updateGroupTable(ctx, flow.GroupMod)
khenaidoo19d7b632018-10-30 10:49:50 -0400533}
534
Kent Hagerman45a13e42020-04-13 12:23:50 -0400535// EnableLogicalDevicePort enables logical device port
536func (ldMgr *LogicalManager) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530537 ctx = utils.WithRPCMetadataContext(ctx, "EnableLogicalDevicePort")
538 logger.Debugw(ctx, "enable-logical-device-port", log.Fields{"logical-device-id": id})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400539 agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id)
540 if agent == nil {
541 return nil, status.Errorf(codes.NotFound, "%s", id.Id)
khenaidoo19d7b632018-10-30 10:49:50 -0400542 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400543 portNo, err := strconv.ParseUint(id.PortId, 10, 32)
544 if err != nil {
545 return nil, status.Errorf(codes.InvalidArgument, "failed to parse %s as a number", id.PortId)
546 }
547 return &empty.Empty{}, agent.enableLogicalPort(ctx, uint32(portNo))
Kent Hagerman45a13e42020-04-13 12:23:50 -0400548}
549
550// DisableLogicalDevicePort disables logical device port
551func (ldMgr *LogicalManager) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) {
Himani Chawlab4c25912020-11-12 17:16:38 +0530552 ctx = utils.WithRPCMetadataContext(ctx, "DisableLogicalDevicePort")
553 logger.Debugw(ctx, "disable-logical-device-port", log.Fields{"logical-device-id": id})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400554 agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id)
555 if agent == nil {
556 return nil, status.Errorf(codes.NotFound, "%s", id.Id)
557 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400558 portNo, err := strconv.ParseUint(id.PortId, 10, 32)
559 if err != nil {
560 return nil, status.Errorf(codes.InvalidArgument, "failed to parse %s as a number", id.PortId)
561 }
562 return &empty.Empty{}, agent.disableLogicalPort(ctx, uint32(portNo))
khenaidoo19d7b632018-10-30 10:49:50 -0400563}
khenaidoofdbad6e2018-11-06 22:26:38 -0500564
Kent Hagerman2b216042020-04-03 18:28:56 -0400565func (ldMgr *LogicalManager) packetIn(ctx context.Context, logicalDeviceID string, port uint32, transactionID string, packet []byte) error {
Himani Chawlab4c25912020-11-12 17:16:38 +0530566 logger.Debugw(ctx, "packet-in", log.Fields{"logical-device-id": logicalDeviceID, "port": port})
npujar467fe752020-01-16 20:17:45 +0530567 if agent := ldMgr.getLogicalDeviceAgent(ctx, logicalDeviceID); agent != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000568 agent.packetIn(ctx, port, transactionID, packet)
khenaidoofdbad6e2018-11-06 22:26:38 -0500569 } else {
divyadesaicb8b59d2020-08-18 09:55:47 +0000570 logger.Error(ctx, "logical-device-not-exist", log.Fields{"logical-device-id": logicalDeviceID})
khenaidoofdbad6e2018-11-06 22:26:38 -0500571 }
572 return nil
573}
Kent Hagerman2b216042020-04-03 18:28:56 -0400574
Kent Hagerman45a13e42020-04-13 12:23:50 -0400575// StreamPacketsOut sends packets to adapter
576func (ldMgr *LogicalManager) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000577 ctx := context.Background()
Himani Chawlab4c25912020-11-12 17:16:38 +0530578 logger.Debugw(ctx, "stream-packets-out-request", log.Fields{"packets": packets})
579
Kent Hagerman45a13e42020-04-13 12:23:50 -0400580loop:
581 for {
582 select {
583 case <-packets.Context().Done():
Himani Chawlab4c25912020-11-12 17:16:38 +0530584 logger.Infow(ctx, "stream-packets-out-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400585 break loop
586 default:
587 }
588
589 packet, err := packets.Recv()
590
Himani Chawlab4c25912020-11-12 17:16:38 +0530591 pktCtx := utils.WithRPCMetadataContext(packets.Context(), "StreamPacketsOut")
592
Kent Hagerman45a13e42020-04-13 12:23:50 -0400593 if err == io.EOF {
Himani Chawlab4c25912020-11-12 17:16:38 +0530594 logger.Debugw(ctx, "received-eof", log.Fields{"packets": packets})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400595 break loop
596 }
Kent Hagerman45a13e42020-04-13 12:23:50 -0400597 if err != nil {
Himani Chawlab4c25912020-11-12 17:16:38 +0530598 logger.Errorw(ctx, "failed-to-receive-packet-out", log.Fields{"error": err})
Andrea Campanellaff2d73a2021-03-04 14:47:17 +0100599 // we do not have the resource Id here due to error in the packet, setting to empty
Himani Chawla606a4f02021-03-23 19:45:58 +0530600 ldMgr.SendRPCEvent(pktCtx, "", err.Error(), nil,
601 "RPC_ERROR_RAISE_EVENT", voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix())
Kent Hagerman45a13e42020-04-13 12:23:50 -0400602 continue
603 }
604
Himani Chawlab4c25912020-11-12 17:16:38 +0530605 if agent := ldMgr.getLogicalDeviceAgent(pktCtx, packet.Id); agent != nil {
606 agent.packetOut(pktCtx, packet.PacketOut)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400607 } else {
Himani Chawlab4c25912020-11-12 17:16:38 +0530608 logger.Errorf(ctx, "no-logical-device-agent-present", log.Fields{"logical-device-id": packet.Id})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400609 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400610 }
Kent Hagerman45a13e42020-04-13 12:23:50 -0400611
Himani Chawlab4c25912020-11-12 17:16:38 +0530612 logger.Debugw(ctx, "stream-packets-out-request-done", log.Fields{"packets": packets})
Kent Hagerman45a13e42020-04-13 12:23:50 -0400613 return nil
Kent Hagerman2b216042020-04-03 18:28:56 -0400614}
Himani Chawlab4c25912020-11-12 17:16:38 +0530615
616func (ldMgr *LogicalManager) SendRPCEvent(ctx context.Context, resourceID, desc string, context map[string]string,
617 id string, category voltha.EventCategory_Types, subCategory *voltha.EventSubCategory_Types, raisedTs int64) {
Mahir Gunyelb0343bf2021-05-11 14:14:26 -0700618 ldMgr.Manager.Agent.GetAndSendRPCEvent(ctx, resourceID, desc, context, id,
Himani Chawlab4c25912020-11-12 17:16:38 +0530619 category, subCategory, raisedTs)
620}