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 testRebootRequestTypeMeFrame(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 | condition := uint8(rand.Int31n(3)) // [0, 3] |
| 40 | |
| 41 | frame, omciErr := meframe.GenFrame(meInstance, RebootRequestType, meframe.TransactionID(tid), |
| 42 | meframe.RebootCondition(condition), 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, RebootRequestType, omciObj.MessageType) |
| 60 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 61 | |
| 62 | msgLayer := packet.Layer(LayerTypeRebootRequest) |
| 63 | assert.NotNil(t, msgLayer) |
| 64 | |
| 65 | msgObj, msgOk := msgLayer.(*RebootRequest) |
| 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, condition, msgObj.RebootCondition) |
| 72 | } |
| 73 | |
| 74 | func testRebootResponseTypeMeFrame(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 | result := me.Results(rand.Int31n(7)) // [0, 6] Not all types will be tested |
| 85 | |
| 86 | frame, omciErr := meframe.GenFrame(meInstance, RebootResponseType, meframe.TransactionID(tid), |
| 87 | meframe.Result(result), 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, RebootResponseType, omciObj.MessageType) |
| 105 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 106 | |
| 107 | msgLayer := packet.Layer(LayerTypeRebootResponse) |
| 108 | assert.NotNil(t, msgLayer) |
| 109 | |
| 110 | msgObj, msgOk := msgLayer.(*RebootResponse) |
| 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, result, msgObj.Result) |
| 117 | } |