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