blob: 069fc050f1704267828b48df951f907e71d3d906 [file] [log] [blame]
Tomek OsiƄskie9ccf412018-01-13 19:44:11 +01001/*
2 * Copyright 2018-present Open Networking Foundation
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
17package org.onosproject.provider.xmpp.device.impl;
18
19import com.google.common.base.Preconditions;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onlab.packet.ChassisId;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.AnnotationKeys;
29import org.onosproject.net.DefaultAnnotations;
30import org.onosproject.net.Device;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.MastershipRole;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.SparseAnnotations;
35import org.onosproject.net.device.DefaultDeviceDescription;
36import org.onosproject.net.device.DeviceDescription;
37import org.onosproject.net.device.DeviceProvider;
38import org.onosproject.net.device.DeviceProviderRegistry;
39import org.onosproject.net.device.DeviceProviderService;
40import org.onosproject.net.device.DeviceService;
41import org.onosproject.net.provider.AbstractProvider;
42import org.onosproject.net.provider.ProviderId;
43import org.onosproject.xmpp.core.XmppController;
44import org.onosproject.xmpp.core.XmppDeviceId;
45import org.onosproject.xmpp.core.XmppDeviceListener;
46import org.osgi.service.component.ComponentContext;
47import org.slf4j.Logger;
48import org.xmpp.packet.JID;
49
50import static org.slf4j.LoggerFactory.getLogger;
51
52/**
53 * Provider which will try to fetch the details of XMPP devices from the core and run a capability discovery on each of
54 * the device.
55 */
56@Component(immediate = true)
57public class XmppDeviceProvider extends AbstractProvider implements DeviceProvider {
58
59 private final Logger logger = getLogger(getClass());
60
61 private static final String PROVIDER = "org.onosproject.provider.xmpp.device";
62 private static final String APP_NAME = "org.onosproject.xmpp";
63 private static final String XMPP = "xmpp";
64 private static final String ADDRESS = "address";
65
66 private static final String HARDWARE_VERSION = "XMPP Device";
67 private static final String SOFTWARE_VERSION = "1.0";
68 private static final String SERIAL_NUMBER = "unknown";
69 private static final String IS_NULL_MSG = "XMPP device info is null";
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected DeviceProviderRegistry providerRegistry;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected DeviceService deviceService;
76
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected CoreService coreService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected XmppController controller;
82
83 protected DeviceProviderService providerService;
84
85 protected ApplicationId appId;
86
87 private XmppDeviceListener deviceListener = new InternalXmppDeviceListener();
88
89 public XmppDeviceProvider() {
90 super(new ProviderId(XMPP, PROVIDER));
91 }
92
93 @Activate
94 public void activate(ComponentContext context) {
95 providerService = providerRegistry.register(this);
96 appId = coreService.registerApplication(APP_NAME);
97 controller.addXmppDeviceListener(deviceListener);
98 logger.info("Started");
99 }
100
101 @Deactivate
102 public void deactivate() {
103 controller.removeXmppDeviceListener(deviceListener);
104 providerRegistry.unregister(this);
105 providerService = null;
106 }
107
108 @Override
109 public void triggerProbe(DeviceId deviceId) {
110
111 }
112
113 @Override
114 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
115
116 }
117
118 @Override
119 public boolean isReachable(DeviceId deviceId) {
120 String id = deviceId.uri().getSchemeSpecificPart();
121 JID jid = new JID(id);
122 XmppDeviceId xmppDeviceId = new XmppDeviceId(jid);
123 return controller.getDevice(xmppDeviceId) != null;
124 }
125
126 @Override
127 public void changePortState(DeviceId deviceId, PortNumber portNumber, boolean enable) {
128
129 }
130
131 private void connectDevice(XmppDeviceId xmppDeviceId) {
132 DeviceId deviceId = DeviceId.deviceId(xmppDeviceId.id());
133 String ipAddress = controller.getDevice(xmppDeviceId).getIpAddress().getAddress().getHostAddress();
134 // Assumption: manufacturer is uniquely identified by domain part of JID
135 String manufacturer = xmppDeviceId.getJid().getDomain();
136
137 ChassisId cid = new ChassisId();
138
139 SparseAnnotations annotations = DefaultAnnotations.builder()
140 .set(AnnotationKeys.PROTOCOL, XMPP.toUpperCase())
141 .set("IpAddress", ipAddress)
142 .build();
143 DeviceDescription deviceDescription = new DefaultDeviceDescription(
144 deviceId.uri(),
145 Device.Type.OTHER,
146 manufacturer, HARDWARE_VERSION,
147 SOFTWARE_VERSION, SERIAL_NUMBER,
148 cid, true,
149 annotations);
150
151 if (deviceService.getDevice(deviceId) == null) {
152 providerService.deviceConnected(deviceId, deviceDescription);
153 }
154 }
155
156 private void disconnectDevice(XmppDeviceId xmppDeviceId) {
157 Preconditions.checkNotNull(xmppDeviceId, IS_NULL_MSG);
158
159 DeviceId deviceId = DeviceId.deviceId(xmppDeviceId.id());
160 if (deviceService.getDevice(deviceId) != null) {
161 providerService.deviceDisconnected(deviceId);
162 logger.info("XMPP device {} removed from XMPP controller", deviceId);
163 } else {
164 logger.warn("XMPP device {} does not exist in the store, " +
165 "or it may already have been removed", deviceId);
166 }
167 }
168
169 private class InternalXmppDeviceListener implements XmppDeviceListener {
170
171 @Override
172 public void deviceConnected(XmppDeviceId deviceId) {
173 logger.info("NOTIFICATION: device {} connected", deviceId);
174 connectDevice(deviceId);
175 }
176
177 @Override
178 public void deviceDisconnected(XmppDeviceId deviceId) {
179 logger.info("NOTIFICATION: device {} disconnected", deviceId);
180 disconnectDevice(deviceId);
181 }
182 }
183
184
185}