blob: 923825711eb9b12defa6e43743a5b8b9abe2ad42 [file] [log] [blame]
CNlucius5b2fff12015-08-20 14:13:46 +08001/*
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 */
16package org.onosproject.vtnrsc.subnet.impl;
17
CNlucius5b2fff12015-08-20 14:13:46 +080018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
jiangruiac0879a2015-09-08 16:58:49 +080024import org.onlab.packet.IpAddress;
alshabibd9b70002015-09-01 17:00:37 -070025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.store.serializers.KryoNamespaces;
28import org.onosproject.store.service.Serializer;
CNlucius5b2fff12015-08-20 14:13:46 +080029import org.onosproject.store.service.StorageService;
alshabibd9b70002015-09-01 17:00:37 -070030import org.onosproject.vtnrsc.AllocationPool;
jiangruifef9b2e2015-09-10 09:44:21 +080031import org.onosproject.vtnrsc.DefaultAllocationPool;
jiangruiac0879a2015-09-08 16:58:49 +080032import org.onosproject.vtnrsc.DefaultSubnet;
alshabibd9b70002015-09-01 17:00:37 -070033import org.onosproject.vtnrsc.HostRoute;
CNlucius5b2fff12015-08-20 14:13:46 +080034import org.onosproject.vtnrsc.Subnet;
35import org.onosproject.vtnrsc.SubnetId;
alshabibd9b70002015-09-01 17:00:37 -070036import org.onosproject.vtnrsc.TenantId;
37import org.onosproject.vtnrsc.TenantNetworkId;
CNlucius5b2fff12015-08-20 14:13:46 +080038import org.onosproject.vtnrsc.subnet.SubnetService;
39import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
40import org.slf4j.Logger;
41
alshabibd9b70002015-09-01 17:00:37 -070042import java.util.Arrays;
43import java.util.Collections;
44import java.util.Map;
45
46import static com.google.common.base.Preconditions.checkNotNull;
47import static org.slf4j.LoggerFactory.getLogger;
48
CNlucius5b2fff12015-08-20 14:13:46 +080049/**
50 * Provides implementation of the Subnet service.
51 */
52@Component(immediate = true)
53@Service
54public class SubnetManager implements SubnetService {
55
56 private static final String SUBNET_ID_NULL = "Subnet ID cannot be null";
57 private static final String SUBNET_NOT_NULL = "Subnet cannot be null";
alshabibd9b70002015-09-01 17:00:37 -070058 private static final String SUBNET = "vtn-subnet-store";
59 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
60
CNlucius5b2fff12015-08-20 14:13:46 +080061
62 private final Logger log = getLogger(getClass());
63
alshabibd9b70002015-09-01 17:00:37 -070064 protected Map<SubnetId, Subnet> subnetStore;
65 protected ApplicationId appId;
66
CNlucius5b2fff12015-08-20 14:13:46 +080067 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected StorageService storageService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibd9b70002015-09-01 17:00:37 -070071 protected CoreService coreService;
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
CNlucius5b2fff12015-08-20 14:13:46 +080074 protected TenantNetworkService tenantNetworkService;
75
76 @Activate
77 public void activate() {
alshabibd9b70002015-09-01 17:00:37 -070078
79 appId = coreService.registerApplication(VTNRSC_APP);
80
81 subnetStore = storageService.<SubnetId, Subnet>consistentMapBuilder()
82 .withName(SUBNET)
83 .withApplicationId(appId)
84 .withPurgeOnUninstall()
85 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
86 Subnet.class,
87 SubnetId.class,
88 TenantNetworkId.class,
89 TenantId.class,
90 HostRoute.class,
91 Subnet.Mode.class,
jiangruiac0879a2015-09-08 16:58:49 +080092 AllocationPool.class,
jiangruifef9b2e2015-09-10 09:44:21 +080093 DefaultAllocationPool.class,
jiangruiac0879a2015-09-08 16:58:49 +080094 DefaultSubnet.class,
95 IpAddress.Version.class))
alshabibd9b70002015-09-01 17:00:37 -070096 .build().asJavaMap();
97
98 log.info("Started");
CNlucius5b2fff12015-08-20 14:13:46 +080099 }
100
101 @Deactivate
102 public void deactivate() {
alshabibd9b70002015-09-01 17:00:37 -0700103 log.info("Stopped");
CNlucius5b2fff12015-08-20 14:13:46 +0800104 }
105
106 @Override
107 public Iterable<Subnet> getSubnets() {
108 return Collections.unmodifiableCollection(subnetStore.values());
109 }
110
111 @Override
112 public Subnet getSubnet(SubnetId subnetId) {
113 checkNotNull(subnetId, SUBNET_ID_NULL);
114 return subnetStore.get(subnetId);
115 }
116
117 @Override
118 public boolean exists(SubnetId subnetId) {
119 checkNotNull(subnetId, SUBNET_ID_NULL);
120 return subnetStore.containsKey(subnetId);
121 }
122
123 @Override
124 public boolean createSubnets(Iterable<Subnet> subnets) {
125 checkNotNull(subnets, SUBNET_NOT_NULL);
126 for (Subnet subnet : subnets) {
127 if (!tenantNetworkService.exists(subnet.networkId())) {
128 log.debug("The network identifier that the subnet {} belong to is not exist",
129 subnet.networkId().toString(), subnet.id().toString());
130 return false;
131 }
132 subnetStore.put(subnet.id(), subnet);
133 if (!subnetStore.containsKey(subnet.id())) {
134 log.debug("The identified subnet whose identifier is {} create failed",
135 subnet.id().toString());
136 return false;
137 }
138 }
139 return true;
140 }
141
142 @Override
143 public boolean updateSubnets(Iterable<Subnet> subnets) {
144 checkNotNull(subnets, SUBNET_NOT_NULL);
145 if (subnets != null) {
146 for (Subnet subnet : subnets) {
147 if (!subnetStore.containsKey(subnet.id())) {
148 log.debug("The subnet is not exist whose identifier is {}",
149 subnet.id().toString());
150 return false;
151 }
152
153 subnetStore.put(subnet.id(), subnet);
154
155 if (!subnet.equals(subnetStore.get(subnet.id()))) {
156 log.debug("The subnet is updated failed whose identifier is {}",
157 subnet.id().toString());
158 return false;
159 }
160 }
161 }
162 return true;
163 }
164
165 @Override
166 public boolean removeSubnets(Iterable<SubnetId> subnetIds) {
167 checkNotNull(subnetIds, SUBNET_ID_NULL);
168 if (subnetIds != null) {
169 for (SubnetId subnetId : subnetIds) {
170 subnetStore.remove(subnetId);
171 if (subnetStore.containsKey(subnetId)) {
172 log.debug("The subnet created is failed whose identifier is {}",
173 subnetId.toString());
174 return false;
175 }
176 }
177 }
178 return true;
179 }
180
181}