Bug 4933: Yang parser does not accept deviate "not-supported"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / VirtualNamespaceContext.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.yangtools.yang.parser.stmt.reactor;
9
10 import com.google.common.collect.HashMultimap;
11 import com.google.common.collect.Multimap;
12 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
13 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedNamespaceBehaviour;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
15
16 final class VirtualNamespaceContext<K, V, N extends IdentifierNamespace<K, V>, DK>
17         extends NamespaceBehaviourWithListeners<K, V, N> {
18
19     private final Multimap<DK, NamespaceBehaviourWithListeners.ValueAddedListener<K>> listeners = HashMultimap.create();
20     private final DerivedNamespaceBehaviour<K, V, DK, N, ?> derivedDelegate;
21
22     public VirtualNamespaceContext(DerivedNamespaceBehaviour<K, V, DK, N, ?> delegate) {
23         super(delegate);
24         this.derivedDelegate = delegate;
25     }
26
27     protected boolean isRequestedValue(NamespaceBehaviourWithListeners.ValueAddedListener<K> listener, NamespaceStorageNode storage, V value) {
28         return value == getFrom(listener.getCtxNode(), listener.getKey());
29     }
30
31     @Override
32     protected void addListener(K key, NamespaceBehaviourWithListeners.ValueAddedListener<K> listener) {
33         listeners.put(derivedDelegate.getSignificantKey(key), listener);
34     }
35
36
37     void addedToSourceNamespace(NamespaceBehaviour.NamespaceStorageNode storage, DK key, V value) {
38         notifyListeners(storage, listeners.get(key).iterator(), value);
39     }
40
41     @Override
42     public void addTo(final NamespaceStorageNode storage, final K key, final V value) {
43         delegate.addTo(storage, key, value);
44         notifyListeners(storage, listeners.get(derivedDelegate.getSignificantKey(key)).iterator(), value);
45         notifyDerivedNamespaces(storage, key, value);
46     }
47 }