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 java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.opendaylight.controller.sal.common.util.Arguments;
15 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.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 class PacketOutConvertor {
34 private static final Logger logger = LoggerFactory.getLogger(MeterConvertor.class);
35 private static final String PREFIX_SEPARATOR = "/";
37 private PacketOutConvertor() {
41 // Get all the data for the PacketOut from the Yang/SAL-Layer
46 * @return PacketOutInput required by OF Library
48 public static PacketOutInput toPacketOutInput(TransmitPacketInput inputPacket, short version, Long xid,
49 BigInteger datapathid) {
51 // Build Port ID from TransmitPacketInput.Ingress
52 PortNumber inPortNr = null;
53 Long bufferId = OFConstants.OFP_NO_BUFFER;
54 List<Action> actions = new ArrayList<>();
55 List<PathArgument> inArgs = null;
56 PacketOutInputBuilder builder = new PacketOutInputBuilder();
57 if (inputPacket.getIngress() != null) {
58 inArgs = inputPacket.getIngress().getValue().getPath();
60 if (inArgs != null && inArgs.size() >= 3) {
61 inPortNr = getPortNumber(inArgs.get(2));
63 // The packetOut originated from the controller
64 inPortNr = new PortNumber(0xfffffffdL);
67 // Build Buffer ID to be NO_OFP_NO_BUFFER
68 if (inputPacket.getBufferId() != null) {
69 bufferId = inputPacket.getBufferId();
72 PortNumber outPort = null;
73 NodeConnectorRef outRef = inputPacket.getEgress();
74 List<PathArgument> outArgs = outRef.getValue().getPathArguments();
75 if (outArgs.size() >= 3) {
76 outPort = getPortNumber(outArgs.get(2));
78 new Exception("PORT NR not exist in Egress"); // TODO : P4 search
83 // TODO VD P! wait for way to move Actions (e.g. augmentation)
85 // FIXME VD implementation for testing PacketIn (REMOVE IT)
86 if (inputPacket.getAction() == null) {
87 ActionBuilder aBuild = new ActionBuilder();
88 aBuild.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output.class);
89 PortActionBuilder paBuild = new PortActionBuilder();
90 paBuild.setPort(outPort);
91 aBuild.addAugmentation(PortAction.class, paBuild.build());
92 MaxLengthActionBuilder mlBuild = new MaxLengthActionBuilder();
93 mlBuild.setMaxLength(0xffff);
94 aBuild.addAugmentation(MaxLengthAction.class, mlBuild.build());
95 actions.add(aBuild.build());
96 builder.setAction(actions);
98 builder.setAction(ActionConvertor.getActions(inputPacket.getAction(), version, datapathid));
101 builder.setData(inputPacket.getPayload());
102 builder.setVersion(version);
104 builder.setInPort(inPortNr);
105 builder.setBufferId(bufferId);
106 // --------------------------------------------------------
108 return builder.build();
111 private static PortNumber getPortNumber(PathArgument pathArgument) {
112 // FIXME VD P! find InstanceIdentifier helper
113 InstanceIdentifier.IdentifiableItem item = Arguments.checkInstanceOf(pathArgument,
114 InstanceIdentifier.IdentifiableItem.class);
115 NodeConnectorKey key = Arguments.checkInstanceOf(item.getKey(), NodeConnectorKey.class);
116 String[] split = key.getId().getValue().split(":");
117 Long port = Long.decode(split[split.length - 1]);
118 return new PortNumber(port);