blob: daffcec05637755eb5454bbd14b5dad298481b26 [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
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 device
18
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020019import (
20 "strconv"
21 "sync"
22)
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090023
24type DeviceState int
25
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020026// Device interface provides common methods for OLT and ONU devices
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090027type Device interface {
28 Initialize()
29 UpdateIntState(intstate DeviceState)
30 GetIntState() DeviceState
31 GetDevkey() Devkey
32}
33
34type Devkey struct {
35 ID uint32
36 Intfid uint32
37}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090038
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020039// Olt structure consists required fields for OLT
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090040type Olt struct {
41 ID uint32
42 NumPonIntf uint32
43 NumNniIntf uint32
44 Mac string
45 SerialNumber string
46 Manufacture string
47 Name string
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090048 InternalState DeviceState
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049 OperState string
50 Intfs []intf
51 HeartbeatSignature uint32
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090052 mu *sync.Mutex
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090053}
54
55type intf struct {
56 Type string
57 IntfID uint32
58 OperState string
59}
60
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090061/* OltState
62OLT_INACTIVE -> OLT_PREACTIVE -> ACTIVE
63 (ActivateOLT) (Enable)
64 <- <-
65*/
66
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090067const (
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090068 OLT_INACTIVE DeviceState = iota // OLT/ONUs are not instantiated
69 OLT_PREACTIVE // Before PacketInDaemon Running
70 OLT_ACTIVE // After PacketInDaemon Running
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090071)
72
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020073// NewOlt creates and return new Olt object
Keita NISHIMOTO9708e042018-10-27 09:24:44 +090074func NewOlt(oltid uint32, npon uint32, nnni uint32) *Olt {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090075 olt := Olt{}
76 olt.ID = oltid
77 olt.NumPonIntf = npon
78 olt.NumNniIntf = nnni
79 olt.Name = "BBSIM OLT"
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090080 olt.InternalState = OLT_INACTIVE
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090081 olt.OperState = "up"
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +020082 olt.Manufacture = "BBSIM"
83 olt.SerialNumber = "BBSIMOLT00" + strconv.FormatInt(int64(oltid), 10)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090084 olt.Intfs = make([]intf, olt.NumPonIntf+olt.NumNniIntf)
85 olt.HeartbeatSignature = oltid
Keita NISHIMOTO3f080622019-01-16 10:21:22 +090086 olt.mu = &sync.Mutex{}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090087 for i := uint32(0); i < olt.NumNniIntf; i++ {
88 olt.Intfs[i].IntfID = i
89 olt.Intfs[i].OperState = "up"
90 olt.Intfs[i].Type = "nni"
91 }
92 for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf+olt.NumNniIntf; i++ {
93 olt.Intfs[i].IntfID = i
94 olt.Intfs[i].OperState = "up"
95 olt.Intfs[i].Type = "pon"
96 }
97 return &olt
98}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090099
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200100// Initialize method initializes NNI and PON ports
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900101func (olt *Olt) Initialize() {
102 olt.InternalState = OLT_INACTIVE
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900103 olt.OperState = "up"
104 for i := uint32(0); i < olt.NumNniIntf; i++ {
105 olt.Intfs[i].IntfID = i
106 olt.Intfs[i].OperState = "up"
107 olt.Intfs[i].Type = "nni"
108 }
109 for i := uint32(olt.NumNniIntf); i < olt.NumPonIntf+olt.NumNniIntf; i++ {
110 olt.Intfs[i].IntfID = i
111 olt.Intfs[i].OperState = "up"
112 olt.Intfs[i].Type = "pon"
113 }
114}
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900115
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200116// GetIntState returns internal state of OLT
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900117func (olt *Olt) GetIntState() DeviceState {
118 olt.mu.Lock()
119 defer olt.mu.Unlock()
120 return olt.InternalState
121}
122
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200123// GetDevkey returns device key of OLT
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900124func (olt *Olt) GetDevkey () Devkey {
125 return Devkey{ID: olt.ID}
126}
127
Zdravko Bozakov8b9328c2019-05-15 05:13:34 +0200128// UpdateIntState method updates OLT internal state
Keita NISHIMOTO3f080622019-01-16 10:21:22 +0900129func (olt *Olt) UpdateIntState(intstate DeviceState) {
130 olt.mu.Lock()
131 defer olt.mu.Unlock()
132 olt.InternalState = intstate
133}