Shad Ansari | 2eac6a4 | 2018-11-14 22:35:39 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package core |
| 18 | |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 19 | import ( |
Shad Ansari | 1ac4c43 | 2019-01-14 22:00:00 -0800 | [diff] [blame] | 20 | "time" |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 21 | |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 22 | "context" |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 23 | |
| 24 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
| 25 | "gerrit.opencord.org/voltha-bbsim/device" |
| 26 | "gerrit.opencord.org/voltha-bbsim/protos" |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 27 | omci "github.com/opencord/omci-sim" |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 30 | func RunOmciResponder(ctx context.Context, omciOut chan openolt.OmciMsg, omciIn chan openolt.OmciIndication, onumap map[uint32][]*device.Onu, errch chan error) { |
Keita NISHIMOTO | 0c1c083 | 2019-01-16 07:06:30 +0900 | [diff] [blame] | 31 | go func() { //For monitoring the OMCI states TODO: This part should be eliminated because it is out of scope of this library |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 32 | t := time.NewTicker(1 * time.Second) |
| 33 | defer t.Stop() |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 34 | for { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 35 | select { |
| 36 | case <-t.C: |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 37 | 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 Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 43 | case <-ctx.Done(): |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 44 | logger.Debug("Omci Monitoring process was done") |
| 45 | return |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 46 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 47 | } |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 48 | }() |
Shad Ansari | 8213c53 | 2018-12-11 13:53:34 -0800 | [diff] [blame] | 49 | |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 50 | go func() { |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 51 | defer logger.Debug("Omci response process was done") |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 52 | |
| 53 | var resp openolt.OmciIndication |
| 54 | |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 55 | for { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 56 | select { |
| 57 | case m := <-omciOut: |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 58 | resp_pkt, err := omci.OmciSim(m.IntfId, m.OnuId, HexDecode(m.Pkt)) |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 59 | 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 Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 66 | case *omci.OmciError: |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 67 | // Error in processing omci message. Log and carry on. |
| 68 | logger.Debug("%s", err.Msg) |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 69 | continue |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 70 | default: |
| 71 | // Fatal error, exit. |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 72 | errch <- err |
| 73 | return |
| 74 | } |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 75 | case <-ctx.Done(): |
| 76 | return |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 77 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 78 | } |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 79 | }() |
Shad Ansari | 2eac6a4 | 2018-11-14 22:35:39 -0800 | [diff] [blame] | 80 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 81 | |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 82 | func 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 Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 85 | p := make([]byte, len(pkt)/2) |
| 86 | for i, j := 0, 0; i < len(pkt); i, j = i+2, j+1 { |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 87 | // Go figure this ;) |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 88 | 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 Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 93 | return p |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 96 | func isAllOmciInitDone(onumap map[uint32][]*device.Onu) bool { |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 97 | for _, onus := range onumap { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 98 | for _, onu := range onus { |
Shad Ansari | b744bf2 | 2019-01-17 11:36:46 -0800 | [diff] [blame] | 99 | if omci.GetOnuOmciState(onu.OnuID, onu.IntfID) == omci.INCOMPLETE { |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 100 | return false |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return true |
| 105 | } |