CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [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 static com.google.common.base.Preconditions.checkNotNull;
|
| 19 |
|
| 20 | import java.util.Collection;
|
| 21 | import java.util.Collections;
|
| 22 | import java.util.concurrent.ConcurrentHashMap;
|
| 23 |
|
| 24 | import org.apache.felix.scr.annotations.Activate;
|
| 25 | import org.apache.felix.scr.annotations.Component;
|
| 26 | import org.apache.felix.scr.annotations.Deactivate;
|
| 27 | import org.apache.felix.scr.annotations.Reference;
|
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality;
|
| 29 | import org.apache.felix.scr.annotations.Service;
|
| 30 | import org.onosproject.net.DeviceId;
|
| 31 | import org.onosproject.store.service.StorageService;
|
| 32 | import org.onosproject.vtnrsc.TenantId;
|
| 33 | import org.onosproject.vtnrsc.TenantNetworkId;
|
| 34 | import org.onosproject.vtnrsc.VirtualPort;
|
| 35 | import org.onosproject.vtnrsc.VirtualPortId;
|
| 36 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
|
| 37 | import org.onosproject.vtnrsc.virtualport.VirtualPortService;
|
| 38 | import org.slf4j.Logger;
|
| 39 | import org.slf4j.LoggerFactory;
|
| 40 |
|
| 41 | /**
|
| 42 | * Provides implementation of the VirtualPort APIs.
|
| 43 | */
|
| 44 | @Component(immediate = true)
|
| 45 | @Service
|
| 46 | public class VirtualPortManager implements VirtualPortService {
|
| 47 |
|
| 48 | private final Logger log = LoggerFactory.getLogger(getClass());
|
| 49 |
|
| 50 | private static final String VIRTUALPORT_ID_NULL = "VirtualPort ID cannot be null";
|
| 51 | private static final String VIRTUALPORT_NOT_NULL = "VirtualPort cannot be null";
|
| 52 | private static final String TENANTID_NOT_NULL = "TenantId cannot be null";
|
| 53 | private static final String NETWORKID_NOT_NULL = "NetworkId cannot be null";
|
| 54 | private static final String DEVICEID_NOT_NULL = "DeviceId cannot be null";
|
| 55 |
|
| 56 | protected ConcurrentHashMap<VirtualPortId, VirtualPort> vPortStore =
|
| 57 | new ConcurrentHashMap<>();
|
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
|
| 59 | protected StorageService storageService;
|
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
|
| 61 | protected TenantNetworkService networkService;
|
| 62 |
|
| 63 | @Activate
|
| 64 | public void activate() {
|
| 65 | log.info("Started");
|
| 66 | }
|
| 67 |
|
| 68 | @Deactivate
|
| 69 | public void deactivate() {
|
| 70 | vPortStore.clear();
|
| 71 | log.info("Stoppped");
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public boolean exists(VirtualPortId vPortId) {
|
| 76 | checkNotNull(vPortId, VIRTUALPORT_ID_NULL);
|
| 77 | return vPortStore.containsKey(vPortId);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public VirtualPort getPort(VirtualPortId vPortId) {
|
| 82 | checkNotNull(vPortId, VIRTUALPORT_ID_NULL);
|
| 83 | return vPortStore.get(vPortId);
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public Collection<VirtualPort> getPorts() {
|
| 88 | return Collections.unmodifiableCollection(vPortStore.values());
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | public Collection<VirtualPort> getPorts(TenantNetworkId networkId) {
|
| 93 | checkNotNull(networkId, NETWORKID_NOT_NULL);
|
| 94 | Collection<VirtualPort> vPortWithNetworkIds = vPortStore.values();
|
| 95 | for (VirtualPort vPort : vPortWithNetworkIds) {
|
| 96 | if (!vPort.networkId().equals(networkId)) {
|
| 97 | vPortWithNetworkIds.remove(vPort);
|
| 98 | }
|
| 99 | }
|
| 100 | return vPortWithNetworkIds;
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public Collection<VirtualPort> getPorts(TenantId tenantId) {
|
| 105 | checkNotNull(tenantId, TENANTID_NOT_NULL);
|
| 106 | Collection<VirtualPort> vPortWithTenantIds = vPortStore.values();
|
| 107 | for (VirtualPort vPort : vPortWithTenantIds) {
|
| 108 | if (!vPort.tenantId().equals(tenantId)) {
|
| 109 | vPortWithTenantIds.remove(vPort);
|
| 110 | }
|
| 111 | }
|
| 112 | return vPortWithTenantIds;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public Collection<VirtualPort> getPorts(DeviceId deviceId) {
|
| 117 | checkNotNull(deviceId, DEVICEID_NOT_NULL);
|
| 118 | Collection<VirtualPort> vPortWithDeviceIds = vPortStore.values();
|
| 119 | for (VirtualPort vPort : vPortWithDeviceIds) {
|
| 120 | if (!vPort.deviceId().equals(deviceId)) {
|
| 121 | vPortWithDeviceIds.remove(vPort);
|
| 122 | }
|
| 123 | }
|
| 124 | return vPortWithDeviceIds;
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public boolean createPorts(Iterable<VirtualPort> vPorts) {
|
| 129 | checkNotNull(vPorts, VIRTUALPORT_NOT_NULL);
|
| 130 | for (VirtualPort vPort : vPorts) {
|
| 131 | log.debug("vPortId is {} ", vPort.portId().toString());
|
| 132 | vPortStore.put(vPort.portId(), vPort);
|
| 133 | if (!vPortStore.containsKey(vPort.portId())) {
|
| 134 | log.debug("The virtualPort is created failed whose identifier is {} ",
|
| 135 | vPort.portId().toString());
|
| 136 | return false;
|
| 137 | }
|
| 138 | }
|
| 139 | return true;
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public boolean updatePorts(Iterable<VirtualPort> vPorts) {
|
| 144 | checkNotNull(vPorts, VIRTUALPORT_NOT_NULL);
|
| 145 | if (vPorts != null) {
|
| 146 | for (VirtualPort vPort : vPorts) {
|
| 147 | vPortStore.put(vPort.portId(), vPort);
|
| 148 | if (!vPortStore.containsKey(vPort.portId())) {
|
| 149 | log.debug("The virtualPort is not exist whose identifier is {}",
|
| 150 | vPort.portId().toString());
|
| 151 | return false;
|
| 152 | }
|
| 153 |
|
| 154 | vPortStore.put(vPort.portId(), vPort);
|
| 155 |
|
| 156 | if (!vPort.equals(vPortStore.get(vPort.portId()))) {
|
| 157 | log.debug("The virtualPort is updated failed whose identifier is {}",
|
| 158 | vPort.portId().toString());
|
| 159 | return false;
|
| 160 | }
|
| 161 | }
|
| 162 | }
|
| 163 | return true;
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public boolean removePorts(Iterable<VirtualPortId> vPortIds) {
|
| 168 | checkNotNull(vPortIds, VIRTUALPORT_ID_NULL);
|
| 169 | if (vPortIds != null) {
|
| 170 | for (VirtualPortId vPortId : vPortIds) {
|
| 171 | vPortStore.remove(vPortId);
|
| 172 | if (vPortStore.containsKey(vPortId)) {
|
| 173 | log.debug("The virtualPort is removed failed whose identifier is {}",
|
| 174 | vPortId.toString());
|
| 175 | return false;
|
| 176 | }
|
| 177 | }
|
| 178 | }
|
| 179 | return true;
|
| 180 | }
|
| 181 |
|
| 182 | }
|