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