2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.deserialization.factories;
11 import io.netty.buffer.ByteBuf;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.impl.util.OpenflowUtils;
20 import org.opendaylight.openflowjava.util.ByteBufUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionTypeV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder;
30 * Translates FeaturesReply messages (OpenFlow v1.0)
31 * @author michal.polkorab
33 public class OF10FeaturesReplyMessageFactory implements OFDeserializer<GetFeaturesOutput> {
35 private static final byte PADDING_IN_FEATURES_REPLY_HEADER = 3;
38 public GetFeaturesOutput deserialize(ByteBuf rawMessage) {
39 GetFeaturesOutputBuilder builder = new GetFeaturesOutputBuilder();
40 builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
41 builder.setXid(rawMessage.readUnsignedInt());
42 byte[] datapathId = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
43 rawMessage.readBytes(datapathId);
44 builder.setDatapathId(new BigInteger(1, datapathId));
45 builder.setBuffers(rawMessage.readUnsignedInt());
46 builder.setTables(rawMessage.readUnsignedByte());
47 rawMessage.skipBytes(PADDING_IN_FEATURES_REPLY_HEADER);
48 builder.setCapabilitiesV10(createCapabilitiesV10(rawMessage.readUnsignedInt()));
49 builder.setActionsV10(createActionsV10(rawMessage.readUnsignedInt()));
50 List<PhyPort> ports = new ArrayList<>();
51 while (rawMessage.readableBytes() > 0) {
52 ports.add(deserializePort(rawMessage));
54 builder.setPhyPort(ports);
55 return builder.build();
58 private static CapabilitiesV10 createCapabilitiesV10(long input) {
59 final Boolean flowStats = (input & (1 << 0)) != 0;
60 final Boolean tableStats = (input & (1 << 1)) != 0;
61 final Boolean portStats = (input & (1 << 2)) != 0;
62 final Boolean stp = (input & (1 << 3)) != 0;
63 final Boolean reserved = (input & (1 << 4)) != 0;
64 final Boolean ipReasm = (input & (1 << 5)) != 0;
65 final Boolean queueStats = (input & (1 << 6)) != 0;
66 final Boolean arpMatchIp = (input & (1 << 7)) != 0;
67 return new CapabilitiesV10(arpMatchIp, flowStats, ipReasm,
68 portStats, queueStats, reserved, stp, tableStats);
71 private static ActionTypeV10 createActionsV10(long input) {
72 final Boolean output = (input & (1 << 0)) != 0;
73 final Boolean setVLANvid = (input & (1 << 1)) != 0;
74 final Boolean setVLANpcp = (input & (1 << 2)) != 0;
75 final Boolean stripVLAN = (input & (1 << 3)) != 0;
76 final Boolean setDLsrc = (input & (1 << 4)) != 0;
77 final Boolean setDLdst = (input & (1 << 5)) != 0;
78 final Boolean setNWsrc = (input & (1 << 6)) != 0;
79 final Boolean setNWdst = (input & (1 << 7)) != 0;
80 final Boolean setNWtos = (input & (1 << 8)) != 0;
81 final Boolean setTPsrc = (input & (1 << 9)) != 0;
82 final Boolean setTPdst = (input & (1 << 10)) != 0;
83 final Boolean enqueue = (input & (1 << 11)) != 0;
84 final Boolean vendor = (input & (1 << 12)) != 0;
85 return new ActionTypeV10(enqueue, output, setDLdst, setDLsrc,
86 setNWdst, setNWsrc, setNWtos, setTPdst, setTPsrc,
87 setVLANpcp, setVLANvid, stripVLAN, vendor);
90 private static PhyPort deserializePort(ByteBuf rawMessage) {
91 PhyPortBuilder builder = new PhyPortBuilder();
92 builder.setPortNo((long) rawMessage.readUnsignedShort());
93 byte[] address = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
94 rawMessage.readBytes(address);
95 builder.setHwAddr(new MacAddress(ByteBufUtils.macAddressToString(address)));
96 builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
97 builder.setConfigV10(OpenflowUtils.createPortConfig(rawMessage.readUnsignedInt()));
98 builder.setStateV10(OpenflowUtils.createPortState(rawMessage.readUnsignedInt()));
99 builder.setCurrentFeaturesV10(OpenflowUtils.createPortFeatures(rawMessage.readUnsignedInt()));
100 builder.setAdvertisedFeaturesV10(OpenflowUtils.createPortFeatures(rawMessage.readUnsignedInt()));
101 builder.setSupportedFeaturesV10(OpenflowUtils.createPortFeatures(rawMessage.readUnsignedInt()));
102 builder.setPeerFeaturesV10(OpenflowUtils.createPortFeatures(rawMessage.readUnsignedInt()));
103 return builder.build();