eb343d43eee1cbc9e96c40e99b735c007dec30b2
[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.api.extensibility.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
20 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
21 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
22 import org.opendaylight.openflowjava.protocol.impl.util.EnhancedTypeKeyMakerFactory;
23 import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
27
28 /**
29  * Translates FlowMod messages
30  * @author timotej.kubas
31  * @author michal.polkorab
32  */
33 public class FlowModInputMessageFactory implements OFSerializer<FlowModInput>, SerializerRegistryInjector {
34     private static final byte MESSAGE_TYPE = 14;
35     private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
36     private SerializerRegistry registry;
37
38     @Override
39     public void serialize(FlowModInput message, ByteBuf outBuffer) {
40         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
41         outBuffer.writeLong(message.getCookie().longValue());
42         outBuffer.writeLong(message.getCookieMask().longValue());
43         outBuffer.writeByte(message.getTableId().getValue().byteValue());
44         outBuffer.writeByte(message.getCommand().getIntValue());
45         outBuffer.writeShort(message.getIdleTimeout().intValue());
46         outBuffer.writeShort(message.getHardTimeout().intValue());
47         outBuffer.writeShort(message.getPriority());
48         outBuffer.writeInt(message.getBufferId().intValue());
49         outBuffer.writeInt(message.getOutPort().getValue().intValue());
50         outBuffer.writeInt(message.getOutGroup().intValue());
51         outBuffer.writeShort(createFlowModFlagsBitmask(message.getFlags()));
52         ByteBufUtils.padBuffer(PADDING_IN_FLOW_MOD_MESSAGE, outBuffer);
53         registry.<Match, OFSerializer<Match>>getSerializer(new MessageTypeKey<>(message.getVersion(), Match.class))
54             .serialize(message.getMatch(), outBuffer);
55         ListSerializer.serializeList(message.getInstruction(), EnhancedTypeKeyMakerFactory
56                 .createInstructionKeyBuilder(EncodeConstants.OF13_VERSION_ID), registry, outBuffer);
57         ByteBufUtils.updateOFHeaderLength(outBuffer);
58     }
59
60     @Override
61     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
62         this.registry = serializerRegistry;
63     }
64
65     private static int createFlowModFlagsBitmask(FlowModFlags flags) {
66         int flowModFlagBitmask = 0;
67         Map<Integer, Boolean> flowModFlagsMap = new HashMap<>();
68         flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM());
69         flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP());
70         flowModFlagsMap.put(2, flags.isOFPFFRESETCOUNTS());
71         flowModFlagsMap.put(3, flags.isOFPFFNOPKTCOUNTS());
72         flowModFlagsMap.put(4, flags.isOFPFFNOBYTCOUNTS());
73         
74         flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);
75         return flowModFlagBitmask;
76     }
77
78 }