blob: a0b5beda48534602c3f56114779474b3e1590420 [file] [log] [blame]
kesavandfdf77632021-01-26 23:40:33 -05001/*
Joey Armstrong89c812c2024-01-12 19:00:20 -05002 * Copyright 2020-2024 Open Networking Foundation (ONF) and the ONF Contributors
kesavandfdf77632021-01-26 23:40:33 -05003 *
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
Joey Armstrong89c812c2024-01-12 19:00:20 -050017// Package uniprt provides the utilities for uni port configuration
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000018package uniprt
kesavandfdf77632021-01-26 23:40:33 -050019
20import (
21 "context"
khenaidoo7d3c5582021-08-11 18:09:44 -040022 "time"
23
mpagenko836a1fd2021-11-01 16:12:42 +000024 "github.com/opencord/omci-lib-go/v2"
25 me "github.com/opencord/omci-lib-go/v2/generated"
khenaidoo7d3c5582021-08-11 18:09:44 -040026 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000027 cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common"
khenaidoo7d3c5582021-08-11 18:09:44 -040028 "github.com/opencord/voltha-protos/v5/go/extension"
kesavandfdf77632021-01-26 23:40:33 -050029)
30
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +000031const uniStatusTimeout = 3
kesavandfdf77632021-01-26 23:40:33 -050032
Joey Armstrong89c812c2024-01-12 19:00:20 -050033// UniPortStatus implements methods to get uni port status info
kesavandfdf77632021-01-26 23:40:33 -050034type UniPortStatus struct {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000035 pDeviceHandler cmn.IdeviceHandler
36 pOmiCC *cmn.OmciCC
37 omciRespChn chan cmn.Message
kesavandfdf77632021-01-26 23:40:33 -050038 pLastTxMeInstance *me.ManagedEntity
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053039 deviceID string
kesavandfdf77632021-01-26 23:40:33 -050040}
41
Joey Armstrong89c812c2024-01-12 19:00:20 -050042// NewUniPortStatus creates a new instance of UniPortStatus
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000043func NewUniPortStatus(apDeviceHandler cmn.IdeviceHandler, apOmicc *cmn.OmciCC) *UniPortStatus {
kesavandfdf77632021-01-26 23:40:33 -050044 return &UniPortStatus{
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000045 deviceID: apDeviceHandler.GetDeviceID(),
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000046 pDeviceHandler: apDeviceHandler,
47 pOmiCC: apOmicc,
48 omciRespChn: make(chan cmn.Message),
kesavandfdf77632021-01-26 23:40:33 -050049 }
50
51}
52
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000053// GetUniPortStatus - TODO: add comment
54func (portStatus *UniPortStatus) GetUniPortStatus(ctx context.Context, uniIdx uint32) *extension.SingleGetValueResponse {
55 for _, uniPort := range *portStatus.pDeviceHandler.GetUniEntityMap() {
kesavandfdf77632021-01-26 23:40:33 -050056
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000057 if uniPort.UniID == uint8(uniIdx) && uniPort.PortType == cmn.UniPPTP {
kesavandfdf77632021-01-26 23:40:33 -050058
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +000059 requestedAttributes := me.AttributeValueMap{
60 me.PhysicalPathTerminationPointEthernetUni_AdministrativeState: 0,
61 me.PhysicalPathTerminationPointEthernetUni_OperationalState: 0,
62 me.PhysicalPathTerminationPointEthernetUni_ConfigurationInd: 0}
Girish Gowdra0b235842021-03-09 13:06:46 -080063 // Note: No reference to fetch the OMCI timeout configuration value, so hard code it to 10s
Holger Hildebrandtd930cb22022-06-17 09:24:50 +000064 meInstance, err := portStatus.pOmiCC.SendGetMe(ctx, me.PhysicalPathTerminationPointEthernetUniClassID,
65 uniPort.EntityID, requestedAttributes, 10, true, portStatus.omciRespChn, false)
ozgecanetsiab36ed572021-04-01 10:38:48 +030066 if err != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000067 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
ozgecanetsiab36ed572021-04-01 10:38:48 +030068 }
69 if meInstance != nil {
kesavandfdf77632021-01-26 23:40:33 -050070 portStatus.pLastTxMeInstance = meInstance
71
72 //verify response
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000073 return portStatus.waitforGetUniPortStatus(ctx, meInstance)
kesavandfdf77632021-01-26 23:40:33 -050074 }
75 }
76 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000077 logger.Errorw(ctx, "GetUniPortStatus uniIdx is not valid", log.Fields{"uniIdx": uniIdx, "device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000078 return PostUniStatusErrResponse(extension.GetValueResponse_INVALID_PORT_TYPE)
kesavandfdf77632021-01-26 23:40:33 -050079}
80
Akash Reddy Kankanala92dfdf82025-03-23 22:07:09 +053081//nolint:unparam // ctx and apMeInstance are required by interface
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000082func (portStatus *UniPortStatus) waitforGetUniPortStatus(ctx context.Context, apMeInstance *me.ManagedEntity) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -050083
84 select {
85 // maybe be also some outside cancel (but no context modeled for the moment ...)
86 case <-ctx.Done():
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000087 logger.Errorw(ctx, "waitforGetUniPortStatus Context done", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000088 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050089 case <-time.After(uniStatusTimeout * time.Second):
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000090 logger.Errorw(ctx, "waitforGetUniPortStatus timeout", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000091 return PostUniStatusErrResponse(extension.GetValueResponse_TIMEOUT)
kesavandfdf77632021-01-26 23:40:33 -050092
93 case omciMsg := <-portStatus.omciRespChn:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000094 if omciMsg.Type != cmn.OMCI {
95 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050096 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000097 msg, _ := omciMsg.Data.(cmn.OmciMessage)
kesavandfdf77632021-01-26 23:40:33 -050098 return portStatus.processGetUnitStatusResp(ctx, msg)
99 }
100
101}
102
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000103func (portStatus *UniPortStatus) processGetUnitStatusResp(ctx context.Context, msg cmn.OmciMessage) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500104 logger.Debugw(ctx, "processGetUniStatusResp:", log.Fields{"msg.Omci.MessageType": msg.OmciMsg.MessageType,
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000105 "msg.OmciMsg.TransactionID": msg.OmciMsg.TransactionID, "DeviceIdentfier": msg.OmciMsg.DeviceIdentifier,
106 "device-id": portStatus.deviceID})
kesavandfdf77632021-01-26 23:40:33 -0500107
108 if msg.OmciMsg.MessageType != omci.GetResponseType {
109 logger.Debugw(ctx, "processGetUniStatusResp error", log.Fields{"incorrect RespType": msg.OmciMsg.MessageType,
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000110 "expected": omci.GetResponseType, "device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000111 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500112 }
113
114 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
115 if msgLayer == nil {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000116 logger.Errorw(ctx, "processGetUniStatusResp omci Msg layer not found", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000117 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500118
119 }
120 msgObj, msgOk := msgLayer.(*omci.GetResponse)
121 if !msgOk {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000122 logger.Errorw(ctx, "processGetUniStatusResp omci msgObj layer could not be found", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000123 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500124
125 }
126
127 if msgObj.Result != me.Success {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000128 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500129 }
130 meAttributes := msgObj.Attributes
131
132 singleValResp := extension.SingleGetValueResponse{
133 Response: &extension.GetValueResponse{
134 Status: extension.GetValueResponse_OK,
135 Response: &extension.GetValueResponse_UniInfo{
136 UniInfo: &extension.GetOnuUniInfoResponse{},
137 },
138 },
139 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000140 if pptpEthUniOperState, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_OperationalState]; ok {
mgoudad611f4c2025-10-30 14:49:27 +0530141 switch pptpEthUniOperState.(uint8) {
142 case 0:
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000143 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_ENABLED
mgoudad611f4c2025-10-30 14:49:27 +0530144 case 1:
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000145 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_DISABLED
mgoudad611f4c2025-10-30 14:49:27 +0530146 default:
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000147 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
148 }
kesavandfdf77632021-01-26 23:40:33 -0500149 } else {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000150 logger.Infow(ctx, "processGetUniStatusResp - optional attribute pptpEthUniOperState not present!",
151 log.Fields{"device-id": portStatus.deviceID})
kesavandfdf77632021-01-26 23:40:33 -0500152 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
153 }
154
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000155 if pptpEthUniAdminState, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_OperationalState]; ok {
mgoudad611f4c2025-10-30 14:49:27 +0530156 switch pptpEthUniAdminState.(uint8) {
157 case 0:
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000158 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_UNLOCKED
mgoudad611f4c2025-10-30 14:49:27 +0530159 case 1:
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000160 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_LOCKED
mgoudad611f4c2025-10-30 14:49:27 +0530161 default:
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000162 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_ADMSTATE_UNDEFINED
163 }
kesavandfdf77632021-01-26 23:40:33 -0500164 } else {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000165 logger.Errorw(ctx, "processGetUniStatusResp - mandatory attribute pptpEthUniAdminState not present!",
166 log.Fields{"device-id": portStatus.deviceID})
167 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500168 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000169
170 if pptpEthUniConfigInd, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_ConfigurationInd]; ok {
171 configIndMap := map[uint8]extension.GetOnuUniInfoResponse_ConfigurationInd{
172 0: 0,
173 1: extension.GetOnuUniInfoResponse_TEN_BASE_T_FDX,
174 2: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_FDX,
175 3: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_FDX,
176 4: extension.GetOnuUniInfoResponse_TEN_G_ETHERNET_FDX,
177 17: extension.GetOnuUniInfoResponse_TEN_BASE_T_HDX,
178 18: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_HDX,
179 19: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_HDX,
180 }
181 configInd := pptpEthUniConfigInd.(uint8)
182 singleValResp.Response.GetUniInfo().ConfigInd = configIndMap[configInd]
183 } else {
184 logger.Errorw(ctx, "processGetUniStatusResp - mandatory attribute pptpEthUniConfigInd not present!",
185 log.Fields{"device-id": portStatus.deviceID})
186 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500187 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000188
kesavandfdf77632021-01-26 23:40:33 -0500189 return &singleValResp
190}
191
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000192// PostUniStatusErrResponse - TODO: add comment
193func PostUniStatusErrResponse(reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500194 return &extension.SingleGetValueResponse{
195 Response: &extension.GetValueResponse{
196 Status: extension.GetValueResponse_ERROR,
197 ErrReason: reason,
198 },
199 }
200}