blob: 5e7c4e93e19951659a1ebd482f79c450e4054801 [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
Charles Chand6d581a2015-11-18 16:51:08 -080062 private ApplicationId appId;
Charles Chan6ea94fc2016-05-10 17:29:47 -070063 private static final String APP_NAME = "org.onosproject.netcfghost";
Charles Chanb3007e12016-05-20 10:55:40 -070064 private static final ProviderId PROVIDER_ID = new ProviderId("host", APP_NAME);
Charles Chand6d581a2015-11-18 16:51:08 -080065 protected HostProviderService providerService;
66
67 private final Logger log = LoggerFactory.getLogger(getClass());
68 private final InternalNetworkConfigListener networkConfigListener =
69 new InternalNetworkConfigListener();
70
71 /**
72 * Creates an network config host location provider.
73 */
74 public NetworkConfigHostProvider() {
Charles Chan6ea94fc2016-05-10 17:29:47 -070075 super(PROVIDER_ID);
Charles Chand6d581a2015-11-18 16:51:08 -080076 }
77
78 @Activate
79 protected void activate() {
80 appId = coreService.registerApplication(APP_NAME);
81 providerService = providerRegistry.register(this);
82 networkConfigRegistry.addListener(networkConfigListener);
83 readInitialConfig();
84 log.info("Started");
85 }
86
87 @Deactivate
88 protected void deactivate() {
89 networkConfigRegistry.removeListener(networkConfigListener);
90 providerRegistry.unregister(this);
91 providerService = null;
92 log.info("Stopped");
93 }
94
95 @Override
96 public void triggerProbe(Host host) {
97 /*
Charles Chanb3007e12016-05-20 10:55:40 -070098 * Note: All hosts are configured in network config host provider.
Charles Chand6d581a2015-11-18 16:51:08 -080099 * Therefore no probe is required.
100 */
101 }
102
103 /**
104 * Adds host information.
105 * IP information will be appended if host exists.
106 *
107 * @param mac MAC address of the host
108 * @param vlan VLAN ID of the host
109 * @param hloc Location of the host
110 * @param ips Set of IP addresses of the host
111 */
112 protected void addHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
113 HostId hid = HostId.hostId(mac, vlan);
Charles Chan7e0e2e62016-04-15 16:30:41 -0700114 HostDescription desc = (ips != null) ?
sdn5e935452016-08-30 04:12:54 -0700115 new DefaultHostDescription(mac, vlan, hloc, ips, true) :
116 new DefaultHostDescription(mac, vlan, hloc, true);
Charles Chand6d581a2015-11-18 16:51:08 -0800117 providerService.hostDetected(hid, desc, false);
118 }
119
120 /**
121 * Updates host information.
122 * IP information will be replaced if host exists.
123 *
124 * @param mac MAC address of the host
125 * @param vlan VLAN ID of the host
126 * @param hloc Location of the host
127 * @param ips Set of IP addresses of the host
128 */
129 protected void updateHost(MacAddress mac, VlanId vlan, HostLocation hloc, Set<IpAddress> ips) {
130 HostId hid = HostId.hostId(mac, vlan);
sdn5e935452016-08-30 04:12:54 -0700131 HostDescription desc = new DefaultHostDescription(mac, vlan, hloc, ips, true);
Charles Chand6d581a2015-11-18 16:51:08 -0800132 providerService.hostDetected(hid, desc, true);
133 }
134
135 /**
136 * Removes host information.
137 *
138 * @param mac MAC address of the host
139 * @param vlan VLAN ID of the host
140 */
141 protected void removeHost(MacAddress mac, VlanId vlan) {
142 HostId hid = HostId.hostId(mac, vlan);
143 providerService.hostVanished(hid);
144 }
145
146 private void readInitialConfig() {
147 networkConfigRegistry.getSubjects(HostId.class).forEach(hostId -> {
148 MacAddress mac = hostId.mac();
149 VlanId vlan = hostId.vlanId();
150 BasicHostConfig hostConfig =
151 networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
152 Set<IpAddress> ipAddresses = hostConfig.ipAddresses();
153 ConnectPoint location = hostConfig.location();
154 HostLocation hloc = new HostLocation(location, System.currentTimeMillis());
155 addHost(mac, vlan, hloc, ipAddresses);
156 });
157 }
158
159 private class InternalNetworkConfigListener implements NetworkConfigListener {
160 @Override
161 public void event(NetworkConfigEvent event) {
162 // Do not process non-host, register and unregister events
163 if (!event.configClass().equals(BasicHostConfig.class) ||
164 event.type() == NetworkConfigEvent.Type.CONFIG_REGISTERED ||
165 event.type() == NetworkConfigEvent.Type.CONFIG_UNREGISTERED) {
166 return;
167 }
168
169 HostId hostId = (HostId) event.subject();
170 MacAddress mac = hostId.mac();
171 VlanId vlan = hostId.vlanId();
172 BasicHostConfig hostConfig =
173 networkConfigRegistry.getConfig(hostId, BasicHostConfig.class);
174 Set<IpAddress> ipAddresses = null;
175 HostLocation hloc = null;
176
177 // Note: There will be no config presented in the CONFIG_REMOVE case
178 if (hostConfig != null) {
179 ipAddresses = hostConfig.ipAddresses();
180 ConnectPoint location = hostConfig.location();
181 hloc = new HostLocation(location, System.currentTimeMillis());
182 }
183
184 switch (event.type()) {
185 case CONFIG_ADDED:
186 addHost(mac, vlan, hloc, ipAddresses);
187 break;
188 case CONFIG_UPDATED:
189 updateHost(mac, vlan, hloc, ipAddresses);
190 break;
191 case CONFIG_REMOVED:
192 removeHost(mac, vlan);
193 break;
194 default:
195 break;
196 }
197 }
198 }
199}