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