6861ff9b3cae870b9f06eb6b5b6f175973e8d760
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMMountPointListenerAdapter.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.controller.md.sal.binding.impl;
9
10 import com.google.common.base.Optional;
11 import org.opendaylight.controller.md.sal.binding.api.MountPointService.MountPointListener;
12 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
13 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
14 import org.opendaylight.yangtools.concepts.ListenerRegistration;
15 import org.opendaylight.yangtools.yang.binding.DataObject;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
19
20 final class BindingDOMMountPointListenerAdapter<T extends MountPointListener>
21         implements ListenerRegistration<T>, MountProvisionListener {
22
23     private final T listener;
24     private final ListenerRegistration<MountProvisionListener> registration;
25     private final BindingToNormalizedNodeCodec codec;
26
27     BindingDOMMountPointListenerAdapter(final T listener, final BindingToNormalizedNodeCodec codec,
28             final DOMMountPointService mountService) {
29         this.listener = listener;
30         this.codec = codec;
31         this.registration = mountService.registerProvisionListener(this);
32     }
33
34     @Override
35     public T getInstance() {
36         return listener;
37     }
38
39     @Override
40     public void close() {
41         registration.close();
42     }
43
44     @Override
45     public void onMountPointCreated(final YangInstanceIdentifier path) {
46         try {
47             final InstanceIdentifier<? extends DataObject> bindingPath = toBinding(path);
48             listener.onMountPointCreated(bindingPath);
49         } catch (final DeserializationException e) {
50             BindingDOMMountPointServiceAdapter.LOG.error("Unable to translate mountPoint path {}. Omitting event.",
51                     path, e);
52         }
53
54     }
55
56     private InstanceIdentifier<? extends DataObject> toBinding(final YangInstanceIdentifier path)
57             throws DeserializationException {
58         final Optional<InstanceIdentifier<? extends DataObject>> instanceIdentifierOptional = codec.toBinding(path);
59         if (instanceIdentifierOptional.isPresent()) {
60             return instanceIdentifierOptional.get();
61         } else {
62             throw new DeserializationException("Deserialization unsuccessful, " + instanceIdentifierOptional);
63         }
64     }
65
66     @Override
67     public void onMountPointRemoved(final YangInstanceIdentifier path) {
68         try {
69             final InstanceIdentifier<? extends DataObject> bindingPath = toBinding(path);
70             listener.onMountPointRemoved(bindingPath);
71         } catch (final DeserializationException e) {
72             BindingDOMMountPointServiceAdapter.LOG.error("Unable to translate mountPoint path {}. Omitting event.",
73                     path, e);
74         }
75     }
76 }

©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.