blob: ba4bdf0604e278bde7b4d278431e6a0c2ba68dbf [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
17//Package adaptercoreonu provides the utility for onu devices, flows and statistics
18package adaptercoreonu
19
20import (
21 "context"
22 "github.com/opencord/omci-lib-go"
23 me "github.com/opencord/omci-lib-go/generated"
24 "github.com/opencord/voltha-lib-go/v4/pkg/log"
25 "github.com/opencord/voltha-protos/v4/go/extension"
26 "time"
27)
28
29const (
30 uniStatusTimeout = 3
31 adminState = "AdministrativeState"
32 operationalState = "OperationalState"
33 configInd = "ConfigurationInd"
34)
35
36//UniPortStatus implements methods to get uni port status info
37type UniPortStatus struct {
38 omciRespChn chan Message
39 pOmiCC *omciCC
40 pLastTxMeInstance *me.ManagedEntity
41}
42
43//NewUniPortStatus creates a new instance of UniPortStatus
44func NewUniPortStatus(pOmicc *omciCC) *UniPortStatus {
45 return &UniPortStatus{
46 omciRespChn: make(chan Message),
47 pOmiCC: pOmicc,
48 }
49
50}
51
52func (portStatus *UniPortStatus) getUniPortStatus(ctx context.Context, uniIdx uint32) *extension.SingleGetValueResponse {
53 for _, uniPort := range portStatus.pOmiCC.pBaseDeviceHandler.uniEntityMap {
54
55 if uniPort.uniID == uint8(uniIdx) && uniPort.portType == uniPPTP {
56
57 requestedAttributes := me.AttributeValueMap{adminState: 0, operationalState: 0, configInd: 0}
58 if meInstance := portStatus.pOmiCC.sendGetMe(ctx, me.PhysicalPathTerminationPointEthernetUniClassID, uniPort.entityID, requestedAttributes, ConstDefaultOmciTimeout, true, portStatus.omciRespChn); meInstance != nil {
59 portStatus.pLastTxMeInstance = meInstance
60
61 //verify response
62 return portStatus.waitforgetUniPortStatus(ctx, meInstance)
63 }
64 }
65 }
66 logger.Errorw(ctx, "getUniPortStatus uniIdx is not valid", log.Fields{"uniIdx": uniIdx})
67 return postUniStatusErrResponse(extension.GetValueResponse_INVALID_PORT_TYPE)
68}
69
70func (portStatus *UniPortStatus) waitforgetUniPortStatus(ctx context.Context, apMeInstance *me.ManagedEntity) *extension.SingleGetValueResponse {
71
72 select {
73 // maybe be also some outside cancel (but no context modeled for the moment ...)
74 case <-ctx.Done():
75 logger.Errorf(ctx, "waitforgetUniPortStatus Context done")
76 return postUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
77 case <-time.After(uniStatusTimeout * time.Second):
78 logger.Errorf(ctx, "waitforgetUniPortStatus timeout")
79 return postUniStatusErrResponse(extension.GetValueResponse_TIMEOUT)
80
81 case omciMsg := <-portStatus.omciRespChn:
82 if omciMsg.Type != OMCI {
83 return postUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
84 }
85 msg, _ := omciMsg.Data.(OmciMessage)
86 return portStatus.processGetUnitStatusResp(ctx, msg)
87 }
88
89}
90
91func (portStatus *UniPortStatus) processGetUnitStatusResp(ctx context.Context, msg OmciMessage) *extension.SingleGetValueResponse {
92 logger.Debugw(ctx, "processGetUniStatusResp:", log.Fields{"msg.Omci.MessageType": msg.OmciMsg.MessageType,
93 "msg.OmciMsg.TransactionID": msg.OmciMsg.TransactionID, "DeviceIdentfier": msg.OmciMsg.DeviceIdentifier})
94
95 if msg.OmciMsg.MessageType != omci.GetResponseType {
96 logger.Debugw(ctx, "processGetUniStatusResp error", log.Fields{"incorrect RespType": msg.OmciMsg.MessageType,
97 "expected": omci.GetResponseType})
98 return postUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
99 }
100
101 msgLayer := (*msg.OmciPacket).Layer(omci.LayerTypeGetResponse)
102 if msgLayer == nil {
103 logger.Errorf(ctx, "processGetUniStatusResp omci Msg layer not found - ")
104 return postUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
105
106 }
107 msgObj, msgOk := msgLayer.(*omci.GetResponse)
108 if !msgOk {
109 logger.Errorf(ctx, "processGetUniStatusResp omci msgObj layer could not be found ")
110 return postUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
111
112 }
113
114 if msgObj.Result != me.Success {
115 return postUniStatusErrResponse(extension.GetValueResponse_INTERNAL_ERROR)
116 }
117 meAttributes := msgObj.Attributes
118
119 singleValResp := extension.SingleGetValueResponse{
120 Response: &extension.GetValueResponse{
121 Status: extension.GetValueResponse_OK,
122 Response: &extension.GetValueResponse_UniInfo{
123 UniInfo: &extension.GetOnuUniInfoResponse{},
124 },
125 },
126 }
127 if meAttributes[operationalState].(uint8) == 0 {
128 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_ENABLED
129 } else if meAttributes[operationalState].(uint8) == 1 {
130 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_DISABLED
131 } else {
132 singleValResp.Response.GetUniInfo().OperState = extension.GetOnuUniInfoResponse_OPERSTATE_UNDEFINED
133 }
134
135 if meAttributes[adminState].(uint8) == 0 {
136 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_UNLOCKED
137 } else if meAttributes[adminState].(uint8) == 1 {
138 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_LOCKED
139 } else {
140 singleValResp.Response.GetUniInfo().AdmState = extension.GetOnuUniInfoResponse_ADMSTATE_UNDEFINED
141 }
142 configIndMap := map[uint8]extension.GetOnuUniInfoResponse_ConfigurationInd{
143 0: 0,
144 1: extension.GetOnuUniInfoResponse_TEN_BASE_T_FDX,
145 2: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_FDX,
146 3: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_FDX,
147 4: extension.GetOnuUniInfoResponse_TEN_G_ETHERNET_FDX,
148 17: extension.GetOnuUniInfoResponse_TEN_BASE_T_HDX,
149 18: extension.GetOnuUniInfoResponse_HUNDRED_BASE_T_HDX,
150 19: extension.GetOnuUniInfoResponse_GIGABIT_ETHERNET_HDX,
151 }
152 configInd := meAttributes[configInd].(uint8)
153 singleValResp.Response.GetUniInfo().ConfigInd = configIndMap[configInd]
154 return &singleValResp
155}
156
157func postUniStatusErrResponse(reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse {
158 return &extension.SingleGetValueResponse{
159 Response: &extension.GetValueResponse{
160 Status: extension.GetValueResponse_ERROR,
161 ErrReason: reason,
162 },
163 }
164}