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