blob: 8f43d75adc3f388ab00b12cf125b1f33f606ade6 [file] [log] [blame]
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -07003 *
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
18import 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;
24import org.onlab.packet.IpAddress;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.store.serializers.KryoNamespaces;
28import org.onosproject.store.service.Serializer;
29import org.onosproject.store.service.StorageService;
30import org.onosproject.vtnrsc.AllocationPool;
31import org.onosproject.vtnrsc.DefaultAllocationPool;
32import org.onosproject.vtnrsc.DefaultHostRoute;
33import org.onosproject.vtnrsc.DefaultSubnet;
34import org.onosproject.vtnrsc.HostRoute;
35import org.onosproject.vtnrsc.Subnet;
36import org.onosproject.vtnrsc.SubnetId;
37import org.onosproject.vtnrsc.TenantId;
38import org.onosproject.vtnrsc.TenantNetworkId;
39import org.onosproject.vtnrsc.subnet.SubnetService;
40import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
41import org.slf4j.Logger;
42
43import java.util.Arrays;
44import java.util.Collections;
45import java.util.Map;
46
47import static com.google.common.base.Preconditions.checkNotNull;
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Provides implementation of the Subnet service.
52 */
53@Component(immediate = true)
54@Service
55public class SubnetManager implements SubnetService {
56
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";
61
62
63 private final Logger log = getLogger(getClass());
64
65 protected Map<SubnetId, Subnet> subnetStore;
66 protected ApplicationId appId;
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected StorageService storageService;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected CoreService coreService;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected TenantNetworkService tenantNetworkService;
76
77 @Activate
78 public void activate() {
79
80 appId = coreService.registerApplication(VTNRSC_APP);
81
82 subnetStore = storageService.<SubnetId, Subnet>consistentMapBuilder()
83 .withName(SUBNET)
84 .withApplicationId(appId)
85 .withPurgeOnUninstall()
86 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
87 Subnet.class,
88 SubnetId.class,
89 TenantNetworkId.class,
90 TenantId.class,
91 HostRoute.class,
92 DefaultHostRoute.class,
93 Subnet.Mode.class,
94 AllocationPool.class,
95 DefaultAllocationPool.class,
96 DefaultSubnet.class,
97 IpAddress.Version.class))
98 .build().asJavaMap();
99
100 log.info("Started");
101 }
102
103 @Deactivate
104 public void deactivate() {
105 log.info("Stopped");
106 }
107
108 @Override
109 public Iterable<Subnet> getSubnets() {
110 return Collections.unmodifiableCollection(subnetStore.values());
111 }
112
113 @Override
114 public Subnet getSubnet(SubnetId subnetId) {
115 checkNotNull(subnetId, SUBNET_ID_NULL);
116 return subnetStore.get(subnetId);
117 }
118
119 @Override
120 public boolean exists(SubnetId subnetId) {
121 checkNotNull(subnetId, SUBNET_ID_NULL);
122 return subnetStore.containsKey(subnetId);
123 }
124
125 @Override
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());
132 return false;
133 }
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());
138 return false;
139 }
140 }
141 return true;
142 }
143
144 @Override
145 public boolean updateSubnets(Iterable<Subnet> subnets) {
146 checkNotNull(subnets, SUBNET_NOT_NULL);
Satish Kca0bc8b2015-11-28 16:06:54 +0530147 for (Subnet subnet : subnets) {
148 if (!subnetStore.containsKey(subnet.id())) {
149 log.debug("The subnet is not exist whose identifier is {}",
150 subnet.id().toString());
151 return false;
152 }
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -0700153
Satish Kca0bc8b2015-11-28 16:06:54 +0530154 subnetStore.put(subnet.id(), subnet);
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -0700155
Satish Kca0bc8b2015-11-28 16:06:54 +0530156 if (!subnet.equals(subnetStore.get(subnet.id()))) {
157 log.debug("The subnet is updated failed whose identifier is {}",
158 subnet.id().toString());
159 return false;
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -0700160 }
161 }
162 return true;
163 }
164
165 @Override
166 public boolean removeSubnets(Iterable<SubnetId> subnetIds) {
167 checkNotNull(subnetIds, SUBNET_ID_NULL);
Satish Kca0bc8b2015-11-28 16:06:54 +0530168 for (SubnetId subnetId : subnetIds) {
169 subnetStore.remove(subnetId);
170 if (subnetStore.containsKey(subnetId)) {
171 log.debug("The subnet created is failed whose identifier is {}",
172 subnetId.toString());
173 return false;
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -0700174 }
175 }
176 return true;
177 }
178
179}