Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cfg.ComponentConfigService; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.net.flow.FlowRuleService; |
| 25 | import org.onosproject.openstacknetworking.api.Constants; |
| 26 | import org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler; |
| 27 | import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler; |
| 28 | import org.onosproject.openstacknode.api.NodeState; |
| 29 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 30 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 31 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 32 | |
| 33 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 34 | import 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)") |
| 41 | public 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 | } |