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