Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present 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.opencord.cordvtn.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.opencord.cordvtn.api.core.ServiceNetworkAdminService; |
| 22 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 23 | import org.opencord.cordvtn.api.net.ServicePort; |
| 24 | import org.opencord.cordvtn.rest.XosVtnNetworkingClient; |
| 25 | |
| 26 | import java.util.List; |
| 27 | import java.util.stream.Collectors; |
| 28 | |
| 29 | /** |
| 30 | * Synchronizes network states with XOS VTN service. |
| 31 | * This command can be used to actively synchronize XOS network with VTN |
| 32 | * service network. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "cordvtn-sync-xos-states", |
| 35 | description = "Synchronizes network states with Neutron") |
| 36 | public class CordVtnSyncXosStatesCommand extends AbstractShellCommand { |
| 37 | |
| 38 | @Argument(index = 0, name = "endpoint", description = "XOS VTN service endpoint", |
| 39 | required = true, multiValued = false) |
| 40 | private String endpoint = null; |
| 41 | |
| 42 | @Argument(index = 1, name = "user", description = "XOS admin user name", |
| 43 | required = true, multiValued = false) |
| 44 | private String user = null; |
| 45 | |
| 46 | @Argument(index = 2, name = "password", description = "XOS admin user password", |
| 47 | required = true, multiValued = false) |
| 48 | private String password = null; |
| 49 | |
| 50 | private static final String NET_FORMAT = "%-40s%-20s%-20s%-8s%-20s%s"; |
| 51 | private static final String PORT_FORMAT = "%-40s%-20s%-18s%-8s%s"; |
| 52 | |
| 53 | @Override |
| 54 | protected void execute() { |
| 55 | ServiceNetworkAdminService snetService = |
| 56 | AbstractShellCommand.get(ServiceNetworkAdminService.class); |
| 57 | |
| 58 | XosVtnNetworkingClient client = XosVtnNetworkingClient.builder() |
| 59 | .endpoint(endpoint) |
| 60 | .user(user) |
| 61 | .password(password) |
| 62 | .build(); |
| 63 | |
| 64 | print("Synchronizing service networks..."); |
| 65 | print(NET_FORMAT, "ID", "Name", "Type", "VNI", "Subnet", "Service IP"); |
| 66 | client.serviceNetworks().forEach(snet -> { |
| 67 | if (snetService.serviceNetwork(snet.id()) != null) { |
| 68 | snetService.updateServiceNetwork(snet); |
| 69 | } else { |
| 70 | snetService.createServiceNetwork(snet); |
| 71 | } |
| 72 | ServiceNetwork updated = snetService.serviceNetwork(snet.id()); |
| 73 | print(NET_FORMAT, updated.id(), |
| 74 | updated.name(), |
| 75 | updated.type(), |
| 76 | updated.segmentId(), |
| 77 | updated.subnet(), |
| 78 | updated.serviceIp()); |
| 79 | }); |
| 80 | |
| 81 | // FIXME creating a port fails until XOS service API provides network ID |
| 82 | print("\nSynchronizing service ports..."); |
| 83 | print(PORT_FORMAT, "ID", "MAC", "IP", "VLAN", "WAN IPs"); |
| 84 | client.servicePorts().forEach(sport -> { |
| 85 | if (snetService.servicePort(sport.id()) != null) { |
| 86 | snetService.updateServicePort(sport); |
| 87 | } else { |
| 88 | snetService.createServicePort(sport); |
| 89 | } |
| 90 | ServicePort updated = snetService.servicePort(sport.id()); |
| 91 | List<String> floatingIps = updated.addressPairs().stream() |
| 92 | .map(ip -> ip.ip().toString()) |
| 93 | .collect(Collectors.toList()); |
| 94 | print(PORT_FORMAT, updated.id(), |
| 95 | updated.mac(), |
| 96 | updated.ip(), |
| 97 | updated.vlanId() != null ? updated.vlanId() : "", |
| 98 | floatingIps.isEmpty() ? "" : floatingIps); |
| 99 | }); |
| 100 | } |
| 101 | } |