blob: b9c03265c5f9db88b760bf6b1e716b74c9f7d23d [file] [log] [blame]
jiangruib150da52015-11-27 14:55:03 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
jiangruib150da52015-11-27 14:55:03 +08003 *
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.routerinterface.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.Collection;
22import java.util.Collections;
23import java.util.Set;
24
25import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
28import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
30import org.apache.felix.scr.annotations.Service;
31import org.onlab.util.KryoNamespace;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
34import org.onosproject.store.serializers.KryoNamespaces;
35import org.onosproject.store.service.EventuallyConsistentMap;
36import org.onosproject.store.service.EventuallyConsistentMapEvent;
37import org.onosproject.store.service.EventuallyConsistentMapListener;
38import org.onosproject.store.service.StorageService;
39import org.onosproject.store.service.WallClockTimestamp;
40import org.onosproject.vtnrsc.RouterId;
41import org.onosproject.vtnrsc.RouterInterface;
42import org.onosproject.vtnrsc.SubnetId;
43import org.onosproject.vtnrsc.TenantId;
44import org.onosproject.vtnrsc.VirtualPortId;
45import org.onosproject.vtnrsc.router.RouterService;
46import org.onosproject.vtnrsc.routerinterface.RouterInterfaceEvent;
47import org.onosproject.vtnrsc.routerinterface.RouterInterfaceListener;
48import org.onosproject.vtnrsc.routerinterface.RouterInterfaceService;
49import org.onosproject.vtnrsc.subnet.SubnetService;
50import org.onosproject.vtnrsc.virtualport.VirtualPortService;
51import org.slf4j.Logger;
52
53import com.google.common.collect.Sets;
54
55/**
56 * Provides implementation of the Router interface service.
57 */
58@Component(immediate = true)
59@Service
60public class RouterInterfaceManager implements RouterInterfaceService {
61 private static final String SUBNET_ID_NULL = "Subnet ID cannot be null";
62 private static final String ROUTER_INTERFACE_NULL = "Router Interface cannot be null";
63 private static final String ROUTER_INTERFACE = "vtn-router-interface-store";
64 private static final String VTNRSC_APP = "org.onosproject.vtnrsc";
65 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
66 private static final String EVENT_NOT_NULL = "event cannot be null";
67
68 private final Logger log = getLogger(getClass());
69 private final Set<RouterInterfaceListener> listeners = Sets
70 .newCopyOnWriteArraySet();
71 private EventuallyConsistentMapListener<SubnetId, RouterInterface> routerInterfaceListener =
72 new InnerRouterInterfaceStoreListener();
73 protected EventuallyConsistentMap<SubnetId, RouterInterface> routerInterfaceStore;
74 protected ApplicationId appId;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected StorageService storageService;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected CoreService coreService;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected VirtualPortService virtualPortService;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected SubnetService subnetService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected RouterService routerService;
90
91 @Activate
92 public void activate() {
93 appId = coreService.registerApplication(VTNRSC_APP);
94 KryoNamespace.Builder serializer = KryoNamespace
95 .newBuilder()
96 .register(KryoNamespaces.API)
97 .register(RouterId.class, TenantId.class, VirtualPortId.class,
98 RouterInterface.class, SubnetId.class);
99 routerInterfaceStore = storageService
100 .<SubnetId, RouterInterface>eventuallyConsistentMapBuilder()
101 .withName(ROUTER_INTERFACE).withSerializer(serializer)
102 .withTimestampProvider((k, v) -> new WallClockTimestamp())
103 .build();
104 routerInterfaceStore.addListener(routerInterfaceListener);
105 log.info("Started");
106 }
107
108 @Deactivate
109 public void deactivate() {
110 routerInterfaceStore.removeListener(routerInterfaceListener);
111 routerInterfaceStore.destroy();
112 listeners.clear();
113 log.info("Stopped");
114 }
115
116 @Override
117 public boolean exists(SubnetId subnetId) {
118 checkNotNull(subnetId, SUBNET_ID_NULL);
119 return routerInterfaceStore.containsKey(subnetId);
120 }
121
122 @Override
123 public Collection<RouterInterface> getRouterInterfaces() {
124 return Collections
125 .unmodifiableCollection(routerInterfaceStore.values());
126 }
127
128 @Override
129 public RouterInterface getRouterInterface(SubnetId subnetId) {
130 checkNotNull(subnetId, SUBNET_ID_NULL);
131 return routerInterfaceStore.get(subnetId);
132 }
133
134 @Override
135 public boolean addRouterInterface(RouterInterface routerInterface) {
136 checkNotNull(routerInterface, ROUTER_INTERFACE_NULL);
lishuaid6f0c9e2015-12-16 11:40:01 +0800137 if (!virtualPortService.exists(routerInterface.portId())) {
138 log.debug("The port ID of interface is not exist whose identifier is {}",
139 routerInterface.portId().toString());
140 throw new IllegalArgumentException(
141 "port ID of interface doesn't exist");
142 }
jiangruib150da52015-11-27 14:55:03 +0800143 verifyRouterInterfaceData(routerInterface);
144 routerInterfaceStore.put(routerInterface.subnetId(), routerInterface);
145 if (!routerInterfaceStore.containsKey(routerInterface.subnetId())) {
146 log.debug("The router interface is created failed whose identifier is {}",
147 routerInterface.subnetId().toString());
148 return false;
149 }
150 return true;
151 }
152
153 @Override
154 public boolean removeRouterInterface(RouterInterface routerInterface) {
155 checkNotNull(routerInterface, ROUTER_INTERFACE_NULL);
156 if (!routerInterfaceStore.containsKey(routerInterface.subnetId())) {
157 log.debug("The router interface is not exist whose identifier is {}",
158 routerInterface.subnetId().toString());
159 throw new IllegalArgumentException("subnet ID doesn't exist");
160 }
161 verifyRouterInterfaceData(routerInterface);
162 routerInterfaceStore
163 .remove(routerInterface.subnetId(), routerInterface);
164 if (routerInterfaceStore.containsKey(routerInterface.subnetId())) {
165 log.debug("The router interface deleted is failed whose identifier is {}",
166 routerInterface.subnetId().toString());
167 return false;
168 }
169 return true;
170 }
171
172 @Override
173 public void addListener(RouterInterfaceListener listener) {
174 checkNotNull(listener, LISTENER_NOT_NULL);
175 listeners.add(listener);
176 }
177
178 @Override
179 public void removeListener(RouterInterfaceListener listener) {
180 checkNotNull(listener, LISTENER_NOT_NULL);
181 listeners.remove(listener);
182 }
183
184 /**
185 * Verifies validity of Router interface data.
186 *
187 * @param routers router instance
188 */
189 private void verifyRouterInterfaceData(RouterInterface routerInterface) {
190 checkNotNull(routerInterface, ROUTER_INTERFACE_NULL);
191 if (!subnetService.exists(routerInterface.subnetId())) {
192 log.debug("The subnet ID of interface is not exist whose identifier is {}",
193 routerInterface.subnetId().toString());
194 throw new IllegalArgumentException(
195 "subnet ID of interface doesn't exist");
196 }
jiangruib150da52015-11-27 14:55:03 +0800197 if (!routerService.exists(routerInterface.routerId())) {
198 log.debug("The router ID of interface is not exist whose identifier is {}",
199 routerInterface.routerId().toString());
200 throw new IllegalArgumentException(
201 "router ID of interface doesn't exist");
202 }
203 }
204
205 private class InnerRouterInterfaceStoreListener
206 implements
207 EventuallyConsistentMapListener<SubnetId, RouterInterface> {
208
209 @Override
210 public void event(EventuallyConsistentMapEvent<SubnetId, RouterInterface> event) {
211 checkNotNull(event, EVENT_NOT_NULL);
212 RouterInterface routerInterface = event.value();
213 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
214 notifyListeners(new RouterInterfaceEvent(
215 RouterInterfaceEvent.Type.ROUTER_INTERFACE_PUT,
216 routerInterface));
217 }
218 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
219 notifyListeners(new RouterInterfaceEvent(
220 RouterInterfaceEvent.Type.ROUTER_INTERFACE_DELETE,
221 routerInterface));
222 }
223 }
224 }
225
226 /**
227 * Notifies specify event to all listeners.
228 *
229 * @param event Floating IP event
230 */
231 private void notifyListeners(RouterInterfaceEvent event) {
232 checkNotNull(event, EVENT_NOT_NULL);
233 listeners.forEach(listener -> listener.event(event));
234 }
235}