ea859a2d53fe95aff9fd56ec826877ee986d2b4d
[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.impl.deserialization.OFDeserializer;
18 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
19 import org.opendaylight.openflowjava.protocol.impl.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     private static OF10FeaturesReplyMessageFactory instance;
40
41     private OF10FeaturesReplyMessageFactory() {
42         // do nothing, just singleton
43     }
44     
45     /**
46      * @return singleton factory
47      */
48     public static synchronized OF10FeaturesReplyMessageFactory getInstance() {
49         if (instance == null) {
50             instance = new OF10FeaturesReplyMessageFactory();
51         }
52         return instance;
53     }
54     
55     @Override
56     public GetFeaturesOutput bufferToMessage(ByteBuf rawMessage, short version) {
57         GetFeaturesOutputBuilder builder = new GetFeaturesOutputBuilder();
58         builder.setVersion(version);
59         builder.setXid(rawMessage.readUnsignedInt());
60         byte[] datapathId = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
61         rawMessage.readBytes(datapathId);
62         builder.setDatapathId(new BigInteger(1, datapathId));
63         builder.setBuffers(rawMessage.readUnsignedInt());
64         builder.setTables(rawMessage.readUnsignedByte());
65         rawMessage.skipBytes(PADDING_IN_FEATURES_REPLY_HEADER);
66         builder.setCapabilitiesV10(createCapabilitiesV10(rawMessage.readUnsignedInt()));
67         builder.setActionsV10(createActionsV10(rawMessage.readUnsignedInt()));
68         List<PhyPort> ports = new ArrayList<>();
69         while (rawMessage.readableBytes() > 0) {
70             ports.add(deserializePort(rawMessage));
71         }
72         builder.setPhyPort(ports);
73         return builder.build();
74     }
75
76     private static CapabilitiesV10 createCapabilitiesV10(long input) {
77         final Boolean FLOW_STATS = (input & (1 << 0)) != 0;
78         final Boolean TABLE_STATS = (input & (1 << 1)) != 0;
79         final Boolean PORT_STATS = (input & (1 << 2)) != 0;
80         final Boolean STP = (input & (1 << 3)) != 0;
81         final Boolean RESERVED = (input & (1 << 4)) != 0;
82         final Boolean IP_REASM = (input & (1 << 5)) != 0;
83         final Boolean QUEUE_STATS = (input & (1 << 6)) != 0;
84         final Boolean ARP_MATCH_IP = (input & (1 << 7)) != 0;
85         return new CapabilitiesV10(ARP_MATCH_IP, FLOW_STATS, IP_REASM,
86                 PORT_STATS, QUEUE_STATS, RESERVED, STP, TABLE_STATS);
87     }
88     
89     private static ActionTypeV10 createActionsV10(long input) {
90         final Boolean OUTPUT = (input & (1 << 0)) != 0;
91         final Boolean SET_VLAN_VID = (input & (1 << 1)) != 0;
92         final Boolean SET_VLAN_PCP = (input & (1 << 2)) != 0;
93         final Boolean STRIP_VLAN = (input & (1 << 3)) != 0;
94         final Boolean SET_DL_SRC = (input & (1 << 4)) != 0;
95         final Boolean SET_DL_DST = (input & (1 << 5)) != 0;
96         final Boolean SET_NW_SRC = (input & (1 << 6)) != 0;
97         final Boolean SET_NW_DST = (input & (1 << 7)) != 0;
98         final Boolean SET_NW_TOS = (input & (1 << 8)) != 0;
99         final Boolean SET_TP_SRC = (input & (1 << 9)) != 0;
100         final Boolean SET_TP_DST = (input & (1 << 10)) != 0;
101         final Boolean ENQUEUE = (input & (1 << 11)) != 0;
102         final Boolean VENDOR = (input & (1 << 12)) != 0;
103         return new ActionTypeV10(ENQUEUE, OUTPUT, SET_DL_DST, SET_DL_SRC,
104                 SET_NW_DST, SET_NW_SRC, SET_NW_TOS, SET_TP_DST, SET_TP_SRC,
105                 SET_VLAN_PCP, SET_VLAN_VID, STRIP_VLAN, VENDOR);
106     }
107     
108     private static PhyPort deserializePort(ByteBuf rawMessage) {
109         PhyPortBuilder builder = new PhyPortBuilder();
110         builder.setPortNo((long) rawMessage.readUnsignedShort());
111         byte[] address = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
112         rawMessage.readBytes(address);
113         builder.setHwAddr(new MacAddress(ByteBufUtils.macAddressToString(address)));
114         builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
115         builder.setConfigV10(createPortConfig(rawMessage.readUnsignedInt()));
116         builder.setStateV10(createPortState(rawMessage.readUnsignedInt()));
117         builder.setCurrentFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
118         builder.setAdvertisedFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
119         builder.setSupportedFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
120         builder.setPeerFeaturesV10(createPortFeatures(rawMessage.readUnsignedInt()));
121         return builder.build();
122     }
123
124     
125     
126     private static PortStateV10 createPortState(long input){
127         final Boolean _linkDown = ((input) & (1<<0)) != 0;
128         final Boolean _blocked = ((input) & (1<<1)) != 0;
129         final Boolean _live = ((input) & (1<<2)) != 0;
130         final Boolean _stpListen = ((input) & (0<<8)) != 0;
131         final Boolean _stpLearn = ((input) & (1<<8)) != 0;
132         final Boolean _stpForward = ((input) & (1<<9)) != 0; // equals 2 << 8
133         final Boolean _stpBlock = (((input) & (1<<9)) != 0) && (((input) & (1<<8)) != 0); // equals 3 << 8
134         final Boolean _stpMask = ((input) & (1<<10)) != 0; // equals 4 << 8
135         return new PortStateV10(_blocked, _linkDown, _live, _stpBlock, _stpForward, _stpLearn, _stpListen, _stpMask);
136     }
137     
138     private static PortConfigV10 createPortConfig(long input){
139         final Boolean _portDown = ((input) & (1<<0)) != 0;
140         final Boolean _noStp = ((input) & (1<<1)) != 0;
141         final Boolean _noRecv = ((input) & (1<<2)) != 0;
142         final Boolean _noRecvStp = ((input) & (1<<3)) != 0;
143         final Boolean _noFlood = ((input) & (1<<4)) != 0;
144         final Boolean _noFwd  = ((input) & (1<<5)) != 0;
145         final Boolean _noPacketIn = ((input) & (1<<6)) != 0;
146         return new PortConfigV10(_noFlood, _noFwd, _noPacketIn, _noRecv, _noRecvStp, _noStp, _portDown);
147     }
148     
149     private static PortFeaturesV10 createPortFeatures(long input){
150         final Boolean _10mbHd = ((input) & (1<<0)) != 0;
151         final Boolean _10mbFd = ((input) & (1<<1)) != 0;
152         final Boolean _100mbHd = ((input) & (1<<2)) != 0;
153         final Boolean _100mbFd = ((input) & (1<<3)) != 0;
154         final Boolean _1gbHd = ((input) & (1<<4)) != 0;
155         final Boolean _1gbFd = ((input) & (1<<5)) != 0;
156         final Boolean _10gbFd = ((input) & (1<<6)) != 0;
157         final Boolean _copper = ((input) & (1<<7)) != 0;
158         final Boolean _fiber = ((input) & (1<<8)) != 0;
159         final Boolean _autoneg = ((input) & (1<<9)) != 0;
160         final Boolean _pause = ((input) & (1<<10)) != 0;
161         final Boolean _pauseAsym = ((input) & (1<<11)) != 0;
162         return new PortFeaturesV10(_100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd,
163                 _1gbFd, _1gbHd, _autoneg, _copper, _fiber, _pause, _pauseAsym);
164     }
165     
166 }