2761f71f69014a0480f25b9414ea34f6a6ddd378
[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.grouping.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class PacketOutConvertor {
34     private static final Logger logger = LoggerFactory.getLogger(MeterConvertor.class);
35     private static final String PREFIX_SEPARATOR = "/";
36
37     private PacketOutConvertor() {
38
39     }
40
41     // Get all the data for the PacketOut from the Yang/SAL-Layer
42     /**
43      * @param version
44      * @param Yang
45      *            Data source
46      * @return PacketOutInput required by OF Library
47      */
48     public static PacketOutInput toPacketOutInput(TransmitPacketInput inputPacket, short version, Long xid,
49             BigInteger datapathid) {
50
51         // Build Port ID from TransmitPacketInput.Ingress
52         PortNumber inPortNr = null;
53         Long bufferId = OFConstants.OFP_NO_BUFFER;
54         List<Action> actions = new ArrayList<>();
55         List<PathArgument> inArgs = null;
56         PacketOutInputBuilder builder = new PacketOutInputBuilder();
57         if (inputPacket.getIngress() != null) {
58             inArgs = inputPacket.getIngress().getValue().getPath();
59         }
60         if (inArgs != null && inArgs.size() >= 3) {
61             inPortNr = getPortNumber(inArgs.get(2));
62         } else {
63             // The packetOut originated from the controller
64             inPortNr = new PortNumber(0xfffffffdL);
65         }
66
67         // Build Buffer ID to be NO_OFP_NO_BUFFER
68         if (inputPacket.getBufferId() != null) {
69             bufferId = inputPacket.getBufferId();
70         }
71
72         PortNumber outPort = null;
73         NodeConnectorRef outRef = inputPacket.getEgress();
74         List<PathArgument> outArgs = outRef.getValue().getPath();
75         if (outArgs.size() >= 3) {
76             outPort = getPortNumber(outArgs.get(2));
77         } else {
78             new Exception("PORT NR not exist in Egress"); // TODO : P4 search
79                                                           // for some normal
80                                                           // exception
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));
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) {
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 = Long.decode(split[split.length - 1]);
118         return new PortNumber(port);
119     }
120 }