blob: 7f7c5ab4a12bf23db46934eaa175fbe56044f0e6 [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 (
20 "errors"
21 "fmt"
22 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020023 "github.com/opencord/omci-lib-go/v2"
24 me "github.com/opencord/omci-lib-go/v2/generated"
Himani Chawla13b1ee02021-03-15 01:43:53 +053025 log "github.com/sirupsen/logrus"
26)
27
28type OnuAlarmInfo struct {
29 SequenceNo uint8
30 AlarmBitMap [28]byte
31}
32type OnuAlarmInfoMapKey struct {
33 MeInstance uint16
34 MeClassID me.ClassID
35}
36
Matteo Scandolof9d43412021-01-12 11:11:34 -080037// 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 Chawla13b1ee02021-03-15 01:43:53 +053039func 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 Scandolof9d43412021-01-12 11:11:34 -080072
Himani Chawla13b1ee02021-03-15 01:43:53 +053073func 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 Scandolof9d43412021-01-12 11:11:34 -080087
Himani Chawla13b1ee02021-03-15 01:43:53 +053088 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}
103func 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 Scandolof9d43412021-01-12 11:11:34 -0800118
Himani Chawla13b1ee02021-03-15 01:43:53 +0530119func 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 Scandolof9d43412021-01-12 11:11:34 -0800126 }
127
Himani Chawla13b1ee02021-03-15 01:43:53 +0530128 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 Scandolof9d43412021-01-12 11:11:34 -0800183}