blob: 7cb85d26faee4d86f7dd4fd35075986f5673e995 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001/*
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 /*
18 * NOTE: This file was generated, manual edits will be overwritten!
19 *
20 * Generated by 'goCodeGenerator.py':
21 * https://github.com/cboling/OMCI-parser/README.md
22 */
23
24package generated
25
26import (
27 "errors"
28 "fmt"
29 "github.com/deckarep/golang-set"
30 "github.com/google/gopacket"
31 "math/bits"
32)
33
34// ManagedEntityDefinition defines a Manage Entity
35type ManagedEntityDefinition struct {
36 Name string
37 ClassID ClassID
38 MessageTypes mapset.Set // Mandatory
39 // TODO: Support Optional Message types (this has just been fixed in the code generator)
40 AllowedAttributeMask uint16
41 AttributeDefinitions AttributeDefinitionMap
42 Access ClassAccess
43 Support ClassSupport
44}
45
46func (bme *ManagedEntityDefinition) String() string {
47 return fmt.Sprintf("Definition: %s: CID: %v, Attributes: %v",
48 bme.Name, bme.ClassID, bme.AttributeDefinitions)
49}
50
51// GetName retrieves the name of a managed entity from a ME Definition
52func (bme ManagedEntityDefinition) GetName() string {
53 return bme.Name
54}
55
56// GetClassID retrieves the 16-bit class ID of a managed entity from a ME Definition
57func (bme ManagedEntityDefinition) GetClassID() ClassID {
58 return bme.ClassID
59}
60
Holger Hildebrandte2439342020-12-03 16:06:54 +000061// SetClassID assigns the 16-bit class ID of a managed entity from a ME Definition
62func (bme *ManagedEntityDefinition) SetClassID(classID ClassID) {
63 bme.ClassID = classID
64}
65
Holger Hildebrandtfa074992020-03-27 15:42:06 +000066// GetMessageTypes retrieves the OMCI Message Types supporte3d by a managed entity from a ME Definition
67func (bme ManagedEntityDefinition) GetMessageTypes() mapset.Set {
68 return bme.MessageTypes
69}
70
71// GetAllowedAttributeMask retrieves the allowed/valid 16-bit attribute mask of a managed entity
72// from a ME Definition
73func (bme ManagedEntityDefinition) GetAllowedAttributeMask() uint16 {
74 return bme.AllowedAttributeMask
75}
76
77// GetAttributeDefinitions retrieves the attribute definitions of a managed entity from a ME Definition
78func (bme ManagedEntityDefinition) GetAttributeDefinitions() AttributeDefinitionMap {
79 return bme.AttributeDefinitions
80}
81
82func (bme ManagedEntityDefinition) DecodeAttributes(mask uint16, data []byte, p gopacket.PacketBuilder, msgType byte) (AttributeValueMap, error) {
83 if (mask | bme.GetAllowedAttributeMask()) != bme.GetAllowedAttributeMask() {
84 // TODO: Provide custom error code so a response 'result' can properly be coded
85 return nil, errors.New("unsupported attribute mask")
86 }
87 keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions)
88
89 attrMap := make(AttributeValueMap, bits.OnesCount16(mask))
90 for _, index := range keyList {
91 if index == 0 {
92 continue // Skip Entity ID
93 }
94 attrDef := bme.AttributeDefinitions[index]
95 name := attrDef.GetName()
96
97 if mask&attrDef.Mask != 0 {
98 value, err := attrDef.Decode(data, p, msgType)
99 if err != nil {
100 return nil, err
101 }
102 if attrDef.IsTableAttribute() {
103 switch msgType {
104 default:
105 return nil, fmt.Errorf("unsupported Message Type '%v' for table serialization", msgType)
106
107 case byte(Get) | AK: // Get Response
108 attrMap[name] = value
109 data = data[4:]
110
111 case byte(GetNext) | AK: // Get Next Response
112 // Value is a partial octet buffer we need to collect and at
113 // the end (last segment) pull it up into more appropriate table
114 // rows
115 valueBuffer, ok := value.([]byte)
116 if !ok {
117 panic("unexpected type already returned as get-next-response attribute data")
118 }
119 if existing, found := attrMap[name]; found {
120 prev, ok := existing.([]byte)
121 if !ok {
122 panic("unexpected type already in attribute value map")
123 }
124 attrMap[name] = append(prev, valueBuffer...)
125 } else {
126 attrMap[name] = valueBuffer
127 }
128 if size := attrDef.GetSize(); size != 0 && size > len(valueBuffer) {
129 panic("unexpected size difference")
130 }
131 data = data[len(valueBuffer):]
132
133 case byte(Set) | AR: // Set Request
134 fmt.Println("TODO")
135
136 case byte(SetTable) | AR: // Set Table Request
137 // TODO: Only baseline supported at this time
138 return nil, errors.New("attribute encode for set-table-request not yet supported")
139 }
140 } else {
141 attrMap[name] = value
142 data = data[attrDef.GetSize():]
143 }
144 }
145 }
146 return attrMap, nil
147}
148
149func (bme ManagedEntityDefinition) SerializeAttributes(attr AttributeValueMap, mask uint16,
150 b gopacket.SerializeBuffer, msgType byte, bytesAvailable int, packData bool) (error, uint16) {
151
152 if (mask | bme.GetAllowedAttributeMask()) != bme.GetAllowedAttributeMask() {
153 // TODO: Provide custom error code so a response 'result' can properly be coded
154 return errors.New("unsupported attribute mask"), 0
155 }
156 // TODO: Need to limit number of bytes appended to not exceed packet size
157 // Is there space/metadata info in 'b' parameter to allow this?
158 keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions)
159 var failedMask uint16
160
161 for _, index := range keyList {
162 if index == 0 {
163 continue // Skip Entity ID
164 }
165 attrDef := bme.AttributeDefinitions[index]
166
167 if mask&attrDef.Mask != 0 {
168 value, ok := attr[attrDef.GetName()]
169 if !ok {
170 msg := fmt.Sprintf("attribute not found: '%v'", attrDef.GetName())
171 return errors.New(msg), failedMask
172 }
173 size, err := attrDef.SerializeTo(value, b, msgType, bytesAvailable)
174 if err != nil {
175 failedMask |= attrDef.Mask
176 if packData {
177 continue
178 }
179 return err, failedMask
180 }
181 bytesAvailable -= size
182 }
183 }
184 return nil, failedMask
185}