5d6036ad228f9c13ec815b7b20f5ec774ee48cb6
[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.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.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     private static OF10PortStatusMessageFactory instance;
33     
34     private OF10PortStatusMessageFactory() {
35         // Singleton
36     }
37     
38     /**
39      * @return singleton factory
40      */
41     public static synchronized OF10PortStatusMessageFactory getInstance(){
42         if(instance == null){
43             instance = new OF10PortStatusMessageFactory();
44         }
45         return instance;
46     }
47     
48     @Override
49     public PortStatusMessage bufferToMessage(ByteBuf rawMessage, short version) {
50         PortStatusMessageBuilder builder = new PortStatusMessageBuilder(); 
51         builder.setVersion(version);
52         builder.setXid(rawMessage.readUnsignedInt());
53         builder.setReason(PortReason.forValue(rawMessage.readUnsignedByte()));
54         rawMessage.skipBytes(PADDING_IN_PORT_STATUS_HEADER);
55         deserializePort(rawMessage, builder);
56         return builder.build();
57     }
58
59     private static PortStateV10 createPortState(long input){
60         final Boolean _linkDown = ((input) & (1<<0)) != 0;
61         final Boolean _blocked = ((input) & (1<<1)) != 0;
62         final Boolean _live = ((input) & (1<<2)) != 0;
63         final Boolean _stpListen = ((input) & (0<<8)) != 0;
64         final Boolean _stpLearn = ((input) & (1<<8)) != 0;
65         final Boolean _stpForward = ((input) & (1<<9)) != 0; // equals 2 << 8
66         final Boolean _stpBlock = (((input) & (1<<9)) != 0) && (((input) & (1<<8)) != 0); // equals 3 << 8
67         final Boolean _stpMask = ((input) & (1<<10)) != 0; // equals 4 << 8
68         return new PortStateV10(_blocked, _linkDown, _live, _stpBlock, _stpForward, _stpLearn, _stpListen, _stpMask);
69     }
70     
71     private static PortConfigV10 createPortConfig(long input){
72         final Boolean _portDown = ((input) & (1<<0)) != 0;
73         final Boolean _noStp = ((input) & (1<<1)) != 0;
74         final Boolean _noRecv = ((input) & (1<<2)) != 0;
75         final Boolean _noRecvStp = ((input) & (1<<3)) != 0;
76         final Boolean _noFlood = ((input) & (1<<4)) != 0;
77         final Boolean _noFwd  = ((input) & (1<<5)) != 0;
78         final Boolean _noPacketIn = ((input) & (1<<6)) != 0;
79         return new PortConfigV10(_noFlood, _noFwd, _noPacketIn, _noRecv, _noRecvStp, _noStp, _portDown);
80     }
81     
82     private static PortFeaturesV10 createPortFeatures(long input){
83         final Boolean _10mbHd = ((input) & (1<<0)) != 0;
84         final Boolean _10mbFd = ((input) & (1<<1)) != 0;
85         final Boolean _100mbHd = ((input) & (1<<2)) != 0;
86         final Boolean _100mbFd = ((input) & (1<<3)) != 0;
87         final Boolean _1gbHd = ((input) & (1<<4)) != 0;
88         final Boolean _1gbFd = ((input) & (1<<5)) != 0;
89         final Boolean _10gbFd = ((input) & (1<<6)) != 0;
90         final Boolean _copper = ((input) & (1<<7)) != 0;
91         final Boolean _fiber = ((input) & (1<<8)) != 0;
92         final Boolean _autoneg = ((input) & (1<<9)) != 0;
93         final Boolean _pause = ((input) & (1<<10)) != 0;
94         final Boolean _pauseAsym = ((input) & (1<<11)) != 0;
95         return new PortFeaturesV10(_100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd,
96                 _1gbFd, _1gbHd, _autoneg, _copper, _fiber, _pause, _pauseAsym);
97     }
98     
99     private static void deserializePort(ByteBuf rawMessage, PortStatusMessageBuilder builder) {
100         builder.setPortNo((long) rawMessage.readUnsignedShort());
101         byte[] address = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
102         rawMessage.readBytes(address);
103         builder.setHwAddr(new MacAddress(ByteBufUtils.macAddressToString(address)));
104         builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
105         builder.setConfigV10(createPortConfig(rawMessage.readUnsignedInt()));
106         builder.setStateV10(createPortState(rawMessage.readUnsignedInt()));
107         builder.setCurrentFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
108         builder.setAdvertisedFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
109         builder.setSupportedFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
110         builder.setPeerFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
111     }
112 }