blob: bad5b92809ccbe49f4ccb4660e273dff7d8f0e5c [file] [log] [blame]
Jonathan Hart79a0abd2017-02-23 19:13:27 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hart79a0abd2017-02-23 19:13:27 -08003 *
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 Milkey69ec8712017-08-08 13:00:43 -070017package org.onosproject.routeservice.impl;
Jonathan Hart79a0abd2017-02-23 19:13:27 -080018
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Activate;
20import org.osgi.service.component.annotations.Component;
21import org.osgi.service.component.annotations.Deactivate;
22import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
Jonathan Hart79a0abd2017-02-23 19:13:27 -080024import org.onosproject.core.ApplicationId;
Ray Milkey69ec8712017-08-08 13:00:43 -070025import org.onosproject.routeservice.Route;
26import org.onosproject.routeservice.RouteAdminService;
27import org.onosproject.routeservice.RouteConfig;
Jonathan Hart79a0abd2017-02-23 19:13:27 -080028import org.onosproject.net.config.ConfigFactory;
29import org.onosproject.net.config.NetworkConfigEvent;
30import org.onosproject.net.config.NetworkConfigListener;
31import org.onosproject.net.config.NetworkConfigRegistry;
32import org.onosproject.net.config.basics.SubjectFactories;
33
34import java.util.Set;
35import java.util.stream.Collectors;
36
37/**
38 * Route source that installs static routes configured in the network configuration.
39 */
40@Component(immediate = true)
41public class ConfigurationRouteSource {
42
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart79a0abd2017-02-23 19:13:27 -080044 protected NetworkConfigRegistry netcfgRegistry;
45
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart79a0abd2017-02-23 19:13:27 -080047 protected RouteAdminService routeService;
48
49 private final ConfigFactory<ApplicationId, RouteConfig> routeConfigFactory =
50 new ConfigFactory<ApplicationId, RouteConfig>(
51 SubjectFactories.APP_SUBJECT_FACTORY,
52 RouteConfig.class, "routes", true) {
53 @Override
54 public RouteConfig createConfig() {
55 return new RouteConfig();
56 }
57 };
58 private final InternalNetworkConfigListener netcfgListener =
59 new InternalNetworkConfigListener();
60
61 @Activate
62 protected void activate() {
63 netcfgRegistry.addListener(netcfgListener);
64 netcfgRegistry.registerConfigFactory(routeConfigFactory);
65 }
66
67 @Deactivate
68 protected void deactivate() {
69 netcfgRegistry.removeListener(netcfgListener);
70 netcfgRegistry.unregisterConfigFactory(routeConfigFactory);
71 }
72
73 private void processRouteConfigAdded(NetworkConfigEvent event) {
74 Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes();
75 routeService.update(routes);
76 }
77
78 private void processRouteConfigUpdated(NetworkConfigEvent event) {
79 Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes();
80 Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes();
81 Set<Route> pendingRemove = prevRoutes.stream()
82 .filter(prevRoute -> routes.stream()
83 .noneMatch(route -> route.prefix().equals(prevRoute.prefix())))
84 .collect(Collectors.toSet());
85 Set<Route> pendingUpdate = routes.stream()
86 .filter(route -> !pendingRemove.contains(route)).collect(Collectors.toSet());
87 routeService.update(pendingUpdate);
88 routeService.withdraw(pendingRemove);
89 }
90
91 private void processRouteConfigRemoved(NetworkConfigEvent event) {
92 Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes();
93 routeService.withdraw(prevRoutes);
94 }
95
96 private class InternalNetworkConfigListener implements
97 NetworkConfigListener {
98 @Override
99 public void event(NetworkConfigEvent event) {
100 if (event.configClass().equals(RouteConfig.class)) {
101 switch (event.type()) {
102 case CONFIG_ADDED:
103 processRouteConfigAdded(event);
104 break;
105 case CONFIG_UPDATED:
106 processRouteConfigUpdated(event);
107 break;
108 case CONFIG_REMOVED:
109 processRouteConfigRemoved(event);
110 break;
111 default:
112 break;
113 }
114 }
115 }
116 }
117}