khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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 device |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | |
| 21 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 22 | "github.com/opencord/voltha-protos/v5/go/core" |
| 23 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 24 | "google.golang.org/grpc/codes" |
| 25 | "google.golang.org/grpc/status" |
| 26 | ) |
| 27 | |
| 28 | // |
| 29 | //CreateLogicalDevice creates logical device in core |
| 30 | func (dMgr *Manager) CreateLogicalDevice(ctx context.Context, cDevice *voltha.Device) error { |
| 31 | logger.Info(ctx, "create-logical-device") |
| 32 | // Verify whether the logical device has already been created |
| 33 | if cDevice.ParentId != "" { |
| 34 | logger.Debugw(ctx, "parent-device-already-exist", log.Fields{"device-id": cDevice.Id, "logical-device-id": cDevice.Id}) |
| 35 | return nil |
| 36 | } |
| 37 | var err error |
| 38 | if _, err = dMgr.logicalDeviceMgr.createLogicalDevice(ctx, cDevice); err != nil { |
| 39 | logger.Warnw(ctx, "create-logical-device-error", log.Fields{"device": cDevice}) |
| 40 | return err |
| 41 | } |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | // DeleteLogicalDevice deletes logical device from core |
| 46 | func (dMgr *Manager) DeleteLogicalDevice(ctx context.Context, cDevice *voltha.Device) error { |
| 47 | logger.Info(ctx, "delete-logical-device") |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame^] | 48 | if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 49 | return err |
| 50 | } |
| 51 | // Remove the logical device Id from the parent device |
| 52 | logicalID := "" |
| 53 | dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID) |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | // DeleteLogicalPorts removes the logical ports associated with that deviceId |
| 58 | func (dMgr *Manager) DeleteLogicalPorts(ctx context.Context, cDevice *voltha.Device) error { |
| 59 | logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"device-id": cDevice.Id}) |
| 60 | if err := dMgr.logicalDeviceMgr.deleteLogicalPorts(ctx, cDevice.Id); err != nil { |
| 61 | // Just log the error. The logical device or port may already have been deleted before this callback is invoked. |
| 62 | logger.Warnw(ctx, "delete-logical-ports-error", log.Fields{"device-id": cDevice.Id, "error": err}) |
| 63 | } |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // SetupUNILogicalPorts creates UNI ports on the logical device that represents a child UNI interface |
| 68 | func (dMgr *Manager) SetupUNILogicalPorts(ctx context.Context, cDevice *voltha.Device) error { |
| 69 | logger.Info(ctx, "setup-uni-logical-ports") |
| 70 | cDevicePorts, err := dMgr.listDevicePorts(ctx, cDevice.Id) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | if err := dMgr.logicalDeviceMgr.setupUNILogicalPorts(ctx, cDevice, cDevicePorts); err != nil { |
| 75 | logger.Warnw(ctx, "setup-uni-logical-ports-error", log.Fields{"device": cDevice, "err": err}) |
| 76 | return err |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // RunPostDeviceDelete removes any reference of this device |
| 82 | func (dMgr *Manager) RunPostDeviceDelete(ctx context.Context, cDevice *voltha.Device) error { |
| 83 | logger.Infow(ctx, "run-post-device-delete", log.Fields{"device-id": cDevice.Id}) |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame^] | 84 | if agent := dMgr.getDeviceAgent(ctx, cDevice.Id); agent != nil { |
| 85 | logger.Debugw(ctx, "invoking-delete-device-and-ports", log.Fields{"device-id": cDevice.Id}) |
| 86 | //delete ports |
| 87 | if err := agent.deleteAllPorts(ctx); err != nil { |
| 88 | logger.Warnw(ctx, "failure-delete-device-ports", log.Fields{"device-id": cDevice.Id, "error": err.Error()}) |
| 89 | } |
| 90 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 91 | dMgr.stopManagingDevice(ctx, cDevice.Id) |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | //DeleteAllLogicalPorts is invoked as a callback when the parent device's connection status moves to UNREACHABLE |
| 96 | func (dMgr *Manager) DeleteAllLogicalPorts(ctx context.Context, parentDevice *voltha.Device) error { |
| 97 | logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"parent-device-id": parentDevice.Id}) |
| 98 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(ctx, parentDevice); err != nil { |
| 99 | // Just log error as logical device may already have been deleted |
| 100 | logger.Warnw(ctx, "delete-all-logical-ports-fail", log.Fields{"parent-device-id": parentDevice.Id, "error": err}) |
| 101 | } |
| 102 | return nil |
| 103 | } |
| 104 | |
| 105 | //DeleteAllChildDevices is invoked as a callback when the parent device is deleted |
| 106 | func (dMgr *Manager) DeleteAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error { |
| 107 | logger.Debugw(ctx, "delete-all-child-devices", log.Fields{"parent-device-id": parentCurrDevice.Id}) |
| 108 | force := false |
| 109 | // Get the parent device Transient state, if its FORCE_DELETED(go for force delete for child devices) |
| 110 | // So in cases when this handler is getting called other than DELETE operation, no force option would be used. |
| 111 | agent := dMgr.getDeviceAgent(ctx, parentCurrDevice.Id) |
| 112 | if agent == nil { |
| 113 | return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id) |
| 114 | } |
| 115 | |
| 116 | force = agent.getTransientState() == core.DeviceTransientState_FORCE_DELETING |
| 117 | |
| 118 | ports, _ := dMgr.listDevicePorts(ctx, parentCurrDevice.Id) |
| 119 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, ports) { |
| 120 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame^] | 121 | logger.Debugw(ctx, "invoking-delete-device-and-ports", log.Fields{"device-id": childDeviceID, "force-delete": force}) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 122 | if force { |
| 123 | if err := agent.deleteDeviceForce(ctx); err != nil { |
| 124 | logger.Warnw(ctx, "failure-delete-device-force", log.Fields{"device-id": childDeviceID, |
| 125 | "error": err.Error()}) |
| 126 | } |
| 127 | } else { |
| 128 | if err := agent.deleteDevice(ctx); err != nil { |
| 129 | logger.Warnw(ctx, "failure-delete-device", log.Fields{"device-id": childDeviceID, |
| 130 | "error": err.Error()}) |
| 131 | } |
| 132 | } |
| 133 | // No further action is required here. The deleteDevice will change the device state where the resulting |
| 134 | // callback will take care of cleaning the child device agent. |
| 135 | } |
| 136 | } |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | //DeleteAllDeviceFlows is invoked as a callback when the parent device's connection status moves to UNREACHABLE |
| 141 | func (dMgr *Manager) DeleteAllDeviceFlows(ctx context.Context, parentDevice *voltha.Device) error { |
| 142 | logger.Debugw(ctx, "delete-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
| 143 | if agent := dMgr.getDeviceAgent(ctx, parentDevice.Id); agent != nil { |
| 144 | if err := agent.deleteAllFlows(ctx); err != nil { |
| 145 | logger.Errorw(ctx, "error-deleting-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
| 146 | return err |
| 147 | } |
| 148 | return nil |
| 149 | } |
| 150 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
| 151 | } |
| 152 | |
| 153 | // ChildDeviceLost calls parent adapter to delete child device and all its references |
| 154 | func (dMgr *Manager) ChildDeviceLost(ctx context.Context, curr *voltha.Device) error { |
| 155 | logger.Debugw(ctx, "child-device-lost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId}) |
| 156 | if parentAgent := dMgr.getDeviceAgent(ctx, curr.ParentId); parentAgent != nil { |
| 157 | if err := parentAgent.ChildDeviceLost(ctx, curr); err != nil { |
| 158 | // Just log the message and let the remaining pipeline proceed. |
| 159 | logger.Warnw(ctx, "childDeviceLost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId, "error": err}) |
| 160 | } |
| 161 | } |
| 162 | // Do not return an error as parent device may also have been deleted. Let the remaining pipeline proceed. |
| 163 | return nil |
| 164 | } |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame^] | 165 | |
| 166 | func (dMgr *Manager) DeleteAllLogicalMeters(ctx context.Context, cDevice *voltha.Device) error { |
| 167 | logger.Debugw(ctx, "delete-all-logical-device-meters", log.Fields{"device-id": cDevice.Id}) |
| 168 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalMeters(ctx, cDevice.Id); err != nil { |
| 169 | // Just log the error. The logical device or port may already have been deleted before this callback is invoked. |
| 170 | logger.Warnw(ctx, "delete-logical-ports-error", log.Fields{"device-id": cDevice.Id, "error": err}) |
| 171 | } |
| 172 | return nil |
| 173 | |
| 174 | } |