lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [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.onosproject.vtn.util; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Collection; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 20 | import java.util.Iterator; |
| 21 | import java.util.List; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 22 | |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 24 | import org.onosproject.net.AnnotationKeys; |
| 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Port; |
| 28 | import org.onosproject.net.PortNumber; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 29 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 30 | import org.onosproject.vtnrsc.FixedIp; |
| 31 | import org.onosproject.vtnrsc.TenantNetworkId; |
| 32 | import org.onosproject.vtnrsc.VirtualPort; |
| 33 | import org.onosproject.vtnrsc.VirtualPortId; |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
| 37 | import com.google.common.collect.Sets; |
| 38 | |
| 39 | /** |
| 40 | * VtnData utility class. |
| 41 | */ |
| 42 | public final class VtnData { |
| 43 | |
| 44 | private static final Logger log = LoggerFactory.getLogger(VtnData.class); |
| 45 | private static final String SWITCH_CHANNEL_ID = "channelId"; |
| 46 | private static final String PORT_HEAD = "vxlan"; |
| 47 | |
| 48 | /** |
| 49 | * Constructs a VtnData object. Utility classes should not have a public or |
| 50 | * default constructor, otherwise IDE will compile unsuccessfully. This |
| 51 | * class should not be instantiated. |
| 52 | */ |
| 53 | private VtnData() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the ControllerIp from the device . |
| 58 | * |
| 59 | * @param device Device |
| 60 | * @return Controller Ip |
| 61 | */ |
| 62 | public static String getControllerIpOfSwitch(Device device) { |
| 63 | String url = device.annotations().value(SWITCH_CHANNEL_ID); |
| 64 | return url.substring(0, url.lastIndexOf(":")); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the ControllerId from the device . |
| 69 | * |
| 70 | * @param device Device |
| 71 | * @param devices Devices |
| 72 | * @return Controller Id |
| 73 | */ |
| 74 | public static DeviceId getControllerId(Device device, |
| 75 | Iterable<Device> devices) { |
| 76 | for (Device d : devices) { |
| 77 | if (d.type() == Device.Type.CONTROLLER && d.id().toString() |
| 78 | .contains(getControllerIpOfSwitch(device))) { |
| 79 | return d.id(); |
| 80 | } |
| 81 | } |
| 82 | log.info("Can not find controller for device : {}", device.id()); |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get local tunnel ports. |
| 88 | * |
| 89 | * @param ports Iterable of Port |
| 90 | * @return Collection of PortNumber |
| 91 | */ |
| 92 | public static Collection<PortNumber> getLocalTunnelPorts(Iterable<Port> ports) { |
| 93 | Collection<PortNumber> localTunnelPorts = new ArrayList<>(); |
| 94 | Sets.newHashSet(ports).stream() |
| 95 | .filter(p -> !p.number().equals(PortNumber.LOCAL)) |
| 96 | .forEach(p -> { |
| 97 | if (p.annotations().value(AnnotationKeys.PORT_NAME) |
| 98 | .startsWith(PORT_HEAD)) { |
| 99 | localTunnelPorts.add(p.number()); |
| 100 | } |
| 101 | }); |
| 102 | return localTunnelPorts; |
| 103 | } |
| 104 | |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame] | 105 | /** |
| 106 | * Get VirtualPort. |
| 107 | * |
| 108 | * @param vPortStore EventuallyConsistentMap of VirtualPort |
| 109 | * @param vPortId VirtualPortId of the VirtualPort |
| 110 | * @return VirtualPort |
| 111 | */ |
| 112 | public static VirtualPort getPort(EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore, |
| 113 | VirtualPortId vPortId) { |
| 114 | if (vPortStore != null) { |
| 115 | return vPortStore.get(vPortId); |
| 116 | } |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get VirtualPort. |
| 122 | * |
| 123 | * @param vPortStore EventuallyConsistentMap of VirtualPort |
| 124 | * @param fixedIP FixedIp of the VirtualPort |
| 125 | * @return VirtualPort |
| 126 | */ |
| 127 | public static VirtualPort getPort(EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore, |
| 128 | FixedIp fixedIP) { |
| 129 | if (vPortStore != null) { |
| 130 | List<VirtualPort> vPorts = new ArrayList<>(); |
| 131 | vPortStore.values().stream().forEach(p -> { |
| 132 | Iterator<FixedIp> fixedIps = p.fixedIps().iterator(); |
| 133 | while (fixedIps.hasNext()) { |
| 134 | if (fixedIps.next().equals(fixedIP)) { |
| 135 | vPorts.add(p); |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | }); |
| 140 | if (vPorts.size() == 0) { |
| 141 | return null; |
| 142 | } |
| 143 | return vPorts.get(0); |
| 144 | } |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get VirtualPort. |
| 150 | * |
| 151 | * @param vPortStore EventuallyConsistentMap of VirtualPort |
| 152 | * @param networkId TenantNetworkId of the VirtualPort |
| 153 | * @param ip IpAddress of the VirtualPort |
| 154 | * @return VirtualPort |
| 155 | */ |
| 156 | public static VirtualPort getPort(EventuallyConsistentMap<VirtualPortId, VirtualPort> vPortStore, |
| 157 | TenantNetworkId networkId, IpAddress ip) { |
| 158 | if (vPortStore != null) { |
| 159 | List<VirtualPort> vPorts = new ArrayList<>(); |
| 160 | vPortStore.values().stream() |
| 161 | .filter(p -> p.networkId().equals(networkId)) |
| 162 | .forEach(p -> { |
| 163 | Iterator<FixedIp> fixedIps = p.fixedIps().iterator(); |
| 164 | while (fixedIps.hasNext()) { |
| 165 | if (fixedIps.next().ip().equals(ip)) { |
| 166 | vPorts.add(p); |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | }); |
| 171 | if (vPorts.size() == 0) { |
| 172 | return null; |
| 173 | } |
| 174 | return vPorts.get(0); |
| 175 | } |
| 176 | return null; |
| 177 | } |
| 178 | |
lishuai | 6c56f5e | 2015-11-17 16:38:19 +0800 | [diff] [blame] | 179 | } |