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"; |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 75 | |
| 76 | protected Map<VirtualPortId, VirtualPort> vPortStore; |
| 77 | protected ApplicationId appId; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected StorageService storageService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected TenantNetworkService networkService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected CoreService coreService; |
| 87 | |
| 88 | @Activate |
| 89 | public void activate() { |
| 90 | |
| 91 | appId = coreService.registerApplication(VTNRSC_APP); |
| 92 | |
| 93 | vPortStore = storageService.<VirtualPortId, VirtualPort>consistentMapBuilder() |
| 94 | .withName(VIRTUALPORT) |
| 95 | .withApplicationId(appId) |
| 96 | .withPurgeOnUninstall() |
| 97 | .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API), |
| 98 | VirtualPortId.class, |
| 99 | TenantNetworkId.class, |
| 100 | VirtualPort.State.class, |
| 101 | TenantId.class, |
| 102 | AllowedAddressPair.class, |
| 103 | FixedIp.class, |
| 104 | BindingHostId.class, |
| 105 | SecurityGroup.class, |
| 106 | SubnetId.class, |
| 107 | IpAddress.class, |
| 108 | DefaultVirtualPort.class)) |
| 109 | .build().asJavaMap(); |
| 110 | log.info("Started"); |
| 111 | } |
| 112 | |
| 113 | @Deactivate |
| 114 | public void deactivate() { |
| 115 | vPortStore.clear(); |
| 116 | log.info("Stoppped"); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public boolean exists(VirtualPortId vPortId) { |
| 121 | checkNotNull(vPortId, VIRTUALPORT_ID_NULL); |
| 122 | return vPortStore.containsKey(vPortId); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public VirtualPort getPort(VirtualPortId vPortId) { |
| 127 | checkNotNull(vPortId, VIRTUALPORT_ID_NULL); |
| 128 | return vPortStore.get(vPortId); |
| 129 | } |
| 130 | |
| 131 | @Override |
lishuai | 69b36d5 | 2015-10-21 16:50:16 +0800 | [diff] [blame^] | 132 | public VirtualPort getPort(FixedIp fixedIP) { |
| 133 | checkNotNull(fixedIP, FIXEDIP_NOT_NULL); |
| 134 | List<VirtualPort> vPorts = new ArrayList<>(); |
| 135 | vPortStore.values().stream().forEach(p -> { |
| 136 | Iterator<FixedIp> fixedIps = p.fixedIps().iterator(); |
| 137 | while (fixedIps.hasNext()) { |
| 138 | if (fixedIps.next().equals(fixedIP)) { |
| 139 | vPorts.add(p); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | }); |
| 144 | if (vPorts.size() == 0) { |
| 145 | return null; |
| 146 | } |
| 147 | return vPorts.get(0); |
| 148 | } |
| 149 | |
| 150 | @Override |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 151 | public Collection<VirtualPort> getPorts() { |
| 152 | return Collections.unmodifiableCollection(vPortStore.values()); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Collection<VirtualPort> getPorts(TenantNetworkId networkId) { |
| 157 | checkNotNull(networkId, NETWORKID_NOT_NULL); |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 158 | return vPortStore.values().stream().filter(d -> d.networkId().equals(networkId)) |
| 159 | .collect(Collectors.toList()); |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Collection<VirtualPort> getPorts(TenantId tenantId) { |
| 164 | checkNotNull(tenantId, TENANTID_NOT_NULL); |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 165 | return vPortStore.values().stream().filter(d -> d.tenantId().equals(tenantId)) |
| 166 | .collect(Collectors.toList()); |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Collection<VirtualPort> getPorts(DeviceId deviceId) { |
| 171 | checkNotNull(deviceId, DEVICEID_NOT_NULL); |
lishuai | d2f6a25 | 2015-09-14 15:30:49 +0800 | [diff] [blame] | 172 | return vPortStore.values().stream().filter(d -> d.deviceId().equals(deviceId)) |
| 173 | .collect(Collectors.toList()); |
Sho SHIMIZU | b2b2d98 | 2015-09-11 15:35:06 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public boolean createPorts(Iterable<VirtualPort> vPorts) { |
| 178 | checkNotNull(vPorts, VIRTUALPORT_NOT_NULL); |
| 179 | for (VirtualPort vPort : vPorts) { |
| 180 | log.debug("vPortId is {} ", vPort.portId().toString()); |
| 181 | vPortStore.put(vPort.portId(), vPort); |
| 182 | if (!vPortStore.containsKey(vPort.portId())) { |
| 183 | log.debug("The virtualPort is created failed whose identifier is {} ", |
| 184 | vPort.portId().toString()); |
| 185 | return false; |
| 186 | } |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public boolean updatePorts(Iterable<VirtualPort> vPorts) { |
| 193 | checkNotNull(vPorts, VIRTUALPORT_NOT_NULL); |
| 194 | if (vPorts != null) { |
| 195 | for (VirtualPort vPort : vPorts) { |
| 196 | vPortStore.put(vPort.portId(), vPort); |
| 197 | if (!vPortStore.containsKey(vPort.portId())) { |
| 198 | log.debug("The virtualPort is not exist whose identifier is {}", |
| 199 | vPort.portId().toString()); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | vPortStore.put(vPort.portId(), vPort); |
| 204 | |
| 205 | if (!vPort.equals(vPortStore.get(vPort.portId()))) { |
| 206 | log.debug("The virtualPort is updated failed whose identifier is {}", |
| 207 | vPort.portId().toString()); |
| 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public boolean removePorts(Iterable<VirtualPortId> vPortIds) { |
| 217 | checkNotNull(vPortIds, VIRTUALPORT_ID_NULL); |
| 218 | if (vPortIds != null) { |
| 219 | for (VirtualPortId vPortId : vPortIds) { |
| 220 | vPortStore.remove(vPortId); |
| 221 | if (vPortStore.containsKey(vPortId)) { |
| 222 | log.debug("The virtualPort is removed failed whose identifier is {}", |
| 223 | vPortId.toString()); |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | } |