blob: 1036eefc101bf6467f5d8115d699236b4f89bcee [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Hyunsun Moon53381e82017-03-28 19:58:28 +090022import org.onlab.packet.IpAddress;
23import org.onlab.packet.TpPort;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.incubator.net.virtual.NetworkId;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080026import org.onosproject.net.TenantId;
Jovana Vuletac884b692017-11-28 16:52:35 +010027import org.onosproject.incubator.net.virtual.VirtualNetworkService;
Hyunsun Moon53381e82017-03-28 19:58:28 +090028import org.onosproject.ofagent.api.OFAgent;
29import org.onosproject.ofagent.api.OFAgentAdminService;
30import org.onosproject.ofagent.api.OFController;
31import org.onosproject.ofagent.impl.DefaultOFAgent;
32import org.onosproject.ofagent.impl.DefaultOFController;
33
34import java.util.Set;
35
Jovana Vuletac884b692017-11-28 16:52:35 +010036import static com.google.common.base.Preconditions.checkNotNull;
37
Hyunsun Moon53381e82017-03-28 19:58:28 +090038/**
39 * Creates a new OFAagent.
40 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070041@Service
Hyunsun Moon53381e82017-03-28 19:58:28 +090042@Command(scope = "onos", name = "ofagent-create", description = "Add a new ofagent")
43public class OFAgentCreateCommand extends AbstractShellCommand {
44
45 private static final String PATTERN_IP_PORT = "\\d{1,3}(?:\\.\\d{1,3}){3}(?::\\d{1,5})";
46
47 @Argument(index = 0, name = "network", description = "Virtual network ID",
48 required = true, multiValued = false)
49 private long networkId = NetworkId.NONE.id();
50
51 @Argument(index = 1, name = "controllers",
52 description = "List of external controllers with IP:PORT format",
53 required = false, multiValued = true)
54 private String[] strCtrls = {};
55
56 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070057 protected void doExecute() {
Hyunsun Moon53381e82017-03-28 19:58:28 +090058 Set<OFController> ctrls = Sets.newHashSet();
59 for (String strCtrl : strCtrls) {
60 if (!isValidController(strCtrl)) {
61 print("Invalid controller %s, ignores it.", strCtrl);
62 continue;
63 }
64 String[] temp = strCtrl.split(":");
65 ctrls.add(DefaultOFController.of(IpAddress.valueOf(temp[0]),
66 TpPort.tpPort(Integer.valueOf(temp[1]))));
67 }
68
Jovana Vuletac884b692017-11-28 16:52:35 +010069 VirtualNetworkService virtualNetworkService = get(VirtualNetworkService.class);
70 TenantId tenantId = virtualNetworkService.getTenantId(NetworkId.networkId(networkId));
71 checkNotNull(tenantId, "Virtual network %s does not have tenant.", networkId);
Hyunsun Moon53381e82017-03-28 19:58:28 +090072 OFAgentAdminService adminService = get(OFAgentAdminService.class);
73 OFAgent ofAgent = DefaultOFAgent.builder()
74 .networkId(NetworkId.networkId(networkId))
Jovana Vuletac884b692017-11-28 16:52:35 +010075 .tenantId(tenantId)
Hyunsun Moon53381e82017-03-28 19:58:28 +090076 .controllers(ctrls)
77 .state(OFAgent.State.STOPPED)
78 .build();
79 adminService.createAgent(ofAgent);
Jovana Vuletac884b692017-11-28 16:52:35 +010080 print("Successfully created OFAgent for network %s, tenant %s", networkId, tenantId);
Hyunsun Moon53381e82017-03-28 19:58:28 +090081 }
82
83 private boolean isValidController(String ctrl) {
84 if (!ctrl.matches(PATTERN_IP_PORT)) {
85 return false;
86 }
87
88 String[] temp = ctrl.split(":");
89 try {
90 IpAddress.valueOf(temp[0]);
91 TpPort.tpPort(Integer.valueOf(temp[1]));
92 return true;
93 } catch (IllegalArgumentException e) {
94 return false;
95 }
96 }
97}