blob: 7ba494bb5889e1cbcbe7f30b5faae55bcf0073fb [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
2 * Copyright 2018-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
17package omci
18
19import (
Matteo Scandolo5863f002021-02-08 08:08:14 -080020 "encoding/hex"
21 "fmt"
Matteo Scandolof9d43412021-01-12 11:11:34 -080022 "github.com/google/gopacket"
23 "github.com/opencord/omci-lib-go"
24 me "github.com/opencord/omci-lib-go/generated"
Matteo Scandolo5863f002021-02-08 08:08:14 -080025 "github.com/opencord/voltha-protos/v4/go/openolt"
Matteo Scandolof9d43412021-01-12 11:11:34 -080026 "gotest.tools/assert"
27 "testing"
28)
29
30func omciBytesToMsg(t *testing.T, data []byte) (*omci.OMCI, *gopacket.Packet) {
31 packet := gopacket.NewPacket(data, omci.LayerTypeOMCI, gopacket.NoCopy)
32 if packet == nil {
33 t.Fatal("could not decode rxMsg as OMCI")
34 }
35 omciLayer := packet.Layer(omci.LayerTypeOMCI)
36 if omciLayer == nil {
37 t.Fatal("could not decode omci layer")
38 }
39 omciMsg, ok := omciLayer.(*omci.OMCI)
40 if !ok {
41 t.Fatal("could not assign omci layer")
42 }
43 return omciMsg, &packet
44}
45
46func omciToGetResponse(t *testing.T, omciPkt *gopacket.Packet) *omci.GetResponse {
47 msgLayer := (*omciPkt).Layer(omci.LayerTypeGetResponse)
48 if msgLayer == nil {
49 t.Fatal("omci Msg layer could not be detected for GetResponse - handling of MibSyncChan stopped")
50 }
51 msgObj, msgOk := msgLayer.(*omci.GetResponse)
52 if !msgOk {
53 t.Fatal("omci Msg layer could not be assigned for GetResponse - handling of MibSyncChan stopped")
54 }
55 return msgObj
56}
57
58func TestCreateOnu2gResponse(t *testing.T) {
59 response := createOnu2gResponse(40960, 1)
60 data, _ := serialize(omci.GetResponseType, response, 1)
61
62 // emulate the openonu-go behavior:
63 // omci_cc.receiveMessage process the message (creates a gopacket and extracts the OMCI layer) and invokes a callback
64 // in the GetResponse case omci_cc.receiveOmciResponse
65 // then the OmciMessage (gopacket + OMIC layer) is is published on a channel
66 omciMsg, omciPkt := omciBytesToMsg(t, data)
67
68 assert.Equal(t, omciMsg.MessageType, omci.GetResponseType)
69
70 // that is read by myb_sync.processMibSyncMessages
71 // the myb_sync.handleOmciMessage is called and then
72 // myb_sync.handleOmciGetResponseMessage where we extract the GetResponse layer
73 getResponseLayer := omciToGetResponse(t, omciPkt)
74
75 assert.Equal(t, getResponseLayer.Result, me.Success)
76}
77
78func TestCreateOnugResponse(t *testing.T) {
Matteo Scandolo5863f002021-02-08 08:08:14 -080079 sn := &openolt.SerialNumber{
80 VendorId: []byte("BBSM"),
81 VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)},
82 }
83 response := createOnugResponse(40960, 1, sn)
Matteo Scandolof9d43412021-01-12 11:11:34 -080084 data, _ := serialize(omci.GetResponseType, response, 1)
85
86 omciMsg, omciPkt := omciBytesToMsg(t, data)
87
88 assert.Equal(t, omciMsg.MessageType, omci.GetResponseType)
89
90 getResponseLayer := omciToGetResponse(t, omciPkt)
91
92 assert.Equal(t, getResponseLayer.Result, me.Success)
Matteo Scandolo5863f002021-02-08 08:08:14 -080093 snBytes := (getResponseLayer.Attributes["SerialNumber"]).([]byte)
94 snVendorPart := fmt.Sprintf("%s", snBytes[:4])
95 snNumberPart := hex.EncodeToString(snBytes[4:])
96 serialNumber := snVendorPart + snNumberPart
97 assert.Equal(t, serialNumber, "BBSM00010101")
Matteo Scandolof9d43412021-01-12 11:11:34 -080098}