blob: 66d39610b0e28e3c36365dd61985c07be0f911d3 [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 com.google.common.collect.Sets;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
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;
Jovana Vuletac884b692017-11-28 16:52:35 +010025import org.onosproject.incubator.net.virtual.TenantId;
26import org.onosproject.incubator.net.virtual.VirtualNetworkService;
Hyunsun Moon53381e82017-03-28 19:58:28 +090027import org.onosproject.ofagent.api.OFAgent;
28import org.onosproject.ofagent.api.OFAgentAdminService;
29import org.onosproject.ofagent.api.OFController;
30import org.onosproject.ofagent.impl.DefaultOFAgent;
31import org.onosproject.ofagent.impl.DefaultOFController;
32
33import java.util.Set;
34
Jovana Vuletac884b692017-11-28 16:52:35 +010035import static com.google.common.base.Preconditions.checkNotNull;
36
Hyunsun Moon53381e82017-03-28 19:58:28 +090037/**
38 * Creates a new OFAagent.
39 */
40@Command(scope = "onos", name = "ofagent-create", description = "Add a new ofagent")
41public class OFAgentCreateCommand extends AbstractShellCommand {
42
43 private static final String PATTERN_IP_PORT = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})";
44
45 @Argument(index = 0, name = "network", description = "Virtual network ID",
46 required = true, multiValued = false)
47 private long networkId = NetworkId.NONE.id();
48
49 @Argument(index = 1, name = "controllers",
50 description = "List of external controllers with IP:PORT format",
51 required = false, multiValued = true)
52 private String[] strCtrls = {};
53
54 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070055 protected void doExecute() {
Hyunsun Moon53381e82017-03-28 19:58:28 +090056 Set<OFController> ctrls = Sets.newHashSet();
57 for (String strCtrl : strCtrls) {
58 if (!isValidController(strCtrl)) {
59 print("Invalid controller %s, ignores it.", strCtrl);
60 continue;
61 }
62 String[] temp = strCtrl.split(":");
63 ctrls.add(DefaultOFController.of(IpAddress.valueOf(temp[0]),
64 TpPort.tpPort(Integer.valueOf(temp[1]))));
65 }
66
Jovana Vuletac884b692017-11-28 16:52:35 +010067 VirtualNetworkService virtualNetworkService = get(VirtualNetworkService.class);
68 TenantId tenantId = virtualNetworkService.getTenantId(NetworkId.networkId(networkId));
69 checkNotNull(tenantId, "Virtual network %s does not have tenant.", networkId);
Hyunsun Moon53381e82017-03-28 19:58:28 +090070 OFAgentAdminService adminService = get(OFAgentAdminService.class);
71 OFAgent ofAgent = DefaultOFAgent.builder()
72 .networkId(NetworkId.networkId(networkId))
Jovana Vuletac884b692017-11-28 16:52:35 +010073 .tenantId(tenantId)
Hyunsun Moon53381e82017-03-28 19:58:28 +090074 .controllers(ctrls)
75 .state(OFAgent.State.STOPPED)
76 .build();
77 adminService.createAgent(ofAgent);
Jovana Vuletac884b692017-11-28 16:52:35 +010078 print("Successfully created OFAgent for network %s, tenant %s", networkId, tenantId);
Hyunsun Moon53381e82017-03-28 19:58:28 +090079 }
80
81 private boolean isValidController(String ctrl) {
82 if (!ctrl.matches(PATTERN_IP_PORT)) {
83 return false;
84 }
85
86 String[] temp = ctrl.split(":");
87 try {
88 IpAddress.valueOf(temp[0]);
89 TpPort.tpPort(Integer.valueOf(temp[1]));
90 return true;
91 } catch (IllegalArgumentException e) {
92 return false;
93 }
94 }
95}