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