blob: 4a15d8e7103968b8f6e22ad2f8c0e49889fcaf6f [file] [log] [blame]
jccd8697232015-05-05 14:42:23 +08001/*
2 * Copyright 2014-2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cli.net;
17
18import java.util.Collection;
19import java.util.Optional;
20
21import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
23import org.onlab.packet.IpAddress;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.DefaultApplicationId;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.provider.ProviderId;
30import org.onosproject.net.tunnel.DefaultOpticalTunnelEndPoint;
31import org.onosproject.net.tunnel.IpTunnelEndPoint;
32import org.onosproject.net.tunnel.OpticalLogicId;
33import org.onosproject.net.tunnel.OpticalTunnelEndPoint;
34import org.onosproject.net.tunnel.Tunnel;
35import org.onosproject.net.tunnel.TunnelEndPoint;
36import org.onosproject.net.tunnel.TunnelService;
37
38/**
39 * Borrows all tunnels between specific source tunnel end point and specific
40 * destination tunnel end point. Supports for IP address and optical as tunnel end point now. It's used by consumers.
41 */
42@Command(scope = "onos", name = "borrow-tunnels",
43description = "Borrows all tunnels between specific source tunnel end point"
44 + " and specific destination tunnel end point."
45 + " Supports for IP address and optical as tunnel end point now. It's used by consumers.")
46public class BorrowTunnelCommand extends AbstractShellCommand {
47 @Argument(index = 0, name = "consumerId", description = "consumer id means application id.",
48 required = true, multiValued = false)
49 String consumerId = null;
50 @Argument(index = 1, name = "src", description = "Source tunnel point."
51 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
52 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
53 + " Otherwise src means IP address.", required = true, multiValued = false)
54 String src = null;
55 @Argument(index = 2, name = "dst", description = "Destination tunnel point."
56 + " Only supports for IpTunnelEndPoint and OpticalTunnelEndPoint as end point now."
57 + " If deletess a ODUK or OCH type tunnel, the formatter of this argument is DeviceId-PortNumber."
58 + " Otherwise dst means IP address.", required = true, multiValued = false)
59 String dst = null;
60
61 @Argument(index = 3, name = "type", description = "The type of tunnels,"
62 + " It includes MPLS, VLAN, VXLAN, GRE, ODUK, OCH", required = true, multiValued = false)
63 String type = null;
64 private static final String FMT = "src=%s, dst=%s,"
65 + "type=%s, state=%s, producerName=%s, tunnelName=%s,"
66 + "groupId=%s";
67
68 @Override
69 protected void execute() {
70 TunnelService service = get(TunnelService.class);
71 ApplicationId appId = new DefaultApplicationId(1, consumerId);
72 ProviderId producerName = new ProviderId("default",
73 "org.onosproject.provider.tunnel.default");
74 TunnelEndPoint srcPoint = null;
75 TunnelEndPoint dstPoint = null;
76 if ("MPLS".equals(type) || "VLAN".equals(type) || "VXLAN".equals(type)
77 || "GRE".equals(type)) {
78 srcPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(src));
79 dstPoint = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(dst));
80 } else if ("ODUK".equals(type) || "OCH".equals(type)) {
81 String[] srcArray = src.split("-");
82 String[] dstArray = dst.split("-");
83 srcPoint = new DefaultOpticalTunnelEndPoint(
84 producerName,
85 Optional.of(DeviceId
86 .deviceId(srcArray[0])),
87 Optional.of(PortNumber
88 .portNumber(srcArray[1])),
89 null,
90 OpticalTunnelEndPoint.Type.LAMBDA,
91 OpticalLogicId
92 .logicId(0),
93 true);
94 dstPoint = new DefaultOpticalTunnelEndPoint(
95 producerName,
96 Optional.of(DeviceId
97 .deviceId(dstArray[0])),
98 Optional.of(PortNumber
99 .portNumber(dstArray[1])),
100 null,
101 OpticalTunnelEndPoint.Type.LAMBDA,
102 OpticalLogicId
103 .logicId(0),
104 true);
105 } else {
106 print("Illegal tunnel type. Please input MPLS, VLAN, VXLAN, GRE, ODUK or OCH.");
107 return;
108 }
109 Collection<Tunnel> tunnelSet = service.borrowTunnel(appId, srcPoint, dstPoint);
110 for (Tunnel tunnel : tunnelSet) {
111 print(FMT, tunnel.src(), tunnel.dst(), tunnel.type(),
112 tunnel.state(), tunnel.providerId(), tunnel.tunnelName(),
113 tunnel.groupId());
114 }
115 }
116
117}