blob: c4e9c5c8006905e9528850d106fed512c4053b1f [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;
Ayaka Koshibe2a85e842015-01-29 15:39:40 -080025import org.onosproject.cluster.ClusterService;
26import org.onosproject.mastership.MastershipService;
alshabib3a21b782015-01-11 20:41:26 -080027import org.onosproject.net.Device;
28import org.onosproject.net.Host;
29import org.onosproject.net.HostId;
30import org.onosproject.net.HostLocation;
alshabib839a5e62015-01-14 10:59:00 -080031import org.onosproject.net.MastershipRole;
alshabib3a21b782015-01-11 20:41:26 -080032import org.onosproject.net.PortNumber;
33import org.onosproject.net.device.DeviceEvent;
34import org.onosproject.net.device.DeviceListener;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.host.DefaultHostDescription;
37import org.onosproject.net.host.HostDescription;
38import org.onosproject.net.host.HostProvider;
39import org.onosproject.net.host.HostProviderRegistry;
40import org.onosproject.net.host.HostProviderService;
41import org.onosproject.net.provider.AbstractProvider;
42import org.onosproject.net.provider.ProviderId;
43import org.slf4j.Logger;
44
Ayaka Koshibe2a85e842015-01-29 15:39:40 -080045import static org.onlab.util.Tools.toHex;
Thomas Vachuska20084b72015-03-11 13:46:50 -070046import static org.slf4j.LoggerFactory.getLogger;
alshabib3a21b782015-01-11 20:41:26 -080047
48/**
49 * Null provider to advertise fake hosts.
50 */
51@Component(immediate = true)
52public 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 Koshibe2a85e842015-01-29 15:39:40 -080060 protected MastershipService roleService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected ClusterService nodeService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
alshabib3a21b782015-01-11 20:41:26 -080066 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
alshabib3a21b782015-01-11 20:41:26 -0800102 @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 Koshibe2a85e842015-01-29 15:39:40 -0800114 String nhash = toHex(nodeService.getLocalNode().hashCode());
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 }
alshabib3a21b782015-01-11 20:41:26 -0800122 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 Koshibe2a85e842015-01-29 15:39:40 -0800148 Device dev = event.subject();
149 if (!deviceService.getRole(event.subject().id()).equals(
150 MastershipRole.MASTER)) {
Ayaka Koshibe35c71e12015-01-27 17:10:04 -0800151 log.info("Local node is not master for device {}", event
152 .subject().id());
alshabib839a5e62015-01-14 10:59:00 -0800153 return;
154 }
alshabib3a21b782015-01-11 20:41:26 -0800155 switch (event.type()) {
156
157 case DEVICE_ADDED:
Ayaka Koshibe2a85e842015-01-29 15:39:40 -0800158 addHosts(dev);
alshabib3a21b782015-01-11 20:41:26 -0800159 break;
160 case DEVICE_UPDATED:
161 break;
162 case DEVICE_REMOVED:
Ayaka Koshibe2a85e842015-01-29 15:39:40 -0800163 removeHosts(dev);
alshabib3a21b782015-01-11 20:41:26 -0800164 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
alshabib3a21b782015-01-11 20:41:26 -0800180 }
Ayaka Koshibe2a85e842015-01-29 15:39:40 -0800181
alshabib3a21b782015-01-11 20:41:26 -0800182}