Jian Li | 2525721 | 2019-03-26 13:31:14 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.Completion; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 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.OpenstackRoutingSnatHandler; |
| 29 | import org.onosproject.openstacknode.api.NodeState; |
| 30 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 31 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 32 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 33 | |
| 34 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.getPropertyValueAsBoolean; |
| 35 | |
| 36 | /** |
| 37 | * Configure stateful SNAT mode. |
| 38 | */ |
| 39 | @Service |
| 40 | @Command(scope = "onos", name = "openstack-config-stateful-snat", |
| 41 | description = "Re-configure stateful SNAT mode (true | false)") |
| 42 | public class OpenstackConfigStatefulSnatCommand extends AbstractShellCommand { |
| 43 | |
| 44 | private static final String USE_STATEFUL_SNAT = "useStatefulSnat"; |
| 45 | |
| 46 | @Argument(index = 0, name = "statefulSnat", |
| 47 | description = "Stateful SNAT mode (true | false)", |
| 48 | required = true, multiValued = false) |
| 49 | @Completion(BooleanCompleter.class) |
| 50 | boolean statefulSnat; |
| 51 | |
| 52 | @Override |
| 53 | protected void doExecute() { |
| 54 | |
| 55 | configSnatMode(statefulSnat); |
| 56 | |
| 57 | ComponentConfigService service = get(ComponentConfigService.class); |
| 58 | String snatComponent = OpenstackRoutingSnatHandler.class.getName(); |
| 59 | |
| 60 | while (true) { |
| 61 | boolean snatValue = |
| 62 | getPropertyValueAsBoolean( |
| 63 | service.getProperties(snatComponent), USE_STATEFUL_SNAT); |
| 64 | |
| 65 | if (statefulSnat == snatValue) { |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | purgeRules(); |
| 71 | syncRules(); |
| 72 | } |
| 73 | |
| 74 | private void configSnatMode(boolean snatMode) { |
| 75 | ComponentConfigService service = get(ComponentConfigService.class); |
| 76 | String snatComponent = OpenstackRoutingSnatHandler.class.getName(); |
| 77 | |
| 78 | service.setProperty(snatComponent, USE_STATEFUL_SNAT, String.valueOf(snatMode)); |
| 79 | } |
| 80 | |
| 81 | private void purgeRules() { |
| 82 | FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 83 | CoreService coreService = get(CoreService.class); |
| 84 | ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 85 | if (appId == null) { |
| 86 | error("Failed to purge OpenStack networking flow rules."); |
| 87 | return; |
| 88 | } |
| 89 | flowRuleService.removeFlowRulesById(appId); |
| 90 | } |
| 91 | |
| 92 | private void syncRules() { |
| 93 | // All handlers in this application reacts the node complete event and |
| 94 | // tries to re-configure flow rules for the complete node. |
| 95 | OpenstackNodeService osNodeService = get(OpenstackNodeService.class); |
| 96 | OpenstackNodeAdminService osNodeAdminService = get(OpenstackNodeAdminService.class); |
| 97 | if (osNodeService == null) { |
| 98 | error("Failed to re-install flow rules for OpenStack networking."); |
| 99 | return; |
| 100 | } |
| 101 | osNodeService.completeNodes().forEach(osNode -> { |
| 102 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 103 | osNodeAdminService.updateNode(updated); |
| 104 | }); |
| 105 | } |
| 106 | } |