blob: 747007e9a0a465a19e0e280b5c9e3e74db1e514d [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;
27import org.onlab.util.Tools;
28import org.onosproject.cfg.ComponentConfigService;
29import org.onosproject.incubator.net.routing.NextHopData;
30import org.onosproject.incubator.net.routing.Route;
31import org.onosproject.incubator.net.routing.RouteEvent;
32import org.onosproject.incubator.net.routing.RouteStore;
33import org.onosproject.incubator.net.routing.RouteStoreDelegate;
34import org.onosproject.incubator.net.routing.RouteTableId;
35import org.onosproject.store.AbstractStore;
36import org.onosproject.store.service.StorageService;
37import org.osgi.service.component.ComponentContext;
38import org.osgi.service.component.annotations.Activate;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42import java.util.Collection;
43import java.util.Dictionary;
44import java.util.Map;
45import java.util.Set;
46
Charles Chan143bc182017-01-13 15:46:03 -080047import static com.google.common.base.Preconditions.checkState;
48
Charles Chan0214ded2016-11-18 17:48:37 -080049/**
50 * An implementation of RouteStore that is backed by either LocalRouteStore or
51 * DistributedRouteStore according to configuration.
52 */
53@Service
54@Component
55public class RouteStoreImpl extends AbstractStore<RouteEvent, RouteStoreDelegate>
56 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
71 @Activate
72 public void activate(ComponentContext context) {
73 componentConfigService.registerProperties(getClass());
74 modified(context);
75 }
76
77 @Deactivate
78 public void deactivate() {
79 if (distributed) {
80 ((DistributedRouteStore) currentRouteStore).deactivate();
81 } else {
82 ((LocalRouteStore) currentRouteStore).deactivate();
83 }
84 componentConfigService.unregisterProperties(getClass(), false);
85 }
86
87 @Modified
88 public void modified(ComponentContext context) {
89 Dictionary<?, ?> properties = context.getProperties();
90 if (properties == null) {
91 return;
92 }
93
94 String strDistributed = Tools.get(properties, "distributed");
95 boolean expectDistributed = Boolean.parseBoolean(strDistributed);
96
97 // Start route store during first start or config change
98 // NOTE: new route store will be empty
99 if (currentRouteStore == null || expectDistributed != distributed) {
100 if (expectDistributed) {
101 if (currentRouteStore != null) {
102 ((LocalRouteStore) currentRouteStore).deactivate();
103 }
104 currentRouteStore = new DistributedRouteStore(storageService);
105 ((DistributedRouteStore) currentRouteStore).activate();
Charles Chan143bc182017-01-13 15:46:03 -0800106 ((DistributedRouteStore) currentRouteStore).setDelegate(delegate);
Charles Chan0214ded2016-11-18 17:48:37 -0800107 } else {
108 if (currentRouteStore != null) {
109 ((DistributedRouteStore) currentRouteStore).deactivate();
110 }
111 currentRouteStore = new LocalRouteStore();
112 ((LocalRouteStore) currentRouteStore).activate();
Charles Chan143bc182017-01-13 15:46:03 -0800113 ((LocalRouteStore) currentRouteStore).setDelegate(delegate);
Charles Chan0214ded2016-11-18 17:48:37 -0800114 }
115
116 this.distributed = expectDistributed;
117 log.info("Switched to {} route store", distributed ? "distributed" : "local");
118 }
119
120 }
121
122 @Override
Charles Chan143bc182017-01-13 15:46:03 -0800123 public void setDelegate(RouteStoreDelegate delegate) {
124 checkState(this.delegate == null || this.delegate == delegate,
125 "Store delegate already set");
126 this.delegate = delegate;
127
128 // Set the delegate of underlying route store implementation
129 currentRouteStore.setDelegate(delegate);
130 }
131
132 @Override
133 public void unsetDelegate(RouteStoreDelegate delegate) {
134 if (this.delegate == delegate) {
135 this.delegate = null;
136 }
137
138 // Unset the delegate of underlying route store implementation
139 currentRouteStore.unsetDelegate(delegate);
140 }
141
142 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800143 public void updateRoute(Route route) {
144 currentRouteStore.updateRoute(route);
145 }
146
147 @Override
148 public void removeRoute(Route route) {
149 currentRouteStore.removeRoute(route);
150 }
151
152 @Override
153 public Set<RouteTableId> getRouteTables() {
154 return currentRouteStore.getRouteTables();
155 }
156
157 @Override
158 public Collection<Route> getRoutes(RouteTableId table) {
159 return currentRouteStore.getRoutes(table);
160 }
161
162 @Override
163 public Route longestPrefixMatch(IpAddress ip) {
164 return currentRouteStore.longestPrefixMatch(ip);
165 }
166
167 @Override
168 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
169 return currentRouteStore.getRoutesForNextHop(ip);
170 }
171
172 @Override
173 public void updateNextHop(IpAddress ip, NextHopData nextHopData) {
174 currentRouteStore.updateNextHop(ip, nextHopData);
175 }
176
177 @Override
178 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
179 currentRouteStore.removeNextHop(ip, nextHopData);
180 }
181
182 @Override
183 public NextHopData getNextHop(IpAddress ip) {
184 return currentRouteStore.getNextHop(ip);
185 }
186
187 @Override
188 public Map<IpAddress, NextHopData> getNextHops() {
189 return currentRouteStore.getNextHops();
190 }
191}