blob: 042df7111f32bf93f8a9f8625ba94ebca1a70300 [file] [log] [blame]
Hyunsun Moon53381e82017-03-28 19:58:28 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon53381e82017-03-28 19:58:28 +09003 *
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.ofagent.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.TpPort;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.incubator.net.virtual.NetworkId;
24import org.onosproject.ofagent.api.OFAgent;
25import org.onosproject.ofagent.api.OFAgentAdminService;
26import org.onosproject.ofagent.api.OFAgentService;
27import org.onosproject.ofagent.impl.DefaultOFAgent;
28import org.onosproject.ofagent.impl.DefaultOFController;
29
30/**
31 * Removes the controller from the OFAgent.
32 */
33@Command(scope = "onos", name = "ofagent-controller-delete",
34 description = "Deletes a controller from the ofagent")
35public class OFAgentDeleteControllerCommand extends AbstractShellCommand {
36
37 private static final String PATTERN_IP_PORT = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})";
38
39 @Argument(index = 0, name = "network", description = "Virtual network ID",
40 required = true, multiValued = false)
41 private long networkId = NetworkId.NONE.id();
42
43 @Argument(index = 1, name = "controller",
44 description = "External controller with IP:PORT format",
45 required = true, multiValued = false)
46 private String strCtrl;
47
48 @Override
49 protected void execute() {
50 if (!isValidController(strCtrl)) {
51 error("Invalid controller string %s, must be IP:PORT", strCtrl);
52 return;
53 }
54
55 OFAgentService service = get(OFAgentService.class);
56 OFAgentAdminService adminService = get(OFAgentAdminService.class);
57
58 OFAgent existing = service.agent(NetworkId.networkId(networkId));
59 if (existing == null) {
60 error("OFAgent for network %s does not exist", networkId);
61 return;
62 }
63
64 String[] temp = strCtrl.split(":");
65 OFAgent updated = DefaultOFAgent.builder()
66 .from(existing)
67 .deleteController(DefaultOFController.of(
68 IpAddress.valueOf(temp[0]),
69 TpPort.tpPort(Integer.valueOf(temp[1]))))
70 .build();
71 adminService.updateAgent(updated);
72 }
73
74 private boolean isValidController(String ctrl) {
75 if (!ctrl.matches(PATTERN_IP_PORT)) {
76 return false;
77 }
78
79 String[] temp = ctrl.split(":");
80 try {
81 IpAddress.valueOf(temp[0]);
82 TpPort.tpPort(Integer.valueOf(temp[1]));
83 return true;
84 } catch (IllegalArgumentException e) {
85 return false;
86 }
87 }
88}