lishuai | 08c1efc | 2015-12-10 16:53:07 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
lishuai | 08c1efc | 2015-12-10 16:53:07 +0800 | [diff] [blame] | 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.vtnrsc.cli.virtualport; |
| 17 | |
| 18 | import java.util.Map; |
| 19 | import java.util.Set; |
| 20 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
lishuai | 08c1efc | 2015-12-10 16:53:07 +0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
lishuai | 095f6c7 | 2016-01-06 16:02:49 +0800 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
lishuai | 08c1efc | 2015-12-10 16:53:07 +0800 | [diff] [blame] | 28 | import org.onosproject.vtnrsc.BindingHostId; |
| 29 | import org.onosproject.vtnrsc.DefaultVirtualPort; |
| 30 | import org.onosproject.vtnrsc.FixedIp; |
| 31 | import org.onosproject.vtnrsc.Subnet; |
| 32 | import org.onosproject.vtnrsc.TenantNetwork; |
| 33 | import org.onosproject.vtnrsc.VirtualPort; |
| 34 | import org.onosproject.vtnrsc.VirtualPortId; |
| 35 | import org.onosproject.vtnrsc.subnet.SubnetService; |
| 36 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; |
| 37 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
| 38 | |
| 39 | import com.google.common.collect.Maps; |
| 40 | import com.google.common.collect.Sets; |
| 41 | |
| 42 | /** |
| 43 | * Supports for updating the external gateway virtualPort. |
| 44 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 45 | @Service |
lishuai | 08c1efc | 2015-12-10 16:53:07 +0800 | [diff] [blame] | 46 | @Command(scope = "onos", name = "externalgateway-update", |
| 47 | description = "Supports for updating the external gateway virtualPort.") |
| 48 | public class VirtualPortExGwUpdateCommand extends AbstractShellCommand { |
| 49 | |
| 50 | @Option(name = "-m", aliases = "--macAddress", description = "MAC address.", required = true, |
| 51 | multiValued = false) |
| 52 | String macAddress = ""; |
| 53 | |
| 54 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 55 | protected void doExecute() { |
lishuai | 08c1efc | 2015-12-10 16:53:07 +0800 | [diff] [blame] | 56 | VirtualPortService service = get(VirtualPortService.class); |
| 57 | SubnetService subnetService = get(SubnetService.class); |
| 58 | TenantNetworkService tenantNetworkService = get(TenantNetworkService.class); |
| 59 | Iterable<TenantNetwork> networks = tenantNetworkService.getNetworks(); |
| 60 | if (networks != null) { |
| 61 | for (TenantNetwork network : networks) { |
| 62 | if (network.routerExternal()) { |
| 63 | Iterable<Subnet> subnets = subnetService.getSubnets(); |
| 64 | if (subnets != null) { |
| 65 | for (Subnet subnet : subnets) { |
| 66 | if (network.id().networkId().equals(subnet.networkId().networkId())) { |
| 67 | IpAddress exgwip = subnet.gatewayIp(); |
| 68 | FixedIp fixedGwIp = FixedIp.fixedIp(subnet.id(), exgwip); |
| 69 | VirtualPort exgwPort = service.getPort(fixedGwIp); |
| 70 | if (exgwPort == null) { |
| 71 | createExGwPort(network, subnet, fixedGwIp); |
| 72 | } else { |
| 73 | updateExGwPort(exgwPort); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private void createExGwPort(TenantNetwork network, Subnet subnet, FixedIp fixedGwIp) { |
| 84 | VirtualPortService service = get(VirtualPortService.class); |
| 85 | Map<String, String> strMap = Maps.newHashMap(); |
| 86 | VirtualPort virtualPort = new DefaultVirtualPort(VirtualPortId.portId("externalgateway-update-id"), |
| 87 | network.id(), |
| 88 | false, strMap, |
| 89 | VirtualPort.State.DOWN, |
| 90 | MacAddress.valueOf(macAddress), |
| 91 | subnet.tenantId(), |
lishuai | 095f6c7 | 2016-01-06 16:02:49 +0800 | [diff] [blame] | 92 | DeviceId.deviceId(""), |
lishuai | 08c1efc | 2015-12-10 16:53:07 +0800 | [diff] [blame] | 93 | Sets.newHashSet(fixedGwIp), |
| 94 | BindingHostId.bindingHostId(""), |
| 95 | Sets.newHashSet(), |
| 96 | Sets.newHashSet()); |
| 97 | Set<VirtualPort> virtualPorts = Sets.newHashSet(virtualPort); |
| 98 | service.createPorts(virtualPorts); |
| 99 | } |
| 100 | |
| 101 | private void updateExGwPort(VirtualPort exgwPort) { |
| 102 | VirtualPortService service = get(VirtualPortService.class); |
| 103 | Map<String, String> strMap = Maps.newHashMap(); |
| 104 | strMap.putIfAbsent("name", exgwPort.name()); |
| 105 | strMap.putIfAbsent("deviceOwner", exgwPort.deviceOwner()); |
| 106 | strMap.putIfAbsent("bindingvnicType", exgwPort.bindingVnicType()); |
| 107 | strMap.putIfAbsent("bindingvifType", exgwPort.bindingVifType()); |
| 108 | strMap.putIfAbsent("bindingvnicDetails", exgwPort.bindingVifDetails()); |
| 109 | VirtualPort virtualPort = new DefaultVirtualPort(exgwPort.portId(), |
| 110 | exgwPort.networkId(), |
| 111 | false, strMap, |
| 112 | VirtualPort.State.DOWN, |
| 113 | MacAddress.valueOf(macAddress), |
| 114 | exgwPort.tenantId(), |
| 115 | exgwPort.deviceId(), |
| 116 | exgwPort.fixedIps(), |
| 117 | exgwPort.bindingHostId(), |
| 118 | Sets.newHashSet(exgwPort |
| 119 | .allowedAddressPairs()), |
| 120 | Sets.newHashSet(exgwPort |
| 121 | .securityGroups())); |
| 122 | Set<VirtualPort> virtualPorts = Sets.newHashSet(virtualPort); |
| 123 | service.updatePorts(virtualPorts); |
| 124 | } |
| 125 | } |