blob: 4c294ceb3135d6df670faeb0e7474799287cdf88 [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 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
39}
40
41//NewUniPortStatus creates a new instance of UniPortStatus
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000042func NewUniPortStatus(apDeviceHandler cmn.IdeviceHandler, apOmicc *cmn.OmciCC) *UniPortStatus {
kesavandfdf77632021-01-26 23:40:33 -050043 return &UniPortStatus{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000044 pDeviceHandler: apDeviceHandler,
45 pOmiCC: apOmicc,
46 omciRespChn: make(chan cmn.Message),
kesavandfdf77632021-01-26 23:40:33 -050047 }
48
49}
50
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000051// GetUniPortStatus - TODO: add comment
52func (portStatus *UniPortStatus) GetUniPortStatus(ctx context.Context, uniIdx uint32) *extension.SingleGetValueResponse {
53 for _, uniPort := range *portStatus.pDeviceHandler.GetUniEntityMap() {
kesavandfdf77632021-01-26 23:40:33 -050054
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000055 if uniPort.UniID == uint8(uniIdx) && uniPort.PortType == cmn.UniPPTP {
kesavandfdf77632021-01-26 23:40:33 -050056
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +000057 requestedAttributes := me.AttributeValueMap{
58 me.PhysicalPathTerminationPointEthernetUni_AdministrativeState: 0,
59 me.PhysicalPathTerminationPointEthernetUni_OperationalState: 0,
60 me.PhysicalPathTerminationPointEthernetUni_ConfigurationInd: 0}
Girish Gowdra0b235842021-03-09 13:06:46 -080061 // Note: No reference to fetch the OMCI timeout configuration value, so hard code it to 10s
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000062 meInstance, err := portStatus.pOmiCC.SendGetMe(ctx, me.PhysicalPathTerminationPointEthernetUniClassID, uniPort.EntityID, requestedAttributes, 10, true, portStatus.omciRespChn)
ozgecanetsiab36ed572021-04-01 10:38:48 +030063 if err != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000064 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
ozgecanetsiab36ed572021-04-01 10:38:48 +030065 }
66 if meInstance != nil {
kesavandfdf77632021-01-26 23:40:33 -050067 portStatus.pLastTxMeInstance = meInstance
68
69 //verify response
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000070 return portStatus.waitforGetUniPortStatus(ctx, meInstance)
kesavandfdf77632021-01-26 23:40:33 -050071 }
72 }
73 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000074 logger.Errorw(ctx, "GetUniPortStatus uniIdx is not valid", log.Fields{"uniIdx": uniIdx})
75 return PostUniStatusErrResponse(extension.GetValueResponse_INVALID_PORT_TYPE)
kesavandfdf77632021-01-26 23:40:33 -050076}
77
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000078func (portStatus *UniPortStatus) waitforGetUniPortStatus(ctx context.Context, apMeInstance *me.ManagedEntity) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -050079
80 select {
81 // maybe be also some outside cancel (but no context modeled for the moment ...)
82 case <-ctx.Done():
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000083 logger.Errorf(ctx, "waitforGetUniPortStatus Context done")
84 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050085 case <-time.After(uniStatusTimeout * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000086 logger.Errorf(ctx, "waitforGetUniPortStatus timeout")
87 return PostUniStatusErrResponse(extension.GetValueResponse_TIMEOUT)
kesavandfdf77632021-01-26 23:40:33 -050088
89 case omciMsg := <-portStatus.omciRespChn:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000090 if omciMsg.Type != cmn.OMCI {
91 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050092 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000093 msg, _ := omciMsg.Data.(cmn.OmciMessage)
kesavandfdf77632021-01-26 23:40:33 -050094 return portStatus.processGetUnitStatusResp(ctx, msg)
95 }
96
97}
98
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000099func (portStatus *UniPortStatus) processGetUnitStatusResp(ctx context.Context, msg cmn.OmciMessage) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500100 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 Hildebrandt4b5e73f2021-08-19 06:51:21 +0000106 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500107 }
108
109 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
110 if msgLayer == nil {
111 logger.Errorf(ctx, "processGetUniStatusResp omci Msg layer not found - ")
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000112 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500113
114 }
115 msgObj, msgOk := msgLayer.(*omci.GetResponse)
116 if !msgOk {
117 logger.Errorf(ctx, "processGetUniStatusResp omci msgObj layer could not be found ")
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000118 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500119
120 }
121
122 if msgObj.Result != me.Success {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000123 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500124 }
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 Hildebrandt3ac49bd2022-02-07 17:46:43 +0000135 if meAttributes[me.PhysicalPathTerminationPointEthernetUni_OperationalState].(uint8) == 0 {
kesavandfdf77632021-01-26 23:40:33 -0500136 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_ENABLED
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +0000137 } else if meAttributes[me.PhysicalPathTerminationPointEthernetUni_OperationalState].(uint8) == 1 {
kesavandfdf77632021-01-26 23:40:33 -0500138 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_DISABLED
139 } else {
140 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
141 }
142
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +0000143 if meAttributes[me.PhysicalPathTerminationPointEthernetUni_AdministrativeState].(uint8) == 0 {
kesavandfdf77632021-01-26 23:40:33 -0500144 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_UNLOCKED
Holger Hildebrandt3ac49bd2022-02-07 17:46:43 +0000145 } else if meAttributes[me.PhysicalPathTerminationPointEthernetUni_AdministrativeState].(uint8) == 1 {
kesavandfdf77632021-01-26 23:40:33 -0500146 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 Hildebrandt3ac49bd2022-02-07 17:46:43 +0000160 configInd := meAttributes[me.PhysicalPathTerminationPointEthernetUni_ConfigurationInd].(uint8)
kesavandfdf77632021-01-26 23:40:33 -0500161 singleValResp.Response.GetUniInfo().ConfigInd = configIndMap[configInd]
162 return &singleValResp
163}
164
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000165// PostUniStatusErrResponse - TODO: add comment
166func PostUniStatusErrResponse(reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500167 return &extension.SingleGetValueResponse{
168 Response: &extension.GetValueResponse{
169 Status: extension.GetValueResponse_ERROR,
170 ErrReason: reason,
171 },
172 }
173}