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" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/protos/openflow_13" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/protos/voltha" |
| 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 { |
| 33 | logicalDeviceAgents map[string]*LogicalDeviceAgent |
| 34 | deviceMgr *DeviceManager |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 35 | grpcNbiHdlr *APIHandler |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 36 | adapterProxy *AdapterProxy |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 37 | kafkaICProxy *kafka.InterContainerProxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 38 | clusterDataProxy *model.Proxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 39 | exitChannel chan int |
| 40 | lockLogicalDeviceAgentsMap sync.RWMutex |
| 41 | } |
| 42 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 43 | func newLogicalDeviceManager(deviceMgr *DeviceManager, kafkaICProxy *kafka.InterContainerProxy, cdProxy *model.Proxy) *LogicalDeviceManager { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 44 | var logicalDeviceMgr LogicalDeviceManager |
| 45 | logicalDeviceMgr.exitChannel = make(chan int, 1) |
| 46 | logicalDeviceMgr.logicalDeviceAgents = make(map[string]*LogicalDeviceAgent) |
| 47 | logicalDeviceMgr.deviceMgr = deviceMgr |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 48 | logicalDeviceMgr.kafkaICProxy = kafkaICProxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 49 | logicalDeviceMgr.clusterDataProxy = cdProxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 50 | logicalDeviceMgr.lockLogicalDeviceAgentsMap = sync.RWMutex{} |
| 51 | return &logicalDeviceMgr |
| 52 | } |
| 53 | |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 54 | func (ldMgr *LogicalDeviceManager) setGrpcNbiHandler(grpcNbiHandler *APIHandler) { |
| 55 | ldMgr.grpcNbiHdlr = grpcNbiHandler |
| 56 | } |
| 57 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 58 | func (ldMgr *LogicalDeviceManager) start(ctx context.Context) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | log.Info("starting-logical-device-manager") |
| 60 | log.Info("logical-device-manager-started") |
| 61 | } |
| 62 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 63 | func (ldMgr *LogicalDeviceManager) stop(ctx context.Context) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 64 | log.Info("stopping-logical-device-manager") |
| 65 | ldMgr.exitChannel <- 1 |
| 66 | log.Info("logical-device-manager-stopped") |
| 67 | } |
| 68 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 69 | func sendAPIResponse(ctx context.Context, ch chan interface{}, result interface{}) { |
| 70 | if ctx.Err() == nil { |
| 71 | // Returned response only of the ctx has not been cancelled/timeout/etc |
| 72 | // Channel is automatically closed when a context is Done |
| 73 | ch <- result |
| 74 | log.Debugw("sendResponse", log.Fields{"result": result}) |
| 75 | } else { |
| 76 | // Should the transaction be reverted back? |
| 77 | log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
| 78 | } |
| 79 | } |
| 80 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 81 | func (ldMgr *LogicalDeviceManager) addLogicalDeviceAgentToMap(agent *LogicalDeviceAgent) { |
| 82 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 83 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 84 | if _, exist := ldMgr.logicalDeviceAgents[agent.logicalDeviceId]; !exist { |
| 85 | ldMgr.logicalDeviceAgents[agent.logicalDeviceId] = agent |
| 86 | } |
| 87 | } |
| 88 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 89 | // getLogicalDeviceAgent returns the logical device agent. If the device is not in memory then the device will |
| 90 | // be loaded from dB and a logical device agent created to managed it. |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 91 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceAgent(logicalDeviceId string) *LogicalDeviceAgent { |
| 92 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 93 | if agent, ok := ldMgr.logicalDeviceAgents[logicalDeviceId]; ok { |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 94 | ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 95 | return agent |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 96 | } else { |
| 97 | // Try to load into memory - loading will also create the logical device agent |
| 98 | ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 99 | if err := ldMgr.load(logicalDeviceId); err == nil { |
| 100 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 101 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 102 | if agent, ok = ldMgr.logicalDeviceAgents[logicalDeviceId]; ok { |
| 103 | return agent |
| 104 | } |
| 105 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 106 | } |
| 107 | return nil |
| 108 | } |
| 109 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 110 | func (ldMgr *LogicalDeviceManager) deleteLogicalDeviceAgent(logicalDeviceId string) { |
| 111 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 112 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 113 | delete(ldMgr.logicalDeviceAgents, logicalDeviceId) |
| 114 | } |
| 115 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 116 | // GetLogicalDevice provides a cloned most up to date logical device. If device is not in memory |
| 117 | // it will be fetched from the dB |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 118 | func (ldMgr *LogicalDeviceManager) getLogicalDevice(id string) (*voltha.LogicalDevice, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 119 | log.Debugw("getlogicalDevice", log.Fields{"logicaldeviceid": id}) |
| 120 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 121 | return agent.GetLogicalDevice() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 122 | } |
| 123 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 124 | } |
| 125 | |
| 126 | func (ldMgr *LogicalDeviceManager) listLogicalDevices() (*voltha.LogicalDevices, error) { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 127 | log.Debug("ListAllLogicalDevices") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 128 | result := &voltha.LogicalDevices{} |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 129 | if logicalDevices := ldMgr.clusterDataProxy.List("/logical_devices", 0, false, ""); logicalDevices != nil { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 130 | for _, logicalDevice := range logicalDevices.([]interface{}) { |
| 131 | if agent := ldMgr.getLogicalDeviceAgent(logicalDevice.(*voltha.LogicalDevice).Id); agent == nil { |
| 132 | agent = newLogicalDeviceAgent( |
| 133 | logicalDevice.(*voltha.LogicalDevice).Id, |
| 134 | logicalDevice.(*voltha.LogicalDevice).RootDeviceId, |
| 135 | ldMgr, |
| 136 | ldMgr.deviceMgr, |
| 137 | ldMgr.clusterDataProxy, |
| 138 | ) |
| 139 | ldMgr.addLogicalDeviceAgentToMap(agent) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 140 | go agent.start(nil, true) |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 141 | } |
| 142 | result.Items = append(result.Items, logicalDevice.(*voltha.LogicalDevice)) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | return result, nil |
| 146 | } |
| 147 | |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 148 | // List only logical devices that are in memory |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 149 | //func (ldMgr *LogicalDeviceManager) listLogicalDevices() (*voltha.LogicalDevices, error) { |
| 150 | // log.Debug("listLogicalDevices") |
| 151 | // result := &voltha.LogicalDevices{} |
| 152 | // ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 153 | // defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 154 | // for _, agent := range ldMgr.logicalDeviceAgents { |
| 155 | // if lDevice, err := agent.GetLogicalDevice(); err == nil { |
| 156 | // result.Items = append(result.Items, lDevice) |
| 157 | // } |
| 158 | // } |
| 159 | // return result, nil |
| 160 | //} |
| 161 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 162 | func (ldMgr *LogicalDeviceManager) createLogicalDevice(ctx context.Context, device *voltha.Device) (*string, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 163 | log.Debugw("creating-logical-device", log.Fields{"deviceId": device.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 164 | // Sanity check |
| 165 | if !device.Root { |
| 166 | return nil, errors.New("Device-not-root") |
| 167 | } |
| 168 | |
| 169 | // Create a logical device agent - the logical device Id is based on the mac address of the device |
| 170 | // For now use the serial number - it may contain any combination of alphabetic characters and numbers, |
| 171 | // with length varying from eight characters to a maximum of 14 characters. Mac Address is part of oneof |
| 172 | // in the Device model. May need to be moved out. |
| 173 | macAddress := device.MacAddress |
| 174 | id := strings.Replace(macAddress, ":", "", -1) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 175 | if id == "" { |
| 176 | log.Errorw("mac-address-not-set", log.Fields{"deviceId": device.Id}) |
| 177 | return nil, errors.New("mac-address-not-set") |
| 178 | } |
| 179 | log.Debugw("logical-device-id", log.Fields{"logicaldeviceId": id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 180 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 181 | agent := newLogicalDeviceAgent(id, device.Id, ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 182 | ldMgr.addLogicalDeviceAgentToMap(agent) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 183 | go agent.start(ctx, false) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 184 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 185 | log.Debug("creating-logical-device-ends") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 186 | return &id, nil |
| 187 | } |
| 188 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 189 | // load loads a logical device manager in memory |
| 190 | func (ldMgr *LogicalDeviceManager) load(lDeviceId string) error { |
| 191 | log.Debugw("loading-logical-device", log.Fields{"lDeviceId": lDeviceId}) |
| 192 | // To prevent a race condition, let's hold the logical device agent map lock. This will prevent a loading and |
| 193 | // a create logical device callback from occurring at the same time. |
| 194 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 195 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 196 | if ldAgent, _ := ldMgr.logicalDeviceAgents[lDeviceId]; ldAgent == nil { |
| 197 | // Logical device not in memory - create a temp logical device Agent and let it load from memory |
| 198 | agent := newLogicalDeviceAgent(lDeviceId, "", ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy) |
| 199 | if err := agent.start(nil, true); err != nil { |
khenaidoo | 8c3303d | 2019-02-13 14:59:39 -0500 | [diff] [blame] | 200 | //agent.stop(nil) |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 201 | return err |
| 202 | } |
| 203 | ldMgr.logicalDeviceAgents[agent.logicalDeviceId] = agent |
| 204 | } |
| 205 | // TODO: load the child device |
| 206 | return nil |
| 207 | } |
| 208 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 209 | func (ldMgr *LogicalDeviceManager) deleteLogicalDevice(ctx context.Context, device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 210 | log.Debugw("deleting-logical-device", log.Fields{"deviceId": device.Id}) |
| 211 | // Sanity check |
| 212 | if !device.Root { |
| 213 | return errors.New("Device-not-root") |
| 214 | } |
| 215 | logDeviceId := device.ParentId |
| 216 | if agent := ldMgr.getLogicalDeviceAgent(logDeviceId); agent != nil { |
| 217 | // Stop the logical device agent |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 218 | agent.stop(ctx) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 219 | //Remove the logical device agent from the Map |
| 220 | ldMgr.deleteLogicalDeviceAgent(logDeviceId) |
| 221 | } |
| 222 | |
| 223 | log.Debug("deleting-logical-device-ends") |
| 224 | return nil |
| 225 | } |
| 226 | |
| 227 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceId(device *voltha.Device) (*string, error) { |
| 228 | // Device can either be a parent or a child device |
| 229 | if device.Root { |
| 230 | // Parent device. The ID of a parent device is the logical device ID |
| 231 | return &device.ParentId, nil |
| 232 | } |
| 233 | // Device is child device |
| 234 | // retrieve parent device using child device ID |
| 235 | if parentDevice := ldMgr.deviceMgr.getParentDevice(device); parentDevice != nil { |
| 236 | return &parentDevice.ParentId, nil |
| 237 | } |
| 238 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 239 | } |
| 240 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 241 | func (ldMgr *LogicalDeviceManager) getLogicalPortId(device *voltha.Device) (*voltha.LogicalPortId, error) { |
| 242 | // Get the logical device where this device is attached |
| 243 | var lDeviceId *string |
| 244 | var err error |
| 245 | if lDeviceId, err = ldMgr.getLogicalDeviceId(device); err != nil { |
| 246 | return nil, err |
| 247 | } |
| 248 | var lDevice *voltha.LogicalDevice |
| 249 | if lDevice, err = ldMgr.getLogicalDevice(*lDeviceId); err != nil { |
| 250 | return nil, err |
| 251 | } |
| 252 | // Go over list of ports |
| 253 | for _, port := range lDevice.Ports { |
| 254 | if port.DeviceId == device.Id { |
| 255 | return &voltha.LogicalPortId{Id: *lDeviceId, PortId: port.Id}, nil |
| 256 | } |
| 257 | } |
| 258 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 259 | } |
| 260 | |
| 261 | func (ldMgr *LogicalDeviceManager) ListLogicalDevicePorts(ctx context.Context, id string) (*voltha.LogicalPorts, error) { |
| 262 | log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
| 263 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 264 | return agent.ListLogicalDevicePorts() |
| 265 | } |
| 266 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 267 | |
| 268 | } |
| 269 | |
| 270 | func (ldMgr *LogicalDeviceManager) getLogicalPort(lPortId *voltha.LogicalPortId) (*voltha.LogicalPort, error) { |
| 271 | // Get the logical device where this device is attached |
| 272 | var err error |
| 273 | var lDevice *voltha.LogicalDevice |
| 274 | if lDevice, err = ldMgr.getLogicalDevice(lPortId.Id); err != nil { |
| 275 | return nil, err |
| 276 | } |
| 277 | // Go over list of ports |
| 278 | for _, port := range lDevice.Ports { |
| 279 | if port.Id == lPortId.PortId { |
| 280 | return port, nil |
| 281 | } |
| 282 | } |
| 283 | return nil, status.Errorf(codes.NotFound, "%s-$s", lPortId.Id, lPortId.PortId) |
| 284 | } |
| 285 | |
| 286 | // deleteLogicalPort removes the logical port associated with a child device |
| 287 | func (ldMgr *LogicalDeviceManager) deleteLogicalPort(ctx context.Context, lPortId *voltha.LogicalPortId) error { |
| 288 | log.Debugw("deleting-logical-port", log.Fields{"LDeviceId": lPortId.Id}) |
| 289 | // Get logical port |
| 290 | var logicalPort *voltha.LogicalPort |
| 291 | var err error |
| 292 | if logicalPort, err = ldMgr.getLogicalPort(lPortId); err != nil { |
| 293 | log.Debugw("no-logical-device-port-present", log.Fields{"logicalPortId": lPortId.PortId}) |
| 294 | return err |
| 295 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 296 | // Sanity check |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 297 | if logicalPort.RootPort { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 298 | return errors.New("Device-root") |
| 299 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 300 | if agent := ldMgr.getLogicalDeviceAgent(lPortId.Id); agent != nil { |
| 301 | agent.deleteLogicalPort(logicalPort) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | log.Debug("deleting-logical-port-ends") |
| 305 | return nil |
| 306 | } |
| 307 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 308 | func (ldMgr *LogicalDeviceManager) addUNILogicalPort(ctx context.Context, childDevice *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 309 | log.Debugw("AddUNILogicalPort", log.Fields{"deviceId": childDevice.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 310 | // Sanity check |
| 311 | if childDevice.Root { |
| 312 | return errors.New("Device-root") |
| 313 | } |
| 314 | |
| 315 | // Get the logical device id parent device |
| 316 | parentId := childDevice.ParentId |
| 317 | logDeviceId := ldMgr.deviceMgr.GetParentDeviceId(parentId) |
| 318 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 319 | log.Debugw("AddUNILogicalPort", log.Fields{"logDeviceId": logDeviceId, "parentId": parentId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 320 | |
| 321 | if agent := ldMgr.getLogicalDeviceAgent(*logDeviceId); agent != nil { |
khenaidoo | bcf205b | 2019-01-25 22:21:14 -0500 | [diff] [blame] | 322 | if err := agent.addUNILogicalPort(ctx, childDevice); err != nil { |
| 323 | return err |
| 324 | } |
| 325 | // Update the device routes - let it run in its own go routine as it can take time |
| 326 | go agent.updateRoutes() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 327 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 328 | return nil |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 329 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 330 | |
| 331 | func (ldMgr *LogicalDeviceManager) updateFlowTable(ctx context.Context, id string, flow *openflow_13.OfpFlowMod, ch chan interface{}) { |
| 332 | log.Debugw("updateFlowTable", log.Fields{"logicalDeviceId": id}) |
| 333 | var res interface{} |
| 334 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 335 | res = agent.updateFlowTable(ctx, flow) |
| 336 | log.Debugw("updateFlowTable-result", log.Fields{"result": res}) |
| 337 | } else { |
| 338 | res = status.Errorf(codes.NotFound, "%s", id) |
| 339 | } |
| 340 | sendAPIResponse(ctx, ch, res) |
| 341 | } |
| 342 | |
| 343 | func (ldMgr *LogicalDeviceManager) updateGroupTable(ctx context.Context, id string, groupMod *openflow_13.OfpGroupMod, ch chan interface{}) { |
| 344 | log.Debugw("updateGroupTable", log.Fields{"logicalDeviceId": id}) |
| 345 | var res interface{} |
| 346 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 347 | res = agent.updateGroupTable(ctx, groupMod) |
| 348 | log.Debugw("updateGroupTable-result", log.Fields{"result": res}) |
| 349 | } else { |
| 350 | res = status.Errorf(codes.NotFound, "%s", id) |
| 351 | } |
| 352 | sendAPIResponse(ctx, ch, res) |
| 353 | } |
| 354 | |
| 355 | func (ldMgr *LogicalDeviceManager) enableLogicalPort(ctx context.Context, id *voltha.LogicalPortId, ch chan interface{}) { |
| 356 | log.Debugw("enableLogicalPort", log.Fields{"logicalDeviceId": id}) |
| 357 | var res interface{} |
| 358 | // Get logical port |
| 359 | var logicalPort *voltha.LogicalPort |
| 360 | var err error |
| 361 | if logicalPort, err = ldMgr.getLogicalPort(id); err != nil { |
| 362 | log.Debugw("no-logical-device-port-present", log.Fields{"logicalPortId": id.PortId}) |
| 363 | res = err |
| 364 | } |
| 365 | if agent := ldMgr.getLogicalDeviceAgent(id.Id); agent != nil { |
| 366 | res = agent.enableLogicalPort(logicalPort) |
| 367 | log.Debugw("enableLogicalPort-result", log.Fields{"result": res}) |
| 368 | } else { |
| 369 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 370 | } |
| 371 | sendAPIResponse(ctx, ch, res) |
| 372 | } |
| 373 | |
| 374 | func (ldMgr *LogicalDeviceManager) disableLogicalPort(ctx context.Context, id *voltha.LogicalPortId, ch chan interface{}) { |
| 375 | log.Debugw("disableLogicalPort", log.Fields{"logicalDeviceId": id}) |
| 376 | var res interface{} |
| 377 | // Get logical port |
| 378 | var logicalPort *voltha.LogicalPort |
| 379 | var err error |
| 380 | if logicalPort, err = ldMgr.getLogicalPort(id); err != nil { |
| 381 | log.Debugw("no-logical-device-port-present", log.Fields{"logicalPortId": id.PortId}) |
| 382 | res = err |
| 383 | } |
| 384 | if agent := ldMgr.getLogicalDeviceAgent(id.Id); agent != nil { |
| 385 | res = agent.disableLogicalPort(logicalPort) |
| 386 | log.Debugw("disableLogicalPort-result", log.Fields{"result": res}) |
| 387 | } else { |
| 388 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 389 | } |
| 390 | sendAPIResponse(ctx, ch, res) |
| 391 | } |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 392 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 393 | func (ldMgr *LogicalDeviceManager) packetOut(packetOut *openflow_13.PacketOut) { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 394 | log.Debugw("packetOut", log.Fields{"logicalDeviceId": packetOut.Id}) |
| 395 | if agent := ldMgr.getLogicalDeviceAgent(packetOut.Id); agent != nil { |
| 396 | agent.packetOut(packetOut.PacketOut) |
| 397 | } else { |
| 398 | log.Error("logical-device-not-exist", log.Fields{"logicalDeviceId": packetOut.Id}) |
| 399 | } |
| 400 | } |
| 401 | |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 402 | func (ldMgr *LogicalDeviceManager) packetIn(logicalDeviceId string, port uint32, transactionId string, packet []byte) error { |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 403 | log.Debugw("packetIn", log.Fields{"logicalDeviceId": logicalDeviceId, "port": port}) |
| 404 | if agent := ldMgr.getLogicalDeviceAgent(logicalDeviceId); agent != nil { |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 405 | agent.packetIn(port, transactionId, packet) |
khenaidoo | fdbad6e | 2018-11-06 22:26:38 -0500 | [diff] [blame] | 406 | } else { |
| 407 | log.Error("logical-device-not-exist", log.Fields{"logicalDeviceId": logicalDeviceId}) |
| 408 | } |
| 409 | return nil |
| 410 | } |