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