de874fc0f32d1f415830d0fdf2a16eab2a9e950e
[groupbasedpolicy.git] / sxp-mapper / src / main / java / org / opendaylight / groupbasedpolicy / sxp / mapper / impl / util / SxpListenerUtil.java
1 /**
2  * Copyright (c) 2016 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.groupbasedpolicy.sxp.mapper.impl.util;
9
10 import com.google.common.base.Optional;
11 import com.google.common.util.concurrent.FutureCallback;
12 import javax.annotation.Nullable;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.groupbasedpolicy.sxp.mapper.api.SimpleCachedDao;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17
18 /**
19  * Purpose: provide general logic used by listeners
20  */
21 public final class SxpListenerUtil {
22
23     private SxpListenerUtil() {
24         throw new IllegalAccessError("constructing util class");
25     }
26
27
28     public static <K, V extends DataObject> void updateCachedDao(final SimpleCachedDao<K, V> valueCachedDao,
29                                                                  final K key,
30                                                                  final DataTreeModification<V> change) {
31         final V value = change.getRootNode().getDataAfter();
32         valueCachedDao.update(key, value);
33     }
34
35     public static FutureCallback<Optional<?>> createTxCloseCallback(final ReadOnlyTransaction rTx) {
36         return new FutureCallback<Optional<?>>() {
37             @Override
38             public void onSuccess(@Nullable final Optional<?> result) {
39                 rTx.close();
40             }
41
42             @Override
43             public void onFailure(final Throwable t) {
44                 rTx.close();
45             }
46         };
47     }
48 }