blob: b088d410ac62d7da764b81f7c29cc7f98428af19 [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
31const (
32 uniStatusTimeout = 3
33 adminState = "AdministrativeState"
34 operationalState = "OperationalState"
35 configInd = "ConfigurationInd"
36)
37
38//UniPortStatus implements methods to get uni port status info
39type UniPortStatus struct {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000040 pDeviceHandler cmn.IdeviceHandler
41 pOmiCC *cmn.OmciCC
42 omciRespChn chan cmn.Message
kesavandfdf77632021-01-26 23:40:33 -050043 pLastTxMeInstance *me.ManagedEntity
44}
45
46//NewUniPortStatus creates a new instance of UniPortStatus
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000047func NewUniPortStatus(apDeviceHandler cmn.IdeviceHandler, apOmicc *cmn.OmciCC) *UniPortStatus {
kesavandfdf77632021-01-26 23:40:33 -050048 return &UniPortStatus{
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000049 pDeviceHandler: apDeviceHandler,
50 pOmiCC: apOmicc,
51 omciRespChn: make(chan cmn.Message),
kesavandfdf77632021-01-26 23:40:33 -050052 }
53
54}
55
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000056// GetUniPortStatus - TODO: add comment
57func (portStatus *UniPortStatus) GetUniPortStatus(ctx context.Context, uniIdx uint32) *extension.SingleGetValueResponse {
58 for _, uniPort := range *portStatus.pDeviceHandler.GetUniEntityMap() {
kesavandfdf77632021-01-26 23:40:33 -050059
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000060 if uniPort.UniID == uint8(uniIdx) && uniPort.PortType == cmn.UniPPTP {
kesavandfdf77632021-01-26 23:40:33 -050061
62 requestedAttributes := me.AttributeValueMap{adminState: 0, operationalState: 0, configInd: 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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000076 logger.Errorw(ctx, "GetUniPortStatus uniIdx is not valid", log.Fields{"uniIdx": uniIdx})
77 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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000085 logger.Errorf(ctx, "waitforGetUniPortStatus Context done")
86 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -050087 case <-time.After(uniStatusTimeout * time.Second):
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000088 logger.Errorf(ctx, "waitforGetUniPortStatus timeout")
89 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,
103 "msg.OmciMsg.TransactionID": msg.OmciMsg.TransactionID, "DeviceIdentfier": msg.OmciMsg.DeviceIdentifier})
104
105 if msg.OmciMsg.MessageType != omci.GetResponseType {
106 logger.Debugw(ctx, "processGetUniStatusResp error", log.Fields{"incorrect RespType": msg.OmciMsg.MessageType,
107 "expected": omci.GetResponseType})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000108 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500109 }
110
111 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
112 if msgLayer == nil {
113 logger.Errorf(ctx, "processGetUniStatusResp omci Msg layer not found - ")
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000114 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500115
116 }
117 msgObj, msgOk := msgLayer.(*omci.GetResponse)
118 if !msgOk {
119 logger.Errorf(ctx, "processGetUniStatusResp omci msgObj layer could not be found ")
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000120 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500121
122 }
123
124 if msgObj.Result != me.Success {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000125 return PostUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
kesavandfdf77632021-01-26 23:40:33 -0500126 }
127 meAttributes := msgObj.Attributes
128
129 singleValResp := extension.SingleGetValueResponse{
130 Response: &extension.GetValueResponse{
131 Status: extension.GetValueResponse_OK,
132 Response: &extension.GetValueResponse_UniInfo{
133 UniInfo: &extension.GetOnuUniInfoResponse{},
134 },
135 },
136 }
137 if meAttributes[operationalState].(uint8) == 0 {
138 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_ENABLED
139 } else if meAttributes[operationalState].(uint8) == 1 {
140 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_DISABLED
141 } else {
142 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
143 }
144
145 if meAttributes[adminState].(uint8) == 0 {
146 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_UNLOCKED
147 } else if meAttributes[adminState].(uint8) == 1 {
148 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_LOCKED
149 } else {
150 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_ADMSTATE_UNDEFINED
151 }
152 configIndMap := map[uint8]extension.GetOnuUniInfoResponse_ConfigurationInd{
153 0: 0,
154 1: extension.GetOnuUniInfoResponse_TEN_BASE_T_FDX,
155 2: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_FDX,
156 3: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_FDX,
157 4: extension.GetOnuUniInfoResponse_TEN_G_ETHERNET_FDX,
158 17: extension.GetOnuUniInfoResponse_TEN_BASE_T_HDX,
159 18: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_HDX,
160 19: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_HDX,
161 }
162 configInd := meAttributes[configInd].(uint8)
163 singleValResp.Response.GetUniInfo().ConfigInd = configIndMap[configInd]
164 return &singleValResp
165}
166
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000167// PostUniStatusErrResponse - TODO: add comment
168func PostUniStatusErrResponse(reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse {
kesavandfdf77632021-01-26 23:40:33 -0500169 return &extension.SingleGetValueResponse{
170 Response: &extension.GetValueResponse{
171 Status: extension.GetValueResponse_ERROR,
172 ErrReason: reason,
173 },
174 }
175}