Merge "Reduce/enhance logging in AbstractLeader"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingMountPointAdapter.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 com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.LoadingCache;
13 import org.opendaylight.controller.md.sal.binding.api.BindingService;
14 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
16 import org.opendaylight.controller.md.sal.dom.api.DOMService;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18
19 public class BindingMountPointAdapter implements MountPoint {
20
21     private final InstanceIdentifier<?> identifier;
22     private LoadingCache<Class<? extends BindingService>, Optional<BindingService>> services;
23
24     public BindingMountPointAdapter(final BindingToNormalizedNodeCodec codec, final DOMMountPoint domMountPoint) {
25         identifier = codec.getCodecRegistry().fromYangInstanceIdentifier(domMountPoint.getIdentifier());
26         services = CacheBuilder.newBuilder().build(new BindingDOMAdapterLoader(codec) {
27
28             @Override
29             protected DOMService getDelegate(Class<? extends DOMService> reqDeleg) {
30                 return domMountPoint.getService(reqDeleg).orNull();
31             }
32         });
33     }
34
35     @Override
36     public InstanceIdentifier<?> getIdentifier() {
37         return identifier;
38     }
39
40     @Override
41     public <T extends BindingService> Optional<T> getService(Class<T> service) {
42         Optional<BindingService> potential = services.getUnchecked(service);
43         if(potential.isPresent()) {
44             return Optional.of(service.cast(potential.get()));
45         }
46         return Optional.absent();
47     }
48
49 }