Fix checkstyle issues to enforce it
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / osgi / YangStoreService.java
1 /*
2  * Copyright (c) 2015, 2017 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.config.facade.xml.osgi;
10
11 import com.google.common.collect.Collections2;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.Sets;
14 import com.google.common.util.concurrent.ThreadFactoryBuilder;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import javax.annotation.concurrent.GuardedBy;
22 import org.opendaylight.controller.config.util.capability.Capability;
23 import org.opendaylight.controller.config.util.capability.ModuleListener;
24 import org.opendaylight.controller.config.util.capability.YangModuleCapability;
25 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
26 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
33
34 public class YangStoreService implements YangStoreContext {
35
36     private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
37     private final ExecutorService notificationExecutor = Executors.newSingleThreadExecutor(
38             new ThreadFactoryBuilder().setDaemon(true).setNameFormat("yangstore-capability-notifications").build());
39
40     /**
41      * Guarded by explicit lock to allow for properly synchronizing the initial
42      * notification and modification of the listener set.
43      */
44     @GuardedBy("listeners")
45     private final Set<ModuleListener> listeners = new HashSet<>();
46
47     /**
48      * This is the latest snapshot. Some of its state is always initialized, but the
49      * MXBean maps potentially cause recomputation. Accessing those two specific
50      * methods needs to re-check whether the snapshot has changed asynchronously and
51      * retry if it didi.
52      */
53     private volatile YangStoreSnapshot snap;
54
55     public YangStoreService(final SchemaContextProvider schemaContextProvider,
56             final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
57         this.sourceProvider = sourceProvider;
58     }
59
60     public YangStoreContext getCurrentSnapshot() {
61         return this.snap;
62     }
63
64     @Deprecated
65     @Override
66     public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
67         Map<String, Map<String, ModuleMXBeanEntry>> ret;
68         YangStoreSnapshot snapshot;
69
70         do {
71             snapshot = this.snap;
72             ret = snapshot.getModuleMXBeanEntryMap();
73         } while (!snapshot.equals(this.snap));
74
75         return ret;
76     }
77
78     @Override
79     public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
80         Map<QName, Map<String, ModuleMXBeanEntry>> ret;
81         YangStoreSnapshot snapshot;
82
83         do {
84             snapshot = this.snap;
85             ret = snapshot.getQNamesToIdentitiesToModuleMXBeanEntries();
86         } while (!snapshot.equals(this.snap));
87
88         return ret;
89     }
90
91     @Override
92     public Set<Module> getModules() {
93         return this.snap.getModules();
94     }
95
96     @Override
97     public String getModuleSource(final ModuleIdentifier moduleIdentifier) {
98         return this.snap.getModuleSource(moduleIdentifier);
99     }
100
101     @Override
102     public EnumResolver getEnumResolver() {
103         return this.snap.getEnumResolver();
104     }
105
106     public void refresh(final BindingRuntimeContext runtimeContext) {
107         final YangStoreSnapshot next = new YangStoreSnapshot(runtimeContext, this.sourceProvider);
108         final YangStoreSnapshot previous = this.snap;
109         this.snap = next;
110         this.notificationExecutor.submit(() -> notifyListeners(previous, next));
111     }
112
113     public AutoCloseable registerModuleListener(final ModuleListener listener) {
114         final YangStoreContext context = this.snap;
115
116         synchronized (this.listeners) {
117             if (context != null) {
118                 listener.onCapabilitiesChanged(toCapabilities(context.getModules(), context),
119                         Collections.<Capability>emptySet());
120             }
121             this.listeners.add(listener);
122         }
123
124         return () -> {
125             synchronized (YangStoreService.this.listeners) {
126                 YangStoreService.this.listeners.remove(listener);
127             }
128         };
129     }
130
131     void notifyListeners(final YangStoreSnapshot previous, final YangStoreSnapshot current) {
132         final Set<Module> prevModules = previous.getModules();
133         final Set<Module> currModules = current.getModules();
134         final Set<Module> removed = Sets.difference(prevModules, currModules);
135         final Set<Module> added = Sets.difference(currModules, prevModules);
136
137         final Set<Capability> addedCaps = toCapabilities(added, current);
138         final Set<Capability> removedCaps = toCapabilities(removed, current);
139
140         synchronized (this.listeners) {
141             for (final ModuleListener listener : this.listeners) {
142                 listener.onCapabilitiesChanged(addedCaps, removedCaps);
143             }
144         }
145     }
146
147     private static Set<Capability> toCapabilities(final Set<Module> modules, final YangStoreContext current) {
148         return ImmutableSet.copyOf(Collections2.transform(modules,
149             input -> new YangModuleCapability(input, current.getModuleSource(input))));
150     }
151 }