Extend openflow-protocol-impl serialization
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10PortModInputMessageFactory.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
12 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
13 import org.opendaylight.openflowjava.util.ByteBufUtils;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
20
21 /**
22  * @author giuseppex.petralia@intel.com
23  *
24  */
25 public class OF10PortModInputMessageFactory implements OFDeserializer<PortModInput> {
26
27     @Override
28     public PortModInput deserialize(ByteBuf rawMessage) {
29         PortModInputBuilder builder = new PortModInputBuilder();
30         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
31         builder.setXid(rawMessage.readUnsignedInt());
32         builder.setPortNo(new PortNumber((long) rawMessage.readUnsignedShort()));
33         byte[] hwAddress = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
34         rawMessage.readBytes(hwAddress);
35         builder.setHwAddress(new MacAddress(ByteBufUtils.macAddressToString(hwAddress)));
36         builder.setConfigV10(createPortConfig(rawMessage.readUnsignedInt()));
37         builder.setMaskV10(createPortConfig(rawMessage.readUnsignedInt()));
38         builder.setAdvertiseV10(createPortFeatures(rawMessage.readUnsignedInt()));
39         return builder.build();
40     }
41
42     private static PortConfigV10 createPortConfig(long input) {
43         final Boolean _portDown = ((input) & (1 << 0)) > 0;
44         final Boolean _noStp = ((input) & (1 << 1)) > 0;
45         final Boolean _noRecv = ((input) & (1 << 2)) > 0;
46         final Boolean _noRecvStp = ((input) & (1 << 3)) > 0;
47         final Boolean _noFlood = ((input) & (1 << 4)) > 0;
48         final Boolean _noFwd = ((input) & (1 << 5)) > 0;
49         final Boolean _noPacketIn = ((input) & (1 << 6)) > 0;
50         return new PortConfigV10(_noFlood, _noFwd, _noPacketIn, _noRecv, _noRecvStp, _noStp, _portDown);
51     }
52
53     private static PortFeaturesV10 createPortFeatures(long input) {
54         final Boolean _10mbHd = ((input) & (1 << 0)) > 0;
55         final Boolean _10mbFd = ((input) & (1 << 1)) > 0;
56         final Boolean _100mbHd = ((input) & (1 << 2)) > 0;
57         final Boolean _100mbFd = ((input) & (1 << 3)) > 0;
58         final Boolean _1gbHd = ((input) & (1 << 4)) > 0;
59         final Boolean _1gbFd = ((input) & (1 << 5)) > 0;
60         final Boolean _10gbFd = ((input) & (1 << 6)) > 0;
61         final Boolean _copper = ((input) & (1 << 7)) > 0;
62         final Boolean _fiber = ((input) & (1 << 8)) > 0;
63         final Boolean _autoneg = ((input) & (1 << 9)) > 0;
64         final Boolean _pause = ((input) & (1 << 10)) > 0;
65         final Boolean _pauseAsym = ((input) & (1 << 11)) > 0;
66         return new PortFeaturesV10(_100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd, _1gbFd, _1gbHd, _autoneg, _copper,
67                 _fiber, _pause, _pauseAsym);
68     }
69
70 }