Updated extension registration keys
[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 org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMaker;
20 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory;
21 import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.v10.grouping.MatchV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
26
27 /**
28  * Translates FlowMod messages
29  * @author michal.polkorab
30  */
31 public class OF10FlowModInputMessageFactory implements OFSerializer<FlowModInput>, SerializerRegistryInjector {
32
33     private static final byte MESSAGE_TYPE = 14;
34     private static final TypeKeyMaker<Action> ACTION_KEY_MAKER =
35             TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF10_VERSION_ID);
36     private SerializerRegistry registry;
37
38     @Override
39     public void serialize(final FlowModInput message, final ByteBuf outBuffer) {
40         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
41         OFSerializer<MatchV10> matchSerializer = registry.getSerializer(new MessageTypeKey<>(
42                 message.getVersion(), MatchV10.class));
43         matchSerializer.serialize(message.getMatchV10(), outBuffer);
44         outBuffer.writeLong(message.getCookie().longValue());
45         outBuffer.writeShort(message.getCommand().getIntValue());
46         outBuffer.writeShort(message.getIdleTimeout().intValue());
47         outBuffer.writeShort(message.getHardTimeout().intValue());
48         outBuffer.writeShort(message.getPriority());
49         outBuffer.writeInt(message.getBufferId().intValue());
50         outBuffer.writeShort(message.getOutPort().getValue().intValue());
51         outBuffer.writeShort(createFlowModFlagsBitmask(message.getFlagsV10()));
52         ListSerializer.serializeList(message.getAction(), ACTION_KEY_MAKER, registry, outBuffer);
53         ByteBufUtils.updateOFHeaderLength(outBuffer);
54     }
55
56     private static int createFlowModFlagsBitmask(final FlowModFlagsV10 flags) {
57         return ByteBufUtils.fillBitMask(0,
58                 flags.isOFPFFSENDFLOWREM(),
59                 flags.isOFPFFCHECKOVERLAP(),
60                 flags.isOFPFFEMERG());
61     }
62
63     @Override
64     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
65         this.registry = serializerRegistry;
66     }
67 }