Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PortModInputMessageFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
18 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
22
23 /**
24  * Translates PortMod messages
25  * @author timotej.kubas
26  * @author michal.polkorab
27  */
28 public class PortModInputMessageFactory implements OFSerializer<PortModInput> {
29     private static final byte MESSAGE_TYPE = 16;
30     private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
31     private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
32     private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
33
34     @Override
35     public void serialize(PortModInput message, ByteBuf outBuffer) {
36         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
37         outBuffer.writeInt(message.getPortNo().getValue().intValue());
38         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_01, outBuffer);
39         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(message.getHwAddress().getValue()));
40         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_02, outBuffer);
41         outBuffer.writeInt(createPortConfigBitmask(message.getConfig()));
42         outBuffer.writeInt(createPortConfigBitmask(message.getMask()));
43         outBuffer.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
44         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_03, outBuffer);
45         ByteBufUtils.updateOFHeaderLength(outBuffer);
46     }
47
48     /**
49      * @param config
50      * @return port config bitmask 
51      */
52     private static int createPortConfigBitmask(PortConfig config) {
53         int configBitmask = 0;
54         Map<Integer, Boolean> portConfigMap = new HashMap<>();
55         portConfigMap.put(0, config.isPortDown());
56         portConfigMap.put(2, config.isNoRecv());
57         portConfigMap.put(5, config.isNoFwd());
58         portConfigMap.put(6, config.isNoPacketIn());
59         
60         configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
61         return configBitmask;
62     }
63     
64     private static int createPortFeaturesBitmask(PortFeatures feature) {
65         int configBitmask = 0;
66         Map<Integer, Boolean> portFeaturesMap = new HashMap<>();
67         portFeaturesMap.put(0, feature.is_10mbHd());
68         portFeaturesMap.put(1, feature.is_10mbFd());
69         portFeaturesMap.put(2, feature.is_100mbHd());
70         portFeaturesMap.put(3, feature.is_100mbFd());
71         portFeaturesMap.put(4, feature.is_1gbHd());
72         portFeaturesMap.put(5, feature.is_1gbFd());
73         portFeaturesMap.put(6, feature.is_10gbFd());
74         portFeaturesMap.put(7, feature.is_40gbFd());
75         portFeaturesMap.put(8, feature.is_100gbFd());
76         portFeaturesMap.put(9, feature.is_1tbFd());
77         portFeaturesMap.put(10, feature.isOther());
78         portFeaturesMap.put(11, feature.isCopper());
79         portFeaturesMap.put(12, feature.isFiber());
80         portFeaturesMap.put(13, feature.isAutoneg());
81         portFeaturesMap.put(14, feature.isPause());
82         portFeaturesMap.put(15, feature.isPauseAsym());
83         
84         configBitmask = ByteBufUtils.fillBitMaskFromMap(portFeaturesMap);
85         return configBitmask;
86     }
87
88 }