Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [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" |
| 22 | "fmt" |
| 23 | log "github.com/sirupsen/logrus" |
| 24 | ) |
| 25 | |
| 26 | // OMCI Sim definitions |
| 27 | |
| 28 | type ChMessageType int |
| 29 | |
| 30 | const ( |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 31 | GemPortAdded ChMessageType = 0 |
| 32 | UniLinkUp ChMessageType = 1 |
| 33 | UniLinkDown ChMessageType = 2 |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
| 36 | func (m ChMessageType) String() string { |
| 37 | names := [...]string{ |
| 38 | "GemPortAdded", |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 39 | "UniLinkUp", |
| 40 | "UniLinkDown", |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 41 | } |
| 42 | return names[m] |
| 43 | } |
| 44 | |
| 45 | type OmciChMessageData struct { |
| 46 | IntfId uint32 |
| 47 | OnuId uint32 |
| 48 | } |
| 49 | |
| 50 | type OmciChMessage struct { |
| 51 | Type ChMessageType |
| 52 | Data OmciChMessageData |
Matteo Scandolo | df3f85d | 2020-01-15 12:50:48 -0800 | [diff] [blame] | 53 | Packet []byte |
Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // |
| 57 | // OMCI definitions |
| 58 | // |
| 59 | |
| 60 | // OmciMsgType represents a OMCI message-type |
| 61 | type OmciMsgType byte |
| 62 | |
| 63 | func (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 | |
| 128 | const ( |
| 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 |
| 158 | type OmciClass uint16 |
| 159 | |
| 160 | func (c OmciClass) PrettyPrint() string { |
| 161 | switch c { |
| 162 | case EthernetPMHistoryData: |
| 163 | return "EthernetPMHistoryData" |
| 164 | case ONUG: |
| 165 | return "ONUG" |
| 166 | case ANIG: |
| 167 | return "ANIG" |
| 168 | case GEMPortNetworkCTP: |
| 169 | return "GEMPortNetworkCTP" |
| 170 | default: |
| 171 | log.Tracef("Cant't convert OmciClass %v to string", c) |
| 172 | return string(c) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | const ( |
| 177 | // Managed Entity Class values |
| 178 | EthernetPMHistoryData OmciClass = 24 |
| 179 | ONUG OmciClass = 256 |
| 180 | ANIG OmciClass = 263 |
| 181 | GEMPortNetworkCTP OmciClass = 268 |
| 182 | ) |
| 183 | |
| 184 | // OMCI Message Identifier |
| 185 | type OmciMessageIdentifier struct { |
| 186 | Class OmciClass |
| 187 | Instance uint16 |
| 188 | } |
| 189 | |
| 190 | type OmciContent [32]byte |
| 191 | |
| 192 | type OmciMessage struct { |
| 193 | TransactionId uint16 |
| 194 | MessageType OmciMsgType |
| 195 | DeviceId uint8 |
| 196 | MessageId OmciMessageIdentifier |
| 197 | Content OmciContent |
| 198 | } |
| 199 | |
| 200 | func ParsePkt(pkt []byte) (uint16, uint8, OmciMsgType, OmciClass, uint16, OmciContent, error) { |
| 201 | var m OmciMessage |
| 202 | |
| 203 | r := bytes.NewReader(pkt) |
| 204 | |
| 205 | if err := binary.Read(r, binary.BigEndian, &m); err != nil { |
| 206 | log.WithFields(log.Fields{ |
| 207 | "Packet": pkt, |
| 208 | "omciMsg": fmt.Sprintf("%x", pkt), |
| 209 | }).Errorf("Failed to read packet: %s", err) |
| 210 | return 0, 0, 0, 0, 0, OmciContent{}, errors.New("Failed to read packet") |
| 211 | } |
| 212 | /* Message Type = Set |
| 213 | 0... .... = Destination Bit: 0x0 |
| 214 | .1.. .... = Acknowledge Request: 0x1 |
| 215 | ..0. .... = Acknowledgement: 0x0 |
| 216 | ...0 1000 = Message Type: Set (8) |
| 217 | */ |
| 218 | |
| 219 | log.WithFields(log.Fields{ |
| 220 | "TransactionId": m.TransactionId, |
| 221 | "MessageType": m.MessageType.PrettyPrint(), |
| 222 | "MeClass": m.MessageId.Class, |
| 223 | "MeInstance": m.MessageId.Instance, |
| 224 | "Conent": m.Content, |
| 225 | "Packet": pkt, |
| 226 | }).Tracef("Parsing OMCI Packet") |
| 227 | |
| 228 | return m.TransactionId, m.DeviceId, m.MessageType & 0x1F, m.MessageId.Class, m.MessageId.Instance, m.Content, nil |
| 229 | } |