kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 17 | //Package uniprt provides the utilities for uni port configuration |
| 18 | package uniprt |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 19 | |
| 20 | import ( |
| 21 | "context" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 22 | "time" |
| 23 | |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 24 | "github.com/opencord/omci-lib-go" |
| 25 | me "github.com/opencord/omci-lib-go/generated" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 27 | cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common" |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v5/go/extension" |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | uniStatusTimeout = 3 |
| 33 | adminState = "AdministrativeState" |
| 34 | operationalState = "OperationalState" |
| 35 | configInd = "ConfigurationInd" |
| 36 | ) |
| 37 | |
| 38 | //UniPortStatus implements methods to get uni port status info |
| 39 | type UniPortStatus struct { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 40 | pDeviceHandler cmn.IdeviceHandler |
| 41 | pOmiCC *cmn.OmciCC |
| 42 | omciRespChn chan cmn.Message |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 43 | pLastTxMeInstance *me.ManagedEntity |
| 44 | } |
| 45 | |
| 46 | //NewUniPortStatus creates a new instance of UniPortStatus |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 47 | func NewUniPortStatus(apDeviceHandler cmn.IdeviceHandler, apOmicc *cmn.OmciCC) *UniPortStatus { |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 48 | return &UniPortStatus{ |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 49 | pDeviceHandler: apDeviceHandler, |
| 50 | pOmiCC: apOmicc, |
| 51 | omciRespChn: make(chan cmn.Message), |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | } |
| 55 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 56 | // GetUniPortStatus - TODO: add comment |
| 57 | func (portStatus *UniPortStatus) GetUniPortStatus(ctx context.Context, uniIdx uint32) *extension.SingleGetValueResponse { |
| 58 | for _, uniPort := range *portStatus.pDeviceHandler.GetUniEntityMap() { |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 59 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 60 | if uniPort.UniID == uint8(uniIdx) && uniPort.PortType == cmn.UniPPTP { |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 61 | |
| 62 | requestedAttributes := me.AttributeValueMap{adminState: 0, operationalState: 0, configInd: 0} |
Girish Gowdra | 0b23584 | 2021-03-09 13:06:46 -0800 | [diff] [blame] | 63 | // Note: No reference to fetch the OMCI timeout configuration value, so hard code it to 10s |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 64 | meInstance, err := portStatus.pOmiCC.SendGetMe(ctx, me.PhysicalPathTerminationPointEthernetUniClassID, uniPort.EntityID, requestedAttributes, 10, true, portStatus.omciRespChn) |
ozgecanetsia | b36ed57 | 2021-04-01 10:38:48 +0300 | [diff] [blame] | 65 | if err != nil { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 66 | return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR) |
ozgecanetsia | b36ed57 | 2021-04-01 10:38:48 +0300 | [diff] [blame] | 67 | } |
| 68 | if meInstance != nil { |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 69 | portStatus.pLastTxMeInstance = meInstance |
| 70 | |
| 71 | //verify response |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 72 | return portStatus.waitforGetUniPortStatus(ctx, meInstance) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 76 | logger.Errorw(ctx, "GetUniPortStatus uniIdx is not valid", log.Fields{"uniIdx": uniIdx}) |
| 77 | return PostUniStatusErrResponse(extension.GetValueResponse_INVALID_PORT_TYPE) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 80 | func (portStatus *UniPortStatus) waitforGetUniPortStatus(ctx context.Context, apMeInstance *me.ManagedEntity) *extension.SingleGetValueResponse { |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 81 | |
| 82 | select { |
| 83 | // maybe be also some outside cancel (but no context modeled for the moment ...) |
| 84 | case <-ctx.Done(): |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 85 | logger.Errorf(ctx, "waitforGetUniPortStatus Context done") |
| 86 | return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 87 | case <-time.After(uniStatusTimeout * time.Second): |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 88 | logger.Errorf(ctx, "waitforGetUniPortStatus timeout") |
| 89 | return PostUniStatusErrResponse(extension.GetValueResponse_TIMEOUT) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 90 | |
| 91 | case omciMsg := <-portStatus.omciRespChn: |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 92 | if omciMsg.Type != cmn.OMCI { |
| 93 | return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 94 | } |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 95 | msg, _ := omciMsg.Data.(cmn.OmciMessage) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 96 | return portStatus.processGetUnitStatusResp(ctx, msg) |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 101 | func (portStatus *UniPortStatus) processGetUnitStatusResp(ctx context.Context, msg cmn.OmciMessage) *extension.SingleGetValueResponse { |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 102 | logger.Debugw(ctx, "processGetUniStatusResp:", log.Fields{"msg.Omci.MessageType": msg.OmciMsg.MessageType, |
| 103 | "msg.OmciMsg.TransactionID": msg.OmciMsg.TransactionID, "DeviceIdentfier": msg.OmciMsg.DeviceIdentifier}) |
| 104 | |
| 105 | if msg.OmciMsg.MessageType != omci.GetResponseType { |
| 106 | logger.Debugw(ctx, "processGetUniStatusResp error", log.Fields{"incorrect RespType": msg.OmciMsg.MessageType, |
| 107 | "expected": omci.GetResponseType}) |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 108 | return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse) |
| 112 | if msgLayer == nil { |
| 113 | logger.Errorf(ctx, "processGetUniStatusResp omci Msg layer not found - ") |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 114 | return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 115 | |
| 116 | } |
| 117 | msgObj, msgOk := msgLayer.(*omci.GetResponse) |
| 118 | if !msgOk { |
| 119 | logger.Errorf(ctx, "processGetUniStatusResp omci msgObj layer could not be found ") |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 120 | return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 121 | |
| 122 | } |
| 123 | |
| 124 | if msgObj.Result != me.Success { |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 125 | return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR) |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 126 | } |
| 127 | meAttributes := msgObj.Attributes |
| 128 | |
| 129 | singleValResp := extension.SingleGetValueResponse{ |
| 130 | Response: &extension.GetValueResponse{ |
| 131 | Status: extension.GetValueResponse_OK, |
| 132 | Response: &extension.GetValueResponse_UniInfo{ |
| 133 | UniInfo: &extension.GetOnuUniInfoResponse{}, |
| 134 | }, |
| 135 | }, |
| 136 | } |
| 137 | if meAttributes[operationalState].(uint8) == 0 { |
| 138 | singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_ENABLED |
| 139 | } else if meAttributes[operationalState].(uint8) == 1 { |
| 140 | singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_DISABLED |
| 141 | } else { |
| 142 | singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED |
| 143 | } |
| 144 | |
| 145 | if meAttributes[adminState].(uint8) == 0 { |
| 146 | singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_UNLOCKED |
| 147 | } else if meAttributes[adminState].(uint8) == 1 { |
| 148 | singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_LOCKED |
| 149 | } else { |
| 150 | singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_ADMSTATE_UNDEFINED |
| 151 | } |
| 152 | configIndMap := map[uint8]extension.GetOnuUniInfoResponse_ConfigurationInd{ |
| 153 | 0: 0, |
| 154 | 1: extension.GetOnuUniInfoResponse_TEN_BASE_T_FDX, |
| 155 | 2: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_FDX, |
| 156 | 3: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_FDX, |
| 157 | 4: extension.GetOnuUniInfoResponse_TEN_G_ETHERNET_FDX, |
| 158 | 17: extension.GetOnuUniInfoResponse_TEN_BASE_T_HDX, |
| 159 | 18: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_HDX, |
| 160 | 19: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_HDX, |
| 161 | } |
| 162 | configInd := meAttributes[configInd].(uint8) |
| 163 | singleValResp.Response.GetUniInfo().ConfigInd = configIndMap[configInd] |
| 164 | return &singleValResp |
| 165 | } |
| 166 | |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 167 | // PostUniStatusErrResponse - TODO: add comment |
| 168 | func PostUniStatusErrResponse(reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse { |
kesavand | fdf7763 | 2021-01-26 23:40:33 -0500 | [diff] [blame] | 169 | return &extension.SingleGetValueResponse{ |
| 170 | Response: &extension.GetValueResponse{ |
| 171 | Status: extension.GetValueResponse_ERROR, |
| 172 | ErrReason: reason, |
| 173 | }, |
| 174 | } |
| 175 | } |