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