blob: dec34c7b36d670e302d48be78600d33d5f6546c6 [file] [log] [blame]
Jian Li7f70bb72018-07-06 23:35:30 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
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.openstacknetworking.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;
Jian Lidb521c12018-11-19 17:42:35 +090020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Li7f70bb72018-07-06 23:35:30 +090022import org.onosproject.cfg.ComponentConfigService;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.flow.FlowRuleService;
27import org.onosproject.openstacknetworking.api.Constants;
28import org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler;
29import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler;
30import org.onosproject.openstacknode.api.NodeState;
31import org.onosproject.openstacknode.api.OpenstackNode;
32import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
33import org.onosproject.openstacknode.api.OpenstackNodeService;
34
35import static com.google.common.base.Strings.isNullOrEmpty;
Jian Li7f024de2018-07-07 03:51:02 +090036import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode;
Jian Li7f70bb72018-07-06 23:35:30 +090037import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue;
38
39/**
40 * Configure ARP mode.
41 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070042@Service
Jian Li7f70bb72018-07-06 23:35:30 +090043@Command(scope = "onos", name = "openstack-config-arp-mode",
44 description = "Re-configure ARP mode (proxy | broadcast)")
45public class OpenstackConfigArpModeCommand extends AbstractShellCommand {
46
47 private static final String ARP_MODE_NAME = "arpMode";
Jian Li7f70bb72018-07-06 23:35:30 +090048
49 @Argument(index = 0, name = "arpMode",
50 description = "ARP mode (proxy | broadcast)",
51 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090052 @Completion(ArpModeCompleter.class)
Jian Li7f70bb72018-07-06 23:35:30 +090053 String arpMode = null;
54
55 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070056 protected void doExecute() {
Jian Li7f70bb72018-07-06 23:35:30 +090057
58 if (checkArpMode(arpMode)) {
Jian Li7f024de2018-07-07 03:51:02 +090059 configArpMode(arpMode);
Jian Li7f70bb72018-07-06 23:35:30 +090060
61 ComponentConfigService service = get(ComponentConfigService.class);
62 String switchingComponent = OpenstackSwitchingArpHandler.class.getName();
63 String routingComponent = OpenstackRoutingArpHandler.class.getName();
64
65 // we check the arpMode configured in each component, and purge and
66 // reinstall all rules only if the arpMode is changed to the configured one
67 while (true) {
68 String switchingValue =
69 getPropertyValue(service.getProperties(switchingComponent), ARP_MODE_NAME);
70 String routingValue =
71 getPropertyValue(service.getProperties(routingComponent), ARP_MODE_NAME);
72
73 if (arpMode.equals(switchingValue) && arpMode.equals(routingValue)) {
74 break;
75 }
76 }
77
78 purgeRules();
79 syncRules();
80 }
81 }
82
Jian Li7f70bb72018-07-06 23:35:30 +090083 private void purgeRules() {
Jian Lif1efbe52018-07-17 23:20:16 +090084 FlowRuleService flowRuleService = get(FlowRuleService.class);
85 CoreService coreService = get(CoreService.class);
Jian Li7f70bb72018-07-06 23:35:30 +090086 ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID);
87 if (appId == null) {
88 error("Failed to purge OpenStack networking flow rules.");
89 return;
90 }
91 flowRuleService.removeFlowRulesById(appId);
92 }
93
Jian Li7f024de2018-07-07 03:51:02 +090094 private void configArpMode(String arpMode) {
Jian Li7f70bb72018-07-06 23:35:30 +090095 ComponentConfigService service = get(ComponentConfigService.class);
96 String switchingComponent = OpenstackSwitchingArpHandler.class.getName();
97 String routingComponent = OpenstackRoutingArpHandler.class.getName();
98
99 if (!isNullOrEmpty(arpMode)) {
100 service.setProperty(switchingComponent, ARP_MODE_NAME, arpMode);
101 service.setProperty(routingComponent, ARP_MODE_NAME, arpMode);
102 }
103 }
104
105 private void syncRules() {
106 // All handlers in this application reacts the node complete event and
107 // tries to re-configure flow rules for the complete node.
Jian Lif1efbe52018-07-17 23:20:16 +0900108 OpenstackNodeService osNodeService = get(OpenstackNodeService.class);
109 OpenstackNodeAdminService osNodeAdminService = get(OpenstackNodeAdminService.class);
Jian Li7f70bb72018-07-06 23:35:30 +0900110 if (osNodeService == null) {
111 error("Failed to re-install flow rules for OpenStack networking.");
112 return;
113 }
114 osNodeService.completeNodes().forEach(osNode -> {
115 OpenstackNode updated = osNode.updateState(NodeState.INIT);
116 osNodeAdminService.updateNode(updated);
117 });
118 }
119}