Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package sadis |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 20 | "encoding/json" |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 21 | "fmt" |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 22 | "github.com/gorilla/mux" |
| 23 | "github.com/opencord/bbsim/internal/common" |
| 24 | omcilib "github.com/opencord/bbsim/internal/common/omci" |
Shrey Baid | e72b3cc | 2020-05-12 00:03:06 +0530 | [diff] [blame] | 25 | "net" |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 26 | "net/http" |
| 27 | "net/http/httptest" |
Shrey Baid | e72b3cc | 2020-05-12 00:03:06 +0530 | [diff] [blame] | 28 | "testing" |
| 29 | |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 30 | "github.com/opencord/bbsim/internal/bbsim/devices" |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 31 | "gotest.tools/assert" |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 34 | func createMockDevices() (*devices.OltDevice, *devices.Onu) { |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 35 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 36 | // create a ONU |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 37 | onu := &devices.Onu{ |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 38 | ID: 1, |
| 39 | PonPortID: 1, |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 40 | } |
| 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 Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 58 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 59 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 60 | 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 Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 69 | } |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 70 | |
| 71 | return olt, onu |
| 72 | } |
| 73 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 74 | func TestSadisServer_GetOnuEntryV2(t *testing.T) { |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 75 | |
| 76 | olt, onu := createMockDevices() |
| 77 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 78 | for _, u := range onu.UniPorts { |
| 79 | uni := u.(*devices.UniPort) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 80 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 81 | entry, err := GetOnuEntryV2(olt, onu, fmt.Sprintf("%d", uni.ID+1)) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 82 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 83 | assert.NilError(t, err) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 84 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 85 | assert.Equal(t, fmt.Sprintf("%s-%d", onu.Sn(), uni.ID+1), entry.ID) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 86 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 87 | // 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 | |
| 99 | func 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 Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 150 | func TestSadisServer_GetOnuEntryV2_multi_service(t *testing.T) { |
| 151 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 152 | mac := net.HardwareAddr{0x2e, byte(1), byte(1), byte(1), byte(1), byte(2)} |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 153 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 154 | hsia := devices.Service{Name: "hsia", HwAddress: net.HardwareAddr{0x2e, byte(1), byte(1), byte(1), byte(1), byte(1)}, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 155 | CTag: 900, STag: 900, TechnologyProfileID: 64} |
| 156 | |
| 157 | voip := devices.Service{Name: "voip", HwAddress: mac, |
| 158 | CTag: 901, STag: 900, TechnologyProfileID: 65} |
| 159 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 160 | vod := devices.Service{Name: "vod", HwAddress: net.HardwareAddr{0x2e, byte(1), byte(1), byte(1), byte(1), byte(3)}, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 161 | CTag: 902, STag: 900, TechnologyProfileID: 66} |
| 162 | |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 163 | olt, onu := createMockDevices() |
| 164 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame^] | 165 | onu.UniPorts[0].(*devices.UniPort).Services = []devices.ServiceIf{&hsia, &voip, &vod} |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 166 | |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 167 | uni := "1" |
| 168 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 169 | entry, err := GetOnuEntryV2(olt, onu, uni) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 170 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 171 | assert.NilError(t, err) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 172 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 173 | assert.Equal(t, entry.ID, fmt.Sprintf("%s-%s", onu.Sn(), uni)) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 174 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 175 | assert.Equal(t, len(entry.UniTagList), 3) |
Matteo Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 176 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 177 | 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 Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 180 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 181 | 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 Scandolo | f65e687 | 2020-04-15 15:18:43 -0700 | [diff] [blame] | 184 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 185 | 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 Baid | e72b3cc | 2020-05-12 00:03:06 +0530 | [diff] [blame] | 188 | } |