2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.vtnrsc.subnet.impl;
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.store.serializers.KryoNamespaces;
28 import org.onosproject.store.service.Serializer;
29 import org.onosproject.store.service.StorageService;
30 import org.onosproject.vtnrsc.AllocationPool;
31 import org.onosproject.vtnrsc.DefaultAllocationPool;
32 import org.onosproject.vtnrsc.DefaultHostRoute;
33 import org.onosproject.vtnrsc.DefaultSubnet;
34 import org.onosproject.vtnrsc.HostRoute;
35 import org.onosproject.vtnrsc.Subnet;
36 import org.onosproject.vtnrsc.SubnetId;
37 import org.onosproject.vtnrsc.TenantId;
38 import org.onosproject.vtnrsc.TenantNetworkId;
39 import org.onosproject.vtnrsc.subnet.SubnetService;
40 import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
41 import org.slf4j.Logger;
43 import java.util.Arrays;
44 import java.util.Collections;
47 import static com.google.common.base.Preconditions.checkNotNull;
48 import static org.slf4j.LoggerFactory.getLogger;
51 * Provides implementation of the Subnet service.
53 @Component(immediate = true)
55 public class SubnetManager implements SubnetService {
57 private static final String SUBNET_ID_NULL = "Subnet ID cannot be null";
58 private static final String SUBNET_NOT_NULL = "Subnet cannot be null";
59 private static final String SUBNET = "vtn-subnet-store";
60 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
63 private final Logger log = getLogger(getClass());
65 protected Map<SubnetId, Subnet> subnetStore;
66 protected ApplicationId appId;
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected StorageService storageService;
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected CoreService coreService;
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected TenantNetworkService tenantNetworkService;
78 public void activate() {
80 appId = coreService.registerApplication(VTNRSC_APP);
82 subnetStore = storageService.<SubnetId, Subnet>consistentMapBuilder()
84 .withApplicationId(appId)
85 .withPurgeOnUninstall()
86 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
89 TenantNetworkId.class,
92 DefaultHostRoute.class,
95 DefaultAllocationPool.class,
97 IpAddress.Version.class))
104 public void deactivate() {
109 public Iterable<Subnet> getSubnets() {
110 return Collections.unmodifiableCollection(subnetStore.values());
114 public Subnet getSubnet(SubnetId subnetId) {
115 checkNotNull(subnetId, SUBNET_ID_NULL);
116 return subnetStore.get(subnetId);
120 public boolean exists(SubnetId subnetId) {
121 checkNotNull(subnetId, SUBNET_ID_NULL);
122 return subnetStore.containsKey(subnetId);
126 public boolean createSubnets(Iterable<Subnet> subnets) {
127 checkNotNull(subnets, SUBNET_NOT_NULL);
128 for (Subnet subnet : subnets) {
129 if (!tenantNetworkService.exists(subnet.networkId())) {
130 log.debug("The network identifier that the subnet {} belong to is not exist",
131 subnet.networkId().toString(), subnet.id().toString());
134 subnetStore.put(subnet.id(), subnet);
135 if (!subnetStore.containsKey(subnet.id())) {
136 log.debug("The identified subnet whose identifier is {} create failed",
137 subnet.id().toString());
145 public boolean updateSubnets(Iterable<Subnet> subnets) {
146 checkNotNull(subnets, SUBNET_NOT_NULL);
147 if (subnets != null) {
148 for (Subnet subnet : subnets) {
149 if (!subnetStore.containsKey(subnet.id())) {
150 log.debug("The subnet is not exist whose identifier is {}",
151 subnet.id().toString());
155 subnetStore.put(subnet.id(), subnet);
157 if (!subnet.equals(subnetStore.get(subnet.id()))) {
158 log.debug("The subnet is updated failed whose identifier is {}",
159 subnet.id().toString());
168 public boolean removeSubnets(Iterable<SubnetId> subnetIds) {
169 checkNotNull(subnetIds, SUBNET_ID_NULL);
170 if (subnetIds != null) {
171 for (SubnetId subnetId : subnetIds) {
172 subnetStore.remove(subnetId);
173 if (subnetStore.containsKey(subnetId)) {
174 log.debug("The subnet created is failed whose identifier is {}",
175 subnetId.toString());