Jonathan Hart
Committed by Gerrit Code Review

Create CordConfigService to manage configurations

Change-Id: I1c7346d34b0b562aa968cba66fc836f0414192be
...@@ -30,11 +30,11 @@ ...@@ -30,11 +30,11 @@
30 <artifactId>onos-cord-config</artifactId> 30 <artifactId>onos-cord-config</artifactId>
31 <packaging>bundle</packaging> 31 <packaging>bundle</packaging>
32 32
33 - <description>Cord configuration meta applications </description> 33 + <description>CORD configuration meta application</description>
34 34
35 <properties> 35 <properties>
36 <onos.app.name>org.onosproject.cord-config</onos.app.name> 36 <onos.app.name>org.onosproject.cord-config</onos.app.name>
37 - <onos.app.title>Cord Configuratuon Meta Application</onos.app.title> 37 + <onos.app.title>CORD Configuratuon Meta Application</onos.app.title>
38 <onos.app.category>Utility</onos.app.category> 38 <onos.app.category>Utility</onos.app.category>
39 <onos.app.url>http://opencord.org</onos.app.url> 39 <onos.app.url>http://opencord.org</onos.app.url>
40 </properties> 40 </properties>
......
...@@ -38,8 +38,19 @@ public class AccessDeviceConfig extends Config<DeviceId> { ...@@ -38,8 +38,19 @@ public class AccessDeviceConfig extends Config<DeviceId> {
38 * Gets the access device configuration for this device. 38 * Gets the access device configuration for this device.
39 * 39 *
40 * @return access device configuration 40 * @return access device configuration
41 + * @deprecated in Goldeneye release. Use {@link #getAccessDevice()} instead.
41 */ 42 */
43 + @Deprecated
42 public AccessDeviceData getOlt() { 44 public AccessDeviceData getOlt() {
45 + return getAccessDevice();
46 + }
47 +
48 + /**
49 + * Gets the access device configuration for this device.
50 + *
51 + * @return access device configuration
52 + */
53 + public AccessDeviceData getAccessDevice() {
43 PortNumber uplink = PortNumber.portNumber(node.path(UPLINK).asText()); 54 PortNumber uplink = PortNumber.portNumber(node.path(UPLINK).asText());
44 VlanId vlan = VlanId.vlanId(Short.parseShort(node.path(VLAN).asText())); 55 VlanId vlan = VlanId.vlanId(Short.parseShort(node.path(VLAN).asText()));
45 JsonNode defaultVlanNode = node.path(DEFAULT_VLAN); 56 JsonNode defaultVlanNode = node.path(DEFAULT_VLAN);
......
1 +/*
2 + * Copyright 2016-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 +
17 +package org.onosproject.cordconfig.access;
18 +
19 +import com.google.common.collect.ImmutableSet;
20 +import org.apache.felix.scr.annotations.Activate;
21 +import org.apache.felix.scr.annotations.Component;
22 +import org.apache.felix.scr.annotations.Deactivate;
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.onosproject.net.DeviceId;
27 +import org.onosproject.net.config.ConfigFactory;
28 +import org.onosproject.net.config.NetworkConfigEvent;
29 +import org.onosproject.net.config.NetworkConfigListener;
30 +import org.onosproject.net.config.NetworkConfigRegistry;
31 +import org.onosproject.net.config.basics.SubjectFactories;
32 +
33 +import java.util.Map;
34 +import java.util.Optional;
35 +import java.util.Set;
36 +import java.util.concurrent.ConcurrentHashMap;
37 +
38 +import static com.google.common.base.Preconditions.checkNotNull;
39 +
40 +/**
41 + * Manages the common CORD configuration.
42 + */
43 +@Service
44 +@Component(immediate = true)
45 +public class CordConfig implements CordConfigService {
46 +
47 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 + protected NetworkConfigRegistry networkConfig;
49 +
50 + private Map<DeviceId, AccessDeviceData> accessDevices = new ConcurrentHashMap<>();
51 + private Map<DeviceId, AccessAgentData> accessAgents = new ConcurrentHashMap<>();
52 +
53 + private static final Class<AccessDeviceConfig> ACCESS_DEVICE_CONFIG_CLASS =
54 + AccessDeviceConfig.class;
55 + private static final String ACCESS_DEVICE_CONFIG_KEY = "accessDevice";
56 +
57 + private ConfigFactory<DeviceId, AccessDeviceConfig> deviceConfigFactory =
58 + new ConfigFactory<DeviceId, AccessDeviceConfig>(
59 + SubjectFactories.DEVICE_SUBJECT_FACTORY,
60 + ACCESS_DEVICE_CONFIG_CLASS, ACCESS_DEVICE_CONFIG_KEY) {
61 + @Override
62 + public AccessDeviceConfig createConfig() {
63 + return new AccessDeviceConfig();
64 + }
65 + };
66 +
67 + private static final Class<AccessAgentConfig> ACCESS_AGENT_CONFIG_CLASS =
68 + AccessAgentConfig.class;
69 + private static final String ACCESS_AGENT_CONFIG_KEY = "accessAgent";
70 +
71 + private ConfigFactory<DeviceId, AccessAgentConfig> agentConfigFactory =
72 + new ConfigFactory<DeviceId, AccessAgentConfig>(
73 + SubjectFactories.DEVICE_SUBJECT_FACTORY,
74 + ACCESS_AGENT_CONFIG_CLASS, ACCESS_AGENT_CONFIG_KEY) {
75 + @Override
76 + public AccessAgentConfig createConfig() {
77 + return new AccessAgentConfig();
78 + }
79 + };
80 +
81 + private InternalNetworkConfigListener configListener =
82 + new InternalNetworkConfigListener();
83 +
84 + @Activate
85 + protected void activate() {
86 + networkConfig.registerConfigFactory(deviceConfigFactory);
87 + networkConfig.registerConfigFactory(agentConfigFactory);
88 +
89 + networkConfig.addListener(configListener);
90 +
91 + networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class)
92 + .forEach(this::addAccessDeviceConfig);
93 +
94 + networkConfig.getSubjects(DeviceId.class, AccessAgentConfig.class)
95 + .forEach(this::addAccessAgentConfig);
96 + }
97 +
98 + @Deactivate
99 + protected void deactivate() {
100 + networkConfig.unregisterConfigFactory(deviceConfigFactory);
101 + networkConfig.unregisterConfigFactory(agentConfigFactory);
102 + }
103 +
104 + private void addAccessDeviceConfig(DeviceId subject) {
105 + AccessDeviceConfig config =
106 + networkConfig.getConfig(subject, ACCESS_DEVICE_CONFIG_CLASS);
107 + if (config != null) {
108 + addAccessDevice(config);
109 + }
110 + }
111 +
112 + private void addAccessDevice(AccessDeviceConfig config) {
113 + AccessDeviceData accessDevice = config.getAccessDevice();
114 + accessDevices.put(accessDevice.deviceId(), accessDevice);
115 + }
116 +
117 + private void removeAccessDeviceConfig(DeviceId subject) {
118 + accessDevices.remove(subject);
119 + }
120 +
121 + private void addAccessAgentConfig(DeviceId subject) {
122 + AccessAgentConfig config =
123 + networkConfig.getConfig(subject, ACCESS_AGENT_CONFIG_CLASS);
124 + if (config != null) {
125 + addAccessAgent(config);
126 + }
127 + }
128 +
129 + private void addAccessAgent(AccessAgentConfig config) {
130 + AccessAgentData accessAgent = config.getAgent();
131 + accessAgents.put(accessAgent.deviceId(), accessAgent);
132 + }
133 +
134 + private void removeAccessAgentConfig(DeviceId subject) {
135 + accessAgents.remove(subject);
136 + }
137 +
138 + @Override
139 + public Set<AccessDeviceData> getAccessDevices() {
140 + return ImmutableSet.copyOf(accessDevices.values());
141 + }
142 +
143 + @Override
144 + public Optional<AccessDeviceData> getAccessDevice(DeviceId deviceId) {
145 + checkNotNull(deviceId, "Device ID cannot be null");
146 + return Optional.ofNullable(accessDevices.get(deviceId));
147 + }
148 +
149 + @Override
150 + public Set<AccessAgentData> getAccessAgents() {
151 + return ImmutableSet.copyOf(accessAgents.values());
152 + }
153 +
154 + @Override
155 + public Optional<AccessAgentData> getAccessAgent(DeviceId deviceId) {
156 + checkNotNull(deviceId, "Device ID cannot be null");
157 + return Optional.ofNullable(accessAgents.get(deviceId));
158 + }
159 +
160 + private class InternalNetworkConfigListener implements NetworkConfigListener {
161 + @Override
162 + public void event(NetworkConfigEvent event) {
163 + switch (event.type()) {
164 + case CONFIG_ADDED:
165 + case CONFIG_UPDATED:
166 + if (event.configClass().equals(ACCESS_DEVICE_CONFIG_CLASS)) {
167 + addAccessDeviceConfig((DeviceId) event.subject());
168 + } else if (event.configClass().equals(ACCESS_AGENT_CONFIG_CLASS)) {
169 + addAccessAgentConfig((DeviceId) event.subject());
170 + }
171 + break;
172 + case CONFIG_REMOVED:
173 + if (event.configClass().equals(ACCESS_DEVICE_CONFIG_CLASS)) {
174 + removeAccessDeviceConfig((DeviceId) event.subject());
175 + } else if (event.configClass().equals(ACCESS_AGENT_CONFIG_CLASS)) {
176 + removeAccessAgentConfig((DeviceId) event.subject());
177 + }
178 + break;
179 + case CONFIG_REGISTERED:
180 + case CONFIG_UNREGISTERED:
181 + default:
182 + break;
183 + }
184 + }
185 + }
186 +}
1 +/*
2 + * Copyright 2016-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 +
17 +package org.onosproject.cordconfig.access;
18 +
19 +import org.onosproject.net.DeviceId;
20 +
21 +import java.util.Optional;
22 +import java.util.Set;
23 +
24 +/**
25 + * Provides access to the common CORD configuration.
26 + */
27 +public interface CordConfigService {
28 +
29 + /**
30 + * Retrieves the set of all access devices in the system.
31 + *
32 + * @return set of access devices
33 + */
34 + Set<AccessDeviceData> getAccessDevices();
35 +
36 + /**
37 + * Retrieves the access device with the given device ID.
38 + *
39 + * @param deviceId device ID
40 + * @return access device
41 + */
42 + Optional<AccessDeviceData> getAccessDevice(DeviceId deviceId);
43 +
44 + /**
45 + * Retrieves the set of all access agents in the system.
46 + *
47 + * @return set of access agents
48 + */
49 + Set<AccessAgentData> getAccessAgents();
50 +
51 + /**
52 + * Retrieves the access agent for the given device ID.
53 + *
54 + * @param deviceId device ID
55 + * @return access agent
56 + */
57 + Optional<AccessAgentData> getAccessAgent(DeviceId deviceId);
58 +}