blob: 52cfe2345f1a7d77dac2e7a9002d0d3bab52510f [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
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;
Hyunsun Moon53381e82017-03-28 19:58:28 +090021import org.onlab.packet.IpAddress;
22import org.onlab.packet.TpPort;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.incubator.net.virtual.NetworkId;
25import org.onosproject.ofagent.api.OFAgent;
26import org.onosproject.ofagent.api.OFAgentAdminService;
27import org.onosproject.ofagent.api.OFAgentService;
28import org.onosproject.ofagent.impl.DefaultOFAgent;
29import org.onosproject.ofagent.impl.DefaultOFController;
30
31/**
32 * Adds a controller to the OFAgent.
33 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070034@Service
Hyunsun Moon53381e82017-03-28 19:58:28 +090035@Command(scope = "onos", name = "ofagent-controller-add",
36 description = "Add a controller to the ofagent")
37public class OFAgentAddControllerCommand extends AbstractShellCommand {
38
39 private static final String PATTERN_IP_PORT = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})";
40
41 @Argument(index = 0, name = "network", description = "Virtual network ID",
42 required = true, multiValued = false)
43 private long networkId = NetworkId.NONE.id();
44
45 @Argument(index = 1, name = "controller",
46 description = "External controller with IP:PORT format",
47 required = true, multiValued = false)
48 private String strCtrl;
49
50 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070051 protected void doExecute() {
Hyunsun Moon53381e82017-03-28 19:58:28 +090052 if (!isValidController(strCtrl)) {
53 error("Invalid controller string %s, must be IP:PORT", strCtrl);
54 return;
55 }
56
57 OFAgentService service = get(OFAgentService.class);
58 OFAgentAdminService adminService = get(OFAgentAdminService.class);
59
60 OFAgent existing = service.agent(NetworkId.networkId(networkId));
61 if (existing == null) {
62 error("OFAgent for network %s does not exist", networkId);
63 return;
64 }
65
66 String[] temp = strCtrl.split(":");
67 OFAgent updated = DefaultOFAgent.builder()
68 .from(existing)
69 .addController(DefaultOFController.of(
70 IpAddress.valueOf(temp[0]),
71 TpPort.tpPort(Integer.valueOf(temp[1]))))
72 .build();
73 adminService.updateAgent(updated);
74 }
75
76 private boolean isValidController(String ctrl) {
77 if (!ctrl.matches(PATTERN_IP_PORT)) {
78 return false;
79 }
80
81 String[] temp = ctrl.split(":");
82 try {
83 IpAddress.valueOf(temp[0]);
84 TpPort.tpPort(Integer.valueOf(temp[1]));
85 return true;
86 } catch (IllegalArgumentException e) {
87 return false;
88 }
89 }
90}