blob: 79433c38256bf7df1933de1773e5f993c5117cc2 [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;
21import org.apache.karaf.shell.api.action.Option;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
Yuta HIGUCHI1d547bf2016-08-02 21:44:48 -070023import org.onosproject.cli.app.AllApplicationNamesCompleter;
24import 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 HIGUCHI1d547bf2016-08-02 21:44:48 -070068 // OSGi workaround
69 @SuppressWarnings("unused")
70 private ConnectPointCompleter cpCompleter;
71
72 // OSGi workaround
73 @SuppressWarnings("unused")
74 private AllApplicationNamesCompleter appCompleter;
75
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080076 @Argument(index = 0, name = "ingress",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070077 description = "Ingress Device/Port Description",
78 required = true, multiValued = false)
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080079 String ingressString = "";
Brian O'Connor5296b322014-10-23 14:59:05 -070080
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080081 @Argument(index = 1, name = "egress",
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070082 description = "Egress Device/Port Description",
83 required = true, multiValued = false)
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -080084 String egressString = "";
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070085
86 @Option(name = "-b", aliases = "--bidirectional",
87 description = "If this argument is passed the optical link created will be bidirectional, " +
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070088 "else the link will be unidirectional.",
Aaron Kruglikov0a4da0d2015-06-10 14:40:48 -070089 required = false, multiValued = false)
90 private boolean bidirectional = false;
91
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070092 @Option(name = "-s", aliases = "--signal",
93 description = "Optical Signal. Format = " + SIGNAL_FORMAT,
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070094 required = false, multiValued = false)
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070095 private String signal;
Marc De Leenheeradfeffd2017-06-22 16:05:34 -070096
Marc De Leenheer88194c32015-05-29 22:10:59 -070097 private ConnectPoint createConnectPoint(String devicePortString) {
98 String[] splitted = devicePortString.split("/");
99
100 checkArgument(splitted.length == 2,
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700101 "Connect point must be in \"deviceUri/portNumber\" format");
Marc De Leenheer88194c32015-05-29 22:10:59 -0700102
103 DeviceId deviceId = DeviceId.deviceId(splitted[0]);
Marc De Leenheer88194c32015-05-29 22:10:59 -0700104 DeviceService deviceService = get(DeviceService.class);
105
106 List<Port> ports = deviceService.getPorts(deviceId);
107
108 for (Port port : ports) {
109 if (splitted[1].equals(port.number().name())) {
110 return new ConnectPoint(deviceId, port.number());
111 }
112 }
113
114 return null;
115 }
116
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700117 private OchSignal createOchSignal() throws IllegalArgumentException {
118 if (signal == null) {
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700119 return null;
120 }
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700121 try {
122 String[] splitted = signal.split("/");
123 checkArgument(splitted.length == 4,
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700124 "signal requires 4 parameters: " + SIGNAL_FORMAT);
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700125 int slotGranularity = Integer.parseInt(splitted[0]);
126 String chSpacing = splitted[1];
127 ChannelSpacing channelSpacing = checkNotNull(CHANNEL_SPACING_MAP.get(chSpacing),
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700128 String.format("invalid channel spacing: %s", chSpacing));
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700129 int multiplier = Integer.parseInt(splitted[2]);
130 String gdType = splitted[3].toUpperCase();
fahadnaeemkhana2a6b152017-10-20 15:28:03 -0700131 GridType gridType = GridType.valueOf(gdType);
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700132 return new OchSignal(gridType, channelSpacing, multiplier, slotGranularity);
133 } catch (RuntimeException e) {
134 /* catching RuntimeException as both NullPointerException (thrown by
135 * checkNotNull) and IllegalArgumentException (thrown by checkArgument)
136 * are subclasses of RuntimeException.
137 */
138 String msg = String.format("Invalid signal format: %s, expected format is %s.",
139 signal, SIGNAL_FORMAT);
140 print(msg);
141 throw new IllegalArgumentException(msg, e);
142 }
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700143 }
144
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700145
Brian O'Connor5296b322014-10-23 14:59:05 -0700146 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700147 protected void doExecute() {
Brian O'Connor5296b322014-10-23 14:59:05 -0700148 IntentService service = get(IntentService.class);
Laszlo Papp5bdd0e42017-10-27 10:18:21 +0100149 DeviceService deviceService = get(DeviceService.class);
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -0800150 ConnectPoint ingress = createConnectPoint(ingressString);
151 ConnectPoint egress = createConnectPoint(egressString);
Brian O'Connor5296b322014-10-23 14:59:05 -0700152
Laszlo Papp5bdd0e42017-10-27 10:18:21 +0100153 Intent intent = createOpticalIntent(ingress, egress, deviceService,
154 key(), appId(), bidirectional, createOchSignal());
Marc De Leenheer9f7d1892015-05-30 13:22:24 -0700155
Brian O'Connor5296b322014-10-23 14:59:05 -0700156 service.submit(intent);
Ray Milkeycb33a132015-02-02 10:05:12 -0800157 print("Optical intent submitted:\n%s", intent.toString());
Brian O'Connor5296b322014-10-23 14:59:05 -0700158 }
Brian O'Connor5296b322014-10-23 14:59:05 -0700159}