blob: f5c9d04aa3ab9588b3846c6681253766011d05c4 [file] [log] [blame]
Charles Chan0214ded2016-11-18 17:48:37 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Charles Chan0214ded2016-11-18 17:48:37 -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 */
16
Ray Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice.store;
Charles Chan0214ded2016-11-18 17:48:37 -080018
Charles Chan0214ded2016-11-18 17:48:37 -080019import org.onlab.packet.IpAddress;
Jonathan Hart96c146b2017-02-24 16:32:00 -080020import org.onlab.packet.IpPrefix;
Charles Chan0214ded2016-11-18 17:48:37 -080021import org.onlab.util.Tools;
22import org.onosproject.cfg.ComponentConfigService;
Ray Milkey69ec8712017-08-08 13:00:43 -070023import org.onosproject.routeservice.InternalRouteEvent;
24import org.onosproject.routeservice.Route;
25import org.onosproject.routeservice.RouteSet;
26import org.onosproject.routeservice.RouteStore;
27import org.onosproject.routeservice.RouteStoreDelegate;
28import org.onosproject.routeservice.RouteTableId;
Charles Chan0214ded2016-11-18 17:48:37 -080029import org.onosproject.store.AbstractStore;
30import org.onosproject.store.service.StorageService;
31import org.osgi.service.component.ComponentContext;
32import org.osgi.service.component.annotations.Activate;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033import org.osgi.service.component.annotations.Component;
34import org.osgi.service.component.annotations.Deactivate;
35import org.osgi.service.component.annotations.Modified;
36import org.osgi.service.component.annotations.Reference;
37import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chan0214ded2016-11-18 17:48:37 -080038import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
41import java.util.Collection;
42import java.util.Dictionary;
Charles Chan0214ded2016-11-18 17:48:37 -080043import java.util.Set;
44
Ray Milkey4694e062018-10-31 13:17:18 -070045import static org.onosproject.routeservice.store.OsgiPropertyConstants.DISTRIBUTED;
46import static org.onosproject.routeservice.store.OsgiPropertyConstants.DISTRIBUTED_DEFAULT;
47
Charles Chan0214ded2016-11-18 17:48:37 -080048/**
49 * An implementation of RouteStore that is backed by either LocalRouteStore or
50 * DistributedRouteStore according to configuration.
51 */
Ray Milkey4694e062018-10-31 13:17:18 -070052@Component(
53 service = RouteStore.class,
54 property = {
55 DISTRIBUTED + ":Boolean=" + DISTRIBUTED_DEFAULT
56 }
57)
Jonathan Hart96c146b2017-02-24 16:32:00 -080058public class RouteStoreImpl extends AbstractStore<InternalRouteEvent, RouteStoreDelegate>
Charles Chan0214ded2016-11-18 17:48:37 -080059 implements RouteStore {
60
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan0214ded2016-11-18 17:48:37 -080062 protected ComponentConfigService componentConfigService;
63
Ray Milkeyd84f89b2018-08-17 14:54:17 -070064 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan0214ded2016-11-18 17:48:37 -080065 public StorageService storageService;
66
Ray Milkey4694e062018-10-31 13:17:18 -070067 /** Enable distributed route store. */
Charles Chan0214ded2016-11-18 17:48:37 -080068 private boolean distributed;
69
70 private final Logger log = LoggerFactory.getLogger(getClass());
71 private RouteStore currentRouteStore;
72
Jonathan Hart74850702017-05-04 13:01:24 -070073 private DistributedRouteStore distributedRouteStore;
74 private LocalRouteStore localRouteStore;
75
Charles Chan0214ded2016-11-18 17:48:37 -080076 @Activate
77 public void activate(ComponentContext context) {
Jonathan Hart74850702017-05-04 13:01:24 -070078 distributedRouteStore = new DistributedRouteStore(storageService);
79 distributedRouteStore.activate();
80 localRouteStore = new LocalRouteStore();
81 localRouteStore.activate();
82
Charles Chan0214ded2016-11-18 17:48:37 -080083 componentConfigService.registerProperties(getClass());
84 modified(context);
85 }
86
87 @Deactivate
88 public void deactivate() {
Jonathan Hart74850702017-05-04 13:01:24 -070089 localRouteStore.deactivate();
90 distributedRouteStore.deactivate();
91
Charles Chan0214ded2016-11-18 17:48:37 -080092 componentConfigService.unregisterProperties(getClass(), false);
93 }
94
95 @Modified
96 public void modified(ComponentContext context) {
97 Dictionary<?, ?> properties = context.getProperties();
98 if (properties == null) {
99 return;
100 }
101
Ray Milkey4694e062018-10-31 13:17:18 -0700102 String strDistributed = Tools.get(properties, DISTRIBUTED);
Charles Chan0214ded2016-11-18 17:48:37 -0800103 boolean expectDistributed = Boolean.parseBoolean(strDistributed);
104
105 // Start route store during first start or config change
106 // NOTE: new route store will be empty
107 if (currentRouteStore == null || expectDistributed != distributed) {
108 if (expectDistributed) {
Jonathan Hart74850702017-05-04 13:01:24 -0700109 currentRouteStore = distributedRouteStore;
Charles Chan0214ded2016-11-18 17:48:37 -0800110 } else {
Jonathan Hart74850702017-05-04 13:01:24 -0700111 currentRouteStore = localRouteStore;
Charles Chan0214ded2016-11-18 17:48:37 -0800112 }
113
114 this.distributed = expectDistributed;
115 log.info("Switched to {} route store", distributed ? "distributed" : "local");
116 }
117
118 }
119
120 @Override
Charles Chan143bc182017-01-13 15:46:03 -0800121 public void setDelegate(RouteStoreDelegate delegate) {
Jonathan Hart74850702017-05-04 13:01:24 -0700122 super.setDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800123
Jonathan Hart74850702017-05-04 13:01:24 -0700124 // Set the delegate of underlying route store implementations
125 localRouteStore.setDelegate(delegate);
126 distributedRouteStore.setDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800127 }
128
129 @Override
130 public void unsetDelegate(RouteStoreDelegate delegate) {
Jonathan Hart74850702017-05-04 13:01:24 -0700131 super.unsetDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800132
Jonathan Hart74850702017-05-04 13:01:24 -0700133 // Unset the delegate of underlying route store implementations
134 localRouteStore.unsetDelegate(delegate);
135 distributedRouteStore.unsetDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800136 }
137
138 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800139 public void updateRoute(Route route) {
140 currentRouteStore.updateRoute(route);
141 }
142
143 @Override
piere91c87f2019-10-16 16:58:20 +0200144 public void updateRoutes(Collection<Route> routes) {
145 currentRouteStore.updateRoutes(routes);
146 }
147
148 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800149 public void removeRoute(Route route) {
150 currentRouteStore.removeRoute(route);
151 }
152
153 @Override
piere91c87f2019-10-16 16:58:20 +0200154 public void removeRoutes(Collection<Route> routes) {
155 currentRouteStore.removeRoutes(routes);
156 }
157
158 @Override
Daniel Ginsburg83b76452018-06-09 01:43:59 +0300159 public void replaceRoute(Route route) {
160 currentRouteStore.replaceRoute(route);
161 }
162
163 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800164 public Set<RouteTableId> getRouteTables() {
165 return currentRouteStore.getRouteTables();
166 }
167
168 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800169 public Collection<RouteSet> getRoutes(RouteTableId table) {
Charles Chan0214ded2016-11-18 17:48:37 -0800170 return currentRouteStore.getRoutes(table);
171 }
172
173 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800174 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
175 return currentRouteStore.getRoutesForNextHop(ip);
176 }
177
178 @Override
piere91c87f2019-10-16 16:58:20 +0200179 public Collection<RouteSet> getRoutesForNextHops(Collection<IpAddress> ips) {
180 return currentRouteStore.getRoutesForNextHops(ips);
181 }
182
183 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800184 public RouteSet getRoutes(IpPrefix prefix) {
185 return currentRouteStore.getRoutes(prefix);
186 }
Charles Chan4f365732017-07-20 17:11:57 -0700187
188 @Override
189 public String name() {
190 return currentRouteStore.name();
191 }
Charles Chan0214ded2016-11-18 17:48:37 -0800192}