Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 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 | |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 17 | package org.onosproject.routeservice.store; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 18 | |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IpPrefix; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 28 | import org.onlab.util.Tools; |
| 29 | import org.onosproject.cfg.ComponentConfigService; |
Ray Milkey | 69ec871 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 30 | import org.onosproject.routeservice.InternalRouteEvent; |
| 31 | import org.onosproject.routeservice.Route; |
| 32 | import org.onosproject.routeservice.RouteSet; |
| 33 | import org.onosproject.routeservice.RouteStore; |
| 34 | import org.onosproject.routeservice.RouteStoreDelegate; |
| 35 | import org.onosproject.routeservice.RouteTableId; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 36 | import org.onosproject.store.AbstractStore; |
| 37 | import org.onosproject.store.service.StorageService; |
| 38 | import org.osgi.service.component.ComponentContext; |
| 39 | import org.osgi.service.component.annotations.Activate; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
| 43 | import java.util.Collection; |
| 44 | import java.util.Dictionary; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 45 | import 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 Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 53 | public class RouteStoreImpl extends AbstractStore<InternalRouteEvent, RouteStoreDelegate> |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 54 | 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 Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 69 | private DistributedRouteStore distributedRouteStore; |
| 70 | private LocalRouteStore localRouteStore; |
| 71 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 72 | @Activate |
| 73 | public void activate(ComponentContext context) { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 74 | distributedRouteStore = new DistributedRouteStore(storageService); |
| 75 | distributedRouteStore.activate(); |
| 76 | localRouteStore = new LocalRouteStore(); |
| 77 | localRouteStore.activate(); |
| 78 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 79 | componentConfigService.registerProperties(getClass()); |
| 80 | modified(context); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | public void deactivate() { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 85 | localRouteStore.deactivate(); |
| 86 | distributedRouteStore.deactivate(); |
| 87 | |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 88 | 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 Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 105 | currentRouteStore = distributedRouteStore; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 106 | } else { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 107 | currentRouteStore = localRouteStore; |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | this.distributed = expectDistributed; |
| 111 | log.info("Switched to {} route store", distributed ? "distributed" : "local"); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | @Override |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 117 | public void setDelegate(RouteStoreDelegate delegate) { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 118 | super.setDelegate(delegate); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 119 | |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 120 | // Set the delegate of underlying route store implementations |
| 121 | localRouteStore.setDelegate(delegate); |
| 122 | distributedRouteStore.setDelegate(delegate); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void unsetDelegate(RouteStoreDelegate delegate) { |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 127 | super.unsetDelegate(delegate); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 128 | |
Jonathan Hart | 7485070 | 2017-05-04 13:01:24 -0700 | [diff] [blame] | 129 | // Unset the delegate of underlying route store implementations |
| 130 | localRouteStore.unsetDelegate(delegate); |
| 131 | distributedRouteStore.unsetDelegate(delegate); |
Charles Chan | 143bc18 | 2017-01-13 15:46:03 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 135 | 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 Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 150 | public Collection<RouteSet> getRoutes(RouteTableId table) { |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 151 | return currentRouteStore.getRoutes(table); |
| 152 | } |
| 153 | |
| 154 | @Override |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 155 | public Collection<Route> getRoutesForNextHop(IpAddress ip) { |
| 156 | return currentRouteStore.getRoutesForNextHop(ip); |
| 157 | } |
| 158 | |
| 159 | @Override |
Jonathan Hart | 96c146b | 2017-02-24 16:32:00 -0800 | [diff] [blame] | 160 | public RouteSet getRoutes(IpPrefix prefix) { |
| 161 | return currentRouteStore.getRoutes(prefix); |
| 162 | } |
Charles Chan | 4f36573 | 2017-07-20 17:11:57 -0700 | [diff] [blame] | 163 | |
| 164 | @Override |
| 165 | public String name() { |
| 166 | return currentRouteStore.name(); |
| 167 | } |
Charles Chan | 0214ded | 2016-11-18 17:48:37 -0800 | [diff] [blame] | 168 | } |