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