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