blob: 0e7dd90b57defdbb42f01b367e311106cd94880e [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;
Charles Chanbfc5e552020-07-17 19:12:43 -070033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Jonathan Hart79a0abd2017-02-23 19:13:27 -080035
Charles Chanbfc5e552020-07-17 19:12:43 -070036import java.util.Optional;
Jonathan Hart79a0abd2017-02-23 19:13:27 -080037import java.util.Set;
38import java.util.stream.Collectors;
39
40/**
41 * Route source that installs static routes configured in the network configuration.
42 */
43@Component(immediate = true)
44public class ConfigurationRouteSource {
45
Charles Chanbfc5e552020-07-17 19:12:43 -070046 private static final Logger log = LoggerFactory.getLogger(ConfigurationRouteSource.class);
47
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart79a0abd2017-02-23 19:13:27 -080049 protected NetworkConfigRegistry netcfgRegistry;
50
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart79a0abd2017-02-23 19:13:27 -080052 protected RouteAdminService routeService;
53
54 private final ConfigFactory<ApplicationId, RouteConfig> routeConfigFactory =
55 new ConfigFactory<ApplicationId, RouteConfig>(
56 SubjectFactories.APP_SUBJECT_FACTORY,
57 RouteConfig.class, "routes", true) {
58 @Override
59 public RouteConfig createConfig() {
60 return new RouteConfig();
61 }
62 };
63 private final InternalNetworkConfigListener netcfgListener =
64 new InternalNetworkConfigListener();
65
66 @Activate
67 protected void activate() {
68 netcfgRegistry.addListener(netcfgListener);
69 netcfgRegistry.registerConfigFactory(routeConfigFactory);
Charles Chanbfc5e552020-07-17 19:12:43 -070070
71 // Read initial routes in netcfg
72 netcfgRegistry.getSubjects(ApplicationId.class, RouteConfig.class).forEach(subject -> {
73 Optional.ofNullable(netcfgRegistry.getConfig(subject, RouteConfig.class))
74 .map(RouteConfig::getRoutes)
75 .ifPresent(routes -> {
76 log.info("Load initial routes from netcfg: {}", routes);
77 routeService.update(routes);
78 });
79 });
80
Jonathan Hart79a0abd2017-02-23 19:13:27 -080081 }
82
83 @Deactivate
84 protected void deactivate() {
85 netcfgRegistry.removeListener(netcfgListener);
86 netcfgRegistry.unregisterConfigFactory(routeConfigFactory);
87 }
88
89 private void processRouteConfigAdded(NetworkConfigEvent event) {
90 Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes();
91 routeService.update(routes);
92 }
93
94 private void processRouteConfigUpdated(NetworkConfigEvent event) {
95 Set<Route> routes = ((RouteConfig) event.config().get()).getRoutes();
96 Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes();
97 Set<Route> pendingRemove = prevRoutes.stream()
98 .filter(prevRoute -> routes.stream()
99 .noneMatch(route -> route.prefix().equals(prevRoute.prefix())))
100 .collect(Collectors.toSet());
101 Set<Route> pendingUpdate = routes.stream()
102 .filter(route -> !pendingRemove.contains(route)).collect(Collectors.toSet());
103 routeService.update(pendingUpdate);
104 routeService.withdraw(pendingRemove);
105 }
106
107 private void processRouteConfigRemoved(NetworkConfigEvent event) {
108 Set<Route> prevRoutes = ((RouteConfig) event.prevConfig().get()).getRoutes();
109 routeService.withdraw(prevRoutes);
110 }
111
112 private class InternalNetworkConfigListener implements
113 NetworkConfigListener {
114 @Override
115 public void event(NetworkConfigEvent event) {
116 if (event.configClass().equals(RouteConfig.class)) {
117 switch (event.type()) {
118 case CONFIG_ADDED:
119 processRouteConfigAdded(event);
120 break;
121 case CONFIG_UPDATED:
122 processRouteConfigUpdated(event);
123 break;
124 case CONFIG_REMOVED:
125 processRouteConfigRemoved(event);
126 break;
127 default:
128 break;
129 }
130 }
131 }
132 }
133}