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