blob: 41e39dd81a22bcd204ef86f3544a27f7b7c7fc42 [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);
Charles Chan7e0e2e62016-04-15 16:30:41 -0700113 HostDescription desc = (ips != null) ?
114 new DefaultHostDescription(mac, vlan, hloc, ips) :
115 new DefaultHostDescription(mac, vlan, hloc);
Charles Chand6d581a2015-11-18 16:51:08 -0800116 providerService.hostDetected(hid, desc, false);
117 }
118
119 /**
120 * Updates host information.
121 * IP information will be replaced if host exists.
122 *
123 * @param mac MAC address of the host
124 * @param vlan VLAN ID of the host
125 * @param hloc Location of the host
126 * @param ips Set of IP addresses of the host
127 */
128 protected void updateHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
129 HostId hid = HostId.hostId(mac, vlan);
130 HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips);
131 providerService.hostDetected(hid, desc, true);
132 }
133
134 /**
135 * Removes host information.
136 *
137 * @param mac MAC address of the host
138 * @param vlan VLAN ID of the host
139 */
140 protected void removeHost(MacAddress mac, VlanId vlan) {
141 HostId hid = HostId.hostId(mac, vlan);
142 providerService.hostVanished(hid);
143 }
144
145 private void readInitialConfig() {
146 networkConfigRegistry.getSubjects(HostId.class).forEach(hostId -> {
147 MacAddress mac = hostId.mac();
148 VlanId vlan = hostId.vlanId();
149 BasicHostConfig hostConfig =
150 networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
151 Set<IpAddress> ipAddresses = hostConfig.ipAddresses();
152 ConnectPoint location = hostConfig.location();
153 HostLocation hloc = new HostLocation(location, System.currentTimeMillis());
154 addHost(mac, vlan, hloc, ipAddresses);
155 });
156 }
157
158 private class InternalNetworkConfigListener implements NetworkConfigListener {
159 @Override
160 public void event(NetworkConfigEvent event) {
161 // Do not process non-host, register and unregister events
162 if (!event.configClass().equals(BasicHostConfig.class) ||
163 event.type() == NetworkConfigEvent.Type.CONFIG_REGISTERED ||
164 event.type() == NetworkConfigEvent.Type.CONFIG_UNREGISTERED) {
165 return;
166 }
167
168 HostId hostId = (HostId) event.subject();
169 MacAddress mac = hostId.mac();
170 VlanId vlan = hostId.vlanId();
171 BasicHostConfig hostConfig =
172 networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
173 Set<IpAddress> ipAddresses = null;
174 HostLocation hloc = null;
175
176 // Note: There will be no config presented in the CONFIG_REMOVE case
177 if (hostConfig != null) {
178 ipAddresses = hostConfig.ipAddresses();
179 ConnectPoint location = hostConfig.location();
180 hloc = new HostLocation(location, System.currentTimeMillis());
181 }
182
183 switch (event.type()) {
184 case CONFIG_ADDED:
185 addHost(mac, vlan, hloc, ipAddresses);
186 break;
187 case CONFIG_UPDATED:
188 updateHost(mac, vlan, hloc, ipAddresses);
189 break;
190 case CONFIG_REMOVED:
191 removeHost(mac, vlan);
192 break;
193 default:
194 break;
195 }
196 }
197 }
198}