Fix opendaylight-flow-types.yang cases
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / multipart / MultipartRequestFlowAggregateStatsSerializer.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 com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.MultipartRequestFlowAggregateStats;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.multipart.request.multipart.request.body.multipart.request.flow.aggregate.stats.FlowAggregateStats;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
23
24 public class MultipartRequestFlowAggregateStatsSerializer implements OFSerializer<MultipartRequestFlowAggregateStats>,
25         SerializerRegistryInjector {
26
27     private static final byte PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_01 = 3;
28     private static final byte PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_02 = 4;
29     private SerializerRegistry registry;
30
31     @Override
32     public void serialize(final MultipartRequestFlowAggregateStats multipartRequestFlowAggregateStats,
33             final ByteBuf byteBuf) {
34         final FlowAggregateStats stats = multipartRequestFlowAggregateStats.getFlowAggregateStats();
35         byteBuf.writeByte(MoreObjects.firstNonNull(stats.getTableId(), OFConstants.OFPTT_ALL).byteValue());
36         byteBuf.writeZero(PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_01);
37         byteBuf.writeInt(MoreObjects.firstNonNull(stats.getOutPort(), OFConstants.OFPP_ANY).intValue());
38         byteBuf.writeInt(MoreObjects.firstNonNull(stats.getOutGroup(), OFConstants.OFPG_ANY).intValue());
39         byteBuf.writeZero(PADDING_IN_MULTIPART_REQUEST_FLOW_BODY_02);
40         byteBuf.writeLong(MoreObjects.firstNonNull(stats.getCookie(),
41                 new FlowCookie(OFConstants.DEFAULT_COOKIE)).getValue().longValue());
42         byteBuf.writeLong(MoreObjects.firstNonNull(stats.getCookieMask(),
43                 new FlowCookie(OFConstants.DEFAULT_COOKIE_MASK)).getValue().longValue());
44
45         Preconditions.checkNotNull(registry).<Match, OFSerializer<Match>>getSerializer(
46                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Match.class))
47                 .serialize(stats.getMatch(), byteBuf);
48     }
49
50     @Override
51     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
52         registry = serializerRegistry;
53     }
54 }