Merge "Clean up instance checks and casts"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / actions / OutputActionSerializer.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 com.google.common.base.MoreObjects;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.openflowjava.protocol.impl.util.ActionConstants;
14 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputAction;
18
19 public class OutputActionSerializer extends AbstractActionSerializer<OutputActionCase> {
20
21     @Override
22     public void serialize(OutputActionCase action, ByteBuf outBuffer) {
23         super.serialize(action, outBuffer);
24         final OutputAction outputAction = action.getOutputAction();
25         Long value = InventoryDataServiceUtil.portNumberfromNodeConnectorId(
26                 OpenflowVersion.OF13,
27                 outputAction.getOutputNodeConnector().getValue());
28         if (value == null) {
29             throw new IllegalArgumentException("Not a valid port number: "
30                     + outputAction.getOutputNodeConnector().getValue());
31         }
32         outBuffer.writeInt(value.intValue());
33         outBuffer.writeShort(MoreObjects.firstNonNull(outputAction.getMaxLength(), 0));
34         outBuffer.writeZero(ActionConstants.OUTPUT_PADDING);
35     }
36
37     @Override
38     protected int getLength() {
39         return ActionConstants.LARGER_ACTION_LENGTH;
40     }
41
42     @Override
43     protected int getType() {
44         return ActionConstants.OUTPUT_CODE;
45     }
46 }