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