blob: ab810a806d176bca7993002c876e0feb918cdf74 [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;
jiangruiac0879a2015-09-08 16:58:49 +080031import org.onosproject.vtnrsc.DefaultSubnet;
alshabibd9b70002015-09-01 17:00:37 -070032import org.onosproject.vtnrsc.HostRoute;
CNlucius5b2fff12015-08-20 14:13:46 +080033import org.onosproject.vtnrsc.Subnet;
34import org.onosproject.vtnrsc.SubnetId;
alshabibd9b70002015-09-01 17:00:37 -070035import org.onosproject.vtnrsc.TenantId;
36import org.onosproject.vtnrsc.TenantNetworkId;
CNlucius5b2fff12015-08-20 14:13:46 +080037import org.onosproject.vtnrsc.subnet.SubnetService;
38import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
39import org.slf4j.Logger;
40
alshabibd9b70002015-09-01 17:00:37 -070041import java.util.Arrays;
42import java.util.Collections;
43import java.util.Map;
44
45import static com.google.common.base.Preconditions.checkNotNull;
46import static org.slf4j.LoggerFactory.getLogger;
47
CNlucius5b2fff12015-08-20 14:13:46 +080048/**
49 * Provides implementation of the Subnet service.
50 */
51@Component(immediate = true)
52@Service
53public 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";
alshabibd9b70002015-09-01 17:00:37 -070057 private static final String SUBNET = "vtn-subnet-store";
58 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
59
CNlucius5b2fff12015-08-20 14:13:46 +080060
61 private final Logger log = getLogger(getClass());
62
alshabibd9b70002015-09-01 17:00:37 -070063 protected Map<SubnetId, Subnet> subnetStore;
64 protected ApplicationId appId;
65
CNlucius5b2fff12015-08-20 14:13:46 +080066 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected StorageService storageService;
68
69 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabibd9b70002015-09-01 17:00:37 -070070 protected CoreService coreService;
71
72 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
CNlucius5b2fff12015-08-20 14:13:46 +080073 protected TenantNetworkService tenantNetworkService;
74
75 @Activate
76 public void activate() {
alshabibd9b70002015-09-01 17:00:37 -070077
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,
jiangruiac0879a2015-09-08 16:58:49 +080091 AllocationPool.class,
92 DefaultSubnet.class,
93 IpAddress.Version.class))
alshabibd9b70002015-09-01 17:00:37 -070094 .build().asJavaMap();
95
96 log.info("Started");
CNlucius5b2fff12015-08-20 14:13:46 +080097 }
98
99 @Deactivate
100 public void deactivate() {
alshabibd9b70002015-09-01 17:00:37 -0700101 log.info("Stopped");
CNlucius5b2fff12015-08-20 14:13:46 +0800102 }
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}