blob: 200bb9228605d08656a47de3bfcba9332c6b2888 [file] [log] [blame]
kesavandfdf77632021-01-26 23:40:33 -05001/*
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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000017//Package uniprt provides the utilities for uni port configuration
18package 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
33//UniPortStatus implements methods to get uni port status info
34type UniPortStatus struct {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000035 deviceID string
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000036 pDeviceHandler cmn.IdeviceHandler
37 pOmiCC *cmn.OmciCC
38 omciRespChn chan cmn.Message
kesavandfdf77632021-01-26 23:40:33 -050039 pLastTxMeInstance *me.ManagedEntity
40}
41
42//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
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000081func (portStatus *UniPortStatus) waitforGetUniPortStatus(ctx context.Context, apMeInstance *me.ManagedEntity) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -050082
83 select {
84 // maybe be also some outside cancel (but no context modeled for the moment ...)
85 case <-ctx.Done():
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000086 logger.Errorw(ctx, "waitforGetUniPortStatus Context done", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000087 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050088 case <-time.After(uniStatusTimeout * time.Second):
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000089 logger.Errorw(ctx, "waitforGetUniPortStatus timeout", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000090 return PostUniStatusErrResponse(extension.GetValueResponse_TIMEOUT)
kesavandfdf77632021-01-26 23:40:33 -050091
92 case omciMsg := <-portStatus.omciRespChn:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000093 if omciMsg.Type != cmn.OMCI {
94 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050095 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000096 msg, _ := omciMsg.Data.(cmn.OmciMessage)
kesavandfdf77632021-01-26 23:40:33 -050097 return portStatus.processGetUnitStatusResp(ctx, msg)
98 }
99
100}
101
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000102func (portStatus *UniPortStatus) processGetUnitStatusResp(ctx context.Context, msg cmn.OmciMessage) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500103 logger.Debugw(ctx, "processGetUniStatusResp:", log.Fields{"msg.Omci.MessageType": msg.OmciMsg.MessageType,
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000104 "msg.OmciMsg.TransactionID": msg.OmciMsg.TransactionID, "DeviceIdentfier": msg.OmciMsg.DeviceIdentifier,
105 "device-id": portStatus.deviceID})
kesavandfdf77632021-01-26 23:40:33 -0500106
107 if msg.OmciMsg.MessageType != omci.GetResponseType {
108 logger.Debugw(ctx, "processGetUniStatusResp error", log.Fields{"incorrect RespType": msg.OmciMsg.MessageType,
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000109 "expected": omci.GetResponseType, "device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000110 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500111 }
112
113 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
114 if msgLayer == nil {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000115 logger.Errorw(ctx, "processGetUniStatusResp omci Msg layer not found", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000116 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500117
118 }
119 msgObj, msgOk := msgLayer.(*omci.GetResponse)
120 if !msgOk {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000121 logger.Errorw(ctx, "processGetUniStatusResp omci msgObj layer could not be found", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000122 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500123
124 }
125
126 if msgObj.Result != me.Success {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000127 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500128 }
129 meAttributes := msgObj.Attributes
130
131 singleValResp := extension.SingleGetValueResponse{
132 Response: &extension.GetValueResponse{
133 Status: extension.GetValueResponse_OK,
134 Response: &extension.GetValueResponse_UniInfo{
135 UniInfo: &extension.GetOnuUniInfoResponse{},
136 },
137 },
138 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000139 if pptpEthUniOperState, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_OperationalState]; ok {
140 if pptpEthUniOperState.(uint8) == 0 {
141 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_ENABLED
142 } else if pptpEthUniOperState.(uint8) == 1 {
143 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_DISABLED
144 } else {
145 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
146 }
kesavandfdf77632021-01-26 23:40:33 -0500147 } else {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000148 logger.Infow(ctx, "processGetUniStatusResp - optional attribute pptpEthUniOperState not present!",
149 log.Fields{"device-id": portStatus.deviceID})
kesavandfdf77632021-01-26 23:40:33 -0500150 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
151 }
152
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000153 if pptpEthUniAdminState, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_OperationalState]; ok {
154 if pptpEthUniAdminState.(uint8) == 0 {
155 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_UNLOCKED
156 } else if pptpEthUniAdminState.(uint8) == 1 {
157 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_LOCKED
158 } else {
159 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_ADMSTATE_UNDEFINED
160 }
kesavandfdf77632021-01-26 23:40:33 -0500161 } else {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000162 logger.Errorw(ctx, "processGetUniStatusResp - mandatory attribute pptpEthUniAdminState not present!",
163 log.Fields{"device-id": portStatus.deviceID})
164 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500165 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000166
167 if pptpEthUniConfigInd, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_ConfigurationInd]; ok {
168 configIndMap := map[uint8]extension.GetOnuUniInfoResponse_ConfigurationInd{
169 0: 0,
170 1: extension.GetOnuUniInfoResponse_TEN_BASE_T_FDX,
171 2: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_FDX,
172 3: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_FDX,
173 4: extension.GetOnuUniInfoResponse_TEN_G_ETHERNET_FDX,
174 17: extension.GetOnuUniInfoResponse_TEN_BASE_T_HDX,
175 18: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_HDX,
176 19: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_HDX,
177 }
178 configInd := pptpEthUniConfigInd.(uint8)
179 singleValResp.Response.GetUniInfo().ConfigInd = configIndMap[configInd]
180 } else {
181 logger.Errorw(ctx, "processGetUniStatusResp - mandatory attribute pptpEthUniConfigInd not present!",
182 log.Fields{"device-id": portStatus.deviceID})
183 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500184 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000185
kesavandfdf77632021-01-26 23:40:33 -0500186 return &singleValResp
187}
188
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000189// PostUniStatusErrResponse - TODO: add comment
190func PostUniStatusErrResponse(reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500191 return &extension.SingleGetValueResponse{
192 Response: &extension.GetValueResponse{
193 Status: extension.GetValueResponse_ERROR,
194 ErrReason: reason,
195 },
196 }
197}