blob: 7389e4554febc3a4cd7dd41f0f74066c62cf3ab0 [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001/*
Joey Armstrong7a9af442024-01-03 19:26:36 -05002* Copyright 2021-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidood948f772021-08-11 17:49:24 -04003
Joey Armstrong393daca2023-07-06 08:47:54 -04004* 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
khenaidood948f772021-08-11 17:49:24 -04007
Joey Armstrong393daca2023-07-06 08:47:54 -04008* http://www.apache.org/licenses/LICENSE-2.0
khenaidood948f772021-08-11 17:49:24 -04009
Joey Armstrong393daca2023-07-06 08:47:54 -040010* 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.
khenaidood948f772021-08-11 17:49:24 -040015 */
16package device
17
18import (
19 "context"
khenaidoo68a5e0c2021-11-06 13:08:03 -040020 "errors"
khenaidood948f772021-08-11 17:49:24 -040021
22 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo68a5e0c2021-11-06 13:08:03 -040023 "github.com/opencord/voltha-protos/v5/go/core"
khenaidood948f772021-08-11 17:49:24 -040024 "github.com/opencord/voltha-protos/v5/go/voltha"
25 "google.golang.org/grpc/codes"
26 "google.golang.org/grpc/status"
27)
28
Joey Armstrong393daca2023-07-06 08:47:54 -040029// CreateLogicalDevice creates logical device in core
khenaidood948f772021-08-11 17:49:24 -040030func (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
46func (dMgr *Manager) DeleteLogicalDevice(ctx context.Context, cDevice *voltha.Device) error {
47 logger.Info(ctx, "delete-logical-device")
serkant.uluderyaad1e6832020-12-17 21:08:38 +030048 if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil {
khenaidood948f772021-08-11 17:49:24 -040049 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
58func (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
68func (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
82func (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 Campanella832cff62021-11-05 17:05:18 +010084 //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.uluderyaad1e6832020-12-17 21:08:38 +030094 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 }
khenaidood948f772021-08-11 17:49:24 -0400101 dMgr.stopManagingDevice(ctx, cDevice.Id)
102 return nil
103}
104
Joey Armstrong393daca2023-07-06 08:47:54 -0400105// DeleteAllLogicalPorts is invoked as a callback when the parent device's connection status moves to UNREACHABLE
khenaidood948f772021-08-11 17:49:24 -0400106func (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
khenaidoo87462ef2021-10-28 14:54:49 -0400115// 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.
khenaidood948f772021-08-11 17:49:24 -0400119func (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})
khenaidoo87462ef2021-10-28 14:54:49 -0400121
khenaidood948f772021-08-11 17:49:24 -0400122 agent := dMgr.getDeviceAgent(ctx, parentCurrDevice.Id)
123 if agent == nil {
124 return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id)
125 }
126
nikesh.krishnan1e3b98a2023-11-26 02:39:53 +0530127 for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, parentCurrDevice.Id) {
khenaidood948f772021-08-11 17:49:24 -0400128 if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil {
khenaidoo87462ef2021-10-28 14:54:49 -0400129 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,
khenaidoo68a5e0c2021-11-06 13:08:03 -0400132 "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 }
khenaidood948f772021-08-11 17:49:24 -0400143 }
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 Armstrong393daca2023-07-06 08:47:54 -0400151// DeleteAllDeviceFlows is invoked as a callback when the parent device's connection status moves to UNREACHABLE
khenaidood948f772021-08-11 17:49:24 -0400152func (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
165func (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.uluderyaad1e6832020-12-17 21:08:38 +0300176
177func (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 Gowdra06a0ce22021-12-14 11:09:10 +0530179 // 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.uluderyaad1e6832020-12-17 21:08:38 +0300185 // Just log the error. The logical device or port may already have been deleted before this callback is invoked.
Andrea Campanella832cff62021-11-05 17:05:18 +0100186 logger.Warnw(ctx, "delete-logical-meters-error", log.Fields{"device-id": cDevice.Id, "error": err})
serkant.uluderyaad1e6832020-12-17 21:08:38 +0300187 }
188 return nil
189
190}