alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.provider.nil.host.impl; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.mastership.MastershipService; |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
| 30 | import org.onosproject.net.HostLocation; |
alshabib | 839a5e6 | 2015-01-14 10:59:00 -0800 | [diff] [blame] | 31 | import org.onosproject.net.MastershipRole; |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceEvent; |
| 34 | import org.onosproject.net.device.DeviceListener; |
| 35 | import org.onosproject.net.device.DeviceService; |
| 36 | import org.onosproject.net.host.DefaultHostDescription; |
| 37 | import org.onosproject.net.host.HostDescription; |
| 38 | import org.onosproject.net.host.HostProvider; |
| 39 | import org.onosproject.net.host.HostProviderRegistry; |
| 40 | import org.onosproject.net.host.HostProviderService; |
| 41 | import org.onosproject.net.provider.AbstractProvider; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.slf4j.Logger; |
| 44 | |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 45 | import static org.onlab.util.Tools.toHex; |
Thomas Vachuska | 20084b7 | 2015-03-11 13:46:50 -0700 | [diff] [blame] | 46 | import static org.slf4j.LoggerFactory.getLogger; |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Null provider to advertise fake hosts. |
| 50 | */ |
| 51 | @Component(immediate = true) |
| 52 | public class NullHostProvider extends AbstractProvider implements HostProvider { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected DeviceService deviceService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 60 | protected MastershipService roleService; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected ClusterService nodeService; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 66 | protected HostProviderRegistry providerRegistry; |
| 67 | |
| 68 | private HostProviderService providerService; |
| 69 | |
| 70 | //make sure the device has enough ports to accomodate all of them. |
| 71 | private static final int HOSTSPERDEVICE = 5; |
| 72 | |
| 73 | private final InternalHostProvider hostProvider = new InternalHostProvider(); |
| 74 | |
| 75 | /** |
| 76 | * Creates an OpenFlow host provider. |
| 77 | */ |
| 78 | public NullHostProvider() { |
| 79 | super(new ProviderId("null", "org.onosproject.provider.nil")); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Creates a provider with the supplier identifier. |
| 84 | * |
| 85 | * @param id provider id |
| 86 | */ |
| 87 | protected NullHostProvider(ProviderId id) { |
| 88 | super(id); |
| 89 | } |
| 90 | |
| 91 | @Activate |
| 92 | public void activate() { |
| 93 | providerService = providerRegistry.register(this); |
| 94 | for (Device dev : deviceService.getDevices()) { |
| 95 | addHosts(dev); |
| 96 | } |
| 97 | deviceService.addListener(hostProvider); |
| 98 | |
| 99 | log.info("Started"); |
| 100 | } |
| 101 | |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 102 | @Deactivate |
| 103 | public void deactivate() { |
| 104 | providerRegistry.unregister(this); |
| 105 | deviceService.removeListener(hostProvider); |
| 106 | providerService = null; |
| 107 | log.info("Stopped"); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void triggerProbe(Host host) {} |
| 112 | |
| 113 | private void addHosts(Device device) { |
Ayaka Koshibe | ef69be3 | 2015-03-15 22:29:19 -0700 | [diff] [blame] | 114 | String nhash = toHex(nodeService.getLocalNode().id().hashCode()); |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 115 | String dhash = device.id().toString(); |
| 116 | // make sure this instance owns the device. |
| 117 | if (!nhash.substring(nhash.length() - 3) |
| 118 | .equals(dhash.substring(14, 17))) { |
| 119 | log.warn("Device {} is not mine. Can't add hosts.", device.id()); |
| 120 | return; |
| 121 | } |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 122 | for (int i = 0; i < HOSTSPERDEVICE; i++) { |
| 123 | providerService.hostDetected( |
| 124 | HostId.hostId(MacAddress.valueOf(i + device.hashCode()), |
| 125 | VlanId.vlanId((short) -1)), |
| 126 | buildHostDescription(device, i)); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private void removeHosts(Device device) { |
| 131 | for (int i = 0; i < HOSTSPERDEVICE; i++) { |
| 132 | providerService.hostVanished( |
| 133 | HostId.hostId(MacAddress.valueOf(i + device.hashCode()), |
| 134 | VlanId.vlanId((short) -1))); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | private HostDescription buildHostDescription(Device device, int port) { |
| 139 | MacAddress mac = MacAddress.valueOf(device.hashCode() + port); |
| 140 | HostLocation location = new HostLocation(device.id(), |
| 141 | PortNumber.portNumber(port), 0L); |
| 142 | return new DefaultHostDescription(mac, VlanId.vlanId((short) -1), location); |
| 143 | } |
| 144 | |
| 145 | private class InternalHostProvider implements DeviceListener { |
| 146 | @Override |
| 147 | public void event(DeviceEvent event) { |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 148 | Device dev = event.subject(); |
| 149 | if (!deviceService.getRole(event.subject().id()).equals( |
| 150 | MastershipRole.MASTER)) { |
Ayaka Koshibe | 35c71e1 | 2015-01-27 17:10:04 -0800 | [diff] [blame] | 151 | log.info("Local node is not master for device {}", event |
| 152 | .subject().id()); |
alshabib | 839a5e6 | 2015-01-14 10:59:00 -0800 | [diff] [blame] | 153 | return; |
| 154 | } |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 155 | switch (event.type()) { |
| 156 | |
| 157 | case DEVICE_ADDED: |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 158 | addHosts(dev); |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 159 | break; |
| 160 | case DEVICE_UPDATED: |
| 161 | break; |
| 162 | case DEVICE_REMOVED: |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 163 | removeHosts(dev); |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 164 | break; |
| 165 | case DEVICE_SUSPENDED: |
| 166 | break; |
| 167 | case DEVICE_AVAILABILITY_CHANGED: |
| 168 | break; |
| 169 | case PORT_ADDED: |
| 170 | break; |
| 171 | case PORT_UPDATED: |
| 172 | break; |
| 173 | case PORT_REMOVED: |
| 174 | break; |
| 175 | default: |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 180 | } |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 181 | |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 182 | } |