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 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 22 | "strings" |
| 23 | "sync" |
| 24 | |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/db/model" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v2/pkg/kafka" |
| 27 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
| 28 | "github.com/opencord/voltha-lib-go/v2/pkg/probe" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 29 | "github.com/opencord/voltha-protos/v2/go/openflow_13" |
| 30 | "github.com/opencord/voltha-protos/v2/go/voltha" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 31 | "google.golang.org/grpc/codes" |
| 32 | "google.golang.org/grpc/status" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 35 | // LogicalDeviceManager represent logical device manager attributes |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 36 | type LogicalDeviceManager struct { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 37 | logicalDeviceAgents sync.Map |
| 38 | core *Core |
| 39 | deviceMgr *DeviceManager |
| 40 | grpcNbiHdlr *APIHandler |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 41 | kafkaICProxy *kafka.InterContainerProxy |
| 42 | clusterDataProxy *model.Proxy |
| 43 | exitChannel chan int |
| 44 | defaultTimeout int64 |
| 45 | logicalDevicesLoadingLock sync.RWMutex |
| 46 | logicalDeviceLoadingInProgress map[string][]chan int |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 47 | } |
| 48 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 49 | func newLogicalDeviceManager(core *Core, deviceMgr *DeviceManager, kafkaICProxy *kafka.InterContainerProxy, cdProxy *model.Proxy, timeout int64) *LogicalDeviceManager { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 50 | var logicalDeviceMgr LogicalDeviceManager |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 51 | logicalDeviceMgr.core = core |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | logicalDeviceMgr.exitChannel = make(chan int, 1) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 53 | logicalDeviceMgr.deviceMgr = deviceMgr |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 54 | logicalDeviceMgr.kafkaICProxy = kafkaICProxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 55 | logicalDeviceMgr.clusterDataProxy = cdProxy |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 56 | logicalDeviceMgr.defaultTimeout = timeout |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 57 | logicalDeviceMgr.logicalDevicesLoadingLock = sync.RWMutex{} |
| 58 | logicalDeviceMgr.logicalDeviceLoadingInProgress = make(map[string][]chan int) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | return &logicalDeviceMgr |
| 60 | } |
| 61 | |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 62 | func (ldMgr *LogicalDeviceManager) setGrpcNbiHandler(grpcNbiHandler *APIHandler) { |
| 63 | ldMgr.grpcNbiHdlr = grpcNbiHandler |
| 64 | } |
| 65 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 66 | func (ldMgr *LogicalDeviceManager) start(ctx context.Context) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 67 | log.Info("starting-logical-device-manager") |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 68 | probe.UpdateStatusFromContext(ctx, "logical-device-manager", probe.ServiceStatusRunning) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 69 | log.Info("logical-device-manager-started") |
| 70 | } |
| 71 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 72 | func (ldMgr *LogicalDeviceManager) stop(ctx context.Context) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 73 | log.Info("stopping-logical-device-manager") |
| 74 | ldMgr.exitChannel <- 1 |
David K. Bainbridge | b4a9ab0 | 2019-09-20 15:12:16 -0700 | [diff] [blame] | 75 | probe.UpdateStatusFromContext(ctx, "logical-device-manager", probe.ServiceStatusStopped) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 76 | log.Info("logical-device-manager-stopped") |
| 77 | } |
| 78 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 79 | func sendAPIResponse(ctx context.Context, ch chan interface{}, result interface{}) { |
| 80 | if ctx.Err() == nil { |
| 81 | // Returned response only of the ctx has not been cancelled/timeout/etc |
| 82 | // Channel is automatically closed when a context is Done |
| 83 | ch <- result |
| 84 | log.Debugw("sendResponse", log.Fields{"result": result}) |
| 85 | } else { |
| 86 | // Should the transaction be reverted back? |
| 87 | log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
| 88 | } |
| 89 | } |
| 90 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 91 | func (ldMgr *LogicalDeviceManager) addLogicalDeviceAgentToMap(agent *LogicalDeviceAgent) { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 92 | if _, exist := ldMgr.logicalDeviceAgents.Load(agent.logicalDeviceID); !exist { |
| 93 | ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 97 | // getLogicalDeviceAgent returns the logical device agent. If the device is not in memory then the device will |
| 98 | // be loaded from dB and a logical device agent created to managed it. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 99 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceAgent(logicalDeviceID string) *LogicalDeviceAgent { |
| 100 | agent, ok := ldMgr.logicalDeviceAgents.Load(logicalDeviceID) |
| 101 | if ok { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 102 | return agent.(*LogicalDeviceAgent) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 103 | } |
| 104 | // Try to load into memory - loading will also create the logical device agent |
| 105 | if err := ldMgr.load(logicalDeviceID); err == nil { |
| 106 | if agent, ok = ldMgr.logicalDeviceAgents.Load(logicalDeviceID); ok { |
| 107 | return agent.(*LogicalDeviceAgent) |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 108 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 109 | } |
| 110 | return nil |
| 111 | } |
| 112 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 113 | func (ldMgr *LogicalDeviceManager) deleteLogicalDeviceAgent(logicalDeviceID string) { |
| 114 | ldMgr.logicalDeviceAgents.Delete(logicalDeviceID) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 115 | } |
| 116 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 117 | // GetLogicalDevice provides a cloned most up to date logical device. If device is not in memory |
| 118 | // it will be fetched from the dB |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 119 | func (ldMgr *LogicalDeviceManager) getLogicalDevice(id string) (*voltha.LogicalDevice, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 120 | log.Debugw("getlogicalDevice", log.Fields{"logicaldeviceid": id}) |
| 121 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 122 | return agent.GetLogicalDevice(), nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 123 | } |
| 124 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 125 | } |
| 126 | |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 127 | func (ldMgr *LogicalDeviceManager) listManagedLogicalDevices() (*voltha.LogicalDevices, error) { |
| 128 | log.Debug("listManagedLogicalDevices") |
| 129 | result := &voltha.LogicalDevices{} |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 130 | ldMgr.logicalDeviceAgents.Range(func(key, value interface{}) bool { |
| 131 | agent := value.(*LogicalDeviceAgent) |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 132 | if ld := agent.GetLogicalDevice(); ld != nil { |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 133 | result.Items = append(result.Items, ld) |
| 134 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 135 | return true |
| 136 | }) |
| 137 | |
khenaidoo | 43aa6bd | 2019-05-29 13:35:13 -0400 | [diff] [blame] | 138 | return result, nil |
| 139 | } |
| 140 | |
khenaidoo | ba6b6c4 | 2019-08-02 09:11:56 -0400 | [diff] [blame] | 141 | //listLogicalDevices returns the list of all logical devices |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 142 | func (ldMgr *LogicalDeviceManager) listLogicalDevices() (*voltha.LogicalDevices, error) { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 143 | log.Debug("ListAllLogicalDevices") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 144 | result := &voltha.LogicalDevices{} |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 145 | logicalDevices, err := ldMgr.clusterDataProxy.List(context.Background(), "/logical_devices", 0, true, "") |
| 146 | if err != nil { |
| 147 | log.Errorw("failed-to-list-logical-devices-from-cluster-proxy", log.Fields{"error": err}) |
| 148 | return nil, err |
| 149 | } |
| 150 | if logicalDevices != nil { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 151 | for _, logicalDevice := range logicalDevices.([]interface{}) { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 152 | result.Items = append(result.Items, logicalDevice.(*voltha.LogicalDevice)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | return result, nil |
| 156 | } |
| 157 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 158 | func (ldMgr *LogicalDeviceManager) createLogicalDevice(ctx context.Context, device *voltha.Device) (*string, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 159 | log.Debugw("creating-logical-device", log.Fields{"deviceId": device.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 160 | // Sanity check |
| 161 | if !device.Root { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 162 | return nil, errors.New("device-not-root") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // Create a logical device agent - the logical device Id is based on the mac address of the device |
| 166 | // For now use the serial number - it may contain any combination of alphabetic characters and numbers, |
| 167 | // with length varying from eight characters to a maximum of 14 characters. Mac Address is part of oneof |
| 168 | // in the Device model. May need to be moved out. |
| 169 | macAddress := device.MacAddress |
| 170 | id := strings.Replace(macAddress, ":", "", -1) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 171 | if id == "" { |
| 172 | log.Errorw("mac-address-not-set", log.Fields{"deviceId": device.Id}) |
| 173 | return nil, errors.New("mac-address-not-set") |
| 174 | } |
| 175 | log.Debugw("logical-device-id", log.Fields{"logicaldeviceId": id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 176 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 177 | agent := newLogicalDeviceAgent(id, device.Id, ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy, ldMgr.defaultTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 178 | ldMgr.addLogicalDeviceAgentToMap(agent) |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 179 | |
| 180 | // Update the root device with the logical device Id reference |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 181 | if err := ldMgr.deviceMgr.setParentID(device, id); err != nil { |
khenaidoo | ad06fd7 | 2019-10-28 12:26:05 -0400 | [diff] [blame] | 182 | log.Errorw("failed-setting-parent-id", log.Fields{"logicalDeviceId": id, "deviceId": device.Id}) |
| 183 | return nil, err |
| 184 | } |
| 185 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 186 | go func() { |
| 187 | err := agent.start(ctx, false) |
| 188 | if err != nil { |
| 189 | log.Errorw("unable-to-create-the-logical-device", log.Fields{"error": err}) |
| 190 | } |
| 191 | }() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 192 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 193 | log.Debug("creating-logical-device-ends") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 194 | return &id, nil |
| 195 | } |
| 196 | |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 197 | // stopManagingLogicalDeviceWithDeviceId stops the management of the logical device. This implies removal of any |
| 198 | // reference of this logical device in cache. The device Id is passed as param because the logical device may already |
| 199 | // have been removed from the model. This function returns the logical device Id if found |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 200 | func (ldMgr *LogicalDeviceManager) stopManagingLogicalDeviceWithDeviceID(id string) string { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 201 | log.Infow("stop-managing-logical-device", log.Fields{"deviceId": id}) |
| 202 | // 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] | 203 | var ldID = "" |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 204 | ldMgr.logicalDeviceAgents.Range(func(key, value interface{}) bool { |
| 205 | ldAgent := value.(*LogicalDeviceAgent) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 206 | if ldAgent.rootDeviceID == id { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 207 | log.Infow("stopping-logical-device-agent", log.Fields{"lDeviceId": key}) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 208 | if err := ldAgent.stop(context.TODO()); err != nil { |
| 209 | log.Errorw("failed-to-stop-LDAgent", log.Fields{"error": err}) |
| 210 | return false |
| 211 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 212 | ldID = key.(string) |
| 213 | ldMgr.logicalDeviceAgents.Delete(ldID) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 214 | } |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 215 | return true |
| 216 | }) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 217 | return ldID |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | //getLogicalDeviceFromModel retrieves the logical device data from the model. |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 221 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceFromModel(lDeviceID string) (*voltha.LogicalDevice, error) { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 222 | logicalDevice, err := ldMgr.clusterDataProxy.Get(context.Background(), "/logical_devices/"+lDeviceID, 0, false, "") |
| 223 | if err != nil { |
| 224 | log.Errorw("failed-to-get-logical-devices-from-cluster-proxy", log.Fields{"error": err}) |
| 225 | return nil, err |
| 226 | } |
| 227 | if logicalDevice != nil { |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 228 | if lDevice, ok := logicalDevice.(*voltha.LogicalDevice); ok { |
| 229 | return lDevice, nil |
| 230 | } |
| 231 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 232 | return nil, status.Error(codes.NotFound, lDeviceID) |
khenaidoo | 6d62c00 | 2019-05-15 21:57:03 -0400 | [diff] [blame] | 233 | } |
| 234 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 235 | // load loads a logical device manager in memory |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 236 | func (ldMgr *LogicalDeviceManager) load(lDeviceID string) error { |
| 237 | if lDeviceID == "" { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 238 | return nil |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 239 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 240 | // Add a lock to prevent two concurrent calls from loading the same device twice |
| 241 | ldMgr.logicalDevicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 242 | if _, exist := ldMgr.logicalDeviceLoadingInProgress[lDeviceID]; !exist { |
| 243 | if ldAgent, _ := ldMgr.logicalDeviceAgents.Load(lDeviceID); ldAgent == nil { |
| 244 | ldMgr.logicalDeviceLoadingInProgress[lDeviceID] = []chan int{make(chan int, 1)} |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 245 | ldMgr.logicalDevicesLoadingLock.Unlock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 246 | if _, err := ldMgr.getLogicalDeviceFromModel(lDeviceID); err == nil { |
| 247 | log.Debugw("loading-logical-device", log.Fields{"lDeviceId": lDeviceID}) |
| 248 | agent := newLogicalDeviceAgent(lDeviceID, "", ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy, ldMgr.defaultTimeout) |
| 249 | if err := agent.start(context.TODO(), true); err != nil { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 250 | if err := agent.stop(context.TODO()); err != nil { |
| 251 | log.Errorw("failed-to-stop-agent", log.Fields{"error": err}) |
| 252 | return err |
| 253 | } |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 254 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 255 | ldMgr.logicalDeviceAgents.Store(agent.logicalDeviceID, agent) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 256 | } |
| 257 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 258 | log.Debugw("logicalDevice not in model", log.Fields{"lDeviceId": lDeviceID}) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 259 | } |
| 260 | // announce completion of task to any number of waiting channels |
| 261 | ldMgr.logicalDevicesLoadingLock.Lock() |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 262 | if v, ok := ldMgr.logicalDeviceLoadingInProgress[lDeviceID]; ok { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 263 | for _, ch := range v { |
| 264 | close(ch) |
| 265 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 266 | delete(ldMgr.logicalDeviceLoadingInProgress, lDeviceID) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 267 | } |
| 268 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 269 | } else { |
| 270 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 271 | } |
| 272 | } else { |
| 273 | ch := make(chan int, 1) |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 274 | ldMgr.logicalDeviceLoadingInProgress[lDeviceID] = append(ldMgr.logicalDeviceLoadingInProgress[lDeviceID], ch) |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 275 | ldMgr.logicalDevicesLoadingLock.Unlock() |
| 276 | // Wait for the channel to be closed, implying the process loading this device is done. |
| 277 | <-ch |
| 278 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 279 | if _, exist := ldMgr.logicalDeviceAgents.Load(lDeviceID); exist { |
khenaidoo | 4c9e559 | 2019-09-09 16:20:41 -0400 | [diff] [blame] | 280 | return nil |
| 281 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 282 | return status.Errorf(codes.Aborted, "Error loading logical device %s", lDeviceID) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 283 | } |
| 284 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 285 | func (ldMgr *LogicalDeviceManager) deleteLogicalDevice(ctx context.Context, device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 286 | log.Debugw("deleting-logical-device", log.Fields{"deviceId": device.Id}) |
| 287 | // Sanity check |
| 288 | if !device.Root { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 289 | return errors.New("device-not-root") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 290 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 291 | logDeviceID := device.ParentId |
| 292 | if agent := ldMgr.getLogicalDeviceAgent(logDeviceID); agent != nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 293 | // Stop the logical device agent |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 294 | if err := agent.stop(ctx); err != nil { |
| 295 | log.Errorw("failed-to-stop-agent", log.Fields{"error": err}) |
| 296 | return err |
| 297 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 298 | //Remove the logical device agent from the Map |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 299 | ldMgr.deleteLogicalDeviceAgent(logDeviceID) |
| 300 | err := ldMgr.core.deviceOwnership.AbandonDevice(logDeviceID) |
| 301 | if err != nil { |
| 302 | log.Errorw("unable-to-abandon-the-device", log.Fields{"error": err}) |
| 303 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | log.Debug("deleting-logical-device-ends") |
| 307 | return nil |
| 308 | } |
| 309 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 310 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceID(device *voltha.Device) (*string, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 311 | // Device can either be a parent or a child device |
| 312 | if device.Root { |
| 313 | // Parent device. The ID of a parent device is the logical device ID |
| 314 | return &device.ParentId, nil |
| 315 | } |
| 316 | // Device is child device |
| 317 | // retrieve parent device using child device ID |
| 318 | if parentDevice := ldMgr.deviceMgr.getParentDevice(device); parentDevice != nil { |
| 319 | return &parentDevice.ParentId, nil |
| 320 | } |
| 321 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 322 | } |
| 323 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 324 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceIDFromDeviceID(deviceID string) (*string, error) { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 325 | // Get the device |
| 326 | var device *voltha.Device |
| 327 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 328 | if device, err = ldMgr.deviceMgr.GetDevice(deviceID); err != nil { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 329 | return nil, err |
| 330 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 331 | return ldMgr.getLogicalDeviceID(device) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 332 | } |
| 333 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 334 | func (ldMgr *LogicalDeviceManager) getLogicalPortID(device *voltha.Device) (*voltha.LogicalPortId, error) { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 335 | // Get the logical device where this device is attached |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 336 | var lDeviceID *string |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 337 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 338 | if lDeviceID, err = ldMgr.getLogicalDeviceID(device); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 339 | return nil, err |
| 340 | } |
| 341 | var lDevice *voltha.LogicalDevice |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 342 | if lDevice, err = ldMgr.getLogicalDevice(*lDeviceID); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 343 | return nil, err |
| 344 | } |
| 345 | // Go over list of ports |
| 346 | for _, port := range lDevice.Ports { |
| 347 | if port.DeviceId == device.Id { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 348 | return &voltha.LogicalPortId{Id: *lDeviceID, PortId: port.Id}, nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 352 | } |
| 353 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 354 | // ListLogicalDeviceFlows returns the flows of logical device |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 355 | func (ldMgr *LogicalDeviceManager) ListLogicalDeviceFlows(ctx context.Context, id string) (*openflow_13.Flows, error) { |
| 356 | log.Debugw("ListLogicalDeviceFlows", log.Fields{"logicaldeviceid": id}) |
| 357 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 358 | return agent.ListLogicalDeviceFlows(), nil |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 359 | } |
| 360 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 361 | } |
| 362 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 363 | // ListLogicalDeviceFlowGroups returns logical device flow groups |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 364 | func (ldMgr *LogicalDeviceManager) ListLogicalDeviceFlowGroups(ctx context.Context, id string) (*openflow_13.FlowGroups, error) { |
| 365 | log.Debugw("ListLogicalDeviceFlowGroups", log.Fields{"logicaldeviceid": id}) |
| 366 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 367 | return agent.ListLogicalDeviceFlowGroups(), nil |
khenaidoo | dd23717 | 2019-05-27 16:37:17 -0400 | [diff] [blame] | 368 | } |
| 369 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 370 | } |
| 371 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 372 | // ListLogicalDevicePorts returns logical device ports |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 373 | func (ldMgr *LogicalDeviceManager) ListLogicalDevicePorts(ctx context.Context, id string) (*voltha.LogicalPorts, error) { |
| 374 | log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
| 375 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 376 | return agent.ListLogicalDevicePorts(), nil |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 377 | } |
| 378 | return nil, status.Errorf(codes.NotFound, "%s", id) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 379 | } |
| 380 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 381 | func (ldMgr *LogicalDeviceManager) getLogicalPort(lPortID *voltha.LogicalPortId) (*voltha.LogicalPort, error) { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 382 | // Get the logical device where this device is attached |
| 383 | var err error |
| 384 | var lDevice *voltha.LogicalDevice |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 385 | if lDevice, err = ldMgr.getLogicalDevice(lPortID.Id); err != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 386 | return nil, err |
| 387 | } |
| 388 | // Go over list of ports |
| 389 | for _, port := range lDevice.Ports { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 390 | if port.Id == lPortID.PortId { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 391 | return port, nil |
| 392 | } |
| 393 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 394 | return nil, status.Errorf(codes.NotFound, "%s-%s", lPortID.Id, lPortID.PortId) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 395 | } |
| 396 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 397 | // updateLogicalPort sets up a logical port on the logical device based on the device port |
| 398 | // information, if needed |
| 399 | func (ldMgr *LogicalDeviceManager) updateLogicalPort(device *voltha.Device, port *voltha.Port) error { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 400 | ldID, err := ldMgr.getLogicalDeviceID(device) |
| 401 | if err != nil || *ldID == "" { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 402 | // This is not an error as the logical device may not have been created at this time. In such a case, |
| 403 | // the ports will be created when the logical device is ready. |
| 404 | return nil |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 405 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 406 | if agent := ldMgr.getLogicalDeviceAgent(*ldID); agent != nil { |
| 407 | if err := agent.updateLogicalPort(device, port); err != nil { |
| 408 | return err |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | return nil |
| 412 | } |
| 413 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 414 | // deleteLogicalPort removes the logical port associated with a device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 415 | func (ldMgr *LogicalDeviceManager) deleteLogicalPort(ctx context.Context, lPortID *voltha.LogicalPortId) error { |
| 416 | log.Debugw("deleting-logical-port", log.Fields{"LDeviceId": lPortID.Id}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 417 | // Get logical port |
| 418 | var logicalPort *voltha.LogicalPort |
| 419 | var err error |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 420 | if logicalPort, err = ldMgr.getLogicalPort(lPortID); err != nil { |
| 421 | log.Debugw("no-logical-device-port-present", log.Fields{"logicalPortId": lPortID.PortId}) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 422 | return err |
| 423 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 424 | // Sanity check |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 425 | if logicalPort.RootPort { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 426 | return errors.New("device-root") |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 427 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 428 | if agent := ldMgr.getLogicalDeviceAgent(lPortID.Id); agent != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 429 | if err := agent.deleteLogicalPort(logicalPort); err != nil { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 430 | log.Warnw("deleting-logicalport-failed", log.Fields{"LDeviceId": lPortID.Id, "error": err}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 431 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | log.Debug("deleting-logical-port-ends") |
| 435 | return nil |
| 436 | } |
| 437 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 438 | // deleteLogicalPort removes the logical port associated with a child device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 439 | func (ldMgr *LogicalDeviceManager) deleteLogicalPorts(deviceID string) error { |
| 440 | log.Debugw("deleting-logical-ports", log.Fields{"deviceId": deviceID}) |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 441 | // Get logical port |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 442 | ldID, err := ldMgr.getLogicalDeviceIDFromDeviceID(deviceID) |
| 443 | if err != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 444 | return err |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 445 | } |
| 446 | if agent := ldMgr.getLogicalDeviceAgent(*ldID); agent != nil { |
| 447 | if err = agent.deleteLogicalPorts(deviceID); err != nil { |
| 448 | log.Warnw("deleteLogicalPorts-failed", log.Fields{"ldeviceId": *ldID}) |
| 449 | return err |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | log.Debug("deleting-logical-port-ends") |
| 453 | return nil |
| 454 | } |
| 455 | |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 456 | func (ldMgr *LogicalDeviceManager) setupUNILogicalPorts(ctx context.Context, childDevice *voltha.Device) error { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 457 | log.Debugw("setupUNILogicalPorts", log.Fields{"childDeviceId": childDevice.Id, "parentDeviceId": childDevice.ParentId, "current-data": childDevice}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 458 | // Sanity check |
| 459 | if childDevice.Root { |
| 460 | return errors.New("Device-root") |
| 461 | } |
| 462 | |
| 463 | // Get the logical device id parent device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 464 | parentID := childDevice.ParentId |
| 465 | logDeviceID := ldMgr.deviceMgr.GetParentDeviceID(parentID) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 466 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 467 | log.Debugw("setupUNILogicalPorts", log.Fields{"logDeviceId": logDeviceID, "parentId": parentID}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 468 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 469 | if parentID == "" || logDeviceID == "" { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 470 | return errors.New("device-in-invalid-state") |
khenaidoo | 5e677ae | 2019-02-28 17:26:29 -0500 | [diff] [blame] | 471 | } |
| 472 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 473 | if agent := ldMgr.getLogicalDeviceAgent(logDeviceID); agent != nil { |
khenaidoo | fc1314d | 2019-03-14 09:34:21 -0400 | [diff] [blame] | 474 | if err := agent.setupUNILogicalPorts(ctx, childDevice); err != nil { |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 475 | return err |
| 476 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 477 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 478 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 479 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 480 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 481 | func (ldMgr *LogicalDeviceManager) deleteAllLogicalPorts(device *voltha.Device) error { |
| 482 | log.Debugw("deleteAllLogicalPorts", log.Fields{"deviceId": device.Id}) |
| 483 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 484 | var ldID *string |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 485 | var err error |
| 486 | //Get the logical device Id for this device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 487 | if ldID, err = ldMgr.getLogicalDeviceID(device); err != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 488 | log.Warnw("no-logical-device-found", log.Fields{"deviceId": device.Id, "error": err}) |
| 489 | return err |
| 490 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 491 | if agent := ldMgr.getLogicalDeviceAgent(*ldID); agent != nil { |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 492 | if err := agent.deleteAllLogicalPorts(device); err != nil { |
| 493 | return err |
| 494 | } |
| 495 | } |
| 496 | return nil |
| 497 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 498 | func (ldMgr *LogicalDeviceManager) updatePortState(deviceID string, portNo uint32, state voltha.OperStatus_OperStatus) error { |
| 499 | log.Debugw("updatePortState", log.Fields{"deviceId": deviceID, "state": state, "portNo": portNo}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 500 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 501 | var ldID *string |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 502 | var err error |
| 503 | //Get the logical device Id for this device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 504 | if ldID, err = ldMgr.getLogicalDeviceIDFromDeviceID(deviceID); err != nil { |
| 505 | log.Warnw("no-logical-device-found", log.Fields{"deviceId": deviceID, "error": err}) |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 506 | return err |
| 507 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 508 | if agent := ldMgr.getLogicalDeviceAgent(*ldID); agent != nil { |
| 509 | if err := agent.updatePortState(deviceID, portNo, state); err != nil { |
khenaidoo | 171b98e | 2019-10-31 11:48:15 -0400 | [diff] [blame] | 510 | return err |
| 511 | } |
| 512 | } |
| 513 | return nil |
| 514 | } |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 515 | |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 516 | func (ldMgr *LogicalDeviceManager) updatePortsState(device *voltha.Device, state voltha.AdminState_AdminState) error { |
Stephane Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 517 | log.Debugw("updatePortsState", log.Fields{"deviceId": device.Id, "state": state, "current-data": device}) |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 518 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 519 | var ldID *string |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 520 | var err error |
| 521 | //Get the logical device Id for this device |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 522 | if ldID, err = ldMgr.getLogicalDeviceID(device); err != nil { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 523 | log.Warnw("no-logical-device-found", log.Fields{"deviceId": device.Id, "error": err}) |
| 524 | return err |
| 525 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 526 | if agent := ldMgr.getLogicalDeviceAgent(*ldID); agent != nil { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 527 | if err := agent.updatePortsState(device, state); err != nil { |
| 528 | return err |
| 529 | } |
| 530 | } |
| 531 | return nil |
| 532 | } |
| 533 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 534 | func (ldMgr *LogicalDeviceManager) updateFlowTable(ctx context.Context, id string, flow *openflow_13.OfpFlowMod, ch chan interface{}) { |
| 535 | log.Debugw("updateFlowTable", log.Fields{"logicalDeviceId": id}) |
| 536 | var res interface{} |
| 537 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 538 | res = agent.updateFlowTable(ctx, flow) |
| 539 | log.Debugw("updateFlowTable-result", log.Fields{"result": res}) |
| 540 | } else { |
| 541 | res = status.Errorf(codes.NotFound, "%s", id) |
| 542 | } |
| 543 | sendAPIResponse(ctx, ch, res) |
| 544 | } |
| 545 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 546 | func (ldMgr *LogicalDeviceManager) updateMeterTable(ctx context.Context, id string, meter *openflow_13.OfpMeterMod, ch chan interface{}) { |
| 547 | log.Debugw("updateMeterTable", log.Fields{"logicalDeviceId": id}) |
| 548 | var res interface{} |
| 549 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 550 | res = agent.updateMeterTable(ctx, meter) |
| 551 | log.Debugw("updateMeterTable-result", log.Fields{"result": res}) |
| 552 | } else { |
| 553 | res = status.Errorf(codes.NotFound, "%s", id) |
| 554 | } |
| 555 | sendAPIResponse(ctx, ch, res) |
| 556 | } |
| 557 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 558 | // ListLogicalDeviceMeters returns logical device meters |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 559 | func (ldMgr *LogicalDeviceManager) ListLogicalDeviceMeters(ctx context.Context, id string) (*openflow_13.Meters, error) { |
| 560 | log.Debugw("ListLogicalDeviceMeters", log.Fields{"logicalDeviceId": id}) |
| 561 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 562 | return agent.ListLogicalDeviceMeters(), nil |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 563 | } |
| 564 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 565 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 566 | func (ldMgr *LogicalDeviceManager) updateGroupTable(ctx context.Context, id string, groupMod *openflow_13.OfpGroupMod, ch chan interface{}) { |
| 567 | log.Debugw("updateGroupTable", log.Fields{"logicalDeviceId": id}) |
| 568 | var res interface{} |
| 569 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 570 | res = agent.updateGroupTable(ctx, groupMod) |
| 571 | log.Debugw("updateGroupTable-result", log.Fields{"result": res}) |
| 572 | } else { |
| 573 | res = status.Errorf(codes.NotFound, "%s", id) |
| 574 | } |
| 575 | sendAPIResponse(ctx, ch, res) |
| 576 | } |
| 577 | |
| 578 | func (ldMgr *LogicalDeviceManager) enableLogicalPort(ctx context.Context, id *voltha.LogicalPortId, ch chan interface{}) { |
| 579 | log.Debugw("enableLogicalPort", log.Fields{"logicalDeviceId": id}) |
| 580 | var res interface{} |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 581 | if agent := ldMgr.getLogicalDeviceAgent(id.Id); agent != nil { |
Girish Kumar | 0d4597e | 2019-10-24 18:13:14 +0000 | [diff] [blame] | 582 | res = agent.enableLogicalPort(id.PortId) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 583 | log.Debugw("enableLogicalPort-result", log.Fields{"result": res}) |
| 584 | } else { |
| 585 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 586 | } |
| 587 | sendAPIResponse(ctx, ch, res) |
| 588 | } |
| 589 | |
| 590 | func (ldMgr *LogicalDeviceManager) disableLogicalPort(ctx context.Context, id *voltha.LogicalPortId, ch chan interface{}) { |
| 591 | log.Debugw("disableLogicalPort", log.Fields{"logicalDeviceId": id}) |
| 592 | var res interface{} |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 593 | if agent := ldMgr.getLogicalDeviceAgent(id.Id); agent != nil { |
Girish Kumar | 0d4597e | 2019-10-24 18:13:14 +0000 | [diff] [blame] | 594 | res = agent.disableLogicalPort(id.PortId) |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 595 | log.Debugw("disableLogicalPort-result", log.Fields{"result": res}) |
| 596 | } else { |
| 597 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 598 | } |
| 599 | sendAPIResponse(ctx, ch, res) |
| 600 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 601 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 602 | func (ldMgr *LogicalDeviceManager) packetIn(logicalDeviceID string, port uint32, transactionID string, packet []byte) error { |
| 603 | log.Debugw("packetIn", log.Fields{"logicalDeviceId": logicalDeviceID, "port": port}) |
| 604 | if agent := ldMgr.getLogicalDeviceAgent(logicalDeviceID); agent != nil { |
| 605 | agent.packetIn(port, transactionID, packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 606 | } else { |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 607 | log.Error("logical-device-not-exist", log.Fields{"logicalDeviceId": logicalDeviceID}) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 608 | } |
| 609 | return nil |
| 610 | } |