blob: b3fafa9734753ea0a8572024c8660e0ed6e0abf9 [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;
Ray Milkey4efc66f2018-10-10 13:37:42 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Luca Prete83bac342016-12-06 19:42:05 -080022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.EncapsulationType;
26import org.onosproject.net.config.NetworkConfigService;
27import org.onosproject.sdnip.SdnIp;
Ray Milkey4efc66f2018-10-10 13:37:42 -070028import org.onosproject.sdnip.cli.completer.SdnIpCommandCompleter;
29import org.onosproject.sdnip.cli.completer.SdnIpEncapCompleter;
Luca Prete83bac342016-12-06 19:42:05 -080030import org.onosproject.sdnip.config.SdnIpConfig;
31
32/**
33 * CLI to interact with the SDN-IP application.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
Luca Prete83bac342016-12-06 19:42:05 -080036@Command(scope = "onos", name = "sdnip",
37 description = "Manages the SDN-IP application")
38public class SdnIpCommand extends AbstractShellCommand {
39
40 // Color codes and style
41 private static final String BOLD = "\u001B[1m";
42 private static final String COLOR_ERROR = "\u001B[31m";
43 private static final String RESET = "\u001B[0m";
44
45 // Messages and string formatter
46 private static final String ENCAP_NOT_FOUND =
47 COLOR_ERROR + "Encapsulation type " + BOLD + "%s" + RESET +
48 COLOR_ERROR + " not found" + RESET;
49
50 private static final String SDNIP_COMMAND_NOT_FOUND =
51 COLOR_ERROR + "SDN-IP command " + BOLD + "%s" + RESET + COLOR_ERROR +
52 " not found" + RESET;
53
54 @Argument(index = 0, name = "command", description = "Command name" +
55 " {set-encap}",
56 required = true, multiValued = false)
Ray Milkey4efc66f2018-10-10 13:37:42 -070057 @Completion(SdnIpCommandCompleter.class)
Luca Prete83bac342016-12-06 19:42:05 -080058 String command = null;
59
60 @Argument(index = 1, name = "encapType", description = "The encapsulation" +
61 " type {NONE | VLAN | MPLS}",
62 required = true, multiValued = false)
Ray Milkey4efc66f2018-10-10 13:37:42 -070063 @Completion(SdnIpEncapCompleter.class)
Luca Prete83bac342016-12-06 19:42:05 -080064 String encapType = null;
65
66 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070067 protected void doExecute() {
Luca Prete83bac342016-12-06 19:42:05 -080068 if (command != null) {
69 switch (command) {
70 case "set-encap":
71 setEncap(encapType);
72 break;
73 default:
74 print(SDNIP_COMMAND_NOT_FOUND, command);
75 }
76 }
77 }
78
79 /**
80 * Sets the encapsulation type for SDN-IP.
81 *
82 * @param encap the encapsulation type
83 */
84 private void setEncap(String encap) {
85 EncapsulationType encapType = EncapsulationType.enumFromString(encap);
86 if (encapType.equals(EncapsulationType.NONE) &&
87 !encapType.toString().equals(encap)) {
88 print(ENCAP_NOT_FOUND, encap);
89 return;
90 }
91
92 NetworkConfigService configService = get(NetworkConfigService.class);
93 CoreService coreService = get(CoreService.class);
94 ApplicationId appId = coreService.getAppId(SdnIp.SDN_IP_APP);
95
96 SdnIpConfig config = configService.addConfig(appId, SdnIpConfig.class);
97
98 config.setEncap(encapType);
99 config.apply();
100
101 //configService.applyConfig(appId, SdnIpConfig.class, config.node());
102 }
103}