Add multipart request message serializers
[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 io.netty.buffer.ByteBuf;
12 import java.util.Objects;
13 import java.util.Optional;
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.multipart.types.rev170112.multipart.request.MultipartRequestBody;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.request.multipart.request.body.MultipartRequestTableFeatures;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.feature.prop.type.TableFeaturePropType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.table.features.TableProperties;
24
25 public class MultipartRequestTableFeaturesSerializer implements OFSerializer<MultipartRequestBody>, SerializerRegistryInjector {
26
27     private static final byte PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY = 5;
28     private SerializerRegistry registry;
29
30     @Override
31     public void serialize(final MultipartRequestBody multipartRequestBody, final ByteBuf byteBuf) {
32         final MultipartRequestTableFeatures multipartRequestTableFeatures = MultipartRequestTableFeatures
33             .class
34             .cast(multipartRequestBody);
35
36         Optional
37             .ofNullable(multipartRequestTableFeatures.getTableFeatures())
38             .ifPresent(tableFeatures -> tableFeatures
39                 .stream()
40                 .filter(Objects::nonNull)
41                 .forEach(tableFeature -> {
42                     final int featureIndex = byteBuf.writerIndex();
43                     byteBuf.writeShort(EncodeConstants.EMPTY_LENGTH);
44                     byteBuf.writeByte(tableFeature.getTableId().byteValue());
45                     byteBuf.writeZero(PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY);
46                     byteBuf.writeBytes(tableFeature.getName().getBytes());
47                     byteBuf.writeZero(32 - tableFeature.getName().getBytes().length);
48                     byteBuf.writeLong(tableFeature.getMetadataMatch().longValue());
49                     byteBuf.writeLong(tableFeature.getMetadataWrite().longValue());
50                     byteBuf.writeInt(ByteBufUtils.fillBitMask(0, tableFeature.getConfig().isDEPRECATEDMASK()));
51                     byteBuf.writeInt(tableFeature.getMaxEntries().intValue());
52                     serializeProperties(tableFeature.getTableProperties(), byteBuf);
53                     byteBuf.setShort(featureIndex, byteBuf.writerIndex() - featureIndex);
54                 }));
55     }
56
57     @SuppressWarnings("unchecked")
58     private void serializeProperties(final TableProperties tableProperties, final ByteBuf byteBuf) {
59         Optional
60             .ofNullable(tableProperties)
61             .flatMap(properties -> Optional.ofNullable(properties.getTableFeatureProperties()))
62             .ifPresent(properties -> properties
63                 .stream()
64                 .filter(Objects::nonNull)
65                 .forEach(property -> {
66                     final Class<? extends TableFeaturePropType> clazz = (Class<? extends TableFeaturePropType>) property
67                         .getTableFeaturePropType()
68                         .getImplementedInterface();
69
70                     registry.
71                         <TableFeaturePropType, OFSerializer<TableFeaturePropType>>getSerializer(
72                             new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, clazz))
73                         .serialize(property.getTableFeaturePropType(), byteBuf);
74                 }));
75     }
76
77     @Override
78     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
79         registry = serializerRegistry;
80     }
81 }