Extensibility support (serialization part)
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10PortModInputMessageFactory.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.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.protocol.rev130731.PortModInput;
22
23 /**
24  * Translates PortMod messages
25  * @author michal.polkorab
26  */
27 public class OF10PortModInputMessageFactory implements OFSerializer<PortModInput> {
28
29     private static final byte MESSAGE_TYPE = 15;
30     private static final byte PADDING_IN_PORT_MOD_MESSAGE = 4;
31
32     @Override
33     public void serialize(PortModInput message, ByteBuf outBuffer) {
34         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
35         outBuffer.writeShort(message.getPortNo().getValue().intValue());
36         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(message.getHwAddress().getValue()));
37         outBuffer.writeInt(createPortConfigBitmask(message.getConfigV10()));
38         outBuffer.writeInt(createPortConfigBitmask(message.getMaskV10()));
39         outBuffer.writeInt(createPortFeaturesBitmask(message.getAdvertiseV10()));
40         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE, outBuffer);
41         ByteBufUtils.updateOFHeaderLength(outBuffer);
42     }
43
44     /**
45      * @param config
46      * @return port config bitmask 
47      */
48     private static int createPortConfigBitmask(PortConfigV10 config) {
49         int configBitmask = 0;
50         Map<Integer, Boolean> portConfigMap = new HashMap<>();
51         portConfigMap.put(0, config.isPortDown());
52         portConfigMap.put(1, config.isNoStp());
53         portConfigMap.put(2, config.isNoRecv());
54         portConfigMap.put(3, config.isNoRecvStp());
55         portConfigMap.put(4, config.isNoFlood());
56         portConfigMap.put(5, config.isNoFwd());
57         portConfigMap.put(6, config.isNoPacketIn());
58         
59         configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
60         return configBitmask;
61     }
62     
63     private static int createPortFeaturesBitmask(PortFeaturesV10 feature) {
64         int configBitmask = 0;
65         Map<Integer, Boolean> portFeaturesMap = new HashMap<>();
66         portFeaturesMap.put(0, feature.is_10mbHd());
67         portFeaturesMap.put(1, feature.is_10mbFd());
68         portFeaturesMap.put(2, feature.is_100mbHd());
69         portFeaturesMap.put(3, feature.is_100mbFd());
70         portFeaturesMap.put(4, feature.is_1gbHd());
71         portFeaturesMap.put(5, feature.is_1gbFd());
72         portFeaturesMap.put(6, feature.is_10gbFd());
73         portFeaturesMap.put(7, feature.isCopper());
74         portFeaturesMap.put(8, feature.isFiber());
75         portFeaturesMap.put(9, feature.isAutoneg());
76         portFeaturesMap.put(10, feature.isPause());
77         portFeaturesMap.put(11, feature.isPauseAsym());
78         configBitmask = ByteBufUtils.fillBitMaskFromMap(portFeaturesMap);
79         return configBitmask;
80     }
81
82 }