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 |
| 35 | adapterProxy *AdapterProxy |
| 36 | kafkaProxy *kafka.KafkaMessagingProxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 37 | clusterDataProxy *model.Proxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 38 | exitChannel chan int |
| 39 | lockLogicalDeviceAgentsMap sync.RWMutex |
| 40 | } |
| 41 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 42 | func newLogicalDeviceManager(deviceMgr *DeviceManager, kafkaProxy *kafka.KafkaMessagingProxy, cdProxy *model.Proxy) *LogicalDeviceManager { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 43 | var logicalDeviceMgr LogicalDeviceManager |
| 44 | logicalDeviceMgr.exitChannel = make(chan int, 1) |
| 45 | logicalDeviceMgr.logicalDeviceAgents = make(map[string]*LogicalDeviceAgent) |
| 46 | logicalDeviceMgr.deviceMgr = deviceMgr |
| 47 | logicalDeviceMgr.kafkaProxy = kafkaProxy |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 48 | logicalDeviceMgr.clusterDataProxy = cdProxy |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 49 | logicalDeviceMgr.lockLogicalDeviceAgentsMap = sync.RWMutex{} |
| 50 | return &logicalDeviceMgr |
| 51 | } |
| 52 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 53 | func (ldMgr *LogicalDeviceManager) start(ctx context.Context) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 54 | log.Info("starting-logical-device-manager") |
| 55 | log.Info("logical-device-manager-started") |
| 56 | } |
| 57 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 58 | func (ldMgr *LogicalDeviceManager) stop(ctx context.Context) { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | log.Info("stopping-logical-device-manager") |
| 60 | ldMgr.exitChannel <- 1 |
| 61 | log.Info("logical-device-manager-stopped") |
| 62 | } |
| 63 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 64 | func sendAPIResponse(ctx context.Context, ch chan interface{}, result interface{}) { |
| 65 | if ctx.Err() == nil { |
| 66 | // Returned response only of the ctx has not been cancelled/timeout/etc |
| 67 | // Channel is automatically closed when a context is Done |
| 68 | ch <- result |
| 69 | log.Debugw("sendResponse", log.Fields{"result": result}) |
| 70 | } else { |
| 71 | // Should the transaction be reverted back? |
| 72 | log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
| 73 | } |
| 74 | } |
| 75 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 76 | func (ldMgr *LogicalDeviceManager) addLogicalDeviceAgentToMap(agent *LogicalDeviceAgent) { |
| 77 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 78 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 79 | if _, exist := ldMgr.logicalDeviceAgents[agent.logicalDeviceId]; !exist { |
| 80 | ldMgr.logicalDeviceAgents[agent.logicalDeviceId] = agent |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceAgent(logicalDeviceId string) *LogicalDeviceAgent { |
| 85 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 86 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 87 | if agent, ok := ldMgr.logicalDeviceAgents[logicalDeviceId]; ok { |
| 88 | return agent |
| 89 | } |
| 90 | return nil |
| 91 | } |
| 92 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 93 | func (ldMgr *LogicalDeviceManager) deleteLogicalDeviceAgent(logicalDeviceId string) { |
| 94 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 95 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 96 | delete(ldMgr.logicalDeviceAgents, logicalDeviceId) |
| 97 | } |
| 98 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 99 | // GetLogicalDevice provides a cloned most up to date logical device |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 100 | func (ldMgr *LogicalDeviceManager) getLogicalDevice(id string) (*voltha.LogicalDevice, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 101 | log.Debugw("getlogicalDevice", log.Fields{"logicaldeviceid": id}) |
| 102 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 103 | return agent.GetLogicalDevice() |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 104 | } |
| 105 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 106 | } |
| 107 | |
| 108 | func (ldMgr *LogicalDeviceManager) listLogicalDevices() (*voltha.LogicalDevices, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 109 | log.Debug("listLogicalDevices") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 110 | result := &voltha.LogicalDevices{} |
| 111 | ldMgr.lockLogicalDeviceAgentsMap.Lock() |
| 112 | defer ldMgr.lockLogicalDeviceAgentsMap.Unlock() |
| 113 | for _, agent := range ldMgr.logicalDeviceAgents { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 114 | if lDevice, err := agent.GetLogicalDevice(); err == nil { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 115 | result.Items = append(result.Items, lDevice) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return result, nil |
| 119 | } |
| 120 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 121 | func (ldMgr *LogicalDeviceManager) createLogicalDevice(ctx context.Context, device *voltha.Device) (*string, error) { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 122 | log.Debugw("creating-logical-device", log.Fields{"deviceId": device.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 123 | // Sanity check |
| 124 | if !device.Root { |
| 125 | return nil, errors.New("Device-not-root") |
| 126 | } |
| 127 | |
| 128 | // Create a logical device agent - the logical device Id is based on the mac address of the device |
| 129 | // For now use the serial number - it may contain any combination of alphabetic characters and numbers, |
| 130 | // with length varying from eight characters to a maximum of 14 characters. Mac Address is part of oneof |
| 131 | // in the Device model. May need to be moved out. |
| 132 | macAddress := device.MacAddress |
| 133 | id := strings.Replace(macAddress, ":", "", -1) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 134 | if id == "" { |
| 135 | log.Errorw("mac-address-not-set", log.Fields{"deviceId": device.Id}) |
| 136 | return nil, errors.New("mac-address-not-set") |
| 137 | } |
| 138 | log.Debugw("logical-device-id", log.Fields{"logicaldeviceId": id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 139 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 140 | agent := newLogicalDeviceAgent(id, device, ldMgr, ldMgr.deviceMgr, ldMgr.clusterDataProxy) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 141 | ldMgr.addLogicalDeviceAgentToMap(agent) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 142 | go agent.start(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 143 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 144 | log.Debug("creating-logical-device-ends") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 145 | return &id, nil |
| 146 | } |
| 147 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 148 | func (ldMgr *LogicalDeviceManager) deleteLogicalDevice(ctx context.Context, device *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 149 | log.Debugw("deleting-logical-device", log.Fields{"deviceId": device.Id}) |
| 150 | // Sanity check |
| 151 | if !device.Root { |
| 152 | return errors.New("Device-not-root") |
| 153 | } |
| 154 | logDeviceId := device.ParentId |
| 155 | if agent := ldMgr.getLogicalDeviceAgent(logDeviceId); agent != nil { |
| 156 | // Stop the logical device agent |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 157 | agent.stop(ctx) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 158 | //Remove the logical device agent from the Map |
| 159 | ldMgr.deleteLogicalDeviceAgent(logDeviceId) |
| 160 | } |
| 161 | |
| 162 | log.Debug("deleting-logical-device-ends") |
| 163 | return nil |
| 164 | } |
| 165 | |
| 166 | func (ldMgr *LogicalDeviceManager) getLogicalDeviceId(device *voltha.Device) (*string, error) { |
| 167 | // Device can either be a parent or a child device |
| 168 | if device.Root { |
| 169 | // Parent device. The ID of a parent device is the logical device ID |
| 170 | return &device.ParentId, nil |
| 171 | } |
| 172 | // Device is child device |
| 173 | // retrieve parent device using child device ID |
| 174 | if parentDevice := ldMgr.deviceMgr.getParentDevice(device); parentDevice != nil { |
| 175 | return &parentDevice.ParentId, nil |
| 176 | } |
| 177 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 178 | } |
| 179 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 180 | func (ldMgr *LogicalDeviceManager) getLogicalPortId(device *voltha.Device) (*voltha.LogicalPortId, error) { |
| 181 | // Get the logical device where this device is attached |
| 182 | var lDeviceId *string |
| 183 | var err error |
| 184 | if lDeviceId, err = ldMgr.getLogicalDeviceId(device); err != nil { |
| 185 | return nil, err |
| 186 | } |
| 187 | var lDevice *voltha.LogicalDevice |
| 188 | if lDevice, err = ldMgr.getLogicalDevice(*lDeviceId); err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | // Go over list of ports |
| 192 | for _, port := range lDevice.Ports { |
| 193 | if port.DeviceId == device.Id { |
| 194 | return &voltha.LogicalPortId{Id: *lDeviceId, PortId: port.Id}, nil |
| 195 | } |
| 196 | } |
| 197 | return nil, status.Errorf(codes.NotFound, "%s", device.Id) |
| 198 | } |
| 199 | |
| 200 | func (ldMgr *LogicalDeviceManager) ListLogicalDevicePorts(ctx context.Context, id string) (*voltha.LogicalPorts, error) { |
| 201 | log.Debugw("ListLogicalDevicePorts", log.Fields{"logicaldeviceid": id}) |
| 202 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 203 | return agent.ListLogicalDevicePorts() |
| 204 | } |
| 205 | return nil, status.Errorf(codes.NotFound, "%s", id) |
| 206 | |
| 207 | } |
| 208 | |
| 209 | func (ldMgr *LogicalDeviceManager) getLogicalPort(lPortId *voltha.LogicalPortId) (*voltha.LogicalPort, error) { |
| 210 | // Get the logical device where this device is attached |
| 211 | var err error |
| 212 | var lDevice *voltha.LogicalDevice |
| 213 | if lDevice, err = ldMgr.getLogicalDevice(lPortId.Id); err != nil { |
| 214 | return nil, err |
| 215 | } |
| 216 | // Go over list of ports |
| 217 | for _, port := range lDevice.Ports { |
| 218 | if port.Id == lPortId.PortId { |
| 219 | return port, nil |
| 220 | } |
| 221 | } |
| 222 | return nil, status.Errorf(codes.NotFound, "%s-$s", lPortId.Id, lPortId.PortId) |
| 223 | } |
| 224 | |
| 225 | // deleteLogicalPort removes the logical port associated with a child device |
| 226 | func (ldMgr *LogicalDeviceManager) deleteLogicalPort(ctx context.Context, lPortId *voltha.LogicalPortId) error { |
| 227 | log.Debugw("deleting-logical-port", log.Fields{"LDeviceId": lPortId.Id}) |
| 228 | // Get logical port |
| 229 | var logicalPort *voltha.LogicalPort |
| 230 | var err error |
| 231 | if logicalPort, err = ldMgr.getLogicalPort(lPortId); err != nil { |
| 232 | log.Debugw("no-logical-device-port-present", log.Fields{"logicalPortId": lPortId.PortId}) |
| 233 | return err |
| 234 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 235 | // Sanity check |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 236 | if logicalPort.RootPort { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 237 | return errors.New("Device-root") |
| 238 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 239 | if agent := ldMgr.getLogicalDeviceAgent(lPortId.Id); agent != nil { |
| 240 | agent.deleteLogicalPort(logicalPort) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | log.Debug("deleting-logical-port-ends") |
| 244 | return nil |
| 245 | } |
| 246 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 247 | func (ldMgr *LogicalDeviceManager) addUNILogicalPort(ctx context.Context, childDevice *voltha.Device) error { |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 248 | log.Debugw("AddUNILogicalPort", log.Fields{"deviceId": childDevice.Id}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 249 | // Sanity check |
| 250 | if childDevice.Root { |
| 251 | return errors.New("Device-root") |
| 252 | } |
| 253 | |
| 254 | // Get the logical device id parent device |
| 255 | parentId := childDevice.ParentId |
| 256 | logDeviceId := ldMgr.deviceMgr.GetParentDeviceId(parentId) |
| 257 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 258 | log.Debugw("AddUNILogicalPort", log.Fields{"logDeviceId": logDeviceId, "parentId": parentId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 259 | |
| 260 | if agent := ldMgr.getLogicalDeviceAgent(*logDeviceId); agent != nil { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 261 | return agent.addUNILogicalPort(ctx, childDevice) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 262 | } |
| 263 | return status.Errorf(codes.NotFound, "%s", childDevice.Id) |
| 264 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 265 | |
| 266 | func (ldMgr *LogicalDeviceManager) updateFlowTable(ctx context.Context, id string, flow *openflow_13.OfpFlowMod, ch chan interface{}) { |
| 267 | log.Debugw("updateFlowTable", log.Fields{"logicalDeviceId": id}) |
| 268 | var res interface{} |
| 269 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 270 | res = agent.updateFlowTable(ctx, flow) |
| 271 | log.Debugw("updateFlowTable-result", log.Fields{"result": res}) |
| 272 | } else { |
| 273 | res = status.Errorf(codes.NotFound, "%s", id) |
| 274 | } |
| 275 | sendAPIResponse(ctx, ch, res) |
| 276 | } |
| 277 | |
| 278 | func (ldMgr *LogicalDeviceManager) updateGroupTable(ctx context.Context, id string, groupMod *openflow_13.OfpGroupMod, ch chan interface{}) { |
| 279 | log.Debugw("updateGroupTable", log.Fields{"logicalDeviceId": id}) |
| 280 | var res interface{} |
| 281 | if agent := ldMgr.getLogicalDeviceAgent(id); agent != nil { |
| 282 | res = agent.updateGroupTable(ctx, groupMod) |
| 283 | log.Debugw("updateGroupTable-result", log.Fields{"result": res}) |
| 284 | } else { |
| 285 | res = status.Errorf(codes.NotFound, "%s", id) |
| 286 | } |
| 287 | sendAPIResponse(ctx, ch, res) |
| 288 | } |
| 289 | |
| 290 | func (ldMgr *LogicalDeviceManager) enableLogicalPort(ctx context.Context, id *voltha.LogicalPortId, ch chan interface{}) { |
| 291 | log.Debugw("enableLogicalPort", log.Fields{"logicalDeviceId": id}) |
| 292 | var res interface{} |
| 293 | // Get logical port |
| 294 | var logicalPort *voltha.LogicalPort |
| 295 | var err error |
| 296 | if logicalPort, err = ldMgr.getLogicalPort(id); err != nil { |
| 297 | log.Debugw("no-logical-device-port-present", log.Fields{"logicalPortId": id.PortId}) |
| 298 | res = err |
| 299 | } |
| 300 | if agent := ldMgr.getLogicalDeviceAgent(id.Id); agent != nil { |
| 301 | res = agent.enableLogicalPort(logicalPort) |
| 302 | log.Debugw("enableLogicalPort-result", log.Fields{"result": res}) |
| 303 | } else { |
| 304 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 305 | } |
| 306 | sendAPIResponse(ctx, ch, res) |
| 307 | } |
| 308 | |
| 309 | func (ldMgr *LogicalDeviceManager) disableLogicalPort(ctx context.Context, id *voltha.LogicalPortId, ch chan interface{}) { |
| 310 | log.Debugw("disableLogicalPort", log.Fields{"logicalDeviceId": id}) |
| 311 | var res interface{} |
| 312 | // Get logical port |
| 313 | var logicalPort *voltha.LogicalPort |
| 314 | var err error |
| 315 | if logicalPort, err = ldMgr.getLogicalPort(id); err != nil { |
| 316 | log.Debugw("no-logical-device-port-present", log.Fields{"logicalPortId": id.PortId}) |
| 317 | res = err |
| 318 | } |
| 319 | if agent := ldMgr.getLogicalDeviceAgent(id.Id); agent != nil { |
| 320 | res = agent.disableLogicalPort(logicalPort) |
| 321 | log.Debugw("disableLogicalPort-result", log.Fields{"result": res}) |
| 322 | } else { |
| 323 | res = status.Errorf(codes.NotFound, "%s", id.Id) |
| 324 | } |
| 325 | sendAPIResponse(ctx, ch, res) |
| 326 | } |