blob: 2181114413eebdc82a14f49778f69f74da5952fb [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 Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Luca Prete83bac342016-12-06 19:42:05 -080021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.EncapsulationType;
25import org.onosproject.net.config.NetworkConfigService;
26import org.onosproject.sdnip.SdnIp;
27import org.onosproject.sdnip.config.SdnIpConfig;
28
29/**
30 * CLI to interact with the SDN-IP application.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Luca Prete83bac342016-12-06 19:42:05 -080033@Command(scope = "onos", name = "sdnip",
34 description = "Manages the SDN-IP application")
35public 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 Milkey86ad7bb2018-09-27 12:32:28 -070062 protected void doExecute() {
Luca Prete83bac342016-12-06 19:42:05 -080063 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}