Copyright update
[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.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.PortConfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
21
22 /**
23  * Translates PortMod messages
24  * @author timotej.kubas
25  * @author michal.polkorab
26  */
27 public class PortModInputMessageFactory implements OFSerializer<PortModInput> {
28     private static final byte MESSAGE_TYPE = 16;
29     private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
30     private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
31     private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
32     private static final int MESSAGE_LENGTH = 40;
33     private static PortModInputMessageFactory instance;
34     
35     private PortModInputMessageFactory() {
36         // singleton
37     }
38     
39     /**
40      * @return singleton factory
41      */
42     public static synchronized PortModInputMessageFactory getInstance() {
43         if (instance == null) {
44             instance = new PortModInputMessageFactory();
45         }
46         return instance;
47     }
48     
49     @Override
50     public void messageToBuffer(short version, ByteBuf out, PortModInput message) {
51         ByteBufUtils.writeOFHeader(instance, message, out);
52         out.writeInt(message.getPortNo().getValue().intValue());
53         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_01, out);
54         out.writeBytes(ByteBufUtils.macAddressToBytes(message.getHwAddress().getValue()));
55         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_02, out);
56         out.writeInt(createPortConfigBitmask(message.getConfig()));
57         out.writeInt(createPortConfigBitmask(message.getMask()));
58         out.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
59         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_03, out);
60     }
61
62     @Override
63     public int computeLength(PortModInput message) {
64         return MESSAGE_LENGTH;
65     }
66
67     @Override
68     public byte getMessageType() {
69         return MESSAGE_TYPE;
70     }
71    
72     
73     /**
74      * @param config
75      * @return port config bitmask 
76      */
77     private static int createPortConfigBitmask(PortConfig config) {
78         int configBitmask = 0;
79         Map<Integer, Boolean> portConfigMap = new HashMap<>();
80         portConfigMap.put(0, config.isPortDown());
81         portConfigMap.put(2, config.isNoRecv());
82         portConfigMap.put(5, config.isNoFwd());
83         portConfigMap.put(6, config.isNoPacketIn());
84         
85         configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
86         return configBitmask;
87     }
88     
89     private static int createPortFeaturesBitmask(PortFeatures feature) {
90         int configBitmask = 0;
91         Map<Integer, Boolean> portFeaturesMap = new HashMap<>();
92         portFeaturesMap.put(0, feature.is_10mbHd());
93         portFeaturesMap.put(1, feature.is_10mbFd());
94         portFeaturesMap.put(2, feature.is_100mbHd());
95         portFeaturesMap.put(3, feature.is_100mbFd());
96         portFeaturesMap.put(4, feature.is_1gbHd());
97         portFeaturesMap.put(5, feature.is_1gbFd());
98         portFeaturesMap.put(6, feature.is_10gbFd());
99         portFeaturesMap.put(7, feature.is_40gbFd());
100         portFeaturesMap.put(8, feature.is_100gbFd());
101         portFeaturesMap.put(9, feature.is_1tbFd());
102         portFeaturesMap.put(10, feature.isOther());
103         portFeaturesMap.put(11, feature.isCopper());
104         portFeaturesMap.put(12, feature.isFiber());
105         portFeaturesMap.put(13, feature.isAutoneg());
106         portFeaturesMap.put(14, feature.isPause());
107         portFeaturesMap.put(15, feature.isPauseAsym());
108         
109         configBitmask = ByteBufUtils.fillBitMaskFromMap(portFeaturesMap);
110         return configBitmask;
111     }
112 }