Merge "BUG-8607: Fix issues in checkstyle enforcement for module openflowplugin-common"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10PortStatusMessageFactory.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.PortConfigV10;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
21
22 /**
23  * @author giuseppex.petralia@intel.com
24  *
25  */
26 public class OF10PortStatusMessageFactory implements OFSerializer<PortStatusMessage> {
27
28     private static final byte MESSAGE_TYPE = 12;
29     private static final byte PADDING = 7;
30
31     @Override
32     public void serialize(final PortStatusMessage message, final ByteBuf outBuffer) {
33         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
34         outBuffer.writeByte(message.getReason().getIntValue());
35         outBuffer.writeZero(PADDING);
36         outBuffer.writeShort(message.getPortNo().intValue());
37         outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(message.getHwAddr()));
38         writeName(message.getName(), outBuffer);
39         writePortConfig(message.getConfigV10(), outBuffer);
40         writePortState(message.getStateV10(), outBuffer);
41         writePortFeature(message.getCurrentFeaturesV10(), outBuffer);
42         writePortFeature(message.getAdvertisedFeaturesV10(), outBuffer);
43         writePortFeature(message.getSupportedFeaturesV10(), outBuffer);
44         writePortFeature(message.getPeerFeaturesV10(), outBuffer);
45         ByteBufUtils.updateOFHeaderLength(outBuffer);
46     }
47
48     private void writePortFeature(final PortFeaturesV10 feature, final ByteBuf outBuffer) {
49         Map<Integer, Boolean> map = new HashMap<>();
50         map.put(0, feature.is_10mbHd());
51         map.put(1, feature.is_10mbFd());
52         map.put(2, feature.is_100mbHd());
53         map.put(3, feature.is_100mbFd());
54         map.put(4, feature.is_1gbHd());
55         map.put(5, feature.is_1gbFd());
56         map.put(6, feature.is_10gbFd());
57         map.put(7, feature.isCopper());
58         map.put(8, feature.isFiber());
59         map.put(9, feature.isAutoneg());
60         map.put(10, feature.isPause());
61         map.put(11, feature.isPauseAsym());
62         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
63         outBuffer.writeInt(bitmap);
64     }
65
66     private void writePortState(final PortStateV10 state, final ByteBuf outBuffer) {
67         Map<Integer, Boolean> map = new HashMap<>();
68         map.put(0, state.isLinkDown());
69         map.put(1, state.isBlocked());
70         map.put(2, state.isLive());
71         map.put(3, state.isStpListen());
72         map.put(4, state.isStpLearn());
73         map.put(5, state.isStpForward());
74         map.put(6, state.isStpBlock());
75         map.put(7, state.isStpMask());
76         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
77         outBuffer.writeInt(bitmap);
78     }
79
80     private void writePortConfig(final PortConfigV10 config, final ByteBuf outBuffer) {
81         Map<Integer, Boolean> map = new HashMap<>();
82         map.put(0, config.isPortDown());
83         map.put(1, config.isNoStp());
84         map.put(2, config.isNoRecv());
85         map.put(3, config.isNoRecvStp());
86         map.put(4, config.isNoFlood());
87         map.put(5, config.isNoFwd());
88         map.put(6, config.isNoPacketIn());
89         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
90         outBuffer.writeInt(bitmap);
91     }
92
93     private void writeName(final String name, final ByteBuf outBuffer) {
94         byte[] nameBytes = name.getBytes();
95         if (nameBytes.length < 16) {
96             byte[] nameBytesPadding = new byte[16];
97             int i = 0;
98             for (byte b : nameBytes) {
99                 nameBytesPadding[i] = b;
100                 i++;
101             }
102             for (; i < 16; i++) {
103                 nameBytesPadding[i] = 0x0;
104             }
105             outBuffer.writeBytes(nameBytesPadding);
106         } else {
107             outBuffer.writeBytes(nameBytes);
108         }
109     }
110 }