Merge "BUG 2661 - sonar issues in openflowplugin-api"
[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 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
9
10 import org.opendaylight.controller.sal.common.util.Arguments;
11 import org.opendaylight.openflowplugin.api.OFConstants;
12 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
13 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import java.math.BigInteger;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 public final class PacketOutConvertor {
36     private static final Logger LOG = LoggerFactory.getLogger(MeterConvertor.class);
37
38     private PacketOutConvertor() {
39
40     }
41
42     // Get all the data for the PacketOut from the Yang/SAL-Layer
43     /**
44      * @param version
45      * @param Yang
46      *            Data source
47      * @return PacketOutInput required by OF Library
48      */
49     public static PacketOutInput toPacketOutInput(TransmitPacketInput inputPacket, short version, Long xid,
50             BigInteger datapathid) {
51
52         // Build Port ID from TransmitPacketInput.Ingress
53         PortNumber inPortNr = null;
54         Long bufferId = OFConstants.OFP_NO_BUFFER;
55         List<Action> actions = new ArrayList<>();
56         List<PathArgument> inArgs = null;
57         PacketOutInputBuilder builder = new PacketOutInputBuilder();
58         if (inputPacket.getIngress() != null) {
59             inArgs = inputPacket.getIngress().getValue().getPath();
60         }
61         if (inArgs != null && inArgs.size() >= 3) {
62             inPortNr = getPortNumber(inArgs.get(2), version);
63         } else {
64             // The packetOut originated from the controller
65             inPortNr = new PortNumber(0xfffffffdL);
66         }
67
68         // Build Buffer ID to be NO_OFP_NO_BUFFER
69         if (inputPacket.getBufferId() != null) {
70             bufferId = inputPacket.getBufferId();
71         }
72
73         PortNumber outPort = null;
74         NodeConnectorRef outRef = inputPacket.getEgress();
75         List<PathArgument> outArgs = outRef.getValue().getPath();
76         if (outArgs.size() >= 3) {
77             outPort = getPortNumber(outArgs.get(2), version);
78         } else {
79             // TODO : P4 search for some normal exception
80             new Exception("PORT NR not exist in Egress");
81         }
82
83         // TODO VD P! wait for way to move Actions (e.g. augmentation)
84
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);
97         } else {
98             builder.setAction(ActionConvertor.getActions(inputPacket.getAction(), version, datapathid, null));
99         }
100
101         builder.setData(inputPacket.getPayload());
102         builder.setVersion(version);
103         builder.setXid(xid);
104         builder.setInPort(inPortNr);
105         builder.setBufferId(bufferId);
106         // --------------------------------------------------------
107
108         return builder.build();
109     }
110
111     private static PortNumber getPortNumber(PathArgument pathArgument, Short ofVersion) {
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 = OpenflowPortsUtil.getPortFromLogicalName(OpenflowVersion.get(ofVersion), split[split.length - 1]);
118         return new PortNumber(port);
119     }
120 }