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.subnet.impl;
|
| 17 |
|
| 18 | import static com.google.common.base.Preconditions.checkNotNull;
|
| 19 | import static org.slf4j.LoggerFactory.getLogger;
|
| 20 |
|
| 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.store.service.StorageService;
|
| 31 | import org.onosproject.vtnrsc.Subnet;
|
| 32 | import org.onosproject.vtnrsc.SubnetId;
|
| 33 | import org.onosproject.vtnrsc.subnet.SubnetService;
|
| 34 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
|
| 35 | import org.slf4j.Logger;
|
| 36 |
|
| 37 | /**
|
| 38 | * Provides implementation of the Subnet service.
|
| 39 | */
|
| 40 | @Component(immediate = true)
|
| 41 | @Service
|
| 42 | public class SubnetManager implements SubnetService {
|
| 43 |
|
| 44 | private static final String SUBNET_ID_NULL = "Subnet ID cannot be null";
|
| 45 | private static final String SUBNET_NOT_NULL = "Subnet cannot be null";
|
| 46 |
|
| 47 | private final Logger log = getLogger(getClass());
|
| 48 |
|
| 49 | protected ConcurrentHashMap<SubnetId, Subnet> subnetStore =
|
| 50 | new ConcurrentHashMap<>();
|
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
|
| 52 | protected StorageService storageService;
|
| 53 |
|
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
|
| 55 | protected TenantNetworkService tenantNetworkService;
|
| 56 |
|
| 57 | @Activate
|
| 58 | public void activate() {
|
| 59 | log.info("SubnetManager started");
|
| 60 | }
|
| 61 |
|
| 62 | @Deactivate
|
| 63 | public void deactivate() {
|
| 64 | subnetStore.clear();
|
| 65 | log.info("SubnetManager stopped");
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public Iterable<Subnet> getSubnets() {
|
| 70 | return Collections.unmodifiableCollection(subnetStore.values());
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public Subnet getSubnet(SubnetId subnetId) {
|
| 75 | checkNotNull(subnetId, SUBNET_ID_NULL);
|
| 76 | return subnetStore.get(subnetId);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public boolean exists(SubnetId subnetId) {
|
| 81 | checkNotNull(subnetId, SUBNET_ID_NULL);
|
| 82 | return subnetStore.containsKey(subnetId);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public boolean createSubnets(Iterable<Subnet> subnets) {
|
| 87 | checkNotNull(subnets, SUBNET_NOT_NULL);
|
| 88 | for (Subnet subnet : subnets) {
|
| 89 | if (!tenantNetworkService.exists(subnet.networkId())) {
|
| 90 | log.debug("The network identifier that the subnet {} belong to is not exist",
|
| 91 | subnet.networkId().toString(), subnet.id().toString());
|
| 92 | return false;
|
| 93 | }
|
| 94 | subnetStore.put(subnet.id(), subnet);
|
| 95 | if (!subnetStore.containsKey(subnet.id())) {
|
| 96 | log.debug("The identified subnet whose identifier is {} create failed",
|
| 97 | subnet.id().toString());
|
| 98 | return false;
|
| 99 | }
|
| 100 | }
|
| 101 | return true;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public boolean updateSubnets(Iterable<Subnet> subnets) {
|
| 106 | checkNotNull(subnets, SUBNET_NOT_NULL);
|
| 107 | if (subnets != null) {
|
| 108 | for (Subnet subnet : subnets) {
|
| 109 | if (!subnetStore.containsKey(subnet.id())) {
|
| 110 | log.debug("The subnet is not exist whose identifier is {}",
|
| 111 | subnet.id().toString());
|
| 112 | return false;
|
| 113 | }
|
| 114 |
|
| 115 | subnetStore.put(subnet.id(), subnet);
|
| 116 |
|
| 117 | if (!subnet.equals(subnetStore.get(subnet.id()))) {
|
| 118 | log.debug("The subnet is updated failed whose identifier is {}",
|
| 119 | subnet.id().toString());
|
| 120 | return false;
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 | return true;
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public boolean removeSubnets(Iterable<SubnetId> subnetIds) {
|
| 129 | checkNotNull(subnetIds, SUBNET_ID_NULL);
|
| 130 | if (subnetIds != null) {
|
| 131 | for (SubnetId subnetId : subnetIds) {
|
| 132 | subnetStore.remove(subnetId);
|
| 133 | if (subnetStore.containsKey(subnetId)) {
|
| 134 | log.debug("The subnet created is failed whose identifier is {}",
|
| 135 | subnetId.toString());
|
| 136 | return false;
|
| 137 | }
|
| 138 | }
|
| 139 | }
|
| 140 | return true;
|
| 141 | }
|
| 142 |
|
| 143 | }
|