Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | |
| 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 { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 85 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
| 86 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 87 | go func(portID uint32, ctx context.Context) { |
| 88 | if err := agent.deviceMgr.logicalDeviceMgr.updatePortState(ctx, agent.deviceID, portID, operStatus); err != nil { |
| 89 | // TODO: VOL-2707 |
| 90 | logger.Warnw(ctx, "unable-to-update-logical-port-state", log.Fields{"error": err}) |
| 91 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 92 | }(portID, subCtx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | portHandle.Unlock() |
| 96 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 97 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 98 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | 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] | 102 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 103 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 104 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 105 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 106 | |
| 107 | portHandle, have := agent.portLoader.Lock(portNo) |
| 108 | if !have { |
| 109 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 110 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 111 | defer portHandle.Unlock() |
| 112 | |
| 113 | port := portHandle.GetReadOnly() |
| 114 | if port.Type != portType { |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | newPort := *port // clone top-level port struct |
| 119 | newPort.OperStatus = operStatus |
| 120 | return portHandle.Update(ctx, &newPort) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | func (agent *Agent) deleteAllPorts(ctx context.Context) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 124 | logger.Debugw(ctx, "delete-all-ports", log.Fields{"device-id": agent.deviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 125 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 126 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 127 | if err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 128 | return err |
| 129 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 130 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 131 | if device.AdminState != voltha.AdminState_DISABLED && !agent.isDeletionInProgress() { |
| 132 | err := status.Error(codes.FailedPrecondition, fmt.Sprintf("invalid-admin-state-%v", |
| 133 | device.AdminState)) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 134 | 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] | 135 | return err |
| 136 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 137 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 138 | for portID := range agent.portLoader.ListIDs() { |
| 139 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 140 | if err := portHandle.Delete(ctx); err != nil { |
| 141 | portHandle.Unlock() |
| 142 | return err |
| 143 | } |
| 144 | portHandle.Unlock() |
| 145 | } |
| 146 | } |
| 147 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | func (agent *Agent) addPort(ctx context.Context, port *voltha.Port) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 151 | logger.Debugw(ctx, "addPort", log.Fields{"device-id": agent.deviceID}) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 152 | var desc string |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 153 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 154 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 155 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 156 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 157 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 158 | port.AdminState = voltha.AdminState_ENABLED |
| 159 | |
| 160 | portHandle, created, err := agent.portLoader.LockOrCreate(ctx, port) |
| 161 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 162 | desc = err.Error() |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 163 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 164 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 165 | defer portHandle.Unlock() |
| 166 | |
| 167 | if created { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 168 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 169 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 170 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 171 | |
| 172 | oldPort := portHandle.GetReadOnly() |
| 173 | if oldPort.Label != "" || oldPort.Type != voltha.Port_PON_OLT { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 174 | logger.Debugw(ctx, "port-already-exists", log.Fields{"port": port}) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 175 | desc = fmt.Sprintf("port already exists, port : %s", port) |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 176 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 177 | return nil |
| 178 | } |
| 179 | |
| 180 | // Creation of OLT PON port is being processed after a default PON port was created. Just update it. |
| 181 | logger.Infow(ctx, "update-pon-port-created-by-default", log.Fields{"default-port": oldPort, "port-to-add": port}) |
| 182 | newPort := *oldPort // clone top-level port struct |
| 183 | newPort.Label = port.Label |
| 184 | newPort.OperStatus = port.OperStatus |
| 185 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 186 | err = portHandle.Update(ctx, &newPort) |
| 187 | if err != nil { |
| 188 | desc = err.Error() |
| 189 | return err |
| 190 | } |
| 191 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
| 192 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | func (agent *Agent) addPeerPort(ctx context.Context, peerPort *voltha.Port_PeerPort) error { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 196 | 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] | 197 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 198 | var portHandle *port.Handle |
| 199 | if agent.isRootDevice { |
| 200 | // If an ONU PON port needs to be referenced before the corresponding creation of the OLT PON port, then create the OLT PON port |
| 201 | // 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] | 202 | ponPort := &voltha.Port{ |
| 203 | PortNo: peerPort.PortNo, |
| 204 | Type: voltha.Port_PON_OLT, |
| 205 | AdminState: voltha.AdminState_ENABLED, |
| 206 | DeviceId: agent.deviceID, |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 207 | Peers: []*voltha.Port_PeerPort{peerPort}, |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 208 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 209 | |
| 210 | h, created, err := agent.portLoader.LockOrCreate(ctx, ponPort) |
| 211 | if err != nil { |
| 212 | return err |
| 213 | } |
| 214 | defer h.Unlock() |
| 215 | |
| 216 | if created { |
| 217 | logger.Infow(ctx, "added-default-pon-port", log.Fields{"device-id": agent.deviceID, "peer": peerPort, "pon-port": ponPort}) |
| 218 | return nil |
| 219 | } |
| 220 | |
| 221 | portHandle = h |
| 222 | } else { |
| 223 | h, have := agent.portLoader.Lock(peerPort.PortNo) |
| 224 | if !have { |
| 225 | return nil |
| 226 | } |
| 227 | defer h.Unlock() |
| 228 | |
| 229 | portHandle = h |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 232 | logger.Debugw(ctx, "found-peer", log.Fields{"device-id": agent.deviceID, "portNo": peerPort.PortNo, "deviceId": agent.deviceID}) |
| 233 | |
| 234 | newPort := proto.Clone(portHandle.GetReadOnly()).(*voltha.Port) |
| 235 | newPort.Peers = append(newPort.Peers, peerPort) |
| 236 | |
| 237 | return portHandle.Update(ctx, newPort) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 240 | func (agent *Agent) disablePort(ctx context.Context, portID uint32) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 241 | 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] | 242 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 243 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 244 | var desc string |
| 245 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 246 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 247 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 248 | portHandle, have := agent.portLoader.Lock(portID) |
| 249 | if !have { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 250 | err = status.Errorf(codes.InvalidArgument, "%v", portID) |
| 251 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 252 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 253 | defer portHandle.Unlock() |
| 254 | |
| 255 | oldPort := portHandle.GetReadOnly() |
| 256 | |
| 257 | if oldPort.Type != voltha.Port_PON_OLT { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 258 | err = status.Errorf(codes.Unimplemented, "disabling of Port Type %v unimplemented", oldPort.Type) |
| 259 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 262 | newPort := *oldPort |
| 263 | newPort.AdminState = voltha.AdminState_DISABLED |
| 264 | if err := portHandle.Update(ctx, &newPort); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 265 | return err |
| 266 | } |
| 267 | |
| 268 | //send request to adapter |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 269 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 270 | if err != nil { |
| 271 | return err |
| 272 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 273 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 274 | // Send the request to the adapter |
| 275 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 276 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 277 | logger.Errorw(ctx, "grpc-client-nil", |
| 278 | log.Fields{ |
| 279 | "error": err, |
| 280 | "device-id": agent.deviceID, |
| 281 | "device-type": agent.deviceType, |
| 282 | "adapter-endpoint": device.AdapterEndpoint, |
| 283 | }) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 284 | return err |
| 285 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 286 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 287 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 288 | go func() { |
| 289 | defer cancel() |
| 290 | _, err := client.DisablePort(subCtx, &newPort) |
| 291 | if err == nil { |
| 292 | agent.onSuccess(subCtx, nil, nil, true) |
| 293 | } else { |
| 294 | agent.onFailure(subCtx, err, nil, nil, true) |
| 295 | } |
| 296 | }() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 297 | return nil |
| 298 | } |
| 299 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 300 | func (agent *Agent) enablePort(ctx context.Context, portID uint32) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 301 | 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] | 302 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 303 | var err error |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 304 | var desc string |
| 305 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 306 | defer func() { agent.logDeviceUpdate(ctx, nil, nil, operStatus, err, desc) }() |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 307 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 308 | portHandle, have := agent.portLoader.Lock(portID) |
| 309 | if !have { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 310 | err = status.Errorf(codes.InvalidArgument, "%v", portID) |
| 311 | return err |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 312 | } |
| 313 | defer portHandle.Unlock() |
| 314 | |
| 315 | oldPort := portHandle.GetReadOnly() |
| 316 | |
| 317 | if oldPort.Type != voltha.Port_PON_OLT { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 318 | err = status.Errorf(codes.Unimplemented, "enabling of Port Type %v unimplemented", oldPort.Type) |
| 319 | return err |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | newPort := *oldPort |
| 323 | newPort.AdminState = voltha.AdminState_ENABLED |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 324 | if err = portHandle.Update(ctx, &newPort); err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 325 | return err |
| 326 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 327 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 328 | //send request to adapter |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 329 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 330 | if err != nil { |
| 331 | return err |
| 332 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 333 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 334 | client, err := agent.adapterMgr.GetAdapterClient(ctx, agent.adapterEndpoint) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 335 | if err != nil { |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 336 | logger.Errorw(ctx, "grpc-client-nil", |
| 337 | log.Fields{ |
| 338 | "error": err, |
| 339 | "device-id": agent.deviceID, |
| 340 | "device-type": agent.deviceType, |
| 341 | "adapter-endpoint": device.AdapterEndpoint, |
| 342 | }) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 343 | return err |
| 344 | } |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 345 | subCtx, cancel := context.WithTimeout(coreutils.WithAllMetadataFromContext(ctx), agent.rpcTimeout) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 346 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 347 | go func() { |
| 348 | defer cancel() |
| 349 | _, err := client.EnablePort(subCtx, &newPort) |
| 350 | if err == nil { |
| 351 | agent.onSuccess(subCtx, nil, nil, true) |
| 352 | } else { |
| 353 | agent.onFailure(subCtx, err, nil, nil, true) |
| 354 | } |
| 355 | }() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 356 | return nil |
| 357 | } |