blob: 749293afd778ebf2d5ceff191f96df1059f96f9a [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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000064 meInstance, err := portStatus.pOmiCC.SendGetMe(ctx, me.PhysicalPathTerminationPointEthernetUniClassID, uniPort.EntityID, requestedAttributes, 10, true, portStatus.omciRespChn)
ozgecanetsiab36ed572021-04-01 10:38:48 +030065 if err != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000066 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
ozgecanetsiab36ed572021-04-01 10:38:48 +030067 }
68 if meInstance != nil {
kesavandfdf77632021-01-26 23:40:33 -050069 portStatus.pLastTxMeInstance = meInstance
70
71 //verify response
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000072 return portStatus.waitforGetUniPortStatus(ctx, meInstance)
kesavandfdf77632021-01-26 23:40:33 -050073 }
74 }
75 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000076 logger.Errorw(ctx, "GetUniPortStatus uniIdx is not valid", log.Fields{"uniIdx": uniIdx, "device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000077 return PostUniStatusErrResponse(extension.GetValueResponse_INVALID_PORT_TYPE)
kesavandfdf77632021-01-26 23:40:33 -050078}
79
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000080func (portStatus *UniPortStatus) waitforGetUniPortStatus(ctx context.Context, apMeInstance *me.ManagedEntity) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -050081
82 select {
83 // maybe be also some outside cancel (but no context modeled for the moment ...)
84 case <-ctx.Done():
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000085 logger.Errorw(ctx, "waitforGetUniPortStatus Context done", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000086 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050087 case <-time.After(uniStatusTimeout * time.Second):
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +000088 logger.Errorw(ctx, "waitforGetUniPortStatus timeout", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000089 return PostUniStatusErrResponse(extension.GetValueResponse_TIMEOUT)
kesavandfdf77632021-01-26 23:40:33 -050090
91 case omciMsg := <-portStatus.omciRespChn:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000092 if omciMsg.Type != cmn.OMCI {
93 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050094 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000095 msg, _ := omciMsg.Data.(cmn.OmciMessage)
kesavandfdf77632021-01-26 23:40:33 -050096 return portStatus.processGetUnitStatusResp(ctx, msg)
97 }
98
99}
100
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000101func (portStatus *UniPortStatus) processGetUnitStatusResp(ctx context.Context, msg cmn.OmciMessage) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500102 logger.Debugw(ctx, "processGetUniStatusResp:", log.Fields{"msg.Omci.MessageType": msg.OmciMsg.MessageType,
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000103 "msg.OmciMsg.TransactionID": msg.OmciMsg.TransactionID, "DeviceIdentfier": msg.OmciMsg.DeviceIdentifier,
104 "device-id": portStatus.deviceID})
kesavandfdf77632021-01-26 23:40:33 -0500105
106 if msg.OmciMsg.MessageType != omci.GetResponseType {
107 logger.Debugw(ctx, "processGetUniStatusResp error", log.Fields{"incorrect RespType": msg.OmciMsg.MessageType,
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000108 "expected": omci.GetResponseType, "device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000109 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500110 }
111
112 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
113 if msgLayer == nil {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000114 logger.Errorw(ctx, "processGetUniStatusResp omci Msg layer not found", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000115 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500116
117 }
118 msgObj, msgOk := msgLayer.(*omci.GetResponse)
119 if !msgOk {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000120 logger.Errorw(ctx, "processGetUniStatusResp omci msgObj layer could not be found", log.Fields{"device-id": portStatus.deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000121 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500122
123 }
124
125 if msgObj.Result != me.Success {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000126 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500127 }
128 meAttributes := msgObj.Attributes
129
130 singleValResp := extension.SingleGetValueResponse{
131 Response: &extension.GetValueResponse{
132 Status: extension.GetValueResponse_OK,
133 Response: &extension.GetValueResponse_UniInfo{
134 UniInfo: &extension.GetOnuUniInfoResponse{},
135 },
136 },
137 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000138 if pptpEthUniOperState, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_OperationalState]; ok {
139 if pptpEthUniOperState.(uint8) == 0 {
140 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_ENABLED
141 } else if pptpEthUniOperState.(uint8) == 1 {
142 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_DISABLED
143 } else {
144 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
145 }
kesavandfdf77632021-01-26 23:40:33 -0500146 } else {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000147 logger.Infow(ctx, "processGetUniStatusResp - optional attribute pptpEthUniOperState not present!",
148 log.Fields{"device-id": portStatus.deviceID})
kesavandfdf77632021-01-26 23:40:33 -0500149 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
150 }
151
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000152 if pptpEthUniAdminState, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_OperationalState]; ok {
153 if pptpEthUniAdminState.(uint8) == 0 {
154 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_UNLOCKED
155 } else if pptpEthUniAdminState.(uint8) == 1 {
156 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_LOCKED
157 } else {
158 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_ADMSTATE_UNDEFINED
159 }
kesavandfdf77632021-01-26 23:40:33 -0500160 } else {
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000161 logger.Errorw(ctx, "processGetUniStatusResp - mandatory attribute pptpEthUniAdminState not present!",
162 log.Fields{"device-id": portStatus.deviceID})
163 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500164 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000165
166 if pptpEthUniConfigInd, ok := meAttributes[me.PhysicalPathTerminationPointEthernetUni_ConfigurationInd]; ok {
167 configIndMap := map[uint8]extension.GetOnuUniInfoResponse_ConfigurationInd{
168 0: 0,
169 1: extension.GetOnuUniInfoResponse_TEN_BASE_T_FDX,
170 2: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_FDX,
171 3: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_FDX,
172 4: extension.GetOnuUniInfoResponse_TEN_G_ETHERNET_FDX,
173 17: extension.GetOnuUniInfoResponse_TEN_BASE_T_HDX,
174 18: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_HDX,
175 19: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_HDX,
176 }
177 configInd := pptpEthUniConfigInd.(uint8)
178 singleValResp.Response.GetUniInfo().ConfigInd = configIndMap[configInd]
179 } else {
180 logger.Errorw(ctx, "processGetUniStatusResp - mandatory attribute pptpEthUniConfigInd not present!",
181 log.Fields{"device-id": portStatus.deviceID})
182 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500183 }
Holger Hildebrandtfdb4bba2022-03-10 12:12:59 +0000184
kesavandfdf77632021-01-26 23:40:33 -0500185 return &singleValResp
186}
187
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000188// PostUniStatusErrResponse - TODO: add comment
189func PostUniStatusErrResponse(reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500190 return &extension.SingleGetValueResponse{
191 Response: &extension.GetValueResponse{
192 Status: extension.GetValueResponse_ERROR,
193 ErrReason: reason,
194 },
195 }
196}