Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10FeaturesReplyMessageFactory.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 java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionTypeV10;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
30
31 /**
32  * Translates FeaturesReply messages (OpenFlow v1.0)
33  * @author michal.polkorab
34  */
35 public class OF10FeaturesReplyMessageFactory implements OFDeserializer<GetFeaturesOutput> {
36     
37     private static final byte PADDING_IN_FEATURES_REPLY_HEADER = 3;
38
39     @Override
40     public GetFeaturesOutput deserialize(ByteBuf rawMessage) {
41         GetFeaturesOutputBuilder builder = new GetFeaturesOutputBuilder();
42         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
43         builder.setXid(rawMessage.readUnsignedInt());
44         byte[] datapathId = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
45         rawMessage.readBytes(datapathId);
46         builder.setDatapathId(new BigInteger(1, datapathId));
47         builder.setBuffers(rawMessage.readUnsignedInt());
48         builder.setTables(rawMessage.readUnsignedByte());
49         rawMessage.skipBytes(PADDING_IN_FEATURES_REPLY_HEADER);
50         builder.setCapabilitiesV10(createCapabilitiesV10(rawMessage.readUnsignedInt()));
51         builder.setActionsV10(createActionsV10(rawMessage.readUnsignedInt()));
52         List<PhyPort> ports = new ArrayList<>();
53         while (rawMessage.readableBytes() > 0) {
54             ports.add(deserializePort(rawMessage));
55         }
56         builder.setPhyPort(ports);
57         return builder.build();
58     }
59
60     private static CapabilitiesV10 createCapabilitiesV10(long input) {
61         final Boolean FLOW_STATS = (input & (1 << 0)) != 0;
62         final Boolean TABLE_STATS = (input & (1 << 1)) != 0;
63         final Boolean PORT_STATS = (input & (1 << 2)) != 0;
64         final Boolean STP = (input & (1 << 3)) != 0;
65         final Boolean RESERVED = (input & (1 << 4)) != 0;
66         final Boolean IP_REASM = (input & (1 << 5)) != 0;
67         final Boolean QUEUE_STATS = (input & (1 << 6)) != 0;
68         final Boolean ARP_MATCH_IP = (input & (1 << 7)) != 0;
69         return new CapabilitiesV10(ARP_MATCH_IP, FLOW_STATS, IP_REASM,
70                 PORT_STATS, QUEUE_STATS, RESERVED, STP, TABLE_STATS);
71     }
72     
73     private static ActionTypeV10 createActionsV10(long input) {
74         final Boolean OUTPUT = (input & (1 << 0)) != 0;
75         final Boolean SET_VLAN_VID = (input & (1 << 1)) != 0;
76         final Boolean SET_VLAN_PCP = (input & (1 << 2)) != 0;
77         final Boolean STRIP_VLAN = (input & (1 << 3)) != 0;
78         final Boolean SET_DL_SRC = (input & (1 << 4)) != 0;
79         final Boolean SET_DL_DST = (input & (1 << 5)) != 0;
80         final Boolean SET_NW_SRC = (input & (1 << 6)) != 0;
81         final Boolean SET_NW_DST = (input & (1 << 7)) != 0;
82         final Boolean SET_NW_TOS = (input & (1 << 8)) != 0;
83         final Boolean SET_TP_SRC = (input & (1 << 9)) != 0;
84         final Boolean SET_TP_DST = (input & (1 << 10)) != 0;
85         final Boolean ENQUEUE = (input & (1 << 11)) != 0;
86         final Boolean VENDOR = (input & (1 << 12)) != 0;
87         return new ActionTypeV10(ENQUEUE, OUTPUT, SET_DL_DST, SET_DL_SRC,
88                 SET_NW_DST, SET_NW_SRC, SET_NW_TOS, SET_TP_DST, SET_TP_SRC,
89                 SET_VLAN_PCP, SET_VLAN_VID, STRIP_VLAN, VENDOR);
90     }
91     
92     private static PhyPort deserializePort(ByteBuf rawMessage) {
93         PhyPortBuilder builder = new PhyPortBuilder();
94         builder.setPortNo((long) rawMessage.readUnsignedShort());
95         byte[] address = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
96         rawMessage.readBytes(address);
97         builder.setHwAddr(new MacAddress(ByteBufUtils.macAddressToString(address)));
98         builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
99         builder.setConfigV10(createPortConfig(rawMessage.readUnsignedInt()));
100         builder.setStateV10(createPortState(rawMessage.readUnsignedInt()));
101         builder.setCurrentFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
102         builder.setAdvertisedFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
103         builder.setSupportedFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
104         builder.setPeerFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
105         return builder.build();
106     }
107
108     
109     
110     private static PortStateV10 createPortState(long input){
111         final Boolean _linkDown = ((input) & (1<<0)) != 0;
112         final Boolean _blocked = ((input) & (1<<1)) != 0;
113         final Boolean _live = ((input) & (1<<2)) != 0;
114         final Boolean _stpListen = ((input) & (0<<8)) != 0;
115         final Boolean _stpLearn = ((input) & (1<<8)) != 0;
116         final Boolean _stpForward = ((input) & (1<<9)) != 0; // equals 2 << 8
117         final Boolean _stpBlock = (((input) & (1<<9)) != 0) && (((input) & (1<<8)) != 0); // equals 3 << 8
118         final Boolean _stpMask = ((input) & (1<<10)) != 0; // equals 4 << 8
119         return new PortStateV10(_blocked, _linkDown, _live, _stpBlock, _stpForward, _stpLearn, _stpListen, _stpMask);
120     }
121     
122     private static PortConfigV10 createPortConfig(long input){
123         final Boolean _portDown = ((input) & (1<<0)) != 0;
124         final Boolean _noStp = ((input) & (1<<1)) != 0;
125         final Boolean _noRecv = ((input) & (1<<2)) != 0;
126         final Boolean _noRecvStp = ((input) & (1<<3)) != 0;
127         final Boolean _noFlood = ((input) & (1<<4)) != 0;
128         final Boolean _noFwd  = ((input) & (1<<5)) != 0;
129         final Boolean _noPacketIn = ((input) & (1<<6)) != 0;
130         return new PortConfigV10(_noFlood, _noFwd, _noPacketIn, _noRecv, _noRecvStp, _noStp, _portDown);
131     }
132     
133     private static PortFeaturesV10 createPortFeatures(long input){
134         final Boolean _10mbHd = ((input) & (1<<0)) != 0;
135         final Boolean _10mbFd = ((input) & (1<<1)) != 0;
136         final Boolean _100mbHd = ((input) & (1<<2)) != 0;
137         final Boolean _100mbFd = ((input) & (1<<3)) != 0;
138         final Boolean _1gbHd = ((input) & (1<<4)) != 0;
139         final Boolean _1gbFd = ((input) & (1<<5)) != 0;
140         final Boolean _10gbFd = ((input) & (1<<6)) != 0;
141         final Boolean _copper = ((input) & (1<<7)) != 0;
142         final Boolean _fiber = ((input) & (1<<8)) != 0;
143         final Boolean _autoneg = ((input) & (1<<9)) != 0;
144         final Boolean _pause = ((input) & (1<<10)) != 0;
145         final Boolean _pauseAsym = ((input) & (1<<11)) != 0;
146         return new PortFeaturesV10(_100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd,
147                 _1gbFd, _1gbHd, _autoneg, _copper, _fiber, _pause, _pauseAsym);
148     }
149     
150 }