blob: 6db557e3f53fbce877aa0255359455052da82a2b [file] [log] [blame]
Chip Boling610117d2021-09-09 11:24:34 -05001/*
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 */
17package meframe_test
18
19import (
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
29func testGetNextRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) {
30 params := me.ParamData{
31 EntityID: uint16(0),
32 Attributes: make(me.AttributeValueMap, 0),
33 }
34 // TODO: Loop over all table attributes for this class ID
35 // Find first attribute that is a table definition
36 // TODO: Test request of more than 1 attribute. G.988 specifies that a status
37 // code of (3) should be returned. Raise error during encode instead of
38 // waiting for compliant ONU. May want to have an 'ignore' to allow it.
39 attrDefs := managedEntity.GetAttributeDefinitions()
40 for _, attrDef := range attrDefs {
41 if attrDef.Index == 0 {
42 continue // Skip entity ID, already specified
43 } else if attrDef.IsTableAttribute() {
44 // TODO: Tables without a size are not supported. At least needs to be one octet
45 if attrDef.Size == 0 {
46 continue
47 }
48 // Allow 'nil' as parameter value for GetNextRequests since we only need names
49 params.Attributes[attrDef.GetName()] = nil
50 break
51 }
52 }
53 if len(params.Attributes) == 0 {
54 return
55 }
56 bitmask, attrErr := me.GetAttributesBitmap(attrDefs, getAttributeNameSet(params.Attributes))
57 assert.Nil(t, attrErr)
58
59 // Create the managed instance
60 meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params)
61 assert.NotNil(t, err)
62 assert.Equal(t, err.StatusCode(), me.Success)
63
64 seqNumber := uint16(rand.Int31n(0xFFFF)) // [0, 0xFFFE]
65 tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF]
66
67 frame, omciErr := meframe.GenFrame(meInstance, GetNextRequestType, meframe.TransactionID(tid),
68 meframe.SequenceNumberCountOrSize(seqNumber), meframe.AttributeMask(bitmask), meframe.FrameFormat(messageSet))
69 assert.NotNil(t, frame)
70 assert.NotZero(t, len(frame))
71 assert.Nil(t, omciErr)
72
73 ///////////////////////////////////////////////////////////////////
74 // Now decode and compare
75 packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy)
76 assert.NotNil(t, packet)
77
78 omciLayer := packet.Layer(LayerTypeOMCI)
79 assert.NotNil(t, omciLayer)
80
81 omciObj, omciOk := omciLayer.(*OMCI)
82 assert.NotNil(t, omciObj)
83 assert.True(t, omciOk)
84 assert.Equal(t, tid, omciObj.TransactionID)
85 assert.Equal(t, GetNextRequestType, omciObj.MessageType)
86 assert.Equal(t, messageSet, omciObj.DeviceIdentifier)
87
88 msgLayer := packet.Layer(LayerTypeGetNextRequest)
89 assert.NotNil(t, msgLayer)
90
91 msgObj, msgOk := msgLayer.(*GetNextRequest)
92 assert.NotNil(t, msgObj)
93 assert.True(t, msgOk)
94
95 assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass)
96 assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance)
97 assert.Equal(t, meInstance.GetAttributeMask(), msgObj.AttributeMask)
98 assert.Equal(t, seqNumber, msgObj.SequenceNumber)
99}
100
101func testGetNextResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) {
102 params := me.ParamData{
103 EntityID: uint16(0),
104 Attributes: make(me.AttributeValueMap, 0),
105 }
106 // TODO: Loop over result types (here and other responses with results)
107 result := me.Success // me.Results(rand.Int31n(7)) // [0, 6]
108 bitmask := uint16(0)
109 attrDefs := managedEntity.GetAttributeDefinitions()
110
111 // TODO: Loop over all table attributes for this class ID
112 if result == me.Success {
113 // Find first attribute that is a table definition
114 // TODO: Test request of more than 1 attribute. G.988 specifies that a status
115 // code of (3) should be returned. Raise error during encode instead of
116 // waiting for compliant ONU. May want to have an 'ignore' to allow it.
117 for _, attrDef := range attrDefs {
118 if attrDef.Index == 0 {
119 continue // Skip entity ID, already specified
120 } else if attrDef.IsTableAttribute() {
121 if len(params.Attributes) == 0 {
122 // Need a parameter that is a table attribute
123 return
124 }
125 params.Attributes[attrDef.GetName()] = pickAValue(attrDef)
126 break
127 }
128 }
129 if len(params.Attributes) == 0 {
130 return
131 }
132 assert.NotEmpty(t, params.Attributes) // Need a parameter that is a table attribute
133 var attrErr error
134 bitmask, attrErr = me.GetAttributesBitmap(attrDefs, getAttributeNameSet(params.Attributes))
135 assert.Nil(t, attrErr)
136 }
137 // Create the managed instance
138 meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params)
139 assert.NotNil(t, err)
140 assert.Equal(t, err.StatusCode(), me.Success)
141
142 tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF]
143
144 frame, omciErr := meframe.GenFrame(meInstance, GetNextResponseType, meframe.TransactionID(tid), meframe.Result(result),
145 meframe.AttributeMask(bitmask), meframe.FrameFormat(messageSet))
146 assert.NotNil(t, frame)
147 assert.NotZero(t, len(frame))
148 assert.Nil(t, omciErr)
149
150 ///////////////////////////////////////////////////////////////////
151 // Now decode and compare
152 cid := meInstance.GetClassID()
153 assert.NotEqual(t, cid, 0)
154 packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy)
155 assert.NotNil(t, packet)
156
157 omciLayer := packet.Layer(LayerTypeOMCI)
158 assert.NotNil(t, omciLayer)
159
160 omciObj, omciOk := omciLayer.(*OMCI)
161 assert.NotNil(t, omciObj)
162 assert.True(t, omciOk)
163 assert.Equal(t, tid, omciObj.TransactionID)
164 assert.Equal(t, GetNextResponseType, omciObj.MessageType)
165 assert.Equal(t, messageSet, omciObj.DeviceIdentifier)
166
167 msgLayer := packet.Layer(LayerTypeGetNextResponse)
168 assert.NotNil(t, msgLayer)
169
170 msgObj, msgOk := msgLayer.(*GetNextResponse)
171 assert.NotNil(t, msgObj)
172 assert.True(t, msgOk)
173
174 assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass)
175 assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance)
176 assert.Equal(t, meInstance.GetAttributeMask(), msgObj.AttributeMask)
177
178 switch msgObj.Result {
179 default:
180 assert.Equal(t, result, msgObj.Result)
181
182 case me.Success:
183 assert.Equal(t, result, msgObj.Result)
184 // The attributes should be equal but for variable length table attribute (size = 0 in structure)
185 // we will have the frame padding returned as well.
186 for attrName, value := range meInstance.GetAttributeValueMap() {
187 attr, err := me.GetAttributeDefinitionByName(attrDefs, attrName)
188 assert.Nil(t, err)
189 assert.NotNil(t, attr)
190 assert.Equal(t, attrName, attr.GetName())
191 if attr.IsTableAttribute() {
192 instValue := value.([]byte)
193 msgValue := msgObj.Attributes[attrName].([]byte)
194 assert.True(t, len(instValue) <= len(msgValue))
195 assert.Equal(t, msgValue[:len(instValue)], instValue)
196 } else {
197 assert.Equal(t, value, msgObj.Attributes[attrName])
198 }
199 }
200 }
201}