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