blob: 3caed492af7f460921ef5698e31a4e5f5d018bac [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"
Keita NISHIMOTO9617c852019-06-17 21:46:44 +090021
Shad Ansarib744bf22019-01-17 11:36:46 -080022 omci "github.com/opencord/omci-sim"
Zack Williams2abf3932019-08-05 14:07:05 -070023 "github.com/opencord/voltha-bbsim/common/logger"
Matt Jeanneret7c9c5f22019-08-09 14:40:12 -040024 openolt "github.com/opencord/voltha-protos/go/openolt"
Shad Ansari45806172018-11-19 17:13:08 -080025)
26
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020027// RunOmciResponder starts a go routine to process/respond to OMCI messages from VOLTHA
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020028func (s *Server) RunOmciResponder(ctx context.Context, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, errch chan error) {
Shad Ansaria3384b02019-01-03 15:11:11 -080029 go func() {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090030 defer logger.Debug("Omci response process was done")
Shad Ansari65fed642019-01-16 14:04:19 -080031
32 var resp openolt.OmciIndication
33
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090034 for {
Shad Ansaria3384b02019-01-03 15:11:11 -080035 select {
36 case m := <-omciOut:
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020037 respPkt, err := omci.OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt))
Shad Ansari65fed642019-01-16 14:04:19 -080038 switch err := err.(type) {
39 case nil:
40 // Success
41 resp.IntfId = m.IntfId
42 resp.OnuId = m.OnuId
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020043 resp.Pkt = respPkt
Shad Ansari65fed642019-01-16 14:04:19 -080044 omciIn <- resp
Zdravko Bozakov7401ff22019-05-28 22:45:12 +020045 s.handleOmciAction(resp.Pkt, resp.IntfId, resp.OnuId)
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 }
Zdravko Bozakov078a2712019-07-19 23:25:15 +020089 } else if MEClass == omci.GEMPortNetworkCTP {
90 switch msgType {
91 case omci.Create:
92 logger.Info("GEMPort created")
93 gemport, err := omci.GetGemPortId(IntfID, OnuID)
94 if err != nil {
95 logger.Error("error in getting gemport %v", err)
96 return
97 }
98 logger.Info("GEM Port %d created at ONU %d intf-id %d", gemport, OnuID, IntfID)
99 }
Zdravko Bozakov7401ff22019-05-28 22:45:12 +0200100 }
101}