Merge "-refactored utility code to separate modules and classes -upgraded stress...
[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 java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.controller.sal.common.util.Arguments;
15 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.ActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class PacketOutConvertor {
35     private static final Logger logger = LoggerFactory.getLogger(MeterConvertor.class);
36     private static final String PREFIX_SEPARATOR = "/";
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<ActionsList> actions = new ArrayList<ActionsList>();
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));
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().getPathArguments();
76         if (outArgs.size() >= 3) {
77             outPort = getPortNumber(outArgs.get(2));
78         } else {
79             new Exception("PORT NR not exist in Egress"); // TODO : P4 search
80                                                           // for some normal
81                                                           // exception
82         }
83
84         // TODO VD P! wait for way to move Actions (e.g. augmentation)
85
86         // FIXME VD implementation for testing PacketIn (REMOVE IT)
87         if (inputPacket.getAction() == null) {
88             ActionsListBuilder asBuild = new ActionsListBuilder();
89             ActionBuilder aBuild = new ActionBuilder();
90             aBuild.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output.class);
91             PortActionBuilder paBuild = new PortActionBuilder();
92             paBuild.setPort(outPort);
93             aBuild.addAugmentation(PortAction.class, paBuild.build());
94             MaxLengthActionBuilder mlBuild = new MaxLengthActionBuilder();
95             mlBuild.setMaxLength(0xffff);
96             aBuild.addAugmentation(MaxLengthAction.class, mlBuild.build());
97             asBuild.setAction(aBuild.build());
98             actions.add(asBuild.build());
99             builder.setActionsList(actions);
100         } else {
101             builder.setActionsList(ActionConvertor.getActionList(inputPacket.getAction(), version, datapathid));
102         }
103
104         builder.setData(inputPacket.getPayload());
105         builder.setVersion(version);
106         builder.setXid(xid);
107         builder.setInPort(inPortNr);
108         builder.setBufferId(bufferId);
109         // --------------------------------------------------------
110
111         return builder.build();
112     }
113
114     private static PortNumber getPortNumber(PathArgument pathArgument) {
115         // FIXME VD P! find InstanceIdentifier helper
116         InstanceIdentifier.IdentifiableItem item = Arguments.checkInstanceOf(pathArgument,
117                 InstanceIdentifier.IdentifiableItem.class);
118         NodeConnectorKey key = Arguments.checkInstanceOf(item.getKey(), NodeConnectorKey.class);
119         String[] split = key.getId().getValue().split(":");
120         Long port = Long.decode(split[split.length - 1]);
121         return new PortNumber(port);
122     }
123 }