blob: 06997154fa92d86cf27fae56a53a4562f438d7a6 [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
47/**
48 * An implementation of RouteStore that is backed by either LocalRouteStore or
49 * DistributedRouteStore according to configuration.
50 */
51@Service
52@Component
53public class RouteStoreImpl extends AbstractStore<RouteEvent, RouteStoreDelegate>
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
69 @Activate
70 public void activate(ComponentContext context) {
71 componentConfigService.registerProperties(getClass());
72 modified(context);
73 }
74
75 @Deactivate
76 public void deactivate() {
77 if (distributed) {
78 ((DistributedRouteStore) currentRouteStore).deactivate();
79 } else {
80 ((LocalRouteStore) currentRouteStore).deactivate();
81 }
82 componentConfigService.unregisterProperties(getClass(), false);
83 }
84
85 @Modified
86 public void modified(ComponentContext context) {
87 Dictionary<?, ?> properties = context.getProperties();
88 if (properties == null) {
89 return;
90 }
91
92 String strDistributed = Tools.get(properties, "distributed");
93 boolean expectDistributed = Boolean.parseBoolean(strDistributed);
94
95 // Start route store during first start or config change
96 // NOTE: new route store will be empty
97 if (currentRouteStore == null || expectDistributed != distributed) {
98 if (expectDistributed) {
99 if (currentRouteStore != null) {
100 ((LocalRouteStore) currentRouteStore).deactivate();
101 }
102 currentRouteStore = new DistributedRouteStore(storageService);
103 ((DistributedRouteStore) currentRouteStore).activate();
104 } else {
105 if (currentRouteStore != null) {
106 ((DistributedRouteStore) currentRouteStore).deactivate();
107 }
108 currentRouteStore = new LocalRouteStore();
109 ((LocalRouteStore) currentRouteStore).activate();
110 }
111
112 this.distributed = expectDistributed;
113 log.info("Switched to {} route store", distributed ? "distributed" : "local");
114 }
115
116 }
117
118 @Override
119 public void updateRoute(Route route) {
120 currentRouteStore.updateRoute(route);
121 }
122
123 @Override
124 public void removeRoute(Route route) {
125 currentRouteStore.removeRoute(route);
126 }
127
128 @Override
129 public Set<RouteTableId> getRouteTables() {
130 return currentRouteStore.getRouteTables();
131 }
132
133 @Override
134 public Collection<Route> getRoutes(RouteTableId table) {
135 return currentRouteStore.getRoutes(table);
136 }
137
138 @Override
139 public Route longestPrefixMatch(IpAddress ip) {
140 return currentRouteStore.longestPrefixMatch(ip);
141 }
142
143 @Override
144 public Collection<Route> getRoutesForNextHop(IpAddress ip) {
145 return currentRouteStore.getRoutesForNextHop(ip);
146 }
147
148 @Override
149 public void updateNextHop(IpAddress ip, NextHopData nextHopData) {
150 currentRouteStore.updateNextHop(ip, nextHopData);
151 }
152
153 @Override
154 public void removeNextHop(IpAddress ip, NextHopData nextHopData) {
155 currentRouteStore.removeNextHop(ip, nextHopData);
156 }
157
158 @Override
159 public NextHopData getNextHop(IpAddress ip) {
160 return currentRouteStore.getNextHop(ip);
161 }
162
163 @Override
164 public Map<IpAddress, NextHopData> getNextHops() {
165 return currentRouteStore.getNextHops();
166 }
167}