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 testSetRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 30 | params := me.ParamData{ |
| 31 | EntityID: uint16(0), |
| 32 | Attributes: make(me.AttributeValueMap, 0), |
| 33 | } |
| 34 | attrDefs := managedEntity.GetAttributeDefinitions() |
| 35 | tableAttrFound := false |
| 36 | for _, attrDef := range attrDefs { |
| 37 | if attrDef.Index == 0 { |
| 38 | continue // Skip entity ID, already specified |
| 39 | } else if attrDef.IsTableAttribute() { |
| 40 | tableAttrFound = true |
| 41 | continue // TODO: Skip table attributes for now |
| 42 | } else if attrDef.GetAccess().Contains(me.Write) { |
| 43 | params.Attributes[attrDef.GetName()] = pickAValue(attrDef) |
| 44 | } |
| 45 | } |
| 46 | if tableAttrFound && len(params.Attributes) == 0 { |
| 47 | // The only set attribute may have been a table and we do not have |
| 48 | // a test for that right now. |
| 49 | return |
| 50 | } |
| 51 | assert.NotEmpty(t, params.Attributes) // Need a parameter that is a table attribute |
| 52 | bitmask, attrErr := me.GetAttributesBitmap(attrDefs, getAttributeNameSet(params.Attributes)) |
| 53 | assert.Nil(t, attrErr) |
| 54 | |
| 55 | // Create the managed instance |
| 56 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 57 | assert.NotNil(t, err) |
| 58 | assert.Equal(t, err.StatusCode(), me.Success) |
| 59 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 60 | |
| 61 | frame, omciErr := meframe.GenFrame(meInstance, SetRequestType, meframe.TransactionID(tid), |
| 62 | meframe.AttributeMask(bitmask), meframe.FrameFormat(messageSet)) |
| 63 | // some frames cannot fit all the attributes |
| 64 | if omciErr != nil { |
| 65 | if _, ok := omciErr.(*me.MessageTruncatedError); ok { |
| 66 | return |
| 67 | } |
| 68 | } |
| 69 | assert.NotNil(t, frame) |
| 70 | assert.NotZero(t, len(frame)) |
| 71 | |
| 72 | /////////////////////////////////////////////////////////////////// |
| 73 | // Now decode and compare |
| 74 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 75 | assert.NotNil(t, packet) |
| 76 | |
| 77 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 78 | assert.NotNil(t, omciLayer) |
| 79 | |
| 80 | omciObj, omciOk := omciLayer.(*OMCI) |
| 81 | assert.NotNil(t, omciObj) |
| 82 | assert.True(t, omciOk) |
| 83 | assert.Equal(t, tid, omciObj.TransactionID) |
| 84 | assert.Equal(t, SetRequestType, omciObj.MessageType) |
| 85 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 86 | |
| 87 | msgLayer := packet.Layer(LayerTypeSetRequest) |
| 88 | assert.NotNil(t, msgLayer) |
| 89 | |
| 90 | msgObj, msgOk := msgLayer.(*SetRequest) |
| 91 | assert.NotNil(t, msgObj) |
| 92 | assert.True(t, msgOk) |
| 93 | |
| 94 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 95 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 96 | assert.Equal(t, meInstance.GetAttributeValueMap(), msgObj.Attributes) |
| 97 | } |
| 98 | |
| 99 | func testSetResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) { |
| 100 | params := me.ParamData{ |
| 101 | EntityID: uint16(0), |
| 102 | } |
| 103 | // Create the managed instance |
| 104 | meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params) |
| 105 | assert.NotNil(t, err) |
| 106 | assert.Equal(t, err.StatusCode(), me.Success) |
| 107 | |
| 108 | tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF] |
| 109 | result := me.Results(rand.Int31n(10)) // [0, 9] Not all types will be tested |
| 110 | |
| 111 | // Always pass a failure mask, but should only get encoded if result == ParameterError |
| 112 | var unsupportedMask uint16 |
| 113 | var failedMask uint16 |
| 114 | attrDefs := managedEntity.GetAttributeDefinitions() |
| 115 | for _, attrDef := range attrDefs { |
| 116 | if attrDef.Index == 0 { |
| 117 | continue // Skip entity ID, already specified |
| 118 | |
| 119 | } else if attrDef.GetAccess().Contains(me.Write) { |
| 120 | // Random 10% chance this parameter unsupported and |
| 121 | // 10% it failed |
| 122 | switch rand.Int31n(5) { |
| 123 | case 0: |
| 124 | unsupportedMask |= attrDef.Mask |
| 125 | case 1: |
| 126 | failedMask |= attrDef.Mask |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | bitmask, attrErr := me.GetAttributesBitmap(attrDefs, getAttributeNameSet(params.Attributes)) |
| 131 | assert.Nil(t, attrErr) |
| 132 | |
| 133 | frame, omciErr := meframe.GenFrame(meInstance, SetResponseType, |
| 134 | meframe.TransactionID(tid), meframe.Result(result), |
| 135 | meframe.AttributeMask(bitmask), meframe.FrameFormat(messageSet), |
| 136 | meframe.AttributeExecutionMask(failedMask), |
| 137 | meframe.UnsupportedAttributeMask(unsupportedMask)) |
| 138 | assert.NotNil(t, frame) |
| 139 | assert.NotZero(t, len(frame)) |
| 140 | assert.Nil(t, omciErr) |
| 141 | |
| 142 | /////////////////////////////////////////////////////////////////// |
| 143 | // Now decode and compare |
| 144 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 145 | assert.NotNil(t, packet) |
| 146 | |
| 147 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 148 | assert.NotNil(t, omciLayer) |
| 149 | |
| 150 | omciObj, omciOk := omciLayer.(*OMCI) |
| 151 | assert.NotNil(t, omciObj) |
| 152 | assert.True(t, omciOk) |
| 153 | assert.Equal(t, tid, omciObj.TransactionID) |
| 154 | assert.Equal(t, SetResponseType, omciObj.MessageType) |
| 155 | assert.Equal(t, messageSet, omciObj.DeviceIdentifier) |
| 156 | |
| 157 | msgLayer := packet.Layer(LayerTypeSetResponse) |
| 158 | assert.NotNil(t, msgLayer) |
| 159 | |
| 160 | msgObj, msgOk := msgLayer.(*SetResponse) |
| 161 | assert.NotNil(t, msgObj) |
| 162 | assert.True(t, msgOk) |
| 163 | |
| 164 | assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass) |
| 165 | assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance) |
| 166 | assert.Equal(t, result, msgObj.Result) |
| 167 | |
| 168 | if result == me.AttributeFailure { |
| 169 | assert.Equal(t, failedMask, msgObj.FailedAttributeMask) |
| 170 | assert.Equal(t, unsupportedMask, msgObj.UnsupportedAttributeMask) |
| 171 | } else { |
| 172 | assert.Zero(t, msgObj.FailedAttributeMask) |
| 173 | assert.Zero(t, msgObj.UnsupportedAttributeMask) |
| 174 | } |
| 175 | } |