blob: f1748019388f4fc155c879edbb7c4d05f9bfaea2 [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
45/**
46 * An implementation of RouteStore that is backed by either LocalRouteStore or
47 * DistributedRouteStore according to configuration.
48 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049@Component(service = RouteStore.class)
Jonathan Hart96c146b2017-02-24 16:32:00 -080050public class RouteStoreImpl extends AbstractStore<InternalRouteEvent, RouteStoreDelegate>
Charles Chan0214ded2016-11-18 17:48:37 -080051 implements RouteStore {
52
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan0214ded2016-11-18 17:48:37 -080054 protected ComponentConfigService componentConfigService;
55
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan0214ded2016-11-18 17:48:37 -080057 public StorageService storageService;
58
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059 //@Property(name = "distributed", boolValue = false,
60 // label = "Enable distributed route store")
Charles Chan0214ded2016-11-18 17:48:37 -080061 private boolean distributed;
62
63 private final Logger log = LoggerFactory.getLogger(getClass());
64 private RouteStore currentRouteStore;
65
Jonathan Hart74850702017-05-04 13:01:24 -070066 private DistributedRouteStore distributedRouteStore;
67 private LocalRouteStore localRouteStore;
68
Charles Chan0214ded2016-11-18 17:48:37 -080069 @Activate
70 public void activate(ComponentContext context) {
Jonathan Hart74850702017-05-04 13:01:24 -070071 distributedRouteStore = new DistributedRouteStore(storageService);
72 distributedRouteStore.activate();
73 localRouteStore = new LocalRouteStore();
74 localRouteStore.activate();
75
Charles Chan0214ded2016-11-18 17:48:37 -080076 componentConfigService.registerProperties(getClass());
77 modified(context);
78 }
79
80 @Deactivate
81 public void deactivate() {
Jonathan Hart74850702017-05-04 13:01:24 -070082 localRouteStore.deactivate();
83 distributedRouteStore.deactivate();
84
Charles Chan0214ded2016-11-18 17:48:37 -080085 componentConfigService.unregisterProperties(getClass(), false);
86 }
87
88 @Modified
89 public void modified(ComponentContext context) {
90 Dictionary<?, ?> properties = context.getProperties();
91 if (properties == null) {
92 return;
93 }
94
95 String strDistributed = Tools.get(properties, "distributed");
96 boolean expectDistributed = Boolean.parseBoolean(strDistributed);
97
98 // Start route store during first start or config change
99 // NOTE: new route store will be empty
100 if (currentRouteStore == null || expectDistributed != distributed) {
101 if (expectDistributed) {
Jonathan Hart74850702017-05-04 13:01:24 -0700102 currentRouteStore = distributedRouteStore;
Charles Chan0214ded2016-11-18 17:48:37 -0800103 } else {
Jonathan Hart74850702017-05-04 13:01:24 -0700104 currentRouteStore = localRouteStore;
Charles Chan0214ded2016-11-18 17:48:37 -0800105 }
106
107 this.distributed = expectDistributed;
108 log.info("Switched to {} route store", distributed ? "distributed" : "local");
109 }
110
111 }
112
113 @Override
Charles Chan143bc182017-01-13 15:46:03 -0800114 public void setDelegate(RouteStoreDelegate delegate) {
Jonathan Hart74850702017-05-04 13:01:24 -0700115 super.setDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800116
Jonathan Hart74850702017-05-04 13:01:24 -0700117 // Set the delegate of underlying route store implementations
118 localRouteStore.setDelegate(delegate);
119 distributedRouteStore.setDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800120 }
121
122 @Override
123 public void unsetDelegate(RouteStoreDelegate delegate) {
Jonathan Hart74850702017-05-04 13:01:24 -0700124 super.unsetDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800125
Jonathan Hart74850702017-05-04 13:01:24 -0700126 // Unset the delegate of underlying route store implementations
127 localRouteStore.unsetDelegate(delegate);
128 distributedRouteStore.unsetDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800129 }
130
131 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800132 public void updateRoute(Route route) {
133 currentRouteStore.updateRoute(route);
134 }
135
136 @Override
137 public void removeRoute(Route route) {
138 currentRouteStore.removeRoute(route);
139 }
140
141 @Override
Daniel Ginsburg83b76452018-06-09 01:43:59 +0300142 public void replaceRoute(Route route) {
143 currentRouteStore.replaceRoute(route);
144 }
145
146 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800147 public Set<RouteTableId> getRouteTables() {
148 return currentRouteStore.getRouteTables();
149 }
150
151 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800152 public Collection<RouteSet> getRoutes(RouteTableId table) {
Charles Chan0214ded2016-11-18 17:48:37 -0800153 return currentRouteStore.getRoutes(table);
154 }
155
156 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800157 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
158 return currentRouteStore.getRoutesForNextHop(ip);
159 }
160
161 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800162 public RouteSet getRoutes(IpPrefix prefix) {
163 return currentRouteStore.getRoutes(prefix);
164 }
Charles Chan4f365732017-07-20 17:11:57 -0700165
166 @Override
167 public String name() {
168 return currentRouteStore.name();
169 }
Charles Chan0214ded2016-11-18 17:48:37 -0800170}