blob: d5ee60ed2420e03b0a4265dbb7a0f958abe648db [file] [log] [blame]
alshabib3a21b782015-01-11 20:41:26 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.provider.nil.host.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.onlab.packet.MacAddress;
24import org.onlab.packet.VlanId;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070025import org.onosproject.cfg.ComponentConfigService;
Ayaka Koshibe2a85e842015-01-29 15:39:40 -080026import org.onosproject.cluster.ClusterService;
27import org.onosproject.mastership.MastershipService;
alshabib3a21b782015-01-11 20:41:26 -080028import org.onosproject.net.Device;
29import org.onosproject.net.Host;
30import org.onosproject.net.HostId;
31import org.onosproject.net.HostLocation;
alshabib839a5e62015-01-14 10:59:00 -080032import org.onosproject.net.MastershipRole;
alshabib3a21b782015-01-11 20:41:26 -080033import org.onosproject.net.PortNumber;
34import org.onosproject.net.device.DeviceEvent;
35import org.onosproject.net.device.DeviceListener;
36import org.onosproject.net.device.DeviceService;
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;
45
46import static org.slf4j.LoggerFactory.getLogger;
Ayaka Koshibe2a85e842015-01-29 15:39:40 -080047import static org.onlab.util.Tools.toHex;
alshabib3a21b782015-01-11 20:41:26 -080048
49/**
50 * Null provider to advertise fake hosts.
51 */
52@Component(immediate = true)
53public class NullHostProvider extends AbstractProvider implements HostProvider {
54
55 private final Logger log = getLogger(getClass());
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected DeviceService deviceService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ayaka Koshibe2a85e842015-01-29 15:39:40 -080061 protected MastershipService roleService;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ClusterService nodeService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib3a21b782015-01-11 20:41:26 -080067 protected HostProviderRegistry providerRegistry;
68
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
70 protected ComponentConfigService cfgService;
71
alshabib3a21b782015-01-11 20:41:26 -080072 private HostProviderService providerService;
73
74 //make sure the device has enough ports to accomodate all of them.
75 private static final int HOSTSPERDEVICE = 5;
76
77 private final InternalHostProvider hostProvider = new InternalHostProvider();
78
79 /**
80 * Creates an OpenFlow host provider.
81 */
82 public NullHostProvider() {
83 super(new ProviderId("null", "org.onosproject.provider.nil"));
84 }
85
86 /**
87 * Creates a provider with the supplier identifier.
88 *
89 * @param id provider id
90 */
91 protected NullHostProvider(ProviderId id) {
92 super(id);
93 }
94
95 @Activate
96 public void activate() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070097 cfgService.registerProperties(getClass());
alshabib3a21b782015-01-11 20:41:26 -080098 providerService = providerRegistry.register(this);
99 for (Device dev : deviceService.getDevices()) {
100 addHosts(dev);
101 }
102 deviceService.addListener(hostProvider);
103
104 log.info("Started");
105 }
106
alshabib3a21b782015-01-11 20:41:26 -0800107 @Deactivate
108 public void deactivate() {
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700109 cfgService.unregisterProperties(getClass(), false);
alshabib3a21b782015-01-11 20:41:26 -0800110 providerRegistry.unregister(this);
111 deviceService.removeListener(hostProvider);
112 providerService = null;
113 log.info("Stopped");
114 }
115
116 @Override
117 public void triggerProbe(Host host) {}
118
119 private void addHosts(Device device) {
Ayaka Koshibe2a85e842015-01-29 15:39:40 -0800120 String nhash = toHex(nodeService.getLocalNode().hashCode());
121 String dhash = device.id().toString();
122 // make sure this instance owns the device.
123 if (!nhash.substring(nhash.length() - 3)
124 .equals(dhash.substring(14, 17))) {
125 log.warn("Device {} is not mine. Can't add hosts.", device.id());
126 return;
127 }
alshabib3a21b782015-01-11 20:41:26 -0800128 for (int i = 0; i < HOSTSPERDEVICE; i++) {
129 providerService.hostDetected(
130 HostId.hostId(MacAddress.valueOf(i + device.hashCode()),
131 VlanId.vlanId((short) -1)),
132 buildHostDescription(device, i));
133 }
134 }
135
136 private void removeHosts(Device device) {
137 for (int i = 0; i < HOSTSPERDEVICE; i++) {
138 providerService.hostVanished(
139 HostId.hostId(MacAddress.valueOf(i + device.hashCode()),
140 VlanId.vlanId((short) -1)));
141 }
142 }
143
144 private HostDescription buildHostDescription(Device device, int port) {
145 MacAddress mac = MacAddress.valueOf(device.hashCode() + port);
146 HostLocation location = new HostLocation(device.id(),
147 PortNumber.portNumber(port), 0L);
148 return new DefaultHostDescription(mac, VlanId.vlanId((short) -1), location);
149 }
150
151 private class InternalHostProvider implements DeviceListener {
152 @Override
153 public void event(DeviceEvent event) {
Ayaka Koshibe2a85e842015-01-29 15:39:40 -0800154 Device dev = event.subject();
155 if (!deviceService.getRole(event.subject().id()).equals(
156 MastershipRole.MASTER)) {
Ayaka Koshibe35c71e12015-01-27 17:10:04 -0800157 log.info("Local node is not master for device {}", event
158 .subject().id());
alshabib839a5e62015-01-14 10:59:00 -0800159 return;
160 }
alshabib3a21b782015-01-11 20:41:26 -0800161 switch (event.type()) {
162
163 case DEVICE_ADDED:
Ayaka Koshibe2a85e842015-01-29 15:39:40 -0800164 addHosts(dev);
alshabib3a21b782015-01-11 20:41:26 -0800165 break;
166 case DEVICE_UPDATED:
167 break;
168 case DEVICE_REMOVED:
Ayaka Koshibe2a85e842015-01-29 15:39:40 -0800169 removeHosts(dev);
alshabib3a21b782015-01-11 20:41:26 -0800170 break;
171 case DEVICE_SUSPENDED:
172 break;
173 case DEVICE_AVAILABILITY_CHANGED:
174 break;
175 case PORT_ADDED:
176 break;
177 case PORT_UPDATED:
178 break;
179 case PORT_REMOVED:
180 break;
181 default:
182 break;
183 }
184 }
185
alshabib3a21b782015-01-11 20:41:26 -0800186 }
Ayaka Koshibe2a85e842015-01-29 15:39:40 -0800187
alshabib3a21b782015-01-11 20:41:26 -0800188}