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; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 34 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 35 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue; |
| 36 | |
| 37 | /** |
| 38 | * Configure ARP mode. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "openstack-config-arp-mode", |
| 41 | description = "Re-configure ARP mode (proxy | broadcast)") |
| 42 | public class OpenstackConfigArpModeCommand extends AbstractShellCommand { |
| 43 | |
| 44 | private static final String ARP_MODE_NAME = "arpMode"; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 45 | |
| 46 | @Argument(index = 0, name = "arpMode", |
| 47 | description = "ARP mode (proxy | broadcast)", |
| 48 | required = true, multiValued = false) |
| 49 | String arpMode = null; |
| 50 | |
| 51 | @Override |
| 52 | protected void execute() { |
| 53 | |
| 54 | if (checkArpMode(arpMode)) { |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 55 | configArpMode(arpMode); |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 56 | |
| 57 | ComponentConfigService service = get(ComponentConfigService.class); |
| 58 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 59 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 60 | |
| 61 | // we check the arpMode configured in each component, and purge and |
| 62 | // reinstall all rules only if the arpMode is changed to the configured one |
| 63 | while (true) { |
| 64 | String switchingValue = |
| 65 | getPropertyValue(service.getProperties(switchingComponent), ARP_MODE_NAME); |
| 66 | String routingValue = |
| 67 | getPropertyValue(service.getProperties(routingComponent), ARP_MODE_NAME); |
| 68 | |
| 69 | if (arpMode.equals(switchingValue) && arpMode.equals(routingValue)) { |
| 70 | break; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | purgeRules(); |
| 75 | syncRules(); |
| 76 | } |
| 77 | } |
| 78 | |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 79 | private void purgeRules() { |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame^] | 80 | FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 81 | CoreService coreService = get(CoreService.class); |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 82 | ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 83 | if (appId == null) { |
| 84 | error("Failed to purge OpenStack networking flow rules."); |
| 85 | return; |
| 86 | } |
| 87 | flowRuleService.removeFlowRulesById(appId); |
| 88 | } |
| 89 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 90 | private void configArpMode(String arpMode) { |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 91 | ComponentConfigService service = get(ComponentConfigService.class); |
| 92 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 93 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 94 | |
| 95 | if (!isNullOrEmpty(arpMode)) { |
| 96 | service.setProperty(switchingComponent, ARP_MODE_NAME, arpMode); |
| 97 | service.setProperty(routingComponent, ARP_MODE_NAME, arpMode); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private void syncRules() { |
| 102 | // All handlers in this application reacts the node complete event and |
| 103 | // tries to re-configure flow rules for the complete node. |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame^] | 104 | OpenstackNodeService osNodeService = get(OpenstackNodeService.class); |
| 105 | OpenstackNodeAdminService osNodeAdminService = get(OpenstackNodeAdminService.class); |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 106 | if (osNodeService == null) { |
| 107 | error("Failed to re-install flow rules for OpenStack networking."); |
| 108 | return; |
| 109 | } |
| 110 | osNodeService.completeNodes().forEach(osNode -> { |
| 111 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 112 | osNodeAdminService.updateNode(updated); |
| 113 | }); |
| 114 | } |
| 115 | } |