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