blob: e7b7c0c2a8fdc7345ef02fa7ba1fd84596d472d5 [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"
21 "gerrit.opencord.org/voltha-bbsim/protos"
22 "reflect"
23)
24
25type onuState int
26
27const (
28 ONU_PRE_ACTIVATED onuState = iota
29 ONU_ACTIVATED
30)
31
32type Onu struct {
33 InternalState onuState
34 IntfID uint32
35 OperState string
36 SerialNumber *openolt.SerialNumber
37 OnuID uint32
38}
39
Keita NISHIMOTO246b8282018-10-13 04:14:51 +090040func createSN(oltid uint32, intfid uint32, onuid uint32) []byte {
41 sn := []byte{0, byte(oltid % 256), byte(intfid), byte(onuid)}
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090042 return sn
43}
44
45func CreateOnus(oltid uint32, intfid uint32, nonus uint32, nnni uint32) []*Onu {
46 onus := []*Onu{}
47 for i := 0; i < int(nonus); i++ {
48 onu := Onu{}
49 onu.InternalState = ONU_PRE_ACTIVATED
50 onu.IntfID = intfid
51 onu.OperState = "up"
52 onu.SerialNumber = new(openolt.SerialNumber)
53 onu.SerialNumber.VendorId = []byte("NONE")
Keita NISHIMOTO246b8282018-10-13 04:14:51 +090054 onu.SerialNumber.VendorSpecific = createSN(oltid, intfid, uint32(i))
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090055 onus = append(onus, &onu)
56 }
57 return onus
58}
59
60func ValidateONU(targetonu openolt.Onu, regonus map[uint32][]*Onu) bool {
61 for _, onus := range regonus {
62 for _, onu := range onus {
63 if ValidateSN(*targetonu.SerialNumber, *onu.SerialNumber) {
64 return true
65 }
66 }
67 }
68 return false
69}
70
71func ValidateSN(sn1 openolt.SerialNumber, sn2 openolt.SerialNumber) bool {
72 return reflect.DeepEqual(sn1.VendorId, sn2.VendorId) && reflect.DeepEqual(sn1.VendorSpecific, sn2.VendorSpecific)
73}
74
75func UpdateOnusOpStatus(ponif uint32, onus []*Onu, opstatus string) {
76 for i, onu := range onus {
77 onu.OperState = "up"
78 log.Printf("(PONIF:%d) ONU [%d] discovered.\n", ponif, i)
79 }
80}