blob: e18a7373a29b03737605a9440654bc86fda09eb7 [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
2 * Copyright 2018-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package omci
18
Himani Chawla13b1ee02021-03-15 01:43:53 +053019import (
Holger Hildebrandt24d27422022-07-11 05:50:02 +000020 "encoding/hex"
Himani Chawla13b1ee02021-03-15 01:43:53 +053021 "errors"
22 "fmt"
Holger Hildebrandt24d27422022-07-11 05:50:02 +000023 "strconv"
24
Himani Chawla13b1ee02021-03-15 01:43:53 +053025 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020026 "github.com/opencord/omci-lib-go/v2"
27 me "github.com/opencord/omci-lib-go/v2/generated"
Himani Chawla13b1ee02021-03-15 01:43:53 +053028 log "github.com/sirupsen/logrus"
29)
30
31type OnuAlarmInfo struct {
32 SequenceNo uint8
33 AlarmBitMap [28]byte
34}
35type OnuAlarmInfoMapKey struct {
36 MeInstance uint16
37 MeClassID me.ClassID
38}
39
Matteo Scandolof9d43412021-01-12 11:11:34 -080040// CreateUniStatusAlarm will generate an Alarm packet to report that the Link is UP or DOWN
41// as a consequence of a SetRequest on PhysicalPathTerminationPointEthernetUniClassID
Himani Chawla13b1ee02021-03-15 01:43:53 +053042func CreateUniStatusAlarm(raiseAlarm bool, entityId uint16, sequenceNo uint8) ([]byte, [28]byte) {
43 notif := &omci.AlarmNotificationMsg{
44 MeBasePacket: omci.MeBasePacket{
45 EntityClass: me.PhysicalPathTerminationPointEthernetUniClassID,
46 EntityInstance: entityId,
47 },
48 AlarmSequenceNumber: byte(sequenceNo),
49 }
50 if raiseAlarm {
51 //PPTP class has only 0th bit alarm for UNI LOS
52 if err := notif.ActivateAlarm(0); err != nil {
53 omciLogger.WithFields(log.Fields{
54 "Err": err,
55 }).Error("Cannot Create AlarmNotificationMsg")
56 return nil, [28]byte{}
57 }
58 } else {
59 if err := notif.ClearAlarm(0); err != nil {
60 omciLogger.WithFields(log.Fields{
61 "Err": err,
62 }).Error("Cannot Create AlarmNotificationMsg")
63 return nil, [28]byte{}
64 }
65 }
66 pkt, err := Serialize(omci.AlarmNotificationType, notif, 0)
67 if err != nil {
68 omciLogger.WithFields(log.Fields{
69 "Err": err,
70 }).Error("Cannot Serialize AlarmNotificationMsg")
71 return nil, [28]byte{}
72 }
73 return pkt, notif.AlarmBitmap
74}
Matteo Scandolof9d43412021-01-12 11:11:34 -080075
Holger Hildebrandt24d27422022-07-11 05:50:02 +000076func CreateGetAllAlarmsResponse(omciMsg *omci.OMCI, onuAlarmDetails map[OnuAlarmInfoMapKey]OnuAlarmInfo) ([]byte, error) {
Himani Chawla13b1ee02021-03-15 01:43:53 +053077 var alarmEntityClass me.ClassID
Himani Chawla13b1ee02021-03-15 01:43:53 +053078 var noOfCommands uint16 = 0
Holger Hildebrandt24d27422022-07-11 05:50:02 +000079 var isExtended bool = false
Matteo Scandolof9d43412021-01-12 11:11:34 -080080
Holger Hildebrandt24d27422022-07-11 05:50:02 +000081 if omciMsg.DeviceIdentifier == omci.ExtendedIdent {
82 isExtended = true
83 }
84 alarmEntityClass = me.PhysicalPathTerminationPointEthernetUniClassID //Currently doing it for PPTP classID only
85
86 key := OnuAlarmInfoMapKey{
87 MeClassID: alarmEntityClass,
88 }
89 for i := 257; i < 261; i++ { //Currently doing it for up to four PPTP instances only
90 key.MeInstance = uint16(i)
91 if _, ok := onuAlarmDetails[key]; ok {
92 noOfCommands++
93 }
94 }
95 response := &omci.GetAllAlarmsResponse{
Himani Chawla13b1ee02021-03-15 01:43:53 +053096 MeBasePacket: omci.MeBasePacket{
97 EntityClass: me.OnuDataClassID,
Holger Hildebrandt24d27422022-07-11 05:50:02 +000098 Extended: isExtended,
Himani Chawla13b1ee02021-03-15 01:43:53 +053099 },
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000100 NumberOfCommands: noOfCommands,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530101 }
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000102 omciLayer := &omci.OMCI{
103 TransactionID: omciMsg.TransactionID,
104 MessageType: omci.GetAllAlarmsResponseType,
105 DeviceIdentifier: omciMsg.DeviceIdentifier,
106 }
107 var options gopacket.SerializeOptions
108 options.FixLengths = true
109
110 buffer := gopacket.NewSerializeBuffer()
111 err := gopacket.SerializeLayers(buffer, options, omciLayer, response)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530112 if err != nil {
113 omciLogger.WithFields(log.Fields{
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000114 "Err": err,
115 "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16),
116 }).Error("cannot-Serialize-GetAllAlarmsResponse")
Himani Chawla13b1ee02021-03-15 01:43:53 +0530117 return nil, err
118 }
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000119 pkt := buffer.Bytes()
120
121 log.WithFields(log.Fields{
122 "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16),
123 "pkt": hex.EncodeToString(pkt),
124 }).Trace("omci-get-all-alarms-response")
125
Himani Chawla13b1ee02021-03-15 01:43:53 +0530126 return pkt, nil
127}
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000128
Himani Chawla13b1ee02021-03-15 01:43:53 +0530129func ParseGetAllAlarmsNextRequest(omciPkt gopacket.Packet) (*omci.GetAllAlarmsNextRequest, error) {
130 msgLayer := omciPkt.Layer(omci.LayerTypeGetAllAlarmsNextRequest)
131 if msgLayer == nil {
132 err := "omci Msg layer could not be detected for LayerTypeGetAllAlarmsNextRequest"
133 omciLogger.Error(err)
134 return nil, errors.New(err)
135 }
136 msgObj, msgOk := msgLayer.(*omci.GetAllAlarmsNextRequest)
137 if !msgOk {
138 err := "omci Msg layer could not be assigned for GetAllAlarmsNextRequest"
139 omciLogger.Error(err)
140 return nil, errors.New(err)
141 }
142 return msgObj, nil
143}
Matteo Scandolof9d43412021-01-12 11:11:34 -0800144
Himani Chawla13b1ee02021-03-15 01:43:53 +0530145func CreateGetAllAlarmsNextResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, onuAlarmDetails map[OnuAlarmInfoMapKey]OnuAlarmInfo) ([]byte, error) {
146
147 msgObj, err := ParseGetAllAlarmsNextRequest(omciPkt)
148 if err != nil {
149 err := "omci Msg layer could not be assigned for LayerTypeGetRequest"
150 omciLogger.Error(err)
151 return nil, errors.New(err)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800152 }
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000153 if msgObj.CommandSequenceNumber > 4 { //Currently doing it for up to four PPTP instances only
Himani Chawla13b1ee02021-03-15 01:43:53 +0530154 omciLogger.Warn("unsupported-CommandSequenceNumber-in-get-all-alarm-next", msgObj.CommandSequenceNumber)
155 return nil, fmt.Errorf("unspported-command-sequence-number-in-get-all-alarms-next")
156 }
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000157 var alarmEntityClass me.ClassID
158 var meInstance uint16
159 var alarmBitMap [28]byte
160 var isExtended bool
161 var additionalAlarms omci.AdditionalAlarmsData
Himani Chawla13b1ee02021-03-15 01:43:53 +0530162
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000163 if omciMsg.DeviceIdentifier == omci.ExtendedIdent {
164 isExtended = true
165 } else {
166 isExtended = false
167 }
168 alarmEntityClass = me.PhysicalPathTerminationPointEthernetUniClassID //Currently doing it for PPTP classID only
169
170 key := OnuAlarmInfoMapKey{
171 MeClassID: alarmEntityClass,
172 }
173 meInstance = 257 + msgObj.CommandSequenceNumber
174 key.MeInstance = meInstance
175 if alarmInfo, ok := onuAlarmDetails[key]; ok {
176 alarmBitMap = alarmInfo.AlarmBitMap
177 } else {
178 return nil, fmt.Errorf("alarm-info-for-me-not-present-in-alarm-info-map")
179 }
Himani Chawla13b1ee02021-03-15 01:43:53 +0530180 response := &omci.GetAllAlarmsNextResponse{
181 MeBasePacket: omci.MeBasePacket{
182 EntityClass: me.OnuDataClassID,
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000183 Extended: isExtended,
Himani Chawla13b1ee02021-03-15 01:43:53 +0530184 },
185 AlarmEntityClass: alarmEntityClass,
186 AlarmEntityInstance: meInstance,
187 AlarmBitMap: alarmBitMap,
188 }
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000189 if isExtended && msgObj.CommandSequenceNumber == 0 {
190 for i := 258; i < 261; i++ {
191 key.MeInstance = uint16(i)
192 if addAlarmInfo, ok := onuAlarmDetails[key]; ok {
193 additionalAlarms.AlarmEntityClass = key.MeClassID
194 additionalAlarms.AlarmEntityInstance = uint16(i)
195 additionalAlarms.AlarmBitMap = addAlarmInfo.AlarmBitMap
196 response.AdditionalAlarms = append(response.AdditionalAlarms, additionalAlarms)
197 }
198 }
199 }
200 omciLayer := &omci.OMCI{
201 TransactionID: omciMsg.TransactionID,
202 MessageType: omci.GetAllAlarmsNextResponseType,
203 DeviceIdentifier: omciMsg.DeviceIdentifier,
204 }
205 var options gopacket.SerializeOptions
206 options.FixLengths = true
Himani Chawla13b1ee02021-03-15 01:43:53 +0530207
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000208 buffer := gopacket.NewSerializeBuffer()
209 err = gopacket.SerializeLayers(buffer, options, omciLayer, response)
Himani Chawla13b1ee02021-03-15 01:43:53 +0530210 if err != nil {
211 omciLogger.WithFields(log.Fields{
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000212 "Err": err,
213 "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16),
214 }).Error("cannot-Serialize-GetAllAlarmsNextResponse")
Himani Chawla13b1ee02021-03-15 01:43:53 +0530215 return nil, err
216 }
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000217 pkt := buffer.Bytes()
Himani Chawla13b1ee02021-03-15 01:43:53 +0530218
Holger Hildebrandt24d27422022-07-11 05:50:02 +0000219 log.WithFields(log.Fields{
220 "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16),
221 "pkt": hex.EncodeToString(pkt),
222 }).Trace("omci-get-all-alarms-next-response")
Himani Chawla13b1ee02021-03-15 01:43:53 +0530223 return pkt, nil
Matteo Scandolof9d43412021-01-12 11:11:34 -0800224}