Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package omci_test |
| 19 | |
| 20 | import ( |
| 21 | "github.com/google/gopacket" |
Andrea Campanella | e0cd823 | 2021-10-15 15:10:23 +0200 | [diff] [blame] | 22 | . "github.com/opencord/omci-lib-go/v2" |
| 23 | me "github.com/opencord/omci-lib-go/v2/generated" |
| 24 | "github.com/opencord/omci-lib-go/v2/meframe" |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 25 | "github.com/stretchr/testify/assert" |
| 26 | "strings" |
| 27 | "testing" |
| 28 | ) |
| 29 | |
| 30 | func TestCreateRequestDecode(t *testing.T) { |
| 31 | goodMessage := "000C440A010C01000400800003010000" + |
| 32 | "00000000000000000000000000000000" + |
| 33 | "000000000000000000000028" |
| 34 | data, err := stringToPacket(goodMessage) |
| 35 | assert.NoError(t, err) |
| 36 | |
| 37 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 38 | assert.NotNil(t, packet) |
| 39 | |
| 40 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 41 | assert.NotNil(t, omciLayer) |
| 42 | |
| 43 | omciMsg, ok := omciLayer.(*OMCI) |
| 44 | assert.True(t, ok) |
| 45 | assert.NotNil(t, omciMsg) |
| 46 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 47 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 48 | assert.Equal(t, LayerTypeCreateRequest, omciMsg.NextLayerType()) |
| 49 | assert.Equal(t, uint16(0xc), omciMsg.TransactionID) |
| 50 | assert.Equal(t, CreateRequestType, omciMsg.MessageType) |
| 51 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 52 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 53 | |
| 54 | msgLayer := packet.Layer(LayerTypeCreateRequest) |
| 55 | assert.NotNil(t, msgLayer) |
| 56 | |
| 57 | request, ok2 := msgLayer.(*CreateRequest) |
| 58 | assert.True(t, ok2) |
| 59 | assert.NotNil(t, request) |
| 60 | assert.Equal(t, LayerTypeCreateRequest, request.LayerType()) |
| 61 | assert.Equal(t, LayerTypeCreateRequest, request.CanDecode()) |
| 62 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 63 | assert.Equal(t, me.GemPortNetworkCtpClassID, request.EntityClass) |
| 64 | assert.Equal(t, uint16(0x100), request.EntityInstance) |
| 65 | |
| 66 | attributes := request.Attributes |
| 67 | assert.NotNil(t, attributes) |
| 68 | |
| 69 | // As this is a create request, gather up all set-by-create attributes |
| 70 | // make sure we got them all, and nothing else |
| 71 | meDefinition, omciErr := me.LoadManagedEntityDefinition(request.EntityClass) |
| 72 | assert.NotNil(t, omciErr) |
| 73 | assert.Equal(t, omciErr.StatusCode(), me.Success) |
| 74 | |
| 75 | attrDefs := meDefinition.GetAttributeDefinitions() |
| 76 | |
| 77 | sbcMask := getSbcMask(meDefinition) |
| 78 | for index := uint(1); index < uint(len(attrDefs)); index++ { |
| 79 | attrName := attrDefs[index].GetName() |
| 80 | |
| 81 | if sbcMask&uint16(1<<(uint)(16-index)) != 0 { |
| 82 | _, ok3 := attributes[attrName] |
| 83 | assert.True(t, ok3) |
| 84 | } else { |
| 85 | _, ok3 := attributes[attrName] |
| 86 | assert.False(t, ok3) |
| 87 | } |
| 88 | //fmt.Printf("Name: %v, Value: %v\n", attrName, attributes[attrName]) |
| 89 | } |
| 90 | // Test serialization back to former string |
| 91 | var options gopacket.SerializeOptions |
| 92 | options.FixLengths = true |
| 93 | |
| 94 | buffer := gopacket.NewSerializeBuffer() |
| 95 | err = gopacket.SerializeLayers(buffer, options, omciMsg, request) |
| 96 | assert.NoError(t, err) |
| 97 | |
| 98 | outgoingPacket := buffer.Bytes() |
| 99 | reconstituted := packetToString(outgoingPacket) |
| 100 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 101 | |
| 102 | // Verify string output for message |
| 103 | packetString := packet.String() |
| 104 | assert.NotZero(t, len(packetString)) |
| 105 | } |
| 106 | |
| 107 | func TestCreateRequestSerialize(t *testing.T) { |
| 108 | goodMessage := "000C440A010C0100040080000301000000000000000000000000000000000000000000000000000000000028" |
| 109 | |
| 110 | omciLayer := &OMCI{ |
| 111 | TransactionID: 0x0c, |
| 112 | MessageType: CreateRequestType, |
| 113 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 114 | // Length: 0x28, // Optional, defaults to 40 octets |
| 115 | } |
| 116 | request := &CreateRequest{ |
| 117 | MeBasePacket: MeBasePacket{ |
| 118 | EntityClass: me.GemPortNetworkCtpClassID, |
| 119 | EntityInstance: uint16(0x100), |
| 120 | }, |
| 121 | Attributes: me.AttributeValueMap{ |
| 122 | "PortId": 0x400, |
| 123 | "TContPointer": 0x8000, |
| 124 | "Direction": 3, |
| 125 | "TrafficManagementPointerForUpstream": 0x100, |
| 126 | "TrafficDescriptorProfilePointerForUpstream": 0, |
| 127 | "PriorityQueuePointerForDownStream": 0, |
| 128 | "TrafficDescriptorProfilePointerForDownstream": 0, |
| 129 | "EncryptionKeyRing": 0, |
| 130 | }, |
| 131 | } |
| 132 | // Test serialization back to former string |
| 133 | var options gopacket.SerializeOptions |
| 134 | options.FixLengths = true |
| 135 | |
| 136 | buffer := gopacket.NewSerializeBuffer() |
| 137 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 138 | assert.NoError(t, err) |
| 139 | |
| 140 | outgoingPacket := buffer.Bytes() |
| 141 | reconstituted := packetToString(outgoingPacket) |
| 142 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 143 | } |
| 144 | |
| 145 | func TestCreateRequestZeroTICSerialize(t *testing.T) { |
| 146 | omciLayer := &OMCI{ |
| 147 | TransactionID: 0x0000, |
| 148 | MessageType: CreateRequestType, |
| 149 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 150 | // Length: 0x28, // Optional, defaults to 40 octets |
| 151 | } |
| 152 | request := &CreateRequest{ |
| 153 | MeBasePacket: MeBasePacket{ |
| 154 | EntityClass: me.GemPortNetworkCtpClassID, |
| 155 | EntityInstance: uint16(0x100), |
| 156 | }, |
| 157 | Attributes: me.AttributeValueMap{ |
| 158 | "PortId": 0x400, |
| 159 | "TContPointer": 0x8000, |
| 160 | "Direction": 3, |
| 161 | "TrafficManagementPointerForUpstream": 0x100, |
| 162 | "TrafficDescriptorProfilePointerForUpstream": 0, |
| 163 | "PriorityQueuePointerForDownStream": 0, |
| 164 | "TrafficDescriptorProfilePointerForDownstream": 0, |
| 165 | "EncryptionKeyRing": 0, |
| 166 | }, |
| 167 | } |
| 168 | // Test serialization back to former string |
| 169 | var options gopacket.SerializeOptions |
| 170 | options.FixLengths = true |
| 171 | |
| 172 | buffer := gopacket.NewSerializeBuffer() |
| 173 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 174 | assert.Error(t, err) |
| 175 | } |
| 176 | |
| 177 | func TestCreateResponseDecode(t *testing.T) { |
| 178 | goodMessage := "0157240a01100001000000000000000000000000000000000000000000000000000000000000000000000028a9ccbeb9" |
| 179 | data, err := stringToPacket(goodMessage) |
| 180 | assert.NoError(t, err) |
| 181 | |
| 182 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 183 | assert.NotNil(t, packet) |
| 184 | |
| 185 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 186 | assert.NotNil(t, omciLayer) |
| 187 | |
| 188 | omciMsg, ok := omciLayer.(*OMCI) |
| 189 | assert.True(t, ok) |
| 190 | assert.NotNil(t, omciMsg) |
| 191 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 192 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 193 | assert.Equal(t, LayerTypeCreateResponse, omciMsg.NextLayerType()) |
| 194 | assert.Equal(t, uint16(0x0157), omciMsg.TransactionID) |
| 195 | assert.Equal(t, CreateResponseType, omciMsg.MessageType) |
| 196 | assert.Equal(t, BaselineIdent, omciMsg.DeviceIdentifier) |
| 197 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 198 | |
| 199 | msgLayer := packet.Layer(LayerTypeCreateResponse) |
| 200 | assert.NotNil(t, msgLayer) |
| 201 | |
| 202 | response, ok2 := msgLayer.(*CreateResponse) |
| 203 | assert.True(t, ok2) |
| 204 | assert.NotNil(t, response) |
| 205 | assert.Equal(t, LayerTypeCreateResponse, response.LayerType()) |
| 206 | assert.Equal(t, LayerTypeCreateResponse, response.CanDecode()) |
| 207 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 208 | |
| 209 | // Verify string output for message |
| 210 | packetString := packet.String() |
| 211 | assert.NotZero(t, len(packetString)) |
| 212 | } |
| 213 | |
| 214 | func TestCreateResponseSerialize(t *testing.T) { |
| 215 | goodMessage := "0157240a01100001000000000000000000000000000000000000000000000000000000000000000000000028" |
| 216 | |
| 217 | omciLayer := &OMCI{ |
| 218 | TransactionID: 0x0157, |
| 219 | MessageType: CreateResponseType, |
| 220 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 221 | // Length: 0x28, // Optional, defaults to 40 octets |
| 222 | } |
| 223 | request := &CreateResponse{ |
| 224 | MeBasePacket: MeBasePacket{ |
| 225 | EntityClass: me.GalEthernetProfileClassID, |
| 226 | EntityInstance: uint16(1), |
| 227 | }, |
| 228 | Result: me.Success, |
| 229 | AttributeExecutionMask: uint16(0), |
| 230 | } |
| 231 | // Test serialization back to former string |
| 232 | var options gopacket.SerializeOptions |
| 233 | options.FixLengths = true |
| 234 | |
| 235 | buffer := gopacket.NewSerializeBuffer() |
| 236 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 237 | assert.NoError(t, err) |
| 238 | |
| 239 | outgoingPacket := buffer.Bytes() |
| 240 | reconstituted := packetToString(outgoingPacket) |
| 241 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 242 | } |
| 243 | |
| 244 | func TestCreateResponseZeroTICSerialize(t *testing.T) { |
| 245 | omciLayer := &OMCI{ |
| 246 | TransactionID: 0x0, |
| 247 | MessageType: CreateResponseType, |
| 248 | // DeviceIdentifier: omci.BaselineIdent, // Optional, defaults to Baseline |
| 249 | // Length: 0x28, // Optional, defaults to 40 octets |
| 250 | } |
| 251 | request := &CreateResponse{ |
| 252 | MeBasePacket: MeBasePacket{ |
| 253 | EntityClass: me.GalEthernetProfileClassID, |
| 254 | EntityInstance: uint16(1), |
| 255 | }, |
| 256 | Result: me.Success, |
| 257 | AttributeExecutionMask: uint16(0), |
| 258 | } |
| 259 | // Test serialization back to former string |
| 260 | var options gopacket.SerializeOptions |
| 261 | options.FixLengths = true |
| 262 | |
| 263 | buffer := gopacket.NewSerializeBuffer() |
| 264 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 265 | assert.Error(t, err) |
| 266 | } |
| 267 | |
| 268 | func TestExtendedCreateRequestDecode(t *testing.T) { |
| 269 | goodMessage := "000C440B010C0100000E0400800003010000000000000000" |
| 270 | data, err := stringToPacket(goodMessage) |
| 271 | assert.NoError(t, err) |
| 272 | // TODO: Trailing optional SBC attributes can be omitted at the option |
| 273 | // of the transmitter |
| 274 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 275 | assert.NotNil(t, packet) |
| 276 | |
| 277 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 278 | assert.NotNil(t, omciLayer) |
| 279 | |
| 280 | omciMsg, ok := omciLayer.(*OMCI) |
| 281 | assert.True(t, ok) |
| 282 | assert.NotNil(t, omciMsg) |
| 283 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 284 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 285 | assert.Equal(t, LayerTypeCreateRequest, omciMsg.NextLayerType()) |
| 286 | assert.Equal(t, uint16(0xc), omciMsg.TransactionID) |
| 287 | assert.Equal(t, CreateRequestType, omciMsg.MessageType) |
| 288 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 289 | assert.Equal(t, uint16(14), omciMsg.Length) |
| 290 | |
| 291 | msgLayer := packet.Layer(LayerTypeCreateRequest) |
| 292 | assert.NotNil(t, msgLayer) |
| 293 | |
| 294 | request, ok2 := msgLayer.(*CreateRequest) |
| 295 | assert.True(t, ok2) |
| 296 | assert.NotNil(t, request) |
| 297 | assert.Equal(t, LayerTypeCreateRequest, request.LayerType()) |
| 298 | assert.Equal(t, LayerTypeCreateRequest, request.CanDecode()) |
| 299 | assert.Equal(t, gopacket.LayerTypePayload, request.NextLayerType()) |
| 300 | assert.Equal(t, me.GemPortNetworkCtpClassID, request.EntityClass) |
| 301 | assert.Equal(t, uint16(0x100), request.EntityInstance) |
| 302 | |
| 303 | attributes := request.Attributes |
| 304 | assert.NotNil(t, attributes) |
| 305 | |
| 306 | // As this is a create request, gather up all set-by-create attributes |
| 307 | // make sure we got them all, and nothing else |
| 308 | meDefinition, omciErr := me.LoadManagedEntityDefinition(request.EntityClass) |
| 309 | assert.NotNil(t, omciErr) |
| 310 | assert.Equal(t, omciErr.StatusCode(), me.Success) |
| 311 | |
| 312 | attrDefs := meDefinition.GetAttributeDefinitions() |
| 313 | |
| 314 | sbcMask := getSbcMask(meDefinition) |
| 315 | for index := uint(1); index < uint(len(attrDefs)); index++ { |
| 316 | attrName := attrDefs[index].GetName() |
| 317 | |
| 318 | if sbcMask&uint16(1<<(uint)(16-index)) != 0 { |
| 319 | _, ok3 := attributes[attrName] |
| 320 | assert.True(t, ok3) |
| 321 | } else { |
| 322 | _, ok3 := attributes[attrName] |
| 323 | assert.False(t, ok3) |
| 324 | } |
| 325 | //fmt.Printf("Name: %v, Value: %v\n", attrName, attributes[attrName]) |
| 326 | } |
| 327 | // Test serialization back to former string |
| 328 | var options gopacket.SerializeOptions |
| 329 | options.FixLengths = true |
| 330 | |
| 331 | buffer := gopacket.NewSerializeBuffer() |
| 332 | err = gopacket.SerializeLayers(buffer, options, omciMsg, request) |
| 333 | assert.NoError(t, err) |
| 334 | |
| 335 | outgoingPacket := buffer.Bytes() |
| 336 | reconstituted := packetToString(outgoingPacket) |
| 337 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 338 | |
| 339 | // Verify string output for message |
| 340 | packetString := packet.String() |
| 341 | assert.NotZero(t, len(packetString)) |
| 342 | } |
| 343 | |
| 344 | func TestExtendedCreateRequestSerialize(t *testing.T) { |
| 345 | goodMessage := "000C440B010C0100000E0400800003010000000000000000" |
| 346 | omciLayer := &OMCI{ |
| 347 | TransactionID: 0x0c, |
| 348 | MessageType: CreateRequestType, |
| 349 | DeviceIdentifier: ExtendedIdent, |
| 350 | } |
| 351 | request := &CreateRequest{ |
| 352 | MeBasePacket: MeBasePacket{ |
| 353 | EntityClass: me.GemPortNetworkCtpClassID, |
| 354 | EntityInstance: uint16(0x100), |
| 355 | Extended: true, |
| 356 | }, |
| 357 | Attributes: me.AttributeValueMap{ |
| 358 | "PortId": 0x0400, |
| 359 | "TContPointer": 0x8000, |
| 360 | "Direction": 3, |
| 361 | "TrafficManagementPointerForUpstream": 0x100, |
| 362 | "TrafficDescriptorProfilePointerForUpstream": 0, |
| 363 | "PriorityQueuePointerForDownStream": 0, |
| 364 | "TrafficDescriptorProfilePointerForDownstream": 0, |
| 365 | "EncryptionKeyRing": 0, |
| 366 | }, |
| 367 | } |
| 368 | // Test serialization back to former string |
| 369 | var options gopacket.SerializeOptions |
| 370 | options.FixLengths = true |
| 371 | |
| 372 | buffer := gopacket.NewSerializeBuffer() |
| 373 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 374 | assert.NoError(t, err) |
| 375 | |
| 376 | outgoingPacket := buffer.Bytes() |
| 377 | reconstituted := packetToString(outgoingPacket) |
| 378 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 379 | } |
| 380 | |
| 381 | func TestExtendedCreateResponseDecode(t *testing.T) { |
| 382 | goodMessage := "0157240b01100001000100" |
| 383 | data, err := stringToPacket(goodMessage) |
| 384 | assert.NoError(t, err) |
| 385 | // TODO: Also test sending reason code 3 |
| 386 | |
| 387 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 388 | assert.NotNil(t, packet) |
| 389 | |
| 390 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 391 | assert.NotNil(t, omciLayer) |
| 392 | |
| 393 | omciMsg, ok := omciLayer.(*OMCI) |
| 394 | assert.True(t, ok) |
| 395 | assert.NotNil(t, omciMsg) |
| 396 | assert.Equal(t, LayerTypeOMCI, omciMsg.LayerType()) |
| 397 | assert.Equal(t, LayerTypeOMCI, omciMsg.CanDecode()) |
| 398 | assert.Equal(t, LayerTypeCreateResponse, omciMsg.NextLayerType()) |
| 399 | assert.Equal(t, uint16(0x0157), omciMsg.TransactionID) |
| 400 | assert.Equal(t, CreateResponseType, omciMsg.MessageType) |
| 401 | assert.Equal(t, ExtendedIdent, omciMsg.DeviceIdentifier) |
| 402 | assert.Equal(t, uint16(1), omciMsg.Length) |
| 403 | |
| 404 | msgLayer := packet.Layer(LayerTypeCreateResponse) |
| 405 | assert.NotNil(t, msgLayer) |
| 406 | |
| 407 | response, ok2 := msgLayer.(*CreateResponse) |
| 408 | assert.True(t, ok2) |
| 409 | assert.NotNil(t, response) |
| 410 | assert.Equal(t, LayerTypeCreateResponse, response.LayerType()) |
| 411 | assert.Equal(t, LayerTypeCreateResponse, response.CanDecode()) |
| 412 | assert.Equal(t, gopacket.LayerTypePayload, response.NextLayerType()) |
| 413 | assert.Equal(t, me.Success, response.Result) |
| 414 | |
| 415 | // Verify string output for message |
| 416 | packetString := packet.String() |
| 417 | assert.NotZero(t, len(packetString)) |
| 418 | } |
| 419 | |
| 420 | func TestExtendedCreateResponseSerialize(t *testing.T) { |
| 421 | goodMessage := "0157240b01100001000100" |
| 422 | |
| 423 | omciLayer := &OMCI{ |
| 424 | TransactionID: 0x0157, |
| 425 | MessageType: CreateResponseType, |
| 426 | DeviceIdentifier: ExtendedIdent, |
| 427 | } |
| 428 | request := &CreateResponse{ |
| 429 | MeBasePacket: MeBasePacket{ |
| 430 | EntityClass: me.GalEthernetProfileClassID, |
| 431 | EntityInstance: uint16(1), |
| 432 | Extended: true, |
| 433 | }, |
| 434 | Result: me.Success, |
| 435 | AttributeExecutionMask: uint16(0), // Optional since success |
| 436 | } |
| 437 | // Test serialization back to former string |
| 438 | var options gopacket.SerializeOptions |
| 439 | options.FixLengths = true |
| 440 | |
| 441 | buffer := gopacket.NewSerializeBuffer() |
| 442 | err := gopacket.SerializeLayers(buffer, options, omciLayer, request) |
| 443 | assert.NoError(t, err) |
| 444 | |
| 445 | outgoingPacket := buffer.Bytes() |
| 446 | reconstituted := packetToString(outgoingPacket) |
| 447 | assert.Equal(t, strings.ToLower(goodMessage), reconstituted) |
| 448 | } |
| 449 | |
| 450 | func TestCreate8021pMapperService_profile(t *testing.T) { |
| 451 | create8021pMapperServiceProfile := "0007440A00828000ffffffffffffffff" + |
| 452 | "ffffffffffffffffffff000000000000" + |
| 453 | "000000000000000000000028" |
| 454 | |
| 455 | data, err := stringToPacket(create8021pMapperServiceProfile) |
| 456 | assert.NoError(t, err) |
| 457 | |
| 458 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 459 | assert.NotNil(t, packet) |
| 460 | |
| 461 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 462 | assert.NotNil(t, packet) |
| 463 | |
| 464 | omciMsg, ok := omciLayer.(*OMCI) |
| 465 | assert.True(t, ok) |
| 466 | assert.Equal(t, uint16(7), omciMsg.TransactionID) |
| 467 | assert.Equal(t, CreateRequestType, omciMsg.MessageType) |
| 468 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 469 | |
| 470 | msgLayer := packet.Layer(LayerTypeCreateRequest) |
| 471 | assert.NotNil(t, msgLayer) |
| 472 | |
| 473 | createRequest, ok2 := msgLayer.(*CreateRequest) |
| 474 | assert.True(t, ok2) |
| 475 | assert.Equal(t, me.Ieee8021PMapperServiceProfileClassID, createRequest.EntityClass) |
| 476 | assert.Equal(t, uint16(0x8000), createRequest.EntityInstance) |
| 477 | |
| 478 | attributes := createRequest.Attributes |
| 479 | assert.NotNil(t, attributes) |
| 480 | assert.Equal(t, 13, len(attributes)) |
| 481 | |
| 482 | // As this is a create request, gather up all set-by-create attributes |
| 483 | // make sure we got them all, and nothing else |
| 484 | meDefinition, omciErr := me.LoadManagedEntityDefinition(createRequest.EntityClass) |
| 485 | assert.NotNil(t, omciErr) |
| 486 | assert.Equal(t, me.Success, omciErr.StatusCode()) |
| 487 | |
| 488 | attrDefs := meDefinition.GetAttributeDefinitions() |
| 489 | |
| 490 | for index := uint(1); index <= uint(9); index++ { |
| 491 | attrName := attrDefs[index].GetName() |
| 492 | value, ok := attributes[attrName] |
| 493 | assert.True(t, ok) |
| 494 | |
| 495 | value16, ok3 := value.(uint16) |
| 496 | assert.True(t, ok3) |
| 497 | assert.Equal(t, uint16(0xffff), value16) |
| 498 | } |
| 499 | |
| 500 | sbcMask := getSbcMask(meDefinition) |
| 501 | for index := uint(1); index < uint(len(attrDefs)); index++ { |
| 502 | attrName := attrDefs[index].GetName() |
| 503 | |
| 504 | if sbcMask&uint16(1<<(uint)(16-index)) != 0 { |
| 505 | _, ok3 := attributes[attrName] |
| 506 | assert.True(t, ok3) |
| 507 | } else { |
| 508 | _, ok3 := attributes[attrName] |
| 509 | assert.False(t, ok3) |
| 510 | } |
| 511 | } |
| 512 | // Test serialization back to former string |
| 513 | var options gopacket.SerializeOptions |
| 514 | options.FixLengths = true |
| 515 | |
| 516 | buffer := gopacket.NewSerializeBuffer() |
| 517 | err = gopacket.SerializeLayers(buffer, options, omciMsg, createRequest) |
| 518 | assert.NoError(t, err) |
| 519 | |
| 520 | outgoingPacket := buffer.Bytes() |
| 521 | reconstituted := packetToString(outgoingPacket) |
| 522 | assert.Equal(t, strings.ToLower(create8021pMapperServiceProfile), reconstituted) |
| 523 | } |
| 524 | |
| 525 | func TestCreateGalEthernetProfile(t *testing.T) { |
| 526 | createGalEthernetProfile := "0002440A011000010030000000000000" + |
| 527 | "00000000000000000000000000000000" + |
| 528 | "000000000000000000000028" |
| 529 | |
| 530 | data, err := stringToPacket(createGalEthernetProfile) |
| 531 | assert.NoError(t, err) |
| 532 | |
| 533 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 534 | assert.NotNil(t, packet) |
| 535 | |
| 536 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 537 | assert.NotNil(t, packet) |
| 538 | |
| 539 | omciMsg, ok := omciLayer.(*OMCI) |
| 540 | assert.True(t, ok) |
| 541 | assert.Equal(t, uint16(2), omciMsg.TransactionID) |
| 542 | assert.Equal(t, CreateRequestType, omciMsg.MessageType) |
| 543 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 544 | |
| 545 | msgLayer := packet.Layer(LayerTypeCreateRequest) |
| 546 | assert.NotNil(t, msgLayer) |
| 547 | |
| 548 | omciMsg2, ok2 := msgLayer.(*CreateRequest) |
| 549 | assert.True(t, ok2) |
| 550 | assert.Equal(t, me.GalEthernetProfileClassID, omciMsg2.EntityClass) |
| 551 | assert.Equal(t, uint16(1), omciMsg2.EntityInstance) |
| 552 | |
| 553 | // Test serialization back to former string |
| 554 | var options gopacket.SerializeOptions |
| 555 | options.FixLengths = true |
| 556 | |
| 557 | buffer := gopacket.NewSerializeBuffer() |
| 558 | err = gopacket.SerializeLayers(buffer, options, omciMsg, omciMsg2) |
| 559 | assert.NoError(t, err) |
| 560 | |
| 561 | outgoingPacket := buffer.Bytes() |
| 562 | reconstituted := packetToString(outgoingPacket) |
| 563 | assert.Equal(t, strings.ToLower(createGalEthernetProfile), reconstituted) |
| 564 | } |
| 565 | |
| 566 | func TestCreate_macBridgeService_profile(t *testing.T) { |
| 567 | var createMacBridgeServiceProfile = "000B440A002D02010001008000140002" + |
| 568 | "000f0001000000000000000000000000" + |
| 569 | "000000000000000000000028" |
| 570 | |
| 571 | data, err := stringToPacket(createMacBridgeServiceProfile) |
| 572 | assert.NoError(t, err) |
| 573 | |
| 574 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 575 | assert.NotNil(t, packet) |
| 576 | |
| 577 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 578 | assert.NotNil(t, packet) |
| 579 | |
| 580 | omciMsg, ok := omciLayer.(*OMCI) |
| 581 | assert.True(t, ok) |
| 582 | assert.Equal(t, uint16(0xb), omciMsg.TransactionID) |
| 583 | assert.Equal(t, CreateRequestType, omciMsg.MessageType) |
| 584 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 585 | |
| 586 | msgLayer := packet.Layer(LayerTypeCreateRequest) |
| 587 | assert.NotNil(t, msgLayer) |
| 588 | |
| 589 | createRequest, ok2 := msgLayer.(*CreateRequest) |
| 590 | assert.True(t, ok2) |
| 591 | assert.Equal(t, me.MacBridgeServiceProfileClassID, createRequest.EntityClass) |
| 592 | assert.Equal(t, uint16(0x201), createRequest.EntityInstance) |
| 593 | |
| 594 | attributes := createRequest.Attributes |
| 595 | assert.NotNil(t, attributes) |
| 596 | |
| 597 | // As this is a create request, gather up all set-by-create attributes |
| 598 | // make sure we got them all, and nothing else |
| 599 | meDefinition, omciErr := me.LoadManagedEntityDefinition(createRequest.EntityClass) |
| 600 | assert.NotNil(t, omciErr) |
| 601 | assert.Equal(t, me.Success, omciErr.StatusCode()) |
| 602 | |
| 603 | attrDefs := meDefinition.GetAttributeDefinitions() |
| 604 | |
| 605 | sbcMask := getSbcMask(meDefinition) |
| 606 | for index := uint(1); index < uint(len(attrDefs)); index++ { |
| 607 | attrName := attrDefs[index].GetName() |
| 608 | |
| 609 | if sbcMask&uint16(1<<(uint)(16-index)) != 0 { |
| 610 | _, ok3 := attributes[attrName] |
| 611 | assert.True(t, ok3) |
| 612 | } else { |
| 613 | _, ok3 := attributes[attrName] |
| 614 | assert.False(t, ok3) |
| 615 | } |
| 616 | } |
| 617 | // Test serialization back to former string |
| 618 | var options gopacket.SerializeOptions |
| 619 | options.FixLengths = true |
| 620 | |
| 621 | buffer := gopacket.NewSerializeBuffer() |
| 622 | err = gopacket.SerializeLayers(buffer, options, omciMsg, createRequest) |
| 623 | assert.NoError(t, err) |
| 624 | |
| 625 | outgoingPacket := buffer.Bytes() |
| 626 | reconstituted := packetToString(outgoingPacket) |
| 627 | assert.Equal(t, strings.ToLower(createMacBridgeServiceProfile), reconstituted) |
| 628 | } |
| 629 | |
| 630 | func TestCreateGemPortNetworkCtp(t *testing.T) { |
| 631 | createGemPortNetworkCtp := "000C440A010C01000400800003010000" + |
| 632 | "00000000000000000000000000000000" + |
| 633 | "000000000000000000000028" |
| 634 | |
| 635 | data, err := stringToPacket(createGemPortNetworkCtp) |
| 636 | assert.NoError(t, err) |
| 637 | |
| 638 | packet := gopacket.NewPacket(data, LayerTypeOMCI, gopacket.NoCopy) |
| 639 | assert.NotNil(t, packet) |
| 640 | |
| 641 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 642 | assert.NotNil(t, packet) |
| 643 | |
| 644 | omciMsg, ok := omciLayer.(*OMCI) |
| 645 | assert.True(t, ok) |
| 646 | assert.Equal(t, uint16(0xc), omciMsg.TransactionID) |
| 647 | assert.Equal(t, CreateRequestType, omciMsg.MessageType) |
| 648 | assert.Equal(t, uint16(40), omciMsg.Length) |
| 649 | |
| 650 | msgLayer := packet.Layer(LayerTypeCreateRequest) |
| 651 | assert.NotNil(t, msgLayer) |
| 652 | |
| 653 | createRequest, ok2 := msgLayer.(*CreateRequest) |
| 654 | assert.True(t, ok2) |
| 655 | assert.Equal(t, me.GemPortNetworkCtpClassID, createRequest.EntityClass) |
| 656 | assert.Equal(t, uint16(0x100), createRequest.EntityInstance) |
| 657 | |
| 658 | attributes := createRequest.Attributes |
| 659 | assert.NotNil(t, attributes) |
| 660 | |
| 661 | // As this is a create request, gather up all set-by-create attributes |
| 662 | // make sure we got them all, and nothing else |
| 663 | meDefinition, omciErr := me.LoadManagedEntityDefinition(createRequest.EntityClass) |
| 664 | assert.NotNil(t, omciErr) |
| 665 | assert.Equal(t, me.Success, omciErr.StatusCode()) |
| 666 | |
| 667 | attrDefs := meDefinition.GetAttributeDefinitions() |
| 668 | |
| 669 | sbcMask := getSbcMask(meDefinition) |
| 670 | for index := uint(1); index < uint(len(attrDefs)); index++ { |
| 671 | attrName := attrDefs[index].GetName() |
| 672 | |
| 673 | if sbcMask&uint16(1<<(uint)(16-index)) != 0 { |
| 674 | _, ok3 := attributes[attrName] |
| 675 | assert.True(t, ok3) |
| 676 | } else { |
| 677 | _, ok3 := attributes[attrName] |
| 678 | assert.False(t, ok3) |
| 679 | } |
| 680 | } |
| 681 | // Test serialization back to former string |
| 682 | var options gopacket.SerializeOptions |
| 683 | options.FixLengths = true |
| 684 | |
| 685 | buffer := gopacket.NewSerializeBuffer() |
| 686 | err = gopacket.SerializeLayers(buffer, options, omciMsg, createRequest) |
| 687 | assert.NoError(t, err) |
| 688 | |
| 689 | outgoingPacket := buffer.Bytes() |
| 690 | reconstituted := packetToString(outgoingPacket) |
| 691 | assert.Equal(t, strings.ToLower(createGemPortNetworkCtp), reconstituted) |
| 692 | } |
| 693 | |
| 694 | // TestCreateMulticastOperationsProfileMe tests a hand-coded managed entity in the generated |
| 695 | // subdirectory |
| 696 | func TestCreateMulticastOperationsProfileMe(t *testing.T) { |
| 697 | // Test various create request for this ME |
| 698 | meParams := me.ParamData{ |
| 699 | EntityID: uint16(0x501), |
| 700 | Attributes: me.AttributeValueMap{ |
| 701 | "IgmpVersion": 2, |
| 702 | "IgmpFunction": 0, |
| 703 | "ImmediateLeave": 0, |
| 704 | "UpstreamIgmpTci": 0, |
| 705 | "Robustness": 2, |
| 706 | "QuerierIpAddress": 0, |
| 707 | "QueryInterval": 125, |
| 708 | "QueryMaxResponseTime": 100, |
| 709 | "LastMemberQueryInterval": 10, |
| 710 | "UnauthorizedJoinRequestBehaviour": 0, |
| 711 | "UpstreamIgmpRate": 0, |
| 712 | "UpstreamIgmpTagControl": 0, |
| 713 | "DownstreamIgmpAndMulticastTci": []byte{0, 0, 0}, |
| 714 | }, |
| 715 | } |
| 716 | meInstance, newErr := me.NewMulticastOperationsProfile(meParams) |
| 717 | assert.NotNil(t, meInstance) |
| 718 | assert.Equal(t, newErr.StatusCode(), me.Success) |
| 719 | |
| 720 | tid := uint16(123) |
| 721 | frame, omciErr := meframe.GenFrame(meInstance, CreateRequestType, meframe.TransactionID(tid)) |
| 722 | assert.NotNil(t, frame) |
| 723 | assert.NotZero(t, len(frame)) |
| 724 | assert.Nil(t, omciErr) |
| 725 | |
| 726 | /////////////////////////////////////////////////////////////////// |
| 727 | // Now decode |
| 728 | packet := gopacket.NewPacket(frame, LayerTypeOMCI, gopacket.NoCopy) |
| 729 | assert.NotNil(t, packet) |
| 730 | |
| 731 | omciLayer := packet.Layer(LayerTypeOMCI) |
| 732 | assert.NotNil(t, omciLayer) |
| 733 | |
| 734 | omciObj, omciOk := omciLayer.(*OMCI) |
| 735 | assert.NotNil(t, omciObj) |
| 736 | assert.True(t, omciOk) |
| 737 | assert.Equal(t, tid, omciObj.TransactionID) |
| 738 | assert.Equal(t, omciObj.MessageType, CreateRequestType) |
| 739 | assert.Equal(t, omciObj.DeviceIdentifier, BaselineIdent) |
| 740 | |
| 741 | msgLayer := packet.Layer(LayerTypeCreateRequest) |
| 742 | assert.NotNil(t, msgLayer) |
| 743 | |
| 744 | msgObj, msgOk := msgLayer.(*CreateRequest) |
| 745 | assert.NotNil(t, msgObj) |
| 746 | assert.True(t, msgOk) |
| 747 | |
| 748 | assert.Equal(t, msgObj.EntityClass, meInstance.GetClassID()) |
| 749 | assert.Equal(t, msgObj.EntityInstance, meInstance.GetEntityID()) |
| 750 | } |