Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -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.sdnip.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.net.EncapsulationType; |
| 25 | import org.onosproject.net.config.NetworkConfigService; |
| 26 | import org.onosproject.sdnip.SdnIp; |
| 27 | import org.onosproject.sdnip.config.SdnIpConfig; |
| 28 | |
| 29 | /** |
| 30 | * CLI to interact with the SDN-IP application. |
| 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 32 | @Service |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 33 | @Command(scope = "onos", name = "sdnip", |
| 34 | description = "Manages the SDN-IP application") |
| 35 | public class SdnIpCommand extends AbstractShellCommand { |
| 36 | |
| 37 | // Color codes and style |
| 38 | private static final String BOLD = "\u001B[1m"; |
| 39 | private static final String COLOR_ERROR = "\u001B[31m"; |
| 40 | private static final String RESET = "\u001B[0m"; |
| 41 | |
| 42 | // Messages and string formatter |
| 43 | private static final String ENCAP_NOT_FOUND = |
| 44 | COLOR_ERROR + "Encapsulation type " + BOLD + "%s" + RESET + |
| 45 | COLOR_ERROR + " not found" + RESET; |
| 46 | |
| 47 | private static final String SDNIP_COMMAND_NOT_FOUND = |
| 48 | COLOR_ERROR + "SDN-IP command " + BOLD + "%s" + RESET + COLOR_ERROR + |
| 49 | " not found" + RESET; |
| 50 | |
| 51 | @Argument(index = 0, name = "command", description = "Command name" + |
| 52 | " {set-encap}", |
| 53 | required = true, multiValued = false) |
| 54 | String command = null; |
| 55 | |
| 56 | @Argument(index = 1, name = "encapType", description = "The encapsulation" + |
| 57 | " type {NONE | VLAN | MPLS}", |
| 58 | required = true, multiValued = false) |
| 59 | String encapType = null; |
| 60 | |
| 61 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 62 | protected void doExecute() { |
Luca Prete | 83bac34 | 2016-12-06 19:42:05 -0800 | [diff] [blame] | 63 | if (command != null) { |
| 64 | switch (command) { |
| 65 | case "set-encap": |
| 66 | setEncap(encapType); |
| 67 | break; |
| 68 | default: |
| 69 | print(SDNIP_COMMAND_NOT_FOUND, command); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets the encapsulation type for SDN-IP. |
| 76 | * |
| 77 | * @param encap the encapsulation type |
| 78 | */ |
| 79 | private void setEncap(String encap) { |
| 80 | EncapsulationType encapType = EncapsulationType.enumFromString(encap); |
| 81 | if (encapType.equals(EncapsulationType.NONE) && |
| 82 | !encapType.toString().equals(encap)) { |
| 83 | print(ENCAP_NOT_FOUND, encap); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | NetworkConfigService configService = get(NetworkConfigService.class); |
| 88 | CoreService coreService = get(CoreService.class); |
| 89 | ApplicationId appId = coreService.getAppId(SdnIp.SDN_IP_APP); |
| 90 | |
| 91 | SdnIpConfig config = configService.addConfig(appId, SdnIpConfig.class); |
| 92 | |
| 93 | config.setEncap(encapType); |
| 94 | config.apply(); |
| 95 | |
| 96 | //configService.applyConfig(appId, SdnIpConfig.class, config.node()); |
| 97 | } |
| 98 | } |