Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 8 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 10 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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 | */ |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 17 | /* |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 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 | |
| 24 | package generated |
| 25 | |
| 26 | import ( |
| 27 | "encoding/binary" |
| 28 | "errors" |
| 29 | "fmt" |
| 30 | "github.com/deckarep/golang-set" |
| 31 | "github.com/google/gopacket" |
| 32 | ) |
| 33 | |
| 34 | // ManagedEntity provides a complete instance of a Managed Entity |
| 35 | type ManagedEntity struct { |
| 36 | definition ManagedEntityDefinition |
| 37 | attributeMask uint16 |
| 38 | requestedAttributeMask uint16 |
| 39 | attributes AttributeValueMap |
| 40 | } |
| 41 | |
| 42 | // String provides a simple string that describes this struct |
| 43 | func (entity *ManagedEntity) String() string { |
| 44 | return fmt.Sprintf("ManagedEntity: %v, EntityID: (%d/%#x): Attributes: %v", |
| 45 | entity.GetClassID(), entity.GetEntityID(), entity.GetEntityID(), entity.attributes) |
| 46 | } |
| 47 | |
| 48 | // NewManagedEntity creates a ManagedEntity given an ME Definition and parameter/attribute data |
| 49 | func NewManagedEntity(definition ManagedEntityDefinition, params ...ParamData) (*ManagedEntity, OmciErrors) { |
| 50 | entity := &ManagedEntity{ |
| 51 | definition: definition, |
| 52 | attributes: make(map[string]interface{}), |
| 53 | } |
| 54 | if params != nil { |
| 55 | if err := entity.setAttributes(params...); err.StatusCode() != Success { |
| 56 | return nil, err |
| 57 | } |
| 58 | } |
| 59 | return entity, NewOmciSuccess() |
| 60 | } |
| 61 | |
| 62 | // GetManagedEntityDefinition provides the ME definition of a Managed Entity |
| 63 | func (entity *ManagedEntity) GetManagedEntityDefinition() ManagedEntityDefinition { |
| 64 | return entity.definition |
| 65 | } |
| 66 | |
| 67 | // GetName provides the ME Name of a Managed Entity |
| 68 | func (entity ManagedEntity) GetName() string { |
| 69 | return entity.definition.GetName() |
| 70 | } |
| 71 | |
| 72 | // GetClassID returns the 16-bit class ID of a Managed Entity |
| 73 | func (entity ManagedEntity) GetClassID() ClassID { |
| 74 | return entity.definition.GetClassID() |
| 75 | } |
| 76 | |
| 77 | // GetMessageTypes returns the OMCI message types that a Managed Entity supports |
| 78 | func (entity ManagedEntity) GetMessageTypes() mapset.Set { |
| 79 | return entity.definition.GetMessageTypes() |
| 80 | } |
| 81 | |
| 82 | // GetAllowedAttributeMask returns the 16-bit bitmask of attributes a Managed Entity supports |
| 83 | func (entity ManagedEntity) GetAllowedAttributeMask() uint16 { |
| 84 | return entity.definition.GetAllowedAttributeMask() |
| 85 | } |
| 86 | |
| 87 | // GetAttributeDefinitions returns the attribute definition map for a Managed Entity |
| 88 | func (entity ManagedEntity) GetAttributeDefinitions() AttributeDefinitionMap { |
| 89 | return entity.definition.GetAttributeDefinitions() |
| 90 | } |
| 91 | |
| 92 | // DecodeAttributes will decode the attributes portion of a Managed Entity frame/packet |
| 93 | func (entity *ManagedEntity) DecodeAttributes(mask uint16, data []byte, p gopacket.PacketBuilder, msgType byte) (AttributeValueMap, error) { |
| 94 | return entity.definition.DecodeAttributes(mask, data, p, msgType) |
| 95 | } |
| 96 | |
| 97 | // SerializeAttributes will serialize the attributes of a Managed Entity type |
| 98 | func (entity *ManagedEntity) SerializeAttributes(attr AttributeValueMap, mask uint16, |
| 99 | b gopacket.SerializeBuffer, msgType byte, bytesAvailable int, packData bool) (error, uint16) { |
| 100 | return entity.definition.SerializeAttributes(attr, mask, b, msgType, bytesAvailable, packData) |
| 101 | } |
| 102 | |
| 103 | // GetEntityID will return the Entity/Instance ID for a Managed Entity |
| 104 | func (entity *ManagedEntity) GetEntityID() uint16 { |
| 105 | if eid, err := entity.GetAttributeByIndex(0); err == nil { |
| 106 | return eid.(uint16) |
| 107 | } |
| 108 | return 0 |
| 109 | } |
| 110 | |
| 111 | // SetEntityID will set the Entity/Instance ID for a Managed Entity |
| 112 | func (entity *ManagedEntity) SetEntityID(eid uint16) error { |
| 113 | return entity.SetAttributeByIndex(0, eid) |
| 114 | } |
| 115 | |
| 116 | // GetAttributeMask will return the 16-bit attribute mask of a Managed Entity |
| 117 | func (entity *ManagedEntity) GetAttributeMask() uint16 { |
| 118 | return entity.attributeMask |
| 119 | } |
| 120 | |
| 121 | // SetRequestedAttributeMask is used to initialize the requested attribute mask to a specific |
| 122 | // value. This should only be done on "Get" type operations that need to fetch and attribute |
| 123 | // and store it in the entity. For other operations (create, set, ...) you should specify |
| 124 | // the attributes and values in the Params initialization or use the SetAttribute |
| 125 | func (entity *ManagedEntity) SetRequestedAttributeMask(mask uint16) { |
| 126 | entity.requestedAttributeMask = mask |
| 127 | } |
| 128 | |
| 129 | // GetRequestedAttributeMask will return the 16-bit requested attribute mask of a Managed Entity. |
| 130 | // This is only specified for requests that perform a Get operation |
| 131 | func (entity *ManagedEntity) GetRequestedAttributeMask() uint16 { |
| 132 | return entity.requestedAttributeMask |
| 133 | } |
| 134 | |
| 135 | // GetAttributeValueMap will return the map of attributes of a Managed Entity |
| 136 | func (entity *ManagedEntity) GetAttributeValueMap() AttributeValueMap { |
| 137 | return entity.attributes |
| 138 | } |
| 139 | |
| 140 | // GetAttribute will return the value of a specific attribute for the specified attribute by name |
| 141 | func (entity *ManagedEntity) GetAttribute(name string) (interface{}, error) { |
| 142 | value, ok := entity.attributes[name] |
| 143 | if !ok { |
| 144 | return 0, fmt.Errorf("attribute '%v' not found", name) |
| 145 | } |
| 146 | return value, nil |
| 147 | } |
| 148 | |
| 149 | // GetAttributeByIndex will return the value of a specific attribute for the specified attribute by index |
| 150 | func (entity *ManagedEntity) GetAttributeByIndex(index uint) (interface{}, error) { |
| 151 | if len(entity.attributes) == 0 { |
| 152 | return nil, errors.New("attributes have already been set") |
| 153 | } |
| 154 | if _, ok := entity.definition.AttributeDefinitions[index]; !ok { |
| 155 | return nil, fmt.Errorf("invalid attribute index: %d, should be 0..%d", |
| 156 | index, len(entity.definition.AttributeDefinitions)-1) |
| 157 | } |
| 158 | return entity.GetAttribute(entity.definition.AttributeDefinitions[index].Name) |
| 159 | } |
| 160 | |
| 161 | func (entity *ManagedEntity) setAttributes(params ...ParamData) OmciErrors { |
| 162 | if entity.attributes == nil { |
| 163 | entity.attributes = make(map[string]interface{}) |
| 164 | } else if len(entity.attributes) > 0 { |
| 165 | return NewNonStatusError("attributes have already been set") |
| 166 | } |
| 167 | eidName := entity.definition.AttributeDefinitions[0].Name |
| 168 | if len(params) == 0 { |
| 169 | entity.attributes[eidName] = uint16(0) |
| 170 | return NewOmciSuccess() |
| 171 | } |
| 172 | entity.attributes[eidName] = params[0].EntityID |
| 173 | |
| 174 | for name, value := range params[0].Attributes { |
| 175 | if name == eidName { |
| 176 | continue |
| 177 | } |
| 178 | if err := entity.SetAttribute(name, value); err.StatusCode() != Success { |
| 179 | return err |
| 180 | } |
| 181 | } |
| 182 | return NewOmciSuccess() |
| 183 | } |
| 184 | |
| 185 | // SetAttribute can be uses to set the value of a specific attribute by name |
| 186 | func (entity *ManagedEntity) SetAttribute(name string, value interface{}) OmciErrors { |
| 187 | attrDef, err := GetAttributeDefinitionByName(entity.definition.GetAttributeDefinitions(), name) |
| 188 | if err != nil { |
| 189 | // Not found, which means not in the attribute definition map |
| 190 | return NewProcessingError(err.Error()) |
| 191 | } else if entity.attributes == nil { |
| 192 | entity.attributes = make(map[string]interface{}) |
| 193 | } |
| 194 | mask := attrDef.Mask |
| 195 | // check any constraints |
| 196 | if constraintCheck := attrDef.GetConstraints(); constraintCheck != nil { |
| 197 | err = constraintCheck(value) |
| 198 | if err != nil { |
| 199 | return NewParameterError(mask, entity.GetAttributeDefinitions(), err) |
| 200 | } |
| 201 | } |
| 202 | entity.attributes[name] = value |
| 203 | entity.attributeMask |= mask |
| 204 | return NewOmciSuccess() |
| 205 | } |
| 206 | |
| 207 | // SetAttributeByIndex can be uses to set the value of a specific attribute by attribute index (0..15) |
| 208 | func (entity *ManagedEntity) SetAttributeByIndex(index uint, value interface{}) error { |
| 209 | attrDef, ok := entity.definition.AttributeDefinitions[index] |
| 210 | if !ok { |
| 211 | return fmt.Errorf("invalid attribute index: %d, should be 0..%d", |
| 212 | index, len(entity.definition.AttributeDefinitions)-1) |
| 213 | } else if entity.attributes == nil { |
| 214 | entity.attributes = make(map[string]interface{}) |
| 215 | } |
| 216 | mask := attrDef.Mask |
| 217 | // check any constraints |
| 218 | if constraintCheck := attrDef.GetConstraints(); constraintCheck != nil { |
| 219 | err := constraintCheck(value) |
| 220 | if err != nil { |
| 221 | return NewParameterError(mask, entity.GetAttributeDefinitions(), err) |
| 222 | } |
| 223 | } |
| 224 | entity.attributes[attrDef.Name] = value |
| 225 | entity.attributeMask |= mask |
| 226 | return nil |
| 227 | } |
| 228 | |
| 229 | // DeleteAttribute is used to remove a specific attribute from a Managed Index by name |
| 230 | func (entity *ManagedEntity) DeleteAttribute(name string) error { |
| 231 | attrDef, err := GetAttributeDefinitionByName(entity.definition.GetAttributeDefinitions(), name) |
| 232 | if err != nil { |
| 233 | return err |
| 234 | } |
| 235 | if entity.attributes != nil { |
| 236 | delete(entity.attributes, name) |
| 237 | entity.attributeMask &= ^attrDef.Mask |
| 238 | } |
| 239 | return nil |
| 240 | } |
| 241 | |
| 242 | // DeleteAttributeByIndex is used to remove a specific attribute from a Managed Index by attribute index (0..15) |
| 243 | func (entity *ManagedEntity) DeleteAttributeByIndex(index uint) error { |
| 244 | attrDef, ok := entity.definition.AttributeDefinitions[index] |
| 245 | if !ok { |
| 246 | return fmt.Errorf("invalid attribute index: %d, should be 0..%d", |
| 247 | index, len(entity.definition.AttributeDefinitions)-1) |
| 248 | } |
| 249 | if entity.attributes != nil { |
| 250 | delete(entity.attributes, attrDef.Name) |
| 251 | entity.attributeMask &= ^attrDef.Mask |
| 252 | } |
| 253 | return nil |
| 254 | } |
| 255 | |
| 256 | // DecodeFromBytes decodes a Managed Entity give an octet stream pointing to the ME within a frame |
| 257 | func (entity *ManagedEntity) DecodeFromBytes(data []byte, p gopacket.PacketBuilder, msgType byte) error { |
| 258 | if len(data) < 6 { |
| 259 | p.SetTruncated() |
| 260 | return errors.New("frame too small") |
| 261 | } |
| 262 | classID := ClassID(binary.BigEndian.Uint16(data[0:2])) |
| 263 | entityID := binary.BigEndian.Uint16(data[2:4]) |
| 264 | parameters := ParamData{EntityID: entityID} |
| 265 | |
| 266 | meDefinition, omciErr := LoadManagedEntityDefinition(classID, parameters) |
| 267 | if omciErr.StatusCode() != Success { |
| 268 | return omciErr.GetError() |
| 269 | } |
| 270 | entity.definition = meDefinition.definition |
| 271 | entity.attributeMask = binary.BigEndian.Uint16(data[4:6]) |
| 272 | entity.attributes = make(map[string]interface{}) |
| 273 | entity.SetEntityID(entityID) |
| 274 | packetAttributes, err := entity.DecodeAttributes(entity.GetAttributeMask(), data[6:], p, msgType) |
| 275 | if err != nil { |
| 276 | return err |
| 277 | } |
| 278 | for name, value := range packetAttributes { |
| 279 | entity.attributes[name] = value |
| 280 | } |
| 281 | return nil |
| 282 | } |
| 283 | |
| 284 | // SerializeTo serializes a Managed Entity into an octet stream |
| 285 | func (entity *ManagedEntity) SerializeTo(b gopacket.SerializeBuffer, msgType byte, bytesAvailable int, opts gopacket.SerializeOptions) error { |
| 286 | // Add class ID and entity ID |
| 287 | bytes, err := b.AppendBytes(6) |
| 288 | if err != nil { |
| 289 | return err |
| 290 | } |
| 291 | binary.BigEndian.PutUint16(bytes, uint16(entity.GetClassID())) |
| 292 | binary.BigEndian.PutUint16(bytes[2:], entity.GetEntityID()) |
| 293 | binary.BigEndian.PutUint16(bytes[4:], entity.GetAttributeMask()) |
| 294 | |
| 295 | // TODO: Need to limit number of bytes appended to not exceed packet size |
| 296 | // Is there space/metadata info in 'b' parameter to allow this? |
| 297 | err, _ = entity.SerializeAttributes(entity.attributes, entity.GetAttributeMask(), b, |
| 298 | msgType, bytesAvailable, opts.FixLengths) |
| 299 | return err |
| 300 | } |