Merge "Startup arch - remove artifactId prefix from dir names."
[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.mapping.api.NetconfOperationServiceFactoryListener;
26 import org.opendaylight.controller.netconf.util.CloseableUtil;
27
28 /**
29  * NetconfOperationService aggregator. Makes a collection of operation services accessible as one.
30  */
31 public class AggregatedNetconfOperationServiceFactory implements NetconfOperationServiceFactory, NetconfOperationServiceFactoryListener, AutoCloseable {
32
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();
36
37     @Override
38     public synchronized void onAddNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
39         factories.add(service);
40
41         for (final CapabilityListener listener : listeners) {
42             AutoCloseable reg = service.registerCapabilityListener(listener);
43             registrations.put(service, reg);
44         }
45     }
46
47     @Override
48     public synchronized void onRemoveNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
49         factories.remove(service);
50
51         for (final AutoCloseable autoCloseable : registrations.get(service)) {
52             try {
53                 autoCloseable.close();
54             } catch (Exception e) {
55                 // FIXME Issue warning
56             }
57         }
58
59         registrations.removeAll(service);
60     }
61
62     @Override
63     public synchronized Set<Capability> getCapabilities() {
64         final HashSet<Capability> capabilities = Sets.newHashSet();
65         for (final NetconfOperationServiceFactory factory : factories) {
66             capabilities.addAll(factory.getCapabilities());
67         }
68         return capabilities;
69     }
70
71     @Override
72     public synchronized AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
73         final Map<NetconfOperationServiceFactory, AutoCloseable> regs = Maps.newHashMap();
74
75         for (final NetconfOperationServiceFactory factory : factories) {
76             final AutoCloseable reg = factory.registerCapabilityListener(listener);
77             regs.put(factory, reg);
78         }
79         listeners.add(listener);
80
81         return new AutoCloseable() {
82             @Override
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());
89                     }
90                 }
91             }
92         };
93     }
94
95     @Override
96     public synchronized NetconfOperationService createService(final String netconfSessionIdForReporting) {
97         return new AggregatedNetconfOperation(factories, netconfSessionIdForReporting);
98     }
99
100     @Override
101     public synchronized void close() throws Exception {
102         factories.clear();
103         for (AutoCloseable reg : registrations.values()) {
104             reg.close();
105         }
106         registrations.clear();
107         listeners.clear();
108     }
109
110     private static final class AggregatedNetconfOperation implements NetconfOperationService {
111
112         private final Set<NetconfOperationService> services;
113
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));
118             }
119             this.services = b.build();
120         }
121
122         @Override
123         public Set<NetconfOperation> getNetconfOperations() {
124             final HashSet<NetconfOperation> operations = Sets.newHashSet();
125             for (final NetconfOperationService service : services) {
126                 operations.addAll(service.getNetconfOperations());
127             }
128             return operations;
129         }
130
131         @Override
132         public void close() {
133             try {
134                 CloseableUtil.closeAll(services);
135             } catch (final Exception e) {
136                 throw new IllegalStateException("Unable to properly close all aggregated services", e);
137             }
138         }
139     }
140 }