Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -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 | package core |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
| 20 | "encoding/binary" |
| 21 | "errors" |
Matteo Scandolo | a002681 | 2019-08-20 11:01:32 -0700 | [diff] [blame] | 22 | "fmt" |
| 23 | log "github.com/sirupsen/logrus" |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
Matteo Scandolo | 203314b | 2019-08-26 14:28:42 -0700 | [diff] [blame] | 26 | // OMCI Sim definitions |
| 27 | |
| 28 | type ChMessageType int |
| 29 | |
| 30 | const ( |
| 31 | GemPortAdded ChMessageType = iota |
| 32 | ) |
| 33 | |
| 34 | func (m ChMessageType) String() string { |
| 35 | names := [...]string{ |
| 36 | "GemPortAdded", |
| 37 | } |
| 38 | return names[m] |
| 39 | } |
| 40 | |
| 41 | type OmciChMessageData struct { |
| 42 | IntfId uint32 |
| 43 | OnuId uint32 |
| 44 | } |
| 45 | |
| 46 | type OmciChMessage struct { |
| 47 | Type ChMessageType |
| 48 | Data OmciChMessageData |
| 49 | } |
| 50 | |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 51 | // |
| 52 | // OMCI definitions |
| 53 | // |
| 54 | |
| 55 | // OmciMsgType represents a OMCI message-type |
| 56 | type OmciMsgType byte |
| 57 | |
Matteo Scandolo | a002681 | 2019-08-20 11:01:32 -0700 | [diff] [blame] | 58 | func (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: |
Matteo Scandolo | 203314b | 2019-08-26 14:28:42 -0700 | [diff] [blame] | 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) |
Matteo Scandolo | a002681 | 2019-08-20 11:01:32 -0700 | [diff] [blame] | 119 | return string(t) |
| 120 | } |
| 121 | } |
| 122 | |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 123 | const ( |
| 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 | |
Matteo Scandolo | 203314b | 2019-08-26 14:28:42 -0700 | [diff] [blame] | 151 | |
| 152 | // OMCI Managed Entity Class |
| 153 | type OmciClass uint16 |
| 154 | |
| 155 | func (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 | |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 171 | const ( |
| 172 | // Managed Entity Class values |
Zdravko Bozakov | 3d76214 | 2019-07-15 16:57:17 +0200 | [diff] [blame] | 173 | EthernetPMHistoryData OmciClass = 24 |
| 174 | ONUG OmciClass = 256 |
| 175 | ANIG OmciClass = 263 |
| 176 | GEMPortNetworkCTP OmciClass = 268 |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 177 | ) |
| 178 | |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 179 | // OMCI Message Identifier |
| 180 | type OmciMessageIdentifier struct { |
| 181 | Class OmciClass |
| 182 | Instance uint16 |
| 183 | } |
| 184 | |
| 185 | type OmciContent [32]byte |
| 186 | |
| 187 | type OmciMessage struct { |
| 188 | TransactionId uint16 |
| 189 | MessageType OmciMsgType |
| 190 | DeviceId uint8 |
| 191 | MessageId OmciMessageIdentifier |
| 192 | Content OmciContent |
| 193 | } |
| 194 | |
| 195 | func 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 { |
Matteo Scandolo | a002681 | 2019-08-20 11:01:32 -0700 | [diff] [blame] | 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") |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 206 | } |
Mahir Gunyel | 133913f | 2019-02-14 19:57:07 -0800 | [diff] [blame] | 207 | /* Message Type = Set |
Zdravko Bozakov | 8e9d85a | 2019-05-27 21:02:59 +0200 | [diff] [blame] | 208 | 0... .... = Destination Bit: 0x0 |
| 209 | .1.. .... = Acknowledge Request: 0x1 |
| 210 | ..0. .... = Acknowledgement: 0x0 |
| 211 | ...0 1000 = Message Type: Set (8) |
Mahir Gunyel | 133913f | 2019-02-14 19:57:07 -0800 | [diff] [blame] | 212 | */ |
Matteo Scandolo | a002681 | 2019-08-20 11:01:32 -0700 | [diff] [blame] | 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 | |
Mahir Gunyel | 133913f | 2019-02-14 19:57:07 -0800 | [diff] [blame] | 223 | return m.TransactionId, m.DeviceId, m.MessageType & 0x1F, m.MessageId.Class, m.MessageId.Instance, m.Content, nil |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 224 | } |