Bug 5540 - PortConvertor, MatchConvertor
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / translator / PacketReceivedTranslator.java
1 /**
2  * Copyright (c) 2015 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.impl.translator;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.math.BigInteger;
13 import java.util.Optional;
14 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
15 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.extension.api.AugmentTuple;
18 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
19 import org.opendaylight.openflowplugin.impl.util.NodeConnectorRefToPortTranslator;
20 import org.opendaylight.openflowplugin.openflow.md.core.extension.MatchExtensionHelper;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionDatapathIdConvertorData;
23 import org.opendaylight.openflowplugin.openflow.md.util.PacketInUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
31
32 /**
33  * Created by tkubas on 4/1/15.
34  */
35 public class PacketReceivedTranslator implements MessageTranslator<PacketInMessage, PacketReceived> {
36     @Override
37     public PacketReceived translate(final PacketInMessage input, final DeviceInfo deviceInfo, final Object connectionDistinguisher) {
38
39         PacketReceivedBuilder packetReceivedBuilder = new PacketReceivedBuilder();
40         BigInteger datapathId = deviceInfo.getDatapathId();
41
42         // TODO: connection cookie from connection distinguisher
43         // packetReceivedBuilder.setConnectionCookie(new ConnectionCookie(input.getCookie().longValue()));
44
45         packetReceivedBuilder.setPayload(input.getData());
46
47         // get the Cookie if it exists
48         if (input.getCookie() != null) {
49             packetReceivedBuilder.setFlowCookie(new FlowCookie(input.getCookie()));
50         }
51
52         // Try to create the NodeConnectorRef
53         BigInteger dataPathId = deviceInfo.getDatapathId();
54         NodeConnectorRef nodeConnectorRef = NodeConnectorRefToPortTranslator.toNodeConnectorRef(input, dataPathId);
55
56         // If we was able to create NodeConnectorRef, use it
57         if (nodeConnectorRef != null) {
58             packetReceivedBuilder.setIngress(nodeConnectorRef);
59         }
60
61         packetReceivedBuilder.setPacketInReason(PacketInUtil.getMdSalPacketInReason(input.getReason()));
62
63         if (input.getTableId() != null) {
64             packetReceivedBuilder.setTableId(new TableId(input.getTableId().getValue().shortValue()));
65         }
66
67         if (input.getMatch() != null) {
68             org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match packetInMatch = getPacketInMatch(input, datapathId);
69             packetReceivedBuilder.setMatch(packetInMatch);
70         }
71
72         return packetReceivedBuilder.build();
73     }
74
75     @VisibleForTesting
76     static org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match getPacketInMatch(final PacketInMessage input, final BigInteger datapathId) {
77         final VersionDatapathIdConvertorData datapathIdConvertorData = new VersionDatapathIdConvertorData(input.getVersion());
78         datapathIdConvertorData.setDatapathId(datapathId);
79
80         final Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder> matchOptional = ConvertorManager.getInstance().convert(input.getMatch(), datapathIdConvertorData);
81         final MatchBuilder matchBuilder = matchOptional.isPresent() ?
82                 new MatchBuilder(matchOptional.get().build()) :
83                 new MatchBuilder();
84
85         final AugmentTuple<org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.Match> matchExtensionWrap =
86                 MatchExtensionHelper.processAllExtensions(
87                         input.getMatch().getMatchEntry(),
88                         OpenflowVersion.get(input.getVersion()),
89                         MatchPath.PACKETRECEIVED_MATCH);
90
91         if (matchExtensionWrap != null) {
92             matchBuilder.addAugmentation(matchExtensionWrap.getAugmentationClass(), matchExtensionWrap.getAugmentationObject());
93         }
94
95         return matchBuilder.build();
96     }
97 }