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