khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 17 | package device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame^] | 22 | "github.com/opencord/voltha-lib-go/v4/pkg/probe" |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 23 | "io" |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 24 | "strconv" |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 25 | "strings" |
| 26 | "sync" |
| 27 | "time" |
| 28 | |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 29 | "github.com/golang/protobuf/ptypes/empty" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 30 | "github.com/opencord/voltha-go/db/model" |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 31 | "github.com/opencord/voltha-go/rw_core/core/device/event" |
| 32 | "github.com/opencord/voltha-go/rw_core/utils" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 33 | "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" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 37 | "google.golang.org/grpc/codes" |
| 38 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | ) |
| 40 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 41 | // LogicalManager represent logical device manager attributes |
| 42 | type LogicalManager struct { |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 43 | *event.Manager |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 44 | logicalDeviceAgents sync.Map |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 45 | deviceMgr *Manager |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 46 | kafkaICProxy kafka.InterContainerProxy |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 47 | dbPath *model.Path |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 48 | ldProxy *model.Proxy |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 49 | defaultTimeout time.Duration |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 50 | logicalDevicesLoadingLock sync.RWMutex |
| 51 | logicalDeviceLoadingInProgress map[string][]chan int |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | } |
| 53 | |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame^] | 54 | func (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 Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 77 | func (ldMgr *LogicalManager) addLogicalDeviceAgentToMap(agent *LogicalAgent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 78 | if _, exist := ldMgr.logicalDeviceAgents.Load(agent.logicalDeviceID); !exist { |
| 79 | ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 83 | // 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 Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 85 | func (ldMgr *LogicalManager) getLogicalDeviceAgent(ctx context.Context, logicalDeviceID string) *LogicalAgent { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 86 | logger.Debugw(ctx, "get-logical-device-agent", log.Fields{"logical-device-id": logicalDeviceID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 87 | agent, ok := ldMgr.logicalDeviceAgents.Load(logicalDeviceID) |
| 88 | if ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 89 | lda := agent.(*LogicalAgent) |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 90 | 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 Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 94 | logger.Debugf(ctx, "logical-device-%s-is-not-ready-to-serve-requests", logicalDeviceID) |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 95 | return nil |
| 96 | } |
| 97 | return lda |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 98 | } |
| 99 | // Try to load into memory - loading will also create the logical device agent |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 100 | if err := ldMgr.load(ctx, logicalDeviceID); err == nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 101 | if agent, ok = ldMgr.logicalDeviceAgents.Load(logicalDeviceID); ok { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 102 | return agent.(*LogicalAgent) |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 103 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 104 | } |
| 105 | return nil |
| 106 | } |
| 107 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 108 | func (ldMgr *LogicalManager) deleteLogicalDeviceAgent(logicalDeviceID string) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 109 | ldMgr.logicalDeviceAgents.Delete(logicalDeviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 110 | } |
| 111 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 112 | // 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 Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 114 | func (ldMgr *LogicalManager) GetLogicalDevice(ctx context.Context, id *voltha.ID) (*voltha.LogicalDevice, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 115 | ctx = utils.WithRPCMetadataContext(ctx, "GetLogicalDevice") |
| 116 | logger.Debugw(ctx, "get-logical-device", log.Fields{"logical-device-id": id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 117 | if agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id); agent != nil { |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 118 | return agent.GetLogicalDeviceReadOnly(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 119 | } |
| 120 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 121 | } |
| 122 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 123 | //ListLogicalDevices returns the list of all logical devices |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 124 | func (ldMgr *LogicalManager) ListLogicalDevices(ctx context.Context, _ *empty.Empty) (*voltha.LogicalDevices, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 125 | ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDevices") |
| 126 | logger.Debug(ctx, "list-all-logical-devices") |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 127 | |
| 128 | var logicalDevices []*voltha.LogicalDevice |
Esin Karaman | a4e4e00 | 2021-03-02 10:42:16 +0000 | [diff] [blame] | 129 | 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 | }) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame^] | 137 | logger.Debugw(ctx, "list-all-logical-devices", log.Fields{"num-logical-devices": len(logicalDevices)}) |
| 138 | |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 139 | return &voltha.LogicalDevices{Items: logicalDevices}, nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 140 | } |
| 141 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 142 | func (ldMgr *LogicalManager) createLogicalDevice(ctx context.Context, device *voltha.Device) (*string, error) { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 143 | logger.Debugw(ctx, "creating-logical-device", log.Fields{"device-id": device.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 144 | // Sanity check |
| 145 | if !device.Root { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 146 | return nil, errors.New("device-not-root") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 147 | } |
| 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 Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 153 | id := utils.CreateLogicalDeviceID() |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 154 | sn := strings.Replace(device.MacAddress, ":", "", -1) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 155 | if id == "" { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 156 | logger.Errorw(ctx, "mac-address-not-set", log.Fields{"device-id": device.Id, "serial-number": sn}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 157 | return nil, errors.New("mac-address-not-set") |
| 158 | } |
David Bainbridge | d1afd66 | 2020-03-26 18:27:41 -0700 | [diff] [blame] | 159 | |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 160 | logger.Debugw(ctx, "logical-device-id", log.Fields{"logical-device-id": id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 161 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 162 | agent := newLogicalAgent(ctx, id, sn, device.Id, ldMgr, ldMgr.deviceMgr, ldMgr.dbPath, ldMgr.ldProxy, ldMgr.defaultTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 163 | ldMgr.addLogicalDeviceAgentToMap(agent) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 164 | |
| 165 | // Update the root device with the logical device Id reference |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 166 | if err := ldMgr.deviceMgr.setParentID(ctx, device, id); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 167 | logger.Errorw(ctx, "failed-setting-parent-id", log.Fields{"logical-device-id": id, "device-id": device.Id}) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 168 | return nil, err |
| 169 | } |
| 170 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 171 | go func() { |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 172 | //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 Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 174 | ldCtx := utils.WithSpanAndRPCMetadataFromContext(ctx) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame^] | 175 | err := agent.start(ldCtx, false, nil) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 176 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 177 | logger.Errorw(ctx, "unable-to-create-the-logical-device", log.Fields{"error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 178 | ldMgr.deleteLogicalDeviceAgent(id) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 179 | } |
| 180 | }() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 181 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 182 | logger.Debug(ctx, "creating-logical-device-ends") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 183 | return &id, nil |
| 184 | } |
| 185 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 186 | // 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 Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 189 | func (ldMgr *LogicalManager) stopManagingLogicalDeviceWithDeviceID(ctx context.Context, id string) string { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 190 | logger.Infow(ctx, "stop-managing-logical-device", log.Fields{"device-id": id}) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 191 | // Go over the list of logical device agents to find the one which has rootDeviceId as id |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 192 | var ldID = "" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 193 | ldMgr.logicalDeviceAgents.Range(func(key, value interface{}) bool { |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 194 | ldAgent := value.(*LogicalAgent) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 195 | if ldAgent.rootDeviceID == id { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 196 | logger.Infow(ctx, "stopping-logical-device-agent", log.Fields{"logical-device-id": key}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 197 | if err := ldAgent.stop(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 198 | logger.Errorw(ctx, "failed-to-stop-LDAgent", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 199 | return false |
| 200 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 201 | ldID = key.(string) |
| 202 | ldMgr.logicalDeviceAgents.Delete(ldID) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 203 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 204 | return true |
| 205 | }) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 206 | return ldID |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | //getLogicalDeviceFromModel retrieves the logical device data from the model. |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 210 | func (ldMgr *LogicalManager) getLogicalDeviceFromModel(ctx context.Context, lDeviceID string) (*voltha.LogicalDevice, error) { |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 211 | logicalDevice := &voltha.LogicalDevice{} |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 212 | if have, err := ldMgr.ldProxy.Get(ctx, lDeviceID, logicalDevice); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 213 | logger.Errorw(ctx, "failed-to-get-logical-devices-from-cluster-proxy", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 214 | return nil, err |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 215 | } else if !have { |
| 216 | return nil, status.Error(codes.NotFound, lDeviceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 217 | } |
Kent Hagerman | 4f355f5 | 2020-03-30 16:01:33 -0400 | [diff] [blame] | 218 | |
| 219 | return logicalDevice, nil |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 220 | } |
| 221 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 222 | // load loads a logical device manager in memory |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 223 | func (ldMgr *LogicalManager) load(ctx context.Context, lDeviceID string) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 224 | if lDeviceID == "" { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 225 | return nil |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 226 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 227 | // Add a lock to prevent two concurrent calls from loading the same device twice |
| 228 | ldMgr.logicalDevicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 229 | 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)} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 232 | ldMgr.logicalDevicesLoadingLock.Unlock() |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 233 | if _, err := ldMgr.getLogicalDeviceFromModel(ctx, lDeviceID); err == nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 234 | logger.Debugw(ctx, "loading-logical-device", log.Fields{"lDeviceId": lDeviceID}) |
| 235 | agent := newLogicalAgent(ctx, lDeviceID, "", "", ldMgr, ldMgr.deviceMgr, ldMgr.dbPath, ldMgr.ldProxy, ldMgr.defaultTimeout) |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame^] | 236 | if err := agent.start(ctx, true, nil); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 237 | return err |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 238 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 239 | ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 240 | } else { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 241 | logger.Debugw(ctx, "logical-device-not-in-model", log.Fields{"logical-device-id": lDeviceID}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 242 | } |
| 243 | // announce completion of task to any number of waiting channels |
| 244 | ldMgr.logicalDevicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 245 | if v, ok := ldMgr.logicalDeviceLoadingInProgress[lDeviceID]; ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 246 | for _, ch := range v { |
| 247 | close(ch) |
| 248 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 249 | delete(ldMgr.logicalDeviceLoadingInProgress, lDeviceID) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 250 | } |
| 251 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 252 | } else { |
| 253 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 254 | } |
| 255 | } else { |
| 256 | ch := make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 257 | ldMgr.logicalDeviceLoadingInProgress[lDeviceID] = append(ldMgr.logicalDeviceLoadingInProgress[lDeviceID], ch) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 258 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 259 | // Wait for the channel to be closed, implying the process loading this device is done. |
| 260 | <-ch |
| 261 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 262 | if _, exist := ldMgr.logicalDeviceAgents.Load(lDeviceID); exist { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 263 | return nil |
| 264 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 265 | return status.Errorf(codes.Aborted, "Error loading logical device %s", lDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 266 | } |
| 267 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 268 | func (ldMgr *LogicalManager) deleteLogicalDevice(ctx context.Context, device *voltha.Device) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 269 | logger.Debugw(ctx, "deleting-logical-device", log.Fields{"device-id": device.Id}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 270 | // Sanity check |
| 271 | if !device.Root { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 272 | return errors.New("device-not-root") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 273 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 274 | logDeviceID := device.ParentId |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 275 | if agent := ldMgr.getLogicalDeviceAgent(ctx, logDeviceID); agent != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 276 | // Stop the logical device agent |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 277 | if err := agent.stop(ctx); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 278 | logger.Errorw(ctx, "failed-to-stop-agent", log.Fields{"error": err}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 279 | return err |
| 280 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 281 | //Remove the logical device agent from the Map |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 282 | ldMgr.deleteLogicalDeviceAgent(logDeviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 285 | logger.Debug(ctx, "deleting-logical-device-ends") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 286 | return nil |
| 287 | } |
| 288 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 289 | func (ldMgr *LogicalManager) getLogicalDeviceID(ctx context.Context, device *voltha.Device) (*string, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 290 | // 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 Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 297 | // TODO: return (string, have) instead of *string |
| 298 | // also: If not root device, just return device.parentID instead of loading the parent device. |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 299 | if parentDevice := ldMgr.deviceMgr.getParentDevice(ctx, device); parentDevice != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 300 | return &parentDevice.ParentId, nil |
| 301 | } |
| 302 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 303 | } |
| 304 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 305 | func (ldMgr *LogicalManager) getLogicalDeviceIDFromDeviceID(ctx context.Context, deviceID string) (*string, error) { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 306 | // Get the device |
| 307 | var device *voltha.Device |
| 308 | var err error |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 309 | if device, err = ldMgr.deviceMgr.getDeviceReadOnly(ctx, deviceID); err != nil { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 310 | return nil, err |
| 311 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 312 | return ldMgr.getLogicalDeviceID(ctx, device) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 313 | } |
| 314 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 315 | // ListLogicalDeviceFlows returns the flows of logical device |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 316 | func (ldMgr *LogicalManager) ListLogicalDeviceFlows(ctx context.Context, id *voltha.ID) (*openflow_13.Flows, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 317 | ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDeviceFlows") |
| 318 | logger.Debugw(ctx, "list-logical-device-flows", log.Fields{"logical-device-id": id.Id}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 319 | agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id) |
| 320 | if agent == nil { |
| 321 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 322 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 323 | |
| 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 | } |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame^] | 330 | logger.Debugw(ctx, "list-logical-device-flows", log.Fields{"logical-device-id": id.Id, "num-flows": len(flows)}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 331 | return &openflow_13.Flows{Items: ret}, nil |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 332 | } |
| 333 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 334 | // ListLogicalDeviceFlowGroups returns logical device flow groups |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 335 | func (ldMgr *LogicalManager) ListLogicalDeviceFlowGroups(ctx context.Context, id *voltha.ID) (*openflow_13.FlowGroups, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 336 | ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDeviceFlowGroups") |
| 337 | logger.Debugw(ctx, "list-logical-device-flow-groups", log.Fields{"logical-device-id": id.Id}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 338 | agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id) |
| 339 | if agent == nil { |
| 340 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 341 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 342 | |
| 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 | } |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame^] | 349 | logger.Debugw(ctx, "list-logical-device-flow-groups", log.Fields{"logical-device-id": id.Id, "num-groups": len(groups)}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 350 | return &openflow_13.FlowGroups{Items: ret}, nil |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 351 | } |
| 352 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 353 | // ListLogicalDevicePorts returns logical device ports |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 354 | func (ldMgr *LogicalManager) ListLogicalDevicePorts(ctx context.Context, id *voltha.ID) (*voltha.LogicalPorts, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 355 | ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDevicePorts") |
| 356 | logger.Debugw(ctx, "list-logical-device-ports", log.Fields{"logical-device-id": id.Id}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 357 | agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id) |
| 358 | if agent == nil { |
| 359 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 360 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 361 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 362 | ports := agent.listLogicalDevicePorts(ctx) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 363 | ctr, ret := 0, make([]*voltha.LogicalPort, len(ports)) |
| 364 | for _, port := range ports { |
| 365 | ret[ctr] = port |
| 366 | ctr++ |
| 367 | } |
khenaidoo | 7585a96 | 2021-06-10 16:15:38 -0400 | [diff] [blame^] | 368 | logger.Debugw(ctx, "list-logical-device-ports", log.Fields{"logical-device-id": id.Id, "num-ports": len(ports)}) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 369 | return &voltha.LogicalPorts{Items: ret}, nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 372 | // GetLogicalDevicePort returns logical device port details |
| 373 | func (ldMgr *LogicalManager) GetLogicalDevicePort(ctx context.Context, lPortID *voltha.LogicalPortId) (*voltha.LogicalPort, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 374 | ctx = utils.WithRPCMetadataContext(ctx, "GetLogicalDevicePort") |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 375 | // 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) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 379 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 380 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 381 | for _, port := range agent.listLogicalDevicePorts(ctx) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 382 | if port.Id == lPortID.PortId { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 383 | return port, nil |
| 384 | } |
| 385 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 386 | return nil, status.Errorf(codes.NotFound, "%s-%s", lPortID.Id, lPortID.PortId) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 387 | } |
| 388 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 389 | // updateLogicalPort sets up a logical port on the logical device based on the device port |
| 390 | // information, if needed |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 391 | func (ldMgr *LogicalManager) updateLogicalPort(ctx context.Context, device *voltha.Device, devicePorts map[uint32]*voltha.Port, port *voltha.Port) error { |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 392 | ldID, err := ldMgr.getLogicalDeviceID(ctx, device) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 393 | if err != nil || *ldID == "" { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 394 | // 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 |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 397 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 398 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 399 | if err := agent.updateLogicalPort(ctx, device, devicePorts, port); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 400 | return err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | return nil |
| 404 | } |
| 405 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 406 | // deleteLogicalPort removes the logical port associated with a child device |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 407 | func (ldMgr *LogicalManager) deleteLogicalPorts(ctx context.Context, deviceID string) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 408 | logger.Debugw(ctx, "deleting-logical-ports", log.Fields{"device-id": deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 409 | // Get logical port |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 410 | ldID, err := ldMgr.getLogicalDeviceIDFromDeviceID(ctx, deviceID) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 411 | if err != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 412 | return err |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 413 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 414 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
| 415 | if err = agent.deleteLogicalPorts(ctx, deviceID); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 416 | logger.Warnw(ctx, "delete-logical-ports-failed", log.Fields{"logical-device-id": *ldID}) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 417 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 418 | } |
| 419 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 420 | logger.Debug(ctx, "deleting-logical-ports-ends") |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 421 | return nil |
| 422 | } |
| 423 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 424 | func (ldMgr *LogicalManager) setupUNILogicalPorts(ctx context.Context, childDevice *voltha.Device, childDevicePorts map[uint32]*voltha.Port) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 425 | logger.Debugw(ctx, "setup-uni-logical-ports", log.Fields{"child-device-id": childDevice.Id, "parent-device-id": childDevice.ParentId, "current-data": childDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 426 | // Sanity check |
| 427 | if childDevice.Root { |
| 428 | return errors.New("Device-root") |
| 429 | } |
| 430 | |
| 431 | // Get the logical device id parent device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 432 | parentID := childDevice.ParentId |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 433 | logDeviceID := ldMgr.deviceMgr.GetParentDeviceID(ctx, parentID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 434 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 435 | logger.Debugw(ctx, "setup-uni-logical-ports", log.Fields{"logical-device-id": logDeviceID, "parentId": parentID}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 436 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 437 | if parentID == "" || logDeviceID == "" { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 438 | return errors.New("device-in-invalid-state") |
khenaidoo | 5e677ae | 2019-02-28 17:26:29 -0500 | [diff] [blame] | 439 | } |
| 440 | |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 441 | if agent := ldMgr.getLogicalDeviceAgent(ctx, logDeviceID); agent != nil { |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 442 | if err := agent.setupUNILogicalPorts(ctx, childDevice, childDevicePorts); err != nil { |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 443 | return err |
| 444 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 445 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 446 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 447 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 448 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 449 | func (ldMgr *LogicalManager) deleteAllLogicalPorts(ctx context.Context, device *voltha.Device) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 450 | logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"device-id": device.Id}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 451 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 452 | var ldID *string |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 453 | var err error |
| 454 | //Get the logical device Id for this device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 455 | if ldID, err = ldMgr.getLogicalDeviceID(ctx, device); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 456 | logger.Warnw(ctx, "no-logical-device-found", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 457 | return err |
| 458 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 459 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 460 | if err := agent.deleteAllLogicalPorts(ctx); err != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 461 | return err |
| 462 | } |
| 463 | } |
| 464 | return nil |
| 465 | } |
Hardik Windlass | c704def | 2020-02-26 18:23:19 +0000 | [diff] [blame] | 466 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 467 | func (ldMgr *LogicalManager) updatePortState(ctx context.Context, deviceID string, portNo uint32, state voltha.OperStatus_Types) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 468 | logger.Debugw(ctx, "update-Port-state", log.Fields{"device-id": deviceID, "state": state, "port-no": portNo}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 469 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 470 | var ldID *string |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 471 | var err error |
| 472 | //Get the logical device Id for this device |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 473 | if ldID, err = ldMgr.getLogicalDeviceIDFromDeviceID(ctx, deviceID); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 474 | logger.Warnw(ctx, "no-logical-device-found", log.Fields{"device-id": deviceID, "error": err}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 475 | return err |
| 476 | } |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 477 | if agent := ldMgr.getLogicalDeviceAgent(ctx, *ldID); agent != nil { |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 478 | if err := agent.updatePortState(ctx, portNo, state); err != nil { |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 479 | return err |
| 480 | } |
| 481 | } |
| 482 | return nil |
| 483 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 484 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 485 | // UpdateLogicalDeviceFlowTable updates logical device flow table |
| 486 | func (ldMgr *LogicalManager) UpdateLogicalDeviceFlowTable(ctx context.Context, flow *openflow_13.FlowTableUpdate) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 487 | ctx = utils.WithRPCMetadataContext(ctx, "UpdateLogicalDeviceFlowTable") |
| 488 | logger.Debugw(ctx, "update-logical-device-flow-table", log.Fields{"logical-device-id": flow.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 489 | agent := ldMgr.getLogicalDeviceAgent(ctx, flow.Id) |
| 490 | if agent == nil { |
| 491 | return nil, status.Errorf(codes.NotFound, "%s", flow.Id) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 492 | } |
Maninder | f421da6 | 2020-12-04 11:44:58 +0530 | [diff] [blame] | 493 | return &empty.Empty{}, agent.updateFlowTable(ctx, flow) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 494 | } |
| 495 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 496 | // UpdateLogicalDeviceMeterTable - This function sends meter mod request to logical device manager and waits for response |
| 497 | func (ldMgr *LogicalManager) UpdateLogicalDeviceMeterTable(ctx context.Context, meter *openflow_13.MeterModUpdate) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 498 | ctx = utils.WithRPCMetadataContext(ctx, "UpdateLogicalDeviceMeterTable") |
| 499 | logger.Debugw(ctx, "update-logical-device-meter-table", log.Fields{"logical-device-id": meter.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 500 | agent := ldMgr.getLogicalDeviceAgent(ctx, meter.Id) |
| 501 | if agent == nil { |
| 502 | return nil, status.Errorf(codes.NotFound, "%s", meter.Id) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 503 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 504 | return &empty.Empty{}, agent.updateMeterTable(ctx, meter.MeterMod) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 505 | } |
| 506 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 507 | // ListLogicalDeviceMeters returns logical device meters |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 508 | func (ldMgr *LogicalManager) ListLogicalDeviceMeters(ctx context.Context, id *voltha.ID) (*openflow_13.Meters, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 509 | ctx = utils.WithRPCMetadataContext(ctx, "ListLogicalDeviceMeters") |
| 510 | logger.Debugw(ctx, "list-logical-device-meters", log.Fields{"logical-device-id": id.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 511 | agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id) |
| 512 | if agent == nil { |
| 513 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 514 | } |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 515 | 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 |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 524 | // UpdateLogicalDeviceFlowGroupTable updates logical device flow group table |
| 525 | func (ldMgr *LogicalManager) UpdateLogicalDeviceFlowGroupTable(ctx context.Context, flow *openflow_13.FlowGroupTableUpdate) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 526 | ctx = utils.WithRPCMetadataContext(ctx, "UpdateLogicalDeviceFlowGroupTable") |
| 527 | logger.Debugw(ctx, "update-group-table", log.Fields{"logical-device-id": flow.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 528 | agent := ldMgr.getLogicalDeviceAgent(ctx, flow.Id) |
| 529 | if agent == nil { |
| 530 | return nil, status.Errorf(codes.NotFound, "%s", flow.Id) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 531 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 532 | return &empty.Empty{}, agent.updateGroupTable(ctx, flow.GroupMod) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 533 | } |
| 534 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 535 | // EnableLogicalDevicePort enables logical device port |
| 536 | func (ldMgr *LogicalManager) EnableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 537 | ctx = utils.WithRPCMetadataContext(ctx, "EnableLogicalDevicePort") |
| 538 | logger.Debugw(ctx, "enable-logical-device-port", log.Fields{"logical-device-id": id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 539 | agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id) |
| 540 | if agent == nil { |
| 541 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 542 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 543 | 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 Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // DisableLogicalDevicePort disables logical device port |
| 551 | func (ldMgr *LogicalManager) DisableLogicalDevicePort(ctx context.Context, id *voltha.LogicalPortId) (*empty.Empty, error) { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 552 | ctx = utils.WithRPCMetadataContext(ctx, "DisableLogicalDevicePort") |
| 553 | logger.Debugw(ctx, "disable-logical-device-port", log.Fields{"logical-device-id": id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 554 | agent := ldMgr.getLogicalDeviceAgent(ctx, id.Id) |
| 555 | if agent == nil { |
| 556 | return nil, status.Errorf(codes.NotFound, "%s", id.Id) |
| 557 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 558 | 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)) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 563 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 564 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 565 | func (ldMgr *LogicalManager) packetIn(ctx context.Context, logicalDeviceID string, port uint32, transactionID string, packet []byte) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 566 | logger.Debugw(ctx, "packet-in", log.Fields{"logical-device-id": logicalDeviceID, "port": port}) |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 567 | if agent := ldMgr.getLogicalDeviceAgent(ctx, logicalDeviceID); agent != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 568 | agent.packetIn(ctx, port, transactionID, packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 569 | } else { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 570 | logger.Error(ctx, "logical-device-not-exist", log.Fields{"logical-device-id": logicalDeviceID}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 571 | } |
| 572 | return nil |
| 573 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 574 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 575 | // StreamPacketsOut sends packets to adapter |
| 576 | func (ldMgr *LogicalManager) StreamPacketsOut(packets voltha.VolthaService_StreamPacketsOutServer) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 577 | ctx := context.Background() |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 578 | logger.Debugw(ctx, "stream-packets-out-request", log.Fields{"packets": packets}) |
| 579 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 580 | loop: |
| 581 | for { |
| 582 | select { |
| 583 | case <-packets.Context().Done(): |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 584 | logger.Infow(ctx, "stream-packets-out-context-done", log.Fields{"packets": packets, "error": packets.Context().Err()}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 585 | break loop |
| 586 | default: |
| 587 | } |
| 588 | |
| 589 | packet, err := packets.Recv() |
| 590 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 591 | pktCtx := utils.WithRPCMetadataContext(packets.Context(), "StreamPacketsOut") |
| 592 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 593 | if err == io.EOF { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 594 | logger.Debugw(ctx, "received-eof", log.Fields{"packets": packets}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 595 | break loop |
| 596 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 597 | if err != nil { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 598 | logger.Errorw(ctx, "failed-to-receive-packet-out", log.Fields{"error": err}) |
Andrea Campanella | ff2d73a | 2021-03-04 14:47:17 +0100 | [diff] [blame] | 599 | // we do not have the resource Id here due to error in the packet, setting to empty |
Himani Chawla | 606a4f0 | 2021-03-23 19:45:58 +0530 | [diff] [blame] | 600 | ldMgr.SendRPCEvent(pktCtx, "", err.Error(), nil, |
| 601 | "RPC_ERROR_RAISE_EVENT", voltha.EventCategory_COMMUNICATION, nil, time.Now().Unix()) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 602 | continue |
| 603 | } |
| 604 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 605 | if agent := ldMgr.getLogicalDeviceAgent(pktCtx, packet.Id); agent != nil { |
| 606 | agent.packetOut(pktCtx, packet.PacketOut) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 607 | } else { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 608 | logger.Errorf(ctx, "no-logical-device-agent-present", log.Fields{"logical-device-id": packet.Id}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 609 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 610 | } |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 611 | |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 612 | logger.Debugw(ctx, "stream-packets-out-request-done", log.Fields{"packets": packets}) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 613 | return nil |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 614 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 615 | |
| 616 | func (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 Gunyel | b0343bf | 2021-05-11 14:14:26 -0700 | [diff] [blame] | 618 | ldMgr.Manager.Agent.GetAndSendRPCEvent(ctx, resourceID, desc, context, id, |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 619 | category, subCategory, raisedTs) |
| 620 | } |