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