lishuai
Committed by Gerrit Code Review

[ONOS-3584] Add the cli of updatting external gateway macadress.

Change-Id: I8956b8508cbfa7905447ec0f183478b3f308bf7b
1 +/*
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 + */
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;
26 +import org.onosproject.vtnrsc.BindingHostId;
27 +import org.onosproject.vtnrsc.DefaultVirtualPort;
28 +import org.onosproject.vtnrsc.FixedIp;
29 +import org.onosproject.vtnrsc.Subnet;
30 +import org.onosproject.vtnrsc.TenantNetwork;
31 +import org.onosproject.vtnrsc.VirtualPort;
32 +import org.onosproject.vtnrsc.VirtualPortId;
33 +import org.onosproject.vtnrsc.subnet.SubnetService;
34 +import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
35 +import org.onosproject.vtnrsc.virtualport.VirtualPortService;
36 +
37 +import com.google.common.collect.Maps;
38 +import 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.")
45 +public 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 +}
...@@ -53,6 +53,9 @@ ...@@ -53,6 +53,9 @@
53 <action class="org.onosproject.vtnrsc.cli.virtualport.VirtualPortUpdateCommand"/> 53 <action class="org.onosproject.vtnrsc.cli.virtualport.VirtualPortUpdateCommand"/>
54 </command> 54 </command>
55 <command> 55 <command>
56 + <action class="org.onosproject.vtnrsc.cli.virtualport.VirtualPortExGwUpdateCommand"/>
57 + </command>
58 + <command>
56 <action class="org.onosproject.vtnrsc.cli.floatingip.FloatingIpCreateCommand"/> 59 <action class="org.onosproject.vtnrsc.cli.floatingip.FloatingIpCreateCommand"/>
57 </command> 60 </command>
58 <command> 61 <command>
......