Bump upstreams
[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.nio.charset.StandardCharsets;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.openflowjava.util.ByteBufUtils;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionTypeV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
25
26 /**
27  * Translates FeaturesReply messages.
28  *
29  * @author giuseppex.petralia@intel.com
30  */
31 public class OF10FeaturesReplyMessageFactory implements OFSerializer<GetFeaturesOutput> {
32
33     private static final byte PADDING = 3;
34     private static final byte MESSAGE_TYPE = 6;
35
36     @Override
37     public void serialize(final GetFeaturesOutput message, final ByteBuf outBuffer) {
38         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
39         outBuffer.writeLong(message.getDatapathId().longValue());
40         outBuffer.writeInt(message.getBuffers().intValue());
41         outBuffer.writeByte(message.getTables().intValue());
42         outBuffer.writeZero(PADDING);
43         outBuffer.writeInt(createCapabilities(message.getCapabilitiesV10()));
44         outBuffer.writeInt(createActionsV10(message.getActionsV10()));
45         for (PhyPort port : message.getPhyPort()) {
46             outBuffer.writeShort(port.getPortNo().intValue());
47             outBuffer.writeBytes(IetfYangUtil.macAddressBytes(port.getHwAddr()));
48             writeName(port.getName(), outBuffer);
49             writePortConfig(port.getConfigV10(), outBuffer);
50             writePortState(port.getStateV10(), outBuffer);
51             writePortFeature(port.getCurrentFeaturesV10(), outBuffer);
52             writePortFeature(port.getAdvertisedFeaturesV10(), outBuffer);
53             writePortFeature(port.getSupportedFeaturesV10(), outBuffer);
54             writePortFeature(port.getPeerFeaturesV10(), outBuffer);
55         }
56         ByteBufUtils.updateOFHeaderLength(outBuffer);
57     }
58
59     private static void writePortFeature(final PortFeaturesV10 feature, final ByteBuf outBuffer) {
60         Map<Integer, Boolean> map = new HashMap<>();
61         map.put(0, feature.get_10mbHd());
62         map.put(1, feature.get_10mbFd());
63         map.put(2, feature.get_100mbHd());
64         map.put(3, feature.get_100mbFd());
65         map.put(4, feature.get_1gbHd());
66         map.put(5, feature.get_1gbFd());
67         map.put(6, feature.get_10gbFd());
68         map.put(7, feature.getCopper());
69         map.put(8, feature.getFiber());
70         map.put(9, feature.getAutoneg());
71         map.put(10, feature.getPause());
72         map.put(11, feature.getPauseAsym());
73         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
74         outBuffer.writeInt(bitmap);
75     }
76
77     private static void writePortState(final PortStateV10 state, final ByteBuf outBuffer) {
78         Map<Integer, Boolean> map = new HashMap<>();
79         map.put(0, state.getLinkDown());
80         map.put(1, state.getBlocked());
81         map.put(2, state.getLive());
82         map.put(3, state.getStpListen());
83         map.put(4, state.getStpLearn());
84         map.put(5, state.getStpForward());
85         map.put(6, state.getStpBlock());
86         map.put(7, state.getStpMask());
87         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
88         outBuffer.writeInt(bitmap);
89     }
90
91     private static void writePortConfig(final PortConfigV10 config, final ByteBuf outBuffer) {
92         Map<Integer, Boolean> map = new HashMap<>();
93         map.put(0, config.getPortDown());
94         map.put(1, config.getNoStp());
95         map.put(2, config.getNoRecv());
96         map.put(3, config.getNoRecvStp());
97         map.put(4, config.getNoFlood());
98         map.put(5, config.getNoFwd());
99         map.put(6, config.getNoPacketIn());
100         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
101         outBuffer.writeInt(bitmap);
102     }
103
104     private static int createCapabilities(final CapabilitiesV10 capabilities) {
105         Map<Integer, Boolean> map = new HashMap<>();
106         map.put(0, capabilities.getOFPCFLOWSTATS());
107         map.put(1, capabilities.getOFPCTABLESTATS());
108         map.put(2, capabilities.getOFPCPORTSTATS());
109         map.put(3, capabilities.getOFPCSTP());
110         map.put(4, capabilities.getOFPCRESERVED());
111         map.put(5, capabilities.getOFPCIPREASM());
112         map.put(6, capabilities.getOFPCQUEUESTATS());
113         map.put(7, capabilities.getOFPCARPMATCHIP());
114         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
115         return bitmap;
116     }
117
118     private static int createActionsV10(final ActionTypeV10 action) {
119         return ByteBufUtils.fillBitMask(0, action.getOFPATOUTPUT(), action.getOFPATSETVLANVID(),
120                 action.getOFPATSETVLANPCP(), action.getOFPATSTRIPVLAN(), action.getOFPATSETDLSRC(),
121                 action.getOFPATSETDLDST(), action.getOFPATSETNWSRC(), action.getOFPATSETNWDST(),
122                 action.getOFPATSETNWTOS(), action.getOFPATSETTPSRC(), action.getOFPATSETTPDST(),
123                 action.getOFPATENQUEUE(), action.getOFPATVENDOR());
124     }
125
126     private static void writeName(final String name, final ByteBuf outBuffer) {
127         byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
128         if (nameBytes.length < 16) {
129             byte[] nameBytesPadding = new byte[16];
130             int index = 0;
131             for (byte b : nameBytes) {
132                 nameBytesPadding[index] = b;
133                 index++;
134             }
135             for (; index < 16; index++) {
136                 nameBytesPadding[index] = 0x0;
137             }
138             outBuffer.writeBytes(nameBytesPadding);
139         } else {
140             outBuffer.writeBytes(nameBytes);
141         }
142     }
143 }