Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / multipart / MultipartRequestTableFeaturesSerializer.java
1 /*
2  * Copyright (c) 2017 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 package org.opendaylight.openflowplugin.impl.protocol.serialization.multipart;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import java.nio.charset.StandardCharsets;
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.protocol.api.keys.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowjava.util.ByteBufUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.request.multipart.request.body.MultipartRequestTableFeatures;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.TableFeaturePropType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.TableProperties;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.table.properties.TableFeatureProperties;
25
26 public class MultipartRequestTableFeaturesSerializer implements OFSerializer<MultipartRequestTableFeatures>,
27         SerializerRegistryInjector {
28     private static final byte PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY = 5;
29
30     private SerializerRegistry registry = null;
31
32     @Override
33     public void serialize(final MultipartRequestTableFeatures multipartRequestTableFeatures, final ByteBuf byteBuf) {
34         for (TableFeatures tableFeature : multipartRequestTableFeatures.nonnullTableFeatures().values()) {
35             final int featureIndex = byteBuf.writerIndex();
36             byteBuf.writeShort(EncodeConstants.EMPTY_LENGTH);
37             byteBuf.writeByte(tableFeature.getTableId().byteValue());
38             byteBuf.writeZero(PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY);
39
40             final byte[] nameBytes = tableFeature.getName().getBytes(StandardCharsets.UTF_8);
41             byteBuf.writeBytes(nameBytes);
42             byteBuf.writeZero(32 - nameBytes.length);
43             // FIXME: use writeUint() instead of .longValue()/intValue() here
44             byteBuf.writeLong(tableFeature.getMetadataMatch().longValue());
45             byteBuf.writeLong(tableFeature.getMetadataWrite().longValue());
46             byteBuf.writeInt(ByteBufUtils.fillBitMask(0, tableFeature.getConfig().getDEPRECATEDMASK()));
47             byteBuf.writeInt(tableFeature.getMaxEntries().intValue());
48             serializeProperties(tableFeature.getTableProperties(), byteBuf);
49             byteBuf.setShort(featureIndex, byteBuf.writerIndex() - featureIndex);
50         }
51     }
52
53     @SuppressWarnings("unchecked")
54     private void serializeProperties(final TableProperties tableProperties, final ByteBuf byteBuf) {
55         if (tableProperties != null) {
56             for (TableFeatureProperties property : tableProperties.nonnullTableFeatureProperties().values()) {
57                 final Class<? extends TableFeaturePropType> clazz =
58                     (Class<? extends TableFeaturePropType>) property.getTableFeaturePropType().implementedInterface();
59
60                 registry.<TableFeaturePropType, OFSerializer<TableFeaturePropType>>getSerializer(
61                         new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_3, clazz))
62                             .serialize(property.getTableFeaturePropType(), byteBuf);
63             }
64         }
65     }
66
67     @Override
68     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
69         registry = requireNonNull(serializerRegistry);
70     }
71 }