blob: 5214691b9aa59390e9bff044eab3dfe797be7cff [file] [log] [blame]
xuzhang91b2ff42015-08-11 11:05:19 +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.app.vtnrsc.subnet.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.Collections;
22
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.apache.felix.scr.annotations.Service;
29import org.onlab.util.KryoNamespace;
30import org.onosproject.app.vtnrsc.Subnet;
31import org.onosproject.app.vtnrsc.SubnetId;
32import org.onosproject.app.vtnrsc.subnet.SubnetService;
33import org.onosproject.app.vtnrsc.tenantnetwork.TenantNetworkService;
34import org.onosproject.store.service.EventuallyConsistentMap;
35import org.onosproject.store.service.MultiValuedTimestamp;
36import org.onosproject.store.service.StorageService;
37import org.onosproject.store.service.WallClockTimestamp;
38import org.slf4j.Logger;
39
40/**
41 * Provides implementation of the Subnet service.
42 */
43@Component(immediate = true)
44@Service
45public class SubnetManager implements SubnetService {
46
47 private static final String SUBNET_ID_NULL = "Subnet ID cannot be null";
48 private static final String SUBNET_NOT_NULL = "Subnet cannot be null";
49
50 private final Logger log = getLogger(getClass());
51
52 private EventuallyConsistentMap<SubnetId, Subnet> subnetStore;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected StorageService storageService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected TenantNetworkService tenantNetworkService;
59
60 @Activate
61 public void activate() {
62 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
63 .register(MultiValuedTimestamp.class);
64 subnetStore = storageService
65 .<SubnetId, Subnet>eventuallyConsistentMapBuilder()
66 .withName("all_subnet").withSerializer(serializer)
67 .withTimestampProvider((k, v) -> new WallClockTimestamp())
68 .build();
69
70 log.info("SubnetManager started");
71 }
72
73 @Deactivate
74 public void deactivate() {
75 subnetStore.destroy();
76 log.info("SubnetManager stopped");
77 }
78
79 @Override
80 public Iterable<Subnet> getSubnets() {
81 return Collections.unmodifiableCollection(subnetStore.values());
82 }
83
84 @Override
85 public Subnet getSubnet(SubnetId subnetId) {
86 checkNotNull(subnetId, SUBNET_ID_NULL);
87 return subnetStore.get(subnetId);
88 }
89
90 @Override
91 public boolean exists(SubnetId subnetId) {
92 checkNotNull(subnetId, SUBNET_ID_NULL);
93 return subnetStore.containsKey(subnetId);
94 }
95
96 @Override
97 public boolean createSubnets(Iterable<Subnet> subnets) {
98 checkNotNull(subnets, SUBNET_NOT_NULL);
99 for (Subnet subnet : subnets) {
100 if (!tenantNetworkService.exists(subnet.networkId())) {
101 log.debug("The network identifier that the subnet {} belong to is not exist",
102 subnet.networkId().toString(), subnet.id().toString());
103 return false;
104 }
105 subnetStore.put(subnet.id(), subnet);
106 if (!subnetStore.containsKey(subnet.id())) {
107 log.debug("The identified subnet whose identifier is {} create failed",
108 subnet.id().toString());
109 return false;
110 }
111 }
112 return true;
113 }
114
115 @Override
116 public boolean updateSubnets(Iterable<Subnet> subnets) {
117 checkNotNull(subnets, SUBNET_NOT_NULL);
118 if (subnets != null) {
119 for (Subnet subnet : subnets) {
120 if (!subnetStore.containsKey(subnet.id())) {
121 log.debug("The subnet is not exist whose identifier is {}",
122 subnet.id().toString());
123 return false;
124 }
125
126 subnetStore.put(subnet.id(), subnet);
127
128 if (!subnet.equals(subnetStore.get(subnet.id()))) {
129 log.debug("The subnet is updated failed whose identifier is {}",
130 subnet.id().toString());
131 return false;
132 }
133 }
134 }
135 return true;
136 }
137
138 @Override
139 public boolean removeSubnets(Iterable<SubnetId> subnetIds) {
140 checkNotNull(subnetIds, SUBNET_ID_NULL);
141 if (subnetIds != null) {
142 for (SubnetId subnetId : subnetIds) {
143 subnetStore.remove(subnetId);
144 if (subnetStore.containsKey(subnetId)) {
145 log.debug("The subnet created is failed whose identifier is {}",
146 subnetId.toString());
147 return false;
148 }
149 }
150 }
151 return true;
152 }
153
154}