blob: 07f9cc1f90284da94f0f4ae72b84bf56268cbbc4 [file] [log] [blame]
Brian O'Connor5296b322014-10-23 14:59:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian O'Connor5296b322014-10-23 14:59:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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'Connor5296b322014-10-23 14:59:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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'Connor5296b322014-10-23 14:59:05 -070015 */
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070016package org.onosproject.net.optical.cli;
Brian O'Connor5296b322014-10-23 14:59:05 -070017
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070018import com.google.common.collect.ImmutableMap;
Brian O'Connor5296b322014-10-23 14:59:05 -070019import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070021import org.apache.karaf.shell.commands.Option;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070022import org.onosproject.cli.app.AllApplicationNamesCompleter;
23import org.onosproject.cli.net.ConnectPointCompleter;
24import org.onosproject.cli.net.ConnectivityIntentCommand;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070025import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.ConnectPoint;
Marc De Leenheer88194c32015-05-29 22:10:59 -070027import org.onosproject.net.DeviceId;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070028import org.onosproject.net.GridType;
29import org.onosproject.net.OchSignal;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070030import org.onosproject.net.Port;
31import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.Intent;
33import org.onosproject.net.intent.IntentService;
Brian O'Connor5296b322014-10-23 14:59:05 -070034
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070035import java.util.Map;
Marc De Leenheer88194c32015-05-29 22:10:59 -070036import java.util.List;
37
38import static com.google.common.base.Preconditions.checkArgument;
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070039import static com.google.common.base.Preconditions.checkNotNull;
Laszlo Papp5bdd0e42017-10-27 10:18:21 +010040import static org.onosproject.net.optical.util.OpticalIntentUtility.createOpticalIntent;
Marc De Leenheer88194c32015-05-29 22:10:59 -070041
Brian O'Connor5296b322014-10-23 14:59:05 -070042/**
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070043 * Installs optical connectivity or circuit intents, depending on given port types.
Brian O'Connor5296b322014-10-23 14:59:05 -070044 */
45@Command(scope = "onos", name = "add-optical-intent",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070046 description = "Installs optical connectivity intent")
Brian O'Connor5296b322014-10-23 14:59:05 -070047public class AddOpticalIntentCommand extends ConnectivityIntentCommand {
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070048 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";
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070050
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070051 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";
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070056
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 HIGUCHI1d547bf2016-08-02 21:44:48 -070066 // OSGi workaround
67 @SuppressWarnings("unused")
68 private ConnectPointCompleter cpCompleter;
69
70 // OSGi workaround
71 @SuppressWarnings("unused")
72 private AllApplicationNamesCompleter appCompleter;
73
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080074 @Argument(index = 0, name = "ingress",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070075 description = "Ingress Device/Port Description",
76 required = true, multiValued = false)
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080077 String ingressString = "";
Brian O'Connor5296b322014-10-23 14:59:05 -070078
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080079 @Argument(index = 1, name = "egress",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070080 description = "Egress Device/Port Description",
81 required = true, multiValued = false)
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080082 String egressString = "";
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070083
84 @Option(name = "-b", aliases = "--bidirectional",
85 description = "If this argument is passed the optical link created will be bidirectional, " +
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070086 "else the link will be unidirectional.",
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070087 required = false, multiValued = false)
88 private boolean bidirectional = false;
89
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070090 @Option(name = "-s", aliases = "--signal",
91 description = "Optical Signal. Format = " + SIGNAL_FORMAT,
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070092 required = false, multiValued = false)
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070093 private String signal;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070094
Marc De Leenheer88194c32015-05-29 22:10:59 -070095 private ConnectPoint createConnectPoint(String devicePortString) {
96 String[] splitted = devicePortString.split("/");
97
98 checkArgument(splitted.length == 2,
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070099 "Connect point must be in \"deviceUri/portNumber\" format");
Marc De Leenheer88194c32015-05-29 22:10:59 -0700100
101 DeviceId deviceId = DeviceId.deviceId(splitted[0]);
Marc De Leenheer88194c32015-05-29 22:10:59 -0700102 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
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700115 private OchSignal createOchSignal() throws IllegalArgumentException {
116 if (signal == null) {
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700117 return null;
118 }
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700119 try {
120 String[] splitted = signal.split("/");
121 checkArgument(splitted.length == 4,
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700122 "signal requires 4 parameters: " + SIGNAL_FORMAT);
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700123 int slotGranularity = Integer.parseInt(splitted[0]);
124 String chSpacing = splitted[1];
125 ChannelSpacing channelSpacing = checkNotNull(CHANNEL_SPACING_MAP.get(chSpacing),
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700126 String.format("invalid channel spacing: %s", chSpacing));
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700127 int multiplier = Integer.parseInt(splitted[2]);
128 String gdType = splitted[3].toUpperCase();
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700129 GridType gridType = GridType.valueOf(gdType);
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700130 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 Leenheeradfeffd2017-06-22 16:05:34 -0700141 }
142
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700143
Brian O'Connor5296b322014-10-23 14:59:05 -0700144 @Override
145 protected void execute() {
146 IntentService service = get(IntentService.class);
Laszlo Papp5bdd0e42017-10-27 10:18:21 +0100147 DeviceService deviceService = get(DeviceService.class);
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -0800148 ConnectPoint ingress = createConnectPoint(ingressString);
149 ConnectPoint egress = createConnectPoint(egressString);
Brian O'Connor5296b322014-10-23 14:59:05 -0700150
Laszlo Papp5bdd0e42017-10-27 10:18:21 +0100151 Intent intent = createOpticalIntent(ingress, egress, deviceService,
152 key(), appId(), bidirectional, createOchSignal());
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700153
Brian O'Connor5296b322014-10-23 14:59:05 -0700154 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -0800155 print("Optical intent submitted:\n%s", intent.toString());
Brian O'Connor5296b322014-10-23 14:59:05 -0700156 }
Brian O'Connor5296b322014-10-23 14:59:05 -0700157}