blob: 8fbe789eb07f5ff163c9c06996f30c88165d95d4 [file] [log] [blame]
Matteo Scandolof65e6872020-04-15 15:18:43 -07001/*
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 sadis
18
19import (
Matteo Scandolo8a574812021-05-20 15:18:53 -070020 "encoding/json"
Matteo Scandolof65e6872020-04-15 15:18:43 -070021 "fmt"
Matteo Scandolo8a574812021-05-20 15:18:53 -070022 "github.com/gorilla/mux"
23 "github.com/opencord/bbsim/internal/common"
24 omcilib "github.com/opencord/bbsim/internal/common/omci"
Shrey Baide72b3cc2020-05-12 00:03:06 +053025 "net"
Matteo Scandolo8a574812021-05-20 15:18:53 -070026 "net/http"
27 "net/http/httptest"
Shrey Baide72b3cc2020-05-12 00:03:06 +053028 "testing"
29
Matteo Scandolof65e6872020-04-15 15:18:43 -070030 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolof65e6872020-04-15 15:18:43 -070031 "gotest.tools/assert"
Matteo Scandolof65e6872020-04-15 15:18:43 -070032)
33
Shrey Baid688b4242020-07-10 20:40:10 +053034func createMockDevices() (*devices.OltDevice, *devices.Onu) {
Matteo Scandolof65e6872020-04-15 15:18:43 -070035
Matteo Scandolo8a574812021-05-20 15:18:53 -070036 // create a ONU
Shrey Baid688b4242020-07-10 20:40:10 +053037 onu := &devices.Onu{
Matteo Scandolof65e6872020-04-15 15:18:43 -070038 ID: 1,
39 PonPortID: 1,
Matteo Scandolo8a574812021-05-20 15:18:53 -070040 }
41 onu.SerialNumber = devices.NewSN(0, onu.PonPortID, onu.ID)
42
43 // create 2 UNIs for the ONU
44 unis := []devices.UniPortIf{
45 &devices.UniPort{ID: 0, Onu: onu, MeId: omcilib.GenerateUniPortEntityId(1)},
46 &devices.UniPort{ID: 1, Onu: onu, MeId: omcilib.GenerateUniPortEntityId(2)},
47 }
48 onu.UniPorts = unis
49
50 // create a service on each UNI
51 c_tag := 900
52 for i, u := range onu.UniPorts {
53 uni := u.(*devices.UniPort)
54 mac := net.HardwareAddr{0x2e, 0x01, byte(1), byte(1), byte(0), byte(i)}
55 uni.Services = []devices.ServiceIf{
56 &devices.Service{Name: "hsia", CTag: c_tag + i, STag: 900, NeedsEapol: true, NeedsDhcp: true, NeedsIgmp: true, HwAddress: mac, TechnologyProfileID: 64},
57 }
Matteo Scandolof65e6872020-04-15 15:18:43 -070058 }
Matteo Scandolo4a036262020-08-17 15:56:13 -070059
Matteo Scandolo8a574812021-05-20 15:18:53 -070060 olt := &devices.OltDevice{
61 ID: 0,
62 Pons: []*devices.PonPort{
63 {
64 ID: 0,
65 NumOnu: 1,
66 Onus: []*devices.Onu{onu},
67 },
68 },
Matteo Scandolo4a036262020-08-17 15:56:13 -070069 }
Matteo Scandolof65e6872020-04-15 15:18:43 -070070
71 return olt, onu
72}
73
Matteo Scandolo4a036262020-08-17 15:56:13 -070074func TestSadisServer_GetOnuEntryV2(t *testing.T) {
Matteo Scandolof65e6872020-04-15 15:18:43 -070075
76 olt, onu := createMockDevices()
77
Matteo Scandolo8a574812021-05-20 15:18:53 -070078 for _, u := range onu.UniPorts {
79 uni := u.(*devices.UniPort)
Matteo Scandolof65e6872020-04-15 15:18:43 -070080
Matteo Scandolo8a574812021-05-20 15:18:53 -070081 entry, err := GetOnuEntryV2(olt, onu, fmt.Sprintf("%d", uni.ID+1))
Matteo Scandolof65e6872020-04-15 15:18:43 -070082
Matteo Scandolo8a574812021-05-20 15:18:53 -070083 assert.NilError(t, err)
Matteo Scandolof65e6872020-04-15 15:18:43 -070084
Matteo Scandolo8a574812021-05-20 15:18:53 -070085 assert.Equal(t, fmt.Sprintf("%s-%d", onu.Sn(), uni.ID+1), entry.ID)
Matteo Scandolo4a036262020-08-17 15:56:13 -070086
Matteo Scandolo8a574812021-05-20 15:18:53 -070087 // we only have one service, thus get a single entry in the UniTagList
88 assert.Equal(t, len(entry.UniTagList), 1)
89 assert.Equal(t, entry.UniTagList[0].PonCTag, int(900+uni.ID))
90 assert.Equal(t, entry.UniTagList[0].PonSTag, 900)
91 assert.Equal(t, entry.UniTagList[0].DownstreamBandwidthProfile, "User_Bandwidth2")
92 assert.Equal(t, entry.UniTagList[0].UpstreamBandwidthProfile, "User_Bandwidth1")
93 assert.Equal(t, entry.UniTagList[0].TechnologyProfileID, 64)
94 assert.Equal(t, entry.UniTagList[0].IsDhcpRequired, true)
95 assert.Equal(t, entry.UniTagList[0].IsIgmpRequired, true)
96 }
97}
98
99func TestSadisServer_ServeStaticConfig(t *testing.T) {
100 olt, onu := createMockDevices()
101 common.Config = &common.GlobalConfig{
102 Olt: common.OltConfig{
103 ID: olt.ID,
104 PonPorts: 1,
105 OnusPonPort: 1,
106 DeviceId: net.HardwareAddr{0xA, 0xA, 0xA, 0xA, 0xA, byte(olt.ID)}.String(),
107 },
108 }
109
110 s := &SadisServer{
111 Olt: olt,
112 }
113
114 // Need to create a router that we can pass the request through so that the vars will be added to the context
115 rr := httptest.NewRecorder()
116 router := mux.NewRouter()
117 router.HandleFunc(StaticConfigUrl, s.ServeStaticConfig)
118
119 // check that v2 returns the expected result
120 req, err := http.NewRequest("GET", "/v2/static", nil)
121 if err != nil {
122 t.Fatal(err)
123 }
124 router.ServeHTTP(rr, req)
125
126 if status := rr.Code; status != http.StatusOK {
127 t.Errorf("handler returned wrong status code: got %v want %v",
128 status, http.StatusOK)
129 }
130
131 cfg := SadisConfig{}
132 if err := json.Unmarshal(rr.Body.Bytes(), &cfg); err != nil {
133 t.Fatal(err.Error())
134 }
135
136 assert.Equal(t, len(cfg.Sadis.Entries), 3) // 2 UNI and 1 OLT
137
138 // OLT
139 oltEntry := cfg.Sadis.Entries[0].(map[string]interface{})
140 assert.Equal(t, oltEntry["hardwareIdentifier"], common.Config.Olt.DeviceId)
141
142 // UNIs
143 for i, u := range onu.UniPorts {
144 uni := u.(*devices.UniPort)
145 uniEntry := cfg.Sadis.Entries[i+1].(map[string]interface{})
146 assert.Equal(t, uniEntry["id"], fmt.Sprintf("%s-%d", onu.Sn(), uni.ID+1))
147 }
Matteo Scandolof65e6872020-04-15 15:18:43 -0700148}
149
Matteo Scandolo4a036262020-08-17 15:56:13 -0700150func TestSadisServer_GetOnuEntryV2_multi_service(t *testing.T) {
151
Matteo Scandolo8a574812021-05-20 15:18:53 -0700152 mac := net.HardwareAddr{0x2e, byte(1), byte(1), byte(1), byte(1), byte(2)}
Matteo Scandolo4a036262020-08-17 15:56:13 -0700153
Matteo Scandolo8a574812021-05-20 15:18:53 -0700154 hsia := devices.Service{Name: "hsia", HwAddress: net.HardwareAddr{0x2e, byte(1), byte(1), byte(1), byte(1), byte(1)},
Matteo Scandolo4a036262020-08-17 15:56:13 -0700155 CTag: 900, STag: 900, TechnologyProfileID: 64}
156
157 voip := devices.Service{Name: "voip", HwAddress: mac,
158 CTag: 901, STag: 900, TechnologyProfileID: 65}
159
Matteo Scandolo8a574812021-05-20 15:18:53 -0700160 vod := devices.Service{Name: "vod", HwAddress: net.HardwareAddr{0x2e, byte(1), byte(1), byte(1), byte(1), byte(3)},
Matteo Scandolo4a036262020-08-17 15:56:13 -0700161 CTag: 902, STag: 900, TechnologyProfileID: 66}
162
Matteo Scandolof65e6872020-04-15 15:18:43 -0700163 olt, onu := createMockDevices()
164
Matteo Scandolo8a574812021-05-20 15:18:53 -0700165 onu.UniPorts[0].(*devices.UniPort).Services = []devices.ServiceIf{&hsia, &voip, &vod}
Matteo Scandolo4a036262020-08-17 15:56:13 -0700166
Matteo Scandolof65e6872020-04-15 15:18:43 -0700167 uni := "1"
168
Matteo Scandolo4a036262020-08-17 15:56:13 -0700169 entry, err := GetOnuEntryV2(olt, onu, uni)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700170
Matteo Scandolo4a036262020-08-17 15:56:13 -0700171 assert.NilError(t, err)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700172
Matteo Scandolo4a036262020-08-17 15:56:13 -0700173 assert.Equal(t, entry.ID, fmt.Sprintf("%s-%s", onu.Sn(), uni))
Matteo Scandolof65e6872020-04-15 15:18:43 -0700174
Matteo Scandolo4a036262020-08-17 15:56:13 -0700175 assert.Equal(t, len(entry.UniTagList), 3)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700176
Matteo Scandolo4a036262020-08-17 15:56:13 -0700177 assert.Equal(t, entry.UniTagList[0].PonCTag, 900)
178 assert.Equal(t, entry.UniTagList[0].PonSTag, 900)
179 assert.Equal(t, entry.UniTagList[0].TechnologyProfileID, 64)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700180
Matteo Scandolo4a036262020-08-17 15:56:13 -0700181 assert.Equal(t, entry.UniTagList[1].PonCTag, 901)
182 assert.Equal(t, entry.UniTagList[1].PonSTag, 900)
183 assert.Equal(t, entry.UniTagList[1].TechnologyProfileID, 65)
Matteo Scandolof65e6872020-04-15 15:18:43 -0700184
Matteo Scandolo4a036262020-08-17 15:56:13 -0700185 assert.Equal(t, entry.UniTagList[2].PonCTag, 902)
186 assert.Equal(t, entry.UniTagList[2].PonSTag, 900)
187 assert.Equal(t, entry.UniTagList[2].TechnologyProfileID, 66)
Shrey Baide72b3cc2020-05-12 00:03:06 +0530188}