Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package meframe_test |
| 18 | |
| 19 | import ( |
| 20 | "github.com/google/gopacket" |
| 21 | . "github.com/opencord/omci-lib-go" |
| 22 | me "github.com/opencord/omci-lib-go/generated" |
| 23 | "github.com/opencord/omci-lib-go/meframe" |
| 24 | "github.com/stretchr/testify/assert" |
| 25 | "math/rand" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | func testMibUploadRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 30 | params := me.ParamData{ |
| 31 | EntityID: uint16(0), |
| 32 | } |
| 33 | // Create the managed instance |
| 34 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 35 | assert.NotNil(t, err) |
| 36 | assert.Equal(t, err.StatusCode(), me.Success) |
| 37 | |
| 38 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 39 | |
| 40 | frame, omciErr := meframe.GenFrame(meInstance, MibUploadRequestType, meframe.TransactionID(tid), meframe.FrameFormat(messageSet)) |
| 41 | assert.NotNil(t, frame) |
| 42 | assert.NotZero(t, len(frame)) |
| 43 | assert.Nil(t, omciErr) |
| 44 | |
| 45 | /////////////////////////////////////////////////////////////////// |
| 46 | // Now decode and compare |
| 47 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 48 | assert.NotNil(t, packet) |
| 49 | |
| 50 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 51 | assert.NotNil(t, omciLayer) |
| 52 | |
| 53 | omciObj, omciOk := omciLayer.(*OMCI) |
| 54 | assert.NotNil(t, omciObj) |
| 55 | assert.True(t, omciOk) |
| 56 | assert.Equal(t, tid, omciObj.TransactionID) |
| 57 | assert.Equal(t, MibUploadRequestType, omciObj.MessageType) |
| 58 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 59 | |
| 60 | msgLayer := packet.Layer(LayerTypeMibUploadRequest) |
| 61 | assert.NotNil(t, msgLayer) |
| 62 | |
| 63 | msgObj, msgOk := msgLayer.(*MibUploadRequest) |
| 64 | assert.NotNil(t, msgObj) |
| 65 | assert.True(t, msgOk) |
| 66 | |
| 67 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 68 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 69 | } |
| 70 | |
| 71 | func testMibUploadResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 72 | params := me.ParamData{ |
| 73 | EntityID: uint16(0), |
| 74 | } |
| 75 | // Create the managed instance |
| 76 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 77 | assert.NotNil(t, err) |
| 78 | assert.Equal(t, err.StatusCode(), me.Success) |
| 79 | |
| 80 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 81 | numOfCommands := uint16(rand.Int31n(5)) // [0, 5) |
| 82 | |
| 83 | frame, omciErr := meframe.GenFrame(meInstance, MibUploadResponseType, meframe.TransactionID(tid), |
| 84 | meframe.SequenceNumberCountOrSize(numOfCommands), meframe.FrameFormat(messageSet)) |
| 85 | assert.NotNil(t, frame) |
| 86 | assert.NotZero(t, len(frame)) |
| 87 | assert.Nil(t, omciErr) |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////// |
| 90 | // Now decode and compare |
| 91 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 92 | assert.NotNil(t, packet) |
| 93 | |
| 94 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 95 | assert.NotNil(t, omciLayer) |
| 96 | |
| 97 | omciObj, omciOk := omciLayer.(*OMCI) |
| 98 | assert.NotNil(t, omciObj) |
| 99 | assert.True(t, omciOk) |
| 100 | assert.Equal(t, tid, omciObj.TransactionID) |
| 101 | assert.Equal(t, MibUploadResponseType, omciObj.MessageType) |
| 102 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 103 | |
| 104 | msgLayer := packet.Layer(LayerTypeMibUploadResponse) |
| 105 | assert.NotNil(t, msgLayer) |
| 106 | |
| 107 | msgObj, msgOk := msgLayer.(*MibUploadResponse) |
| 108 | assert.NotNil(t, msgObj) |
| 109 | assert.True(t, msgOk) |
| 110 | |
| 111 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 112 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 113 | assert.Equal(t, numOfCommands, msgObj.NumberOfCommands) |
| 114 | } |
| 115 | |
| 116 | func testMibUploadNextRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 117 | params := me.ParamData{ |
| 118 | EntityID: uint16(0), |
| 119 | } |
| 120 | // Create the managed instance |
| 121 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 122 | assert.NotNil(t, err) |
| 123 | assert.Equal(t, err.StatusCode(), me.Success) |
| 124 | |
| 125 | seqNumber := uint16(rand.Int31n(0xFFFF)) // [0, 0xFFFE] |
| 126 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 127 | |
| 128 | var frame []byte |
| 129 | frame, omciErr := meframe.GenFrame(meInstance, MibUploadNextRequestType, meframe.TransactionID(tid), |
| 130 | meframe.SequenceNumberCountOrSize(seqNumber), meframe.FrameFormat(messageSet)) |
| 131 | assert.NotNil(t, frame) |
| 132 | assert.NotZero(t, len(frame)) |
| 133 | assert.Nil(t, omciErr) |
| 134 | |
| 135 | /////////////////////////////////////////////////////////////////// |
| 136 | // Now decode and compare |
| 137 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 138 | assert.NotNil(t, packet) |
| 139 | |
| 140 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 141 | assert.NotNil(t, omciLayer) |
| 142 | |
| 143 | omciObj, omciOk := omciLayer.(*OMCI) |
| 144 | assert.NotNil(t, omciObj) |
| 145 | assert.True(t, omciOk) |
| 146 | assert.Equal(t, tid, omciObj.TransactionID) |
| 147 | assert.Equal(t, MibUploadNextRequestType, omciObj.MessageType) |
| 148 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 149 | |
| 150 | msgLayer := packet.Layer(LayerTypeMibUploadNextRequest) |
| 151 | assert.NotNil(t, msgLayer) |
| 152 | |
| 153 | msgObj, msgOk := msgLayer.(*MibUploadNextRequest) |
| 154 | assert.NotNil(t, msgObj) |
| 155 | assert.True(t, msgOk) |
| 156 | |
| 157 | assert.Equal(t, seqNumber, msgObj.CommandSequenceNumber) |
| 158 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 159 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 160 | } |
| 161 | |
| 162 | func testMibUploadNextResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 163 | params := me.ParamData{ |
| 164 | EntityID: uint16(0), |
| 165 | } |
| 166 | // Create the managed instance |
| 167 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 168 | assert.NotNil(t, err) |
| 169 | assert.Equal(t, err.StatusCode(), me.Success) |
| 170 | |
| 171 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 172 | |
| 173 | // TODO: Since only baseline messages supported, send only one ME |
| 174 | uploadMe := meInstance |
| 175 | |
| 176 | frame, omciErr := meframe.GenFrame(meInstance, MibUploadNextResponseType, meframe.TransactionID(tid), |
| 177 | meframe.Payload(uploadMe), meframe.FrameFormat(messageSet)) |
| 178 | assert.NotNil(t, frame) |
| 179 | assert.NotZero(t, len(frame)) |
| 180 | assert.Nil(t, omciErr) |
| 181 | |
| 182 | /////////////////////////////////////////////////////////////////// |
| 183 | // Now decode and compare |
| 184 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 185 | assert.NotNil(t, packet) |
| 186 | |
| 187 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 188 | assert.NotNil(t, omciLayer) |
| 189 | |
| 190 | omciObj, omciOk := omciLayer.(*OMCI) |
| 191 | assert.NotNil(t, omciObj) |
| 192 | assert.True(t, omciOk) |
| 193 | assert.Equal(t, tid, omciObj.TransactionID) |
| 194 | assert.Equal(t, MibUploadNextResponseType, omciObj.MessageType) |
| 195 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 196 | |
| 197 | msgLayer := packet.Layer(LayerTypeMibUploadNextResponse) |
| 198 | assert.NotNil(t, msgLayer) |
| 199 | |
| 200 | msgObj, msgOk := msgLayer.(*MibUploadNextResponse) |
| 201 | assert.NotNil(t, msgObj) |
| 202 | assert.True(t, msgOk) |
| 203 | |
| 204 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 205 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 206 | assert.Equal(t, uploadMe.GetClassID(), msgObj.ReportedME.GetClassID()) |
| 207 | assert.Equal(t, uploadMe.GetEntityID(), msgObj.ReportedME.GetEntityID()) |
| 208 | } |