Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / TableModInputMessageFactory.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.impl.serialization.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableConfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
20
21 /**
22  * Translates TableMod messages
23  * @author timotej.kubas
24  * @author michal.polkorab
25  */
26 public class TableModInputMessageFactory implements OFSerializer<TableModInput> {
27     private static final byte MESSAGE_TYPE = 17;
28     private static final byte PADDING_IN_TABLE_MOD_MESSAGE = 3;
29     private static final int MESSAGE_LENGTH = 16;
30     private static TableModInputMessageFactory instance;
31     
32     private TableModInputMessageFactory() {
33         // just singleton
34     }
35     
36     /**
37      * @return singleton factory
38      */
39     public static synchronized TableModInputMessageFactory getInstance() {
40         if(instance == null){
41             instance = new TableModInputMessageFactory();
42         }
43         return instance;
44     }
45     
46     @Override
47     public void messageToBuffer(short version, ByteBuf out, TableModInput message) {
48         ByteBufUtils.writeOFHeader(instance, message, out);
49         out.writeByte(message.getTableId().getValue().byteValue());
50         ByteBufUtils.padBuffer(PADDING_IN_TABLE_MOD_MESSAGE, out);
51         out.writeInt(createConfigBitmask(message.getConfig()));
52     }
53
54     @Override
55     public int computeLength(TableModInput message) {
56         return MESSAGE_LENGTH;
57     }
58
59     @Override
60     public byte getMessageType() {
61         return MESSAGE_TYPE;
62     }
63     
64     /**
65      * @param tableConfig
66      * @return port config bitmask 
67      */
68     private static int createConfigBitmask(TableConfig tableConfig) {
69         Map<Integer, Boolean> portConfigMap = new HashMap<>();
70         portConfigMap.put(3, tableConfig.isOFPTCDEPRECATEDMASK());
71         int configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
72         return configBitmask;
73     }
74 }