Return ImmutableSet from transformCapabilities()
[netconf.git] / plugins / netconf-server-mdsal / src / main / java / org / opendaylight / netconf / server / mdsal / CurrentSchemaContext.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.netconf.server.mdsal;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.Set;
15 import java.util.concurrent.atomic.AtomicReference;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
18 import org.opendaylight.netconf.server.api.monitoring.CapabilityListener;
19 import org.opendaylight.yangtools.concepts.Registration;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
22 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
24
25 // Non-final for mocking
26 @SuppressWarnings("checkstyle:FinalClass")
27 public class CurrentSchemaContext implements EffectiveModelContextListener, AutoCloseable {
28     private final AtomicReference<EffectiveModelContext> currentContext = new AtomicReference<>();
29     private final Set<CapabilityListener> listeners = Collections.synchronizedSet(new HashSet<>());
30     private final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider;
31
32     private Registration schemaContextListenerListenerRegistration;
33
34     private CurrentSchemaContext(final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
35         this.rootSchemaSourceProvider = rootSchemaSourceProvider;
36     }
37
38     // keep spotbugs from complaining about overridable method in constructor
39     public static CurrentSchemaContext create(final DOMSchemaService schemaService,
40                          final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
41         var context = new CurrentSchemaContext(rootSchemaSourceProvider);
42         final Registration registration = schemaService.registerSchemaContextListener(context);
43         context.setRegistration(registration);
44         return context;
45     }
46
47     private void setRegistration(final Registration registration) {
48         schemaContextListenerListenerRegistration = registration;
49     }
50
51     public @NonNull EffectiveModelContext getCurrentContext() {
52         final var ret = currentContext.get();
53         checkState(ret != null, "Current context not received");
54         return ret;
55     }
56
57     @Override
58     public void onModelContextUpdated(final EffectiveModelContext schemaContext) {
59         currentContext.set(schemaContext);
60         // FIXME is notifying all the listeners from this callback wise ?
61         final var addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(schemaContext,
62             rootSchemaSourceProvider);
63         for (var listener : listeners) {
64             listener.onCapabilitiesChanged(addedCaps, Set.of());
65         }
66     }
67
68     @Override
69     public void close() {
70         listeners.clear();
71         schemaContextListenerListenerRegistration.close();
72         currentContext.set(null);
73     }
74
75     public Registration registerCapabilityListener(final CapabilityListener listener) {
76         listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
77                 rootSchemaSourceProvider), Set.of());
78         listeners.add(listener);
79         return () -> listeners.remove(listener);
80     }
81 }