blob: 5632af8b092f7ba18b9a18f4c7cedac899ff0d61 [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 */
17
18package meframe_test
19
20import (
21 "github.com/google/gopacket"
22 . "github.com/opencord/omci-lib-go"
23 me "github.com/opencord/omci-lib-go/generated"
24 "github.com/opencord/omci-lib-go/meframe"
25 "github.com/stretchr/testify/assert"
26 "math/rand"
27 "testing"
28)
29
30func testCreateRequestTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) {
31 // Generate the frame. Use a default Entity ID of zero, but for the
32 // OMCI library, we need to specify all supported Set-By-Create
33 params := me.ParamData{
34 EntityID: uint16(0),
35 Attributes: make(me.AttributeValueMap, 0),
36 }
37 for _, attrDef := range managedEntity.GetAttributeDefinitions() {
38 if attrDef.Index == 0 {
39 continue // Skip entity ID, already specified
40
41 } else if attrDef.GetAccess().Contains(me.SetByCreate) {
42 params.Attributes[attrDef.GetName()] = pickAValue(attrDef)
43 }
44 }
45 // Create the managed instance
46 meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params)
47 tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF]
48 assert.NotNil(t, err)
49 assert.Equal(t, err.StatusCode(), me.Success)
50
51 frame, omciErr := meframe.GenFrame(meInstance, CreateRequestType, meframe.TransactionID(tid), meframe.FrameFormat(messageSet))
52 assert.NotNil(t, frame)
53 assert.NotZero(t, len(frame))
54 assert.Nil(t, omciErr)
55
56 ///////////////////////////////////////////////////////////////////
57 // Now decode and compare
58 packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy)
59 assert.NotNil(t, packet)
60
61 omciLayer := packet.Layer(LayerTypeOMCI)
62 assert.NotNil(t, omciLayer)
63
64 omciObj, omciOk := omciLayer.(*OMCI)
65 assert.NotNil(t, omciObj)
66 assert.True(t, omciOk)
67 assert.Equal(t, tid, omciObj.TransactionID)
68 assert.Equal(t, CreateRequestType, omciObj.MessageType)
69 assert.Equal(t, messageSet, omciObj.DeviceIdentifier)
70
71 msgLayer := packet.Layer(LayerTypeCreateRequest)
72 assert.NotNil(t, msgLayer)
73
74 msgObj, msgOk := msgLayer.(*CreateRequest)
75 assert.NotNil(t, msgObj)
76 assert.True(t, msgOk)
77
78 assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass)
79 assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance)
80 assert.Equal(t, meInstance.GetAttributeValueMap(), msgObj.Attributes)
81}
82
83func testCreateResponseTypeMeFrame(t *testing.T, managedEntity *me.ManagedEntity, messageSet DeviceIdent) {
84 params := me.ParamData{
85 EntityID: uint16(0),
86 }
87 // Create the managed instance
88 meInstance, err := me.NewManagedEntity(managedEntity.GetManagedEntityDefinition(), params)
89 assert.NotNil(t, err)
90 assert.Equal(t, err.StatusCode(), me.Success)
91
92 tid := uint16(rand.Int31n(0xFFFE) + 1) // [1, 0xFFFF]
93 result := me.Results(rand.Int31n(7)) // [0, 6] Not all types will be tested
94
95 // Always pass a failure mask, but should only get encoded if result == ParameterError
96 var mask uint16
97 for _, attrDef := range managedEntity.GetAttributeDefinitions() {
98 if attrDef.Index == 0 {
99 continue // Skip entity ID, already specified
100
101 } else if attrDef.GetAccess().Contains(me.SetByCreate) {
102 // Random 20% chance this parameter was bad
103 if result == me.ParameterError && rand.Int31n(5) == 0 {
104 mask |= attrDef.Mask
105 }
106 }
107 }
108 frame, omciErr := meframe.GenFrame(meInstance, CreateResponseType,
109 meframe.TransactionID(tid), meframe.Result(result), meframe.AttributeExecutionMask(mask), meframe.FrameFormat(messageSet))
110 assert.NotNil(t, frame)
111 assert.NotZero(t, len(frame))
112 assert.Nil(t, omciErr)
113
114 ///////////////////////////////////////////////////////////////////
115 // Now decode and compare
116 packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy)
117 assert.NotNil(t, packet)
118
119 omciLayer := packet.Layer(LayerTypeOMCI)
120 assert.NotNil(t, omciLayer)
121
122 omciObj, omciOk := omciLayer.(*OMCI)
123 assert.NotNil(t, omciObj)
124 assert.True(t, omciOk)
125 assert.Equal(t, tid, omciObj.TransactionID)
126 assert.Equal(t, CreateResponseType, omciObj.MessageType)
127 assert.Equal(t, messageSet, omciObj.DeviceIdentifier)
128
129 msgLayer := packet.Layer(LayerTypeCreateResponse)
130 assert.NotNil(t, msgLayer)
131
132 msgObj, msgOk := msgLayer.(*CreateResponse)
133 assert.NotNil(t, msgObj)
134 assert.True(t, msgOk)
135
136 assert.Equal(t, meInstance.GetClassID(), msgObj.EntityClass)
137 assert.Equal(t, meInstance.GetEntityID(), msgObj.EntityInstance)
138 assert.Equal(t, result, msgObj.Result)
139
140 if result == me.ParameterError {
141 assert.Equal(t, mask, msgObj.AttributeExecutionMask)
142 } else {
143 assert.Zero(t, msgObj.AttributeExecutionMask)
144 }
145}