blob: 09b39e034b1cce33d40ed70d6d0dcf053e1e546d [file] [log] [blame]
khenaidoo89b0e942018-10-21 21:11:33 -04001/*
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 */
16package graph
17
18import (
19 "errors"
20 "fmt"
William Kurkiandaa6bb22019-03-07 12:26:28 -050021 "github.com/opencord/voltha-protos/go/openflow_13"
22 "github.com/opencord/voltha-protos/go/voltha"
khenaidoo89b0e942018-10-21 21:11:33 -040023 "github.com/stretchr/testify/assert"
24 "testing"
25 "time"
26)
27
28var ld voltha.LogicalDevice
29var olt voltha.Device
30var onusOnPort4 []voltha.Device
31var onusOnPort5 []voltha.Device
32
33const (
34 maxOnuOnPort4 int = 64
35 maxOnuOnPort5 int = 64
36)
37
38func init() {
39
40 logicalDeviceId := "ld"
41 oltDeviceId := "olt"
42
43 // Setup ONUs on OLT port 4
44 onusOnPort4 = make([]voltha.Device, 0)
45 var onu voltha.Device
46 var id string
47 oltPeerPort := uint32(4)
48 for i := 0; i < maxOnuOnPort4; i++ {
49 id := fmt.Sprintf("onu%d", i)
50 onu = voltha.Device{Id: id, ParentId: oltDeviceId}
51 ponPort := voltha.Port{PortNo: 1, DeviceId: onu.Id, Type: voltha.Port_PON_ONU}
52 ponPort.Peers = make([]*voltha.Port_PeerPort, 0)
53 peerPort := voltha.Port_PeerPort{DeviceId: oltDeviceId, PortNo: oltPeerPort}
54 ponPort.Peers = append(ponPort.Peers, &peerPort)
55 uniPort := voltha.Port{PortNo: 2, DeviceId: onu.Id, Type: voltha.Port_ETHERNET_UNI}
56 onu.Ports = make([]*voltha.Port, 0)
57 onu.Ports = append(onu.Ports, &ponPort)
58 onu.Ports = append(onu.Ports, &uniPort)
59 onusOnPort4 = append(onusOnPort4, onu)
60 }
61
62 // Setup ONUs on OLT port 5
63 onusOnPort5 = make([]voltha.Device, 0)
64 oltPeerPort = uint32(5)
65 for i := 0; i < maxOnuOnPort5; i++ {
66 id := fmt.Sprintf("onu%d", i+maxOnuOnPort4)
67 onu = voltha.Device{Id: id, ParentId: oltDeviceId}
68 ponPort := voltha.Port{PortNo: 1, DeviceId: onu.Id, Type: voltha.Port_PON_ONU}
69 ponPort.Peers = make([]*voltha.Port_PeerPort, 0)
70 peerPort := voltha.Port_PeerPort{DeviceId: oltDeviceId, PortNo: oltPeerPort}
71 ponPort.Peers = append(ponPort.Peers, &peerPort)
72 uniPort := voltha.Port{PortNo: 2, DeviceId: onu.Id, Type: voltha.Port_ETHERNET_UNI}
73 onu.Ports = make([]*voltha.Port, 0)
74 onu.Ports = append(onu.Ports, &ponPort)
75 onu.Ports = append(onu.Ports, &uniPort)
76 onusOnPort5 = append(onusOnPort5, onu)
77 }
78
79 // Setup OLT
80 // Setup the OLT device
81 olt = voltha.Device{Id: oltDeviceId, ParentId: logicalDeviceId}
82 p1 := voltha.Port{PortNo: 2, DeviceId: oltDeviceId, Type: voltha.Port_ETHERNET_NNI}
83 p2 := voltha.Port{PortNo: 3, DeviceId: oltDeviceId, Type: voltha.Port_ETHERNET_NNI}
84 p3 := voltha.Port{PortNo: 4, DeviceId: oltDeviceId, Type: voltha.Port_PON_OLT}
85 p4 := voltha.Port{PortNo: 5, DeviceId: oltDeviceId, Type: voltha.Port_PON_OLT}
86 p3.Peers = make([]*voltha.Port_PeerPort, 0)
87 for _, onu := range onusOnPort4 {
88 peerPort := voltha.Port_PeerPort{DeviceId: onu.Id, PortNo: p3.PortNo}
89 p3.Peers = append(p3.Peers, &peerPort)
90 }
91 p4.Peers = make([]*voltha.Port_PeerPort, 0)
92 for _, onu := range onusOnPort5 {
93 peerPort := voltha.Port_PeerPort{DeviceId: onu.Id, PortNo: p4.PortNo}
94 p4.Peers = append(p4.Peers, &peerPort)
95 }
96 olt.Ports = make([]*voltha.Port, 0)
97 olt.Ports = append(olt.Ports, &p1)
98 olt.Ports = append(olt.Ports, &p2)
99 olt.Ports = append(olt.Ports, &p3)
100 olt.Ports = append(olt.Ports, &p4)
101
102 // Setup the logical device
103 ld = voltha.LogicalDevice{Id: logicalDeviceId}
104 ld.Ports = make([]*voltha.LogicalPort, 0)
105 ofpPortNo := 1
106 //Add olt ports
107 for i, port := range olt.Ports {
108 if port.Type == voltha.Port_ETHERNET_NNI {
109 id = fmt.Sprintf("nni-%d", i)
110 lp := voltha.LogicalPort{Id: id, DeviceId: olt.Id, DevicePortNo: port.PortNo, OfpPort: &openflow_13.OfpPort{PortNo: uint32(ofpPortNo)}, RootPort: true}
111 ld.Ports = append(ld.Ports, &lp)
112 ofpPortNo = ofpPortNo + 1
113 }
114 }
115 //Add onu ports on port 4
116 for i, onu := range onusOnPort4 {
117 for _, port := range onu.Ports {
118 if port.Type == voltha.Port_ETHERNET_UNI {
119 id = fmt.Sprintf("uni-%d", i)
120 lp := voltha.LogicalPort{Id: id, DeviceId: onu.Id, DevicePortNo: port.PortNo, OfpPort: &openflow_13.OfpPort{PortNo: uint32(ofpPortNo)}, RootPort: false}
121 ld.Ports = append(ld.Ports, &lp)
122 ofpPortNo = ofpPortNo + 1
123 }
124 }
125 }
126 //Add onu ports on port 5
127 for i, onu := range onusOnPort5 {
128 for _, port := range onu.Ports {
129 if port.Type == voltha.Port_ETHERNET_UNI {
130 id = fmt.Sprintf("uni-%d", i+10)
131 lp := voltha.LogicalPort{Id: id, DeviceId: onu.Id, DevicePortNo: port.PortNo, OfpPort: &openflow_13.OfpPort{PortNo: uint32(ofpPortNo)}, RootPort: false}
132 ld.Ports = append(ld.Ports, &lp)
133 ofpPortNo = ofpPortNo + 1
134 }
135 }
136 }
137}
138
139func GetDeviceHelper(id string) (*voltha.Device, error) {
140 if id == "olt" {
141 return &olt, nil
142 }
143 for _, onu := range onusOnPort4 {
144 if onu.Id == id {
145 return &onu, nil
146 }
147 }
148 for _, onu := range onusOnPort5 {
149 if onu.Id == id {
150 return &onu, nil
151 }
152 }
153 return nil, errors.New("Not-found")
154}
155
156func TestGetRoutes(t *testing.T) {
157
158 getDevice := GetDeviceHelper
159
160 // Create a device graph and computes Routes
161 start := time.Now()
162 dg := NewDeviceGraph(getDevice)
163 dg.ComputeRoutes(ld.Ports)
164 fmt.Println("Total Time creating graph & compute Routes:", time.Since(start))
165 assert.NotNil(t, dg.GGraph)
166 assert.EqualValues(t, (maxOnuOnPort4*4 + maxOnuOnPort5*4), len(dg.Routes))
167 //for k, v := range dg.Routes {
168 // fmt.Println("key", k, " value:", v)
169 //}
170
171}