blob: 1db8780ca643d731eea5df1537748e99519c2586 [file] [log] [blame]
Luca Prete83bac342016-12-06 19:42:05 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Luca Prete83bac342016-12-06 19:42:05 -08003 *
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 */
16package org.onosproject.sdnip.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Luca Prete83bac342016-12-06 19:42:05 -080020import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
23import org.onosproject.net.EncapsulationType;
24import org.onosproject.net.config.NetworkConfigService;
25import org.onosproject.sdnip.SdnIp;
26import 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")
33public 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 Milkey86ad7bb2018-09-27 12:32:28 -070060 protected void doExecute() {
Luca Prete83bac342016-12-06 19:42:05 -080061 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}