Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 15 | */ |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.cli; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 17 | |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.app.AllApplicationNamesCompleter; |
| 23 | import org.onosproject.cli.net.ConnectPointCompleter; |
| 24 | import org.onosproject.cli.net.ConnectivityIntentCommand; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 25 | import org.onosproject.net.ChannelSpacing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 28 | import org.onosproject.net.GridType; |
| 29 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.intent.Intent; |
| 33 | import org.onosproject.net.intent.IntentService; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 34 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 35 | import java.util.Map; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 36 | import java.util.List; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkArgument; |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 39 | import static com.google.common.base.Preconditions.checkNotNull; |
Laszlo Papp | 5bdd0e4 | 2017-10-27 10:18:21 +0100 | [diff] [blame] | 40 | import static org.onosproject.net.optical.util.OpticalIntentUtility.createOpticalIntent; |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 41 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 42 | /** |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 43 | * Installs optical connectivity or circuit intents, depending on given port types. |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 44 | */ |
| 45 | @Command(scope = "onos", name = "add-optical-intent", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 46 | description = "Installs optical connectivity intent") |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 47 | public class AddOpticalIntentCommand extends ConnectivityIntentCommand { |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 48 | private static final String SIGNAL_FORMAT = "slotGranularity/channelSpacing(in GHz e.g 6.25,12.5,25,50,100)/" + |
| 49 | "spaceMultiplier/gridType(cwdm, flex, dwdm) " + "e.g 1/6.25/1/flex"; |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 50 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 51 | private static final String CH_6P25 = "6.25"; |
| 52 | private static final String CH_12P5 = "12.5"; |
| 53 | private static final String CH_25 = "25"; |
| 54 | private static final String CH_50 = "50"; |
| 55 | private static final String CH_100 = "100"; |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 56 | |
| 57 | private static final Map<String, ChannelSpacing> CHANNEL_SPACING_MAP = ImmutableMap |
| 58 | .<String, ChannelSpacing>builder() |
| 59 | .put(CH_6P25, ChannelSpacing.CHL_6P25GHZ) |
| 60 | .put(CH_12P5, ChannelSpacing.CHL_12P5GHZ) |
| 61 | .put(CH_25, ChannelSpacing.CHL_25GHZ) |
| 62 | .put(CH_50, ChannelSpacing.CHL_50GHZ) |
| 63 | .put(CH_100, ChannelSpacing.CHL_100GHZ) |
| 64 | .build(); |
| 65 | |
Yuta HIGUCHI | 1d547bf | 2016-08-02 21:44:48 -0700 | [diff] [blame] | 66 | // OSGi workaround |
| 67 | @SuppressWarnings("unused") |
| 68 | private ConnectPointCompleter cpCompleter; |
| 69 | |
| 70 | // OSGi workaround |
| 71 | @SuppressWarnings("unused") |
| 72 | private AllApplicationNamesCompleter appCompleter; |
| 73 | |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 74 | @Argument(index = 0, name = "ingress", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 75 | description = "Ingress Device/Port Description", |
| 76 | required = true, multiValued = false) |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 77 | String ingressString = ""; |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 78 | |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 79 | @Argument(index = 1, name = "egress", |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 80 | description = "Egress Device/Port Description", |
| 81 | required = true, multiValued = false) |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 82 | String egressString = ""; |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 83 | |
| 84 | @Option(name = "-b", aliases = "--bidirectional", |
| 85 | description = "If this argument is passed the optical link created will be bidirectional, " + |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 86 | "else the link will be unidirectional.", |
Aaron Kruglikov | 0a4da0d | 2015-06-10 14:40:48 -0700 | [diff] [blame] | 87 | required = false, multiValued = false) |
| 88 | private boolean bidirectional = false; |
| 89 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 90 | @Option(name = "-s", aliases = "--signal", |
| 91 | description = "Optical Signal. Format = " + SIGNAL_FORMAT, |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 92 | required = false, multiValued = false) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 93 | private String signal; |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 94 | |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 95 | private ConnectPoint createConnectPoint(String devicePortString) { |
| 96 | String[] splitted = devicePortString.split("/"); |
| 97 | |
| 98 | checkArgument(splitted.length == 2, |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 99 | "Connect point must be in \"deviceUri/portNumber\" format"); |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 100 | |
| 101 | DeviceId deviceId = DeviceId.deviceId(splitted[0]); |
Marc De Leenheer | 88194c3 | 2015-05-29 22:10:59 -0700 | [diff] [blame] | 102 | DeviceService deviceService = get(DeviceService.class); |
| 103 | |
| 104 | List<Port> ports = deviceService.getPorts(deviceId); |
| 105 | |
| 106 | for (Port port : ports) { |
| 107 | if (splitted[1].equals(port.number().name())) { |
| 108 | return new ConnectPoint(deviceId, port.number()); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return null; |
| 113 | } |
| 114 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 115 | private OchSignal createOchSignal() throws IllegalArgumentException { |
| 116 | if (signal == null) { |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 117 | return null; |
| 118 | } |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 119 | try { |
| 120 | String[] splitted = signal.split("/"); |
| 121 | checkArgument(splitted.length == 4, |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 122 | "signal requires 4 parameters: " + SIGNAL_FORMAT); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 123 | int slotGranularity = Integer.parseInt(splitted[0]); |
| 124 | String chSpacing = splitted[1]; |
| 125 | ChannelSpacing channelSpacing = checkNotNull(CHANNEL_SPACING_MAP.get(chSpacing), |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 126 | String.format("invalid channel spacing: %s", chSpacing)); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 127 | int multiplier = Integer.parseInt(splitted[2]); |
| 128 | String gdType = splitted[3].toUpperCase(); |
fahadnaeemkhan | a2a6b15 | 2017-10-20 15:28:03 -0700 | [diff] [blame] | 129 | GridType gridType = GridType.valueOf(gdType); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 130 | return new OchSignal(gridType, channelSpacing, multiplier, slotGranularity); |
| 131 | } catch (RuntimeException e) { |
| 132 | /* catching RuntimeException as both NullPointerException (thrown by |
| 133 | * checkNotNull) and IllegalArgumentException (thrown by checkArgument) |
| 134 | * are subclasses of RuntimeException. |
| 135 | */ |
| 136 | String msg = String.format("Invalid signal format: %s, expected format is %s.", |
| 137 | signal, SIGNAL_FORMAT); |
| 138 | print(msg); |
| 139 | throw new IllegalArgumentException(msg, e); |
| 140 | } |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 141 | } |
| 142 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 143 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 144 | @Override |
| 145 | protected void execute() { |
| 146 | IntentService service = get(IntentService.class); |
Laszlo Papp | 5bdd0e4 | 2017-10-27 10:18:21 +0100 | [diff] [blame] | 147 | DeviceService deviceService = get(DeviceService.class); |
Yuta HIGUCHI | d2f041e | 2017-03-10 12:45:15 -0800 | [diff] [blame] | 148 | ConnectPoint ingress = createConnectPoint(ingressString); |
| 149 | ConnectPoint egress = createConnectPoint(egressString); |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 150 | |
Laszlo Papp | 5bdd0e4 | 2017-10-27 10:18:21 +0100 | [diff] [blame] | 151 | Intent intent = createOpticalIntent(ingress, egress, deviceService, |
| 152 | key(), appId(), bidirectional, createOchSignal()); |
Marc De Leenheer | 9f7d189 | 2015-05-30 13:22:24 -0700 | [diff] [blame] | 153 | |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 154 | service.submit(intent); |
Ray Milkey | cb33a13 | 2015-02-02 10:05:12 -0800 | [diff] [blame] | 155 | print("Optical intent submitted:\n%s", intent.toString()); |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 156 | } |
Brian O'Connor | 5296b32 | 2014-10-23 14:59:05 -0700 | [diff] [blame] | 157 | } |