Fix yang source provisioning from MD-SAL netconf northbound
[netconf.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / 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.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.config.util.capability.Capability;
17 import org.opendaylight.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 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
24
25 public class CurrentSchemaContext implements SchemaContextListener, AutoCloseable {
26     final AtomicReference<SchemaContext> currentContext = new AtomicReference<SchemaContext>();
27     private final ListenerRegistration<SchemaContextListener> schemaContextListenerListenerRegistration;
28     private final Set<CapabilityListener> listeners1 = Collections.synchronizedSet(Sets.<CapabilityListener>newHashSet());
29     private SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider;
30
31     public SchemaContext getCurrentContext() {
32         Preconditions.checkState(currentContext.get() != null, "Current context not received");
33         return currentContext.get();
34     }
35
36     public CurrentSchemaContext(final SchemaService schemaService, final SchemaSourceProvider<YangTextSchemaSource> rootSchemaSourceProvider) {
37         this.rootSchemaSourceProvider = rootSchemaSourceProvider;
38         schemaContextListenerListenerRegistration = schemaService.registerSchemaContextListener(this);
39     }
40
41     @Override
42     public void onGlobalContextUpdated(final SchemaContext schemaContext) {
43         currentContext.set(schemaContext);
44         // FIXME is notifying all the listeners from this callback wise ?
45         final Set<Capability> addedCaps = MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(), rootSchemaSourceProvider);
46         for (final CapabilityListener listener : listeners1) {
47             listener.onCapabilitiesChanged(addedCaps, Collections.<Capability>emptySet());
48         }
49     }
50
51     @Override
52     public void close() throws Exception {
53         listeners1.clear();
54         schemaContextListenerListenerRegistration.close();
55         currentContext.set(null);
56     }
57
58     public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
59         listener.onCapabilitiesChanged(MdsalNetconfOperationServiceFactory.transformCapabilities(currentContext.get(), rootSchemaSourceProvider), Collections.<Capability>emptySet());
60         listeners1.add(listener);
61         return new AutoCloseable() {
62             @Override
63             public void close() throws Exception {
64                 listeners1.remove(listener);
65             }
66         };
67     }
68 }