Copyright update
[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.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.PortConfigV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
21
22 /**
23  * Translates PortMod messages
24  * @author michal.polkorab
25  */
26 public class OF10PortModInputMessageFactory implements OFSerializer<PortModInput> {
27
28     private static final byte MESSAGE_TYPE = 15;
29     private static final byte PADDING_IN_PORT_MOD_MESSAGE = 4;
30     private static final int MESSAGE_LENGTH = 32;
31     
32     private static OF10PortModInputMessageFactory instance;
33     
34     private OF10PortModInputMessageFactory() {
35         // singleton
36     }
37     
38     /**
39      * @return singleton factory
40      */
41     public static synchronized OF10PortModInputMessageFactory getInstance() {
42         if (instance == null) {
43             instance = new OF10PortModInputMessageFactory();
44         }
45         return instance;
46     }
47     
48     @Override
49     public void messageToBuffer(short version, ByteBuf out, PortModInput message) {
50         ByteBufUtils.writeOFHeader(instance, message, out);
51         out.writeShort(message.getPortNo().getValue().intValue());
52         out.writeBytes(ByteBufUtils.macAddressToBytes(message.getHwAddress().getValue()));
53         out.writeInt(createPortConfigBitmask(message.getConfigV10()));
54         out.writeInt(createPortConfigBitmask(message.getMaskV10()));
55         out.writeInt(createPortFeaturesBitmask(message.getAdvertiseV10()));
56         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE, out);
57     }
58
59     @Override
60     public int computeLength(PortModInput message) {
61         return MESSAGE_LENGTH;
62     }
63
64     @Override
65     public byte getMessageType() {
66         return MESSAGE_TYPE;
67     }
68    
69     
70     /**
71      * @param config
72      * @return port config bitmask 
73      */
74     private static int createPortConfigBitmask(PortConfigV10 config) {
75         int configBitmask = 0;
76         Map<Integer, Boolean> portConfigMap = new HashMap<>();
77         portConfigMap.put(0, config.isPortDown());
78         portConfigMap.put(1, config.isNoStp());
79         portConfigMap.put(2, config.isNoRecv());
80         portConfigMap.put(3, config.isNoRecvStp());
81         portConfigMap.put(4, config.isNoFlood());
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(PortFeaturesV10 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.isCopper());
100         portFeaturesMap.put(8, feature.isFiber());
101         portFeaturesMap.put(9, feature.isAutoneg());
102         portFeaturesMap.put(10, feature.isPause());
103         portFeaturesMap.put(11, feature.isPauseAsym());
104         configBitmask = ByteBufUtils.fillBitMaskFromMap(portFeaturesMap);
105         return configBitmask;
106     }
107 }