blob: a83c07a8188cd0b1be08f5fc57fab2825454c465 [file] [log] [blame]
Shad Ansari2eac6a42018-11-14 22:35:39 -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
Shad Ansari45806172018-11-19 17:13:08 -080019import (
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090020 "context"
Shad Ansaria3384b02019-01-03 15:11:11 -080021 "gerrit.opencord.org/voltha-bbsim/common/logger"
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020022 openolt "gerrit.opencord.org/voltha-bbsim/protos"
Shad Ansarib744bf22019-01-17 11:36:46 -080023 omci "github.com/opencord/omci-sim"
Shad Ansari45806172018-11-19 17:13:08 -080024)
25
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020026// RunOmciResponder starts a go routine to process/respond to OMCI messages from VOLTHA
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020027func (s *Server) RunOmciResponder(ctx context.Context, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, errch chan error) {
Shad Ansaria3384b02019-01-03 15:11:11 -080028 go func() {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090029 defer logger.Debug("Omci response process was done")
Shad Ansari65fed642019-01-16 14:04:19 -080030
31 var resp openolt.OmciIndication
32
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090033 for {
Shad Ansaria3384b02019-01-03 15:11:11 -080034 select {
35 case m := <-omciOut:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020036 respPkt, err := omci.OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt))
Shad Ansari65fed642019-01-16 14:04:19 -080037 switch err := err.(type) {
38 case nil:
39 // Success
40 resp.IntfId = m.IntfId
41 resp.OnuId = m.OnuId
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020042 resp.Pkt = respPkt
Shad Ansari65fed642019-01-16 14:04:19 -080043 omciIn <- resp
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020044 s.handleOmciAction(resp.Pkt, resp.IntfId, resp.OnuId)
45
46
Shad Ansarib744bf22019-01-17 11:36:46 -080047 case *omci.OmciError:
Shad Ansari65fed642019-01-16 14:04:19 -080048 // Error in processing omci message. Log and carry on.
49 logger.Debug("%s", err.Msg)
Shad Ansaria3384b02019-01-03 15:11:11 -080050 continue
Shad Ansari65fed642019-01-16 14:04:19 -080051 default:
52 // Fatal error, exit.
Shad Ansaria3384b02019-01-03 15:11:11 -080053 errch <- err
54 return
55 }
Shad Ansaria3384b02019-01-03 15:11:11 -080056 case <-ctx.Done():
57 return
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090058 }
Shad Ansari45806172018-11-19 17:13:08 -080059 }
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090060 }()
Shad Ansari2eac6a42018-11-14 22:35:39 -080061}
Shad Ansari45806172018-11-19 17:13:08 -080062
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020063// HexDecode converts the hex encoding to binary
Shad Ansaria5c79892018-11-29 16:32:59 -080064func HexDecode(pkt []byte) []byte {
Shad Ansaria5c79892018-11-29 16:32:59 -080065 // TODO - Change openolt adapter to send raw binary instead of hex encoded
Shad Ansari45806172018-11-19 17:13:08 -080066 p := make([]byte, len(pkt)/2)
67 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
Shad Ansaria5c79892018-11-29 16:32:59 -080068 // Go figure this ;)
Shad Ansari45806172018-11-19 17:13:08 -080069 u := (pkt[i] & 15) + (pkt[i]>>6)*9
70 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
71 p[j] = u<<4 + l
72 }
73 logger.Debug("Omci decoded: %x.", p)
Shad Ansaria5c79892018-11-29 16:32:59 -080074 return p
Shad Ansari45806172018-11-19 17:13:08 -080075}
76
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020077func (s *Server) handleOmciAction(pkt []byte, IntfID uint32, OnuID uint32) {
78 logger.Debug("handleOmciAction invoked")
79 MEClass := omci.OmciClass(uint16(pkt[5]) | uint16(pkt[4])<<8)
80 msgType := omci.OmciMsgType(pkt[2] & 0x1F)
81 logger.Debug("ME Class %d, msgType %d", MEClass, msgType)
82
83 if MEClass == omci.ONUG {
84 switch msgType {
85 case omci.Reboot:
86 logger.Info("ONU reboot recieved")
87 s.handleONUSoftReboot(IntfID, OnuID)
88 }
89 }
90}