Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10PortStatusMessageFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
14 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessageBuilder;
23
24 /**
25  * Translates PortStatus messages (OpenFlow v1.0)
26  * @author michal.polkorab
27  */
28 public class OF10PortStatusMessageFactory implements OFDeserializer<PortStatusMessage> {
29
30     private static final byte PADDING_IN_PORT_STATUS_HEADER = 7;
31
32     @Override
33     public PortStatusMessage deserialize(ByteBuf rawMessage) {
34         PortStatusMessageBuilder builder = new PortStatusMessageBuilder(); 
35         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
36         builder.setXid(rawMessage.readUnsignedInt());
37         builder.setReason(PortReason.forValue(rawMessage.readUnsignedByte()));
38         rawMessage.skipBytes(PADDING_IN_PORT_STATUS_HEADER);
39         deserializePort(rawMessage, builder);
40         return builder.build();
41     }
42
43     private static PortStateV10 createPortState(long input){
44         final Boolean _linkDown = ((input) & (1<<0)) != 0;
45         final Boolean _blocked = ((input) & (1<<1)) != 0;
46         final Boolean _live = ((input) & (1<<2)) != 0;
47         final Boolean _stpListen = ((input) & (0<<8)) != 0;
48         final Boolean _stpLearn = ((input) & (1<<8)) != 0;
49         final Boolean _stpForward = ((input) & (1<<9)) != 0; // equals 2 << 8
50         final Boolean _stpBlock = (((input) & (1<<9)) != 0) && (((input) & (1<<8)) != 0); // equals 3 << 8
51         final Boolean _stpMask = ((input) & (1<<10)) != 0; // equals 4 << 8
52         return new PortStateV10(_blocked, _linkDown, _live, _stpBlock, _stpForward, _stpLearn, _stpListen, _stpMask);
53     }
54     
55     private static PortConfigV10 createPortConfig(long input){
56         final Boolean _portDown = ((input) & (1<<0)) != 0;
57         final Boolean _noStp = ((input) & (1<<1)) != 0;
58         final Boolean _noRecv = ((input) & (1<<2)) != 0;
59         final Boolean _noRecvStp = ((input) & (1<<3)) != 0;
60         final Boolean _noFlood = ((input) & (1<<4)) != 0;
61         final Boolean _noFwd  = ((input) & (1<<5)) != 0;
62         final Boolean _noPacketIn = ((input) & (1<<6)) != 0;
63         return new PortConfigV10(_noFlood, _noFwd, _noPacketIn, _noRecv, _noRecvStp, _noStp, _portDown);
64     }
65     
66     private static PortFeaturesV10 createPortFeatures(long input){
67         final Boolean _10mbHd = ((input) & (1<<0)) != 0;
68         final Boolean _10mbFd = ((input) & (1<<1)) != 0;
69         final Boolean _100mbHd = ((input) & (1<<2)) != 0;
70         final Boolean _100mbFd = ((input) & (1<<3)) != 0;
71         final Boolean _1gbHd = ((input) & (1<<4)) != 0;
72         final Boolean _1gbFd = ((input) & (1<<5)) != 0;
73         final Boolean _10gbFd = ((input) & (1<<6)) != 0;
74         final Boolean _copper = ((input) & (1<<7)) != 0;
75         final Boolean _fiber = ((input) & (1<<8)) != 0;
76         final Boolean _autoneg = ((input) & (1<<9)) != 0;
77         final Boolean _pause = ((input) & (1<<10)) != 0;
78         final Boolean _pauseAsym = ((input) & (1<<11)) != 0;
79         return new PortFeaturesV10(_100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd,
80                 _1gbFd, _1gbHd, _autoneg, _copper, _fiber, _pause, _pauseAsym);
81     }
82     
83     private static void deserializePort(ByteBuf rawMessage, PortStatusMessageBuilder builder) {
84         builder.setPortNo((long) rawMessage.readUnsignedShort());
85         byte[] address = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
86         rawMessage.readBytes(address);
87         builder.setHwAddr(new MacAddress(ByteBufUtils.macAddressToString(address)));
88         builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
89         builder.setConfigV10(createPortConfig(rawMessage.readUnsignedInt()));
90         builder.setStateV10(createPortState(rawMessage.readUnsignedInt()));
91         builder.setCurrentFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
92         builder.setAdvertisedFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
93         builder.setSupportedFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
94         builder.setPeerFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
95     }
96 }