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; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame^] | 25 | import org.onosproject.cfg.ComponentConfigService; |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.mastership.MastershipService; |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.Host; |
| 30 | import org.onosproject.net.HostId; |
| 31 | import org.onosproject.net.HostLocation; |
alshabib | 839a5e6 | 2015-01-14 10:59:00 -0800 | [diff] [blame] | 32 | import org.onosproject.net.MastershipRole; |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.device.DeviceEvent; |
| 35 | import org.onosproject.net.device.DeviceListener; |
| 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.host.DefaultHostDescription; |
| 38 | import org.onosproject.net.host.HostDescription; |
| 39 | import org.onosproject.net.host.HostProvider; |
| 40 | import org.onosproject.net.host.HostProviderRegistry; |
| 41 | import org.onosproject.net.host.HostProviderService; |
| 42 | import org.onosproject.net.provider.AbstractProvider; |
| 43 | import org.onosproject.net.provider.ProviderId; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 47 | import static org.onlab.util.Tools.toHex; |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Null provider to advertise fake hosts. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | public 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 Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 61 | protected MastershipService roleService; |
| 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected ClusterService nodeService; |
| 65 | |
| 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 67 | protected HostProviderRegistry providerRegistry; |
| 68 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame^] | 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected ComponentConfigService cfgService; |
| 71 | |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 72 | 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 Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame^] | 97 | cfgService.registerProperties(getClass()); |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 98 | 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 | |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 107 | @Deactivate |
| 108 | public void deactivate() { |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame^] | 109 | cfgService.unregisterProperties(getClass(), false); |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 110 | 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 Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 120 | 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 | } |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 128 | 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 Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 154 | Device dev = event.subject(); |
| 155 | if (!deviceService.getRole(event.subject().id()).equals( |
| 156 | MastershipRole.MASTER)) { |
Ayaka Koshibe | 35c71e1 | 2015-01-27 17:10:04 -0800 | [diff] [blame] | 157 | log.info("Local node is not master for device {}", event |
| 158 | .subject().id()); |
alshabib | 839a5e6 | 2015-01-14 10:59:00 -0800 | [diff] [blame] | 159 | return; |
| 160 | } |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 161 | switch (event.type()) { |
| 162 | |
| 163 | case DEVICE_ADDED: |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 164 | addHosts(dev); |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 165 | break; |
| 166 | case DEVICE_UPDATED: |
| 167 | break; |
| 168 | case DEVICE_REMOVED: |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 169 | removeHosts(dev); |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 170 | 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 | |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 186 | } |
Ayaka Koshibe | 2a85e84 | 2015-01-29 15:39:40 -0800 | [diff] [blame] | 187 | |
alshabib | 3a21b78 | 2015-01-11 20:41:26 -0800 | [diff] [blame] | 188 | } |