2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
10 import com.google.common.collect.Iterables;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.controller.sal.common.util.Arguments;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public final class PacketOutConvertor {
34 private static final Logger LOG = LoggerFactory.getLogger(MeterConvertor.class);
36 private PacketOutConvertor() {
40 // Get all the data for the PacketOut from the Yang/SAL-Layer
43 * @param version openflow version
44 * @param inputPacket input packet
45 * @param datapathid datapath id
47 * @return PacketOutInput required by OF Library
49 public static PacketOutInput toPacketOutInput(final TransmitPacketInput inputPacket, final short version, final Long xid,
50 final BigInteger datapathid) {
52 LOG.trace("toPacketOutInput for datapathId:{}, xid:{}", datapathid, xid);
53 // Build Port ID from TransmitPacketInput.Ingress
54 PortNumber inPortNr = null;
55 Long bufferId = OFConstants.OFP_NO_BUFFER;
56 Iterable<PathArgument> inArgs = null;
57 PacketOutInputBuilder builder = new PacketOutInputBuilder();
58 if (inputPacket.getIngress() != null) {
59 inArgs = inputPacket.getIngress().getValue().getPathArguments();
61 if (inArgs != null && Iterables.size(inArgs) >= 3) {
62 inPortNr = getPortNumber(Iterables.get(inArgs, 2), version);
64 // The packetOut originated from the controller
65 inPortNr = new PortNumber(0xfffffffdL);
68 // Build Buffer ID to be NO_OFP_NO_BUFFER
69 if (inputPacket.getBufferId() != null) {
70 bufferId = inputPacket.getBufferId();
73 PortNumber outPort = null;
74 NodeConnectorRef outRef = inputPacket.getEgress();
75 Iterable<PathArgument> outArgs = outRef.getValue().getPathArguments();
76 if (Iterables.size(outArgs) >= 3) {
77 outPort = getPortNumber(Iterables.get(outArgs, 2), version);
79 // TODO : P4 search for some normal exception
80 new Exception("PORT NR not exist in Egress");
83 List<Action> actions = null;
84 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> inputActions =
85 inputPacket.getAction();
86 if (inputActions != null) {
87 actions = ActionConvertor.getActions(inputActions, version, datapathid, null);
90 actions = new ArrayList<>();
91 // TODO VD P! wait for way to move Actions (e.g. augmentation)
92 ActionBuilder aBuild = new ActionBuilder();
94 OutputActionCaseBuilder outputActionCaseBuilder =
95 new OutputActionCaseBuilder();
97 OutputActionBuilder outputActionBuilder =
98 new OutputActionBuilder();
100 outputActionBuilder.setPort(outPort);
101 outputActionBuilder.setMaxLength(OFConstants.OFPCML_NO_BUFFER);
103 outputActionCaseBuilder.setOutputAction(outputActionBuilder.build());
105 aBuild.setActionChoice(outputActionCaseBuilder.build());
107 actions.add(aBuild.build());
110 builder.setAction(actions);
111 builder.setData(inputPacket.getPayload());
112 builder.setVersion(version);
114 builder.setInPort(inPortNr);
115 builder.setBufferId(bufferId);
116 // --------------------------------------------------------
118 return builder.build();
121 private static PortNumber getPortNumber(final PathArgument pathArgument, final Short ofVersion) {
122 // FIXME VD P! find InstanceIdentifier helper
123 InstanceIdentifier.IdentifiableItem<?, ?> item = Arguments.checkInstanceOf(pathArgument,
124 InstanceIdentifier.IdentifiableItem.class);
125 NodeConnectorKey key = Arguments.checkInstanceOf(item.getKey(), NodeConnectorKey.class);
126 String[] split = key.getId().getValue().split(":");
127 Long port = OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.get(ofVersion), split[split.length - 1]);
128 return new PortNumber(port);