alshabib
Committed by Gerrit Code Review

very simple null host provider

Change-Id: I020ccd525e3e96a933ea9f69865d8e03af151500
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;
25 +import org.onosproject.net.Device;
26 +import org.onosproject.net.Host;
27 +import org.onosproject.net.HostId;
28 +import org.onosproject.net.HostLocation;
29 +import org.onosproject.net.PortNumber;
30 +import org.onosproject.net.device.DeviceEvent;
31 +import org.onosproject.net.device.DeviceListener;
32 +import org.onosproject.net.device.DeviceService;
33 +import org.onosproject.net.host.DefaultHostDescription;
34 +import org.onosproject.net.host.HostDescription;
35 +import org.onosproject.net.host.HostProvider;
36 +import org.onosproject.net.host.HostProviderRegistry;
37 +import org.onosproject.net.host.HostProviderService;
38 +import org.onosproject.net.provider.AbstractProvider;
39 +import org.onosproject.net.provider.ProviderId;
40 +import org.slf4j.Logger;
41 +
42 +import static org.slf4j.LoggerFactory.getLogger;
43 +
44 +/**
45 + * Null provider to advertise fake hosts.
46 + */
47 +@Component(immediate = true)
48 +public class NullHostProvider extends AbstractProvider implements HostProvider {
49 +
50 + private final Logger log = getLogger(getClass());
51 +
52 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 + protected DeviceService deviceService;
54 +
55 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 + protected HostProviderRegistry providerRegistry;
57 +
58 + private HostProviderService providerService;
59 +
60 + //make sure the device has enough ports to accomodate all of them.
61 + private static final int HOSTSPERDEVICE = 5;
62 +
63 + private final InternalHostProvider hostProvider = new InternalHostProvider();
64 +
65 + /**
66 + * Creates an OpenFlow host provider.
67 + */
68 + public NullHostProvider() {
69 + super(new ProviderId("null", "org.onosproject.provider.nil"));
70 + }
71 +
72 + /**
73 + * Creates a provider with the supplier identifier.
74 + *
75 + * @param id provider id
76 + */
77 + protected NullHostProvider(ProviderId id) {
78 + super(id);
79 + }
80 +
81 + @Activate
82 + public void activate() {
83 + providerService = providerRegistry.register(this);
84 + for (Device dev : deviceService.getDevices()) {
85 + addHosts(dev);
86 + }
87 + deviceService.addListener(hostProvider);
88 +
89 + log.info("Started");
90 + }
91 +
92 +
93 +
94 + @Deactivate
95 + public void deactivate() {
96 + providerRegistry.unregister(this);
97 + deviceService.removeListener(hostProvider);
98 + providerService = null;
99 + log.info("Stopped");
100 + }
101 +
102 + @Override
103 + public void triggerProbe(Host host) {}
104 +
105 + private void addHosts(Device device) {
106 + for (int i = 0; i < HOSTSPERDEVICE; i++) {
107 + providerService.hostDetected(
108 + HostId.hostId(MacAddress.valueOf(i + device.hashCode()),
109 + VlanId.vlanId((short) -1)),
110 + buildHostDescription(device, i));
111 + }
112 + }
113 +
114 + private void removeHosts(Device device) {
115 + for (int i = 0; i < HOSTSPERDEVICE; i++) {
116 + providerService.hostVanished(
117 + HostId.hostId(MacAddress.valueOf(i + device.hashCode()),
118 + VlanId.vlanId((short) -1)));
119 + }
120 + }
121 +
122 + private HostDescription buildHostDescription(Device device, int port) {
123 + MacAddress mac = MacAddress.valueOf(device.hashCode() + port);
124 + HostLocation location = new HostLocation(device.id(),
125 + PortNumber.portNumber(port), 0L);
126 + return new DefaultHostDescription(mac, VlanId.vlanId((short) -1), location);
127 + }
128 +
129 + private class InternalHostProvider implements DeviceListener {
130 + @Override
131 + public void event(DeviceEvent event) {
132 + switch (event.type()) {
133 +
134 + case DEVICE_ADDED:
135 + addHosts(event.subject());
136 + break;
137 + case DEVICE_UPDATED:
138 + break;
139 + case DEVICE_REMOVED:
140 + removeHosts(event.subject());
141 + break;
142 + case DEVICE_SUSPENDED:
143 + break;
144 + case DEVICE_AVAILABILITY_CHANGED:
145 + break;
146 + case PORT_ADDED:
147 + break;
148 + case PORT_UPDATED:
149 + break;
150 + case PORT_REMOVED:
151 + break;
152 + default:
153 + break;
154 + }
155 + }
156 +
157 +
158 + }
159 +}