Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [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 | */ |
| 17 | |
| 18 | package meframe |
| 19 | |
| 20 | import ( |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "github.com/google/gopacket" |
| 24 | . "github.com/opencord/omci-lib-go" |
| 25 | me "github.com/opencord/omci-lib-go/generated" |
| 26 | ) |
| 27 | |
| 28 | func GetNextRequestFrame(m *me.ManagedEntity, opt options) (gopacket.SerializableLayer, error) { |
| 29 | if opt.frameFormat == ExtendedIdent { |
| 30 | return nil, errors.New("extended message set for this message type is not supported") |
| 31 | } |
| 32 | // Validate attribute mask |
| 33 | mask, err := checkAttributeMask(m, opt.attributeMask) |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | // Now scan attributes and reduce mask to only those requested |
| 38 | mask, err = calculateAttributeMask(m, mask) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | if mask == 0 { |
| 43 | return nil, errors.New("no attributes requested for GetNextRequest") |
| 44 | } |
| 45 | // TODO: If more than one attribute or the attribute requested is not a table attribute, return an error |
| 46 | // Common for all MEs |
| 47 | meLayer := &GetNextRequest{ |
| 48 | MeBasePacket: MeBasePacket{ |
| 49 | EntityClass: m.GetClassID(), |
| 50 | EntityInstance: m.GetEntityID(), |
| 51 | Extended: opt.frameFormat == ExtendedIdent, |
| 52 | }, |
| 53 | AttributeMask: mask, |
| 54 | SequenceNumber: opt.sequenceNumberCountOrSize, |
| 55 | } |
| 56 | return meLayer, nil |
| 57 | } |
| 58 | |
| 59 | func GetNextResponseFrame(m *me.ManagedEntity, opt options) (gopacket.SerializableLayer, error) { |
| 60 | if opt.frameFormat == ExtendedIdent { |
| 61 | return nil, errors.New("extended message set for this message type is not supported") |
| 62 | } |
| 63 | // Validate attribute mask |
| 64 | mask, err := checkAttributeMask(m, opt.attributeMask) |
| 65 | if err != nil { |
| 66 | return nil, err |
| 67 | } |
| 68 | mask, err = calculateAttributeMask(m, mask) |
| 69 | if err != nil { |
| 70 | return nil, err |
| 71 | } |
| 72 | // |
| 73 | // Common for all MEs |
| 74 | meLayer := &GetNextResponse{ |
| 75 | MeBasePacket: MeBasePacket{ |
| 76 | EntityClass: m.GetClassID(), |
| 77 | EntityInstance: m.GetEntityID(), |
| 78 | Extended: opt.frameFormat == ExtendedIdent, |
| 79 | }, |
| 80 | Result: opt.result, |
| 81 | AttributeMask: 0, |
| 82 | Attributes: make(me.AttributeValueMap), |
| 83 | } |
| 84 | if meLayer.Result == me.Success { |
| 85 | // Get payload space available |
| 86 | maxPayload := maxPacketAvailable(m, opt) |
| 87 | payloadAvailable := int(maxPayload) - 3 // Less results and attribute mask |
| 88 | meDefinition := m.GetManagedEntityDefinition() |
| 89 | attrDefs := meDefinition.GetAttributeDefinitions() |
| 90 | attrMap := m.GetAttributeValueMap() |
| 91 | |
| 92 | if mask == 0 { |
| 93 | return nil, errors.New("no attributes requested for GetNextResponse") |
| 94 | } |
| 95 | // TODO: If more than one attribute or the attribute requested is not a table attribute, return an error |
| 96 | // Iterate down the attributes (Attribute 0 is the ManagedEntity ID) |
| 97 | var attrIndex uint |
| 98 | for attrIndex = 1; attrIndex <= 16; attrIndex++ { |
| 99 | // Is this attribute requested |
| 100 | if mask&(1<<(16-attrIndex)) != 0 { |
| 101 | // Get definitions since we need the name |
| 102 | attrDef, ok := attrDefs[attrIndex] |
| 103 | if !ok { |
| 104 | msg := fmt.Sprintf("Unexpected error, index %v not valued for ME %v", |
| 105 | attrIndex, meDefinition.GetName()) |
| 106 | return nil, errors.New(msg) |
| 107 | } |
| 108 | var attrValue interface{} |
| 109 | attrValue, ok = attrMap[attrDef.Name] |
| 110 | if !ok || attrValue == nil { |
| 111 | msg := fmt.Sprintf("Unexpected error, attribute %v not provided in ME %v: %v", |
| 112 | attrDef.GetName(), meDefinition.GetName(), m) |
| 113 | return nil, errors.New(msg) |
| 114 | } |
| 115 | // Is space available? |
| 116 | if attrDef.Size <= payloadAvailable { |
| 117 | // Mark bit handled |
| 118 | mask &= ^attrDef.Mask |
| 119 | meLayer.AttributeMask |= attrDef.Mask |
| 120 | meLayer.Attributes[attrDef.Name] = attrValue |
| 121 | payloadAvailable -= attrDef.Size |
| 122 | } else { |
| 123 | // TODO: Should we set truncate? |
| 124 | msg := fmt.Sprintf("out-of-space. Cannot fit attribute %v into GetNextResponse message", |
| 125 | attrDef.GetName()) |
| 126 | return nil, me.NewMessageTruncatedError(msg) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | return meLayer, nil |
| 132 | } |