Merge "Fix checkstyle warnings for impl/protocol test package"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / actions / SetFieldActionSerializer.java
1 /*
2  * Copyright (c) 2016 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.actions;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
15 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.impl.util.ActionConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetField;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
22
23 public class SetFieldActionSerializer extends AbstractActionSerializer implements SerializerRegistryInjector {
24     private SerializerRegistry registry;
25
26     @Override
27     public void serialize(Action action, ByteBuf outBuffer) {
28         // Serialize field type and save position
29         final int startIndex = outBuffer.writerIndex();
30         outBuffer.writeShort(getType());
31         final int lengthIndex = outBuffer.writerIndex();
32         outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
33
34         // Serialize match (using small workaround with serializeHeader method to serialize only match entries)
35         final SetField setField = SetFieldCase.class.cast(action).getSetField();
36         final HeaderSerializer<Match> serializer = registry
37                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, Match.class));
38         serializer.serializeHeader(setField, outBuffer);
39
40         // Serialize padding based on match length
41         int paddingRemainder = (outBuffer.writerIndex() - startIndex) % EncodeConstants.PADDING;
42         if (paddingRemainder != 0) {
43             outBuffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
44         }
45         outBuffer.setShort(lengthIndex, outBuffer.writerIndex() - startIndex);
46     }
47
48     @Override
49     protected int getLength() {
50         return ActionConstants.GENERAL_ACTION_LENGTH;
51     }
52
53     @Override
54     protected int getType() {
55         return ActionConstants.SET_FIELD_CODE;
56     }
57
58     @Override
59     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
60         registry = serializerRegistry;
61     }
62 }