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 |
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 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;
|
jiangrui | ac0879a | 2015-09-08 16:58:49 +0800 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress;
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId;
|
| 26 | import org.onosproject.core.CoreService;
|
| 27 | import org.onosproject.store.serializers.KryoNamespaces;
|
| 28 | import org.onosproject.store.service.Serializer;
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 29 | import org.onosproject.store.service.StorageService;
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 30 | import org.onosproject.vtnrsc.AllocationPool;
|
jiangrui | ac0879a | 2015-09-08 16:58:49 +0800 | [diff] [blame] | 31 | import org.onosproject.vtnrsc.DefaultSubnet;
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 32 | import org.onosproject.vtnrsc.HostRoute;
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 33 | import org.onosproject.vtnrsc.Subnet;
|
| 34 | import org.onosproject.vtnrsc.SubnetId;
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 35 | import org.onosproject.vtnrsc.TenantId;
|
| 36 | import org.onosproject.vtnrsc.TenantNetworkId;
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 37 | import org.onosproject.vtnrsc.subnet.SubnetService;
|
| 38 | import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
|
| 39 | import org.slf4j.Logger;
|
| 40 |
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 41 | import java.util.Arrays;
|
| 42 | import java.util.Collections;
|
| 43 | import java.util.Map;
|
| 44 |
|
| 45 | import static com.google.common.base.Preconditions.checkNotNull;
|
| 46 | import static org.slf4j.LoggerFactory.getLogger;
|
| 47 |
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 48 | /**
|
| 49 | * Provides implementation of the Subnet service.
|
| 50 | */
|
| 51 | @Component(immediate = true)
|
| 52 | @Service
|
| 53 | public class SubnetManager implements SubnetService {
|
| 54 |
|
| 55 | private static final String SUBNET_ID_NULL = "Subnet ID cannot be null";
|
| 56 | private static final String SUBNET_NOT_NULL = "Subnet cannot be null";
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 57 | private static final String SUBNET = "vtn-subnet-store";
|
| 58 | private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
|
| 59 |
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 60 |
|
| 61 | private final Logger log = getLogger(getClass());
|
| 62 |
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 63 | protected Map<SubnetId, Subnet> subnetStore;
|
| 64 | protected ApplicationId appId;
|
| 65 |
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
|
| 67 | protected StorageService storageService;
|
| 68 |
|
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 70 | protected CoreService coreService;
|
| 71 |
|
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 73 | protected TenantNetworkService tenantNetworkService;
|
| 74 |
|
| 75 | @Activate
|
| 76 | public void activate() {
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 77 |
|
| 78 | appId = coreService.registerApplication(VTNRSC_APP);
|
| 79 |
|
| 80 | subnetStore = storageService.<SubnetId, Subnet>consistentMapBuilder()
|
| 81 | .withName(SUBNET)
|
| 82 | .withApplicationId(appId)
|
| 83 | .withPurgeOnUninstall()
|
| 84 | .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
|
| 85 | Subnet.class,
|
| 86 | SubnetId.class,
|
| 87 | TenantNetworkId.class,
|
| 88 | TenantId.class,
|
| 89 | HostRoute.class,
|
| 90 | Subnet.Mode.class,
|
jiangrui | ac0879a | 2015-09-08 16:58:49 +0800 | [diff] [blame] | 91 | AllocationPool.class,
|
| 92 | DefaultSubnet.class,
|
| 93 | IpAddress.Version.class))
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 94 | .build().asJavaMap();
|
| 95 |
|
| 96 | log.info("Started");
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 97 | }
|
| 98 |
|
| 99 | @Deactivate
|
| 100 | public void deactivate() {
|
alshabib | d9b7000 | 2015-09-01 17:00:37 -0700 | [diff] [blame] | 101 | log.info("Stopped");
|
CNlucius | 5b2fff1 | 2015-08-20 14:13:46 +0800 | [diff] [blame] | 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public Iterable<Subnet> getSubnets() {
|
| 106 | return Collections.unmodifiableCollection(subnetStore.values());
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public Subnet getSubnet(SubnetId subnetId) {
|
| 111 | checkNotNull(subnetId, SUBNET_ID_NULL);
|
| 112 | return subnetStore.get(subnetId);
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public boolean exists(SubnetId subnetId) {
|
| 117 | checkNotNull(subnetId, SUBNET_ID_NULL);
|
| 118 | return subnetStore.containsKey(subnetId);
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public boolean createSubnets(Iterable<Subnet> subnets) {
|
| 123 | checkNotNull(subnets, SUBNET_NOT_NULL);
|
| 124 | for (Subnet subnet : subnets) {
|
| 125 | if (!tenantNetworkService.exists(subnet.networkId())) {
|
| 126 | log.debug("The network identifier that the subnet {} belong to is not exist",
|
| 127 | subnet.networkId().toString(), subnet.id().toString());
|
| 128 | return false;
|
| 129 | }
|
| 130 | subnetStore.put(subnet.id(), subnet);
|
| 131 | if (!subnetStore.containsKey(subnet.id())) {
|
| 132 | log.debug("The identified subnet whose identifier is {} create failed",
|
| 133 | subnet.id().toString());
|
| 134 | return false;
|
| 135 | }
|
| 136 | }
|
| 137 | return true;
|
| 138 | }
|
| 139 |
|
| 140 | @Override
|
| 141 | public boolean updateSubnets(Iterable<Subnet> subnets) {
|
| 142 | checkNotNull(subnets, SUBNET_NOT_NULL);
|
| 143 | if (subnets != null) {
|
| 144 | for (Subnet subnet : subnets) {
|
| 145 | if (!subnetStore.containsKey(subnet.id())) {
|
| 146 | log.debug("The subnet is not exist whose identifier is {}",
|
| 147 | subnet.id().toString());
|
| 148 | return false;
|
| 149 | }
|
| 150 |
|
| 151 | subnetStore.put(subnet.id(), subnet);
|
| 152 |
|
| 153 | if (!subnet.equals(subnetStore.get(subnet.id()))) {
|
| 154 | log.debug("The subnet is updated failed whose identifier is {}",
|
| 155 | subnet.id().toString());
|
| 156 | return false;
|
| 157 | }
|
| 158 | }
|
| 159 | }
|
| 160 | return true;
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public boolean removeSubnets(Iterable<SubnetId> subnetIds) {
|
| 165 | checkNotNull(subnetIds, SUBNET_ID_NULL);
|
| 166 | if (subnetIds != null) {
|
| 167 | for (SubnetId subnetId : subnetIds) {
|
| 168 | subnetStore.remove(subnetId);
|
| 169 | if (subnetStore.containsKey(subnetId)) {
|
| 170 | log.debug("The subnet created is failed whose identifier is {}",
|
| 171 | subnetId.toString());
|
| 172 | return false;
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 | return true;
|
| 177 | }
|
| 178 |
|
| 179 | }
|