blob: 972b6877b2f97b0f83428b8cd5fa9a5ed9e24303 [file] [log] [blame]
Charles Chand6d581a2015-11-18 16:51:08 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Charles Chand6d581a2015-11-18 16:51:08 -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.provider.netcfghost;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.Host;
31import org.onosproject.net.HostId;
32import org.onosproject.net.HostLocation;
33import org.onosproject.net.config.NetworkConfigEvent;
34import org.onosproject.net.config.NetworkConfigListener;
35import org.onosproject.net.config.NetworkConfigRegistry;
36import org.onosproject.net.config.basics.BasicHostConfig;
37import org.onosproject.net.host.DefaultHostDescription;
38import org.onosproject.net.host.HostDescription;
39import org.onosproject.net.host.HostProvider;
40import org.onosproject.net.host.HostProviderRegistry;
41import org.onosproject.net.host.HostProviderService;
42import org.onosproject.net.provider.AbstractProvider;
43import org.onosproject.net.provider.ProviderId;
44import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46import java.util.Set;
47
48/**
49 * Host provider that uses network config service to discover hosts.
50 */
51@Component(immediate = true)
52public class NetworkConfigHostProvider extends AbstractProvider implements HostProvider {
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected CoreService coreService;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected HostProviderRegistry providerRegistry;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected NetworkConfigRegistry networkConfigRegistry;
61
62 private static final String APP_NAME = "org.onosproject.provider.netcfghost";
63 private ApplicationId appId;
64 protected HostProviderService providerService;
65
66 private final Logger log = LoggerFactory.getLogger(getClass());
67 private final InternalNetworkConfigListener networkConfigListener =
68 new InternalNetworkConfigListener();
69
70 /**
71 * Creates an network config host location provider.
72 */
73 public NetworkConfigHostProvider() {
74 super(new ProviderId("host", APP_NAME));
75 }
76
77 @Activate
78 protected void activate() {
79 appId = coreService.registerApplication(APP_NAME);
80 providerService = providerRegistry.register(this);
81 networkConfigRegistry.addListener(networkConfigListener);
82 readInitialConfig();
83 log.info("Started");
84 }
85
86 @Deactivate
87 protected void deactivate() {
88 networkConfigRegistry.removeListener(networkConfigListener);
89 providerRegistry.unregister(this);
90 providerService = null;
91 log.info("Stopped");
92 }
93
94 @Override
95 public void triggerProbe(Host host) {
96 /*
97 * Note: In CORD deployment, we assume that all hosts are configured.
98 * Therefore no probe is required.
99 */
100 }
101
102 /**
103 * Adds host information.
104 * IP information will be appended if host exists.
105 *
106 * @param mac MAC address of the host
107 * @param vlan VLAN ID of the host
108 * @param hloc Location of the host
109 * @param ips Set of IP addresses of the host
110 */
111 protected void addHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
112 HostId hid = HostId.hostId(mac, vlan);
113 HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips);
114 providerService.hostDetected(hid, desc, false);
115 }
116
117 /**
118 * Updates host information.
119 * IP information will be replaced if host exists.
120 *
121 * @param mac MAC address of the host
122 * @param vlan VLAN ID of the host
123 * @param hloc Location of the host
124 * @param ips Set of IP addresses of the host
125 */
126 protected void updateHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
127 HostId hid = HostId.hostId(mac, vlan);
128 HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips);
129 providerService.hostDetected(hid, desc, true);
130 }
131
132 /**
133 * Removes host information.
134 *
135 * @param mac MAC address of the host
136 * @param vlan VLAN ID of the host
137 */
138 protected void removeHost(MacAddress mac, VlanId vlan) {
139 HostId hid = HostId.hostId(mac, vlan);
140 providerService.hostVanished(hid);
141 }
142
143 private void readInitialConfig() {
144 networkConfigRegistry.getSubjects(HostId.class).forEach(hostId -> {
145 MacAddress mac = hostId.mac();
146 VlanId vlan = hostId.vlanId();
147 BasicHostConfig hostConfig =
148 networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
149 Set<IpAddress> ipAddresses = hostConfig.ipAddresses();
150 ConnectPoint location = hostConfig.location();
151 HostLocation hloc = new HostLocation(location, System.currentTimeMillis());
152 addHost(mac, vlan, hloc, ipAddresses);
153 });
154 }
155
156 private class InternalNetworkConfigListener implements NetworkConfigListener {
157 @Override
158 public void event(NetworkConfigEvent event) {
159 // Do not process non-host, register and unregister events
160 if (!event.configClass().equals(BasicHostConfig.class) ||
161 event.type() == NetworkConfigEvent.Type.CONFIG_REGISTERED ||
162 event.type() == NetworkConfigEvent.Type.CONFIG_UNREGISTERED) {
163 return;
164 }
165
166 HostId hostId = (HostId) event.subject();
167 MacAddress mac = hostId.mac();
168 VlanId vlan = hostId.vlanId();
169 BasicHostConfig hostConfig =
170 networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
171 Set<IpAddress> ipAddresses = null;
172 HostLocation hloc = null;
173
174 // Note: There will be no config presented in the CONFIG_REMOVE case
175 if (hostConfig != null) {
176 ipAddresses = hostConfig.ipAddresses();
177 ConnectPoint location = hostConfig.location();
178 hloc = new HostLocation(location, System.currentTimeMillis());
179 }
180
181 switch (event.type()) {
182 case CONFIG_ADDED:
183 addHost(mac, vlan, hloc, ipAddresses);
184 break;
185 case CONFIG_UPDATED:
186 updateHost(mac, vlan, hloc, ipAddresses);
187 break;
188 case CONFIG_REMOVED:
189 removeHost(mac, vlan);
190 break;
191 default:
192 break;
193 }
194 }
195 }
196}