Fix for bug #357- Set log levels on all log statements to appropriate level
[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.util.ArrayList;
11 import java.util.List;
12
13 import org.opendaylight.controller.sal.common.util.Arguments;
14 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.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
50      // Build Port ID from TransmitPacketInput.Ingress
51         PortNumber inPortNr = null;
52         
53         List<PathArgument> inArgs = null;
54         if (inputPacket.getIngress() != null) {
55             inArgs = inputPacket.getIngress().getValue().getPath();
56         }
57         if (inArgs != null && inArgs.size() >= 3) {
58             inPortNr = getPortNumber(inArgs.get(2));
59         } else {
60             // The packetOut originated from the controller
61             inPortNr = new PortNumber(0xfffffffdL);
62         }
63         
64         // Build Buffer ID to be NO_OFP_NO_BUFFER
65         Long bufferId = OFConstants.OFP_NO_BUFFER;
66         
67         PortNumber outPort = null;
68         NodeConnectorRef outRef = inputPacket.getEgress();
69         List<PathArgument> outArgs = outRef.getValue().getPathArguments();
70         if (outArgs.size() >= 3) {
71             outPort = getPortNumber(outArgs.get(2));
72         } else {
73             new Exception("PORT NR not exist in Egress"); //TODO : P4 search for some normal exception
74         }
75         
76         // TODO VD P! wait for way to move Actions (e.g. augmentation)
77         
78         // FIXME VD implementation for testing PacketIn (REMOVE IT)
79         List<ActionsList> actions = new ArrayList<ActionsList>();
80         ActionsListBuilder asBuild = new ActionsListBuilder();
81         ActionBuilder aBuild = new ActionBuilder();
82         aBuild.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output.class);
83         PortActionBuilder paBuild = new PortActionBuilder();
84         paBuild.setPort(outPort);        
85         aBuild.addAugmentation(PortAction.class, paBuild.build());
86         MaxLengthActionBuilder mlBuild = new MaxLengthActionBuilder();
87         mlBuild.setMaxLength(0xffff);
88         aBuild.addAugmentation(MaxLengthAction.class, mlBuild.build());
89         asBuild.setAction(aBuild.build());
90         actions.add(asBuild.build());
91         
92         PacketOutInputBuilder builder = new PacketOutInputBuilder();
93         builder.setActionsList(actions);
94         builder.setData(inputPacket.getPayload());
95         builder.setVersion(version);
96         builder.setXid(xid);
97         builder.setInPort(inPortNr);
98         builder.setBufferId(bufferId);
99         // --------------------------------------------------------
100         
101         return builder.build();
102     }
103     
104     private static PortNumber getPortNumber(PathArgument pathArgument) {
105         //FIXME VD P! find InstanceIdentifier helper 
106         InstanceIdentifier.IdentifiableItem item = Arguments.checkInstanceOf(pathArgument, InstanceIdentifier.IdentifiableItem.class);
107         NodeConnectorKey key = Arguments.checkInstanceOf(item.getKey(), NodeConnectorKey.class);
108         String[] split = key.getId().getValue().split(":");
109         Long port = Long.decode(split[split.length-1]);
110         return new PortNumber(port);
111     }
112 }