Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
Himani Chawla | ac1f5ad | 2021-02-04 21:21:54 +0530 | [diff] [blame] | 17 | /* |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [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 { |
Himani Chawla | ac1f5ad | 2021-02-04 21:21:54 +0530 | [diff] [blame] | 36 | Name string |
| 37 | ClassID ClassID |
| 38 | MessageTypes mapset.Set // Mandatory |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 39 | AllowedAttributeMask uint16 |
| 40 | AttributeDefinitions AttributeDefinitionMap |
| 41 | Access ClassAccess |
| 42 | Support ClassSupport |
Himani Chawla | ac1f5ad | 2021-02-04 21:21:54 +0530 | [diff] [blame] | 43 | Alarms AlarmMap // AlarmBit -> AlarmName |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [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 | |
Holger Hildebrandt | e243934 | 2020-12-03 16:06:54 +0000 | [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 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [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 | |
Himani Chawla | ac1f5ad | 2021-02-04 21:21:54 +0530 | [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 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 92 | func (bme ManagedEntityDefinition) DecodeAttributes(mask uint16, data []byte, p gopacket.PacketBuilder, msgType byte) (AttributeValueMap, error) { |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 93 | badMask := (mask | bme.GetAllowedAttributeMask()) ^ bme.GetAllowedAttributeMask() |
| 94 | |
| 95 | var maskErr error |
| 96 | if badMask != 0 { |
| 97 | maskErr = fmt.Errorf("unsupported attribute mask %#x, valid: %#x for ME %v (Class ID: %d)", |
| 98 | mask, bme.GetAllowedAttributeMask(), bme.GetName(), bme.ClassID) |
| 99 | mask &= bme.GetAllowedAttributeMask() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 100 | } |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 101 | // Process known attributes |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 102 | keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions) |
| 103 | |
| 104 | attrMap := make(AttributeValueMap, bits.OnesCount16(mask)) |
| 105 | for _, index := range keyList { |
| 106 | if index == 0 { |
| 107 | continue // Skip Entity ID |
| 108 | } |
| 109 | attrDef := bme.AttributeDefinitions[index] |
| 110 | name := attrDef.GetName() |
| 111 | |
| 112 | if mask&attrDef.Mask != 0 { |
| 113 | value, err := attrDef.Decode(data, p, msgType) |
| 114 | if err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | if attrDef.IsTableAttribute() { |
| 118 | switch msgType { |
| 119 | default: |
mpagenko | 7033f4e | 2021-11-19 18:04:22 +0000 | [diff] [blame^] | 120 | return nil, fmt.Errorf("unsupported Message Type '%v/0x%02x' for table decode", |
| 121 | MsgType(msgType&MsgTypeMask), msgType) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 122 | |
| 123 | case byte(Get) | AK: // Get Response |
| 124 | attrMap[name] = value |
| 125 | data = data[4:] |
| 126 | |
| 127 | case byte(GetNext) | AK: // Get Next Response |
| 128 | // Value is a partial octet buffer we need to collect and at |
| 129 | // the end (last segment) pull it up into more appropriate table |
| 130 | // rows |
| 131 | valueBuffer, ok := value.([]byte) |
| 132 | if !ok { |
| 133 | panic("unexpected type already returned as get-next-response attribute data") |
| 134 | } |
| 135 | if existing, found := attrMap[name]; found { |
| 136 | prev, ok := existing.([]byte) |
| 137 | if !ok { |
| 138 | panic("unexpected type already in attribute value map") |
| 139 | } |
| 140 | attrMap[name] = append(prev, valueBuffer...) |
| 141 | } else { |
| 142 | attrMap[name] = valueBuffer |
| 143 | } |
| 144 | if size := attrDef.GetSize(); size != 0 && size > len(valueBuffer) { |
| 145 | panic("unexpected size difference") |
| 146 | } |
| 147 | data = data[len(valueBuffer):] |
| 148 | |
| 149 | case byte(Set) | AR: // Set Request |
Girish Gowdra | 6afb56a | 2021-04-27 17:47:57 -0700 | [diff] [blame] | 150 | // TODO: No support at this time |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 151 | |
| 152 | case byte(SetTable) | AR: // Set Table Request |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 153 | attrMap[name] = value |
| 154 | data = data[len(data):] |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 155 | } |
| 156 | } else { |
| 157 | attrMap[name] = value |
| 158 | data = data[attrDef.GetSize():] |
| 159 | } |
| 160 | } |
| 161 | } |
mpagenko | 836a1fd | 2021-11-01 16:12:42 +0000 | [diff] [blame] | 162 | // If badMask is non-zero. Handle it by re-encoding the error as a custom relaxed |
| 163 | // decode error that the caller of this decode can process if they wish to relax |
| 164 | // the decoding |
| 165 | if badMask != 0 { |
| 166 | maskErr = NewUnknownAttributeDecodeError(maskErr.Error(), badMask, data) |
| 167 | } |
| 168 | return attrMap, maskErr |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | func (bme ManagedEntityDefinition) SerializeAttributes(attr AttributeValueMap, mask uint16, |
| 172 | b gopacket.SerializeBuffer, msgType byte, bytesAvailable int, packData bool) (error, uint16) { |
| 173 | |
| 174 | if (mask | bme.GetAllowedAttributeMask()) != bme.GetAllowedAttributeMask() { |
| 175 | // TODO: Provide custom error code so a response 'result' can properly be coded |
| 176 | return errors.New("unsupported attribute mask"), 0 |
| 177 | } |
| 178 | // TODO: Need to limit number of bytes appended to not exceed packet size |
| 179 | // Is there space/metadata info in 'b' parameter to allow this? |
| 180 | keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions) |
| 181 | var failedMask uint16 |
| 182 | |
| 183 | for _, index := range keyList { |
| 184 | if index == 0 { |
| 185 | continue // Skip Entity ID |
| 186 | } |
| 187 | attrDef := bme.AttributeDefinitions[index] |
| 188 | |
| 189 | if mask&attrDef.Mask != 0 { |
| 190 | value, ok := attr[attrDef.GetName()] |
| 191 | if !ok { |
| 192 | msg := fmt.Sprintf("attribute not found: '%v'", attrDef.GetName()) |
| 193 | return errors.New(msg), failedMask |
| 194 | } |
| 195 | size, err := attrDef.SerializeTo(value, b, msgType, bytesAvailable) |
| 196 | if err != nil { |
| 197 | failedMask |= attrDef.Mask |
| 198 | if packData { |
| 199 | continue |
| 200 | } |
| 201 | return err, failedMask |
| 202 | } |
| 203 | bytesAvailable -= size |
| 204 | } |
| 205 | } |
| 206 | return nil, failedMask |
| 207 | } |