2 * Copyright (c) 2013 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
9 package org.opendaylight.netconf.impl.osgi;
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.ImmutableSet.Builder;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Multimap;
16 import com.google.common.collect.Sets;
17 import java.util.HashSet;
20 import org.opendaylight.controller.config.util.capability.Capability;
21 import org.opendaylight.netconf.mapping.api.NetconfOperation;
22 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
23 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
24 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
25 import org.opendaylight.netconf.util.CloseableUtil;
26 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * NetconfOperationService aggregator. Makes a collection of operation services accessible as one.
33 public class AggregatedNetconfOperationServiceFactory implements NetconfOperationServiceFactory, NetconfOperationServiceFactoryListener, AutoCloseable {
35 private static final Logger LOG = LoggerFactory.getLogger(AggregatedNetconfOperationServiceFactory.class);
37 private final Set<NetconfOperationServiceFactory> factories = new HashSet<>();
38 private final Multimap<NetconfOperationServiceFactory, AutoCloseable> registrations = HashMultimap.create();
39 private final Set<CapabilityListener> listeners = Sets.newHashSet();
42 public synchronized void onAddNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
43 factories.add(service);
45 for (final CapabilityListener listener : listeners) {
46 AutoCloseable reg = service.registerCapabilityListener(listener);
47 registrations.put(service, reg);
52 public synchronized void onRemoveNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
53 factories.remove(service);
55 for (final AutoCloseable autoCloseable : registrations.get(service)) {
57 autoCloseable.close();
58 } catch (Exception e) {
59 LOG.warn("Unable to close listener registration", e);
63 registrations.removeAll(service);
67 public synchronized Set<Capability> getCapabilities() {
68 final HashSet<Capability> capabilities = Sets.newHashSet();
69 for (final NetconfOperationServiceFactory factory : factories) {
70 capabilities.addAll(factory.getCapabilities());
76 public synchronized AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
77 final Map<NetconfOperationServiceFactory, AutoCloseable> regs = Maps.newHashMap();
79 for (final NetconfOperationServiceFactory factory : factories) {
80 final AutoCloseable reg = factory.registerCapabilityListener(listener);
81 regs.put(factory, reg);
83 listeners.add(listener);
85 return new AutoCloseable() {
87 public void close() throws Exception {
88 synchronized (AggregatedNetconfOperationServiceFactory.this) {
89 listeners.remove(listener);
90 CloseableUtil.closeAll(regs.values());
91 for (final Map.Entry<NetconfOperationServiceFactory, AutoCloseable> reg : regs.entrySet()) {
92 registrations.remove(reg.getKey(), reg.getValue());
100 public synchronized NetconfOperationService createService(final String netconfSessionIdForReporting) {
101 return new AggregatedNetconfOperation(factories, netconfSessionIdForReporting);
105 public synchronized void close() throws Exception {
107 for (AutoCloseable reg : registrations.values()) {
110 registrations.clear();
114 private static final class AggregatedNetconfOperation implements NetconfOperationService {
116 private final Set<NetconfOperationService> services;
118 public AggregatedNetconfOperation(final Set<NetconfOperationServiceFactory> factories, final String netconfSessionIdForReporting) {
119 final Builder<NetconfOperationService> b = ImmutableSet.builder();
120 for (final NetconfOperationServiceFactory factory : factories) {
121 b.add(factory.createService(netconfSessionIdForReporting));
123 this.services = b.build();
127 public Set<NetconfOperation> getNetconfOperations() {
128 final HashSet<NetconfOperation> operations = Sets.newHashSet();
129 for (final NetconfOperationService service : services) {
130 operations.addAll(service.getNetconfOperations());
136 public void close() {
138 CloseableUtil.closeAll(services);
139 } catch (final Exception e) {
140 throw new IllegalStateException("Unable to properly close all aggregated services", e);