blob: 1f22d4d9038adf4a1f52b9ff050e422cefbba35c [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cfg.ComponentConfigService;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.flow.FlowRuleService;
25import org.onosproject.openstacknetworking.api.Constants;
26import org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler;
27import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler;
28import org.onosproject.openstacknode.api.NodeState;
29import org.onosproject.openstacknode.api.OpenstackNode;
30import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
31import org.onosproject.openstacknode.api.OpenstackNodeService;
32
33import static com.google.common.base.Strings.isNullOrEmpty;
34import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue;
35
36/**
37 * Configure ARP mode.
38 */
39@Command(scope = "onos", name = "openstack-config-arp-mode",
40 description = "Re-configure ARP mode (proxy | broadcast)")
41public class OpenstackConfigArpModeCommand extends AbstractShellCommand {
42
43 private static final String ARP_MODE_NAME = "arpMode";
44 private static final String PROXY_MODE = "proxy";
45 private static final String BROADCAST_MODE = "broadcast";
46
47 @Argument(index = 0, name = "arpMode",
48 description = "ARP mode (proxy | broadcast)",
49 required = true, multiValued = false)
50 String arpMode = null;
51
52 @Override
53 protected void execute() {
54
55 if (checkArpMode(arpMode)) {
56 configArpMode();
57
58 ComponentConfigService service = get(ComponentConfigService.class);
59 String switchingComponent = OpenstackSwitchingArpHandler.class.getName();
60 String routingComponent = OpenstackRoutingArpHandler.class.getName();
61
62 // we check the arpMode configured in each component, and purge and
63 // reinstall all rules only if the arpMode is changed to the configured one
64 while (true) {
65 String switchingValue =
66 getPropertyValue(service.getProperties(switchingComponent), ARP_MODE_NAME);
67 String routingValue =
68 getPropertyValue(service.getProperties(routingComponent), ARP_MODE_NAME);
69
70 if (arpMode.equals(switchingValue) && arpMode.equals(routingValue)) {
71 break;
72 }
73 }
74
75 purgeRules();
76 syncRules();
77 }
78 }
79
80 private boolean checkArpMode(String arpMode) {
81
82 if (isNullOrEmpty(arpMode)) {
83 error("ARP mode should not be empty string or null");
84 return false;
85 } else {
86 if (arpMode.equals(PROXY_MODE) || arpMode.equals(BROADCAST_MODE)) {
87 return true;
88 } else {
89 error("ARP mode should be either proxy or broadcast");
90 return false;
91 }
92 }
93 }
94
95 private void purgeRules() {
96 FlowRuleService flowRuleService = AbstractShellCommand.get(FlowRuleService.class);
97 CoreService coreService = AbstractShellCommand.get(CoreService.class);
98 ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID);
99 if (appId == null) {
100 error("Failed to purge OpenStack networking flow rules.");
101 return;
102 }
103 flowRuleService.removeFlowRulesById(appId);
104 }
105
106 private void configArpMode() {
107 ComponentConfigService service = get(ComponentConfigService.class);
108 String switchingComponent = OpenstackSwitchingArpHandler.class.getName();
109 String routingComponent = OpenstackRoutingArpHandler.class.getName();
110
111 if (!isNullOrEmpty(arpMode)) {
112 service.setProperty(switchingComponent, ARP_MODE_NAME, arpMode);
113 service.setProperty(routingComponent, ARP_MODE_NAME, arpMode);
114 }
115 }
116
117 private void syncRules() {
118 // All handlers in this application reacts the node complete event and
119 // tries to re-configure flow rules for the complete node.
120 OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class);
121 OpenstackNodeAdminService osNodeAdminService = AbstractShellCommand.get(OpenstackNodeAdminService.class);
122 if (osNodeService == null) {
123 error("Failed to re-install flow rules for OpenStack networking.");
124 return;
125 }
126 osNodeService.completeNodes().forEach(osNode -> {
127 OpenstackNode updated = osNode.updateState(NodeState.INIT);
128 osNodeAdminService.updateNode(updated);
129 });
130 }
131}