Merge "BUG-2613: Migrating Openflow Specific NSF from controller project to openflowp...
[openflowplugin.git] / drop-test / src / main / java / org / opendaylight / openflowplugin / outputtest / OutputTestCommandProvider.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.outputtest;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import org.apache.commons.lang.ArrayUtils;
14 import org.eclipse.osgi.framework.console.CommandInterpreter;
15 import org.eclipse.osgi.framework.console.CommandProvider;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder;
30 import org.osgi.framework.BundleContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class OutputTestCommandProvider implements CommandProvider {
35
36     private PacketProcessingService packetProcessingService;
37     private ProviderContext pc;
38     private final BundleContext ctx;
39     private boolean sessionInitiated = false;
40     private static Logger LOG = LoggerFactory.getLogger(OutputTestCommandProvider.class);
41
42     public OutputTestCommandProvider(final BundleContext ctx) {
43         this.ctx = ctx;
44     }
45
46     public void onSessionInitiated(final ProviderContext session) {
47         pc = session;
48         packetProcessingService = session.getRpcService(PacketProcessingService.class);
49         ctx.registerService(CommandProvider.class.getName(), this, null);
50         this.sessionInitiated = true;
51     }
52
53     public void _sendOutputMsg(final CommandInterpreter ci) {
54         /* Sending package OUT */
55         LOG.debug("SendOutMsg");
56         if (sessionInitiated) {
57             String inNodeKey = ci.nextArgument();
58
59             // String resultOfPingFlow =
60             // OutputTestUtil.makePingFlowForNode(inNodeKey, pc);
61             // ci.println(resultOfPingFlow);
62
63             TransmitPacketInput input = OutputTestUtil.buildTransmitInputPacket(inNodeKey, new String(
64                     "sendOutputMsg_TEST").getBytes(), "0xfffffffd", // port
65                     "0");
66
67             packetProcessingService.transmitPacket(input);
68         } else {
69             ci.println("Session not initiated, try again in a few seconds");
70         }
71     }
72
73     public void _sendPacketOutputMsg(final CommandInterpreter ci) {
74         /* Sending package OUT with action */
75         LOG.debug("SendOutMsgWithAction");
76         if (sessionInitiated) {
77             String inNodeKey = ci.nextArgument();
78             String inPort = ci.nextArgument();
79             String outPort = "0xfffffffd";
80
81             ArrayList<Byte> _arrayList = new ArrayList<Byte>(40);
82             ArrayList<Byte> list = _arrayList;
83             String _string = new String("sendOutputMsg_TEST");
84             byte[] msg = _string.getBytes();
85             int index = 0;
86             for (final byte b : msg) {
87                 {
88                     list.add(Byte.valueOf(b));
89                     boolean _lessThan = (index < 7);
90                     if (_lessThan) {
91                         int _plus = (index + 1);
92                         index = _plus;
93                     } else {
94                         index = 0;
95                     }
96                 }
97             }
98             boolean _lessThan = (index < 8);
99             boolean _while = _lessThan;
100             while (_while) {
101                 {
102                     list.add((byte)0);
103                     int _plus = (index + 1);
104                     index = _plus;
105                 }
106                 boolean _lessThan_1 = (index < 8);
107                 _while = _lessThan_1;
108             }
109             NodeRef ref = OutputTestUtil.createNodeRef(inNodeKey);
110
111             TransmitPacketInputBuilder packet_out = new TransmitPacketInputBuilder();
112
113             NodeConnectorRef _createNodeConnRef_1 = OutputTestUtil.createNodeConnRef(inNodeKey, inPort);
114             NodeConnectorRef _nodeConnectorRef_1 = new NodeConnectorRef(_createNodeConnRef_1);
115             NodeConnectorRef nIngressConRef = _nodeConnectorRef_1;
116
117             NodeConnectorRef _createNodeConnRef_2 = OutputTestUtil.createNodeConnRef(inNodeKey, outPort);
118             NodeConnectorRef _nodeConnectorRef_2 = new NodeConnectorRef(_createNodeConnRef_2);
119             NodeConnectorRef nEngressConRef = _nodeConnectorRef_2;
120
121             final ArrayList<Byte> _converted_list = list;
122             byte[] _primitive = ArrayUtils.toPrimitive(_converted_list.toArray(new Byte[0]));
123
124             List<Action> actionList = new ArrayList<Action>();
125             ActionBuilder ab = new ActionBuilder();
126
127             OutputActionBuilder output = new OutputActionBuilder();
128             output.setMaxLength(Integer.valueOf(0xffff));
129             Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
130             output.setOutputNodeConnector(value);
131             ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
132             ab.setOrder(0);
133             ab.setKey(new ActionKey(0));
134             actionList.add(ab.build());
135
136             packet_out.setConnectionCookie(null);
137             packet_out.setAction(actionList);
138             packet_out.setPayload(_primitive);
139             packet_out.setNode(ref);
140             packet_out.setIngress(nIngressConRef);
141             packet_out.setEgress(nEngressConRef);
142             packet_out.setBufferId(OFConstants.OFP_NO_BUFFER);
143
144             packetProcessingService.transmitPacket(packet_out.build());
145         } else {
146             ci.println("Session not initiated, try again in a few seconds");
147         }
148     }
149
150     public void _sendOutTopologyMsg(final CommandInterpreter ci) {
151         /* Sending package OUT */
152         LOG.debug("SendOutTopologyMsg");
153
154     }
155
156     @Override
157     public String getHelp() {
158         StringBuilder strBuf = new StringBuilder("-------------- OUT Package ----------\n")
159                 .append(" sendOutputMsg command + nodeId as param sends empty package out \n ");
160         return strBuf.toString();
161     }
162 }