Takahiro Suzuki | 241c10e | 2020-12-17 20:17:57 +0900 | [diff] [blame] | 1 | /* |
| 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 |
| 18 | package adaptercoreonu |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "errors" |
| 23 | |
| 24 | |
| 25 | gp "github.com/google/gopacket" |
| 26 | "github.com/opencord/omci-lib-go" |
| 27 | me "github.com/opencord/omci-lib-go/generated" |
| 28 | |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 30 | ) |
| 31 | |
| 32 | //omciTestRequest structure holds the information for the OMCI test |
| 33 | type omciTestRequest struct { |
| 34 | deviceID string |
| 35 | pDevOmciCC *omciCC |
| 36 | started bool |
| 37 | result bool |
| 38 | exclusiveCc bool |
| 39 | allowFailure bool |
| 40 | txSeqNo uint16 |
| 41 | verifyDone chan<- bool |
| 42 | } |
| 43 | |
| 44 | //newOmciTestRequest returns a new instance of OmciTestRequest |
| 45 | func newOmciTestRequest(ctx context.Context, |
| 46 | deviceID string, omciCc *omciCC, |
| 47 | exclusive bool, allowFailure bool) *omciTestRequest { |
| 48 | logger.Debug("omciTestRequest-init") |
| 49 | var omciTestRequest omciTestRequest |
| 50 | omciTestRequest.deviceID = deviceID |
| 51 | omciTestRequest.pDevOmciCC = omciCc |
| 52 | omciTestRequest.started = false |
| 53 | omciTestRequest.result = false |
| 54 | omciTestRequest.exclusiveCc = exclusive |
| 55 | omciTestRequest.allowFailure = allowFailure |
| 56 | |
| 57 | return &omciTestRequest |
| 58 | } |
| 59 | |
| 60 | // |
| 61 | func (oo *omciTestRequest) performOmciTest(ctx context.Context, execChannel chan<- bool) { |
| 62 | logger.Debug("omciTestRequest-start-test") |
| 63 | |
| 64 | if oo.pDevOmciCC != nil { |
| 65 | oo.verifyDone = execChannel |
| 66 | // test functionality is limited to ONU-2G get request for the moment |
| 67 | // without yet checking the received response automatically here (might be improved ??) |
| 68 | tid := oo.pDevOmciCC.getNextTid(false) |
| 69 | onu2gBaseGet, _ := oo.createOnu2gBaseGet(tid) |
| 70 | omciRxCallbackPair := callbackPair{ |
| 71 | cbKey: tid, |
| 72 | cbEntry: callbackPairEntry{nil, oo.receiveOmciVerifyResponse}, |
| 73 | } |
| 74 | |
| 75 | logger.Debugw("performOmciTest-start sending frame", log.Fields{"for device-id": oo.deviceID}) |
| 76 | // send with default timeout and normal prio |
| 77 | go oo.pDevOmciCC.send(ctx, onu2gBaseGet, ConstDefaultOmciTimeout, 0, false, omciRxCallbackPair) |
| 78 | |
| 79 | } else { |
| 80 | logger.Errorw("performOmciTest: Device does not exist", log.Fields{"for device-id": oo.deviceID}) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // these are OMCI related functions, could/should be collected in a separate file? TODO!!! |
| 85 | // for a simple start just included in here |
| 86 | //basic approach copied from bbsim, cmp /devices/onu.go and /internal/common/omci/mibpackets.go |
| 87 | func (oo *omciTestRequest) createOnu2gBaseGet(tid uint16) ([]byte, error) { |
| 88 | |
| 89 | request := &omci.GetRequest{ |
| 90 | MeBasePacket: omci.MeBasePacket{ |
| 91 | EntityClass: me.Onu2GClassID, |
| 92 | EntityInstance: 0, //there is only the 0 instance of ONU2-G (still hard-coded - TODO!!!) |
| 93 | }, |
| 94 | AttributeMask: 0xE000, //example hardcoded (TODO!!!) request EquId, OmccVersion, VendorCode |
| 95 | } |
| 96 | |
| 97 | oo.txSeqNo = tid |
| 98 | pkt, err := serialize(omci.GetRequestType, request, tid) |
| 99 | if err != nil { |
| 100 | //omciLogger.WithFields(log.Fields{ ... |
| 101 | logger.Errorw("Cannot serialize Onu2-G GetRequest", log.Fields{"Err": err}) |
| 102 | return nil, err |
| 103 | } |
| 104 | return pkt, nil |
| 105 | } |
| 106 | |
| 107 | //supply a response handler - in this testobject the message is evaluated directly, no response channel used |
| 108 | func (oo *omciTestRequest) receiveOmciVerifyResponse(omciMsg *omci.OMCI, packet *gp.Packet, respChan chan Message) error { |
| 109 | |
| 110 | logger.Debugw("verify-omci-message-response received:", log.Fields{"omciMsgType": omciMsg.MessageType, |
| 111 | "transCorrId": omciMsg.TransactionID, "DeviceIdent": omciMsg.DeviceIdentifier}) |
| 112 | |
| 113 | if omciMsg.TransactionID == oo.txSeqNo { |
| 114 | logger.Debugw("verify-omci-message-response", log.Fields{"correct TransCorrId": omciMsg.TransactionID}) |
| 115 | } else { |
| 116 | logger.Debugw("verify-omci-message-response error", log.Fields{"incorrect TransCorrId": omciMsg.TransactionID, |
| 117 | "expected": oo.txSeqNo}) |
| 118 | oo.verifyDone <- false |
| 119 | return errors.New("unexpected TransCorrId") |
| 120 | } |
| 121 | if omciMsg.MessageType == omci.GetResponseType { |
| 122 | logger.Debugw("verify-omci-message-response", log.Fields{"correct RespType": omciMsg.MessageType}) |
| 123 | } else { |
| 124 | logger.Debugw("verify-omci-message-response error", log.Fields{"incorrect RespType": omciMsg.MessageType, |
| 125 | "expected": omci.GetResponseType}) |
| 126 | oo.verifyDone <- false |
| 127 | return errors.New("unexpected MessageType") |
| 128 | } |
| 129 | |
| 130 | |
| 131 | oo.result = true |
| 132 | oo.verifyDone <- true |
| 133 | |
| 134 | return nil |
| 135 | } |