blob: ca580edb537467e465fef076ed2de9bcca5ed989 [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
Matteo Scandolo992a23e2021-02-04 15:35:04 -080058type args struct {
59 generatedPkt *omci.GetResponse
60 transactionId uint16
61}
Matteo Scandolof9d43412021-01-12 11:11:34 -080062
Matteo Scandolo992a23e2021-02-04 15:35:04 -080063type want struct {
64 transactionId uint16
65 attributes map[string]interface{}
66}
Matteo Scandolof9d43412021-01-12 11:11:34 -080067
Matteo Scandolo992a23e2021-02-04 15:35:04 -080068func TestGetResponse(t *testing.T) {
Matteo Scandolof9d43412021-01-12 11:11:34 -080069
Matteo Scandolo992a23e2021-02-04 15:35:04 -080070 // NOTE that we're not testing the SerialNumber attribute part of the ONU-G
71 // response here as it is a special case and it requires transformation.
72 // we specifically test that in TestCreateOnugResponse
73 sn := &openolt.SerialNumber{
74 VendorId: []byte("BBSM"),
75 VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)},
76 }
Matteo Scandolof9d43412021-01-12 11:11:34 -080077
Matteo Scandolo992a23e2021-02-04 15:35:04 -080078 tests := []struct {
79 name string
80 args args
81 want want
82 }{
83 {"getOnu2gResponse",
84 args{createOnu2gResponse(57344, 10), 1},
85 want{1, map[string]interface{}{"OpticalNetworkUnitManagementAndControlChannelOmccVersion": uint8(180)}},
86 },
87 {"getOnugResponse",
88 args{createOnugResponse(40960, 10, sn), 1},
89 want{1, map[string]interface{}{}},
90 },
91 {"getOnuDataResponse",
92 args{createOnuDataResponse(32768, 10, 129), 2},
93 want{2, map[string]interface{}{"MibDataSync": uint8(129)}},
94 },
95 }
96 for _, tt := range tests {
97 t.Run(tt.name, func(t *testing.T) {
98
99 data, _ := Serialize(omci.GetResponseType, tt.args.generatedPkt, tt.args.transactionId)
100
101 // emulate the openonu-go behavior:
102 // omci_cc.receiveMessage process the message (creates a gopacket and extracts the OMCI layer) and invokes a callback
103 // in the GetResponse case omci_cc.receiveOmciResponse
104 // then the OmciMessage (gopacket + OMIC layer) is is published on a channel
105 omciMsg, omciPkt := omciBytesToMsg(t, data)
106
107 assert.Equal(t, omciMsg.MessageType, omci.GetResponseType)
108 assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId)
109
110 // that is read by myb_sync.processMibSyncMessages
111 // the myb_sync.handleOmciMessage is called and then
112 // myb_sync.handleOmciGetResponseMessage where we extract the GetResponse layer
113 getResponseLayer := omciToGetResponse(t, omciPkt)
114
115 assert.Equal(t, getResponseLayer.Result, me.Success)
116
117 for k, v := range tt.want.attributes {
118 attr := getResponseLayer.Attributes[k]
119 assert.Equal(t, attr, v)
120 }
121 })
122 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800123}
124
125func TestCreateOnugResponse(t *testing.T) {
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800126
Matteo Scandolo5863f002021-02-08 08:08:14 -0800127 sn := &openolt.SerialNumber{
128 VendorId: []byte("BBSM"),
129 VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)},
130 }
131 response := createOnugResponse(40960, 1, sn)
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800132 data, _ := Serialize(omci.GetResponseType, response, 1)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800133
134 omciMsg, omciPkt := omciBytesToMsg(t, data)
135
136 assert.Equal(t, omciMsg.MessageType, omci.GetResponseType)
137
138 getResponseLayer := omciToGetResponse(t, omciPkt)
139
140 assert.Equal(t, getResponseLayer.Result, me.Success)
Matteo Scandolo5863f002021-02-08 08:08:14 -0800141 snBytes := (getResponseLayer.Attributes["SerialNumber"]).([]byte)
142 snVendorPart := fmt.Sprintf("%s", snBytes[:4])
143 snNumberPart := hex.EncodeToString(snBytes[4:])
144 serialNumber := snVendorPart + snNumberPart
145 assert.Equal(t, serialNumber, "BBSM00010101")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800146}