blob: 4698121473960dd801c23e332e696f297e712ae4 [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 (
Shad Ansari1ac4c432019-01-14 22:00:00 -080020 "time"
Shad Ansaria5c79892018-11-29 16:32:59 -080021
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090022 "context"
Shad Ansaria3384b02019-01-03 15:11:11 -080023
24 "gerrit.opencord.org/voltha-bbsim/common/logger"
25 "gerrit.opencord.org/voltha-bbsim/device"
26 "gerrit.opencord.org/voltha-bbsim/protos"
Shad Ansari45806172018-11-19 17:13:08 -080027)
28
Shad Ansari65fed642019-01-16 14:04:19 -080029func RunOmciResponder(ctx context.Context, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, onumap map[uint32][]*device.Onu, errch chan error) {
Keita NISHIMOTO0c1c0832019-01-16 07:06:30 +090030 go func() { //For monitoring the OMCI states TODO: This part should be eliminated because it is out of scope of this library
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090031 t := time.NewTicker(1 * time.Second)
32 defer t.Stop()
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090033 for {
Shad Ansaria3384b02019-01-03 15:11:11 -080034 select {
35 case <-t.C:
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090036 logger.Debug("Monitor omci init state")
37 if isAllOmciInitDone(onumap) {
38 logger.Info("OmciRun - All the omci initialization wes done")
39 close(errch)
40 return
41 }
Shad Ansaria3384b02019-01-03 15:11:11 -080042 case <-ctx.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090043 logger.Debug("Omci Monitoring process was done")
44 return
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090045 }
Shad Ansari45806172018-11-19 17:13:08 -080046 }
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090047 }()
Shad Ansari8213c532018-12-11 13:53:34 -080048
Shad Ansaria3384b02019-01-03 15:11:11 -080049 go func() {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090050 defer logger.Debug("Omci response process was done")
Shad Ansari65fed642019-01-16 14:04:19 -080051
52 var resp openolt.OmciIndication
53
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090054 for {
Shad Ansaria3384b02019-01-03 15:11:11 -080055 select {
56 case m := <-omciOut:
Shad Ansari65fed642019-01-16 14:04:19 -080057 resp_pkt, err := OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt))
58 switch err := err.(type) {
59 case nil:
60 // Success
61 resp.IntfId = m.IntfId
62 resp.OnuId = m.OnuId
63 resp.Pkt = resp_pkt
64 omciIn <- resp
65 case *OmciError:
66 // Error in processing omci message. Log and carry on.
67 logger.Debug("%s", err.Msg)
Shad Ansaria3384b02019-01-03 15:11:11 -080068 continue
Shad Ansari65fed642019-01-16 14:04:19 -080069 default:
70 // Fatal error, exit.
Shad Ansaria3384b02019-01-03 15:11:11 -080071 errch <- err
72 return
73 }
Shad Ansaria3384b02019-01-03 15:11:11 -080074 case <-ctx.Done():
75 return
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090076 }
Shad Ansari45806172018-11-19 17:13:08 -080077 }
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090078 }()
Shad Ansari2eac6a42018-11-14 22:35:39 -080079}
Shad Ansari45806172018-11-19 17:13:08 -080080
Shad Ansaria5c79892018-11-29 16:32:59 -080081func HexDecode(pkt []byte) []byte {
82 // Convert the hex encoding to binary
83 // TODO - Change openolt adapter to send raw binary instead of hex encoded
Shad Ansari45806172018-11-19 17:13:08 -080084 p := make([]byte, len(pkt)/2)
85 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
Shad Ansaria5c79892018-11-29 16:32:59 -080086 // Go figure this ;)
Shad Ansari45806172018-11-19 17:13:08 -080087 u := (pkt[i] & 15) + (pkt[i]>>6)*9
88 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
89 p[j] = u<<4 + l
90 }
91 logger.Debug("Omci decoded: %x.", p)
Shad Ansaria5c79892018-11-29 16:32:59 -080092 return p
Shad Ansari45806172018-11-19 17:13:08 -080093}
94
Shad Ansaria3384b02019-01-03 15:11:11 -080095func isAllOmciInitDone(onumap map[uint32][]*device.Onu) bool {
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090096 for _, onus := range onumap {
Shad Ansaria3384b02019-01-03 15:11:11 -080097 for _, onu := range onus {
Shad Ansari65fed642019-01-16 14:04:19 -080098 if GetOnuOmciState(onu.OnuID, onu.IntfID) == INCOMPLETE {
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090099 return false
100 }
101 }
102 }
103 return true
104}