Merge "Fix checkstyle warnings for impl/karaf, lifecycle, common, mastership"
[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>,
26         SerializerRegistryInjector {
27
28     private static final byte PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY = 5;
29     private SerializerRegistry registry;
30
31     @Override
32     public void serialize(final MultipartRequestBody multipartRequestBody, final ByteBuf byteBuf) {
33         final MultipartRequestTableFeatures multipartRequestTableFeatures = MultipartRequestTableFeatures
34             .class
35             .cast(multipartRequestBody);
36
37         Optional
38             .ofNullable(multipartRequestTableFeatures.getTableFeatures())
39             .ifPresent(tableFeatures -> tableFeatures
40                 .stream()
41                 .filter(Objects::nonNull)
42                 .forEach(tableFeature -> {
43                     final int featureIndex = byteBuf.writerIndex();
44                     byteBuf.writeShort(EncodeConstants.EMPTY_LENGTH);
45                     byteBuf.writeByte(tableFeature.getTableId().byteValue());
46                     byteBuf.writeZero(PADDING_IN_MULTIPART_REQUEST_TABLE_FEATURES_BODY);
47                     byteBuf.writeBytes(tableFeature.getName().getBytes());
48                     byteBuf.writeZero(32 - tableFeature.getName().getBytes().length);
49                     byteBuf.writeLong(tableFeature.getMetadataMatch().longValue());
50                     byteBuf.writeLong(tableFeature.getMetadataWrite().longValue());
51                     byteBuf.writeInt(ByteBufUtils.fillBitMask(0, tableFeature.getConfig().isDEPRECATEDMASK()));
52                     byteBuf.writeInt(tableFeature.getMaxEntries().intValue());
53                     serializeProperties(tableFeature.getTableProperties(), byteBuf);
54                     byteBuf.setShort(featureIndex, byteBuf.writerIndex() - featureIndex);
55                 }));
56     }
57
58     @SuppressWarnings("unchecked")
59     private void serializeProperties(final TableProperties tableProperties, final ByteBuf byteBuf) {
60         Optional
61             .ofNullable(tableProperties)
62             .flatMap(properties -> Optional.ofNullable(properties.getTableFeatureProperties()))
63             .ifPresent(properties -> properties
64                 .stream()
65                 .filter(Objects::nonNull)
66                 .forEach(property -> {
67                     final Class<? extends TableFeaturePropType> clazz = (Class<? extends TableFeaturePropType>) property
68                         .getTableFeaturePropType()
69                         .getImplementedInterface();
70
71                     registry.<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 }