Merge "DOMDataTree APIs"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / osgi / AggregatedNetconfOperationServiceFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.impl.osgi;
10
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;
18 import java.util.Map;
19 import java.util.Set;
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.util.CloseableUtil;
26
27 /**
28  * NetconfOperationService aggregator. Makes a collection of operation services accessible as one.
29  */
30 public class AggregatedNetconfOperationServiceFactory implements NetconfOperationServiceFactory, NetconfOperationServiceFactoryListener {
31
32     private final Set<NetconfOperationServiceFactory> factories = new HashSet<>();
33     private final Multimap<NetconfOperationServiceFactory, AutoCloseable> registrations = HashMultimap.create();
34     private final Set<CapabilityListener> listeners = Sets.newHashSet();
35
36     @Override
37     public synchronized void onAddNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
38         factories.add(service);
39
40         for (final CapabilityListener listener : listeners) {
41             AutoCloseable reg = service.registerCapabilityListener(listener);
42             registrations.put(service, reg);
43         }
44     }
45
46     @Override
47     public synchronized void onRemoveNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
48         factories.remove(service);
49
50         for (final AutoCloseable autoCloseable : registrations.get(service)) {
51             try {
52                 autoCloseable.close();
53             } catch (Exception e) {
54                 // FIXME Issue warning
55             }
56         }
57
58         registrations.removeAll(service);
59     }
60
61     @Override
62     public synchronized Set<Capability> getCapabilities() {
63         final HashSet<Capability> capabilities = Sets.newHashSet();
64         for (final NetconfOperationServiceFactory factory : factories) {
65             capabilities.addAll(factory.getCapabilities());
66         }
67         return capabilities;
68     }
69
70     @Override
71     public synchronized AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
72         final Map<NetconfOperationServiceFactory, AutoCloseable> regs = Maps.newHashMap();
73
74         for (final NetconfOperationServiceFactory factory : factories) {
75             final AutoCloseable reg = factory.registerCapabilityListener(listener);
76             regs.put(factory, reg);
77         }
78         listeners.add(listener);
79
80         return new AutoCloseable() {
81             @Override
82             public void close() throws Exception {
83                 synchronized (AggregatedNetconfOperationServiceFactory.this) {
84                     listeners.remove(listener);
85                     CloseableUtil.closeAll(regs.values());
86                     for (final Map.Entry<NetconfOperationServiceFactory, AutoCloseable> reg : regs.entrySet()) {
87                         registrations.remove(reg.getKey(), reg.getValue());
88                     }
89                 }
90             }
91         };
92     }
93
94     @Override
95     public synchronized NetconfOperationService createService(final String netconfSessionIdForReporting) {
96         return new AggregatedNetconfOperation(factories, netconfSessionIdForReporting);
97     }
98
99     private static final class AggregatedNetconfOperation implements NetconfOperationService {
100
101         private final Set<NetconfOperationService> services;
102
103         public AggregatedNetconfOperation(final Set<NetconfOperationServiceFactory> factories, final String netconfSessionIdForReporting) {
104             final Builder<NetconfOperationService> b = ImmutableSet.builder();
105             for (final NetconfOperationServiceFactory factory : factories) {
106                 b.add(factory.createService(netconfSessionIdForReporting));
107             }
108             this.services = b.build();
109         }
110
111         @Override
112         public Set<NetconfOperation> getNetconfOperations() {
113             final HashSet<NetconfOperation> operations = Sets.newHashSet();
114             for (final NetconfOperationService service : services) {
115                 operations.addAll(service.getNetconfOperations());
116             }
117             return operations;
118         }
119
120         @Override
121         public void close() {
122             try {
123                 CloseableUtil.closeAll(services);
124             } catch (final Exception e) {
125                 throw new IllegalStateException("Unable to properly close all aggregated services", e);
126             }
127         }
128     }
129 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.