Merge "TableFeatures deserialization fix"
[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.impl.deserialization.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 PortStatusMessageFactory instance;
32     private static final byte PADDING_IN_PORT_STATUS_HEADER = 7;
33     private static final byte PADDING_IN_OFP_PORT_HEADER_1 = 4;
34     private static final byte PADDING_IN_OFP_PORT_HEADER_2 = 2;
35     
36     private PortStatusMessageFactory() {
37         // Singleton
38     }
39     
40     /**
41      * @return singleton factory
42      */
43     public static synchronized PortStatusMessageFactory getInstance(){
44         if(instance == null){
45             instance = new PortStatusMessageFactory();
46         }
47         return instance;
48     }
49     
50     @Override
51     public PortStatusMessage bufferToMessage(ByteBuf rawMessage, short version) {
52         PortStatusMessageBuilder builder = new PortStatusMessageBuilder(); 
53         builder.setVersion(version);
54         builder.setXid(rawMessage.readUnsignedInt());
55         builder.setReason(PortReason.forValue(rawMessage.readUnsignedByte()));
56         rawMessage.skipBytes(PADDING_IN_PORT_STATUS_HEADER);
57         builder.setPortNo(rawMessage.readUnsignedInt());
58         rawMessage.skipBytes(PADDING_IN_OFP_PORT_HEADER_1);
59         byte[] hwAddress = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
60         rawMessage.readBytes(hwAddress);
61         builder.setHwAddr(new MacAddress(ByteBufUtils.macAddressToString(hwAddress)));
62         rawMessage.skipBytes(PADDING_IN_OFP_PORT_HEADER_2);
63         builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
64         builder.setConfig(createPortConfig(rawMessage.readUnsignedInt()));
65         builder.setState(createPortState(rawMessage.readUnsignedInt()));
66         builder.setCurrentFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
67         builder.setAdvertisedFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
68         builder.setSupportedFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
69         builder.setPeerFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
70         builder.setCurrSpeed(rawMessage.readUnsignedInt());
71         builder.setMaxSpeed(rawMessage.readUnsignedInt());
72         return builder.build();
73     }
74
75     private static PortFeatures createPortFeatures(long input){
76         System.out.println("long: " + input);
77         final Boolean _10mbHd = ((input) & (1<<0)) != 0;
78         final Boolean _10mbFd = ((input) & (1<<1)) != 0;
79         final Boolean _100mbHd = ((input) & (1<<2)) != 0;
80         final Boolean _100mbFd = ((input) & (1<<3)) != 0;
81         final Boolean _1gbHd = ((input) & (1<<4)) != 0;
82         final Boolean _1gbFd = ((input) & (1<<5)) != 0;
83         final Boolean _10gbFd = ((input) & (1<<6)) != 0;
84         final Boolean _40gbFd = ((input) & (1<<7)) != 0;
85         final Boolean _100gbFd = ((input) & (1<<8)) != 0;
86         final Boolean _1tbFd = ((input) & (1<<9)) != 0;
87         final Boolean _other = ((input) & (1<<10)) != 0;
88         final Boolean _copper = ((input) & (1<<11)) != 0;
89         final Boolean _fiber = ((input) & (1<<12)) != 0;
90         final Boolean _autoneg = ((input) & (1<<13)) != 0;
91         final Boolean _pause = ((input) & (1<<14)) != 0;
92         final Boolean _pauseAsym = ((input) & (1<<15)) != 0;
93         return new PortFeatures(_100gbFd, _100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd, _1gbFd,
94                 _1gbHd, _1tbFd, _40gbFd, _autoneg, _copper, _fiber, _other, _pause, _pauseAsym);
95     }
96     
97     private static PortState createPortState(long input){
98         final Boolean _linkDown = ((input) & (1<<0)) != 0;
99         final Boolean _blocked  = ((input) & (1<<1)) != 0;
100         final Boolean _live     = ((input) & (1<<2)) != 0;
101         return new PortState(_blocked, _linkDown, _live);
102     }
103     
104     private static PortConfig createPortConfig(long input){
105         final Boolean _portDown   = ((input) & (1<<0)) != 0;
106         final Boolean _noRecv    = ((input) & (1<<2)) != 0;
107         final Boolean _noFwd       = ((input) & (1<<5)) != 0;
108         final Boolean _noPacketIn = ((input) & (1<<6)) != 0;
109         return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown);
110     }
111     
112 }