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 | |
| 26 | // |
| 27 | // OMCI definitions |
| 28 | // |
| 29 | |
| 30 | // OmciMsgType represents a OMCI message-type |
| 31 | type OmciMsgType byte |
| 32 | |
Matteo Scandolo | a002681 | 2019-08-20 11:01:32 -0700 | [diff] [blame] | 33 | func (t OmciMsgType) PrettyPrint() string { |
| 34 | switch t { |
| 35 | case Create: |
| 36 | return "Create" |
| 37 | case Delete: |
| 38 | return "Delete" |
| 39 | case Set: |
| 40 | return "Set" |
| 41 | case Get: |
| 42 | return "Get" |
| 43 | case GetAllAlarms: |
| 44 | return "GetAllAlarms" |
| 45 | case GetAllAlarmsNext: |
| 46 | return "GetAllAlarmsNext" |
| 47 | case MibUpload: |
| 48 | return "MibUpload" |
| 49 | case MibUploadNext: |
| 50 | return "MibUploadNext" |
| 51 | case MibReset: |
| 52 | return "MibReset" |
| 53 | case AlarmNotification: |
| 54 | return "AlarmNotification" |
| 55 | case AttributeValueChange: |
| 56 | return "AttributeValueChange" |
| 57 | case Test: |
| 58 | return "Test" |
| 59 | case StartSoftwareDownload: |
| 60 | return "StartSoftwareDownload" |
| 61 | case DownloadSection: |
| 62 | return "DownloadSection" |
| 63 | case EndSoftwareDownload: |
| 64 | return "EndSoftwareDownload" |
| 65 | case ActivateSoftware: |
| 66 | return "ActivateSoftware" |
| 67 | case CommitSoftware: |
| 68 | return "CommitSoftware" |
| 69 | case SynchronizeTime: |
| 70 | return "SynchronizeTime" |
| 71 | case Reboot: |
| 72 | return "Reboot" |
| 73 | case GetNext: |
| 74 | return "GetNext" |
| 75 | case TestResult: |
| 76 | return "TestResult" |
| 77 | case GetCurrentData: |
| 78 | return "GetCurrentData" |
| 79 | case SetTable: |
| 80 | return "SetTable" |
| 81 | default: |
| 82 | log.Warnf("Cant't convert state %v to string", t) |
| 83 | return string(t) |
| 84 | } |
| 85 | } |
| 86 | |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 87 | const ( |
| 88 | // Message Types |
| 89 | _ = iota |
| 90 | Create OmciMsgType = 4 |
| 91 | Delete OmciMsgType = 6 |
| 92 | Set OmciMsgType = 8 |
| 93 | Get OmciMsgType = 9 |
| 94 | GetAllAlarms OmciMsgType = 11 |
| 95 | GetAllAlarmsNext OmciMsgType = 12 |
| 96 | MibUpload OmciMsgType = 13 |
| 97 | MibUploadNext OmciMsgType = 14 |
| 98 | MibReset OmciMsgType = 15 |
| 99 | AlarmNotification OmciMsgType = 16 |
| 100 | AttributeValueChange OmciMsgType = 17 |
| 101 | Test OmciMsgType = 18 |
| 102 | StartSoftwareDownload OmciMsgType = 19 |
| 103 | DownloadSection OmciMsgType = 20 |
| 104 | EndSoftwareDownload OmciMsgType = 21 |
| 105 | ActivateSoftware OmciMsgType = 22 |
| 106 | CommitSoftware OmciMsgType = 23 |
| 107 | SynchronizeTime OmciMsgType = 24 |
| 108 | Reboot OmciMsgType = 25 |
| 109 | GetNext OmciMsgType = 26 |
| 110 | TestResult OmciMsgType = 27 |
| 111 | GetCurrentData OmciMsgType = 28 |
| 112 | SetTable OmciMsgType = 29 // Defined in Extended Message Set Only |
| 113 | ) |
| 114 | |
| 115 | const ( |
| 116 | // Managed Entity Class values |
Zdravko Bozakov | 3d76214 | 2019-07-15 16:57:17 +0200 | [diff] [blame] | 117 | EthernetPMHistoryData OmciClass = 24 |
| 118 | ONUG OmciClass = 256 |
| 119 | ANIG OmciClass = 263 |
| 120 | GEMPortNetworkCTP OmciClass = 268 |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 121 | ) |
| 122 | |
| 123 | // OMCI Managed Entity Class |
| 124 | type OmciClass uint16 |
| 125 | |
| 126 | // OMCI Message Identifier |
| 127 | type OmciMessageIdentifier struct { |
| 128 | Class OmciClass |
| 129 | Instance uint16 |
| 130 | } |
| 131 | |
| 132 | type OmciContent [32]byte |
| 133 | |
| 134 | type OmciMessage struct { |
| 135 | TransactionId uint16 |
| 136 | MessageType OmciMsgType |
| 137 | DeviceId uint8 |
| 138 | MessageId OmciMessageIdentifier |
| 139 | Content OmciContent |
| 140 | } |
| 141 | |
| 142 | func ParsePkt(pkt []byte) (uint16, uint8, OmciMsgType, OmciClass, uint16, OmciContent, error) { |
| 143 | var m OmciMessage |
| 144 | |
| 145 | r := bytes.NewReader(pkt) |
| 146 | |
| 147 | if err := binary.Read(r, binary.BigEndian, &m); err != nil { |
Matteo Scandolo | a002681 | 2019-08-20 11:01:32 -0700 | [diff] [blame] | 148 | log.WithFields(log.Fields{ |
| 149 | "Packet": pkt, |
| 150 | "omciMsg": fmt.Sprintf("%x", pkt), |
| 151 | }).Errorf("Failed to read packet: %s", err) |
| 152 | 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] | 153 | } |
Mahir Gunyel | 133913f | 2019-02-14 19:57:07 -0800 | [diff] [blame] | 154 | /* Message Type = Set |
Zdravko Bozakov | 8e9d85a | 2019-05-27 21:02:59 +0200 | [diff] [blame] | 155 | 0... .... = Destination Bit: 0x0 |
| 156 | .1.. .... = Acknowledge Request: 0x1 |
| 157 | ..0. .... = Acknowledgement: 0x0 |
| 158 | ...0 1000 = Message Type: Set (8) |
Mahir Gunyel | 133913f | 2019-02-14 19:57:07 -0800 | [diff] [blame] | 159 | */ |
Matteo Scandolo | a002681 | 2019-08-20 11:01:32 -0700 | [diff] [blame] | 160 | |
| 161 | log.WithFields(log.Fields{ |
| 162 | "TransactionId": m.TransactionId, |
| 163 | "MessageType": m.MessageType.PrettyPrint(), |
| 164 | "MeClass": m.MessageId.Class, |
| 165 | "MeInstance": m.MessageId.Instance, |
| 166 | "Conent": m.Content, |
| 167 | "Packet": pkt, |
| 168 | }).Tracef("Parsing OMCI Packet") |
| 169 | |
Mahir Gunyel | 133913f | 2019-02-14 19:57:07 -0800 | [diff] [blame] | 170 | 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] | 171 | } |