Remove netconf from commons/opendaylight pom
[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.config.util.capability.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 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * NetconfOperationService aggregator. Makes a collection of operation services accessible as one.
32  */
33 public class AggregatedNetconfOperationServiceFactory implements NetconfOperationServiceFactory, NetconfOperationServiceFactoryListener, AutoCloseable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(AggregatedNetconfOperationServiceFactory.class);
36
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();
40
41     @Override
42     public synchronized void onAddNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
43         factories.add(service);
44
45         for (final CapabilityListener listener : listeners) {
46             AutoCloseable reg = service.registerCapabilityListener(listener);
47             registrations.put(service, reg);
48         }
49     }
50
51     @Override
52     public synchronized void onRemoveNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
53         factories.remove(service);
54
55         for (final AutoCloseable autoCloseable : registrations.get(service)) {
56             try {
57                 autoCloseable.close();
58             } catch (Exception e) {
59                 LOG.warn("Unable to close listener registration", e);
60             }
61         }
62
63         registrations.removeAll(service);
64     }
65
66     @Override
67     public synchronized Set<Capability> getCapabilities() {
68         final HashSet<Capability> capabilities = Sets.newHashSet();
69         for (final NetconfOperationServiceFactory factory : factories) {
70             capabilities.addAll(factory.getCapabilities());
71         }
72         return capabilities;
73     }
74
75     @Override
76     public synchronized AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
77         final Map<NetconfOperationServiceFactory, AutoCloseable> regs = Maps.newHashMap();
78
79         for (final NetconfOperationServiceFactory factory : factories) {
80             final AutoCloseable reg = factory.registerCapabilityListener(listener);
81             regs.put(factory, reg);
82         }
83         listeners.add(listener);
84
85         return new AutoCloseable() {
86             @Override
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());
93                     }
94                 }
95             }
96         };
97     }
98
99     @Override
100     public synchronized NetconfOperationService createService(final String netconfSessionIdForReporting) {
101         return new AggregatedNetconfOperation(factories, netconfSessionIdForReporting);
102     }
103
104     @Override
105     public synchronized void close() throws Exception {
106         factories.clear();
107         for (AutoCloseable reg : registrations.values()) {
108             reg.close();
109         }
110         registrations.clear();
111         listeners.clear();
112     }
113
114     private static final class AggregatedNetconfOperation implements NetconfOperationService {
115
116         private final Set<NetconfOperationService> services;
117
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));
122             }
123             this.services = b.build();
124         }
125
126         @Override
127         public Set<NetconfOperation> getNetconfOperations() {
128             final HashSet<NetconfOperation> operations = Sets.newHashSet();
129             for (final NetconfOperationService service : services) {
130                 operations.addAll(service.getNetconfOperations());
131             }
132             return operations;
133         }
134
135         @Override
136         public void close() {
137             try {
138                 CloseableUtil.closeAll(services);
139             } catch (final Exception e) {
140                 throw new IllegalStateException("Unable to properly close all aggregated services", e);
141             }
142         }
143     }
144 }