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 | */ |
Chip Boling | 34ebcb6 | 2021-02-02 12:13:58 -0600 | [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 | "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 |
| 35 | type ManagedEntityDefinition struct { |
Chip Boling | 34ebcb6 | 2021-02-02 12:13:58 -0600 | [diff] [blame^] | 36 | Name string |
| 37 | ClassID ClassID |
| 38 | MessageTypes mapset.Set // Mandatory |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 39 | AllowedAttributeMask uint16 |
| 40 | AttributeDefinitions AttributeDefinitionMap |
| 41 | Access ClassAccess |
| 42 | Support ClassSupport |
Chip Boling | 34ebcb6 | 2021-02-02 12:13:58 -0600 | [diff] [blame^] | 43 | Alarms AlarmMap // AlarmBit -> AlarmName |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func (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 |
| 52 | func (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 |
| 57 | func (bme ManagedEntityDefinition) GetClassID() ClassID { |
| 58 | return bme.ClassID |
| 59 | } |
| 60 | |
Chip Boling | 8a5379a | 2020-11-17 12:59:40 -0600 | [diff] [blame] | 61 | // SetClassID assigns the 16-bit class ID of a managed entity from a ME Definition |
| 62 | func (bme *ManagedEntityDefinition) SetClassID(classID ClassID) { |
| 63 | bme.ClassID = classID |
| 64 | } |
| 65 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 66 | // GetMessageTypes retrieves the OMCI Message Types supporte3d by a managed entity from a ME Definition |
| 67 | func (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 |
| 73 | func (bme ManagedEntityDefinition) GetAllowedAttributeMask() uint16 { |
| 74 | return bme.AllowedAttributeMask |
| 75 | } |
| 76 | |
| 77 | // GetAttributeDefinitions retrieves the attribute definitions of a managed entity from a ME Definition |
| 78 | func (bme ManagedEntityDefinition) GetAttributeDefinitions() AttributeDefinitionMap { |
| 79 | return bme.AttributeDefinitions |
| 80 | } |
| 81 | |
Chip Boling | 34ebcb6 | 2021-02-02 12:13:58 -0600 | [diff] [blame^] | 82 | // GetClassSupport returns ONUs support of this class |
| 83 | func (bme ManagedEntityDefinition) GetClassSupport() ClassSupport { |
| 84 | return bme.Support |
| 85 | } |
| 86 | |
| 87 | // GetAlarmMap returns the Alarm bit number to name map |
| 88 | func (bme ManagedEntityDefinition) GetAlarmMap() AlarmMap { |
| 89 | return bme.Alarms |
| 90 | } |
| 91 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 92 | func (bme ManagedEntityDefinition) DecodeAttributes(mask uint16, data []byte, p gopacket.PacketBuilder, msgType byte) (AttributeValueMap, error) { |
| 93 | if (mask | bme.GetAllowedAttributeMask()) != bme.GetAllowedAttributeMask() { |
| 94 | // TODO: Provide custom error code so a response 'result' can properly be coded |
| 95 | return nil, errors.New("unsupported attribute mask") |
| 96 | } |
| 97 | keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions) |
| 98 | |
| 99 | attrMap := make(AttributeValueMap, bits.OnesCount16(mask)) |
| 100 | for _, index := range keyList { |
| 101 | if index == 0 { |
| 102 | continue // Skip Entity ID |
| 103 | } |
| 104 | attrDef := bme.AttributeDefinitions[index] |
| 105 | name := attrDef.GetName() |
| 106 | |
| 107 | if mask&attrDef.Mask != 0 { |
| 108 | value, err := attrDef.Decode(data, p, msgType) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | if attrDef.IsTableAttribute() { |
| 113 | switch msgType { |
| 114 | default: |
| 115 | return nil, fmt.Errorf("unsupported Message Type '%v' for table serialization", msgType) |
| 116 | |
| 117 | case byte(Get) | AK: // Get Response |
| 118 | attrMap[name] = value |
| 119 | data = data[4:] |
| 120 | |
| 121 | case byte(GetNext) | AK: // Get Next Response |
| 122 | // Value is a partial octet buffer we need to collect and at |
| 123 | // the end (last segment) pull it up into more appropriate table |
| 124 | // rows |
| 125 | valueBuffer, ok := value.([]byte) |
| 126 | if !ok { |
| 127 | panic("unexpected type already returned as get-next-response attribute data") |
| 128 | } |
| 129 | if existing, found := attrMap[name]; found { |
| 130 | prev, ok := existing.([]byte) |
| 131 | if !ok { |
| 132 | panic("unexpected type already in attribute value map") |
| 133 | } |
| 134 | attrMap[name] = append(prev, valueBuffer...) |
| 135 | } else { |
| 136 | attrMap[name] = valueBuffer |
| 137 | } |
| 138 | if size := attrDef.GetSize(); size != 0 && size > len(valueBuffer) { |
| 139 | panic("unexpected size difference") |
| 140 | } |
| 141 | data = data[len(valueBuffer):] |
| 142 | |
| 143 | case byte(Set) | AR: // Set Request |
| 144 | fmt.Println("TODO") |
| 145 | |
| 146 | case byte(SetTable) | AR: // Set Table Request |
| 147 | // TODO: Only baseline supported at this time |
| 148 | return nil, errors.New("attribute encode for set-table-request not yet supported") |
| 149 | } |
| 150 | } else { |
| 151 | attrMap[name] = value |
| 152 | data = data[attrDef.GetSize():] |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | return attrMap, nil |
| 157 | } |
| 158 | |
| 159 | func (bme ManagedEntityDefinition) SerializeAttributes(attr AttributeValueMap, mask uint16, |
| 160 | b gopacket.SerializeBuffer, msgType byte, bytesAvailable int, packData bool) (error, uint16) { |
| 161 | |
| 162 | if (mask | bme.GetAllowedAttributeMask()) != bme.GetAllowedAttributeMask() { |
| 163 | // TODO: Provide custom error code so a response 'result' can properly be coded |
| 164 | return errors.New("unsupported attribute mask"), 0 |
| 165 | } |
| 166 | // TODO: Need to limit number of bytes appended to not exceed packet size |
| 167 | // Is there space/metadata info in 'b' parameter to allow this? |
| 168 | keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions) |
| 169 | var failedMask uint16 |
| 170 | |
| 171 | for _, index := range keyList { |
| 172 | if index == 0 { |
| 173 | continue // Skip Entity ID |
| 174 | } |
| 175 | attrDef := bme.AttributeDefinitions[index] |
| 176 | |
| 177 | if mask&attrDef.Mask != 0 { |
| 178 | value, ok := attr[attrDef.GetName()] |
| 179 | if !ok { |
| 180 | msg := fmt.Sprintf("attribute not found: '%v'", attrDef.GetName()) |
| 181 | return errors.New(msg), failedMask |
| 182 | } |
| 183 | size, err := attrDef.SerializeTo(value, b, msgType, bytesAvailable) |
| 184 | if err != nil { |
| 185 | failedMask |= attrDef.Mask |
| 186 | if packData { |
| 187 | continue |
| 188 | } |
| 189 | return err, failedMask |
| 190 | } |
| 191 | bytesAvailable -= size |
| 192 | } |
| 193 | } |
| 194 | return nil, failedMask |
| 195 | } |