-
Notifications
You must be signed in to change notification settings - Fork 5
/
medef.go
156 lines (142 loc) · 4.95 KB
/
medef.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* NOTE: This file was generated, manual edits will be overwritten!
*
* Generated by 'goCodeGenerator.py':
* https://github.com/cboling/OMCI-parser/README.md
*/
package generated
import (
"errors"
"fmt"
"github.com/deckarep/golang-set"
"github.com/google/gopacket"
"math/bits"
)
type ManagedEntityDefinition struct {
Name string
ClassID ClassID
MessageTypes mapset.Set
AllowedAttributeMask uint16
AttributeDefinitions AttributeDefinitionMap
}
func (bme *ManagedEntityDefinition) String() string {
return fmt.Sprintf("Definition: %s: CID: %v, Attributes: %v",
bme.Name, bme.ClassID, bme.AttributeDefinitions)
}
func (bme *ManagedEntityDefinition) GetName() string {
return bme.Name
}
func (bme *ManagedEntityDefinition) GetClassID() ClassID {
return bme.ClassID
}
func (bme *ManagedEntityDefinition) GetMessageTypes() mapset.Set {
return bme.MessageTypes
}
func (bme *ManagedEntityDefinition) GetAllowedAttributeMask() uint16 {
return bme.AllowedAttributeMask
}
func (bme *ManagedEntityDefinition) GetAttributeDefinitions() *AttributeDefinitionMap {
return &bme.AttributeDefinitions
}
func (bme *ManagedEntityDefinition) DecodeAttributes(mask uint16, data []byte, p gopacket.PacketBuilder, msgType byte) (AttributeValueMap, error) {
if (mask | bme.GetAllowedAttributeMask()) != bme.GetAllowedAttributeMask() {
// TODO: Provide custom error code so a response 'result' can properly be coded
return nil, errors.New("unsupported attribute mask")
}
keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions)
attrMap := make(AttributeValueMap, bits.OnesCount16(mask))
for _, index := range keyList {
if index == 0 {
continue // Skip Entity ID
}
attrDef := bme.AttributeDefinitions[index]
name := attrDef.GetName()
if mask&(1<<(16-uint(index))) != 0 {
value, err := attrDef.Decode(data, p, msgType)
if err != nil {
return nil, err
}
if attrDef.IsTableAttribute() {
switch msgType {
default:
return nil, errors.New(fmt.Sprintf("unsupported Message Type '%v' for table serialization", msgType))
case byte(Get) | AK: // Get Response
attrMap[name] = value
data = data[4:]
case byte(GetNext) | AK: // Get Next Response
// Value is a partial octet buffer we need to collect and at
// the end (last segment) pull it up into more appropriate table
// rows
valueBuffer, ok := value.([]byte)
if !ok {
panic("unexpected type already returned as get-next-response attribute data")
}
if existing, found := attrMap[name]; found {
prev, ok := existing.([]byte)
if !ok {
panic("unexpected type already in attribute value map")
}
attrMap[name] = append(prev, valueBuffer...)
} else {
attrMap[name] = valueBuffer
}
if size := attrDef.GetSize(); size != 0 && size > len(valueBuffer) {
panic("unexpected size difference")
}
data = data[len(valueBuffer):]
case byte(Set) | AR: // Set Request
fmt.Println("TODO")
case byte(SetTable) | AR: // Set Table Request
// TODO: Only baseline supported at this time
return nil, errors.New("attribute encode for set-table-request not yet supported")
}
} else {
attrMap[name] = value
data = data[attrDef.GetSize():]
}
}
}
return attrMap, nil
}
func (bme *ManagedEntityDefinition) SerializeAttributes(attr AttributeValueMap, mask uint16,
b gopacket.SerializeBuffer, msgType byte, bytesAvailable int) error {
if (mask | bme.GetAllowedAttributeMask()) != bme.GetAllowedAttributeMask() {
// TODO: Provide custom error code so a response 'result' can properly be coded
return errors.New("unsupported attribute mask")
}
// TODO: Need to limit number of bytes appended to not exceed packet size
// Is there space/metadata info in 'b' parameter to allow this?
keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions)
for _, index := range keyList {
if index == 0 {
continue // Skip Entity ID
}
attrDef := bme.AttributeDefinitions[index]
if mask&(1<<(16-uint(index))) != 0 {
value, ok := attr[attrDef.GetName()]
if !ok {
msg := fmt.Sprintf("attribute not found: '%v'", attrDef.GetName())
return errors.New(msg)
}
size, err := attrDef.SerializeTo(value, b, msgType, bytesAvailable)
if err != nil {
return err
}
bytesAvailable -= size
}
}
return nil
}