blob: 5d13cb1fe9b7a9c9c2b6eda60fcd65e8f5f5cb07 [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
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -070018import org.onlab.packet.IpAddress;
19import org.onosproject.core.ApplicationId;
20import org.onosproject.core.CoreService;
21import org.onosproject.store.serializers.KryoNamespaces;
22import org.onosproject.store.service.Serializer;
23import org.onosproject.store.service.StorageService;
24import org.onosproject.vtnrsc.AllocationPool;
25import org.onosproject.vtnrsc.DefaultAllocationPool;
26import org.onosproject.vtnrsc.DefaultHostRoute;
27import org.onosproject.vtnrsc.DefaultSubnet;
28import org.onosproject.vtnrsc.HostRoute;
29import org.onosproject.vtnrsc.Subnet;
30import org.onosproject.vtnrsc.SubnetId;
31import org.onosproject.vtnrsc.TenantId;
32import org.onosproject.vtnrsc.TenantNetworkId;
33import org.onosproject.vtnrsc.subnet.SubnetService;
34import org.onosproject.vtnrsc.tenantnetwork.TenantNetworkService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035import org.osgi.service.component.annotations.Activate;
36import org.osgi.service.component.annotations.Component;
37import org.osgi.service.component.annotations.Deactivate;
38import org.osgi.service.component.annotations.Reference;
39import org.osgi.service.component.annotations.ReferenceCardinality;
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -070040import org.slf4j.Logger;
41
42import 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
49/**
50 * Provides implementation of the Subnet service.
51 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052@Component(immediate = true, service = SubnetService.class)
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -070053public 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";
57 private static final String SUBNET = "vtn-subnet-store";
58 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
59
60
61 private final Logger log = getLogger(getClass());
62
63 protected Map<SubnetId, Subnet> subnetStore;
64 protected ApplicationId appId;
65
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -070067 protected StorageService storageService;
68
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -070070 protected CoreService coreService;
71
Ray Milkeyd84f89b2018-08-17 14:54:17 -070072 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -070073 protected TenantNetworkService tenantNetworkService;
74
75 @Activate
76 public void activate() {
77
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 DefaultHostRoute.class,
91 Subnet.Mode.class,
92 AllocationPool.class,
93 DefaultAllocationPool.class,
94 DefaultSubnet.class,
95 IpAddress.Version.class))
96 .build().asJavaMap();
97
98 log.info("Started");
99 }
100
101 @Deactivate
102 public void deactivate() {
103 log.info("Stopped");
104 }
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);
Satish Kca0bc8b2015-11-28 16:06:54 +0530145 for (Subnet subnet : subnets) {
146 if (!subnetStore.containsKey(subnet.id())) {
147 log.debug("The subnet is not exist whose identifier is {}",
148 subnet.id().toString());
149 return false;
150 }
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -0700151
Satish Kca0bc8b2015-11-28 16:06:54 +0530152 subnetStore.put(subnet.id(), subnet);
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -0700153
Satish Kca0bc8b2015-11-28 16:06:54 +0530154 if (!subnet.equals(subnetStore.get(subnet.id()))) {
155 log.debug("The subnet is updated failed whose identifier is {}",
156 subnet.id().toString());
157 return false;
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -0700158 }
159 }
160 return true;
161 }
162
163 @Override
164 public boolean removeSubnets(Iterable<SubnetId> subnetIds) {
165 checkNotNull(subnetIds, SUBNET_ID_NULL);
Satish Kca0bc8b2015-11-28 16:06:54 +0530166 for (SubnetId subnetId : subnetIds) {
167 subnetStore.remove(subnetId);
168 if (subnetStore.containsKey(subnetId)) {
169 log.debug("The subnet created is failed whose identifier is {}",
170 subnetId.toString());
171 return false;
Sho SHIMIZUb2b2d982015-09-11 15:35:06 -0700172 }
173 }
174 return true;
175 }
176
177}