blob: 90ea0af2539474a37e72184d872919ad7e048f9b [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001/*
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 */
16package core
17
18import (
19 "bytes"
20 "encoding/binary"
21 "errors"
22 "fmt"
23 log "github.com/sirupsen/logrus"
24)
25
26// OMCI Sim definitions
27
28type ChMessageType int
29
30const (
Matteo Scandolodf3f85d2020-01-15 12:50:48 -080031 GemPortAdded ChMessageType = 0
32 UniLinkUp ChMessageType = 1
33 UniLinkDown ChMessageType = 2
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070034)
35
36func (m ChMessageType) String() string {
37 names := [...]string{
38 "GemPortAdded",
Matteo Scandolodf3f85d2020-01-15 12:50:48 -080039 "UniLinkUp",
40 "UniLinkDown",
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070041 }
42 return names[m]
43}
44
45type OmciChMessageData struct {
46 IntfId uint32
47 OnuId uint32
48}
49
50type OmciChMessage struct {
51 Type ChMessageType
52 Data OmciChMessageData
Matteo Scandolodf3f85d2020-01-15 12:50:48 -080053 Packet []byte
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070054}
55
56//
57// OMCI definitions
58//
59
60// OmciMsgType represents a OMCI message-type
61type OmciMsgType byte
62
63func (t OmciMsgType) PrettyPrint() string {
64 switch t {
65 case Create:
66 return "Create"
67 case Delete:
68 return "Delete"
69 case Set:
70 return "Set"
71 case Get:
72 return "Get"
73 case GetAllAlarms:
74 return "GetAllAlarms"
75 case GetAllAlarmsNext:
76 return "GetAllAlarmsNext"
77 case MibUpload:
78 return "MibUpload"
79 case MibUploadNext:
80 return "MibUploadNext"
81 case MibReset:
82 return "MibReset"
83 case AlarmNotification:
84 return "AlarmNotification"
85 case AttributeValueChange:
86 return "AttributeValueChange"
87 case Test:
88 return "Test"
89 case StartSoftwareDownload:
90 return "StartSoftwareDownload"
91 case DownloadSection:
92 return "DownloadSection"
93 case EndSoftwareDownload:
94 return "EndSoftwareDownload"
95 case ActivateSoftware:
96 return "ActivateSoftware"
97 case CommitSoftware:
98 return "CommitSoftware"
99 case SynchronizeTime:
100 return "SynchronizeTime"
101 case Reboot:
102 return "Reboot"
103 case GetNext:
104 return "GetNext"
105 case TestResult:
106 return "TestResult"
107 case GetCurrentData:
108 return "GetCurrentData"
109 case SetTable:
110 return "SetTable"
111 default:
112 // FIXME
113 // msg="Cant't convert state 68 to string"
114 // msg="Cant't convert state 72 to string"
115 // msg="Cant't convert state 73 to string"
116 // msg="Cant't convert state 75 to string"
117 // msg="Cant't convert state 76 to string"
118 // msg="Cant't convert state 77 to string"
119 // msg="Cant't convert state 78 to string"
120 // msg="Cant't convert state 79 to string"
121 // msg="Cant't convert state 88 to string"
122
123 log.Tracef("Cant't convert OmciMsgType %v to string", t)
124 return string(t)
125 }
126}
127
128const (
129 // Message Types
130 _ = iota
131 Create OmciMsgType = 4
132 Delete OmciMsgType = 6
133 Set OmciMsgType = 8
134 Get OmciMsgType = 9
135 GetAllAlarms OmciMsgType = 11
136 GetAllAlarmsNext OmciMsgType = 12
137 MibUpload OmciMsgType = 13
138 MibUploadNext OmciMsgType = 14
139 MibReset OmciMsgType = 15
140 AlarmNotification OmciMsgType = 16
141 AttributeValueChange OmciMsgType = 17
142 Test OmciMsgType = 18
143 StartSoftwareDownload OmciMsgType = 19
144 DownloadSection OmciMsgType = 20
145 EndSoftwareDownload OmciMsgType = 21
146 ActivateSoftware OmciMsgType = 22
147 CommitSoftware OmciMsgType = 23
148 SynchronizeTime OmciMsgType = 24
149 Reboot OmciMsgType = 25
150 GetNext OmciMsgType = 26
151 TestResult OmciMsgType = 27
152 GetCurrentData OmciMsgType = 28
153 SetTable OmciMsgType = 29 // Defined in Extended Message Set Only
154)
155
156
157// OMCI Managed Entity Class
158type OmciClass uint16
159
160func (c OmciClass) PrettyPrint() string {
161 switch c {
Matteo Scandolo732c0752020-01-28 07:24:13 -0800162 case SoftwareImage:
163 return "SoftwareImage"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700164 case EthernetPMHistoryData:
165 return "EthernetPMHistoryData"
166 case ONUG:
167 return "ONUG"
Matteo Scandolo732c0752020-01-28 07:24:13 -0800168 case ONU2G:
169 return "ONU2G"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700170 case ANIG:
171 return "ANIG"
172 case GEMPortNetworkCTP:
173 return "GEMPortNetworkCTP"
174 default:
175 log.Tracef("Cant't convert OmciClass %v to string", c)
176 return string(c)
177 }
178}
179
180const (
181 // Managed Entity Class values
Matteo Scandolo732c0752020-01-28 07:24:13 -0800182 SoftwareImage OmciClass = 7
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700183 EthernetPMHistoryData OmciClass = 24
184 ONUG OmciClass = 256
Matteo Scandolo732c0752020-01-28 07:24:13 -0800185 ONU2G OmciClass = 257
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -0700186 ANIG OmciClass = 263
187 GEMPortNetworkCTP OmciClass = 268
188)
189
190// OMCI Message Identifier
191type OmciMessageIdentifier struct {
192 Class OmciClass
193 Instance uint16
194}
195
196type OmciContent [32]byte
197
198type OmciMessage struct {
199 TransactionId uint16
200 MessageType OmciMsgType
201 DeviceId uint8
202 MessageId OmciMessageIdentifier
203 Content OmciContent
204}
205
206func ParsePkt(pkt []byte) (uint16, uint8, OmciMsgType, OmciClass, uint16, OmciContent, error) {
207 var m OmciMessage
208
209 r := bytes.NewReader(pkt)
210
211 if err := binary.Read(r, binary.BigEndian, &m); err != nil {
212 log.WithFields(log.Fields{
213 "Packet": pkt,
214 "omciMsg": fmt.Sprintf("%x", pkt),
215 }).Errorf("Failed to read packet: %s", err)
216 return 0, 0, 0, 0, 0, OmciContent{}, errors.New("Failed to read packet")
217 }
218 /* Message Type = Set
219 0... .... = Destination Bit: 0x0
220 .1.. .... = Acknowledge Request: 0x1
221 ..0. .... = Acknowledgement: 0x0
222 ...0 1000 = Message Type: Set (8)
223 */
224
225 log.WithFields(log.Fields{
226 "TransactionId": m.TransactionId,
227 "MessageType": m.MessageType.PrettyPrint(),
228 "MeClass": m.MessageId.Class,
229 "MeInstance": m.MessageId.Instance,
230 "Conent": m.Content,
231 "Packet": pkt,
232 }).Tracef("Parsing OMCI Packet")
233
234 return m.TransactionId, m.DeviceId, m.MessageType & 0x1F, m.MessageId.Class, m.MessageId.Instance, m.Content, nil
235}