Merge "Bumped to SNAPSHOT dependencies - some dependencies moved to <dependencyManag...
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / FlowModInputMessageFactory.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.openflowjava.protocol.impl.util.InstructionsSerializer;
19 import org.opendaylight.openflowjava.protocol.impl.util.MatchSerializer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
22
23 /**
24  * Translates FlowMod messages
25  * @author timotej.kubas
26  * @author michal.polkorab
27  */
28 public class FlowModInputMessageFactory implements OFSerializer<FlowModInput> {
29     private static final byte MESSAGE_TYPE = 14;
30     private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
31     private static final int MESSAGE_LENGTH = 48;
32     private static FlowModInputMessageFactory instance;
33    
34     private FlowModInputMessageFactory() {
35         // singleton
36     }
37     
38     /**
39      * @return singleton factory
40      */
41     public static synchronized FlowModInputMessageFactory getInstance() {
42         if(instance == null) {
43             instance = new FlowModInputMessageFactory();
44         }
45         return instance;
46     }
47     
48     @Override
49     public void messageToBuffer(short version, ByteBuf out, FlowModInput message) {
50         ByteBufUtils.writeOFHeader(instance, message, out);
51         out.writeLong(message.getCookie().longValue());
52         out.writeLong(message.getCookieMask().longValue());
53         out.writeByte(message.getTableId().getValue().byteValue());
54         out.writeByte(message.getCommand().getIntValue());
55         out.writeShort(message.getIdleTimeout().intValue());
56         out.writeShort(message.getHardTimeout().intValue());
57         out.writeShort(message.getPriority());
58         out.writeInt(message.getBufferId().intValue());
59         out.writeInt(message.getOutPort().getValue().intValue());
60         out.writeInt(message.getOutGroup().intValue());
61         out.writeShort(createFlowModFlagsBitmask(message.getFlags()));
62         ByteBufUtils.padBuffer(PADDING_IN_FLOW_MOD_MESSAGE, out);
63         MatchSerializer.encodeMatch(message.getMatch(), out);
64         InstructionsSerializer.encodeInstructions(message.getInstruction(), out);
65     }
66
67     @Override
68     public int computeLength(FlowModInput message) {
69         return MESSAGE_LENGTH + MatchSerializer.computeMatchLength(message.getMatch())
70                 + InstructionsSerializer.computeInstructionsLength(message.getInstruction());
71     }
72
73     @Override
74     public byte getMessageType() {
75         return MESSAGE_TYPE;
76     }
77
78     private static int createFlowModFlagsBitmask(FlowModFlags flags) {
79         int flowModFlagBitmask = 0;
80         Map<Integer, Boolean> flowModFlagsMap = new HashMap<>();
81         flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM());
82         flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP());
83         flowModFlagsMap.put(2, flags.isOFPFFRESETCOUNTS());
84         flowModFlagsMap.put(3, flags.isOFPFFNOPKTCOUNTS());
85         flowModFlagsMap.put(4, flags.isOFPFFNOBYTCOUNTS());
86         
87         flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);
88         return flowModFlagBitmask;
89     }
90     
91     
92 }