Add methods that allows registering any serializer
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / experimenter / AbstractBundleMessageFactory.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.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.openflowjava.util.ExperimenterSerializerKeyFactory;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleFlags;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundlePropertyType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.BundleProperty;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.bundle.property.bundle.property.entry.BundleExperimenterProperty;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.bundle.property.bundle.property.entry.bundle.experimenter.property.BundleExperimenterPropertyData;
24 import org.opendaylight.yangtools.yang.binding.DataContainer;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Abstract class for common stuff of bundle messages.
30  */
31 public abstract class AbstractBundleMessageFactory<T extends DataContainer> implements OFSerializer<T>,
32         SerializerRegistryInjector {
33
34     private static final Logger LOG = LoggerFactory.getLogger(AbstractBundleMessageFactory.class);
35     protected SerializerRegistry serializerRegistry;
36
37     @Override
38     public void serialize(T input, ByteBuf outBuffer) {
39         // to be extended
40     }
41
42     @Override
43     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
44         this.serializerRegistry = serializerRegistry;
45     }
46
47     protected static void writeBundleFlags(final BundleFlags bundleFlags, final ByteBuf outBuffer) {
48         int flagsBitMap = ByteBufUtils.fillBitMask(0, bundleFlags.isAtomic(), bundleFlags.isOrdered());
49         outBuffer.writeShort(flagsBitMap);
50     }
51
52     protected void writeBundleProperties(final List<BundleProperty> properties, final ByteBuf outBuffer) {
53         for (BundleProperty property : properties) {
54             BundlePropertyType type = property.getType();
55             if (type != null && type.equals(BundlePropertyType.ONFETBPTEXPERIMENTER)) {
56                 int startIndex = outBuffer.writerIndex();
57                 outBuffer.writeShort(type.getIntValue());
58                 int lengthIndex = outBuffer.writerIndex();
59                 outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
60                 writeBundleExperimenterProperty(property, outBuffer);
61                 outBuffer.setShort(lengthIndex, outBuffer.writerIndex() - startIndex);
62             } else {
63                 LOG.warn("lTrying to serialize unknown bundle property (type: {}), skipping", type.getIntValue() );
64             }
65         }
66     }
67
68     protected void writeBundleExperimenterProperty(final BundleProperty bundleProperty, final ByteBuf outBuffer) {
69         BundleExperimenterProperty property = (BundleExperimenterProperty) bundleProperty.getBundlePropertyEntry();
70         int experimenterId = property.getExperimenter().getValue().intValue();
71         int expType = property.getExpType().intValue();
72         outBuffer.writeInt(experimenterId);
73         outBuffer.writeInt(expType);
74         OFSerializer<BundleExperimenterPropertyData> serializer = serializerRegistry.getSerializer(
75                 ExperimenterSerializerKeyFactory.createBundlePropertySerializerKey(EncodeConstants.OF13_VERSION_ID,
76                         experimenterId, expType));
77         serializer.serialize(property.getBundleExperimenterPropertyData(), outBuffer);
78     }
79
80 }