Copyright update
[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.setConfig(createPortConfig(rawMessage.readUnsignedInt()));
64         builder.setState(createPortState(rawMessage.readUnsignedInt()));
65         builder.setCurrentFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
66         builder.setAdvertisedFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
67         builder.setSupportedFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
68         builder.setPeerFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
69         builder.setCurrSpeed(rawMessage.readUnsignedInt());
70         builder.setMaxSpeed(rawMessage.readUnsignedInt());
71         return builder.build();
72     }
73
74     private static PortFeatures createPortFeatures(long input){
75         final Boolean _10mbHd = ((input) & (1<<0)) != 0;
76         final Boolean _10mbFd = ((input) & (1<<1)) != 0;
77         final Boolean _100mbHd = ((input) & (1<<2)) != 0;
78         final Boolean _100mbFd = ((input) & (1<<3)) != 0;
79         final Boolean _1gbHd = ((input) & (1<<4)) != 0;
80         final Boolean _1gbFd = ((input) & (1<<5)) != 0;
81         final Boolean _10gbFd = ((input) & (1<<6)) != 0;
82         final Boolean _40gbFd = ((input) & (1<<7)) != 0;
83         final Boolean _100gbFd = ((input) & (1<<8)) != 0;
84         final Boolean _1tbFd = ((input) & (1<<9)) != 0;
85         final Boolean _other = ((input) & (1<<10)) != 0;
86         final Boolean _copper = ((input) & (1<<11)) != 0;
87         final Boolean _fiber = ((input) & (1<<12)) != 0;
88         final Boolean _autoneg = ((input) & (1<<13)) != 0;
89         final Boolean _pause = ((input) & (1<<14)) != 0;
90         final Boolean _pauseAsym = ((input) & (1<<15)) != 0;
91         return new PortFeatures(_10mbHd, _10mbFd, _100mbHd, _100mbFd, _1gbHd, _1gbFd, _10gbFd,
92                 _40gbFd, _100gbFd, _1tbFd, _other, _copper, _fiber, _autoneg, _pause, _pauseAsym);
93     }
94     
95     private static PortState createPortState(long input){
96         final Boolean _linkDown = ((input) & (1<<0)) != 0;
97         final Boolean _blocked  = ((input) & (1<<1)) != 0;
98         final Boolean _live     = ((input) & (1<<2)) != 0;
99         return new PortState(_blocked, _linkDown, _live);
100     }
101     
102     private static PortConfig createPortConfig(long input){
103         final Boolean _portDown   = ((input) & (1<<0)) != 0;
104         final Boolean _noRecv    = ((input) & (1<<2)) != 0;
105         final Boolean _noFwd       = ((input) & (1<<5)) != 0;
106         final Boolean _noPacketIn = ((input) & (1<<6)) != 0;
107         return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown);
108     }
109     
110 }