cad0c4fda686a081f4071a41ec026dfb696bdba3
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / CurrentAdapterSerializer.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.mdsal.binding.dom.adapter;
9
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.annotations.Beta;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.cache.CacheBuilder;
17 import com.google.common.cache.CacheLoader;
18 import com.google.common.cache.LoadingCache;
19 import java.util.Collection;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24 import java.util.stream.Collectors;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.mdsal.binding.api.ActionSpec;
28 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.mdsal.binding.api.InstanceNotificationSpec;
30 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
31 import org.opendaylight.mdsal.binding.dom.codec.spi.ForwardingBindingDOMCodecServices;
32 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
33 import org.opendaylight.mdsal.binding.runtime.api.ActionRuntimeType;
34 import org.opendaylight.mdsal.binding.runtime.api.InputRuntimeType;
35 import org.opendaylight.mdsal.binding.runtime.api.NotificationRuntimeType;
36 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.QNameModule;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.model.api.stmt.ActionEffectiveStatement;
43 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
44 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
45 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
46 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 @Beta
51 @VisibleForTesting
52 public final class CurrentAdapterSerializer extends ForwardingBindingDOMCodecServices {
53     private static final Logger LOG = LoggerFactory.getLogger(CurrentAdapterSerializer.class);
54
55     private final LoadingCache<InstanceIdentifier<?>, YangInstanceIdentifier> cache = CacheBuilder.newBuilder()
56             .softValues().build(new CacheLoader<InstanceIdentifier<?>, YangInstanceIdentifier>() {
57                 @Override
58                 public YangInstanceIdentifier load(final InstanceIdentifier<?> key) {
59                     return toYangInstanceIdentifier(key);
60                 }
61             });
62
63     private final ConcurrentMap<JavaTypeName, ContextReferenceExtractor> extractors = new ConcurrentHashMap<>();
64     private final @NonNull BindingDOMCodecServices delegate;
65
66     public CurrentAdapterSerializer(final BindingDOMCodecServices delegate) {
67         this.delegate = requireNonNull(delegate);
68     }
69
70     @Override
71     protected BindingDOMCodecServices delegate() {
72         return delegate;
73     }
74
75     @NonNull YangInstanceIdentifier toCachedYangInstanceIdentifier(final @NonNull InstanceIdentifier<?> path) {
76         return cache.getUnchecked(path);
77     }
78
79     <T extends DataObject> @NonNull InstanceIdentifier<T> coerceInstanceIdentifier(final YangInstanceIdentifier dom) {
80         return verifyNotNull(fromYangInstanceIdentifier(dom));
81     }
82
83     DOMDataTreeIdentifier toDOMDataTreeIdentifier(final DataTreeIdentifier<?> path) {
84         return DOMDataTreeIdentifier.of(path.datastore(), toYangInstanceIdentifier(path.path()));
85     }
86
87     Collection<DOMDataTreeIdentifier> toDOMDataTreeIdentifiers(final Collection<DataTreeIdentifier<?>> subtrees) {
88         return subtrees.stream().map(this::toDOMDataTreeIdentifier).collect(Collectors.toSet());
89     }
90
91     @NonNull Absolute getActionPath(final @NonNull ActionSpec<?, ?> spec) {
92         final var type = getRuntimeContext().getTypes().findSchema(JavaTypeName.create(spec.type()))
93             .orElseThrow(() -> new IllegalArgumentException("Action " + spec + " is not known"));
94         if (!(type instanceof ActionRuntimeType actionType)) {
95             throw new IllegalArgumentException("Action " + spec + " resolved to unexpected " + type);
96         }
97
98         final var entry = resolvePath(spec.path());
99         final var stack = entry.getKey();
100         final var stmt = stack.enterSchemaTree(actionType.statement().argument().bindTo(entry.getValue()));
101         verify(stmt instanceof ActionEffectiveStatement, "Action %s resolved to unexpected statement %s", spec, stmt);
102         return stack.toSchemaNodeIdentifier();
103     }
104
105     @NonNull Absolute getNotificationPath(final @NonNull InstanceNotificationSpec<?, ?> spec) {
106         final var type = getRuntimeContext().getTypes().findSchema(JavaTypeName.create(spec.type()))
107             .orElseThrow(() -> new IllegalArgumentException("Notification " + spec + " is not known"));
108         if (!(type instanceof NotificationRuntimeType notifType)) {
109             throw new IllegalArgumentException("Notification " + spec + " resolved to unexpected " + type);
110         }
111
112         final var entry = resolvePath(spec.path());
113         final var stack = entry.getKey();
114         final var stmt = stack.enterSchemaTree(notifType.statement().argument().bindTo(entry.getValue()));
115         verify(stmt instanceof NotificationEffectiveStatement, "Notification %s resolved to unexpected statement %s",
116             spec, stmt);
117         return stack.toSchemaNodeIdentifier();
118     }
119
120     @Nullable ContextReferenceExtractor findExtractor(final @NonNull InputRuntimeType inputType) {
121         final var inputName = inputType.getIdentifier();
122         final var cached = extractors.get(inputName);
123         if (cached != null) {
124             return cached;
125         }
126
127         // Load the class
128         final Class<?> inputClass;
129         try {
130             inputClass = getRuntimeContext().loadClass(inputName);
131         } catch (ClassNotFoundException e) {
132             throw new IllegalArgumentException("Failed to load class for " + inputType, e);
133         }
134
135         // Check if there is an extractor at all
136         final var created = ContextReferenceExtractor.of(inputClass);
137         if (created == null) {
138             return null;
139         }
140
141         // Reconcile with cache
142         final var raced = extractors.putIfAbsent(inputName, created);
143         return raced != null ? raced : created;
144     }
145
146     private @NonNull Entry<SchemaInferenceStack, QNameModule> resolvePath(final @NonNull InstanceIdentifier<?> path) {
147         final var stack = SchemaInferenceStack.of(getRuntimeContext().getEffectiveModelContext());
148         final var it = toYangInstanceIdentifier(path).getPathArguments().iterator();
149         verify(it.hasNext(), "Unexpected empty instance identifier for %s", path);
150
151         QNameModule lastNamespace;
152         do {
153             final var arg = it.next();
154             final var qname = arg.getNodeType();
155             final var stmt = stack.enterDataTree(qname);
156             lastNamespace = qname.getModule();
157             if (stmt instanceof ListEffectiveStatement) {
158                 // Lists have two steps
159                 verify(it.hasNext(), "Unexpected list termination at %s in %s", stmt, path);
160                 // Verify just to make sure we are doing the right thing
161                 final var skipped = it.next();
162                 verify(skipped instanceof NodeIdentifier, "Unexpected skipped list entry item %s in %s", skipped, path);
163                 verify(stmt.argument().equals(skipped.getNodeType()), "Mismatched list entry item %s in %s", skipped,
164                     path);
165             }
166         } while (it.hasNext());
167
168         return Map.entry(stack, lastNamespace);
169     }
170 }