Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [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 device |
| 18 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 19 | import "sync" |
| 20 | |
| 21 | type DeviceState int |
| 22 | |
| 23 | type Device interface { |
| 24 | Initialize() |
| 25 | UpdateIntState(intstate DeviceState) |
| 26 | GetIntState() DeviceState |
| 27 | GetDevkey() Devkey |
| 28 | } |
| 29 | |
| 30 | type Devkey struct { |
| 31 | ID uint32 |
| 32 | Intfid uint32 |
| 33 | } |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 34 | |
| 35 | type Olt struct { |
| 36 | ID uint32 |
| 37 | NumPonIntf uint32 |
| 38 | NumNniIntf uint32 |
| 39 | Mac string |
| 40 | SerialNumber string |
| 41 | Manufacture string |
| 42 | Name string |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 43 | InternalState DeviceState |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 44 | OperState string |
| 45 | Intfs []intf |
| 46 | HeartbeatSignature uint32 |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 47 | mu *sync.Mutex |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | type intf struct { |
| 51 | Type string |
| 52 | IntfID uint32 |
| 53 | OperState string |
| 54 | } |
| 55 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 56 | /* OltState |
| 57 | OLT_INACTIVE -> OLT_PREACTIVE -> ACTIVE |
| 58 | (ActivateOLT) (Enable) |
| 59 | <- <- |
| 60 | */ |
| 61 | |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 62 | const ( |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 63 | OLT_INACTIVE DeviceState = iota // OLT/ONUs are not instantiated |
| 64 | OLT_PREACTIVE // Before PacketInDaemon Running |
| 65 | OLT_ACTIVE // After PacketInDaemon Running |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 66 | ) |
| 67 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 68 | func NewOlt(oltid uint32, npon uint32, nnni uint32) *Olt { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 69 | olt := Olt{} |
| 70 | olt.ID = oltid |
| 71 | olt.NumPonIntf = npon |
| 72 | olt.NumNniIntf = nnni |
| 73 | olt.Name = "BBSIM OLT" |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 74 | olt.InternalState = OLT_INACTIVE |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 75 | olt.OperState = "up" |
| 76 | olt.Intfs = make([]intf, olt.NumPonIntf+olt.NumNniIntf) |
| 77 | olt.HeartbeatSignature = oltid |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 78 | olt.mu = &sync.Mutex{} |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 79 | for i := uint32(0); i < olt.NumNniIntf; i++ { |
| 80 | olt.Intfs[i].IntfID = i |
| 81 | olt.Intfs[i].OperState = "up" |
| 82 | olt.Intfs[i].Type = "nni" |
| 83 | } |
| 84 | for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf+olt.NumNniIntf; i++ { |
| 85 | olt.Intfs[i].IntfID = i |
| 86 | olt.Intfs[i].OperState = "up" |
| 87 | olt.Intfs[i].Type = "pon" |
| 88 | } |
| 89 | return &olt |
| 90 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 91 | |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 92 | func (olt *Olt) Initialize() { |
| 93 | olt.InternalState = OLT_INACTIVE |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 94 | olt.OperState = "up" |
| 95 | for i := uint32(0); i < olt.NumNniIntf; i++ { |
| 96 | olt.Intfs[i].IntfID = i |
| 97 | olt.Intfs[i].OperState = "up" |
| 98 | olt.Intfs[i].Type = "nni" |
| 99 | } |
| 100 | for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf+olt.NumNniIntf; i++ { |
| 101 | olt.Intfs[i].IntfID = i |
| 102 | olt.Intfs[i].OperState = "up" |
| 103 | olt.Intfs[i].Type = "pon" |
| 104 | } |
| 105 | } |
Keita NISHIMOTO | 3f08062 | 2019-01-16 10:21:22 +0900 | [diff] [blame^] | 106 | |
| 107 | func (olt *Olt) GetIntState() DeviceState { |
| 108 | olt.mu.Lock() |
| 109 | defer olt.mu.Unlock() |
| 110 | return olt.InternalState |
| 111 | } |
| 112 | |
| 113 | func (olt *Olt) GetDevkey () Devkey { |
| 114 | return Devkey{ID: olt.ID} |
| 115 | } |
| 116 | |
| 117 | func (olt *Olt) UpdateIntState(intstate DeviceState) { |
| 118 | olt.mu.Lock() |
| 119 | defer olt.mu.Unlock() |
| 120 | olt.InternalState = intstate |
| 121 | } |