2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.protocol.serialization.multipart;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import java.nio.charset.StandardCharsets;
14 import java.util.Objects;
15 import java.util.Optional;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.request.multipart.request.body.MultipartRequestTableFeatures;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.TableFeaturePropType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.TableProperties;
26 public class MultipartRequestTableFeaturesSerializer implements OFSerializer<MultipartRequestTableFeatures>,
27 SerializerRegistryInjector {
29 private static final byte PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY = 5;
30 private SerializerRegistry registry;
33 public void serialize(final MultipartRequestTableFeatures multipartRequestTableFeatures, final ByteBuf byteBuf) {
35 .ofNullable(multipartRequestTableFeatures.nonnullTableFeatures())
36 .ifPresent(tableFeatures -> tableFeatures.values()
38 .filter(Objects::nonNull)
39 .forEach(tableFeature -> {
40 final int featureIndex = byteBuf.writerIndex();
41 byteBuf.writeShort(EncodeConstants.EMPTY_LENGTH);
42 byteBuf.writeByte(tableFeature.getTableId().byteValue());
43 byteBuf.writeZero(PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY);
44 byteBuf.writeBytes(tableFeature.getName().getBytes(StandardCharsets.UTF_8));
45 byteBuf.writeZero(32 - tableFeature.getName().getBytes(StandardCharsets.UTF_8).length);
46 byteBuf.writeLong(tableFeature.getMetadataMatch().longValue());
47 byteBuf.writeLong(tableFeature.getMetadataWrite().longValue());
48 byteBuf.writeInt(ByteBufUtils.fillBitMask(0, tableFeature.getConfig().isDEPRECATEDMASK()));
49 byteBuf.writeInt(tableFeature.getMaxEntries().intValue());
50 serializeProperties(tableFeature.getTableProperties(), byteBuf);
51 byteBuf.setShort(featureIndex, byteBuf.writerIndex() - featureIndex);
55 @SuppressWarnings("unchecked")
56 private void serializeProperties(final TableProperties tableProperties, final ByteBuf byteBuf) {
58 .ofNullable(tableProperties)
59 .flatMap(properties -> Optional.ofNullable(properties.nonnullTableFeatureProperties()))
60 .ifPresent(properties -> properties.values()
62 .filter(Objects::nonNull)
63 .forEach(property -> {
64 final Class<? extends TableFeaturePropType> clazz = (Class<? extends TableFeaturePropType>) property
65 .getTableFeaturePropType()
66 .implementedInterface();
68 Preconditions.checkNotNull(registry)
69 .<TableFeaturePropType, OFSerializer<TableFeaturePropType>>getSerializer(
70 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, clazz))
71 .serialize(property.getTableFeaturePropType(), byteBuf);
76 public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
77 registry = serializerRegistry;