blob: 53ce6afb26c2f126ac5de1149c7938eecc23392f [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001/*
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 */
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
29//
30//CreateLogicalDevice creates logical device in core
31func (dMgr *Manager) CreateLogicalDevice(ctx context.Context, cDevice *voltha.Device) error {
32 logger.Info(ctx, "create-logical-device")
33 // Verify whether the logical device has already been created
34 if cDevice.ParentId != "" {
35 logger.Debugw(ctx, "parent-device-already-exist", log.Fields{"device-id": cDevice.Id, "logical-device-id": cDevice.Id})
36 return nil
37 }
38 var err error
39 if _, err = dMgr.logicalDeviceMgr.createLogicalDevice(ctx, cDevice); err != nil {
40 logger.Warnw(ctx, "create-logical-device-error", log.Fields{"device": cDevice})
41 return err
42 }
43 return nil
44}
45
46// DeleteLogicalDevice deletes logical device from core
47func (dMgr *Manager) DeleteLogicalDevice(ctx context.Context, cDevice *voltha.Device) error {
48 logger.Info(ctx, "delete-logical-device")
serkant.uluderyaad1e6832020-12-17 21:08:38 +030049 if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil {
khenaidood948f772021-08-11 17:49:24 -040050 return err
51 }
52 // Remove the logical device Id from the parent device
53 logicalID := ""
54 dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID)
55 return nil
56}
57
58// DeleteLogicalPorts removes the logical ports associated with that deviceId
59func (dMgr *Manager) DeleteLogicalPorts(ctx context.Context, cDevice *voltha.Device) error {
60 logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"device-id": cDevice.Id})
61 if err := dMgr.logicalDeviceMgr.deleteLogicalPorts(ctx, cDevice.Id); err != nil {
62 // Just log the error. The logical device or port may already have been deleted before this callback is invoked.
63 logger.Warnw(ctx, "delete-logical-ports-error", log.Fields{"device-id": cDevice.Id, "error": err})
64 }
65 return nil
66}
67
68// SetupUNILogicalPorts creates UNI ports on the logical device that represents a child UNI interface
69func (dMgr *Manager) SetupUNILogicalPorts(ctx context.Context, cDevice *voltha.Device) error {
70 logger.Info(ctx, "setup-uni-logical-ports")
71 cDevicePorts, err := dMgr.listDevicePorts(ctx, cDevice.Id)
72 if err != nil {
73 return err
74 }
75 if err := dMgr.logicalDeviceMgr.setupUNILogicalPorts(ctx, cDevice, cDevicePorts); err != nil {
76 logger.Warnw(ctx, "setup-uni-logical-ports-error", log.Fields{"device": cDevice, "err": err})
77 return err
78 }
79 return nil
80}
81
82// RunPostDeviceDelete removes any reference of this device
83func (dMgr *Manager) RunPostDeviceDelete(ctx context.Context, cDevice *voltha.Device) error {
84 logger.Infow(ctx, "run-post-device-delete", log.Fields{"device-id": cDevice.Id})
Andrea Campanella832cff62021-11-05 17:05:18 +010085 //deleting the logical device
86 logger.Debugw(ctx, "delete-logical-device", log.Fields{"device-id": cDevice.Id})
87 if dMgr.logicalDeviceMgr != nil && cDevice.Root {
88 if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil {
89 logger.Warnw(ctx, "failure-delete-logical-device", log.Fields{"device-id": cDevice.Id, "error": err.Error()})
90 }
91 // Remove the logical device Id from the parent device
92 logicalID := ""
93 dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID)
94 }
serkant.uluderyaad1e6832020-12-17 21:08:38 +030095 if agent := dMgr.getDeviceAgent(ctx, cDevice.Id); agent != nil {
96 logger.Debugw(ctx, "invoking-delete-device-and-ports", log.Fields{"device-id": cDevice.Id})
97 //delete ports
98 if err := agent.deleteAllPorts(ctx); err != nil {
99 logger.Warnw(ctx, "failure-delete-device-ports", log.Fields{"device-id": cDevice.Id, "error": err.Error()})
100 }
101 }
khenaidood948f772021-08-11 17:49:24 -0400102 dMgr.stopManagingDevice(ctx, cDevice.Id)
103 return nil
104}
105
106//DeleteAllLogicalPorts is invoked as a callback when the parent device's connection status moves to UNREACHABLE
107func (dMgr *Manager) DeleteAllLogicalPorts(ctx context.Context, parentDevice *voltha.Device) error {
108 logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"parent-device-id": parentDevice.Id})
109 if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(ctx, parentDevice); err != nil {
110 // Just log error as logical device may already have been deleted
111 logger.Warnw(ctx, "delete-all-logical-ports-fail", log.Fields{"parent-device-id": parentDevice.Id, "error": err})
112 }
113 return nil
114}
115
khenaidoo87462ef2021-10-28 14:54:49 -0400116// DeleteAllChildDevices is invoked as a callback when the parent device is deleted. The force
117// delete option is used in this callback because if the child device is in reconcile state then
118// a delete request with no force option would not be sent to the child adapter, hence leaving the
119// system in an unknown state. See https://jira.opencord.org/browse/VOL-4421 for more details.
khenaidood948f772021-08-11 17:49:24 -0400120func (dMgr *Manager) DeleteAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error {
121 logger.Debugw(ctx, "delete-all-child-devices", log.Fields{"parent-device-id": parentCurrDevice.Id})
khenaidoo87462ef2021-10-28 14:54:49 -0400122
khenaidood948f772021-08-11 17:49:24 -0400123 agent := dMgr.getDeviceAgent(ctx, parentCurrDevice.Id)
124 if agent == nil {
125 return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id)
126 }
127
khenaidood948f772021-08-11 17:49:24 -0400128 ports, _ := dMgr.listDevicePorts(ctx, parentCurrDevice.Id)
129 for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, ports) {
130 if agent := dMgr.getDeviceAgent(ctx, childDeviceID); agent != nil {
khenaidoo87462ef2021-10-28 14:54:49 -0400131 logger.Debugw(ctx, "invoking-delete-device", log.Fields{"device-id": childDeviceID, "parent-device-id": parentCurrDevice.Id})
132 if err := agent.deleteDeviceForce(ctx); err != nil {
133 logger.Warnw(ctx, "delete-device-force-failed", log.Fields{"device-id": childDeviceID, "parent-device-id": parentCurrDevice.Id,
khenaidoo68a5e0c2021-11-06 13:08:03 -0400134 "error": err})
135 // We got an error - if its a connection error we should just mark the device as delete failed and
136 // when connection is established then proceed with the deletion instead of reconciling the device.
137 // A DeviceTransientState_DELETE_FAILED does not perform any state transition
138 if errors.Is(err, errNoConnection) {
139 if err = agent.updateTransientState(ctx, core.DeviceTransientState_DELETE_FAILED); err != nil {
140 logger.Warnw(ctx, "failed-updating-transient-state", log.Fields{"device-id": childDeviceID, "parent-device-id": parentCurrDevice.Id,
141 "error": err})
142 }
143 logger.Debugw(ctx, "device-set-to-delete-failed", log.Fields{"device-id": childDeviceID, "parent-device-id": parentCurrDevice.Id})
144 }
khenaidood948f772021-08-11 17:49:24 -0400145 }
146 // No further action is required here. The deleteDevice will change the device state where the resulting
147 // callback will take care of cleaning the child device agent.
148 }
149 }
150 return nil
151}
152
153//DeleteAllDeviceFlows is invoked as a callback when the parent device's connection status moves to UNREACHABLE
154func (dMgr *Manager) DeleteAllDeviceFlows(ctx context.Context, parentDevice *voltha.Device) error {
155 logger.Debugw(ctx, "delete-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id})
156 if agent := dMgr.getDeviceAgent(ctx, parentDevice.Id); agent != nil {
157 if err := agent.deleteAllFlows(ctx); err != nil {
158 logger.Errorw(ctx, "error-deleting-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id})
159 return err
160 }
161 return nil
162 }
163 return status.Errorf(codes.NotFound, "%s", parentDevice.Id)
164}
165
166// ChildDeviceLost calls parent adapter to delete child device and all its references
167func (dMgr *Manager) ChildDeviceLost(ctx context.Context, curr *voltha.Device) error {
168 logger.Debugw(ctx, "child-device-lost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId})
169 if parentAgent := dMgr.getDeviceAgent(ctx, curr.ParentId); parentAgent != nil {
170 if err := parentAgent.ChildDeviceLost(ctx, curr); err != nil {
171 // Just log the message and let the remaining pipeline proceed.
172 logger.Warnw(ctx, "childDeviceLost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId, "error": err})
173 }
174 }
175 // Do not return an error as parent device may also have been deleted. Let the remaining pipeline proceed.
176 return nil
177}
serkant.uluderyaad1e6832020-12-17 21:08:38 +0300178
179func (dMgr *Manager) DeleteAllLogicalMeters(ctx context.Context, cDevice *voltha.Device) error {
180 logger.Debugw(ctx, "delete-all-logical-device-meters", log.Fields{"device-id": cDevice.Id})
181 if err := dMgr.logicalDeviceMgr.deleteAllLogicalMeters(ctx, cDevice.Id); err != nil {
182 // 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 +0100183 logger.Warnw(ctx, "delete-logical-meters-error", log.Fields{"device-id": cDevice.Id, "error": err})
serkant.uluderyaad1e6832020-12-17 21:08:38 +0300184 }
185 return nil
186
187}