Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 5f51f2e | 2023-01-17 17:06:26 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 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 | |
| 17 | package device |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 22 | |
| 23 | "github.com/opencord/voltha-protos/v5/go/common" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 24 | |
| 25 | "github.com/gogo/protobuf/proto" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/rw_core/core/device/port" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 27 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 29 | "github.com/opencord/voltha-protos/v5/go/voltha" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 30 | "google.golang.org/grpc/codes" |
| 31 | "google.golang.org/grpc/status" |
| 32 | ) |
| 33 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 34 | // listDevicePorts returns device ports |
| 35 | func (agent *Agent) listDevicePorts() map[uint32]*voltha.Port { |
| 36 | portIDs := agent.portLoader.ListIDs() |
| 37 | ports := make(map[uint32]*voltha.Port, len(portIDs)) |
| 38 | for portID := range portIDs { |
| 39 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 40 | ports[portID] = portHandle.GetReadOnly() |
| 41 | portHandle.Unlock() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | return ports |
| 45 | } |
| 46 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 47 | // getPorts retrieves the ports information of the device based on the port type. |
| 48 | func (agent *Agent) getPorts(ctx context.Context, portType voltha.Port_PortType) *voltha.Ports { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 49 | logger.Debugw(ctx, "get-ports", log.Fields{"device-id": agent.deviceID, "port-type": portType}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 50 | ports := &voltha.Ports{} |
| 51 | for _, port := range agent.listDevicePorts() { |
| 52 | if port.Type == portType { |
| 53 | ports.Items = append(ports.Items, port) |
| 54 | } |
| 55 | } |
| 56 | return ports |
| 57 | } |
| 58 | |
| 59 | func (agent *Agent) getDevicePort(portID uint32) (*voltha.Port, error) { |
| 60 | portHandle, have := agent.portLoader.Lock(portID) |
| 61 | if !have { |
| 62 | return nil, status.Errorf(codes.NotFound, "port-%d", portID) |
| 63 | } |
| 64 | defer portHandle.Unlock() |
| 65 | return portHandle.GetReadOnly(), nil |
| 66 | } |
| 67 | |
| 68 | func (agent *Agent) updatePortsOperState(ctx context.Context, portTypeFilter uint32, operStatus voltha.OperStatus_Types) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 69 | logger.Debugw(ctx, "update-ports-oper-state", log.Fields{"device-id": agent.deviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 70 | |
| 71 | for portID := range agent.portLoader.ListIDs() { |
| 72 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 73 | if oldPort := portHandle.GetReadOnly(); (1<<oldPort.Type)&portTypeFilter == 0 { // only update port types not included in the mask |
| 74 | // clone top-level port struct |
| 75 | newPort := *oldPort |
| 76 | newPort.OperStatus = operStatus |
| 77 | if err := portHandle.Update(ctx, &newPort); err != nil { |
| 78 | portHandle.Unlock() |
| 79 | return err |
| 80 | } |
| 81 | |
| 82 | // Notify the logical device manager to change the port state |
| 83 | // Do this for NNI and UNIs only. PON ports are not known by logical device |
| 84 | if newPort.Type == voltha.Port_ETHERNET_NNI || newPort.Type == voltha.Port_ETHERNET_UNI { |
nikesh.krishnan | 95142d5 | 2023-02-24 15:32:11 +0530 | [diff] [blame^] | 85 | if err := agent.deviceMgr.logicalDeviceMgr.updatePortState(ctx, agent.deviceID, portID, operStatus); err != nil { |
| 86 | // TODO: VOL-2707 |
| 87 | logger.Warnw(ctx, "unable-to-update-logical-port-state", log.Fields{"error": err}) |
| 88 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 89 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | portHandle.Unlock() |
| 93 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 94 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 95 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func (agent *Agent) updatePortState(ctx context.Context, portType voltha.Port_PortType, portNo uint32, operStatus voltha.OperStatus_Types) error { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 99 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 100 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 101 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 102 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 103 | |
| 104 | portHandle, have := agent.portLoader.Lock(portNo) |
| 105 | if !have { |
| 106 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 107 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 108 | defer portHandle.Unlock() |
| 109 | |
| 110 | port := portHandle.GetReadOnly() |
| 111 | if port.Type != portType { |
| 112 | return nil |
| 113 | } |
| 114 | |
| 115 | newPort := *port // clone top-level port struct |
| 116 | newPort.OperStatus = operStatus |
| 117 | return portHandle.Update(ctx, &newPort) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | func (agent *Agent) deleteAllPorts(ctx context.Context) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 121 | logger.Debugw(ctx, "delete-all-ports", log.Fields{"device-id": agent.deviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 122 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 123 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 124 | if err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 125 | return err |
| 126 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 127 | |
serkant.uluderya | ad1e683 | 2020-12-17 21:08:38 +0300 | [diff] [blame] | 128 | if !agent.isDeletionInProgress() { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 129 | err := status.Error(codes.FailedPrecondition, fmt.Sprintf("invalid-admin-state-%v", |
| 130 | device.AdminState)) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 131 | logger.Warnw(ctx, "invalid-state-removing-ports", log.Fields{"state": device.AdminState, "error": err}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 132 | return err |
| 133 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 134 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 135 | for portID := range agent.portLoader.ListIDs() { |
| 136 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 137 | if err := portHandle.Delete(ctx); err != nil { |
| 138 | portHandle.Unlock() |
| 139 | return err |
| 140 | } |
| 141 | portHandle.Unlock() |
| 142 | } |
| 143 | } |
| 144 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | func (agent *Agent) addPort(ctx context.Context, port *voltha.Port) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 148 | logger.Debugw(ctx, "addPort", log.Fields{"device-id": agent.deviceID}) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 149 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 150 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 151 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 152 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 153 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 154 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 155 | port.AdminState = voltha.AdminState_ENABLED |
| 156 | |
| 157 | portHandle, created, err := agent.portLoader.LockOrCreate(ctx, port) |
| 158 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 159 | desc = err.Error() |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 160 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 161 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 162 | defer portHandle.Unlock() |
| 163 | |
| 164 | if created { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 165 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 166 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 167 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 168 | |
| 169 | oldPort := portHandle.GetReadOnly() |
| 170 | if oldPort.Label != "" || oldPort.Type != voltha.Port_PON_OLT { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 171 | logger.Debugw(ctx, "port-already-exists", log.Fields{"port": port}) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 172 | desc = fmt.Sprintf("port already exists, port : %s", port) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 173 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 174 | return nil |
| 175 | } |
| 176 | |
| 177 | // Creation of OLT PON port is being processed after a default PON port was created. Just update it. |
| 178 | logger.Infow(ctx, "update-pon-port-created-by-default", log.Fields{"default-port": oldPort, "port-to-add": port}) |
| 179 | newPort := *oldPort // clone top-level port struct |
| 180 | newPort.Label = port.Label |
| 181 | newPort.OperStatus = port.OperStatus |
| 182 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 183 | err = portHandle.Update(ctx, &newPort) |
| 184 | if err != nil { |
| 185 | desc = err.Error() |
| 186 | return err |
| 187 | } |
| 188 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
| 189 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | func (agent *Agent) addPeerPort(ctx context.Context, peerPort *voltha.Port_PeerPort) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 193 | logger.Debugw(ctx, "adding-peer-peerPort", log.Fields{"device-id": agent.deviceID, "peer-peerPort": peerPort}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 194 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 195 | var portHandle *port.Handle |
| 196 | if agent.isRootDevice { |
| 197 | // If an ONU PON port needs to be referenced before the corresponding creation of the OLT PON port, then create the OLT PON port |
| 198 | // with default values, and update it later when the OLT PON port creation is processed. |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 199 | ponPort := &voltha.Port{ |
| 200 | PortNo: peerPort.PortNo, |
| 201 | Type: voltha.Port_PON_OLT, |
| 202 | AdminState: voltha.AdminState_ENABLED, |
| 203 | DeviceId: agent.deviceID, |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 204 | Peers: []*voltha.Port_PeerPort{peerPort}, |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 205 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 206 | |
| 207 | h, created, err := agent.portLoader.LockOrCreate(ctx, ponPort) |
| 208 | if err != nil { |
| 209 | return err |
| 210 | } |
| 211 | defer h.Unlock() |
| 212 | |
| 213 | if created { |
| 214 | logger.Infow(ctx, "added-default-pon-port", log.Fields{"device-id": agent.deviceID, "peer": peerPort, "pon-port": ponPort}) |
| 215 | return nil |
| 216 | } |
| 217 | |
| 218 | portHandle = h |
| 219 | } else { |
| 220 | h, have := agent.portLoader.Lock(peerPort.PortNo) |
| 221 | if !have { |
| 222 | return nil |
| 223 | } |
| 224 | defer h.Unlock() |
| 225 | |
| 226 | portHandle = h |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 229 | logger.Debugw(ctx, "found-peer", log.Fields{"device-id": agent.deviceID, "portNo": peerPort.PortNo, "deviceId": agent.deviceID}) |
| 230 | |
| 231 | newPort := proto.Clone(portHandle.GetReadOnly()).(*voltha.Port) |
| 232 | newPort.Peers = append(newPort.Peers, peerPort) |
| 233 | |
| 234 | return portHandle.Update(ctx, newPort) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 237 | func (agent *Agent) disablePort(ctx context.Context, portID uint32) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 238 | logger.Debugw(ctx, "disable-port", log.Fields{"device-id": agent.deviceID, "port-no": portID}) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 239 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 240 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 241 | var desc string |
| 242 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 243 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 244 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 245 | portHandle, have := agent.portLoader.Lock(portID) |
| 246 | if !have { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 247 | err = status.Errorf(codes.InvalidArgument, "%v", portID) |
| 248 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 249 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 250 | defer portHandle.Unlock() |
| 251 | |
| 252 | oldPort := portHandle.GetReadOnly() |
| 253 | |
| 254 | if oldPort.Type != voltha.Port_PON_OLT { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 255 | err = status.Errorf(codes.Unimplemented, "disabling of Port Type %v unimplemented", oldPort.Type) |
| 256 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 259 | newPort := *oldPort |
| 260 | newPort.AdminState = voltha.AdminState_DISABLED |
| 261 | if err := portHandle.Update(ctx, &newPort); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 262 | return err |
| 263 | } |
| 264 | |
| 265 | //send request to adapter |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 266 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 267 | if err != nil { |
| 268 | return err |
| 269 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 270 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 271 | // Send the request to the adapter |
| 272 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 273 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 274 | logger.Errorw(ctx, "grpc-client-nil", |
| 275 | log.Fields{ |
| 276 | "error": err, |
| 277 | "device-id": agent.deviceID, |
| 278 | "device-type": agent.deviceType, |
| 279 | "adapter-endpoint": device.AdapterEndpoint, |
| 280 | }) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 281 | return err |
| 282 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 283 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 284 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 285 | go func() { |
| 286 | defer cancel() |
| 287 | _, err := client.DisablePort(subCtx, &newPort) |
| 288 | if err == nil { |
| 289 | agent.onSuccess(subCtx, nil, nil, true) |
| 290 | } else { |
| 291 | agent.onFailure(subCtx, err, nil, nil, true) |
| 292 | } |
| 293 | }() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 294 | return nil |
| 295 | } |
| 296 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 297 | func (agent *Agent) enablePort(ctx context.Context, portID uint32) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 298 | logger.Debugw(ctx, "enable-port", log.Fields{"device-id": agent.deviceID, "port-no": portID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 299 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 300 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 301 | var desc string |
| 302 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 303 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 304 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 305 | portHandle, have := agent.portLoader.Lock(portID) |
| 306 | if !have { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 307 | err = status.Errorf(codes.InvalidArgument, "%v", portID) |
| 308 | return err |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 309 | } |
| 310 | defer portHandle.Unlock() |
| 311 | |
| 312 | oldPort := portHandle.GetReadOnly() |
| 313 | |
| 314 | if oldPort.Type != voltha.Port_PON_OLT { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 315 | err = status.Errorf(codes.Unimplemented, "enabling of Port Type %v unimplemented", oldPort.Type) |
| 316 | return err |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | newPort := *oldPort |
| 320 | newPort.AdminState = voltha.AdminState_ENABLED |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 321 | if err = portHandle.Update(ctx, &newPort); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 322 | return err |
| 323 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 324 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 325 | //send request to adapter |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 326 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 327 | if err != nil { |
| 328 | return err |
| 329 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 330 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 331 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 332 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 333 | logger.Errorw(ctx, "grpc-client-nil", |
| 334 | log.Fields{ |
| 335 | "error": err, |
| 336 | "device-id": agent.deviceID, |
| 337 | "device-type": agent.deviceType, |
| 338 | "adapter-endpoint": device.AdapterEndpoint, |
| 339 | }) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 340 | return err |
| 341 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 342 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 343 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 344 | go func() { |
| 345 | defer cancel() |
| 346 | _, err := client.EnablePort(subCtx, &newPort) |
| 347 | if err == nil { |
| 348 | agent.onSuccess(subCtx, nil, nil, true) |
| 349 | } else { |
| 350 | agent.onFailure(subCtx, err, nil, nil, true) |
| 351 | } |
| 352 | }() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 353 | return nil |
| 354 | } |