Merge "Bug 6731 - add valid example values"
[netconf.git] / netconf / netconf-impl / src / main / java / org / opendaylight / 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.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.Multimaps;
17 import com.google.common.collect.Sets;
18 import io.netty.util.internal.ConcurrentSet;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import org.opendaylight.controller.config.util.capability.Capability;
24 import org.opendaylight.netconf.mapping.api.NetconfOperation;
25 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
26 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
27 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
28 import org.opendaylight.netconf.util.CloseableUtil;
29 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * NetconfOperationService aggregator. Makes a collection of operation services accessible as one.
35  */
36 public class AggregatedNetconfOperationServiceFactory implements NetconfOperationServiceFactory, NetconfOperationServiceFactoryListener, AutoCloseable {
37
38     private static final Logger LOG = LoggerFactory.getLogger(AggregatedNetconfOperationServiceFactory.class);
39
40     private final Set<NetconfOperationServiceFactory> factories = new ConcurrentSet<>();
41     private final Multimap<NetconfOperationServiceFactory, AutoCloseable> registrations = Multimaps.synchronizedMultimap(HashMultimap.create());
42     private final Set<CapabilityListener> listeners = new ConcurrentSet<>();
43
44     public AggregatedNetconfOperationServiceFactory() {
45     }
46
47     public AggregatedNetconfOperationServiceFactory(final List<NetconfOperationServiceFactory> mappers) {
48         mappers.forEach(this::onAddNetconfOperationServiceFactory);
49     }
50
51     @Override
52     public synchronized void onAddNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
53         factories.add(service);
54
55         for (final CapabilityListener listener : listeners) {
56             AutoCloseable reg = service.registerCapabilityListener(listener);
57             registrations.put(service, reg);
58         }
59     }
60
61     @Override
62     public synchronized void onRemoveNetconfOperationServiceFactory(NetconfOperationServiceFactory service) {
63         factories.remove(service);
64
65         for (final AutoCloseable autoCloseable : registrations.get(service)) {
66             try {
67                 autoCloseable.close();
68             } catch (Exception e) {
69                 LOG.warn("Unable to close listener registration", e);
70             }
71         }
72
73         registrations.removeAll(service);
74     }
75
76     @Override
77     public Set<Capability> getCapabilities() {
78         final HashSet<Capability> capabilities = Sets.newHashSet();
79         for (final NetconfOperationServiceFactory factory : factories) {
80             capabilities.addAll(factory.getCapabilities());
81         }
82         return capabilities;
83     }
84
85     @Override
86     public synchronized AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
87         final Map<NetconfOperationServiceFactory, AutoCloseable> regs = Maps.newHashMap();
88
89         for (final NetconfOperationServiceFactory factory : factories) {
90             final AutoCloseable reg = factory.registerCapabilityListener(listener);
91             regs.put(factory, reg);
92         }
93         listeners.add(listener);
94
95         return new AutoCloseable() {
96             @Override
97             public void close() throws Exception {
98                 synchronized (AggregatedNetconfOperationServiceFactory.this) {
99                     listeners.remove(listener);
100                     CloseableUtil.closeAll(regs.values());
101                     for (final Map.Entry<NetconfOperationServiceFactory, AutoCloseable> reg : regs.entrySet()) {
102                         registrations.remove(reg.getKey(), reg.getValue());
103                     }
104                 }
105             }
106         };
107     }
108
109     @Override
110     public synchronized NetconfOperationService createService(final String netconfSessionIdForReporting) {
111         return new AggregatedNetconfOperation(factories, netconfSessionIdForReporting);
112     }
113
114     @Override
115     public synchronized void close() throws Exception {
116         factories.clear();
117         for (AutoCloseable reg : registrations.values()) {
118             reg.close();
119         }
120         registrations.clear();
121         listeners.clear();
122     }
123
124     private static final class AggregatedNetconfOperation implements NetconfOperationService {
125
126         private final Set<NetconfOperationService> services;
127
128         public AggregatedNetconfOperation(final Set<NetconfOperationServiceFactory> factories, final String netconfSessionIdForReporting) {
129             final Builder<NetconfOperationService> b = ImmutableSet.builder();
130             for (final NetconfOperationServiceFactory factory : factories) {
131                 b.add(factory.createService(netconfSessionIdForReporting));
132             }
133             this.services = b.build();
134         }
135
136         @Override
137         public Set<NetconfOperation> getNetconfOperations() {
138             final HashSet<NetconfOperation> operations = Sets.newHashSet();
139             for (final NetconfOperationService service : services) {
140                 operations.addAll(service.getNetconfOperations());
141             }
142             return operations;
143         }
144
145         @Override
146         public void close() {
147             try {
148                 CloseableUtil.closeAll(services);
149             } catch (final Exception e) {
150                 throw new IllegalStateException("Unable to properly close all aggregated services", e);
151             }
152         }
153     }
154 }