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.controller.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.netconf.api.Capability;
21 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
22 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
23 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
24 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
25 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactoryListener;
26 import org.opendaylight.controller.netconf.util.CloseableUtil;
29 * NetconfOperationService aggregator. Makes a collection of operation services accessible as one.
31 public class AggregatedNetconfOperationServiceFactory implements NetconfOperationServiceFactory, NetconfOperationServiceFactoryListener, AutoCloseable {
33 private final Set<NetconfOperationServiceFactory> factories = new HashSet<>();
34 private final Multimap<NetconfOperationServiceFactory, AutoCloseable> registrations = HashMultimap.create();
35 private final Set<CapabilityListener> listeners = Sets.newHashSet();
38 public synchronized void onAddNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
39 factories.add(service);
41 for (final CapabilityListener listener : listeners) {
42 AutoCloseable reg = service.registerCapabilityListener(listener);
43 registrations.put(service, reg);
48 public synchronized void onRemoveNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
49 factories.remove(service);
51 for (final AutoCloseable autoCloseable : registrations.get(service)) {
53 autoCloseable.close();
54 } catch (Exception e) {
55 // FIXME Issue warning
59 registrations.removeAll(service);
63 public synchronized Set<Capability> getCapabilities() {
64 final HashSet<Capability> capabilities = Sets.newHashSet();
65 for (final NetconfOperationServiceFactory factory : factories) {
66 capabilities.addAll(factory.getCapabilities());
72 public synchronized AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
73 final Map<NetconfOperationServiceFactory, AutoCloseable> regs = Maps.newHashMap();
75 for (final NetconfOperationServiceFactory factory : factories) {
76 final AutoCloseable reg = factory.registerCapabilityListener(listener);
77 regs.put(factory, reg);
79 listeners.add(listener);
81 return new AutoCloseable() {
83 public void close() throws Exception {
84 synchronized (AggregatedNetconfOperationServiceFactory.this) {
85 listeners.remove(listener);
86 CloseableUtil.closeAll(regs.values());
87 for (final Map.Entry<NetconfOperationServiceFactory, AutoCloseable> reg : regs.entrySet()) {
88 registrations.remove(reg.getKey(), reg.getValue());
96 public synchronized NetconfOperationService createService(final String netconfSessionIdForReporting) {
97 return new AggregatedNetconfOperation(factories, netconfSessionIdForReporting);
101 public synchronized void close() throws Exception {
103 for (AutoCloseable reg : registrations.values()) {
106 registrations.clear();
110 private static final class AggregatedNetconfOperation implements NetconfOperationService {
112 private final Set<NetconfOperationService> services;
114 public AggregatedNetconfOperation(final Set<NetconfOperationServiceFactory> factories, final String netconfSessionIdForReporting) {
115 final Builder<NetconfOperationService> b = ImmutableSet.builder();
116 for (final NetconfOperationServiceFactory factory : factories) {
117 b.add(factory.createService(netconfSessionIdForReporting));
119 this.services = b.build();
123 public Set<NetconfOperation> getNetconfOperations() {
124 final HashSet<NetconfOperation> operations = Sets.newHashSet();
125 for (final NetconfOperationService service : services) {
126 operations.addAll(service.getNetconfOperations());
132 public void close() {
134 CloseableUtil.closeAll(services);
135 } catch (final Exception e) {
136 throw new IllegalStateException("Unable to properly close all aggregated services", e);