Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 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.virtualport.impl; |
| 17 | |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
lishuai | 69b36d5 | 2015-10-21 16:50:16 +0800 | [diff] [blame] | 20 | import java.util.ArrayList; |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 21 | import java.util.Arrays; |
| 22 | import java.util.Collection; |
| 23 | import java.util.Collections; |
lishuai | 69b36d5 | 2015-10-21 16:50:16 +0800 | [diff] [blame] | 24 | import java.util.Iterator; |
| 25 | import java.util.List; |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 26 | import java.util.Map; |
| 27 | import java.util.stream.Collectors; |
| 28 | |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
| 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 34 | import org.apache.felix.scr.annotations.Service; |
| 35 | import org.onlab.packet.IpAddress; |
| 36 | import org.onosproject.core.ApplicationId; |
| 37 | import org.onosproject.core.CoreService; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.store.serializers.KryoNamespaces; |
| 40 | import org.onosproject.store.service.Serializer; |
| 41 | import org.onosproject.store.service.StorageService; |
| 42 | import org.onosproject.vtnrsc.AllowedAddressPair; |
| 43 | import org.onosproject.vtnrsc.BindingHostId; |
| 44 | import org.onosproject.vtnrsc.DefaultVirtualPort; |
| 45 | import org.onosproject.vtnrsc.FixedIp; |
| 46 | import org.onosproject.vtnrsc.SecurityGroup; |
| 47 | import org.onosproject.vtnrsc.SubnetId; |
| 48 | import org.onosproject.vtnrsc.TenantId; |
| 49 | import org.onosproject.vtnrsc.TenantNetworkId; |
| 50 | import org.onosproject.vtnrsc.VirtualPort; |
| 51 | import org.onosproject.vtnrsc.VirtualPortId; |
| 52 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService; |
| 53 | import org.onosproject.vtnrsc.virtualport.VirtualPortService; |
| 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Provides implementation of the VirtualPort APIs. |
| 59 | */ |
| 60 | @Component(immediate = true) |
| 61 | @Service |
| 62 | public class VirtualPortManager implements VirtualPortService { |
| 63 | |
| 64 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 65 | |
| 66 | private static final String VIRTUALPORT = "vtn-virtual-port"; |
| 67 | private static final String VTNRSC_APP = "org.onosproject.vtnrsc"; |
| 68 | |
| 69 | private static final String VIRTUALPORT_ID_NULL = "VirtualPort ID cannot be null"; |
| 70 | private static final String VIRTUALPORT_NOT_NULL = "VirtualPort cannot be null"; |
| 71 | private static final String TENANTID_NOT_NULL = "TenantId cannot be null"; |
| 72 | private static final String NETWORKID_NOT_NULL = "NetworkId cannot be null"; |
| 73 | private static final String DEVICEID_NOT_NULL = "DeviceId cannot be null"; |
lishuai | 69b36d5 | 2015-10-21 16:50:16 +0800 | [diff] [blame] | 74 | private static final String FIXEDIP_NOT_NULL = "FixedIp cannot be null"; |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame^] | 75 | private static final String IP_NOT_NULL = "Ip cannot be null"; |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 76 | |
| 77 | protected Map<VirtualPortId, VirtualPort> vPortStore; |
| 78 | protected ApplicationId appId; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected StorageService storageService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected TenantNetworkService networkService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected CoreService coreService; |
| 88 | |
| 89 | @Activate |
| 90 | public void activate() { |
| 91 | |
| 92 | appId = coreService.registerApplication(VTNRSC_APP); |
| 93 | |
| 94 | vPortStore = storageService.<VirtualPortId, VirtualPort>consistentMapBuilder() |
| 95 | .withName(VIRTUALPORT) |
| 96 | .withApplicationId(appId) |
| 97 | .withPurgeOnUninstall() |
| 98 | .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API), |
| 99 | VirtualPortId.class, |
| 100 | TenantNetworkId.class, |
| 101 | VirtualPort.State.class, |
| 102 | TenantId.class, |
| 103 | AllowedAddressPair.class, |
| 104 | FixedIp.class, |
| 105 | BindingHostId.class, |
| 106 | SecurityGroup.class, |
| 107 | SubnetId.class, |
| 108 | IpAddress.class, |
| 109 | DefaultVirtualPort.class)) |
| 110 | .build().asJavaMap(); |
| 111 | log.info("Started"); |
| 112 | } |
| 113 | |
| 114 | @Deactivate |
| 115 | public void deactivate() { |
| 116 | vPortStore.clear(); |
| 117 | log.info("Stoppped"); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean exists(VirtualPortId vPortId) { |
| 122 | checkNotNull(vPortId, VIRTUALPORT_ID_NULL); |
| 123 | return vPortStore.containsKey(vPortId); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public VirtualPort getPort(VirtualPortId vPortId) { |
| 128 | checkNotNull(vPortId, VIRTUALPORT_ID_NULL); |
| 129 | return vPortStore.get(vPortId); |
| 130 | } |
| 131 | |
| 132 | @Override |
lishuai | 69b36d5 | 2015-10-21 16:50:16 +0800 | [diff] [blame] | 133 | public VirtualPort getPort(FixedIp fixedIP) { |
| 134 | checkNotNull(fixedIP, FIXEDIP_NOT_NULL); |
| 135 | List<VirtualPort> vPorts = new ArrayList<>(); |
| 136 | vPortStore.values().stream().forEach(p -> { |
| 137 | Iterator<FixedIp> fixedIps = p.fixedIps().iterator(); |
| 138 | while (fixedIps.hasNext()) { |
| 139 | if (fixedIps.next().equals(fixedIP)) { |
| 140 | vPorts.add(p); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | }); |
| 145 | if (vPorts.size() == 0) { |
| 146 | return null; |
| 147 | } |
| 148 | return vPorts.get(0); |
| 149 | } |
| 150 | |
| 151 | @Override |
lishuai | 858efd3 | 2015-12-04 14:30:36 +0800 | [diff] [blame^] | 152 | public VirtualPort getPort(TenantNetworkId networkId, IpAddress ip) { |
| 153 | checkNotNull(networkId, NETWORKID_NOT_NULL); |
| 154 | checkNotNull(ip, IP_NOT_NULL); |
| 155 | List<VirtualPort> vPorts = new ArrayList<>(); |
| 156 | vPortStore.values().stream().filter(p -> p.networkId().equals(networkId)) |
| 157 | .forEach(p -> { |
| 158 | Iterator<FixedIp> fixedIps = p.fixedIps().iterator(); |
| 159 | while (fixedIps.hasNext()) { |
| 160 | if (fixedIps.next().ip().equals(ip)) { |
| 161 | vPorts.add(p); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | }); |
| 166 | if (vPorts.size() == 0) { |
| 167 | return null; |
| 168 | } |
| 169 | return vPorts.get(0); |
| 170 | } |
| 171 | |
| 172 | @Override |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 173 | public Collection<VirtualPort> getPorts() { |
| 174 | return Collections.unmodifiableCollection(vPortStore.values()); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public Collection<VirtualPort> getPorts(TenantNetworkId networkId) { |
| 179 | checkNotNull(networkId, NETWORKID_NOT_NULL); |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 180 | return vPortStore.values().stream().filter(d -> d.networkId().equals(networkId)) |
| 181 | .collect(Collectors.toList()); |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public Collection<VirtualPort> getPorts(TenantId tenantId) { |
| 186 | checkNotNull(tenantId, TENANTID_NOT_NULL); |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 187 | return vPortStore.values().stream().filter(d -> d.tenantId().equals(tenantId)) |
| 188 | .collect(Collectors.toList()); |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public Collection<VirtualPort> getPorts(DeviceId deviceId) { |
| 193 | checkNotNull(deviceId, DEVICEID_NOT_NULL); |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 194 | return vPortStore.values().stream().filter(d -> d.deviceId().equals(deviceId)) |
| 195 | .collect(Collectors.toList()); |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public boolean createPorts(Iterable<VirtualPort> vPorts) { |
| 200 | checkNotNull(vPorts, VIRTUALPORT_NOT_NULL); |
| 201 | for (VirtualPort vPort : vPorts) { |
| 202 | log.debug("vPortId is {} ", vPort.portId().toString()); |
| 203 | vPortStore.put(vPort.portId(), vPort); |
| 204 | if (!vPortStore.containsKey(vPort.portId())) { |
| 205 | log.debug("The virtualPort is created failed whose identifier is {} ", |
| 206 | vPort.portId().toString()); |
| 207 | return false; |
| 208 | } |
| 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public boolean updatePorts(Iterable<VirtualPort> vPorts) { |
| 215 | checkNotNull(vPorts, VIRTUALPORT_NOT_NULL); |
Sho SHIMIZU | 0fa6d3b | 2015-10-27 11:38:37 -0700 | [diff] [blame] | 216 | for (VirtualPort vPort : vPorts) { |
| 217 | vPortStore.put(vPort.portId(), vPort); |
| 218 | if (!vPortStore.containsKey(vPort.portId())) { |
| 219 | log.debug("The virtualPort is not exist whose identifier is {}", |
| 220 | vPort.portId().toString()); |
| 221 | return false; |
| 222 | } |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 223 | |
Sho SHIMIZU | 0fa6d3b | 2015-10-27 11:38:37 -0700 | [diff] [blame] | 224 | vPortStore.put(vPort.portId(), vPort); |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 225 | |
Sho SHIMIZU | 0fa6d3b | 2015-10-27 11:38:37 -0700 | [diff] [blame] | 226 | if (!vPort.equals(vPortStore.get(vPort.portId()))) { |
| 227 | log.debug("The virtualPort is updated failed whose identifier is {}", |
| 228 | vPort.portId().toString()); |
| 229 | return false; |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public boolean removePorts(Iterable<VirtualPortId> vPortIds) { |
| 237 | checkNotNull(vPortIds, VIRTUALPORT_ID_NULL); |
Sho SHIMIZU | 0fa6d3b | 2015-10-27 11:38:37 -0700 | [diff] [blame] | 238 | for (VirtualPortId vPortId : vPortIds) { |
| 239 | vPortStore.remove(vPortId); |
| 240 | if (vPortStore.containsKey(vPortId)) { |
| 241 | log.debug("The virtualPort is removed failed whose identifier is {}", |
| 242 | vPortId.toString()); |
| 243 | return false; |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | } |