Merge "Fix checkstyle warnings for impl/protocol package"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / instructions / AbstractActionInstructionSerializer.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.instructions;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.Optional;
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.util.EncodeConstants;
16 import org.opendaylight.openflowjava.protocol.impl.util.InstructionConstants;
17 import org.opendaylight.openflowplugin.impl.protocol.serialization.util.ActionUtil;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.OrderComparator;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.ActionList;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction;
21
22 public abstract class AbstractActionInstructionSerializer extends AbstractInstructionSerializer implements
23         SerializerRegistryInjector {
24
25     private SerializerRegistry registry;
26
27     @Override
28     public void serialize(Instruction input, ByteBuf outBuffer) {
29         outBuffer.writeShort(getType());
30     }
31
32     /**
33      * Try to write list of OpenFlowPlugin actions to output buffer.
34      *
35      * @param actions List of OpenFlowPlugin actions
36      * @param outBuffer output buffer
37      * @param startIndex start index of byte buffer
38      */
39     protected void writeActions(ActionList actions, short version, ByteBuf outBuffer, int startIndex) {
40         Optional.ofNullable(actions).flatMap(as -> Optional.ofNullable(as.getAction())).map(as -> {
41             final int lengthIndex = outBuffer.writerIndex();
42             outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
43             outBuffer.writeZero(InstructionConstants.PADDING_IN_ACTIONS_INSTRUCTION);
44             as.stream().sorted(OrderComparator.build()).forEach(a -> ActionUtil
45                     .writeAction(a.getAction(), version, registry, outBuffer));
46             outBuffer.setShort(lengthIndex, outBuffer.writerIndex() - startIndex);
47             return actions;
48         }).orElseGet(() -> {
49             outBuffer.writeShort(getLength());
50             outBuffer.writeZero(InstructionConstants.PADDING_IN_ACTIONS_INSTRUCTION);
51             return actions;
52         });
53     }
54
55     @Override
56     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
57         registry = serializerRegistry;
58     }
59
60 }