adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 18 | package org.onosproject.net.optical.cli; |
| 19 | |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import org.apache.karaf.shell.api.action.Argument; |
| 22 | import org.apache.karaf.shell.api.action.Command; |
| 23 | import org.apache.karaf.shell.api.action.Completion; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 26 | import org.onosproject.cli.net.NetconfOperationCompleter; |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 27 | import org.onosproject.cli.net.OpticalConnectPointCompleter; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 30 | import org.onosproject.net.ChannelSpacing; |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 31 | import org.onosproject.net.ConnectPoint; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 32 | import org.onosproject.net.Device; |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 33 | import org.onosproject.net.GridType; |
| 34 | import org.onosproject.net.OchSignal; |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 35 | import org.onosproject.net.device.DeviceService; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 36 | import org.onosproject.net.flow.DefaultFlowRule; |
| 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.FlowRule; |
| 40 | import org.onosproject.net.flow.FlowRuleService; |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.instructions.Instructions; |
| 44 | |
| 45 | import java.util.Map; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkArgument; |
| 48 | import 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 Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 54 | @Command(scope = "onos", name = "wavelength-config", |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 55 | description = "Enable the optical channel and tune the wavelength via a flow rule ") |
| 56 | public 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 Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 75 | @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; |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 79 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 80 | @Argument(index = 1, name = "connectPoint", |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 81 | description = "Device/Port Description", |
| 82 | required = true, multiValued = false) |
| 83 | @Completion(OpticalConnectPointCompleter.class) |
| 84 | String connectPointString = ""; |
| 85 | |
| 86 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 87 | @Argument(index = 2, name = "signal", |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 88 | 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 Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 123 | 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); |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 128 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 129 | TrafficSelector.Builder trafficSelectorBuilder = DefaultTrafficSelector.builder(); |
| 130 | TrafficTreatment.Builder trafficTreatmentBuilder = DefaultTrafficTreatment.builder(); |
| 131 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder(); |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 132 | |
| 133 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 134 | // an empty traffic selector |
| 135 | TrafficSelector trafficSelector = trafficSelectorBuilder.matchInPort(cp.port()).build(); |
| 136 | OchSignal ochSignal = createOchSignal(); |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 137 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 138 | TrafficTreatment trafficTreatment = trafficTreatmentBuilder |
| 139 | .add(Instructions.modL0Lambda(ochSignal)) |
| 140 | .add(Instructions.createOutput(deviceService.getPort(cp).number())) |
| 141 | .build(); |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 142 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 143 | 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 | } |
adibrastegarnia | 025da38 | 2019-07-24 12:18:18 -0700 | [diff] [blame] | 161 | |
| 162 | } |
| 163 | } |