blob: 1f22d4d9038adf4a1f52b9ff050e422cefbba35c [file] [log] [blame]
/*
* Copyright 2018-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.openstacknetworking.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.flow.FlowRuleService;
import org.onosproject.openstacknetworking.api.Constants;
import org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler;
import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler;
import org.onosproject.openstacknode.api.NodeState;
import org.onosproject.openstacknode.api.OpenstackNode;
import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
import org.onosproject.openstacknode.api.OpenstackNodeService;
import static com.google.common.base.Strings.isNullOrEmpty;
import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue;
/**
* Configure ARP mode.
*/
@Command(scope = "onos", name = "openstack-config-arp-mode",
description = "Re-configure ARP mode (proxy | broadcast)")
public class OpenstackConfigArpModeCommand extends AbstractShellCommand {
private static final String ARP_MODE_NAME = "arpMode";
private static final String PROXY_MODE = "proxy";
private static final String BROADCAST_MODE = "broadcast";
@Argument(index = 0, name = "arpMode",
description = "ARP mode (proxy | broadcast)",
required = true, multiValued = false)
String arpMode = null;
@Override
protected void execute() {
if (checkArpMode(arpMode)) {
configArpMode();
ComponentConfigService service = get(ComponentConfigService.class);
String switchingComponent = OpenstackSwitchingArpHandler.class.getName();
String routingComponent = OpenstackRoutingArpHandler.class.getName();
// we check the arpMode configured in each component, and purge and
// reinstall all rules only if the arpMode is changed to the configured one
while (true) {
String switchingValue =
getPropertyValue(service.getProperties(switchingComponent), ARP_MODE_NAME);
String routingValue =
getPropertyValue(service.getProperties(routingComponent), ARP_MODE_NAME);
if (arpMode.equals(switchingValue) && arpMode.equals(routingValue)) {
break;
}
}
purgeRules();
syncRules();
}
}
private boolean checkArpMode(String arpMode) {
if (isNullOrEmpty(arpMode)) {
error("ARP mode should not be empty string or null");
return false;
} else {
if (arpMode.equals(PROXY_MODE) || arpMode.equals(BROADCAST_MODE)) {
return true;
} else {
error("ARP mode should be either proxy or broadcast");
return false;
}
}
}
private void purgeRules() {
FlowRuleService flowRuleService = AbstractShellCommand.get(FlowRuleService.class);
CoreService coreService = AbstractShellCommand.get(CoreService.class);
ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID);
if (appId == null) {
error("Failed to purge OpenStack networking flow rules.");
return;
}
flowRuleService.removeFlowRulesById(appId);
}
private void configArpMode() {
ComponentConfigService service = get(ComponentConfigService.class);
String switchingComponent = OpenstackSwitchingArpHandler.class.getName();
String routingComponent = OpenstackRoutingArpHandler.class.getName();
if (!isNullOrEmpty(arpMode)) {
service.setProperty(switchingComponent, ARP_MODE_NAME, arpMode);
service.setProperty(routingComponent, ARP_MODE_NAME, arpMode);
}
}
private void syncRules() {
// All handlers in this application reacts the node complete event and
// tries to re-configure flow rules for the complete node.
OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class);
OpenstackNodeAdminService osNodeAdminService = AbstractShellCommand.get(OpenstackNodeAdminService.class);
if (osNodeService == null) {
error("Failed to re-install flow rules for OpenStack networking.");
return;
}
osNodeService.completeNodes().forEach(osNode -> {
OpenstackNode updated = osNode.updateState(NodeState.INIT);
osNodeAdminService.updateNode(updated);
});
}
}