blob: 1f126faa7baf8be265f42634c34b3708458230d2 [file] [log] [blame]
Chip Boling610117d2021-09-09 11:24:34 -05001/*
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
18package meframe
19
20import (
21 "errors"
22 "fmt"
23 "github.com/google/gopacket"
Andrea Campanellae0cd8232021-10-15 15:10:23 +020024 . "github.com/opencord/omci-lib-go/v2"
25 me "github.com/opencord/omci-lib-go/v2/generated"
Chip Boling610117d2021-09-09 11:24:34 -050026)
27
28func SetRequestFrame(m *me.ManagedEntity, opt options) (gopacket.SerializableLayer, error) {
29 mask, err := checkAttributeMask(m, opt.attributeMask)
30 if err != nil {
31 return nil, err
32 }
33 mask, err = calculateAttributeMask(m, mask)
34 if err != nil {
35 return nil, err
36 }
37 meDefinition := m.GetManagedEntityDefinition()
38 attrDefs := meDefinition.GetAttributeDefinitions()
39 attrMap := m.GetAttributeValueMap()
40
41 // Get payload space available
42 maxPayload := maxPacketAvailable(m, opt)
43 payloadAvailable := int(maxPayload) - 2 // Less attribute mask
44 if opt.frameFormat == ExtendedIdent {
45 return nil, errors.New("extended message set for this message type is not supported")
46 // payloadAvailable -= 2 // Less length
47 }
48 meLayer := &SetRequest{
49 MeBasePacket: MeBasePacket{
50 EntityClass: m.GetClassID(),
51 EntityInstance: m.GetEntityID(),
52 Extended: opt.frameFormat == ExtendedIdent,
53 },
54 AttributeMask: 0,
55 Attributes: make(me.AttributeValueMap),
56 }
57 for mask != 0 {
58 // Iterate down the attributes (Attribute 0 is the ManagedEntity ID)
59 var attrIndex uint
60 for attrIndex = 1; attrIndex <= 16; attrIndex++ {
61 // Is this attribute requested
62 if mask&(1<<(16-attrIndex)) != 0 {
63 // Get definitions since we need the name
64 attrDef, ok := attrDefs[attrIndex]
65 if !ok {
66 msg := fmt.Sprintf("Unexpected error, index %v not valued for ME %v",
67 attrIndex, meDefinition.GetName())
68 return nil, errors.New(msg)
69 }
70 var attrValue interface{}
71 attrValue, ok = attrMap[attrDef.Name]
72 if !ok {
73 msg := fmt.Sprintf("Unexpected error, attribute %v not provided in ME %v: %v",
74 attrDef.GetName(), meDefinition.GetName(), m)
75 return nil, errors.New(msg)
76 }
77 // Is space available?
78 if attrDef.Size <= payloadAvailable {
79 // Mark bit handled
80 mask &= ^attrDef.Mask
81 meLayer.AttributeMask |= attrDef.Mask
82 meLayer.Attributes[attrDef.Name] = attrValue
83 payloadAvailable -= attrDef.Size
84 } else {
85 // TODO: Should we set truncate?
86 msg := fmt.Sprintf("out-of-space. Cannot fit attribute %v into SetRequest message",
87 attrDef.GetName())
88 return nil, me.NewMessageTruncatedError(msg)
89 }
90 }
91 }
92 }
93 if meLayer.AttributeMask == 0 {
94 // TODO: Is a set request with no attributes valid?
95 return nil, errors.New("no attributes encoded for SetRequest")
96 }
97 return meLayer, nil
98}
99
100func SetResponseFrame(m *me.ManagedEntity, opt options) (gopacket.SerializableLayer, error) {
101 if opt.frameFormat == ExtendedIdent {
102 return nil, errors.New("extended message set for this message type is not supported")
103 }
104 meLayer := &SetResponse{
105 MeBasePacket: MeBasePacket{
106 EntityClass: m.GetClassID(),
107 EntityInstance: m.GetEntityID(),
108 Extended: opt.frameFormat == ExtendedIdent,
109 },
110 Result: opt.result,
111 }
112 if meLayer.Result == me.AttributeFailure {
113 meLayer.UnsupportedAttributeMask = opt.unsupportedMask
114 meLayer.FailedAttributeMask = opt.attrExecutionMask
115 }
116 return meLayer, nil
117}