blob: 833b20d9695c2f9f209d66f55edcf3ce76ba4f65 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkeyc1e69e82018-10-09 15:59:22 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Option;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070024import org.onosproject.cli.net.ConnectPointCompleter;
25import org.onosproject.cli.net.ConnectivityIntentCommand;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070026import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.ConnectPoint;
Marc De Leenheer88194c32015-05-29 22:10:59 -070028import org.onosproject.net.DeviceId;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070029import org.onosproject.net.GridType;
30import org.onosproject.net.OchSignal;
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070031import org.onosproject.net.Port;
32import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.intent.Intent;
34import org.onosproject.net.intent.IntentService;
Brian O'Connor5296b322014-10-23 14:59:05 -070035
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070036import java.util.Map;
Marc De Leenheer88194c32015-05-29 22:10:59 -070037import java.util.List;
38
39import static com.google.common.base.Preconditions.checkArgument;
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070040import static com.google.common.base.Preconditions.checkNotNull;
Laszlo Papp5bdd0e42017-10-27 10:18:21 +010041import static org.onosproject.net.optical.util.OpticalIntentUtility.createOpticalIntent;
Marc De Leenheer88194c32015-05-29 22:10:59 -070042
Brian O'Connor5296b322014-10-23 14:59:05 -070043/**
Marc De Leenheer9f7d1892015-05-30 13:22:24 -070044 * Installs optical connectivity or circuit intents, depending on given port types.
Brian O'Connor5296b322014-10-23 14:59:05 -070045 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046@Service
Brian O'Connor5296b322014-10-23 14:59:05 -070047@Command(scope = "onos", name = "add-optical-intent",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070048 description = "Installs optical connectivity intent")
Brian O'Connor5296b322014-10-23 14:59:05 -070049public class AddOpticalIntentCommand extends ConnectivityIntentCommand {
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070050 private static final String SIGNAL_FORMAT = "slotGranularity/channelSpacing(in GHz e.g 6.25,12.5,25,50,100)/" +
51 "spaceMultiplier/gridType(cwdm, flex, dwdm) " + "e.g 1/6.25/1/flex";
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070052
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070053 private static final String CH_6P25 = "6.25";
54 private static final String CH_12P5 = "12.5";
55 private static final String CH_25 = "25";
56 private static final String CH_50 = "50";
57 private static final String CH_100 = "100";
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070058
59 private static final Map<String, ChannelSpacing> CHANNEL_SPACING_MAP = ImmutableMap
60 .<String, ChannelSpacing>builder()
61 .put(CH_6P25, ChannelSpacing.CHL_6P25GHZ)
62 .put(CH_12P5, ChannelSpacing.CHL_12P5GHZ)
63 .put(CH_25, ChannelSpacing.CHL_25GHZ)
64 .put(CH_50, ChannelSpacing.CHL_50GHZ)
65 .put(CH_100, ChannelSpacing.CHL_100GHZ)
66 .build();
67
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080068 @Argument(index = 0, name = "ingress",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070069 description = "Ingress Device/Port Description",
70 required = true, multiValued = false)
Ray Milkeyc1e69e82018-10-09 15:59:22 -070071 @Completion(ConnectPointCompleter.class)
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080072 String ingressString = "";
Brian O'Connor5296b322014-10-23 14:59:05 -070073
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080074 @Argument(index = 1, name = "egress",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070075 description = "Egress Device/Port Description",
76 required = true, multiValued = false)
Ray Milkeyc1e69e82018-10-09 15:59:22 -070077 @Completion(ConnectPointCompleter.class)
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080078 String egressString = "";
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070079
80 @Option(name = "-b", aliases = "--bidirectional",
81 description = "If this argument is passed the optical link created will be bidirectional, " +
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070082 "else the link will be unidirectional.",
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070083 required = false, multiValued = false)
84 private boolean bidirectional = false;
85
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070086 @Option(name = "-s", aliases = "--signal",
87 description = "Optical Signal. Format = " + SIGNAL_FORMAT,
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070088 required = false, multiValued = false)
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070089 private String signal;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070090
Marc De Leenheer88194c32015-05-29 22:10:59 -070091 private ConnectPoint createConnectPoint(String devicePortString) {
92 String[] splitted = devicePortString.split("/");
93
94 checkArgument(splitted.length == 2,
fahadnaeemkhana2a6b152017-10-20 15:28:03 -070095 "Connect point must be in \"deviceUri/portNumber\" format");
Marc De Leenheer88194c32015-05-29 22:10:59 -070096
97 DeviceId deviceId = DeviceId.deviceId(splitted[0]);
Marc De Leenheer88194c32015-05-29 22:10:59 -070098 DeviceService deviceService = get(DeviceService.class);
99
100 List<Port> ports = deviceService.getPorts(deviceId);
101
102 for (Port port : ports) {
103 if (splitted[1].equals(port.number().name())) {
104 return new ConnectPoint(deviceId, port.number());
105 }
106 }
107
108 return null;
109 }
110
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700111 private OchSignal createOchSignal() throws IllegalArgumentException {
112 if (signal == null) {
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700113 return null;
114 }
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700115 try {
116 String[] splitted = signal.split("/");
117 checkArgument(splitted.length == 4,
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700118 "signal requires 4 parameters: " + SIGNAL_FORMAT);
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700119 int slotGranularity = Integer.parseInt(splitted[0]);
120 String chSpacing = splitted[1];
121 ChannelSpacing channelSpacing = checkNotNull(CHANNEL_SPACING_MAP.get(chSpacing),
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700122 String.format("invalid channel spacing: %s", chSpacing));
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700123 int multiplier = Integer.parseInt(splitted[2]);
124 String gdType = splitted[3].toUpperCase();
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700125 GridType gridType = GridType.valueOf(gdType);
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700126 return new OchSignal(gridType, channelSpacing, multiplier, slotGranularity);
127 } catch (RuntimeException e) {
128 /* catching RuntimeException as both NullPointerException (thrown by
129 * checkNotNull) and IllegalArgumentException (thrown by checkArgument)
130 * are subclasses of RuntimeException.
131 */
132 String msg = String.format("Invalid signal format: %s, expected format is %s.",
133 signal, SIGNAL_FORMAT);
134 print(msg);
135 throw new IllegalArgumentException(msg, e);
136 }
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700137 }
138
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700139
Brian O'Connor5296b322014-10-23 14:59:05 -0700140 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700141 protected void doExecute() {
Brian O'Connor5296b322014-10-23 14:59:05 -0700142 IntentService service = get(IntentService.class);
Laszlo Papp5bdd0e42017-10-27 10:18:21 +0100143 DeviceService deviceService = get(DeviceService.class);
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -0800144 ConnectPoint ingress = createConnectPoint(ingressString);
145 ConnectPoint egress = createConnectPoint(egressString);
Brian O'Connor5296b322014-10-23 14:59:05 -0700146
Laszlo Papp5bdd0e42017-10-27 10:18:21 +0100147 Intent intent = createOpticalIntent(ingress, egress, deviceService,
148 key(), appId(), bidirectional, createOchSignal());
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700149
Brian O'Connor5296b322014-10-23 14:59:05 -0700150 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -0800151 print("Optical intent submitted:\n%s", intent.toString());
Brian O'Connor5296b322014-10-23 14:59:05 -0700152 }
Brian O'Connor5296b322014-10-23 14:59:05 -0700153}