Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-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 core |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "testing" |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 22 | "time" |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 23 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 24 | "github.com/opencord/voltha-protos/v5/go/openflow_13" |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 26 | "github.com/stretchr/testify/assert" |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 27 | "google.golang.org/protobuf/types/known/timestamppb" |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | testDeviceId = "123145abcdef" |
| 32 | ) |
| 33 | |
| 34 | func getItemWithPath(items []YangItem, path string) (value string, ok bool) { |
| 35 | for _, item := range items { |
| 36 | if item.Path == path { |
| 37 | return item.Value, true |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return "", false |
| 42 | } |
| 43 | |
| 44 | func TestDevicePath(t *testing.T) { |
| 45 | path := getDevicePath(testDeviceId) |
| 46 | assert.Equal(t, fmt.Sprintf("/bbf-device-aggregation:devices/device[name='%s']", testDeviceId), path) |
| 47 | } |
| 48 | |
| 49 | func TestTranslateDevice(t *testing.T) { |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 50 | olt := &voltha.Device{ |
| 51 | Id: testDeviceId, |
| 52 | Root: true, |
| 53 | Vendor: "BBSim", |
| 54 | Model: "asfvolt16", |
| 55 | SerialNumber: "BBSIM_OLT_10", |
| 56 | HardwareVersion: "v0.0.2", |
| 57 | FirmwareVersion: "v0.0.3", |
| 58 | AdminState: voltha.AdminState_ENABLED, |
| 59 | OperStatus: voltha.OperStatus_ACTIVE, |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 60 | } |
| 61 | items := translateDevice(olt) |
| 62 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 63 | oltPath := getDevicePath(testDeviceId) |
| 64 | oltHwPath := getDeviceHardwarePath(testDeviceId) |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 65 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 66 | expected := []YangItem{ |
| 67 | { |
| 68 | Path: oltPath + "/type", |
| 69 | Value: DeviceTypeOlt, |
| 70 | }, |
| 71 | { |
| 72 | Path: oltHwPath + "/mfg-name", |
| 73 | Value: "BBSim", |
| 74 | }, |
| 75 | { |
| 76 | Path: oltHwPath + "/model-name", |
| 77 | Value: "asfvolt16", |
| 78 | }, |
| 79 | { |
| 80 | Path: oltHwPath + "/hardware-rev", |
| 81 | Value: "v0.0.2", |
| 82 | }, |
| 83 | { |
| 84 | Path: oltHwPath + "/firmware-rev", |
| 85 | Value: "v0.0.3", |
| 86 | }, |
| 87 | { |
| 88 | Path: oltHwPath + "/serial-num", |
| 89 | Value: "BBSIM_OLT_10", |
| 90 | }, |
| 91 | { |
| 92 | Path: oltHwPath + "/state/admin-state", |
| 93 | Value: ietfAdminStateUnlocked, |
| 94 | }, |
| 95 | { |
| 96 | Path: oltHwPath + "/state/oper-state", |
| 97 | Value: ietfOperStateEnabled, |
| 98 | }, |
| 99 | } |
| 100 | |
| 101 | assert.NotEmpty(t, items, "No OLT items") |
| 102 | for _, e := range expected { |
| 103 | val, ok := getItemWithPath(items, e.Path) |
| 104 | assert.True(t, ok, e.Path+" missing for OLT") |
| 105 | assert.Equal(t, e.Value, val, "Wrong value for "+e.Path) |
| 106 | } |
| 107 | |
| 108 | onu := &voltha.Device{ |
| 109 | Id: testDeviceId, |
| 110 | Root: false, |
| 111 | Vendor: "BBSM", |
| 112 | Model: "v0.0.1", |
| 113 | SerialNumber: "BBSM000a0001", |
| 114 | HardwareVersion: "v0.0.2", |
| 115 | FirmwareVersion: "v0.0.3", |
| 116 | AdminState: voltha.AdminState_ENABLED, |
| 117 | OperStatus: voltha.OperStatus_ACTIVE, |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 118 | } |
| 119 | items = translateDevice(onu) |
| 120 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 121 | onuPath := getDevicePath(testDeviceId) |
| 122 | onuHwPath := getDeviceHardwarePath(testDeviceId) |
| 123 | |
| 124 | expected = []YangItem{ |
| 125 | { |
| 126 | Path: onuPath + "/type", |
| 127 | Value: DeviceTypeOnu, |
| 128 | }, |
| 129 | { |
| 130 | Path: onuHwPath + "/mfg-name", |
| 131 | Value: "BBSM", |
| 132 | }, |
| 133 | { |
| 134 | Path: onuHwPath + "/model-name", |
| 135 | Value: "v0.0.1", |
| 136 | }, |
| 137 | { |
| 138 | Path: onuHwPath + "/hardware-rev", |
| 139 | Value: "v0.0.2", |
| 140 | }, |
| 141 | { |
| 142 | Path: onuHwPath + "/firmware-rev", |
| 143 | Value: "v0.0.3", |
| 144 | }, |
| 145 | { |
| 146 | Path: onuHwPath + "/serial-num", |
| 147 | Value: "BBSM000a0001", |
| 148 | }, |
| 149 | { |
| 150 | Path: onuHwPath + "/state/admin-state", |
| 151 | Value: ietfAdminStateUnlocked, |
| 152 | }, |
| 153 | { |
| 154 | Path: onuHwPath + "/state/oper-state", |
| 155 | Value: ietfOperStateEnabled, |
| 156 | }, |
| 157 | } |
| 158 | |
| 159 | assert.NotEmpty(t, items, "No ONU items") |
| 160 | for _, e := range expected { |
| 161 | val, ok := getItemWithPath(items, e.Path) |
| 162 | assert.True(t, ok, e.Path+" missing for ONU") |
| 163 | assert.Equal(t, e.Value, val, "Wrong value for "+e.Path) |
| 164 | } |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 167 | func TestTranslateOnuPorts(t *testing.T) { |
| 168 | ports := &voltha.Ports{ |
| 169 | Items: []*voltha.Port{ |
| 170 | { |
| 171 | PortNo: 0, |
| 172 | Type: voltha.Port_ETHERNET_UNI, |
| 173 | OperStatus: voltha.OperStatus_ACTIVE, |
| 174 | }, |
| 175 | }, |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 176 | } |
| 177 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 178 | _, err := translateOnuPorts(testDeviceId, ports) |
| 179 | assert.Error(t, err, "No error for missing Ofp port") |
| 180 | |
| 181 | ports = &voltha.Ports{ |
| 182 | Items: []*voltha.Port{ |
| 183 | { |
| 184 | PortNo: 0, |
| 185 | Type: voltha.Port_ETHERNET_UNI, |
| 186 | OfpPort: &openflow_13.OfpPort{ |
| 187 | Name: "BBSM000a0001-1", |
| 188 | }, |
| 189 | OperStatus: voltha.OperStatus_ACTIVE, |
| 190 | }, |
| 191 | { |
| 192 | PortNo: 1, |
| 193 | Type: voltha.Port_ETHERNET_UNI, |
| 194 | OfpPort: &openflow_13.OfpPort{ |
| 195 | Name: "BBSM000a0001-2", |
| 196 | }, |
| 197 | OperStatus: voltha.OperStatus_UNKNOWN, |
| 198 | }, |
| 199 | { |
| 200 | PortNo: 0, |
| 201 | Type: voltha.Port_PON_ONU, |
| 202 | OperStatus: voltha.OperStatus_UNKNOWN, |
| 203 | }, |
| 204 | }, |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 207 | portsItems, err := translateOnuPorts(testDeviceId, ports) |
| 208 | assert.Nil(t, err, "Translation error") |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 209 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 210 | /*2 items for 2 UNIs, PON is ignored*/ |
| 211 | assert.Equal(t, 4, len(portsItems), "No ports items") |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 212 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 213 | interfacesPath := getDevicePath(testDeviceId) + "/data/ietf-interfaces:interfaces" |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 214 | |
Elia Battiston | 4750d3c | 2022-07-14 13:24:56 +0000 | [diff] [blame^] | 215 | expected := []YangItem{ |
| 216 | { |
| 217 | Path: fmt.Sprintf("%s/interface[name='%s']/oper-status", interfacesPath, "BBSM000a0001-1"), |
| 218 | Value: ietfOperStateUp, |
| 219 | }, |
| 220 | { |
| 221 | Path: fmt.Sprintf("%s/interface[name='%s']/type", interfacesPath, "BBSM000a0001-1"), |
| 222 | Value: "bbf-xpon-if-type:onu-v-vrefpoint", |
| 223 | }, |
| 224 | { |
| 225 | Path: fmt.Sprintf("%s/interface[name='%s']/oper-status", interfacesPath, "BBSM000a0001-2"), |
| 226 | Value: ietfOperStateUnknown, |
| 227 | }, |
| 228 | { |
| 229 | Path: fmt.Sprintf("%s/interface[name='%s']/type", interfacesPath, "BBSM000a0001-2"), |
| 230 | Value: "bbf-xpon-if-type:onu-v-vrefpoint", |
| 231 | }, |
| 232 | } |
| 233 | |
| 234 | for _, e := range expected { |
| 235 | val, ok := getItemWithPath(portsItems, e.Path) |
| 236 | assert.True(t, ok, e.Path+" missing for ports") |
| 237 | assert.Equal(t, e.Value, val, "Wrong value for "+e.Path) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | func TestTranslateOnuActive(t *testing.T) { |
| 242 | timestamp := time.Now() |
| 243 | eventHeader := &voltha.EventHeader{ |
| 244 | Id: "Voltha.openolt.ONU_ACTIVATED.1657705515351182767", |
| 245 | RaisedTs: timestamppb.New(timestamp), |
| 246 | Category: voltha.EventCategory_EQUIPMENT, |
| 247 | Type: voltha.EventType_DEVICE_EVENT, |
| 248 | } |
| 249 | |
| 250 | deviceEvent := &voltha.DeviceEvent{ |
| 251 | ResourceId: testDeviceId, |
| 252 | DeviceEventName: "ONU_ACTIVATED_RAISE_EVENT", |
| 253 | Description: "ONU Event - ONU_ACTIVATED - Raised", |
| 254 | Context: map[string]string{}, |
| 255 | } |
| 256 | |
| 257 | _, _, err := TranslateOnuActivatedEvent(eventHeader, deviceEvent) |
| 258 | assert.Error(t, err, "Empty context produces no error") |
| 259 | |
| 260 | deviceEvent.Context[eventContextKeyPonId] = "0" |
| 261 | deviceEvent.Context[eventContextKeyOnuSn] = "BBSM000a0001" |
| 262 | deviceEvent.Context[eventContextKeyOltSn] = "BBSIM_OLT_10" |
| 263 | |
| 264 | notificationPath := "/bbf-xpon-onu-states:onu-state-change" |
| 265 | expected := []YangItem{ |
| 266 | { |
| 267 | Path: notificationPath + "/detected-serial-number", |
| 268 | Value: "BBSM000a0001", |
| 269 | }, |
| 270 | { |
| 271 | Path: notificationPath + "/onu-state-last-change", |
| 272 | Value: timestamp.Format(time.RFC3339), |
| 273 | }, |
| 274 | { |
| 275 | Path: notificationPath + "/onu-state", |
| 276 | Value: "bbf-xpon-onu-types:onu-present", |
| 277 | }, |
| 278 | { |
| 279 | Path: notificationPath + "/detected-registration-id", |
| 280 | Value: testDeviceId, |
| 281 | }, |
| 282 | } |
| 283 | |
| 284 | notificationItems, channelTerminationItems, err := TranslateOnuActivatedEvent(eventHeader, deviceEvent) |
| 285 | assert.Nil(t, err, "Translation error") |
| 286 | |
| 287 | assert.NotEmpty(t, channelTerminationItems, "No channel termination items") |
| 288 | |
| 289 | assert.NotEmpty(t, notificationItems, "No notification items") |
| 290 | |
| 291 | _, ok := getItemWithPath(notificationItems, notificationPath+"/channel-termination-ref") |
| 292 | assert.True(t, ok, "No channel termination reference in notification") |
| 293 | |
| 294 | for _, e := range expected { |
| 295 | val, ok := getItemWithPath(notificationItems, e.Path) |
| 296 | assert.True(t, ok, e.Path+" missing for notification") |
| 297 | assert.Equal(t, e.Value, val, "Wrong value for "+e.Path) |
Elia Battiston | e1cecb2 | 2022-03-21 10:05:25 +0100 | [diff] [blame] | 298 | } |
| 299 | } |