blob: e35fd909551a3224f53e6944f3d0fb10e66d3bfe [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 Ansarib744bf22019-01-17 11:36:46 -080027 omci "github.com/opencord/omci-sim"
Shad Ansari45806172018-11-19 17:13:08 -080028)
29
Shad Ansari65fed642019-01-16 14:04:19 -080030func 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 +090031 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 +090032 t := time.NewTicker(1 * time.Second)
33 defer t.Stop()
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090034 for {
Shad Ansaria3384b02019-01-03 15:11:11 -080035 select {
36 case <-t.C:
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090037 logger.Debug("Monitor omci init state")
38 if isAllOmciInitDone(onumap) {
39 logger.Info("OmciRun - All the omci initialization wes done")
40 close(errch)
41 return
42 }
Shad Ansaria3384b02019-01-03 15:11:11 -080043 case <-ctx.Done():
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090044 logger.Debug("Omci Monitoring process was done")
45 return
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090046 }
Shad Ansari45806172018-11-19 17:13:08 -080047 }
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090048 }()
Shad Ansari8213c532018-12-11 13:53:34 -080049
Shad Ansaria3384b02019-01-03 15:11:11 -080050 go func() {
Keita NISHIMOTO2b694202018-12-18 07:30:55 +090051 defer logger.Debug("Omci response process was done")
Shad Ansari65fed642019-01-16 14:04:19 -080052
53 var resp openolt.OmciIndication
54
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090055 for {
Shad Ansaria3384b02019-01-03 15:11:11 -080056 select {
57 case m := <-omciOut:
Shad Ansarib744bf22019-01-17 11:36:46 -080058 resp_pkt, err := omci.OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt))
Shad Ansari65fed642019-01-16 14:04:19 -080059 switch err := err.(type) {
60 case nil:
61 // Success
62 resp.IntfId = m.IntfId
63 resp.OnuId = m.OnuId
64 resp.Pkt = resp_pkt
65 omciIn <- resp
Shad Ansarib744bf22019-01-17 11:36:46 -080066 case *omci.OmciError:
Shad Ansari65fed642019-01-16 14:04:19 -080067 // Error in processing omci message. Log and carry on.
68 logger.Debug("%s", err.Msg)
Shad Ansaria3384b02019-01-03 15:11:11 -080069 continue
Shad Ansari65fed642019-01-16 14:04:19 -080070 default:
71 // Fatal error, exit.
Shad Ansaria3384b02019-01-03 15:11:11 -080072 errch <- err
73 return
74 }
Shad Ansaria3384b02019-01-03 15:11:11 -080075 case <-ctx.Done():
76 return
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090077 }
Shad Ansari45806172018-11-19 17:13:08 -080078 }
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090079 }()
Shad Ansari2eac6a42018-11-14 22:35:39 -080080}
Shad Ansari45806172018-11-19 17:13:08 -080081
Shad Ansaria5c79892018-11-29 16:32:59 -080082func HexDecode(pkt []byte) []byte {
83 // Convert the hex encoding to binary
84 // TODO - Change openolt adapter to send raw binary instead of hex encoded
Shad Ansari45806172018-11-19 17:13:08 -080085 p := make([]byte, len(pkt)/2)
86 for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 {
Shad Ansaria5c79892018-11-29 16:32:59 -080087 // Go figure this ;)
Shad Ansari45806172018-11-19 17:13:08 -080088 u := (pkt[i] & 15) + (pkt[i]>>6)*9
89 l := (pkt[i+1] & 15) + (pkt[i+1]>>6)*9
90 p[j] = u<<4 + l
91 }
92 logger.Debug("Omci decoded: %x.", p)
Shad Ansaria5c79892018-11-29 16:32:59 -080093 return p
Shad Ansari45806172018-11-19 17:13:08 -080094}
95
Shad Ansaria3384b02019-01-03 15:11:11 -080096func isAllOmciInitDone(onumap map[uint32][]*device.Onu) bool {
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +090097 for _, onus := range onumap {
Shad Ansaria3384b02019-01-03 15:11:11 -080098 for _, onu := range onus {
Shad Ansarib744bf22019-01-17 11:36:46 -080099 if omci.GetOnuOmciState(onu.OnuID, onu.IntfID) == omci.INCOMPLETE {
Keita NISHIMOTO3af86da2018-12-12 10:34:43 +0900100 return false
101 }
102 }
103 }
104 return true
105}