Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10FlowModInputMessageFactory.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.OF10ActionsSerializer;
19 import org.opendaylight.openflowjava.protocol.impl.util.OF10MatchSerializer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
22
23 /**
24  * Translates FlowMod messages
25  * @author michal.polkorab
26  */
27 public class OF10FlowModInputMessageFactory implements OFSerializer<FlowModInput> {
28
29     private static final byte MESSAGE_TYPE = 14;
30     private static final int MESSAGE_LENGTH = 72;
31     
32     private static OF10FlowModInputMessageFactory instance;
33    
34     private OF10FlowModInputMessageFactory() {
35         // singleton
36     }
37     
38     /**
39      * @return singleton factory
40      */
41     public static synchronized OF10FlowModInputMessageFactory getInstance() {
42         if(instance == null) {
43             instance = new OF10FlowModInputMessageFactory();
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         OF10MatchSerializer.encodeMatchV10(out, message.getMatchV10());
52         out.writeLong(message.getCookie().longValue());
53         out.writeShort(message.getCommand().getIntValue());
54         out.writeShort(message.getIdleTimeout().intValue());
55         out.writeShort(message.getHardTimeout().intValue());
56         out.writeShort(message.getPriority());
57         out.writeInt(message.getBufferId().intValue());
58         out.writeShort(message.getOutPort().getValue().intValue());
59         out.writeShort(createFlowModFlagsBitmask(message.getFlagsV10()));
60         OF10ActionsSerializer.encodeActionsV10(out, message.getActionsList());
61     }
62
63     @Override
64     public int computeLength(FlowModInput message) {
65         return MESSAGE_LENGTH + OF10ActionsSerializer.computeActionsLength(message.getActionsList());
66     }
67
68     @Override
69     public byte getMessageType() {
70         return MESSAGE_TYPE;
71     }
72
73     private static int createFlowModFlagsBitmask(FlowModFlagsV10 flags) {
74         int flowModFlagBitmask = 0;
75         Map<Integer, Boolean> flowModFlagsMap = new HashMap<>();
76         flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM());
77         flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP());
78         flowModFlagsMap.put(2, flags.isOFPFFEMERG());
79         flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);
80         return flowModFlagBitmask;
81     }
82 }