Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1 | /* |
Joey Armstrong | 14628cd | 2023-01-10 08:38:31 -0500 | [diff] [blame] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 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 | |
| 17 | package omci |
| 18 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 19 | import ( |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 20 | "encoding/hex" |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 21 | "errors" |
| 22 | "fmt" |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 23 | "strconv" |
| 24 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 25 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 26 | "github.com/opencord/omci-lib-go/v2" |
| 27 | me "github.com/opencord/omci-lib-go/v2/generated" |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 28 | log "github.com/sirupsen/logrus" |
| 29 | ) |
| 30 | |
| 31 | type OnuAlarmInfo struct { |
| 32 | SequenceNo uint8 |
| 33 | AlarmBitMap [28]byte |
| 34 | } |
| 35 | type OnuAlarmInfoMapKey struct { |
| 36 | MeInstance uint16 |
| 37 | MeClassID me.ClassID |
| 38 | } |
| 39 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 40 | // 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 Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 42 | func 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 75 | |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 76 | func CreateGetAllAlarmsResponse(omciMsg *omci.OMCI, onuAlarmDetails map[OnuAlarmInfoMapKey]OnuAlarmInfo) ([]byte, error) { |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 77 | var alarmEntityClass me.ClassID |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 78 | var noOfCommands uint16 = 0 |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 79 | var isExtended bool = false |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 80 | |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 81 | 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 Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 96 | MeBasePacket: omci.MeBasePacket{ |
| 97 | EntityClass: me.OnuDataClassID, |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 98 | Extended: isExtended, |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 99 | }, |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 100 | NumberOfCommands: noOfCommands, |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 101 | } |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 102 | 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 Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 112 | if err != nil { |
| 113 | omciLogger.WithFields(log.Fields{ |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 114 | "Err": err, |
| 115 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 116 | }).Error("cannot-Serialize-GetAllAlarmsResponse") |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 117 | return nil, err |
| 118 | } |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 119 | 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 Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 126 | return pkt, nil |
| 127 | } |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 128 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 129 | func 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 144 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 145 | func 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 152 | } |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 153 | if msgObj.CommandSequenceNumber > 4 { //Currently doing it for up to four PPTP instances only |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 154 | 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 Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 157 | var alarmEntityClass me.ClassID |
| 158 | var meInstance uint16 |
| 159 | var alarmBitMap [28]byte |
| 160 | var isExtended bool |
| 161 | var additionalAlarms omci.AdditionalAlarmsData |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 162 | |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 163 | 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 Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 180 | response := &omci.GetAllAlarmsNextResponse{ |
| 181 | MeBasePacket: omci.MeBasePacket{ |
| 182 | EntityClass: me.OnuDataClassID, |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 183 | Extended: isExtended, |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 184 | }, |
| 185 | AlarmEntityClass: alarmEntityClass, |
| 186 | AlarmEntityInstance: meInstance, |
| 187 | AlarmBitMap: alarmBitMap, |
| 188 | } |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 189 | 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 Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 207 | |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 208 | buffer := gopacket.NewSerializeBuffer() |
| 209 | err = gopacket.SerializeLayers(buffer, options, omciLayer, response) |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 210 | if err != nil { |
| 211 | omciLogger.WithFields(log.Fields{ |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 212 | "Err": err, |
| 213 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 214 | }).Error("cannot-Serialize-GetAllAlarmsNextResponse") |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 215 | return nil, err |
| 216 | } |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 217 | pkt := buffer.Bytes() |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 218 | |
Holger Hildebrandt | 24d2742 | 2022-07-11 05:50:02 +0000 | [diff] [blame] | 219 | 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 Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 223 | return pkt, nil |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 224 | } |