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 | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 29 | 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] | 30 | 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] | 31 | t := time.NewTicker(1 * time.Second) |
| 32 | defer t.Stop() |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 33 | for { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 34 | select { |
| 35 | case <-t.C: |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 36 | 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 Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 42 | case <-ctx.Done(): |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 43 | logger.Debug("Omci Monitoring process was done") |
| 44 | return |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 45 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 46 | } |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 47 | }() |
Shad Ansari | 8213c53 | 2018-12-11 13:53:34 -0800 | [diff] [blame] | 48 | |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 49 | go func() { |
Keita NISHIMOTO | 2b69420 | 2018-12-18 07:30:55 +0900 | [diff] [blame] | 50 | defer logger.Debug("Omci response process was done") |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 51 | |
| 52 | var resp openolt.OmciIndication |
| 53 | |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 54 | for { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 55 | select { |
| 56 | case m := <-omciOut: |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 57 | 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 Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 68 | continue |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 69 | default: |
| 70 | // Fatal error, exit. |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 71 | errch <- err |
| 72 | return |
| 73 | } |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 74 | case <-ctx.Done(): |
| 75 | return |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 76 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 77 | } |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 78 | }() |
Shad Ansari | 2eac6a4 | 2018-11-14 22:35:39 -0800 | [diff] [blame] | 79 | } |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 80 | |
Shad Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 81 | func 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 Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 84 | p := make([]byte, len(pkt)/2) |
| 85 | 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] | 86 | // Go figure this ;) |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 87 | 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 Ansari | a5c7989 | 2018-11-29 16:32:59 -0800 | [diff] [blame] | 92 | return p |
Shad Ansari | 4580617 | 2018-11-19 17:13:08 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 95 | func isAllOmciInitDone(onumap map[uint32][]*device.Onu) bool { |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 96 | for _, onus := range onumap { |
Shad Ansari | a3384b0 | 2019-01-03 15:11:11 -0800 | [diff] [blame] | 97 | for _, onu := range onus { |
Shad Ansari | 65fed64 | 2019-01-16 14:04:19 -0800 | [diff] [blame] | 98 | if GetOnuOmciState(onu.OnuID, onu.IntfID) == INCOMPLETE { |
Keita NISHIMOTO | 3af86da | 2018-12-12 10:34:43 +0900 | [diff] [blame] | 99 | return false |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | return true |
| 104 | } |