blob: 9990ef6848a29888f59caf8fe8bb1e769a039217 [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"
20
21 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidood948f772021-08-11 17:49:24 -040022 "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
29func (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
45func (dMgr *Manager) DeleteLogicalDevice(ctx context.Context, cDevice *voltha.Device) error {
46 logger.Info(ctx, "delete-logical-device")
serkant.uluderyaad1e6832020-12-17 21:08:38 +030047 if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil {
khenaidood948f772021-08-11 17:49:24 -040048 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
57func (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
67func (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
81func (dMgr *Manager) RunPostDeviceDelete(ctx context.Context, cDevice *voltha.Device) error {
82 logger.Infow(ctx, "run-post-device-delete", log.Fields{"device-id": cDevice.Id})
Andrea Campanella832cff62021-11-05 17:05:18 +010083 //deleting the logical device
84 logger.Debugw(ctx, "delete-logical-device", log.Fields{"device-id": cDevice.Id})
85 if dMgr.logicalDeviceMgr != nil && cDevice.Root {
86 if err := dMgr.logicalDeviceMgr.deleteLogicalDevice(ctx, cDevice); err != nil {
87 logger.Warnw(ctx, "failure-delete-logical-device", log.Fields{"device-id": cDevice.Id, "error": err.Error()})
88 }
89 // Remove the logical device Id from the parent device
90 logicalID := ""
91 dMgr.UpdateDeviceAttribute(ctx, cDevice.Id, "ParentId", logicalID)
92 }
serkant.uluderyaad1e6832020-12-17 21:08:38 +030093 if agent := dMgr.getDeviceAgent(ctx, cDevice.Id); agent != nil {
94 logger.Debugw(ctx, "invoking-delete-device-and-ports", log.Fields{"device-id": cDevice.Id})
95 //delete ports
96 if err := agent.deleteAllPorts(ctx); err != nil {
97 logger.Warnw(ctx, "failure-delete-device-ports", log.Fields{"device-id": cDevice.Id, "error": err.Error()})
98 }
99 }
khenaidood948f772021-08-11 17:49:24 -0400100 dMgr.stopManagingDevice(ctx, cDevice.Id)
101 return nil
102}
103
104//DeleteAllLogicalPorts is invoked as a callback when the parent device's connection status moves to UNREACHABLE
105func (dMgr *Manager) DeleteAllLogicalPorts(ctx context.Context, parentDevice *voltha.Device) error {
106 logger.Debugw(ctx, "delete-all-logical-ports", log.Fields{"parent-device-id": parentDevice.Id})
107 if err := dMgr.logicalDeviceMgr.deleteAllLogicalPorts(ctx, parentDevice); err != nil {
108 // Just log error as logical device may already have been deleted
109 logger.Warnw(ctx, "delete-all-logical-ports-fail", log.Fields{"parent-device-id": parentDevice.Id, "error": err})
110 }
111 return nil
112}
113
khenaidoo87462ef2021-10-28 14:54:49 -0400114// DeleteAllChildDevices is invoked as a callback when the parent device is deleted. The force
115// delete option is used in this callback because if the child device is in reconcile state then
116// a delete request with no force option would not be sent to the child adapter, hence leaving the
117// system in an unknown state. See https://jira.opencord.org/browse/VOL-4421 for more details.
khenaidood948f772021-08-11 17:49:24 -0400118func (dMgr *Manager) DeleteAllChildDevices(ctx context.Context, parentCurrDevice *voltha.Device) error {
119 logger.Debugw(ctx, "delete-all-child-devices", log.Fields{"parent-device-id": parentCurrDevice.Id})
khenaidoo87462ef2021-10-28 14:54:49 -0400120
khenaidood948f772021-08-11 17:49:24 -0400121 agent := dMgr.getDeviceAgent(ctx, parentCurrDevice.Id)
122 if agent == nil {
123 return status.Errorf(codes.NotFound, "%s", parentCurrDevice.Id)
124 }
125
khenaidood948f772021-08-11 17:49:24 -0400126 ports, _ := dMgr.listDevicePorts(ctx, parentCurrDevice.Id)
127 for childDeviceID := range dMgr.getAllChildDeviceIds(ctx, ports) {
128 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,
132 "error": err.Error()})
khenaidood948f772021-08-11 17:49:24 -0400133 }
134 // No further action is required here. The deleteDevice will change the device state where the resulting
135 // callback will take care of cleaning the child device agent.
136 }
137 }
138 return nil
139}
140
141//DeleteAllDeviceFlows is invoked as a callback when the parent device's connection status moves to UNREACHABLE
142func (dMgr *Manager) DeleteAllDeviceFlows(ctx context.Context, parentDevice *voltha.Device) error {
143 logger.Debugw(ctx, "delete-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id})
144 if agent := dMgr.getDeviceAgent(ctx, parentDevice.Id); agent != nil {
145 if err := agent.deleteAllFlows(ctx); err != nil {
146 logger.Errorw(ctx, "error-deleting-all-device-flows", log.Fields{"parent-device-id": parentDevice.Id})
147 return err
148 }
149 return nil
150 }
151 return status.Errorf(codes.NotFound, "%s", parentDevice.Id)
152}
153
154// ChildDeviceLost calls parent adapter to delete child device and all its references
155func (dMgr *Manager) ChildDeviceLost(ctx context.Context, curr *voltha.Device) error {
156 logger.Debugw(ctx, "child-device-lost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId})
157 if parentAgent := dMgr.getDeviceAgent(ctx, curr.ParentId); parentAgent != nil {
158 if err := parentAgent.ChildDeviceLost(ctx, curr); err != nil {
159 // Just log the message and let the remaining pipeline proceed.
160 logger.Warnw(ctx, "childDeviceLost", log.Fields{"child-device-id": curr.Id, "parent-device-id": curr.ParentId, "error": err})
161 }
162 }
163 // Do not return an error as parent device may also have been deleted. Let the remaining pipeline proceed.
164 return nil
165}
serkant.uluderyaad1e6832020-12-17 21:08:38 +0300166
167func (dMgr *Manager) DeleteAllLogicalMeters(ctx context.Context, cDevice *voltha.Device) error {
168 logger.Debugw(ctx, "delete-all-logical-device-meters", log.Fields{"device-id": cDevice.Id})
169 if err := dMgr.logicalDeviceMgr.deleteAllLogicalMeters(ctx, cDevice.Id); err != nil {
170 // 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 +0100171 logger.Warnw(ctx, "delete-logical-meters-error", log.Fields{"device-id": cDevice.Id, "error": err})
serkant.uluderyaad1e6832020-12-17 21:08:38 +0300172 }
173 return nil
174
175}