blob: 113fd44be03bc10a278c30beee50a66245479550 [file] [log] [blame]
lishuai08c1efc2015-12-10 16:53:07 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
lishuai08c1efc2015-12-10 16:53:07 +08003 *
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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070021import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
lishuai08c1efc2015-12-10 16:53:07 +080024import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onosproject.cli.AbstractShellCommand;
lishuai095f6c72016-01-06 16:02:49 +080027import org.onosproject.net.DeviceId;
lishuai08c1efc2015-12-10 16:53:07 +080028import org.onosproject.vtnrsc.BindingHostId;
29import org.onosproject.vtnrsc.DefaultVirtualPort;
30import org.onosproject.vtnrsc.FixedIp;
31import org.onosproject.vtnrsc.Subnet;
32import org.onosproject.vtnrsc.TenantNetwork;
33import org.onosproject.vtnrsc.VirtualPort;
34import org.onosproject.vtnrsc.VirtualPortId;
35import org.onosproject.vtnrsc.subnet.SubnetService;
36import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
37import org.onosproject.vtnrsc.virtualport.VirtualPortService;
38
39import com.google.common.collect.Maps;
40import com.google.common.collect.Sets;
41
42/**
43 * Supports for updating the external gateway virtualPort.
44 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070045@Service
lishuai08c1efc2015-12-10 16:53:07 +080046@Command(scope = "onos", name = "externalgateway-update",
47 description = "Supports for updating the external gateway virtualPort.")
48public 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 Milkey86ad7bb2018-09-27 12:32:28 -070055 protected void doExecute() {
lishuai08c1efc2015-12-10 16:53:07 +080056 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(),
lishuai095f6c72016-01-06 16:02:49 +080092 DeviceId.deviceId(""),
lishuai08c1efc2015-12-10 16:53:07 +080093 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}