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" |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/v4/go/common" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 23 | |
| 24 | "github.com/gogo/protobuf/proto" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/rw_core/core/device/port" |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 26 | coreutils "github.com/opencord/voltha-go/rw_core/utils" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 28 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 29 | "google.golang.org/grpc/codes" |
| 30 | "google.golang.org/grpc/status" |
| 31 | ) |
| 32 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 33 | // listDevicePorts returns device ports |
| 34 | func (agent *Agent) listDevicePorts() map[uint32]*voltha.Port { |
| 35 | portIDs := agent.portLoader.ListIDs() |
| 36 | ports := make(map[uint32]*voltha.Port, len(portIDs)) |
| 37 | for portID := range portIDs { |
| 38 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 39 | ports[portID] = portHandle.GetReadOnly() |
| 40 | portHandle.Unlock() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | return ports |
| 44 | } |
| 45 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 46 | // getPorts retrieves the ports information of the device based on the port type. |
| 47 | 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] | 48 | 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] | 49 | ports := &voltha.Ports{} |
| 50 | for _, port := range agent.listDevicePorts() { |
| 51 | if port.Type == portType { |
| 52 | ports.Items = append(ports.Items, port) |
| 53 | } |
| 54 | } |
| 55 | return ports |
| 56 | } |
| 57 | |
| 58 | func (agent *Agent) getDevicePort(portID uint32) (*voltha.Port, error) { |
| 59 | portHandle, have := agent.portLoader.Lock(portID) |
| 60 | if !have { |
| 61 | return nil, status.Errorf(codes.NotFound, "port-%d", portID) |
| 62 | } |
| 63 | defer portHandle.Unlock() |
| 64 | return portHandle.GetReadOnly(), nil |
| 65 | } |
| 66 | |
| 67 | 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] | 68 | 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] | 69 | |
| 70 | for portID := range agent.portLoader.ListIDs() { |
| 71 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 72 | if oldPort := portHandle.GetReadOnly(); (1<<oldPort.Type)&portTypeFilter == 0 { // only update port types not included in the mask |
| 73 | // clone top-level port struct |
| 74 | newPort := *oldPort |
| 75 | newPort.OperStatus = operStatus |
| 76 | if err := portHandle.Update(ctx, &newPort); err != nil { |
| 77 | portHandle.Unlock() |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | // Notify the logical device manager to change the port state |
| 82 | // Do this for NNI and UNIs only. PON ports are not known by logical device |
| 83 | 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] | 84 | subCtx := coreutils.WithSpanAndRPCMetadataFromContext(ctx) |
| 85 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 86 | go func(portID uint32, ctx context.Context) { |
| 87 | if err := agent.deviceMgr.logicalDeviceMgr.updatePortState(ctx, agent.deviceID, portID, operStatus); err != nil { |
| 88 | // TODO: VOL-2707 |
| 89 | logger.Warnw(ctx, "unable-to-update-logical-port-state", log.Fields{"error": err}) |
| 90 | } |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 91 | }(portID, subCtx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | portHandle.Unlock() |
| 95 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 96 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 97 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | 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] | 101 | // Ensure the enums passed in are valid - they will be invalid if they are not set when this function is invoked |
| 102 | if _, ok := voltha.Port_PortType_value[portType.String()]; !ok { |
| 103 | return status.Errorf(codes.InvalidArgument, "%s", portType) |
| 104 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 105 | |
| 106 | portHandle, have := agent.portLoader.Lock(portNo) |
| 107 | if !have { |
| 108 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 109 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 110 | defer portHandle.Unlock() |
| 111 | |
| 112 | port := portHandle.GetReadOnly() |
| 113 | if port.Type != portType { |
| 114 | return nil |
| 115 | } |
| 116 | |
| 117 | newPort := *port // clone top-level port struct |
| 118 | newPort.OperStatus = operStatus |
| 119 | return portHandle.Update(ctx, &newPort) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | func (agent *Agent) deleteAllPorts(ctx context.Context) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 123 | logger.Debugw(ctx, "delete-all-ports", log.Fields{"device-id": agent.deviceID}) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 124 | |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 125 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 126 | if err != nil { |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 127 | return err |
| 128 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 129 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 130 | if device.AdminState != voltha.AdminState_DISABLED && !agent.isDeletionInProgress() { |
| 131 | err := status.Error(codes.FailedPrecondition, fmt.Sprintf("invalid-admin-state-%v", |
| 132 | device.AdminState)) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 133 | 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] | 134 | return err |
| 135 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 136 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 137 | for portID := range agent.portLoader.ListIDs() { |
| 138 | if portHandle, have := agent.portLoader.Lock(portID); have { |
| 139 | if err := portHandle.Delete(ctx); err != nil { |
| 140 | portHandle.Unlock() |
| 141 | return err |
| 142 | } |
| 143 | portHandle.Unlock() |
| 144 | } |
| 145 | } |
| 146 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | func (agent *Agent) addPort(ctx context.Context, port *voltha.Port) error { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 150 | logger.Debugw(ctx, "addPort", log.Fields{"device-id": agent.deviceID}) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 151 | var desc string |
| 152 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 153 | |
| 154 | defer agent.logDeviceUpdate(ctx, "addPort", nil, nil, operStatus, &desc) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 155 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 156 | port.AdminState = voltha.AdminState_ENABLED |
| 157 | |
| 158 | portHandle, created, err := agent.portLoader.LockOrCreate(ctx, port) |
| 159 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 160 | desc = err.Error() |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 161 | return err |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 162 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 163 | defer portHandle.Unlock() |
| 164 | |
| 165 | if created { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 166 | operStatus.Code = common.OperationResp_OPERATION_SUCCESS |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 167 | return nil |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 168 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 169 | |
| 170 | oldPort := portHandle.GetReadOnly() |
| 171 | if oldPort.Label != "" || oldPort.Type != voltha.Port_PON_OLT { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 172 | logger.Debugw(ctx, "port-already-exists", log.Fields{"port": port}) |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 173 | desc = fmt.Sprintf("port already exists, port : %s", port) |
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 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 240 | var desc string |
| 241 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 242 | |
| 243 | defer agent.logDeviceUpdate(ctx, "disablePort", nil, nil, operStatus, &desc) |
| 244 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 245 | portHandle, have := agent.portLoader.Lock(portID) |
| 246 | if !have { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 247 | desc = fmt.Sprintf("Invalid argument portID: %v", portID) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 248 | return status.Errorf(codes.InvalidArgument, "%v", portID) |
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 { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 255 | desc = fmt.Sprintf("Disabling of Port Type %v unimplemented", oldPort.Type) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 256 | return status.Errorf(codes.InvalidArgument, "Disabling of Port Type %v unimplemented", oldPort.Type) |
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 { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 262 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 263 | return err |
| 264 | } |
| 265 | |
| 266 | //send request to adapter |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 267 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 268 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 269 | desc = err.Error() |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 270 | return err |
| 271 | } |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 272 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 273 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 274 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 275 | ch, err := agent.adapterProxy.DisablePort(ctx, device, &newPort) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 276 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 277 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 278 | cancel() |
| 279 | return err |
| 280 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 281 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 282 | go agent.waitForAdapterResponseAndLogDeviceUpdate(subCtx, cancel, "disablePort", ch, agent.onSuccess, agent.onFailure, nil) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 283 | return nil |
| 284 | } |
| 285 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 286 | func (agent *Agent) enablePort(ctx context.Context, portID uint32) error { |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 287 | 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] | 288 | |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 289 | var desc string |
| 290 | operStatus := &common.OperationResp{Code: common.OperationResp_OPERATION_FAILURE} |
| 291 | |
| 292 | defer agent.logDeviceUpdate(ctx, "enablePort", nil, nil, operStatus, &desc) |
| 293 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 294 | portHandle, have := agent.portLoader.Lock(portID) |
| 295 | if !have { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 296 | desc = fmt.Sprintf("Invalid Argument portID: %v", portID) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 297 | return status.Errorf(codes.InvalidArgument, "%v", portID) |
| 298 | } |
| 299 | defer portHandle.Unlock() |
| 300 | |
| 301 | oldPort := portHandle.GetReadOnly() |
| 302 | |
| 303 | if oldPort.Type != voltha.Port_PON_OLT { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 304 | desc = fmt.Sprintf("Enabling of Port Type %v unimplemented", oldPort.Type) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 305 | return status.Errorf(codes.InvalidArgument, "Enabling of Port Type %v unimplemented", oldPort.Type) |
| 306 | } |
| 307 | |
| 308 | newPort := *oldPort |
| 309 | newPort.AdminState = voltha.AdminState_ENABLED |
| 310 | if err := portHandle.Update(ctx, &newPort); err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 311 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 312 | return err |
| 313 | } |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 314 | |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 315 | //send request to adapter |
Kent Hagerman | cba2f30 | 2020-07-28 13:37:36 -0400 | [diff] [blame] | 316 | device, err := agent.getDeviceReadOnly(ctx) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 317 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 318 | desc = err.Error() |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 319 | return err |
| 320 | } |
Rohan Agrawal | cf12f20 | 2020-08-03 04:42:01 +0000 | [diff] [blame] | 321 | subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), agent.defaultTimeout) |
Himani Chawla | b4c2591 | 2020-11-12 17:16:38 +0530 | [diff] [blame] | 322 | subCtx = coreutils.WithRPCMetadataFromContext(subCtx, ctx) |
| 323 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 324 | ch, err := agent.adapterProxy.EnablePort(ctx, device, &newPort) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 325 | if err != nil { |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 326 | desc = err.Error() |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 327 | cancel() |
| 328 | return err |
| 329 | } |
Maninder | 9a1bc0d | 2020-10-26 11:34:02 +0530 | [diff] [blame] | 330 | operStatus.Code = common.OperationResp_OPERATION_IN_PROGRESS |
| 331 | go agent.waitForAdapterResponseAndLogDeviceUpdate(subCtx, cancel, "enablePort", ch, agent.onSuccess, agent.onFailure, nil) |
Mahir Gunyel | fa6ea27 | 2020-06-10 17:03:51 -0700 | [diff] [blame] | 332 | return nil |
| 333 | } |