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