khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1 | /* |
Joey Armstrong | 7a9af44 | 2024-01-03 19:26:36 -0500 | [diff] [blame^] | 2 | * Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 3 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 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 |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 7 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 9 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 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. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 15 | */ |
| 16 | package device |
| 17 | |
| 18 | import ( |
| 19 | "context" |
khenaidoo | 68a5e0c | 2021-11-06 13:08:03 -0400 | [diff] [blame] | 20 | "errors" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 21 | |
| 22 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
khenaidoo | 68a5e0c | 2021-11-06 13:08:03 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-protos/v5/go/core" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 25 | "google.golang.org/grpc/codes" |
| 26 | "google.golang.org/grpc/status" |
| 27 | ) |
| 28 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 29 | // CreateLogicalDevice creates logical device in core |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 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}) |
Andrea Campanella | 832cff6 | 2021-11-05 17:05:18 +0100 | [diff] [blame] | 84 | //deleting the logical device |
| 85 | logger.Debugw(ctx, "delete-logical-device", log.Fields{"device-id": cDevice.Id}) |
| 86 | if dMgr.logicalDeviceMgr != nil && cDevice.Root { |
| 87 | if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil { |
| 88 | logger.Warnw(ctx, "failure-delete-logical-device", log.Fields{"device-id": cDevice.Id, "error": err.Error()}) |
| 89 | } |
| 90 | // Remove the logical device Id from the parent device |
| 91 | logicalID := "" |
| 92 | dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID) |
| 93 | } |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 94 | if agent := dMgr.getDeviceAgent(ctx, cDevice.Id); agent != nil { |
| 95 | logger.Debugw(ctx, "invoking-delete-device-and-ports", log.Fields{"device-id": cDevice.Id}) |
| 96 | //delete ports |
| 97 | if err := agent.deleteAllPorts(ctx); err != nil { |
| 98 | logger.Warnw(ctx, "failure-delete-device-ports", log.Fields{"device-id": cDevice.Id, "error": err.Error()}) |
| 99 | } |
| 100 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 101 | dMgr.stopManagingDevice(ctx, cDevice.Id) |
| 102 | return nil |
| 103 | } |
| 104 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 105 | // DeleteAllLogicalPorts is invoked as a callback when the parent device's connection status moves to UNREACHABLE |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 106 | func (dMgr *Manager) DeleteAllLogicalPorts(ctx context.Context, parentDevice *voltha.Device) error { |
| 107 | logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"parent-device-id": parentDevice.Id}) |
| 108 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(ctx, parentDevice); err != nil { |
| 109 | // Just log error as logical device may already have been deleted |
| 110 | logger.Warnw(ctx, "delete-all-logical-ports-fail", log.Fields{"parent-device-id": parentDevice.Id, "error": err}) |
| 111 | } |
| 112 | return nil |
| 113 | } |
| 114 | |
khenaidoo | 87462ef | 2021-10-28 14:54:49 -0400 | [diff] [blame] | 115 | // DeleteAllChildDevices is invoked as a callback when the parent device is deleted. The force |
| 116 | // delete option is used in this callback because if the child device is in reconcile state then |
| 117 | // a delete request with no force option would not be sent to the child adapter, hence leaving the |
| 118 | // system in an unknown state. See https://jira.opencord.org/browse/VOL-4421 for more details. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 119 | func (dMgr *Manager) DeleteAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error { |
| 120 | logger.Debugw(ctx, "delete-all-child-devices", log.Fields{"parent-device-id": parentCurrDevice.Id}) |
khenaidoo | 87462ef | 2021-10-28 14:54:49 -0400 | [diff] [blame] | 121 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 122 | agent := dMgr.getDeviceAgent(ctx, parentCurrDevice.Id) |
| 123 | if agent == nil { |
| 124 | return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id) |
| 125 | } |
| 126 | |
nikesh.krishnan | 1e3b98a | 2023-11-26 02:39:53 +0530 | [diff] [blame] | 127 | for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, parentCurrDevice.Id) { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 128 | if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil { |
khenaidoo | 87462ef | 2021-10-28 14:54:49 -0400 | [diff] [blame] | 129 | logger.Debugw(ctx, "invoking-delete-device", log.Fields{"device-id": childDeviceID, "parent-device-id": parentCurrDevice.Id}) |
| 130 | if err := agent.deleteDeviceForce(ctx); err != nil { |
| 131 | logger.Warnw(ctx, "delete-device-force-failed", log.Fields{"device-id": childDeviceID, "parent-device-id": parentCurrDevice.Id, |
khenaidoo | 68a5e0c | 2021-11-06 13:08:03 -0400 | [diff] [blame] | 132 | "error": err}) |
| 133 | // We got an error - if its a connection error we should just mark the device as delete failed and |
| 134 | // when connection is established then proceed with the deletion instead of reconciling the device. |
| 135 | // A DeviceTransientState_DELETE_FAILED does not perform any state transition |
| 136 | if errors.Is(err, errNoConnection) { |
| 137 | if err = agent.updateTransientState(ctx, core.DeviceTransientState_DELETE_FAILED); err != nil { |
| 138 | logger.Warnw(ctx, "failed-updating-transient-state", log.Fields{"device-id": childDeviceID, "parent-device-id": parentCurrDevice.Id, |
| 139 | "error": err}) |
| 140 | } |
| 141 | logger.Debugw(ctx, "device-set-to-delete-failed", log.Fields{"device-id": childDeviceID, "parent-device-id": parentCurrDevice.Id}) |
| 142 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 143 | } |
| 144 | // No further action is required here. The deleteDevice will change the device state where the resulting |
| 145 | // callback will take care of cleaning the child device agent. |
| 146 | } |
| 147 | } |
| 148 | return nil |
| 149 | } |
| 150 | |
Joey Armstrong | 393daca | 2023-07-06 08:47:54 -0400 | [diff] [blame] | 151 | // DeleteAllDeviceFlows is invoked as a callback when the parent device's connection status moves to UNREACHABLE |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 152 | func (dMgr *Manager) DeleteAllDeviceFlows(ctx context.Context, parentDevice *voltha.Device) error { |
| 153 | logger.Debugw(ctx, "delete-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
| 154 | if agent := dMgr.getDeviceAgent(ctx, parentDevice.Id); agent != nil { |
| 155 | if err := agent.deleteAllFlows(ctx); err != nil { |
| 156 | logger.Errorw(ctx, "error-deleting-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id}) |
| 157 | return err |
| 158 | } |
| 159 | return nil |
| 160 | } |
| 161 | return status.Errorf(codes.NotFound, "%s", parentDevice.Id) |
| 162 | } |
| 163 | |
| 164 | // ChildDeviceLost calls parent adapter to delete child device and all its references |
| 165 | func (dMgr *Manager) ChildDeviceLost(ctx context.Context, curr *voltha.Device) error { |
| 166 | logger.Debugw(ctx, "child-device-lost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId}) |
| 167 | if parentAgent := dMgr.getDeviceAgent(ctx, curr.ParentId); parentAgent != nil { |
| 168 | if err := parentAgent.ChildDeviceLost(ctx, curr); err != nil { |
| 169 | // Just log the message and let the remaining pipeline proceed. |
| 170 | logger.Warnw(ctx, "childDeviceLost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId, "error": err}) |
| 171 | } |
| 172 | } |
| 173 | // Do not return an error as parent device may also have been deleted. Let the remaining pipeline proceed. |
| 174 | return nil |
| 175 | } |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 176 | |
| 177 | func (dMgr *Manager) DeleteAllLogicalMeters(ctx context.Context, cDevice *voltha.Device) error { |
| 178 | logger.Debugw(ctx, "delete-all-logical-device-meters", log.Fields{"device-id": cDevice.Id}) |
Girish Gowdra | 06a0ce2 | 2021-12-14 11:09:10 +0530 | [diff] [blame] | 179 | // Get logical device id |
| 180 | ldID, err := dMgr.logicalDeviceMgr.getLogicalDeviceIDFromDeviceID(ctx, cDevice.Id) |
| 181 | if err != nil { |
| 182 | return err |
| 183 | } |
| 184 | if err := dMgr.logicalDeviceMgr.deleteAllLogicalMetersForLogicalDevice(ctx, *ldID); err != nil { |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 185 | // Just log the error. The logical device or port may already have been deleted before this callback is invoked. |
Andrea Campanella | 832cff6 | 2021-11-05 17:05:18 +0100 | [diff] [blame] | 186 | logger.Warnw(ctx, "delete-logical-meters-error", log.Fields{"device-id": cDevice.Id, "error": err}) |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 187 | } |
| 188 | return nil |
| 189 | |
| 190 | } |