Extensibility support (deserialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / PortStatusMessageFactory.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.impl.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.PortConfig;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
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.PortState;
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
26  * @author michal.polkorab
27  * @author timotej.kubas
28  */
29 public class PortStatusMessageFactory implements OFDeserializer<PortStatusMessage> {
30
31     private static final byte PADDING_IN_PORT_STATUS_HEADER = 7;
32     private static final byte PADDING_IN_OFP_PORT_HEADER_1 = 4;
33     private static final byte PADDING_IN_OFP_PORT_HEADER_2 = 2;
34
35     @Override
36     public PortStatusMessage deserialize(ByteBuf rawMessage) {
37         PortStatusMessageBuilder builder = new PortStatusMessageBuilder(); 
38         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
39         builder.setXid(rawMessage.readUnsignedInt());
40         builder.setReason(PortReason.forValue(rawMessage.readUnsignedByte()));
41         rawMessage.skipBytes(PADDING_IN_PORT_STATUS_HEADER);
42         builder.setPortNo(rawMessage.readUnsignedInt());
43         rawMessage.skipBytes(PADDING_IN_OFP_PORT_HEADER_1);
44         byte[] hwAddress = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
45         rawMessage.readBytes(hwAddress);
46         builder.setHwAddr(new MacAddress(ByteBufUtils.macAddressToString(hwAddress)));
47         rawMessage.skipBytes(PADDING_IN_OFP_PORT_HEADER_2);
48         builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
49         builder.setConfig(createPortConfig(rawMessage.readUnsignedInt()));
50         builder.setState(createPortState(rawMessage.readUnsignedInt()));
51         builder.setCurrentFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
52         builder.setAdvertisedFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
53         builder.setSupportedFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
54         builder.setPeerFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
55         builder.setCurrSpeed(rawMessage.readUnsignedInt());
56         builder.setMaxSpeed(rawMessage.readUnsignedInt());
57         return builder.build();
58     }
59
60     private static PortFeatures createPortFeatures(long input){
61         final Boolean _10mbHd = ((input) & (1<<0)) != 0;
62         final Boolean _10mbFd = ((input) & (1<<1)) != 0;
63         final Boolean _100mbHd = ((input) & (1<<2)) != 0;
64         final Boolean _100mbFd = ((input) & (1<<3)) != 0;
65         final Boolean _1gbHd = ((input) & (1<<4)) != 0;
66         final Boolean _1gbFd = ((input) & (1<<5)) != 0;
67         final Boolean _10gbFd = ((input) & (1<<6)) != 0;
68         final Boolean _40gbFd = ((input) & (1<<7)) != 0;
69         final Boolean _100gbFd = ((input) & (1<<8)) != 0;
70         final Boolean _1tbFd = ((input) & (1<<9)) != 0;
71         final Boolean _other = ((input) & (1<<10)) != 0;
72         final Boolean _copper = ((input) & (1<<11)) != 0;
73         final Boolean _fiber = ((input) & (1<<12)) != 0;
74         final Boolean _autoneg = ((input) & (1<<13)) != 0;
75         final Boolean _pause = ((input) & (1<<14)) != 0;
76         final Boolean _pauseAsym = ((input) & (1<<15)) != 0;
77         return new PortFeatures(_100gbFd, _100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd, _1gbFd,
78                 _1gbHd, _1tbFd, _40gbFd, _autoneg, _copper, _fiber, _other, _pause, _pauseAsym);
79     }
80     
81     private static PortState createPortState(long input){
82         final Boolean _linkDown = ((input) & (1<<0)) != 0;
83         final Boolean _blocked  = ((input) & (1<<1)) != 0;
84         final Boolean _live     = ((input) & (1<<2)) != 0;
85         return new PortState(_blocked, _linkDown, _live);
86     }
87     
88     private static PortConfig createPortConfig(long input){
89         final Boolean _portDown   = ((input) & (1<<0)) != 0;
90         final Boolean _noRecv    = ((input) & (1<<2)) != 0;
91         final Boolean _noFwd       = ((input) & (1<<5)) != 0;
92         final Boolean _noPacketIn = ((input) & (1<<6)) != 0;
93         return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown);
94     }
95 }