Add methods that allows registering any serializer
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / experimenter / BundleAddMessageFactory.java
1 /*
2  * Copyright (c) 2016 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.experimenter;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.BundleProperty;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.BundleAddMessage;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.bundle.add.message.Message;
19 import org.opendaylight.yangtools.yang.binding.DataContainer;
20
21 /**
22  * Translates BundleAddMessage messages (OpenFlow v1.3 extension #230).
23  */
24 public class BundleAddMessageFactory extends AbstractBundleMessageFactory<BundleAddMessage> {
25
26     @Override
27     public void serialize(BundleAddMessage input, ByteBuf outBuffer) {
28         outBuffer.writeInt(input.getBundleId().getValue().intValue());
29         outBuffer.writeZero(2);
30         writeBundleFlags(input.getFlags(), outBuffer);
31
32         int msgStart = outBuffer.writerIndex();
33         serializeInnerMessage(input.getMessage(), outBuffer, input.getMessage().getImplementedInterface());
34         int msgLength = outBuffer.writerIndex() - msgStart;
35
36         List<BundleProperty> bundleProperties = input.getBundleProperty();
37         if (bundleProperties != null && !bundleProperties.isEmpty()) {
38             outBuffer.writeZero(paddingNeeded(msgLength));
39             writeBundleProperties(input.getBundleProperty(), outBuffer);
40         }
41     }
42
43     private <T extends DataContainer> void serializeInnerMessage(final Message innerMessage, final ByteBuf outBuffer,
44                                                                  final Class<T> clazz) {
45         OFSerializer<T> serializer = serializerRegistry.getSerializer(
46                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, clazz));
47         serializer.serialize((T)innerMessage, outBuffer);
48     }
49
50     private static int paddingNeeded(final int length) {
51         int paddingRemainder = length % EncodeConstants.PADDING;
52         return (paddingRemainder != 0) ? (EncodeConstants.PADDING - paddingRemainder) : 0;
53     }
54
55 }