2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.impl.osgi;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Sets;
15 import java.util.Collection;
16 import java.util.List;
18 import java.util.concurrent.ScheduledExecutorService;
19 import java.util.concurrent.TimeUnit;
20 import java.util.stream.Collectors;
21 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
22 import org.opendaylight.netconf.api.monitoring.NetconfManagementSession;
23 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
24 import org.opendaylight.netconf.api.monitoring.SessionEvent;
25 import org.opendaylight.netconf.api.monitoring.SessionListener;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SessionsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * This class implements {@link SessionListener} to receive updates about Netconf sessions. Instance notifies its
35 * about session start and end. It also publishes on regular interval list of sessions,
36 * where events like rpc or notification happened.
38 class NetconfSessionMonitoringService implements SessionListener, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(NetconfSessionMonitoringService.class);
42 private final Set<NetconfManagementSession> sessions = Sets.newHashSet();
43 private final Set<NetconfManagementSession> changedSessions = Sets.newHashSet();
44 private final Set<NetconfMonitoringService.SessionsListener> listeners = Sets.newHashSet();
45 private final ScheduledExecutorService executor;
46 private final long updateInterval;
47 private boolean running;
50 * Constructor for {@code NetconfSessionMonitoringService}.
52 * @param schedulingThreadPool thread pool for scheduling session stats updates. If not present, updates won't be
54 * @param updateInterval update interval. If is less than 0, updates won't be scheduled
56 NetconfSessionMonitoringService(Optional<ScheduledThreadPool> schedulingThreadPool, long updateInterval) {
57 this.updateInterval = updateInterval;
58 if (schedulingThreadPool.isPresent() && updateInterval > 0) {
59 this.executor = schedulingThreadPool.get().getExecutor();
60 LOG.info("/netconf-state/sessions will be updated every {} seconds.", updateInterval);
62 LOG.info("Scheduling thread pool is present = {}, "
63 + "update interval {}: /netconf-state/sessions won't be updated.",
64 schedulingThreadPool.isPresent(), updateInterval);
69 synchronized Sessions getSessions() {
70 final Collection<Session> managementSessions = Collections2.transform(sessions,
71 NetconfManagementSession::toManagementSession);
72 return new SessionsBuilder().setSession(ImmutableList.copyOf(managementSessions)).build();
76 public synchronized void onSessionUp(final NetconfManagementSession session) {
77 LOG.debug("Session {} up", session);
78 Preconditions.checkState(!sessions.contains(session), "Session %s was already added", session);
79 sessions.add(session);
80 notifySessionUp(session);
84 public synchronized void onSessionDown(final NetconfManagementSession session) {
85 LOG.debug("Session {} down", session);
86 Preconditions.checkState(sessions.contains(session), "Session %s not present", session);
87 sessions.remove(session);
88 changedSessions.remove(session);
89 notifySessionDown(session);
93 public synchronized void onSessionEvent(SessionEvent event) {
94 changedSessions.add(event.getSession());
97 synchronized AutoCloseable registerListener(final NetconfMonitoringService.SessionsListener listener) {
98 listeners.add(listener);
100 startUpdateSessionStats();
102 return new AutoCloseable() {
104 public void close() throws Exception {
105 listeners.remove(listener);
111 public synchronized void close() throws Exception {
112 stopUpdateSessionStats();
117 private synchronized void updateSessionStats() {
118 if (changedSessions.isEmpty()) {
121 final List<Session> changed = changedSessions.stream()
122 .map(NetconfManagementSession::toManagementSession)
123 .collect(Collectors.toList());
124 final ImmutableList<Session> sessionImmutableList = ImmutableList.copyOf(changed);
125 for (NetconfMonitoringService.SessionsListener listener : listeners) {
126 listener.onSessionsUpdated(sessionImmutableList);
128 changedSessions.clear();
131 private void notifySessionUp(NetconfManagementSession managementSession) {
132 Session session = managementSession.toManagementSession();
133 for (NetconfMonitoringService.SessionsListener listener : listeners) {
134 listener.onSessionStarted(session);
138 private void notifySessionDown(NetconfManagementSession managementSession) {
139 Session session = managementSession.toManagementSession();
140 for (NetconfMonitoringService.SessionsListener listener : listeners) {
141 listener.onSessionEnded(session);
145 private void startUpdateSessionStats() {
146 if (executor != null) {
147 executor.scheduleAtFixedRate(this::updateSessionStats, 1, updateInterval, TimeUnit.SECONDS);
152 private void stopUpdateSessionStats() {
153 if (executor != null) {
154 executor.shutdownNow();