Yuta HIGUCHI | a107172 | 2016-12-07 20:21:16 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yuta HIGUCHI | a107172 | 2016-12-07 20:21:16 -0800 | [diff] [blame] | 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 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import java.util.Optional; |
| 19 | |
| 20 | import org.apache.karaf.shell.commands.Argument; |
| 21 | import org.apache.karaf.shell.commands.Command; |
| 22 | import org.apache.karaf.shell.commands.Option; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.intent.Intent; |
| 26 | import org.onosproject.net.intent.IntentService; |
| 27 | import org.onosproject.net.intent.Key; |
| 28 | import org.onosproject.net.intent.ProtectedTransportIntent; |
| 29 | |
| 30 | /** |
| 31 | * Installs ProtectedTransportIntent. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "add-protected-transport", |
| 34 | description = "Adds ProtectedTransportIntent") |
| 35 | public class AddProtectedTransportIntentCommand |
| 36 | extends AbstractShellCommand { |
| 37 | |
| 38 | @Option(name = "--key", aliases = "-k", |
| 39 | description = "Intent key", |
| 40 | required = false, multiValued = false) |
| 41 | private String keyStr = null; |
| 42 | |
| 43 | @Argument(index = 0, name = "deviceId1", |
| 44 | description = "First Device ID of protected path", |
| 45 | required = true, multiValued = false) |
| 46 | private String deviceId1Str = null; |
| 47 | |
| 48 | @Argument(index = 1, name = "deviceId2", |
| 49 | description = "Second Device ID of protected path", |
| 50 | required = true, multiValued = false) |
| 51 | private String deviceId2Str = null; |
| 52 | |
| 53 | private IntentService intentService; |
| 54 | |
| 55 | @Override |
| 56 | protected void execute() { |
| 57 | intentService = get(IntentService.class); |
| 58 | |
| 59 | DeviceId did1 = DeviceId.deviceId(deviceId1Str); |
| 60 | DeviceId did2 = DeviceId.deviceId(deviceId2Str); |
| 61 | |
| 62 | Intent intent; |
| 63 | intent = ProtectedTransportIntent.builder() |
| 64 | .key(key()) |
| 65 | .appId(appId()) |
| 66 | .one(did1) |
| 67 | .two(did2) |
| 68 | .build(); |
| 69 | |
| 70 | print("Submitting: %s", intent); |
| 71 | intentService.submit(intent); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns Key if specified in the option. |
| 76 | * |
| 77 | * @return Key instance or null |
| 78 | */ |
| 79 | protected Key key() { |
| 80 | return Optional.ofNullable(keyStr) |
| 81 | .map(s -> Key.of(s, appId())) |
| 82 | .orElse(null); |
| 83 | } |
| 84 | } |