Merge "Resolve Bug:707 - ConfigPusher should wait for netconf-impl to register JMX...
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / DataBrokerImpl.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.binding.impl;
9
10 import java.util.Map;
11 import java.util.Map.Entry;
12 import java.util.Set;
13 import java.util.concurrent.atomic.AtomicLong;
14
15 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
16 import org.opendaylight.controller.md.sal.common.api.data.DataReader;
17 import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter;
18 import org.opendaylight.controller.md.sal.common.impl.service.AbstractDataBroker;
19 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
20 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.util.DataObjectReadingUtil;
25
26 import com.google.common.base.Predicate;
27 import com.google.common.collect.ImmutableList;
28 import com.google.common.collect.ImmutableMap;
29 import com.google.common.collect.ImmutableMap.Builder;
30 import com.google.common.collect.Maps;
31
32 public class DataBrokerImpl extends
33         AbstractDataBroker<InstanceIdentifier<? extends DataObject>, DataObject, DataChangeListener> //
34         implements DataProviderService, AutoCloseable {
35
36     private final static class ContainsWildcarded implements Predicate<InstanceIdentifier<? extends DataObject>> {
37
38         private final InstanceIdentifier<? extends DataObject> key;
39
40         public ContainsWildcarded(final InstanceIdentifier<? extends DataObject> key) {
41             this.key = key;
42         }
43
44         @Override
45         public boolean apply(final InstanceIdentifier<? extends DataObject> input) {
46             return key.containsWildcarded(input);
47         }
48     }
49
50     private final static class IsContainedWildcarded implements Predicate<InstanceIdentifier<? extends DataObject>> {
51
52         private final InstanceIdentifier<? extends DataObject> key;
53
54         public IsContainedWildcarded(final InstanceIdentifier<? extends DataObject> key) {
55             this.key = key;
56         }
57
58         @Override
59         public boolean apply(final InstanceIdentifier<? extends DataObject> input) {
60             return input.containsWildcarded(key);
61         }
62     }
63
64     private final AtomicLong nextTransaction = new AtomicLong();
65     private final AtomicLong createdTransactionsCount = new AtomicLong();
66     private final DelegatingDataReadRouter router = new DelegatingDataReadRouter();
67     private DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject> rootCommitHandler;
68
69     public DataBrokerImpl() {
70         setDataReadRouter(router);
71     }
72
73     public void setDataReadDelegate(final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> delegate) {
74         router.setDelegate(delegate);
75     }
76
77     public AtomicLong getCreatedTransactionsCount() {
78         return createdTransactionsCount;
79     }
80
81     @Override
82     public DataTransactionImpl beginTransaction() {
83         String transactionId = "BA-" + nextTransaction.getAndIncrement();
84         createdTransactionsCount.getAndIncrement();
85         return new DataTransactionImpl(transactionId, this);
86     }
87
88     @Override
89     public void close() {
90
91     }
92
93     @Override
94     protected Predicate<InstanceIdentifier<? extends DataObject>> createContainsPredicate(
95             final InstanceIdentifier<? extends DataObject> key) {
96         return new ContainsWildcarded(key);
97     }
98
99     @Override
100     protected Predicate<InstanceIdentifier<? extends DataObject>> createIsContainedPredicate(
101             final InstanceIdentifier<? extends DataObject> key) {
102         return new IsContainedWildcarded(key);
103     }
104
105     @SuppressWarnings({ "unchecked", "rawtypes" })
106     @Override
107     protected Map<InstanceIdentifier<? extends DataObject>, DataObject> deepGetBySubpath(
108             final Map<InstanceIdentifier<? extends DataObject>, DataObject> dataSet,
109             final InstanceIdentifier<? extends DataObject> path) {
110         Builder<InstanceIdentifier<? extends DataObject>, DataObject> builder = ImmutableMap.builder();
111         Map<InstanceIdentifier<? extends DataObject>, DataObject> potential = Maps.filterKeys(dataSet,
112                 createIsContainedPredicate(path));
113         for (Entry<InstanceIdentifier<? extends DataObject>, DataObject> entry : potential.entrySet()) {
114             try {
115                 builder.putAll(DataObjectReadingUtil.readData(entry.getValue(), (InstanceIdentifier) entry.getKey(),
116                         path));
117             } catch (Exception e) {
118                 // FIXME : Log exception;
119             }
120         }
121         return builder.build();
122
123     }
124
125     public class DelegatingDataReadRouter extends
126             AbstractDataReadRouter<InstanceIdentifier<? extends DataObject>, DataObject> {
127
128         private DataReader<InstanceIdentifier<? extends DataObject>, DataObject> delegate;
129
130         @Override
131         public DataObject readConfigurationData(final InstanceIdentifier<? extends DataObject> path) {
132             return delegate.readConfigurationData(path);
133         }
134
135         public void setDelegate(final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> delegate) {
136             this.delegate = delegate;
137         }
138
139         @Override
140         public DataObject readOperationalData(final InstanceIdentifier<? extends DataObject> path) {
141             return delegate.readOperationalData(path);
142         }
143
144         @Override
145         protected DataObject merge(final InstanceIdentifier<? extends DataObject> path, final Iterable<DataObject> data) {
146             throw new UnsupportedOperationException("Not supported");
147         }
148
149         @Override
150         public Registration<DataReader<InstanceIdentifier<? extends DataObject>, DataObject>> registerConfigurationReader(
151                 final InstanceIdentifier<? extends DataObject> path,
152                 final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> reader) {
153             throw new UnsupportedOperationException("Not supported");
154         }
155
156         @Override
157         public Registration<DataReader<InstanceIdentifier<? extends DataObject>, DataObject>> registerOperationalReader(
158                 final InstanceIdentifier<? extends DataObject> path,
159                 final DataReader<InstanceIdentifier<? extends DataObject>, DataObject> reader) {
160             throw new UnsupportedOperationException("Not supported");
161         }
162     }
163
164     @Override
165     protected ImmutableList<DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject>> affectedCommitHandlers(
166             final Set<InstanceIdentifier<? extends DataObject>> paths) {
167         ImmutableList.Builder<DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject>> handlersBuilder = ImmutableList.builder();
168         return handlersBuilder //
169                 .add(rootCommitHandler) //
170                 .addAll(super.affectedCommitHandlers(paths)) //
171                 .build();
172     }
173
174     public void setRootCommitHandler(final DataCommitHandler<InstanceIdentifier<? extends DataObject>, DataObject> commitHandler) {
175         rootCommitHandler = commitHandler;
176     }
177
178 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.