2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.dom.broker.impl;
10 import static com.google.common.base.Preconditions.checkState;
11 import com.google.common.base.Predicate;
12 import com.google.common.collect.FluentIterable;
13 import com.google.common.collect.ImmutableSet;
14 import com.google.common.collect.Iterables;
15 import java.util.ArrayList;
16 import java.util.Comparator;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.Map.Entry;
21 import java.util.concurrent.Future;
22 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
23 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
24 import org.opendaylight.controller.md.sal.common.api.data.DataReader;
25 import org.opendaylight.controller.md.sal.common.impl.AbstractDataModification;
26 import org.opendaylight.controller.md.sal.common.impl.util.AbstractLockableDelegator;
27 import org.opendaylight.controller.sal.core.api.data.DataStore;
28 import org.opendaylight.controller.sal.dom.broker.util.YangDataOperations;
29 import org.opendaylight.controller.sal.dom.broker.util.YangSchemaUtils;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33 import org.opendaylight.yangtools.yang.data.api.Node;
34 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
37 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class SchemaAwareDataStoreAdapter extends AbstractLockableDelegator<DataStore> implements //
47 SchemaContextListener, //
50 private final static Logger LOG = LoggerFactory.getLogger(SchemaAwareDataStoreAdapter.class);
52 private SchemaContext schema = null;
53 private boolean validationEnabled = false;
54 private final DataReader<YangInstanceIdentifier, CompositeNode> reader = new MergeFirstLevelReader();
57 public boolean containsConfigurationPath(final YangInstanceIdentifier path) {
59 getDelegateReadLock().lock();
60 return getDelegate().containsConfigurationPath(path);
63 getDelegateReadLock().unlock();
68 public boolean containsOperationalPath(final YangInstanceIdentifier path) {
70 getDelegateReadLock().lock();
71 return getDelegate().containsOperationalPath(path);
74 getDelegateReadLock().unlock();
79 public Iterable<YangInstanceIdentifier> getStoredConfigurationPaths() {
81 getDelegateReadLock().lock();
82 return getDelegate().getStoredConfigurationPaths();
85 getDelegateReadLock().unlock();
90 public Iterable<YangInstanceIdentifier> getStoredOperationalPaths() {
92 getDelegateReadLock().lock();
93 return getDelegate().getStoredOperationalPaths();
96 getDelegateReadLock().unlock();
101 public CompositeNode readConfigurationData(final YangInstanceIdentifier path) {
102 return reader.readConfigurationData(path);
106 public CompositeNode readOperationalData(final YangInstanceIdentifier path) {
107 return reader.readOperationalData(path);
111 public org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction<YangInstanceIdentifier, CompositeNode> requestCommit(
112 final DataModification<YangInstanceIdentifier, CompositeNode> modification) {
113 validateAgainstSchema(modification);
114 NormalizedDataModification cleanedUp = prepareMergedTransaction(modification);
115 cleanedUp.status = TransactionStatus.SUBMITED;
116 return retrieveDelegate().requestCommit(cleanedUp);
119 public boolean isValidationEnabled() {
120 return validationEnabled;
123 public void setValidationEnabled(final boolean validationEnabled) {
124 this.validationEnabled = validationEnabled;
127 private void validateAgainstSchema(final DataModification<YangInstanceIdentifier, CompositeNode> modification) {
128 if (!validationEnabled) {
132 if (schema == null) {
133 LOG.warn("Validation not performed for {}. Reason: YANG Schema not present.", modification.getIdentifier());
139 protected void onDelegateChanged(final DataStore oldDelegate, final DataStore newDelegate) {
144 public void onGlobalContextUpdated(final SchemaContext context) {
145 this.schema = context;
149 public void close() throws Exception {
153 protected CompositeNode mergeData(final YangInstanceIdentifier path, final CompositeNode stored, final CompositeNode modified,
154 final boolean config) {
155 // long startTime = System.nanoTime();
157 DataSchemaNode node = schemaNodeFor(path);
158 return YangDataOperations.merge(node, stored, modified, config);
160 // System.out.println("Merge time: " + ((System.nanoTime() -
161 // startTime) / 1000.0d));
165 private DataSchemaNode schemaNodeFor(final YangInstanceIdentifier path) {
166 checkState(schema != null, "YANG Schema is not available");
167 return YangSchemaUtils.getSchemaNode(schema, path);
170 private NormalizedDataModification prepareMergedTransaction(
171 final DataModification<YangInstanceIdentifier, CompositeNode> original) {
172 NormalizedDataModification normalized = new NormalizedDataModification(original);
173 LOG.trace("Transaction: {} Removed Configuration {}, Removed Operational {}", original.getIdentifier(),
174 original.getRemovedConfigurationData(), original.getRemovedConfigurationData());
175 LOG.trace("Transaction: {} Created Configuration {}, Created Operational {}", original.getIdentifier(),
176 original.getCreatedConfigurationData().entrySet(), original.getCreatedOperationalData().entrySet());
177 LOG.trace("Transaction: {} Updated Configuration {}, Updated Operational {}", original.getIdentifier(),
178 original.getUpdatedConfigurationData().entrySet(), original.getUpdatedOperationalData().entrySet());
180 for (YangInstanceIdentifier entry : original.getRemovedConfigurationData()) {
181 normalized.deepRemoveConfigurationData(entry);
183 for (YangInstanceIdentifier entry : original.getRemovedOperationalData()) {
184 normalized.deepRemoveOperationalData(entry);
186 for (Entry<YangInstanceIdentifier, CompositeNode> entry : original.getUpdatedConfigurationData().entrySet()) {
187 normalized.putDeepConfigurationData(entry.getKey(), entry.getValue());
189 for (Entry<YangInstanceIdentifier, CompositeNode> entry : original.getUpdatedOperationalData().entrySet()) {
190 normalized.putDeepOperationalData(entry.getKey(), entry.getValue());
195 private Iterable<YangInstanceIdentifier> getConfigurationSubpaths(final YangInstanceIdentifier entry) {
196 // FIXME: This should be replaced by index
197 Iterable<YangInstanceIdentifier> paths = getStoredConfigurationPaths();
199 return getChildrenPaths(entry, paths);
203 public Iterable<YangInstanceIdentifier> getOperationalSubpaths(final YangInstanceIdentifier entry) {
204 // FIXME: This should be indexed
205 Iterable<YangInstanceIdentifier> paths = getStoredOperationalPaths();
207 return getChildrenPaths(entry, paths);
210 private static final Iterable<YangInstanceIdentifier> getChildrenPaths(final YangInstanceIdentifier entry,
211 final Iterable<YangInstanceIdentifier> paths) {
212 ImmutableSet.Builder<YangInstanceIdentifier> children = ImmutableSet.builder();
213 for (YangInstanceIdentifier potential : paths) {
214 if (entry.contains(potential)) {
218 return children.build();
221 private final Comparator<Entry<YangInstanceIdentifier, CompositeNode>> preparationComparator = new Comparator<Entry<YangInstanceIdentifier, CompositeNode>>() {
223 public int compare(final Entry<YangInstanceIdentifier, CompositeNode> o1, final Entry<YangInstanceIdentifier, CompositeNode> o2) {
224 YangInstanceIdentifier o1Key = o1.getKey();
225 YangInstanceIdentifier o2Key = o2.getKey();
226 return Integer.compare(Iterables.size(o1Key.getPathArguments()), Iterables.size(o2Key.getPathArguments()));
230 private class MergeFirstLevelReader implements DataReader<YangInstanceIdentifier, CompositeNode> {
233 public CompositeNode readConfigurationData(final YangInstanceIdentifier path) {
234 getDelegateReadLock().lock();
236 if (Iterables.isEmpty(path.getPathArguments())) {
240 CompositeNode original = getDelegate().readConfigurationData(path);
241 ArrayList<Node<?>> childNodes = new ArrayList<Node<?>>();
242 if (original != null) {
243 childNodes.addAll(original.getValue());
244 qname = original.getNodeType();
246 qname = path.getLastPathArgument().getNodeType();
249 FluentIterable<YangInstanceIdentifier> directChildren = FluentIterable.from(getStoredConfigurationPaths())
250 .filter(new Predicate<YangInstanceIdentifier>() {
252 public boolean apply(final YangInstanceIdentifier input) {
253 if (path.contains(input)) {
254 int nesting = Iterables.size(input.getPathArguments()) - Iterables.size(path.getPathArguments());
262 for (YangInstanceIdentifier instanceIdentifier : directChildren) {
263 childNodes.add(getDelegate().readConfigurationData(instanceIdentifier));
265 if (original == null && childNodes.isEmpty()) {
269 return new CompositeNodeTOImpl(qname, null, childNodes);
271 getDelegateReadLock().unlock();
276 public CompositeNode readOperationalData(final YangInstanceIdentifier path) {
277 getDelegateReadLock().lock();
279 if (Iterables.isEmpty(path.getPathArguments())) {
283 CompositeNode original = getDelegate().readOperationalData(path);
284 ArrayList<Node<?>> childNodes = new ArrayList<Node<?>>();
285 if (original != null) {
286 childNodes.addAll(original.getValue());
287 qname = original.getNodeType();
289 qname = path.getLastPathArgument().getNodeType();
292 FluentIterable<YangInstanceIdentifier> directChildren = FluentIterable.from(getStoredOperationalPaths())
293 .filter(new Predicate<YangInstanceIdentifier>() {
295 public boolean apply(final YangInstanceIdentifier input) {
296 if (path.contains(input)) {
297 int nesting = Iterables.size(input.getPathArguments()) - Iterables.size(path.getPathArguments());
306 for (YangInstanceIdentifier instanceIdentifier : directChildren) {
307 childNodes.add(getDelegate().readOperationalData(instanceIdentifier));
309 if (original == null && childNodes.isEmpty()) {
313 return new CompositeNodeTOImpl(qname, null, childNodes);
315 getDelegateReadLock().unlock();
320 private class NormalizedDataModification extends AbstractDataModification<YangInstanceIdentifier, CompositeNode> {
322 private final String CONFIGURATIONAL_DATA_STORE_MARKER = "configurational";
323 private final String OPERATIONAL_DATA_STORE_MARKER = "operational";
324 private final Object identifier;
325 private TransactionStatus status;
327 public NormalizedDataModification(final DataModification<YangInstanceIdentifier, CompositeNode> original) {
328 super(getDelegate());
329 identifier = original;
330 status = TransactionStatus.NEW;
335 * Ensures all subpaths are removed - this currently does slow lookup in
340 public void deepRemoveOperationalData(final YangInstanceIdentifier entry) {
341 Iterable<YangInstanceIdentifier> paths = getOperationalSubpaths(entry);
342 removeOperationalData(entry);
343 for (YangInstanceIdentifier potential : paths) {
344 removeOperationalData(potential);
348 public void deepRemoveConfigurationData(final YangInstanceIdentifier entry) {
349 Iterable<YangInstanceIdentifier> paths = getConfigurationSubpaths(entry);
350 removeConfigurationData(entry);
351 for (YangInstanceIdentifier potential : paths) {
352 removeConfigurationData(potential);
356 public void putDeepConfigurationData(final YangInstanceIdentifier entryKey, final CompositeNode entryData) {
357 this.putCompositeNodeData(entryKey, entryData, CONFIGURATIONAL_DATA_STORE_MARKER);
360 public void putDeepOperationalData(final YangInstanceIdentifier entryKey, final CompositeNode entryData) {
361 this.putCompositeNodeData(entryKey, entryData, OPERATIONAL_DATA_STORE_MARKER);
365 public Object getIdentifier() {
366 return this.identifier;
370 public TransactionStatus getStatus() {
375 public Future<RpcResult<TransactionStatus>> commit() {
376 throw new UnsupportedOperationException("Commit should not be invoked on this");
380 protected CompositeNode mergeConfigurationData(final YangInstanceIdentifier path, final CompositeNode stored,
381 final CompositeNode modified) {
382 return mergeData(path, stored, modified, true);
386 protected CompositeNode mergeOperationalData(final YangInstanceIdentifier path, final CompositeNode stored,
387 final CompositeNode modified) {
388 return mergeData(path, stored, modified, false);
391 private void putData(final YangInstanceIdentifier entryKey, final CompositeNode entryData, final String dataStoreIdentifier) {
392 if (dataStoreIdentifier != null && entryKey != null && entryData != null) {
393 switch (dataStoreIdentifier) {
394 case (CONFIGURATIONAL_DATA_STORE_MARKER):
395 this.putConfigurationData(entryKey, entryData);
397 case (OPERATIONAL_DATA_STORE_MARKER):
398 this.putOperationalData(entryKey, entryData);
402 LOG.error(dataStoreIdentifier + " is NOT valid DataStore switch marker");
403 throw new RuntimeException(dataStoreIdentifier + " is NOT valid DataStore switch marker");
408 private void putCompositeNodeData(final YangInstanceIdentifier entryKey, final CompositeNode entryData,
409 final String dataStoreIdentifier) {
410 this.putData(entryKey, entryData, dataStoreIdentifier);
412 for (Node<?> child : entryData.getValue()) {
413 YangInstanceIdentifier subEntryId = YangInstanceIdentifier.builder(entryKey).node(child.getNodeType())
415 if (child instanceof CompositeNode) {
416 DataSchemaNode subSchema = schemaNodeFor(subEntryId);
417 CompositeNode compNode = (CompositeNode) child;
418 YangInstanceIdentifier instanceId = null;
420 if (subSchema instanceof ListSchemaNode) {
421 ListSchemaNode listSubSchema = (ListSchemaNode) subSchema;
422 Map<QName, Object> mapOfSubValues = this.getValuesFromListSchema(listSubSchema,
423 (CompositeNode) child);
424 if (mapOfSubValues != null) {
425 instanceId = YangInstanceIdentifier.builder(entryKey)
426 .nodeWithKey(listSubSchema.getQName(), mapOfSubValues).toInstance();
428 } else if (subSchema instanceof ContainerSchemaNode) {
429 ContainerSchemaNode containerSchema = (ContainerSchemaNode) subSchema;
430 instanceId = YangInstanceIdentifier.builder(entryKey).node(subSchema.getQName()).toInstance();
432 if (instanceId != null) {
433 this.putCompositeNodeData(instanceId, compNode, dataStoreIdentifier);
439 private Map<QName, Object> getValuesFromListSchema(final ListSchemaNode listSchema, final CompositeNode entryData) {
440 List<QName> keyDef = listSchema.getKeyDefinition();
441 if (keyDef != null && !keyDef.isEmpty()) {
442 Map<QName, Object> map = new HashMap<QName, Object>();
443 for (QName key : keyDef) {
444 List<Node<?>> data = entryData.get(key);
445 if (data != null && !data.isEmpty()) {
446 for (Node<?> nodeData : data) {
447 if (nodeData instanceof SimpleNode<?>) {
448 map.put(key, data.get(0).getValue());