blob: c07f3a69eb55f8dcb761dc4f400caaa4c9ca5f19 [file] [log] [blame]
Charles Chan0214ded2016-11-18 17:48:37 -08001/*
2 * Copyright 2017-present 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 */
16
17package org.onosproject.incubator.store.routing.impl;
18
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onlab.packet.IpAddress;
Jonathan Hart96c146b2017-02-24 16:32:00 -080027import org.onlab.packet.IpPrefix;
Charles Chan0214ded2016-11-18 17:48:37 -080028import org.onlab.util.Tools;
29import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart96c146b2017-02-24 16:32:00 -080030import org.onosproject.incubator.net.routing.InternalRouteEvent;
Charles Chan0214ded2016-11-18 17:48:37 -080031import org.onosproject.incubator.net.routing.NextHopData;
32import org.onosproject.incubator.net.routing.Route;
Jonathan Hart96c146b2017-02-24 16:32:00 -080033import org.onosproject.incubator.net.routing.RouteSet;
Charles Chan0214ded2016-11-18 17:48:37 -080034import org.onosproject.incubator.net.routing.RouteStore;
35import org.onosproject.incubator.net.routing.RouteStoreDelegate;
36import org.onosproject.incubator.net.routing.RouteTableId;
37import org.onosproject.store.AbstractStore;
38import org.onosproject.store.service.StorageService;
39import org.osgi.service.component.ComponentContext;
40import org.osgi.service.component.annotations.Activate;
41import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
44import java.util.Collection;
45import java.util.Dictionary;
46import java.util.Map;
47import java.util.Set;
48
49/**
50 * An implementation of RouteStore that is backed by either LocalRouteStore or
51 * DistributedRouteStore according to configuration.
52 */
53@Service
54@Component
Jonathan Hart96c146b2017-02-24 16:32:00 -080055public class RouteStoreImpl extends AbstractStore<InternalRouteEvent, RouteStoreDelegate>
Charles Chan0214ded2016-11-18 17:48:37 -080056 implements RouteStore {
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected ComponentConfigService componentConfigService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 public StorageService storageService;
63
64 @Property(name = "distributed", boolValue = false,
65 label = "Enable distributed route store")
66 private boolean distributed;
67
68 private final Logger log = LoggerFactory.getLogger(getClass());
69 private RouteStore currentRouteStore;
70
Jonathan Hart74850702017-05-04 13:01:24 -070071 private DistributedRouteStore distributedRouteStore;
72 private LocalRouteStore localRouteStore;
73
Charles Chan0214ded2016-11-18 17:48:37 -080074 @Activate
75 public void activate(ComponentContext context) {
Jonathan Hart74850702017-05-04 13:01:24 -070076 distributedRouteStore = new DistributedRouteStore(storageService);
77 distributedRouteStore.activate();
78 localRouteStore = new LocalRouteStore();
79 localRouteStore.activate();
80
Charles Chan0214ded2016-11-18 17:48:37 -080081 componentConfigService.registerProperties(getClass());
82 modified(context);
83 }
84
85 @Deactivate
86 public void deactivate() {
Jonathan Hart74850702017-05-04 13:01:24 -070087 localRouteStore.deactivate();
88 distributedRouteStore.deactivate();
89
Charles Chan0214ded2016-11-18 17:48:37 -080090 componentConfigService.unregisterProperties(getClass(), false);
91 }
92
93 @Modified
94 public void modified(ComponentContext context) {
95 Dictionary<?, ?> properties = context.getProperties();
96 if (properties == null) {
97 return;
98 }
99
100 String strDistributed = Tools.get(properties, "distributed");
101 boolean expectDistributed = Boolean.parseBoolean(strDistributed);
102
103 // Start route store during first start or config change
104 // NOTE: new route store will be empty
105 if (currentRouteStore == null || expectDistributed != distributed) {
106 if (expectDistributed) {
Jonathan Hart74850702017-05-04 13:01:24 -0700107 currentRouteStore = distributedRouteStore;
Charles Chan0214ded2016-11-18 17:48:37 -0800108 } else {
Jonathan Hart74850702017-05-04 13:01:24 -0700109 currentRouteStore = localRouteStore;
Charles Chan0214ded2016-11-18 17:48:37 -0800110 }
111
112 this.distributed = expectDistributed;
113 log.info("Switched to {} route store", distributed ? "distributed" : "local");
114 }
115
116 }
117
118 @Override
Charles Chan143bc182017-01-13 15:46:03 -0800119 public void setDelegate(RouteStoreDelegate delegate) {
Jonathan Hart74850702017-05-04 13:01:24 -0700120 super.setDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800121
Jonathan Hart74850702017-05-04 13:01:24 -0700122 // Set the delegate of underlying route store implementations
123 localRouteStore.setDelegate(delegate);
124 distributedRouteStore.setDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800125 }
126
127 @Override
128 public void unsetDelegate(RouteStoreDelegate delegate) {
Jonathan Hart74850702017-05-04 13:01:24 -0700129 super.unsetDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800130
Jonathan Hart74850702017-05-04 13:01:24 -0700131 // Unset the delegate of underlying route store implementations
132 localRouteStore.unsetDelegate(delegate);
133 distributedRouteStore.unsetDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800134 }
135
136 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800137 public void updateRoute(Route route) {
138 currentRouteStore.updateRoute(route);
139 }
140
141 @Override
142 public void removeRoute(Route route) {
143 currentRouteStore.removeRoute(route);
144 }
145
146 @Override
147 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
157 public Route longestPrefixMatch(IpAddress ip) {
158 return currentRouteStore.longestPrefixMatch(ip);
159 }
160
161 @Override
162 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
163 return currentRouteStore.getRoutesForNextHop(ip);
164 }
165
166 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800167 public RouteSet getRoutes(IpPrefix prefix) {
168 return currentRouteStore.getRoutes(prefix);
169 }
170
171 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800172 public void updateNextHop(IpAddress ip, NextHopData nextHopData) {
173 currentRouteStore.updateNextHop(ip, nextHopData);
174 }
175
176 @Override
177 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
178 currentRouteStore.removeNextHop(ip, nextHopData);
179 }
180
181 @Override
182 public NextHopData getNextHop(IpAddress ip) {
183 return currentRouteStore.getNextHop(ip);
184 }
185
186 @Override
187 public Map<IpAddress, NextHopData> getNextHops() {
188 return currentRouteStore.getNextHops();
189 }
190}