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 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Jian Li | db521c1 | 2018-11-19 17:42:35 +0900 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 22 | import org.onosproject.cfg.ComponentConfigService; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.flow.FlowRuleService; |
| 27 | import org.onosproject.openstacknetworking.api.Constants; |
| 28 | import org.onosproject.openstacknetworking.impl.OpenstackRoutingArpHandler; |
| 29 | import org.onosproject.openstacknetworking.impl.OpenstackSwitchingArpHandler; |
| 30 | import org.onosproject.openstacknode.api.NodeState; |
| 31 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 32 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 33 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 34 | |
| 35 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 36 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.checkArpMode; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 37 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValue; |
| 38 | |
| 39 | /** |
| 40 | * Configure ARP mode. |
| 41 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 42 | @Service |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 43 | @Command(scope = "onos", name = "openstack-config-arp-mode", |
| 44 | description = "Re-configure ARP mode (proxy | broadcast)") |
| 45 | public class OpenstackConfigArpModeCommand extends AbstractShellCommand { |
| 46 | |
| 47 | private static final String ARP_MODE_NAME = "arpMode"; |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 48 | |
| 49 | @Argument(index = 0, name = "arpMode", |
| 50 | description = "ARP mode (proxy | broadcast)", |
| 51 | required = true, multiValued = false) |
Jian Li | db521c1 | 2018-11-19 17:42:35 +0900 | [diff] [blame] | 52 | @Completion(ArpModeCompleter.class) |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 53 | String arpMode = null; |
| 54 | |
| 55 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 56 | protected void doExecute() { |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 57 | |
| 58 | if (checkArpMode(arpMode)) { |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 59 | configArpMode(arpMode); |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 60 | |
| 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 = |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 69 | getPropertyValue( |
| 70 | service.getProperties(switchingComponent), ARP_MODE_NAME); |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 71 | String routingValue = |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 72 | getPropertyValue( |
| 73 | service.getProperties(routingComponent), ARP_MODE_NAME); |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 74 | |
| 75 | if (arpMode.equals(switchingValue) && arpMode.equals(routingValue)) { |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | purgeRules(); |
| 81 | syncRules(); |
| 82 | } |
| 83 | } |
| 84 | |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 85 | private void purgeRules() { |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 86 | FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 87 | CoreService coreService = get(CoreService.class); |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 88 | ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 89 | if (appId == null) { |
| 90 | error("Failed to purge OpenStack networking flow rules."); |
| 91 | return; |
| 92 | } |
| 93 | flowRuleService.removeFlowRulesById(appId); |
| 94 | } |
| 95 | |
Jian Li | 7f024de | 2018-07-07 03:51:02 +0900 | [diff] [blame] | 96 | private void configArpMode(String arpMode) { |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 97 | ComponentConfigService service = get(ComponentConfigService.class); |
| 98 | String switchingComponent = OpenstackSwitchingArpHandler.class.getName(); |
| 99 | String routingComponent = OpenstackRoutingArpHandler.class.getName(); |
| 100 | |
| 101 | if (!isNullOrEmpty(arpMode)) { |
| 102 | service.setProperty(switchingComponent, ARP_MODE_NAME, arpMode); |
| 103 | service.setProperty(routingComponent, ARP_MODE_NAME, arpMode); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private void syncRules() { |
| 108 | // All handlers in this application reacts the node complete event and |
| 109 | // tries to re-configure flow rules for the complete node. |
Jian Li | f1efbe5 | 2018-07-17 23:20:16 +0900 | [diff] [blame] | 110 | OpenstackNodeService osNodeService = get(OpenstackNodeService.class); |
| 111 | OpenstackNodeAdminService osNodeAdminService = get(OpenstackNodeAdminService.class); |
Jian Li | 7f70bb7 | 2018-07-06 23:35:30 +0900 | [diff] [blame] | 112 | if (osNodeService == null) { |
| 113 | error("Failed to re-install flow rules for OpenStack networking."); |
| 114 | return; |
| 115 | } |
| 116 | osNodeService.completeNodes().forEach(osNode -> { |
| 117 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 118 | osNodeAdminService.updateNode(updated); |
| 119 | }); |
| 120 | } |
| 121 | } |