blob: 65499a83ad775e5b97dc2a0bdcc4f1c6caf5c220 [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
19import (
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090020 "log"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090021 "reflect"
Matteo Scandoloa96633a2018-10-18 16:23:02 -070022
23 "gerrit.opencord.org/voltha-bbsim/protos"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090024)
25
26type onuState int
27
28const (
29 ONU_PRE_ACTIVATED onuState = iota
30 ONU_ACTIVATED
31)
32
33type Onu struct {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090034 InternalState *onuState
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090035 IntfID uint32
36 OperState string
37 SerialNumber *openolt.SerialNumber
38 OnuID uint32
39}
40
Keita NISHIMOTO246b8282018-10-13 04:14:51 +090041func createSN(oltid uint32, intfid uint32, onuid uint32) []byte {
42 sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090043 return sn
44}
45
46func CreateOnus(oltid uint32, intfid uint32, nonus uint32, nnni uint32) []*Onu {
47 onus := []*Onu{}
48 for i := 0; i < int(nonus); i++ {
49 onu := Onu{}
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090050 onu.InternalState = new(onuState)
51 *onu.InternalState = ONU_PRE_ACTIVATED
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090052 onu.IntfID = intfid
53 onu.OperState = "up"
54 onu.SerialNumber = new(openolt.SerialNumber)
Matteo Scandoloa96633a2018-10-18 16:23:02 -070055 onu.SerialNumber.VendorId = []byte("BBSM")
Keita NISHIMOTO246b8282018-10-13 04:14:51 +090056 onu.SerialNumber.VendorSpecific = createSN(oltid, intfid, uint32(i))
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090057 onus = append(onus, &onu)
58 }
59 return onus
60}
61
Matteo Scandoloa96633a2018-10-18 16:23:02 -070062func (onu *Onu) InitializeStatus() {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090063 onu.OperState = "up"
64 *onu.InternalState = ONU_PRE_ACTIVATED
65}
66
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090067func ValidateONU(targetonu openolt.Onu, regonus map[uint32][]*Onu) bool {
68 for _, onus := range regonus {
69 for _, onu := range onus {
70 if ValidateSN(*targetonu.SerialNumber, *onu.SerialNumber) {
71 return true
72 }
73 }
74 }
75 return false
76}
77
78func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool {
79 return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific)
80}
81
82func UpdateOnusOpStatus(ponif uint32, onus []*Onu, opstatus string) {
83 for i, onu := range onus {
84 onu.OperState = "up"
85 log.Printf("(PONIF:%d) ONU [%d] discovered.\n", ponif, i)
86 }
87}