blob: cffb80d6a9ad3dfe16cad82832ee892b894d6036 [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"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000025 "github.com/opencord/voltha-protos/v5/go/openolt"
Matteo Scandolof9d43412021-01-12 11:11:34 -080026 "gotest.tools/assert"
Matteo Scandoloc00e97a2021-05-27 11:45:27 -070027 "reflect"
Matteo Scandolof9d43412021-01-12 11:11:34 -080028 "testing"
29)
30
31func omciBytesToMsg(t *testing.T, data []byte) (*omci.OMCI, *gopacket.Packet) {
32 packet := gopacket.NewPacket(data, omci.LayerTypeOMCI, gopacket.NoCopy)
33 if packet == nil {
34 t.Fatal("could not decode rxMsg as OMCI")
35 }
36 omciLayer := packet.Layer(omci.LayerTypeOMCI)
37 if omciLayer == nil {
38 t.Fatal("could not decode omci layer")
39 }
40 omciMsg, ok := omciLayer.(*omci.OMCI)
41 if !ok {
42 t.Fatal("could not assign omci layer")
43 }
44 return omciMsg, &packet
45}
46
47func omciToGetResponse(t *testing.T, omciPkt *gopacket.Packet) *omci.GetResponse {
48 msgLayer := (*omciPkt).Layer(omci.LayerTypeGetResponse)
49 if msgLayer == nil {
50 t.Fatal("omci Msg layer could not be detected for GetResponse - handling of MibSyncChan stopped")
51 }
52 msgObj, msgOk := msgLayer.(*omci.GetResponse)
53 if !msgOk {
54 t.Fatal("omci Msg layer could not be assigned for GetResponse - handling of MibSyncChan stopped")
55 }
56 return msgObj
57}
58
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080059type getArgs struct {
Matteo Scandolo992a23e2021-02-04 15:35:04 -080060 generatedPkt *omci.GetResponse
61 transactionId uint16
62}
Matteo Scandolof9d43412021-01-12 11:11:34 -080063
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080064type getWant struct {
Matteo Scandolo992a23e2021-02-04 15:35:04 -080065 transactionId uint16
66 attributes map[string]interface{}
67}
Matteo Scandolof9d43412021-01-12 11:11:34 -080068
Matteo Scandolo992a23e2021-02-04 15:35:04 -080069func TestGetResponse(t *testing.T) {
Matteo Scandolof9d43412021-01-12 11:11:34 -080070
Matteo Scandolo992a23e2021-02-04 15:35:04 -080071 // NOTE that we're not testing the SerialNumber attribute part of the ONU-G
72 // response here as it is a special case and it requires transformation.
73 // we specifically test that in TestCreateOnugResponse
74 sn := &openolt.SerialNumber{
75 VendorId: []byte("BBSM"),
76 VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)},
77 }
Matteo Scandolof9d43412021-01-12 11:11:34 -080078
Matteo Scandolo992a23e2021-02-04 15:35:04 -080079 tests := []struct {
80 name string
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080081 args getArgs
82 want getWant
Matteo Scandolo992a23e2021-02-04 15:35:04 -080083 }{
84 {"getOnu2gResponse",
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080085 getArgs{createOnu2gResponse(57344, 10), 1},
86 getWant{1, map[string]interface{}{"OpticalNetworkUnitManagementAndControlChannelOmccVersion": uint8(180)}},
Matteo Scandolo992a23e2021-02-04 15:35:04 -080087 },
88 {"getOnugResponse",
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080089 getArgs{createOnugResponse(40960, 10, sn), 1},
90 getWant{1, map[string]interface{}{}},
Matteo Scandolo992a23e2021-02-04 15:35:04 -080091 },
92 {"getOnuDataResponse",
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080093 getArgs{createOnuDataResponse(32768, 10, 129), 2},
94 getWant{2, map[string]interface{}{"MibDataSync": uint8(129)}},
Matteo Scandolo992a23e2021-02-04 15:35:04 -080095 },
Matteo Scandolo78d5ee12021-04-14 11:11:32 -070096 {"getGemPortNetworkCtpPerformanceMonitoringHistoryDataResponse",
97 getArgs{createGemPortNetworkCtpPerformanceMonitoringHistoryData(32768, 10), 2},
98 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}},
99 },
100 {"getEthernetFramePerformanceMonitoringHistoryDataUpstreamResponse",
101 getArgs{createEthernetFramePerformanceMonitoringHistoryDataUpstreamResponse(32768, 10), 2},
102 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}},
103 },
104 {"getEthernetFramePerformanceMonitoringHistoryDataDownstreamResponse",
105 getArgs{createEthernetFramePerformanceMonitoringHistoryDataDownstreamResponse(32768, 10), 2},
106 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}},
107 },
108 {"getEthernetPerformanceMonitoringHistoryDataResponse",
109 getArgs{createEthernetPerformanceMonitoringHistoryDataResponse(32768, 10), 2},
110 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}},
111 },
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700112 {"getSoftwareImageResponse",
113 getArgs{createSoftwareImageResponse(61440, 0, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
114 getWant{2, map[string]interface{}{"IsCommitted": uint8(0), "IsActive": uint8(0)}},
115 },
116 {"getSoftwareImageResponseActiveCommitted",
117 getArgs{createSoftwareImageResponse(61440, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
118 getWant{2, map[string]interface{}{"IsCommitted": uint8(1), "IsActive": uint8(1)}},
119 },
120 {"getSoftwareImageResponseVersion",
121 getArgs{createSoftwareImageResponse(61440, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
122 getWant{2, map[string]interface{}{"Version": ToOctets("BBSM_IMG_00001", 14)}},
123 },
124 {"getSoftwareImageResponseProductCode",
125 getArgs{createSoftwareImageResponse(2048, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
126 getWant{2, map[string]interface{}{"ProductCode": ToOctets("BBSIM-ONU", 25)}},
127 },
128 {"getSoftwareImageResponseActiveImageHash",
129 getArgs{createSoftwareImageResponse(1024, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
130 getWant{2, map[string]interface{}{"ImageHash": ToOctets("BBSM_IMG_00001", 25)}},
131 },
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800132 }
133 for _, tt := range tests {
134 t.Run(tt.name, func(t *testing.T) {
135
Matteo Scandolo78d5ee12021-04-14 11:11:32 -0700136 data, err := Serialize(omci.GetResponseType, tt.args.generatedPkt, tt.args.transactionId)
137 if err != nil {
138 t.Fatal("cannot-serial-omci-packet", err)
139 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800140
141 // emulate the openonu-go behavior:
142 // omci_cc.receiveMessage process the message (creates a gopacket and extracts the OMCI layer) and invokes a callback
143 // in the GetResponse case omci_cc.receiveOmciResponse
144 // then the OmciMessage (gopacket + OMIC layer) is is published on a channel
145 omciMsg, omciPkt := omciBytesToMsg(t, data)
146
147 assert.Equal(t, omciMsg.MessageType, omci.GetResponseType)
148 assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId)
149
150 // that is read by myb_sync.processMibSyncMessages
151 // the myb_sync.handleOmciMessage is called and then
152 // myb_sync.handleOmciGetResponseMessage where we extract the GetResponse layer
153 getResponseLayer := omciToGetResponse(t, omciPkt)
154
155 assert.Equal(t, getResponseLayer.Result, me.Success)
156
157 for k, v := range tt.want.attributes {
158 attr := getResponseLayer.Attributes[k]
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700159 attrValue := reflect.ValueOf(attr)
160 if attrValue.Kind() == reflect.Slice {
161 // it the attribute is a list, iterate and compare single values
162 expectedValue := reflect.ValueOf(v)
163 for i := 0; i < attrValue.Len(); i++ {
164 assert.Equal(t, attrValue.Index(i).Interface(), expectedValue.Index(i).Interface(),
165 fmt.Sprintf("Attribute %s does not match, expected: %s, received %s", k, v, attr))
166 }
167 } else {
168 // if it's not a list just compare
169 assert.Equal(t, attr, v)
170 }
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800171 }
172 })
173 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800174}
175
176func TestCreateOnugResponse(t *testing.T) {
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800177
Matteo Scandolo5863f002021-02-08 08:08:14 -0800178 sn := &openolt.SerialNumber{
179 VendorId: []byte("BBSM"),
180 VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)},
181 }
182 response := createOnugResponse(40960, 1, sn)
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800183 data, _ := Serialize(omci.GetResponseType, response, 1)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800184
185 omciMsg, omciPkt := omciBytesToMsg(t, data)
186
187 assert.Equal(t, omciMsg.MessageType, omci.GetResponseType)
188
189 getResponseLayer := omciToGetResponse(t, omciPkt)
190
191 assert.Equal(t, getResponseLayer.Result, me.Success)
Matteo Scandolo5863f002021-02-08 08:08:14 -0800192 snBytes := (getResponseLayer.Attributes["SerialNumber"]).([]byte)
193 snVendorPart := fmt.Sprintf("%s", snBytes[:4])
194 snNumberPart := hex.EncodeToString(snBytes[4:])
195 serialNumber := snVendorPart + snNumberPart
196 assert.Equal(t, serialNumber, "BBSM00010101")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800197}