Merge "Removing { } from NormalizedNodeJsonBodyWriter"
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / 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
9 package org.opendaylight.controller.netconf.mdsal.connector;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Sets;
13 import java.util.Collections;
14 import java.util.Set;
15 import java.util.concurrent.atomic.AtomicReference;
16 import org.opendaylight.controller.netconf.api.Capability;
17 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
18 import org.opendaylight.controller.sal.core.api.model.SchemaService;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
22
23 public class CurrentSchemaContext implements SchemaContextListener, AutoCloseable {
24     final AtomicReference<SchemaContext> currentContext = new AtomicReference<SchemaContext>();
25     private final ListenerRegistration<SchemaContextListener> schemaContextListenerListenerRegistration;
26     private final Set<CapabilityListener> listeners = Collections.synchronizedSet(Sets.<CapabilityListener>newHashSet());
27
28     public SchemaContext getCurrentContext() {
29         Preconditions.checkState(currentContext.get() != null, "Current context not received");
30         return currentContext.get();
31     }
32
33     public CurrentSchemaContext(final SchemaService schemaService) {
34         schemaContextListenerListenerRegistration = schemaService.registerSchemaContextListener(this);
35     }
36
37     @Override
38     public void onGlobalContextUpdated(final SchemaContext schemaContext) {
39         currentContext.set(schemaContext);
40         // FIXME is notifying all the listeners from this callback wise ?
41         final Set<Capability> addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get());
42         for (final CapabilityListener listener : listeners) {
43             listener.onCapabilitiesAdded(addedCaps);
44         }
45     }
46
47     @Override
48     public void close() throws Exception {
49         listeners.clear();
50         schemaContextListenerListenerRegistration.close();
51         currentContext.set(null);
52     }
53
54     public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
55         listener.onCapabilitiesAdded(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get()));
56         listeners.add(listener);
57         return new AutoCloseable() {
58             @Override
59             public void close() throws Exception {
60                 listeners.remove(listener);
61             }
62         };
63     }
64 }