blob: fd0d6083c6f6b09db2f5cc11fb768296bfe54985 [file] [log] [blame]
Jonathan Hart60e7f512017-02-10 10:24:24 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hart60e7f512017-02-10 10:24:24 -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
17package org.onosproject.routing.config;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Sets;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.net.config.ConfigFactory;
23import org.onosproject.net.config.NetworkConfigRegistry;
24import org.onosproject.net.config.NetworkConfigService;
25import org.onosproject.net.config.basics.SubjectFactories;
26import org.onosproject.routing.RoutingService;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import java.util.Collections;
31import java.util.Set;
32
33/**
34 * Helper class to manage routing configurations.
35 */
36public final class RoutingConfiguration {
37
38 private static final String WARNING =
39 "Config apps/org.onosproject.routing/router is deprecated "
40 + "and will be removed in a future release.";
41 private static final String WARNING2 =
42 "Use apps/org.onosproject.routing/routers instead";
43
44 private static final Logger log = LoggerFactory.getLogger(RoutingConfiguration.class);
45
46 private static ConfigFactory<ApplicationId, BgpConfig> bgpConfigFactory =
47 new ConfigFactory<ApplicationId, BgpConfig>(
48 SubjectFactories.APP_SUBJECT_FACTORY, BgpConfig.class, "bgp") {
49 @Override
50 public BgpConfig createConfig() {
51 return new BgpConfig();
52 }
53 };
54
55 private static ConfigFactory<ApplicationId, RouterConfig> routerConfigFactory =
56 new ConfigFactory<ApplicationId, RouterConfig>(
57 SubjectFactories.APP_SUBJECT_FACTORY, RouterConfig.class, "router") {
58 @Override
59 public RouterConfig createConfig() {
60 return new RouterConfig();
61 }
62 };
63
64 private static ConfigFactory<ApplicationId, RoutersConfig> routersConfigFactory =
65 new ConfigFactory<ApplicationId, RoutersConfig>(
66 SubjectFactories.APP_SUBJECT_FACTORY, RoutersConfig.class, "routers", true) {
67 @Override
68 public RoutersConfig createConfig() {
69 return new RoutersConfig();
70 }
71 };
72
73 private static ImmutableList<ConfigFactory<?, ?>> factories = ImmutableList.<ConfigFactory<?, ?>>builder()
74 .add(bgpConfigFactory)
75 .add(routerConfigFactory)
76 .add(routersConfigFactory)
77 .build();
78
79 private static Integer registrations = 0;
80
81 private RoutingConfiguration() {
82 // make checkstyle happy
83 }
84
85 /**
86 * Registers the routing configuration factories.
87 *
88 * @param registry network config registry service
89 */
90 public static void register(NetworkConfigRegistry registry) {
91 synchronized (registrations) {
92 if (registrations == 0) {
93 factories.forEach(registry::registerConfigFactory);
94 }
95 registrations++;
96 }
97 }
98
99 /**
100 * Unregisters the routing configuration factories.
101 * <p>The factories will only be unregistered from the network config
102 * registry if no other routing applications are using them. Any components
103 * that call NetworkConfigHelper#registerConfigFactories need to also call
104 * this method when they are no longer using the config
105 * </p>
106 *
107 * @param registry network config registry service
108 */
109 public static void unregister(NetworkConfigRegistry registry) {
110 synchronized (registrations) {
111 registrations--;
112 if (registrations == 0) {
113 factories.forEach(registry::unregisterConfigFactory);
114 }
115 }
116 }
117
118 /**
119 * Retrieves the router configurations.
120 *
121 * @param configService network config service
122 * @param routingAppId routing app ID
123 * @return set of router configurations
124 */
125 public static Set<RoutersConfig.Router> getRouterConfigurations(
126 NetworkConfigService configService, ApplicationId routingAppId) {
127
128 RouterConfig config = configService.getConfig(
129 routingAppId, RoutingService.ROUTER_CONFIG_CLASS);
130 RoutersConfig multiConfig = configService.getConfig(routingAppId, RoutersConfig.class);
131
132 if (config != null) {
133 log.warn(WARNING);
134 log.warn(WARNING2);
135
136 return Collections.singleton(
137 new RoutersConfig.Router(config.getControlPlaneConnectPoint(),
138 config.getOspfEnabled(),
139 Sets.newHashSet(config.getInterfaces())));
140 } else if (multiConfig != null) {
141 return multiConfig.getRouters();
142 } else {
143 return Collections.emptySet();
144 }
145 }
146}