Bump upstreams
[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.mdsal.dom.api.DOMSchemaService.YangTextSourceExtension;
19 import org.opendaylight.netconf.server.api.monitoring.CapabilityListener;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22
23 // Non-final for mocking
24 @SuppressWarnings("checkstyle:FinalClass")
25 public class CurrentSchemaContext implements AutoCloseable {
26     private final AtomicReference<EffectiveModelContext> currentContext = new AtomicReference<>();
27     private final Set<CapabilityListener> listeners = Collections.synchronizedSet(new HashSet<>());
28     private final YangTextSourceExtension yangTextSourceExtension;
29
30     private Registration schemaContextListenerListenerRegistration;
31
32     private CurrentSchemaContext(final YangTextSourceExtension yangTextSourceExtension) {
33         this.yangTextSourceExtension = yangTextSourceExtension;
34     }
35
36     // keep spotbugs from complaining about overridable method in constructor
37     public static CurrentSchemaContext create(final DOMSchemaService schemaService,
38             final YangTextSourceExtension yangTextSourceExtension) {
39         var context = new CurrentSchemaContext(yangTextSourceExtension);
40         final var registration = schemaService.registerSchemaContextListener(context::onModelContextUpdated);
41         context.setRegistration(registration);
42         return context;
43     }
44
45     private void setRegistration(final Registration registration) {
46         schemaContextListenerListenerRegistration = registration;
47     }
48
49     public @NonNull EffectiveModelContext getCurrentContext() {
50         final var ret = currentContext.get();
51         checkState(ret != null, "Current context not received");
52         return ret;
53     }
54
55     private void onModelContextUpdated(final EffectiveModelContext schemaContext) {
56         currentContext.set(schemaContext);
57         // FIXME is notifying all the listeners from this callback wise ?
58         final var addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(schemaContext,
59             yangTextSourceExtension);
60         for (var listener : listeners) {
61             listener.onCapabilitiesChanged(addedCaps, Set.of());
62         }
63     }
64
65     @Override
66     public void close() {
67         listeners.clear();
68         schemaContextListenerListenerRegistration.close();
69         currentContext.set(null);
70     }
71
72     public Registration registerCapabilityListener(final CapabilityListener listener) {
73         listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(),
74             yangTextSourceExtension), Set.of());
75         listeners.add(listener);
76         return () -> listeners.remove(listener);
77     }
78 }