Extensibility refactoring
[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.SerializerRegistryInjector;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
21 import org.opendaylight.openflowjava.protocol.impl.util.CodingUtils;
22 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
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         OFSerializer<Instruction> instructionSerializer =
56                 registry.getSerializer(new MessageTypeKey<>(message.getVersion(), Instruction.class));
57         CodingUtils.serializeList(message.getInstruction(), instructionSerializer, outBuffer);
58         ByteBufUtils.updateOFHeaderLength(outBuffer);
59     }
60
61     @Override
62     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
63         this.registry = serializerRegistry;
64     }
65
66     private static int createFlowModFlagsBitmask(FlowModFlags flags) {
67         int flowModFlagBitmask = 0;
68         Map<Integer, Boolean> flowModFlagsMap = new HashMap<>();
69         flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM());
70         flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP());
71         flowModFlagsMap.put(2, flags.isOFPFFRESETCOUNTS());
72         flowModFlagsMap.put(3, flags.isOFPFFNOPKTCOUNTS());
73         flowModFlagsMap.put(4, flags.isOFPFFNOBYTCOUNTS());
74         
75         flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);
76         return flowModFlagBitmask;
77     }
78
79 }