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