currentContext.set(null);
}
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
rootSchemaSourceProvider), Collections.emptySet());
listeners1.add(listener);
import org.opendaylight.netconf.api.monitoring.CapabilityListener;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
+import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.ModuleLike;
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
// Advertise validate capability only if DOMDataBroker provides DOMDataTransactionValidator
if (dataBroker.getExtensions().get(DOMDataTransactionValidator.class) != null) {
listener.onCapabilitiesChanged(Collections.singleton(VALIDATE_CAPABILITY), Collections.emptySet());
import org.opendaylight.netconf.mapping.api.NetconfOperationService;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
+import org.opendaylight.yangtools.concepts.Registration;
public final class MdsalMonitoringMapperFactory implements NetconfOperationServiceFactory, AutoCloseable {
private final MonitoringToMdsalWriter monitoringToMdsalWriter;
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
return () -> { };
}
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
+import org.opendaylight.yangtools.concepts.Registration;
public final class NetconfNotificationOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
private final NetconfNotificationRegistry netconfNotificationRegistry;
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
return () -> { };
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.mapping.api;
import java.util.Set;
* Service of netconf operations.
*/
public interface NetconfOperationService extends AutoCloseable {
-
/**
* Get set of netconf operations that are handled by this service.
*/
*/
@Override
void close();
-
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.mapping.api;
import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.netconf.api.capability.Capability;
import org.opendaylight.netconf.api.monitoring.CapabilityListener;
+import org.opendaylight.yangtools.concepts.Registration;
/**
* Factory that must be registered in OSGi service registry in order to be used
* {@link NetconfOperationService}.
*/
public interface NetconfOperationServiceFactory {
-
/**
* Get capabilities supported by current operation service.
*/
- Set<Capability> getCapabilities();
+ @NonNull Set<Capability> getCapabilities();
/**
* Supported capabilities may change over time, registering a listener allows for push based information
* retrieval about current notifications.
*/
- AutoCloseable registerCapabilityListener(CapabilityListener listener);
-
- NetconfOperationService createService(String netconfSessionIdForReporting);
+ @NonNull Registration registerCapabilityListener(CapabilityListener listener);
+ @NonNull NetconfOperationService createService(String netconfSessionIdForReporting);
}
*/
package org.opendaylight.netconf.test.tool;
-import com.google.common.collect.Sets;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.EnumMap;
import java.util.HashMap;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
listener.onCapabilitiesChanged(caps, Set.of());
- return () -> {
- };
+ return () -> { };
}
@Override
final Unlock unLock = new Unlock(currentSessionId);
final DiscardChanges discardChanges = new DiscardChanges(currentSessionId, transactionProvider);
- return Sets.newHashSet(get, getConfig, editConfig, commit, lock, unLock, discardChanges);
+ return Set.of(get, getConfig, editConfig, commit, lock, unLock, discardChanges);
}
@Override
import com.google.common.collect.Sets;
import java.io.File;
-import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import org.opendaylight.netconf.api.capability.Capability;
import org.opendaylight.netconf.test.tool.rpc.SimulatedGetConfig;
import org.opendaylight.netconf.test.tool.rpc.SimulatedLock;
import org.opendaylight.netconf.test.tool.rpc.SimulatedUnLock;
+import org.opendaylight.yangtools.concepts.Registration;
class SimulatedOperationProvider implements NetconfOperationServiceFactory {
private final Set<Capability> caps;
}
@Override
- public AutoCloseable registerCapabilityListener(
- final CapabilityListener listener) {
- listener.onCapabilitiesChanged(caps, Collections.emptySet());
- return () -> {
- };
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
+ listener.onCapabilitiesChanged(caps, Set.of());
+ return () -> { };
}
@Override
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.test.tool.customrpc;
import java.io.File;
-import java.util.Collections;
import java.util.Set;
import org.opendaylight.netconf.api.capability.Capability;
import org.opendaylight.netconf.api.monitoring.CapabilityListener;
import org.opendaylight.netconf.mapping.api.NetconfOperation;
import org.opendaylight.netconf.mapping.api.NetconfOperationService;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
public class SettableOperationProvider implements NetconfOperationServiceFactory {
-
private final File rpcConfig;
public SettableOperationProvider(final File rpcConfig) {
@Override
public Set<Capability> getCapabilities() {
- return Collections.emptySet();
+ return Set.of();
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
- return () -> {
- //no op
- };
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
+ return () -> { };
}
@Override
}
private static class SettableOperationService implements NetconfOperationService {
-
private final SettableRpc rpc;
SettableOperationService(final File rpcConfig) {
- this.rpc = new SettableRpc(rpcConfig);
+ rpc = new SettableRpc(rpcConfig);
}
@Override
public Set<NetconfOperation> getNetconfOperations() {
- return Collections.singleton(rpc);
+ return Set.of(rpc);
}
@Override
this.netconfMonitor = netconfMonitor;
}
- private Element getPlaceholder(final Document innerResult)
- throws DocumentedException {
- final XmlElement rootElement = XmlElement.fromDomElementWithExpected(
- innerResult.getDocumentElement(), XmlNetconfConstants.RPC_REPLY_KEY,
- XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
- return rootElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY).getDomElement();
- }
-
@Override
protected String getOperationName() {
return XmlNetconfConstants.GET;
final NetconfOperationChainedExecution subsequentOperation) {
throw new UnsupportedOperationException("Never gets called");
}
+
+ private static Element getPlaceholder(final Document innerResult) throws DocumentedException {
+ return XmlElement.fromDomElementWithExpected(innerResult.getDocumentElement(),
+ XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)
+ .getOnlyChildElement(XmlNetconfConstants.DATA_KEY)
+ .getDomElement();
+ }
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.test.tool.monitoring;
-import com.google.common.collect.Sets;
import java.util.Set;
import org.opendaylight.controller.config.yang.netconf.mdsal.monitoring.GetSchema;
import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
import org.opendaylight.netconf.mapping.api.NetconfOperationService;
public class NetconfMonitoringOperationService implements NetconfOperationService {
-
- private static final String TESTTOOL_SESSION = "testtool-session";
-
private final NetconfMonitoringService monitor;
public NetconfMonitoringOperationService(final NetconfMonitoringService monitor) {
@Override
public Set<NetconfOperation> getNetconfOperations() {
- return Sets.newHashSet(new Get(monitor), new GetSchema(TESTTOOL_SESSION, monitor));
+ return Set.of(new Get(monitor), new GetSchema("testtool-session", monitor));
}
@Override
public void close() {
+ // No-op
}
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.test.tool.monitoring;
-import java.util.Collections;
+import static java.util.Objects.requireNonNull;
+
import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.netconf.api.capability.Capability;
import org.opendaylight.netconf.api.monitoring.CapabilityListener;
import org.opendaylight.netconf.mapping.api.NetconfOperationService;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
-public class NetconfMonitoringOperationServiceFactory implements NetconfOperationServiceFactory,
- AutoCloseable {
-
- private final NetconfMonitoringOperationService operationService;
-
- private static final AutoCloseable AUTO_CLOSEABLE = () -> {
- // NOOP
- };
+public class NetconfMonitoringOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
+ private final @NonNull NetconfMonitoringOperationService operationService;
public NetconfMonitoringOperationServiceFactory(final NetconfMonitoringOperationService operationService) {
- this.operationService = operationService;
+ this.operationService = requireNonNull(operationService);
}
@Override
@Override
public Set<Capability> getCapabilities() {
- return Collections.emptySet();
+ return Set.of();
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
- return AUTO_CLOSEABLE;
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
+ return () -> { };
}
@Override
public void close() {
+ // No-op
}
}
import org.opendaylight.netconf.impl.SessionIdProvider;
import org.opendaylight.netconf.mapping.api.NetconfOperationService;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
public class OperationsProvider implements NetconfOperationServiceFactory {
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
listener.onCapabilitiesChanged(caps, Set.of());
return () -> { };
}
package org.opendaylight.netconf.test.tool.rpchandler;
-import java.util.Collections;
import java.util.Set;
import org.opendaylight.netconf.api.capability.Capability;
import org.opendaylight.netconf.api.monitoring.CapabilityListener;
import org.opendaylight.netconf.mapping.api.NetconfOperation;
import org.opendaylight.netconf.mapping.api.NetconfOperationService;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
public class SettableOperationRpcProvider implements NetconfOperationServiceFactory {
private final RpcHandler rpcHandler;
- public SettableOperationRpcProvider(RpcHandler rpcHandler) {
+ public SettableOperationRpcProvider(final RpcHandler rpcHandler) {
this.rpcHandler = rpcHandler;
}
@Override
public Set<Capability> getCapabilities() {
- return Collections.emptySet();
+ return Set.of();
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
- return () -> {
- //no op
- };
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
+ return () -> { };
}
@Override
}
private static class SettableOperationService implements NetconfOperationService {
-
private final SettableRpc rpc;
- SettableOperationService(RpcHandler rpcHandler) {
- this.rpc = new SettableRpc(rpcHandler);
+ SettableOperationService(final RpcHandler rpcHandler) {
+ rpc = new SettableRpc(rpcHandler);
}
@Override
public Set<NetconfOperation> getNetconfOperations() {
- return Collections.singleton(rpc);
+ return Set.of(rpc);
}
@Override
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.ImmutableSet.Builder;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import java.util.HashMap;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
import org.opendaylight.netconf.util.CloseableUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.opendaylight.yangtools.concepts.AbstractRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
/**
* NetconfOperationService aggregator. Makes a collection of operation services accessible as one.
*/
public final class AggregatedNetconfOperationServiceFactory
implements NetconfOperationServiceFactory, NetconfOperationServiceFactoryListener, AutoCloseable {
-
- private static final Logger LOG = LoggerFactory.getLogger(AggregatedNetconfOperationServiceFactory.class);
-
private final Set<NetconfOperationServiceFactory> factories = ConcurrentHashMap.newKeySet();
- private final Multimap<NetconfOperationServiceFactory, AutoCloseable> registrations =
+ private final Multimap<NetconfOperationServiceFactory, Registration> registrations =
Multimaps.synchronizedMultimap(HashMultimap.create());
private final Set<CapabilityListener> listeners = ConcurrentHashMap.newKeySet();
factories.add(service);
for (final CapabilityListener listener : listeners) {
- AutoCloseable reg = service.registerCapabilityListener(listener);
- registrations.put(service, reg);
+ registrations.put(service, service.registerCapabilityListener(listener));
}
}
- @SuppressWarnings("checkstyle:IllegalCatch")
@Override
public synchronized void onRemoveNetconfOperationServiceFactory(final NetconfOperationServiceFactory service) {
factories.remove(service);
-
- for (final AutoCloseable autoCloseable : registrations.get(service)) {
- try {
- autoCloseable.close();
- } catch (Exception e) {
- LOG.warn("Unable to close listener registration", e);
- }
- }
-
- registrations.removeAll(service);
+ registrations.removeAll(service).forEach(Registration::close);
}
@Override
}
@Override
- public synchronized AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
- final Map<NetconfOperationServiceFactory, AutoCloseable> regs = new HashMap<>();
+ public synchronized Registration registerCapabilityListener(final CapabilityListener listener) {
+ final Map<NetconfOperationServiceFactory, Registration> regs = new HashMap<>();
for (final NetconfOperationServiceFactory factory : factories) {
- final AutoCloseable reg = factory.registerCapabilityListener(listener);
- regs.put(factory, reg);
+ regs.put(factory, factory.registerCapabilityListener(listener));
}
listeners.add(listener);
- return () -> {
- synchronized (AggregatedNetconfOperationServiceFactory.this) {
- listeners.remove(listener);
- CloseableUtil.closeAll(regs.values());
- for (final Map.Entry<NetconfOperationServiceFactory, AutoCloseable> reg : regs.entrySet()) {
- registrations.remove(reg.getKey(), reg.getValue());
+ return new AbstractRegistration() {
+
+ @Override
+ protected void removeRegistration() {
+ synchronized (AggregatedNetconfOperationServiceFactory.this) {
+ listeners.remove(listener);
+ regs.values().forEach(Registration::close);
+ for (var reg : regs.entrySet()) {
+ registrations.remove(reg.getKey(), reg.getValue());
+ }
}
}
};
}
@Override
- public synchronized void close() throws Exception {
+ public synchronized void close() {
factories.clear();
- for (AutoCloseable reg : registrations.values()) {
- reg.close();
- }
+ registrations.values().forEach(Registration::close);
registrations.clear();
listeners.clear();
}
private static final class AggregatedNetconfOperation implements NetconfOperationService {
-
- private final Set<NetconfOperationService> services;
+ private final ImmutableSet<NetconfOperationService> services;
AggregatedNetconfOperation(final Set<NetconfOperationServiceFactory> factories,
final String netconfSessionIdForReporting) {
- final Builder<NetconfOperationService> b = ImmutableSet.builder();
- for (final NetconfOperationServiceFactory factory : factories) {
- b.add(factory.createService(netconfSessionIdForReporting));
- }
- services = b.build();
+ services = factories.stream()
+ .map(factory -> factory.createService(netconfSessionIdForReporting))
+ .collect(ImmutableSet.toImmutableSet());
}
@Override
public Set<NetconfOperation> getNetconfOperations() {
- final Set<NetconfOperation> operations = new HashSet<>();
- for (final NetconfOperationService service : services) {
- operations.addAll(service.getNetconfOperations());
- }
- return operations;
+ return services.stream()
+ .flatMap(service -> service.getNetconfOperations().stream())
+ .collect(ImmutableSet.toImmutableSet());
}
@SuppressWarnings("checkstyle:IllegalCatch")
import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
import org.opendaylight.netconf.util.test.XmlFileLoader;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.CapabilitiesBuilder;
+import org.opendaylight.yangtools.concepts.Registration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
}
@Override
- public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
- return () -> {
- };
+ public Registration registerCapabilityListener(final CapabilityListener listener) {
+ // No-op
+ return () -> { };
}
@Override
final ChannelPromise sendFuture = mock(ChannelPromise.class);
doAnswer(invocation -> {
- invocation.<GenericFutureListener>getArgument(0).operationComplete(sendFuture);
+ invocation.getArgument(0, GenericFutureListener.class).operationComplete(sendFuture);
return null;
}).when(sendFuture).addListener(any(GenericFutureListener.class));
doReturn(sendFuture).when(channel).newPromise();
import org.opendaylight.netconf.api.capability.Capability;
import org.opendaylight.netconf.api.monitoring.CapabilityListener;
import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.opendaylight.yangtools.concepts.Registration;
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class AggregatedNetconfOperationServiceFactoryTest {
@Mock
private NetconfOperationServiceFactory factory2;
@Mock
- private AutoCloseable autoCloseable1;
+ private Registration reg1;
@Mock
- private AutoCloseable autoCloseable2;
+ private Registration reg2;
@Mock
- private AutoCloseable autoCloseable3;
+ private Registration reg3;
private AggregatedNetconfOperationServiceFactory aggregatedFactory;
aggregatedFactory.registerCapabilityListener(listener1);
aggregatedFactory.registerCapabilityListener(listener2);
- doReturn(autoCloseable1).when(factory1).registerCapabilityListener(listener1);
- doReturn(autoCloseable2).when(factory1).registerCapabilityListener(listener2);
+ doReturn(reg1).when(factory1).registerCapabilityListener(listener1);
+ doReturn(reg2).when(factory1).registerCapabilityListener(listener2);
doReturn(factory1Caps).when(factory1).getCapabilities();
- doReturn(autoCloseable1).when(factory2).registerCapabilityListener(listener1);
- doReturn(autoCloseable2).when(factory2).registerCapabilityListener(listener2);
+ doReturn(reg1).when(factory2).registerCapabilityListener(listener1);
+ doReturn(reg2).when(factory2).registerCapabilityListener(listener2);
doReturn(factory2Caps).when(factory2).getCapabilities();
- doNothing().when(autoCloseable1).close();
- doNothing().when(autoCloseable2).close();
+ doNothing().when(reg1).close();
+ doNothing().when(reg2).close();
- doReturn(autoCloseable3).when(factory1).registerCapabilityListener(listener3);
- doReturn(autoCloseable3).when(factory2).registerCapabilityListener(listener3);
+ doReturn(reg3).when(factory1).registerCapabilityListener(listener3);
+ doReturn(reg3).when(factory2).registerCapabilityListener(listener3);
}
@Test
aggregatedFactory.onRemoveNetconfOperationServiceFactory(factory1);
aggregatedFactory.onRemoveNetconfOperationServiceFactory(factory2);
- verify(autoCloseable1, times(2)).close();
- verify(autoCloseable2, times(2)).close();
+ verify(reg1, times(2)).close();
+ verify(reg2, times(2)).close();
}
@Test
aggregatedFactory.onAddNetconfOperationServiceFactory(factory1);
aggregatedFactory.onAddNetconfOperationServiceFactory(factory2);
aggregatedFactory.close();
- verify(autoCloseable1, times(2)).close();
- verify(autoCloseable2, times(2)).close();
+ verify(reg1, times(2)).close();
+ verify(reg2, times(2)).close();
}
}