8edf30ca16d5124c3b1972c16238a7e72c9efcf0
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10FeaturesReplyMessageFactory.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.openflowjava.util.ByteBufUtils;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionTypeV10;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
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.GetFeaturesOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
23
24 /**
25  * @author giuseppex.petralia@intel.com
26  *
27  */
28 public class OF10FeaturesReplyMessageFactory implements OFSerializer<GetFeaturesOutput> {
29
30     private static final byte PADDING = 3;
31     private static final byte MESSAGE_TYPE = 6;
32
33     @Override
34     public void serialize(GetFeaturesOutput message, ByteBuf outBuffer) {
35         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
36         outBuffer.writeLong(message.getDatapathId().longValue());
37         outBuffer.writeInt(message.getBuffers().intValue());
38         outBuffer.writeByte(message.getTables().intValue());
39         outBuffer.writeZero(PADDING);
40         outBuffer.writeInt(createCapabilities(message.getCapabilitiesV10()));
41         outBuffer.writeInt(createActionsV10(message.getActionsV10()));
42         for (PhyPort port : message.getPhyPort()) {
43             outBuffer.writeShort(port.getPortNo().intValue());
44             writeMacAddress(port.getHwAddr().getValue(), outBuffer);
45             writeName(port.getName(), outBuffer);
46             writePortConfig(port.getConfigV10(), outBuffer);
47             writePortState(port.getStateV10(), outBuffer);
48             writePortFeature(port.getCurrentFeaturesV10(), outBuffer);
49             writePortFeature(port.getAdvertisedFeaturesV10(), outBuffer);
50             writePortFeature(port.getSupportedFeaturesV10(), outBuffer);
51             writePortFeature(port.getPeerFeaturesV10(), outBuffer);
52         }
53         ByteBufUtils.updateOFHeaderLength(outBuffer);
54     }
55
56     private void writePortFeature(PortFeaturesV10 feature, ByteBuf outBuffer) {
57         Map<Integer, Boolean> map = new HashMap<>();
58         map.put(0, feature.is_10mbHd());
59         map.put(1, feature.is_10mbFd());
60         map.put(2, feature.is_100mbHd());
61         map.put(3, feature.is_100mbFd());
62         map.put(4, feature.is_1gbHd());
63         map.put(5, feature.is_1gbFd());
64         map.put(6, feature.is_10gbFd());
65         map.put(7, feature.isCopper());
66         map.put(8, feature.isFiber());
67         map.put(9, feature.isAutoneg());
68         map.put(10, feature.isPause());
69         map.put(11, feature.isPauseAsym());
70         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
71         outBuffer.writeInt(bitmap);
72     }
73
74     private void writePortState(PortStateV10 state, ByteBuf outBuffer) {
75         Map<Integer, Boolean> map = new HashMap<>();
76         map.put(0, state.isLinkDown());
77         map.put(1, state.isBlocked());
78         map.put(2, state.isLive());
79         map.put(3, state.isStpListen());
80         map.put(4, state.isStpLearn());
81         map.put(5, state.isStpForward());
82         map.put(6, state.isStpBlock());
83         map.put(7, state.isStpMask());
84         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
85         outBuffer.writeInt(bitmap);
86     }
87
88     private void writePortConfig(PortConfigV10 config, ByteBuf outBuffer) {
89         Map<Integer, Boolean> map = new HashMap<>();
90         map.put(0, config.isPortDown());
91         map.put(1, config.isNoStp());
92         map.put(2, config.isNoRecv());
93         map.put(3, config.isNoRecvStp());
94         map.put(4, config.isNoFlood());
95         map.put(5, config.isNoFwd());
96         map.put(6, config.isNoPacketIn());
97         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
98         outBuffer.writeInt(bitmap);
99     }
100
101     private static int createCapabilities(CapabilitiesV10 capabilities) {
102         Map<Integer, Boolean> map = new HashMap<>();
103         map.put(0, capabilities.isOFPCFLOWSTATS());
104         map.put(1, capabilities.isOFPCTABLESTATS());
105         map.put(2, capabilities.isOFPCPORTSTATS());
106         map.put(3, capabilities.isOFPCSTP());
107         map.put(4, capabilities.isOFPCRESERVED());
108         map.put(5, capabilities.isOFPCIPREASM());
109         map.put(6, capabilities.isOFPCQUEUESTATS());
110         map.put(7, capabilities.isOFPCARPMATCHIP());
111         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
112         return bitmap;
113     }
114
115     private static int createActionsV10(final ActionTypeV10 action) {
116         return ByteBufUtils.fillBitMask(0, action.isOFPATOUTPUT(), action.isOFPATSETVLANVID(),
117                 action.isOFPATSETVLANPCP(), action.isOFPATSTRIPVLAN(), action.isOFPATSETDLSRC(),
118                 action.isOFPATSETDLDST(), action.isOFPATSETNWSRC(), action.isOFPATSETNWDST(), action.isOFPATSETNWTOS(),
119                 action.isOFPATSETTPSRC(), action.isOFPATSETTPDST(), action.isOFPATENQUEUE(), action.isOFPATVENDOR());
120
121     }
122
123     private void writeMacAddress(String macAddress, ByteBuf outBuffer) {
124         String[] macAddressParts = macAddress.split(":");
125         byte[] macAddressBytes = new byte[6];
126         for (int i = 0; i < 6; i++) {
127             Integer hex = Integer.parseInt(macAddressParts[i], 16);
128             macAddressBytes[i] = hex.byteValue();
129         }
130         outBuffer.writeBytes(macAddressBytes);
131     }
132
133     private void writeName(String name, ByteBuf outBuffer) {
134         byte[] nameBytes = name.getBytes();
135         if (nameBytes.length < 16) {
136             byte[] nameBytesPadding = new byte[16];
137             int i = 0;
138             for (byte b : nameBytes) {
139                 nameBytesPadding[i] = b;
140                 i++;
141             }
142             for (; i < 16; i++) {
143                 nameBytesPadding[i] = 0x0;
144             }
145             outBuffer.writeBytes(nameBytesPadding);
146         } else {
147             outBuffer.writeBytes(nameBytes);
148         }
149
150     }
151 }