blob: 9f504581cb1a9884e4ca42e0e158a2565df6a45f [file] [log] [blame]
adibrastegarnia025da382019-07-24 12:18:18 -07001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16
17
adibrastegarnia025da382019-07-24 12:18:18 -070018package org.onosproject.net.optical.cli;
19
20import com.google.common.collect.ImmutableMap;
21import org.apache.karaf.shell.api.action.Argument;
22import org.apache.karaf.shell.api.action.Command;
23import org.apache.karaf.shell.api.action.Completion;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.onosproject.cli.AbstractShellCommand;
Andrea Campanella3a361452019-08-02 10:17:53 +020026import org.onosproject.cli.net.NetconfOperationCompleter;
adibrastegarnia025da382019-07-24 12:18:18 -070027import org.onosproject.cli.net.OpticalConnectPointCompleter;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
Andrea Campanella3a361452019-08-02 10:17:53 +020030import org.onosproject.net.ChannelSpacing;
adibrastegarnia025da382019-07-24 12:18:18 -070031import org.onosproject.net.ConnectPoint;
Andrea Campanella3a361452019-08-02 10:17:53 +020032import org.onosproject.net.Device;
adibrastegarnia025da382019-07-24 12:18:18 -070033import org.onosproject.net.GridType;
34import org.onosproject.net.OchSignal;
adibrastegarnia025da382019-07-24 12:18:18 -070035import org.onosproject.net.device.DeviceService;
Andrea Campanella3a361452019-08-02 10:17:53 +020036import org.onosproject.net.flow.DefaultFlowRule;
37import org.onosproject.net.flow.DefaultTrafficSelector;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
39import org.onosproject.net.flow.FlowRule;
40import org.onosproject.net.flow.FlowRuleService;
adibrastegarnia025da382019-07-24 12:18:18 -070041import org.onosproject.net.flow.TrafficSelector;
42import org.onosproject.net.flow.TrafficTreatment;
adibrastegarnia025da382019-07-24 12:18:18 -070043import org.onosproject.net.flow.instructions.Instructions;
44
45import java.util.Map;
46
47import static com.google.common.base.Preconditions.checkArgument;
48import static com.google.common.base.Preconditions.checkNotNull;
49
50/**
51 * Enable the optical channel and tune the wavelength via a flow rule based on a given Signal.
52 */
53@Service
Andrea Campanella3a361452019-08-02 10:17:53 +020054@Command(scope = "onos", name = "wavelength-config",
adibrastegarnia025da382019-07-24 12:18:18 -070055 description = "Enable the optical channel and tune the wavelength via a flow rule ")
56public class PortWaveLengthCommand extends AbstractShellCommand {
57
58 private static final String SIGNAL_FORMAT = "slotGranularity/channelSpacing(in GHz e.g 6.25,12.5,25,50,100)/" +
59 "spaceMultiplier/gridType(cwdm, flex, dwdm) " + "e.g 1/6.25/1/flex";
60
61 private static final String CH_6P25 = "6.25";
62 private static final String CH_12P5 = "12.5";
63 private static final String CH_25 = "25";
64 private static final String CH_50 = "50";
65 private static final String CH_100 = "100";
66
67 private static final Map<String, ChannelSpacing> CHANNEL_SPACING_MAP = ImmutableMap
68 .<String, ChannelSpacing>builder()
69 .put(CH_6P25, ChannelSpacing.CHL_6P25GHZ)
70 .put(CH_12P5, ChannelSpacing.CHL_12P5GHZ)
71 .put(CH_25, ChannelSpacing.CHL_25GHZ)
72 .put(CH_50, ChannelSpacing.CHL_50GHZ)
73 .put(CH_100, ChannelSpacing.CHL_100GHZ)
74 .build();
Andrea Campanella3a361452019-08-02 10:17:53 +020075 @Argument(index = 0, name = "operation", description = "Netconf Operation including get, edit-config, etc.",
76 required = true, multiValued = false)
77 @Completion(NetconfOperationCompleter.class)
78 private String operation = null;
adibrastegarnia025da382019-07-24 12:18:18 -070079
Andrea Campanella3a361452019-08-02 10:17:53 +020080 @Argument(index = 1, name = "connectPoint",
adibrastegarnia025da382019-07-24 12:18:18 -070081 description = "Device/Port Description",
82 required = true, multiValued = false)
83 @Completion(OpticalConnectPointCompleter.class)
84 String connectPointString = "";
85
86
Andrea Campanella3a361452019-08-02 10:17:53 +020087 @Argument(index = 2, name = "signal",
adibrastegarnia025da382019-07-24 12:18:18 -070088 description = "Optical Signal. Format = " + SIGNAL_FORMAT,
89 required = true, multiValued = false)
90 String signal = "";
91
92 private OchSignal createOchSignal() throws IllegalArgumentException {
93 if (signal == null) {
94 return null;
95 }
96 try {
97 String[] splitted = signal.split("/");
98 checkArgument(splitted.length == 4,
99 "signal requires 4 parameters: " + SIGNAL_FORMAT);
100 int slotGranularity = Integer.parseInt(splitted[0]);
101 String chSpacing = splitted[1];
102 ChannelSpacing channelSpacing = checkNotNull(CHANNEL_SPACING_MAP.get(chSpacing),
103 String.format("invalid channel spacing: %s", chSpacing));
104 int multiplier = Integer.parseInt(splitted[2]);
105 String gdType = splitted[3].toUpperCase();
106 GridType gridType = GridType.valueOf(gdType);
107 return new OchSignal(gridType, channelSpacing, multiplier, slotGranularity);
108 } catch (RuntimeException e) {
109 /* catching RuntimeException as both NullPointerException (thrown by
110 * checkNotNull) and IllegalArgumentException (thrown by checkArgument)
111 * are subclasses of RuntimeException.
112 */
113 String msg = String.format("Invalid signal format: %s, expected format is %s.",
114 signal, SIGNAL_FORMAT);
115 print(msg);
116 throw new IllegalArgumentException(msg, e);
117 }
118 }
119
120
121 @Override
122 protected void doExecute() throws Exception {
Andrea Campanella3a361452019-08-02 10:17:53 +0200123 if (operation.equals("edit-config")) {
124 FlowRuleService flowService = get(FlowRuleService.class);
125 DeviceService deviceService = get(DeviceService.class);
126 CoreService coreService = get(CoreService.class);
127 ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPointString);
adibrastegarnia025da382019-07-24 12:18:18 -0700128
Andrea Campanella3a361452019-08-02 10:17:53 +0200129 TrafficSelector.Builder trafficSelectorBuilder = DefaultTrafficSelector.builder();
130 TrafficTreatment.Builder trafficTreatmentBuilder = DefaultTrafficTreatment.builder();
131 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
adibrastegarnia025da382019-07-24 12:18:18 -0700132
133
Andrea Campanella3a361452019-08-02 10:17:53 +0200134 // an empty traffic selector
135 TrafficSelector trafficSelector = trafficSelectorBuilder.matchInPort(cp.port()).build();
136 OchSignal ochSignal = createOchSignal();
adibrastegarnia025da382019-07-24 12:18:18 -0700137
Andrea Campanella3a361452019-08-02 10:17:53 +0200138 TrafficTreatment trafficTreatment = trafficTreatmentBuilder
139 .add(Instructions.modL0Lambda(ochSignal))
140 .add(Instructions.createOutput(deviceService.getPort(cp).number()))
141 .build();
adibrastegarnia025da382019-07-24 12:18:18 -0700142
Andrea Campanella3a361452019-08-02 10:17:53 +0200143 Device device = deviceService.getDevice(cp.deviceId());
144 int priority = 100;
145 ApplicationId appId = coreService.registerApplication("org.onosproject.optical-model");
146 log.info(appId.name());
147 FlowRule addFlow = flowRuleBuilder
148 .withPriority(priority)
149 .fromApp(appId)
150 .withTreatment(trafficTreatment)
151 .withSelector(trafficSelector)
152 .forDevice(device.id())
153 .makePermanent()
154 .build();
155 flowService.applyFlowRules(addFlow);
156 String msg = String.format("Setting wavelength %s", ochSignal.centralFrequency().asGHz());
157 print(msg);
158 } else {
159 print("Operation %s are not supported now.", operation);
160 }
adibrastegarnia025da382019-07-24 12:18:18 -0700161
162 }
163}