Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package omci |
| 18 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 23 | "github.com/opencord/omci-lib-go/v2" |
| 24 | me "github.com/opencord/omci-lib-go/v2/generated" |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 25 | log "github.com/sirupsen/logrus" |
| 26 | ) |
| 27 | |
| 28 | type OnuAlarmInfo struct { |
| 29 | SequenceNo uint8 |
| 30 | AlarmBitMap [28]byte |
| 31 | } |
| 32 | type OnuAlarmInfoMapKey struct { |
| 33 | MeInstance uint16 |
| 34 | MeClassID me.ClassID |
| 35 | } |
| 36 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 37 | // CreateUniStatusAlarm will generate an Alarm packet to report that the Link is UP or DOWN |
| 38 | // as a consequence of a SetRequest on PhysicalPathTerminationPointEthernetUniClassID |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 39 | func CreateUniStatusAlarm(raiseAlarm bool, entityId uint16, sequenceNo uint8) ([]byte, [28]byte) { |
| 40 | notif := &omci.AlarmNotificationMsg{ |
| 41 | MeBasePacket: omci.MeBasePacket{ |
| 42 | EntityClass: me.PhysicalPathTerminationPointEthernetUniClassID, |
| 43 | EntityInstance: entityId, |
| 44 | }, |
| 45 | AlarmSequenceNumber: byte(sequenceNo), |
| 46 | } |
| 47 | if raiseAlarm { |
| 48 | //PPTP class has only 0th bit alarm for UNI LOS |
| 49 | if err := notif.ActivateAlarm(0); err != nil { |
| 50 | omciLogger.WithFields(log.Fields{ |
| 51 | "Err": err, |
| 52 | }).Error("Cannot Create AlarmNotificationMsg") |
| 53 | return nil, [28]byte{} |
| 54 | } |
| 55 | } else { |
| 56 | if err := notif.ClearAlarm(0); err != nil { |
| 57 | omciLogger.WithFields(log.Fields{ |
| 58 | "Err": err, |
| 59 | }).Error("Cannot Create AlarmNotificationMsg") |
| 60 | return nil, [28]byte{} |
| 61 | } |
| 62 | } |
| 63 | pkt, err := Serialize(omci.AlarmNotificationType, notif, 0) |
| 64 | if err != nil { |
| 65 | omciLogger.WithFields(log.Fields{ |
| 66 | "Err": err, |
| 67 | }).Error("Cannot Serialize AlarmNotificationMsg") |
| 68 | return nil, [28]byte{} |
| 69 | } |
| 70 | return pkt, notif.AlarmBitmap |
| 71 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 72 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 73 | func CreateGetAllAlarmsResponse(tid uint16, onuAlarmDetails map[OnuAlarmInfoMapKey]OnuAlarmInfo) ([]byte, error) { |
| 74 | var alarmEntityClass me.ClassID |
| 75 | var meInstance uint16 |
| 76 | var noOfCommands uint16 = 0 |
| 77 | alarmEntityClass = me.PhysicalPathTerminationPointEthernetUniClassID //Currently doing for PPTP classID |
| 78 | meInstance = 257 |
| 79 | key := OnuAlarmInfoMapKey{ |
| 80 | MeInstance: meInstance, |
| 81 | MeClassID: alarmEntityClass, |
| 82 | } |
| 83 | if _, ok := onuAlarmDetails[key]; ok { |
| 84 | noOfCommands = 1 |
| 85 | } |
| 86 | numberOfCommands := uint16(noOfCommands) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 87 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 88 | request := &omci.GetAllAlarmsResponse{ |
| 89 | MeBasePacket: omci.MeBasePacket{ |
| 90 | EntityClass: me.OnuDataClassID, |
| 91 | }, |
| 92 | NumberOfCommands: numberOfCommands, |
| 93 | } |
| 94 | pkt, err := Serialize(omci.GetAllAlarmsResponseType, request, tid) |
| 95 | if err != nil { |
| 96 | omciLogger.WithFields(log.Fields{ |
| 97 | "Err": err, |
| 98 | }).Error("Cannot Serialize GetAllAlarmsResponse") |
| 99 | return nil, err |
| 100 | } |
| 101 | return pkt, nil |
| 102 | } |
| 103 | func ParseGetAllAlarmsNextRequest(omciPkt gopacket.Packet) (*omci.GetAllAlarmsNextRequest, error) { |
| 104 | msgLayer := omciPkt.Layer(omci.LayerTypeGetAllAlarmsNextRequest) |
| 105 | if msgLayer == nil { |
| 106 | err := "omci Msg layer could not be detected for LayerTypeGetAllAlarmsNextRequest" |
| 107 | omciLogger.Error(err) |
| 108 | return nil, errors.New(err) |
| 109 | } |
| 110 | msgObj, msgOk := msgLayer.(*omci.GetAllAlarmsNextRequest) |
| 111 | if !msgOk { |
| 112 | err := "omci Msg layer could not be assigned for GetAllAlarmsNextRequest" |
| 113 | omciLogger.Error(err) |
| 114 | return nil, errors.New(err) |
| 115 | } |
| 116 | return msgObj, nil |
| 117 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 118 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 119 | func CreateGetAllAlarmsNextResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, onuAlarmDetails map[OnuAlarmInfoMapKey]OnuAlarmInfo) ([]byte, error) { |
| 120 | |
| 121 | msgObj, err := ParseGetAllAlarmsNextRequest(omciPkt) |
| 122 | if err != nil { |
| 123 | err := "omci Msg layer could not be assigned for LayerTypeGetRequest" |
| 124 | omciLogger.Error(err) |
| 125 | return nil, errors.New(err) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Himani Chawla | 13b1ee0 | 2021-03-15 01:43:53 +0530 | [diff] [blame] | 128 | omciLogger.WithFields(log.Fields{ |
| 129 | "EntityClass": msgObj.EntityClass, |
| 130 | "EntityInstance": msgObj.EntityInstance, |
| 131 | "CommandSequenceNumber": msgObj.CommandSequenceNumber, |
| 132 | }).Trace("received-omci-get-all-alarms-next-request") |
| 133 | |
| 134 | var alarmEntityClass me.ClassID |
| 135 | var meInstance uint16 |
| 136 | var alarmBitMap [28]byte |
| 137 | |
| 138 | switch msgObj.CommandSequenceNumber { |
| 139 | case 0: |
| 140 | alarmEntityClass = me.PhysicalPathTerminationPointEthernetUniClassID |
| 141 | meInstance = 257 |
| 142 | //Checking if the alarm is raised in the bitmap, we will send clear just to generate missed clear alarm and |
| 143 | // vice versa. |
| 144 | key := OnuAlarmInfoMapKey{ |
| 145 | MeInstance: meInstance, |
| 146 | MeClassID: alarmEntityClass, |
| 147 | } |
| 148 | if alarmInfo, ok := onuAlarmDetails[key]; ok { |
| 149 | alarmBitMap = alarmInfo.AlarmBitMap |
| 150 | } else { |
| 151 | return nil, fmt.Errorf("alarm-info-for-me-not-present-in-alarm-info-map") |
| 152 | } |
| 153 | default: |
| 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 | } |
| 157 | |
| 158 | response := &omci.GetAllAlarmsNextResponse{ |
| 159 | MeBasePacket: omci.MeBasePacket{ |
| 160 | EntityClass: me.OnuDataClassID, |
| 161 | }, |
| 162 | AlarmEntityClass: alarmEntityClass, |
| 163 | AlarmEntityInstance: meInstance, |
| 164 | AlarmBitMap: alarmBitMap, |
| 165 | } |
| 166 | |
| 167 | omciLogger.WithFields(log.Fields{ |
| 168 | "AlarmEntityClass": alarmEntityClass, |
| 169 | "AlarmEntityInstance": meInstance, |
| 170 | "AlarmBitMap": alarmBitMap, |
| 171 | }).Trace("created-omci-getAllAlarmsNext-response") |
| 172 | |
| 173 | pkt, err := Serialize(omci.GetAllAlarmsNextResponseType, response, omciMsg.TransactionID) |
| 174 | |
| 175 | if err != nil { |
| 176 | omciLogger.WithFields(log.Fields{ |
| 177 | "Err": err, |
| 178 | }).Fatalf("Cannot Serialize GetAllAlarmsNextRequest") |
| 179 | return nil, err |
| 180 | } |
| 181 | |
| 182 | return pkt, nil |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 183 | } |