Update MRI projects for Aluminium
[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
9 package org.opendaylight.openflowplugin.impl.protocol.serialization.multipart;
10
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;
25
26 public class MultipartRequestTableFeaturesSerializer implements OFSerializer<MultipartRequestTableFeatures>,
27         SerializerRegistryInjector {
28
29     private static final byte PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY = 5;
30     private SerializerRegistry registry;
31
32     @Override
33     public void serialize(final MultipartRequestTableFeatures multipartRequestTableFeatures, final ByteBuf byteBuf) {
34         Optional
35             .ofNullable(multipartRequestTableFeatures.nonnullTableFeatures())
36             .ifPresent(tableFeatures -> tableFeatures.values()
37                 .stream()
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);
52                 }));
53     }
54
55     @SuppressWarnings("unchecked")
56     private void serializeProperties(final TableProperties tableProperties, final ByteBuf byteBuf) {
57         Optional
58             .ofNullable(tableProperties)
59             .flatMap(properties -> Optional.ofNullable(properties.nonnullTableFeatureProperties()))
60             .ifPresent(properties -> properties.values()
61                 .stream()
62                 .filter(Objects::nonNull)
63                 .forEach(property -> {
64                     final Class<? extends TableFeaturePropType> clazz = (Class<? extends TableFeaturePropType>) property
65                         .getTableFeaturePropType()
66                         .implementedInterface();
67
68                     Preconditions.checkNotNull(registry)
69                         .<TableFeaturePropType, OFSerializer<TableFeaturePropType>>getSerializer(
70                             new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, clazz))
71                                 .serialize(property.getTableFeaturePropType(), byteBuf);
72                 }));
73     }
74
75     @Override
76     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
77         registry = serializerRegistry;
78     }
79 }