blob: 037be79bafc9c4b1c76c479bb99f8cb3d5f840cd [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.xmpp.core.ctl;
18
19import org.onosproject.xmpp.core.XmppDevice;
20import org.onosproject.xmpp.core.XmppDeviceFactory;
21import org.onosproject.xmpp.core.XmppDeviceId;
22import org.onosproject.xmpp.core.XmppDeviceAgent;
23import org.onosproject.xmpp.core.XmppSession;
24import org.slf4j.Logger;
25import org.xmpp.packet.JID;
26
27import static org.slf4j.LoggerFactory.getLogger;
28
29/**
30 * Generates XMPP device objects.
31 */
32public final class DefaultXmppDeviceFactory implements XmppDeviceFactory {
33
34 private final Logger logger = getLogger(getClass());
35
Ray Milkey8a0f4dd2018-03-07 09:09:10 -080036 private XmppDeviceAgent agent;
37 private final Object agentLock = new Object();
Tomek Osińskie9ccf412018-01-13 19:44:11 +010038
39 public void init(XmppDeviceAgent manager) {
40 setAgent(manager);
41 }
42
43 /**
44 * Configures XMPP device manager only if it is not initialized.
45 *
46 * @param agent reference object of XMPP device manager
47 */
48 private void setAgent(XmppDeviceAgent agent) {
Ray Milkey8a0f4dd2018-03-07 09:09:10 -080049 synchronized (agentLock) {
Tomek Osińskie9ccf412018-01-13 19:44:11 +010050 if (this.agent == null) {
51 this.agent = agent;
52 } else {
53 logger.warn("XMPP device manager has already been set.");
54 }
55 }
56 }
57
58 public void cleanAgent() {
Ray Milkey8a0f4dd2018-03-07 09:09:10 -080059 synchronized (agentLock) {
Tomek Osińskie9ccf412018-01-13 19:44:11 +010060 if (this.agent != null) {
61 this.agent = null;
62 } else {
63 logger.warn("Manager for XMPP device is not configured");
64 }
65 }
66 }
67
68 public XmppDevice getXmppDevice(JID jid, XmppSession session) {
69 XmppDeviceId xmppDeviceId = new XmppDeviceId(jid);
70
71 return getXmppDeviceInstance(xmppDeviceId, session);
72 }
73
74 private XmppDevice getXmppDeviceInstance(XmppDeviceId xmppDeviceId, XmppSession session) {
75 XmppDevice device = agent.getDevice(xmppDeviceId);
76 if (device != null) {
77 return device;
78 } else {
79 XmppDevice newDevice = createXmppDeviceInstance(xmppDeviceId, session);
80 return newDevice;
81 }
82 }
83
84 private XmppDevice createXmppDeviceInstance(XmppDeviceId xmppDeviceId, XmppSession session) {
85 XmppDevice xmppDevice = new DefaultXmppDevice(xmppDeviceId, this.agent, session);
86 return xmppDevice;
87 }
88
89
90
91}