Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [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.opencord.cordvtn.cli; |
| 17 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.opencord.cordvtn.api.core.ServiceNetworkAdminService; |
| 25 | import org.opencord.cordvtn.api.net.NetworkId; |
| 26 | import org.opencord.cordvtn.api.net.PortId; |
| 27 | import org.opencord.cordvtn.api.net.SegmentId; |
| 28 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 29 | import org.opencord.cordvtn.api.net.ServicePort; |
| 30 | import org.opencord.cordvtn.impl.DefaultServiceNetwork; |
| 31 | import org.opencord.cordvtn.impl.DefaultServicePort; |
| 32 | import org.openstack4j.api.OSClient; |
| 33 | import org.openstack4j.api.exceptions.AuthenticationException; |
| 34 | import org.openstack4j.model.identity.Access; |
| 35 | import org.openstack4j.openstack.OSFactory; |
| 36 | |
| 37 | import java.util.List; |
| 38 | import java.util.stream.Collectors; |
| 39 | |
| 40 | /** |
| 41 | * Synchronizes network states with OpenStack Neutron. |
| 42 | * This command can be used to actively synchronize Neutron network with VTN |
| 43 | * service network. |
| 44 | */ |
| 45 | @Command(scope = "onos", name = "cordvtn-sync-neutron-states", |
| 46 | description = "Synchronizes network states with Neutron") |
| 47 | public class CordVtnSyncNeutronStatesCommand extends AbstractShellCommand { |
| 48 | |
| 49 | @Argument(index = 0, name = "endpoint", description = "OpenStack service endpoint", |
| 50 | required = true, multiValued = false) |
| 51 | private String endpoint = null; |
| 52 | |
| 53 | @Argument(index = 1, name = "tenant", description = "OpenStack admin tenant name", |
| 54 | required = true, multiValued = false) |
| 55 | private String tenant = null; |
| 56 | |
| 57 | @Argument(index = 2, name = "user", description = "OpenStack admin user name", |
| 58 | required = true, multiValued = false) |
| 59 | private String user = null; |
| 60 | |
| 61 | @Argument(index = 3, name = "password", description = "OpenStack admin user password", |
| 62 | required = true, multiValued = false) |
| 63 | private String password = null; |
| 64 | |
Hyunsun Moon | 5510e34 | 2017-02-23 19:41:00 +0900 | [diff] [blame] | 65 | private static final String PORT_NAME_PREFIX = "tap"; |
| 66 | private static final String NET_FORMAT = "%-40s%-30s%-20s%-8s%-20s%s"; |
| 67 | private static final String PORT_FORMAT = "%-40s%-30s%-20s%-18s%-10s%s"; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 68 | |
| 69 | @Override |
| 70 | protected void execute() { |
| 71 | ServiceNetworkAdminService snetService = |
| 72 | AbstractShellCommand.get(ServiceNetworkAdminService.class); |
| 73 | Access osAccess; |
| 74 | try { |
| 75 | osAccess = OSFactory.builder() |
| 76 | .endpoint(this.endpoint) |
| 77 | .tenantName(this.tenant) |
| 78 | .credentials(this.user, this.password) |
| 79 | .authenticate() |
| 80 | .getAccess(); |
| 81 | } catch (AuthenticationException e) { |
| 82 | print("Authentication failed"); |
| 83 | return; |
| 84 | } catch (Exception e) { |
| 85 | print("OpenStack service endpoint is unreachable"); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | print("Synchronizing service networks..."); |
| 90 | print(NET_FORMAT, "ID", "Name", "Type", "VNI", "Subnet", "Service IP"); |
| 91 | OSClient osClient = OSFactory.clientFromAccess(osAccess); |
| 92 | osClient.networking().network().list().forEach(osNet -> { |
| 93 | ServiceNetwork snet = DefaultServiceNetwork.builder() |
| 94 | .id(NetworkId.of(osNet.getId())) |
| 95 | .name(osNet.getName()) |
| 96 | .type(ServiceNetwork.NetworkType.PRIVATE) |
| 97 | .segmentId(SegmentId.of(Long.valueOf(osNet.getProviderSegID()))) |
| 98 | .build(); |
| 99 | try { |
| 100 | if (snetService.serviceNetwork(snet.id()) != null) { |
| 101 | snetService.updateServiceNetwork(snet); |
| 102 | } else { |
| 103 | snetService.createServiceNetwork(snet); |
| 104 | } |
| 105 | } catch (Exception ignore) { |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | osClient.networking().subnet().list().forEach(osSubnet -> { |
| 110 | try { |
| 111 | ServiceNetwork snet = DefaultServiceNetwork.builder() |
| 112 | .id(NetworkId.of(osSubnet.getNetworkId())) |
| 113 | .subnet(IpPrefix.valueOf(osSubnet.getCidr())) |
| 114 | .serviceIp(IpAddress.valueOf(osSubnet.getGateway())) |
| 115 | .build(); |
| 116 | snetService.updateServiceNetwork(snet); |
| 117 | ServiceNetwork updated = snetService.serviceNetwork(snet.id()); |
| 118 | print(NET_FORMAT, updated.id(), |
| 119 | updated.name(), |
| 120 | updated.type(), |
| 121 | updated.segmentId(), |
| 122 | updated.subnet(), |
| 123 | updated.serviceIp()); |
| 124 | } catch (Exception e) { |
| 125 | print(e.getMessage()); |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | print("\nSynchronizing service ports..."); |
Hyunsun Moon | 5510e34 | 2017-02-23 19:41:00 +0900 | [diff] [blame] | 130 | print(PORT_FORMAT, "ID", "Name", "MAC", "IP", "VLAN", "WAN IPs"); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 131 | osClient.networking().port().list().forEach(osPort -> { |
| 132 | ServicePort.Builder sportBuilder = DefaultServicePort.builder() |
| 133 | .id(PortId.of(osPort.getId())) |
Hyunsun Moon | 5510e34 | 2017-02-23 19:41:00 +0900 | [diff] [blame] | 134 | .name(PORT_NAME_PREFIX + osPort.getId().substring(0, 11)) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 135 | .networkId(NetworkId.of(osPort.getNetworkId())); |
| 136 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 137 | if (osPort.getMacAddress() != null) { |
| 138 | sportBuilder.mac(MacAddress.valueOf(osPort.getMacAddress())); |
| 139 | } |
| 140 | if (!osPort.getFixedIps().isEmpty()) { |
| 141 | sportBuilder.ip(IpAddress.valueOf( |
| 142 | osPort.getFixedIps().iterator().next().getIpAddress())); |
| 143 | } |
| 144 | ServicePort sport = sportBuilder.build(); |
| 145 | if (snetService.servicePort(sport.id()) != null) { |
| 146 | snetService.updateServicePort(sport); |
| 147 | } else { |
| 148 | snetService.createServicePort(sport); |
| 149 | } |
| 150 | ServicePort updated = snetService.servicePort(sport.id()); |
| 151 | List<String> floatingIps = updated.addressPairs().stream() |
| 152 | .map(ip -> ip.ip().toString()) |
| 153 | .collect(Collectors.toList()); |
| 154 | print(PORT_FORMAT, updated.id(), |
Hyunsun Moon | 5510e34 | 2017-02-23 19:41:00 +0900 | [diff] [blame] | 155 | updated.name(), |
| 156 | updated.mac(), |
| 157 | updated.ip(), |
| 158 | updated.vlanId() != null ? updated.vlanId() : "", |
| 159 | floatingIps.isEmpty() ? "" : floatingIps); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 160 | }); |
| 161 | } |
| 162 | } |