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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11 import com.google.common.collect.Iterables;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
18 import org.opendaylight.controller.sal.common.util.Arguments;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionConvertorData;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.Convertor;
23 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.PacketOutConvertorData;
24 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
35 import org.opendaylight.yangtools.yang.binding.DataContainer;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Converts a MD-SAL packet out data into the OF library packet out input.
47 * PacketOutConvertorData data = new PacketOutConvertorData(version);
48 * data.setDatapathId(datapathId);
50 * Optional<PacketOutInput> ofPacketInput = ConvertorManager.getInstance().convert(salPacket, data);
54 public class PacketOutConvertor implements Convertor<TransmitPacketInput, PacketOutInput, PacketOutConvertorData> {
55 private static final Logger LOG = LoggerFactory.getLogger(PacketOutConvertor.class);
56 private static final Set<Class<? extends DataContainer>> TYPES = Collections.singleton(TransmitPacketInput.class);
59 * Create default empty meter mot input builder.
60 * Use this method, if result from convertor is empty.
62 * @param version Openflow version
63 * @return default empty meter mod input builder
65 public static PacketOutInput defaultResult(short version) {
66 return new PacketOutInputBuilder()
71 private static PortNumber getPortNumber(final PathArgument pathArgument, final Short ofVersion) {
72 // FIXME VD P! find InstanceIdentifier helper
73 InstanceIdentifier.IdentifiableItem<?, ?> item = Arguments.checkInstanceOf(pathArgument,
74 InstanceIdentifier.IdentifiableItem.class);
75 NodeConnectorKey key = Arguments.checkInstanceOf(item.getKey(), NodeConnectorKey.class);
76 Long port = InventoryDataServiceUtil.portNumberfromNodeConnectorId(
77 OpenflowVersion.get(ofVersion), key.getId());
78 return new PortNumber(port);
82 public Collection<Class<? extends DataContainer>> getTypes() {
87 public PacketOutInput convert(TransmitPacketInput source, PacketOutConvertorData data) {
88 LOG.trace("toPacketOutInput for datapathId:{}, xid:{}", data.getDatapathId(), data.getXid());
89 // Build Port ID from TransmitPacketInput.Ingress
91 Long bufferId = OFConstants.OFP_NO_BUFFER;
92 Iterable<PathArgument> inArgs = null;
93 PacketOutInputBuilder builder = new PacketOutInputBuilder();
95 if (source.getIngress() != null) {
96 inArgs = source.getIngress().getValue().getPathArguments();
99 if (inArgs != null && Iterables.size(inArgs) >= 3) {
100 inPortNr = getPortNumber(Iterables.get(inArgs, 2), data.getVersion());
102 // The packetOut originated from the controller
103 inPortNr = new PortNumber(0xfffffffdL);
106 // Build Buffer ID to be NO_OFP_NO_BUFFER
107 if (source.getBufferId() != null) {
108 bufferId = source.getBufferId();
111 PortNumber outPort = null;
112 NodeConnectorRef outRef = source.getEgress();
113 Iterable<PathArgument> outArgs = outRef.getValue().getPathArguments();
115 if (Iterables.size(outArgs) >= 3) {
116 outPort = getPortNumber(Iterables.get(outArgs, 2), data.getVersion());
118 // TODO : P4 search for some normal exception
119 // new Exception("PORT NR not exist in Egress");
120 LOG.error("PORT NR not exist in Egress");
123 List<Action> actions = new ArrayList<>();
124 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> inputActions = source.getAction();
126 if (inputActions != null) {
127 final ActionConvertorData actionConvertorData = new ActionConvertorData(data.getVersion());
128 actionConvertorData.setDatapathId(data.getDatapathId());
130 final Optional<List<Action>> convertedActions = ConvertorManager.getInstance().convert(
131 inputActions, actionConvertorData);
133 actions = convertedActions.orElse(Collections.emptyList());
136 // TODO VD P! wait for way to move Actions (e.g. augmentation)
137 ActionBuilder aBuild = new ActionBuilder();
138 OutputActionCaseBuilder outputActionCaseBuilder = new OutputActionCaseBuilder();
139 OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
140 outputActionBuilder.setPort(outPort);
141 outputActionBuilder.setMaxLength(OFConstants.OFPCML_NO_BUFFER);
142 outputActionCaseBuilder.setOutputAction(outputActionBuilder.build());
143 aBuild.setActionChoice(outputActionCaseBuilder.build());
144 actions.add(aBuild.build());
147 builder.setAction(actions);
148 builder.setData(source.getPayload());
149 builder.setVersion(data.getVersion());
150 builder.setXid(data.getXid());
151 builder.setInPort(inPortNr);
152 builder.setBufferId(bufferId);
154 return builder.build();