blob: 4d1f5f02be923b68431499cb6f67277ea72b49a3 [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.Route;
Jonathan Hart96c146b2017-02-24 16:32:00 -080032import org.onosproject.incubator.net.routing.RouteSet;
Charles Chan0214ded2016-11-18 17:48:37 -080033import org.onosproject.incubator.net.routing.RouteStore;
34import org.onosproject.incubator.net.routing.RouteStoreDelegate;
35import org.onosproject.incubator.net.routing.RouteTableId;
36import org.onosproject.store.AbstractStore;
37import org.onosproject.store.service.StorageService;
38import org.osgi.service.component.ComponentContext;
39import org.osgi.service.component.annotations.Activate;
40import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
43import java.util.Collection;
44import java.util.Dictionary;
Charles Chan0214ded2016-11-18 17:48:37 -080045import java.util.Set;
46
47/**
48 * An implementation of RouteStore that is backed by either LocalRouteStore or
49 * DistributedRouteStore according to configuration.
50 */
51@Service
52@Component
Jonathan Hart96c146b2017-02-24 16:32:00 -080053public class RouteStoreImpl extends AbstractStore<InternalRouteEvent, RouteStoreDelegate>
Charles Chan0214ded2016-11-18 17:48:37 -080054 implements RouteStore {
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected ComponentConfigService componentConfigService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 public StorageService storageService;
61
62 @Property(name = "distributed", boolValue = false,
63 label = "Enable distributed route store")
64 private boolean distributed;
65
66 private final Logger log = LoggerFactory.getLogger(getClass());
67 private RouteStore currentRouteStore;
68
Jonathan Hart74850702017-05-04 13:01:24 -070069 private DistributedRouteStore distributedRouteStore;
70 private LocalRouteStore localRouteStore;
71
Charles Chan0214ded2016-11-18 17:48:37 -080072 @Activate
73 public void activate(ComponentContext context) {
Jonathan Hart74850702017-05-04 13:01:24 -070074 distributedRouteStore = new DistributedRouteStore(storageService);
75 distributedRouteStore.activate();
76 localRouteStore = new LocalRouteStore();
77 localRouteStore.activate();
78
Charles Chan0214ded2016-11-18 17:48:37 -080079 componentConfigService.registerProperties(getClass());
80 modified(context);
81 }
82
83 @Deactivate
84 public void deactivate() {
Jonathan Hart74850702017-05-04 13:01:24 -070085 localRouteStore.deactivate();
86 distributedRouteStore.deactivate();
87
Charles Chan0214ded2016-11-18 17:48:37 -080088 componentConfigService.unregisterProperties(getClass(), false);
89 }
90
91 @Modified
92 public void modified(ComponentContext context) {
93 Dictionary<?, ?> properties = context.getProperties();
94 if (properties == null) {
95 return;
96 }
97
98 String strDistributed = Tools.get(properties, "distributed");
99 boolean expectDistributed = Boolean.parseBoolean(strDistributed);
100
101 // Start route store during first start or config change
102 // NOTE: new route store will be empty
103 if (currentRouteStore == null || expectDistributed != distributed) {
104 if (expectDistributed) {
Jonathan Hart74850702017-05-04 13:01:24 -0700105 currentRouteStore = distributedRouteStore;
Charles Chan0214ded2016-11-18 17:48:37 -0800106 } else {
Jonathan Hart74850702017-05-04 13:01:24 -0700107 currentRouteStore = localRouteStore;
Charles Chan0214ded2016-11-18 17:48:37 -0800108 }
109
110 this.distributed = expectDistributed;
111 log.info("Switched to {} route store", distributed ? "distributed" : "local");
112 }
113
114 }
115
116 @Override
Charles Chan143bc182017-01-13 15:46:03 -0800117 public void setDelegate(RouteStoreDelegate delegate) {
Jonathan Hart74850702017-05-04 13:01:24 -0700118 super.setDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800119
Jonathan Hart74850702017-05-04 13:01:24 -0700120 // Set the delegate of underlying route store implementations
121 localRouteStore.setDelegate(delegate);
122 distributedRouteStore.setDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800123 }
124
125 @Override
126 public void unsetDelegate(RouteStoreDelegate delegate) {
Jonathan Hart74850702017-05-04 13:01:24 -0700127 super.unsetDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800128
Jonathan Hart74850702017-05-04 13:01:24 -0700129 // Unset the delegate of underlying route store implementations
130 localRouteStore.unsetDelegate(delegate);
131 distributedRouteStore.unsetDelegate(delegate);
Charles Chan143bc182017-01-13 15:46:03 -0800132 }
133
134 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800135 public void updateRoute(Route route) {
136 currentRouteStore.updateRoute(route);
137 }
138
139 @Override
140 public void removeRoute(Route route) {
141 currentRouteStore.removeRoute(route);
142 }
143
144 @Override
145 public Set<RouteTableId> getRouteTables() {
146 return currentRouteStore.getRouteTables();
147 }
148
149 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800150 public Collection<RouteSet> getRoutes(RouteTableId table) {
Charles Chan0214ded2016-11-18 17:48:37 -0800151 return currentRouteStore.getRoutes(table);
152 }
153
154 @Override
Charles Chan0214ded2016-11-18 17:48:37 -0800155 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
156 return currentRouteStore.getRoutesForNextHop(ip);
157 }
158
159 @Override
Jonathan Hart96c146b2017-02-24 16:32:00 -0800160 public RouteSet getRoutes(IpPrefix prefix) {
161 return currentRouteStore.getRoutes(prefix);
162 }
Charles Chan0214ded2016-11-18 17:48:37 -0800163}