7f8a2dc79ba9dd64a6c9ac53b6c81e01cf3b304e
[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.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.Set;
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;
40
41 /**
42  * Converts a MD-SAL packet out data into the OF library packet out input.
43  *
44  * Example usage:
45  * <pre>
46  * {@code
47  * PacketOutConvertorData data = new PacketOutConvertorData(version);
48  * data.setDatapathId(datapathId);
49  * data.setXid(xid);
50  * Optional<PacketOutInput> ofPacketInput = ConvertorManager.getInstance().convert(salPacket, data);
51  * }
52  * </pre>
53  */
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);
57
58     /**
59      * Create default empty meter mot input builder.
60      * Use this method, if result from convertor is empty.
61      *
62      * @param version Openflow version
63      * @return default empty meter mod input builder
64      */
65     public static PacketOutInput defaultResult(short version) {
66         return new PacketOutInputBuilder()
67                 .setVersion(version)
68                 .build();
69     }
70
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);
79     }
80
81     @Override
82     public Collection<Class<? extends DataContainer>> getTypes() {
83         return TYPES;
84     }
85
86     @Override
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
90         PortNumber inPortNr;
91         Long bufferId = OFConstants.OFP_NO_BUFFER;
92         Iterable<PathArgument> inArgs = null;
93         PacketOutInputBuilder builder = new PacketOutInputBuilder();
94
95         if (source.getIngress() != null) {
96             inArgs = source.getIngress().getValue().getPathArguments();
97         }
98
99         if (inArgs != null && Iterables.size(inArgs) >= 3) {
100             inPortNr = getPortNumber(Iterables.get(inArgs, 2), data.getVersion());
101         } else {
102             // The packetOut originated from the controller
103             inPortNr = new PortNumber(0xfffffffdL);
104         }
105
106         // Build Buffer ID to be NO_OFP_NO_BUFFER
107         if (source.getBufferId() != null) {
108             bufferId = source.getBufferId();
109         }
110
111         PortNumber outPort = null;
112         NodeConnectorRef outRef = source.getEgress();
113         Iterable<PathArgument> outArgs = outRef.getValue().getPathArguments();
114
115         if (Iterables.size(outArgs) >= 3) {
116             outPort = getPortNumber(Iterables.get(outArgs, 2), data.getVersion());
117         } else {
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");
121         }
122
123         List<Action> actions = new ArrayList<>();
124         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> inputActions = source.getAction();
125
126         if (inputActions != null) {
127             final ActionConvertorData actionConvertorData = new ActionConvertorData(data.getVersion());
128             actionConvertorData.setDatapathId(data.getDatapathId());
129
130             final Optional<List<Action>> convertedActions = ConvertorManager.getInstance().convert(
131                     inputActions, actionConvertorData);
132
133             actions = convertedActions.orElse(Collections.emptyList());
134
135         } else {
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());
145         }
146
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);
153
154         return builder.build();
155     }
156 }