5bd9cb32b4670809468e087a2150edc344e5d248
[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.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.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 pf10mbHd = ((input) & (1<<0)) != 0;
62         final Boolean pf10mbFd = ((input) & (1<<1)) != 0;
63         final Boolean pf100mbHd = ((input) & (1<<2)) != 0;
64         final Boolean pf100mbFd = ((input) & (1<<3)) != 0;
65         final Boolean pf1gbHd = ((input) & (1<<4)) != 0;
66         final Boolean pf1gbFd = ((input) & (1<<5)) != 0;
67         final Boolean pf10gbFd = ((input) & (1<<6)) != 0;
68         final Boolean pf40gbFd = ((input) & (1<<7)) != 0;
69         final Boolean pf100gbFd = ((input) & (1<<8)) != 0;
70         final Boolean pf1tbFd = ((input) & (1<<9)) != 0;
71         final Boolean pfOther = ((input) & (1<<10)) != 0;
72         final Boolean pfCopper = ((input) & (1<<11)) != 0;
73         final Boolean pfFiber = ((input) & (1<<12)) != 0;
74         final Boolean pfAutoneg = ((input) & (1<<13)) != 0;
75         final Boolean pfPause = ((input) & (1<<14)) != 0;
76         final Boolean pfPauseAsym = ((input) & (1<<15)) != 0;
77         return new PortFeatures(pf100gbFd, pf100mbFd, pf100mbHd, pf10gbFd, pf10mbFd, pf10mbHd, pf1gbFd,
78                 pf1gbHd, pf1tbFd, pf40gbFd, pfAutoneg, pfCopper, pfFiber, pfOther, pfPause, pfPauseAsym);
79     }
80     
81     private static PortState createPortState(long input){
82         final Boolean psLinkDown = ((input) & (1<<0)) != 0;
83         final Boolean psBblocked  = ((input) & (1<<1)) != 0;
84         final Boolean psLive     = ((input) & (1<<2)) != 0;
85         return new PortState(psBblocked, psLinkDown, psLive);
86     }
87     
88     private static PortConfig createPortConfig(long input){
89         final Boolean pcPortDown   = ((input) & (1<<0)) != 0;
90         final Boolean pcNoRecv    = ((input) & (1<<2)) != 0;
91         final Boolean pcNoFwd       = ((input) & (1<<5)) != 0;
92         final Boolean pcNoPacketIn = ((input) & (1<<6)) != 0;
93         return new PortConfig(pcNoFwd, pcNoPacketIn, pcNoRecv, pcPortDown);
94     }
95 }