Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -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 | package core |
| 17 | |
Keita NISHIMOTO | 73bb349 | 2019-01-29 07:11:27 +0900 | [diff] [blame] | 18 | import ( |
| 19 | "errors" |
| 20 | "fmt" |
William Kurkian | 3687c57 | 2019-10-11 15:29:17 -0400 | [diff] [blame] | 21 | "sync" |
Keita NISHIMOTO | 73bb349 | 2019-01-29 07:11:27 +0900 | [diff] [blame] | 22 | ) |
| 23 | |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 24 | type OnuOmciState struct { |
Zdravko Bozakov | f56cca4 | 2019-04-29 10:06:47 +0200 | [diff] [blame] | 25 | gemPortId uint16 |
| 26 | mibUploadCtr uint16 |
Zdravko Bozakov | 3d76214 | 2019-07-15 16:57:17 +0200 | [diff] [blame] | 27 | extraMibUploadCtr uint16 // this is only for debug purposes, will be removed in the future |
Zdravko Bozakov | f56cca4 | 2019-04-29 10:06:47 +0200 | [diff] [blame] | 28 | uniGInstance uint8 |
| 29 | tcontInstance uint8 |
| 30 | pptpInstance uint8 |
Zdravko Bozakov | 3d76214 | 2019-07-15 16:57:17 +0200 | [diff] [blame] | 31 | priorQInstance uint8 // To assign incrementing value to PQ instance-Id |
| 32 | priorQPriority uint8 // Priority of the PriorityQueueG (0-7) |
| 33 | tcontPointer uint8 // Tcont Pointer for PriorQ |
Zdravko Bozakov | f56cca4 | 2019-04-29 10:06:47 +0200 | [diff] [blame] | 34 | state istate |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | type istate int |
| 38 | |
| 39 | // TODO - Needs to reflect real ONU/OMCI state |
| 40 | const ( |
| 41 | INCOMPLETE istate = iota |
| 42 | DONE |
Matt Jeanneret | 869aeab | 2020-01-27 20:11:21 -0500 | [diff] [blame] | 43 | LOCKED |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 44 | ) |
| 45 | |
| 46 | var OnuOmciStateMap = map[OnuKey]*OnuOmciState{} |
William Kurkian | 3687c57 | 2019-10-11 15:29:17 -0400 | [diff] [blame] | 47 | var OnuOmciStateMapLock = sync.RWMutex{} |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 48 | |
| 49 | func NewOnuOmciState() *OnuOmciState { |
| 50 | return &OnuOmciState{gemPortId: 0, mibUploadCtr: 0, uniGInstance: 1, tcontInstance: 0, pptpInstance: 1} |
| 51 | } |
Zdravko Bozakov | f56cca4 | 2019-04-29 10:06:47 +0200 | [diff] [blame] | 52 | func (s *OnuOmciState) ResetOnuOmciState() { |
Zdravko Bozakov | 3d76214 | 2019-07-15 16:57:17 +0200 | [diff] [blame] | 53 | // Resetting the counters |
Mahir Gunyel | ed7b07b | 2019-02-11 12:06:02 -0800 | [diff] [blame] | 54 | s.mibUploadCtr = 0 |
| 55 | s.extraMibUploadCtr = 0 |
| 56 | s.gemPortId = 0 |
| 57 | s.uniGInstance = 1 |
| 58 | s.tcontInstance = 0 |
| 59 | s.pptpInstance = 1 |
Zdravko Bozakov | 3d76214 | 2019-07-15 16:57:17 +0200 | [diff] [blame] | 60 | s.tcontPointer = 0 |
| 61 | s.priorQPriority = 0 |
Mahir Gunyel | ed7b07b | 2019-02-11 12:06:02 -0800 | [diff] [blame] | 62 | } |
Matteo Scandolo | e424acf | 2020-11-09 16:37:07 -0800 | [diff] [blame] | 63 | func GetOnuOmciState(oltId int, intfId uint32, onuId uint32) istate { |
| 64 | key := OnuKey{oltId,intfId, onuId} |
William Kurkian | 3687c57 | 2019-10-11 15:29:17 -0400 | [diff] [blame] | 65 | OnuOmciStateMapLock.RLock() |
| 66 | defer OnuOmciStateMapLock.RUnlock() |
Shad Ansari | 3259e6d | 2019-01-17 15:51:19 -0800 | [diff] [blame] | 67 | if onu, ok := OnuOmciStateMap[key]; ok { |
| 68 | return onu.state |
| 69 | } else { |
| 70 | return INCOMPLETE |
| 71 | } |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Matteo Scandolo | e424acf | 2020-11-09 16:37:07 -0800 | [diff] [blame] | 74 | func GetGemPortId(oltId int, intfId uint32, onuId uint32) (uint16, error) { |
| 75 | key := OnuKey{oltId, intfId, onuId} |
William Kurkian | 3687c57 | 2019-10-11 15:29:17 -0400 | [diff] [blame] | 76 | OnuOmciStateMapLock.RLock() |
| 77 | defer OnuOmciStateMapLock.RUnlock() |
Keita NISHIMOTO | 73bb349 | 2019-01-29 07:11:27 +0900 | [diff] [blame] | 78 | if OnuOmciState, ok := OnuOmciStateMap[key]; ok { |
Keita NISHIMOTO | 5a19cfd | 2019-03-07 08:23:28 +0900 | [diff] [blame] | 79 | if OnuOmciState.state != DONE { |
| 80 | errmsg := fmt.Sprintf("ONU {intfid:%d, onuid:%d} - Not DONE (GemportID is not set)", intfId, onuId) |
| 81 | return 0, errors.New(errmsg) |
| 82 | } |
Keita NISHIMOTO | 73bb349 | 2019-01-29 07:11:27 +0900 | [diff] [blame] | 83 | return OnuOmciState.gemPortId, nil |
| 84 | } |
Mahir Gunyel | ed7b07b | 2019-02-11 12:06:02 -0800 | [diff] [blame] | 85 | errmsg := fmt.Sprintf("ONU {intfid:%d, onuid:%d} - Failed to find a key in OnuOmciStateMap", intfId, onuId) |
Keita NISHIMOTO | 73bb349 | 2019-01-29 07:11:27 +0900 | [diff] [blame] | 86 | return 0, errors.New(errmsg) |
Shad Ansari | 1106b02 | 2019-01-16 22:22:35 -0800 | [diff] [blame] | 87 | } |