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" |
Andrea Campanella | e0cd823 | 2021-10-15 15:10:23 +0200 | [diff] [blame] | 21 | . "github.com/opencord/omci-lib-go/v2" |
| 22 | me "github.com/opencord/omci-lib-go/v2/generated" |
| 23 | "github.com/opencord/omci-lib-go/v2/meframe" |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 24 | "github.com/stretchr/testify/assert" |
| 25 | "math/rand" |
| 26 | "testing" |
| 27 | ) |
| 28 | |
| 29 | func testGetAllAlarmsRequestTypeMeFrame(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 | mode := uint8(rand.Int31n(2)) // [0, 1] |
| 40 | |
| 41 | frame, omciErr := meframe.GenFrame(meInstance, GetAllAlarmsRequestType, meframe.TransactionID(tid), |
| 42 | meframe.RetrievalMode(mode), meframe.FrameFormat(messageSet)) |
| 43 | assert.NotNil(t, frame) |
| 44 | assert.NotZero(t, len(frame)) |
| 45 | assert.Nil(t, omciErr) |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////// |
| 48 | // Now decode and compare |
| 49 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 50 | assert.NotNil(t, packet) |
| 51 | |
| 52 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 53 | assert.NotNil(t, omciLayer) |
| 54 | |
| 55 | omciObj, omciOk := omciLayer.(*OMCI) |
| 56 | assert.NotNil(t, omciObj) |
| 57 | assert.True(t, omciOk) |
| 58 | assert.Equal(t, tid, omciObj.TransactionID) |
| 59 | assert.Equal(t, GetAllAlarmsRequestType, omciObj.MessageType) |
| 60 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 61 | |
| 62 | msgLayer := packet.Layer(LayerTypeGetAllAlarmsRequest) |
| 63 | assert.NotNil(t, msgLayer) |
| 64 | |
| 65 | msgObj, msgOk := msgLayer.(*GetAllAlarmsRequest) |
| 66 | assert.NotNil(t, msgObj) |
| 67 | assert.True(t, msgOk) |
| 68 | |
| 69 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 70 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 71 | assert.Equal(t, mode, msgObj.AlarmRetrievalMode) |
| 72 | } |
| 73 | |
| 74 | func testGetAllAlarmsResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 75 | params := me.ParamData{ |
| 76 | EntityID: uint16(0), |
| 77 | } |
| 78 | // Create the managed instance |
| 79 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 80 | assert.NotNil(t, err) |
| 81 | assert.Equal(t, err.StatusCode(), me.Success) |
| 82 | |
| 83 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 84 | numOfCommands := uint16(rand.Int31n(5)) // [0, 5) |
| 85 | |
| 86 | frame, omciErr := meframe.GenFrame(meInstance, GetAllAlarmsResponseType, meframe.TransactionID(tid), |
| 87 | meframe.SequenceNumberCountOrSize(numOfCommands), meframe.FrameFormat(messageSet)) |
| 88 | assert.NotNil(t, frame) |
| 89 | assert.NotZero(t, len(frame)) |
| 90 | assert.Nil(t, omciErr) |
| 91 | |
| 92 | /////////////////////////////////////////////////////////////////// |
| 93 | // Now decode and compare |
| 94 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 95 | assert.NotNil(t, packet) |
| 96 | |
| 97 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 98 | assert.NotNil(t, omciLayer) |
| 99 | |
| 100 | omciObj, omciOk := omciLayer.(*OMCI) |
| 101 | assert.NotNil(t, omciObj) |
| 102 | assert.True(t, omciOk) |
| 103 | assert.Equal(t, tid, omciObj.TransactionID) |
| 104 | assert.Equal(t, GetAllAlarmsResponseType, omciObj.MessageType) |
| 105 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 106 | |
| 107 | msgLayer := packet.Layer(LayerTypeGetAllAlarmsResponse) |
| 108 | assert.NotNil(t, msgLayer) |
| 109 | |
| 110 | msgObj, msgOk := msgLayer.(*GetAllAlarmsResponse) |
| 111 | assert.NotNil(t, msgObj) |
| 112 | assert.True(t, msgOk) |
| 113 | |
| 114 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 115 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 116 | assert.Equal(t, numOfCommands, msgObj.NumberOfCommands) |
| 117 | } |
| 118 | |
| 119 | func testGetAllAlarmsNextRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 120 | params := me.ParamData{ |
| 121 | EntityID: uint16(0), |
| 122 | } |
| 123 | // Create the managed instance |
| 124 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 125 | assert.NotNil(t, err) |
| 126 | assert.Equal(t, err.StatusCode(), me.Success) |
| 127 | |
| 128 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 129 | sequenceNumber := uint16(rand.Int31n(5)) // [0, 5) |
| 130 | |
| 131 | frame, omciErr := meframe.GenFrame(meInstance, GetAllAlarmsNextRequestType, meframe.TransactionID(tid), |
| 132 | meframe.SequenceNumberCountOrSize(sequenceNumber), meframe.FrameFormat(messageSet)) |
| 133 | assert.NotNil(t, frame) |
| 134 | assert.NotZero(t, len(frame)) |
| 135 | assert.Nil(t, omciErr) |
| 136 | |
| 137 | /////////////////////////////////////////////////////////////////// |
| 138 | // Now decode and compare |
| 139 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 140 | assert.NotNil(t, packet) |
| 141 | |
| 142 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 143 | assert.NotNil(t, omciLayer) |
| 144 | |
| 145 | omciObj, omciOk := omciLayer.(*OMCI) |
| 146 | assert.NotNil(t, omciObj) |
| 147 | assert.True(t, omciOk) |
| 148 | assert.Equal(t, tid, omciObj.TransactionID) |
| 149 | assert.Equal(t, GetAllAlarmsNextRequestType, omciObj.MessageType) |
| 150 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 151 | |
| 152 | msgLayer := packet.Layer(LayerTypeGetAllAlarmsNextRequest) |
| 153 | assert.NotNil(t, msgLayer) |
| 154 | |
| 155 | msgObj, msgOk := msgLayer.(*GetAllAlarmsNextRequest) |
| 156 | assert.NotNil(t, msgObj) |
| 157 | assert.True(t, msgOk) |
| 158 | |
| 159 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 160 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 161 | assert.Equal(t, sequenceNumber, msgObj.CommandSequenceNumber) |
| 162 | } |
| 163 | |
| 164 | func testGetAllAlarmsNextResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 165 | params := me.ParamData{ |
| 166 | EntityID: uint16(0), |
| 167 | } |
| 168 | // Create the managed instance |
| 169 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 170 | assert.NotNil(t, err) |
| 171 | assert.Equal(t, err.StatusCode(), me.Success) |
| 172 | |
| 173 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 174 | |
| 175 | alarmInfo := meframe.AlarmOptions{ |
| 176 | AlarmClassID: 123, // TODO: Real class here? |
| 177 | AlarmInstance: 456, |
| 178 | AlarmBitmap: make([]byte, 28), |
| 179 | } |
| 180 | // TODO: Allow a 1 to 28 octet array to be used and zero fill any remainder... |
| 181 | for octet := 0; octet < 28; octet++ { |
| 182 | alarmInfo.AlarmBitmap[octet] = uint8(rand.Intn(256)) |
| 183 | } |
| 184 | frame, omciErr := meframe.GenFrame(meInstance, GetAllAlarmsNextResponseType, meframe.TransactionID(tid), |
| 185 | meframe.Alarm(alarmInfo), meframe.FrameFormat(messageSet)) |
| 186 | assert.NotNil(t, frame) |
| 187 | assert.NotZero(t, len(frame)) |
| 188 | assert.Nil(t, omciErr) |
| 189 | |
| 190 | /////////////////////////////////////////////////////////////////// |
| 191 | // Now decode and compare |
| 192 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 193 | assert.NotNil(t, packet) |
| 194 | |
| 195 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 196 | assert.NotNil(t, omciLayer) |
| 197 | |
| 198 | omciObj, omciOk := omciLayer.(*OMCI) |
| 199 | assert.NotNil(t, omciObj) |
| 200 | assert.True(t, omciOk) |
| 201 | assert.Equal(t, tid, omciObj.TransactionID) |
| 202 | assert.Equal(t, GetAllAlarmsNextResponseType, omciObj.MessageType) |
| 203 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 204 | |
| 205 | msgLayer := packet.Layer(LayerTypeGetAllAlarmsNextResponse) |
| 206 | assert.NotNil(t, msgLayer) |
| 207 | |
| 208 | msgObj, msgOk := msgLayer.(*GetAllAlarmsNextResponse) |
| 209 | assert.NotNil(t, msgObj) |
| 210 | assert.True(t, msgOk) |
| 211 | |
| 212 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 213 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 214 | assert.Equal(t, alarmInfo.AlarmClassID, msgObj.AlarmEntityClass) |
| 215 | assert.Equal(t, alarmInfo.AlarmInstance, msgObj.AlarmEntityInstance) |
| 216 | for octet := 0; octet < len(alarmInfo.AlarmBitmap); octet++ { |
| 217 | assert.Equal(t, alarmInfo.AlarmBitmap[octet], msgObj.AlarmBitMap[octet]) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func testAlarmNotificationTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 222 | // TODO: Implement |
| 223 | } |