2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13 import junit.framework.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
22 import org.opendaylight.openflowjava.util.ByteBufUtils;
23 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
32 * @author timotej.kubas
33 * @author michal.polkorab
35 public class PortModInputMessageFactoryTest {
36 private static final byte MESSAGE_TYPE = 16;
37 private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
38 private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
39 private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
40 private static final int MESSAGE_LENGTH = 40;
41 private SerializerRegistry registry;
42 private OFSerializer<PortModInput> portModFactory;
45 * Initializes serializer registry and stores correct factory in field
48 public void startUp() {
49 registry = new SerializerRegistryImpl();
51 portModFactory = registry.getSerializer(
52 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PortModInput.class));
56 * Testing of {@link PortModInputMessageFactory} for correct translation from POJO
60 public void testPortModInput() throws Exception {
61 PortModInputBuilder builder = new PortModInputBuilder();
62 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
63 builder.setPortNo(new PortNumber(9L));
64 builder.setHwAddress(new MacAddress("08:00:27:00:B0:EB"));
65 builder.setConfig(new PortConfig(true, false, true, false));
66 builder.setMask(new PortConfig(false, true, false, true));
67 builder.setAdvertise(new PortFeatures(true, false, false, false,
68 false, false, false, true,
69 false, false, false, false,
70 false, false, false, false));
71 PortModInput message = builder.build();
73 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
74 portModFactory.serialize(message, out);
76 BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, MESSAGE_LENGTH);
77 Assert.assertEquals("Wrong PortNo", message.getPortNo().getValue().longValue(), out.readUnsignedInt());
78 out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_01);
79 byte[] address = new byte[6];
80 out.readBytes(address);
81 Assert.assertEquals("Wrong MacAddress", message.getHwAddress().getValue(),
82 new MacAddress(ByteBufUtils.macAddressToString(address)).getValue());
83 out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_02);
84 Assert.assertEquals("Wrong config", message.getConfig(), createPortConfig(out.readInt()));
85 Assert.assertEquals("Wrong mask", message.getMask(), createPortConfig(out.readInt()));
86 Assert.assertEquals("Wrong advertise", message.getAdvertise(), createPortFeatures(out.readInt()));
87 out.skipBytes(PADDING_IN_PORT_MOD_MESSAGE_03);
90 private static PortConfig createPortConfig(long input){
91 final Boolean _portDown = ((input) & (1<<0)) > 0;
92 final Boolean _noRecv = ((input) & (1<<2)) > 0;
93 final Boolean _noFwd = ((input) & (1<<5)) > 0;
94 final Boolean _noPacketIn = ((input) & (1<<6)) > 0;
95 return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown);
98 private static PortFeatures createPortFeatures(long input){
99 final Boolean _10mbHd = ((input) & (1<<0)) > 0;
100 final Boolean _10mbFd = ((input) & (1<<1)) > 0;
101 final Boolean _100mbHd = ((input) & (1<<2)) > 0;
102 final Boolean _100mbFd = ((input) & (1<<3)) > 0;
103 final Boolean _1gbHd = ((input) & (1<<4)) > 0;
104 final Boolean _1gbFd = ((input) & (1<<5)) > 0;
105 final Boolean _10gbFd = ((input) & (1<<6)) > 0;
106 final Boolean _40gbFd = ((input) & (1<<7)) > 0;
107 final Boolean _100gbFd = ((input) & (1<<8)) > 0;
108 final Boolean _1tbFd = ((input) & (1<<9)) > 0;
109 final Boolean _other = ((input) & (1<<10)) > 0;
110 final Boolean _copper = ((input) & (1<<11)) > 0;
111 final Boolean _fiber = ((input) & (1<<12)) > 0;
112 final Boolean _autoneg = ((input) & (1<<13)) > 0;
113 final Boolean _pause = ((input) & (1<<14)) > 0;
114 final Boolean _pauseAsym = ((input) & (1<<15)) > 0;
115 return new PortFeatures(_100gbFd, _100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd,
116 _1gbFd, _1gbHd, _1tbFd, _40gbFd, _autoneg, _copper, _fiber, _other, _pause, _pauseAsym);