blob: 6cc07082c367d7593d79c4de95f464b3465e449e [file] [log] [blame]
Shad Ansari1106b022019-01-16 22:22:35 -08001/*
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
17package core
18
Matteo Scandoloa0026812019-08-20 11:01:32 -070019import (
20 "fmt"
21 log "github.com/sirupsen/logrus"
22)
Shad Ansari1106b022019-01-16 22:22:35 -080023
Matteo Scandolo203314b2019-08-26 14:28:42 -070024var omciCh = make(chan OmciChMessage)
25
26func GetChannel() chan OmciChMessage {
27 return omciCh
28}
29
Shad Ansari1106b022019-01-16 22:22:35 -080030func OmciSim(intfId uint32, onuId uint32, request []byte) ([]byte, error) {
31 var resp []byte
32
33 transactionId, deviceId, msgType, class, instance, content, err := ParsePkt(request)
34 if err != nil {
Matteo Scandoloa0026812019-08-20 11:01:32 -070035 log.WithFields(log.Fields{
36 "IntfId": intfId,
37 "OnuId": onuId,
38 }).Errorf("Cannot parse OMCI msg")
39 return resp, &OmciError{"Cannot parse OMCI msg"}
Shad Ansari1106b022019-01-16 22:22:35 -080040 }
41
Matteo Scandoloa0026812019-08-20 11:01:32 -070042 log.WithFields(log.Fields{
43 "IntfId": intfId,
44 "OnuId": onuId,
45 "TransactionId": transactionId,
46 "MessageType": msgType.PrettyPrint(),
47 "MeClass": class,
48 "MeInstance": instance,
49 //"Conent": content,
50 "omciMsg": fmt.Sprintf("%x", content),
Matteo Scandolo203314b2019-08-26 14:28:42 -070051 }).Tracef("Processing OMCI pakcet")
Shad Ansari1106b022019-01-16 22:22:35 -080052
53 key := OnuKey{intfId, onuId}
54 if _, ok := OnuOmciStateMap[key]; !ok {
55 OnuOmciStateMap[key] = NewOnuOmciState()
56 }
57
58 if _, ok := Handlers[msgType]; !ok {
Matteo Scandoloa0026812019-08-20 11:01:32 -070059 log.WithFields(log.Fields{
60 "IntfId": intfId,
61 "OnuId": onuId,
62 "msgType": msgType,
63 }).Errorf("Ignoring omci msg (msgType %d not handled)", msgType)
Shad Ansari1106b022019-01-16 22:22:35 -080064 return resp, &OmciError{"Unimplemented omci msg"}
65 }
66
67 resp, err = Handlers[msgType](class, content, key)
68 if err != nil {
Matteo Scandoloa0026812019-08-20 11:01:32 -070069 log.WithFields(log.Fields{
70 "IntfId": intfId,
71 "OnuId": onuId,
72 "msgType": msgType,
73 }).Errorf("Unable to send a successful response, error: %s", err)
Keita NISHIMOTO21853b32019-01-25 19:29:59 +090074 return resp, nil
Shad Ansari1106b022019-01-16 22:22:35 -080075 }
Keita NISHIMOTO21853b32019-01-25 19:29:59 +090076
Zdravko Bozakovf56cca42019-04-29 10:06:47 +020077 // In the OMCI message, first 2-bytes is the Transaction Correlation ID
Shad Ansari1106b022019-01-16 22:22:35 -080078 resp[0] = byte(transactionId >> 8)
79 resp[1] = byte(transactionId & 0xFF)
Zdravko Bozakovf56cca42019-04-29 10:06:47 +020080 resp[2] = 0x2<<4 | byte(msgType) // Upper nibble 0x2 is fixed (0010), Lower nibbles defines the msg type (i.e., mib-upload, mib-upload-next, etc)
Shad Ansari1106b022019-01-16 22:22:35 -080081 resp[3] = deviceId
82
Zdravko Bozakovf56cca42019-04-29 10:06:47 +020083 // for create, get and set
84 if ((msgType & 0xFF) != MibUploadNext) && ((msgType & 0xFF) != MibReset) && ((msgType & 0xFF) != MibUpload) {
Zdravko Bozakovf56cca42019-04-29 10:06:47 +020085 // Common fields for create, get, and set
86 resp[4] = byte(class >> 8)
87 resp[5] = byte(class & 0xFF)
88 resp[6] = byte(instance >> 8)
89 resp[7] = byte(instance & 0xFF)
90 resp[8] = 0 // Result: Command Processed Successfully
91
92 // Hardcoding class specific values for Get
93 if (class == 0x82) && ((msgType & 0x0F) == Get) {
94 resp[9] = 0
95 resp[10] = 0x78
96
97 } else if (class == 0x2F) && ((msgType & 0x0F) == Get) {
98 resp[9] = 0x0F
99 resp[10] = 0xB8
100 } else if (class == 0x138) && ((msgType & 0x0F) == Get) {
101 resp[9] = content[0] // 0xBE
102 resp[10] = 0x00
103 }
104 }
105
Matteo Scandoloa0026812019-08-20 11:01:32 -0700106 log.WithFields(log.Fields{
107 "IntfId": intfId,
108 "OnuId": onuId,
109 "msgType": msgType.PrettyPrint(),
110 "omciMsg": fmt.Sprintf("%x", resp),
Matteo Scandolo203314b2019-08-26 14:28:42 -0700111 }).Tracef("OMCI-SIM Response")
Zdravko Bozakovf56cca42019-04-29 10:06:47 +0200112
Shad Ansari1106b022019-01-16 22:22:35 -0800113 return resp, nil
Matteo Scandoloa0026812019-08-20 11:01:32 -0700114}