1f45bed848b74253d80c7b2301883de74faedff7
[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.gaul.modernizer_maven_annotations.SuppressModernizer;
14 import org.opendaylight.controller.md.sal.binding.api.BindingService;
15 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
17 import org.opendaylight.controller.md.sal.dom.api.DOMService;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19
20 @Deprecated
21 public class BindingMountPointAdapter implements MountPoint {
22
23     private final InstanceIdentifier<?> identifier;
24     private LoadingCache<Class<? extends BindingService>, Optional<BindingService>> services;
25
26     public BindingMountPointAdapter(final BindingToNormalizedNodeCodec codec, final DOMMountPoint domMountPoint) {
27         identifier = codec.getCodecRegistry().fromYangInstanceIdentifier(domMountPoint.getIdentifier());
28         services = CacheBuilder.newBuilder().build(new BindingDOMAdapterLoader(codec) {
29
30             @Override
31             protected DOMService getDelegate(final Class<? extends DOMService> reqDeleg) {
32                 return domMountPoint.getService(reqDeleg).orNull();
33             }
34         });
35     }
36
37     @Override
38     public InstanceIdentifier<?> getIdentifier() {
39         return identifier;
40     }
41
42     @Override
43     @SuppressModernizer
44     public <T extends BindingService> Optional<T> getService(final Class<T> service) {
45         Optional<BindingService> potential = services.getUnchecked(service);
46         if (potential.isPresent()) {
47             return Optional.of(service.cast(potential.get()));
48         }
49         return Optional.absent();
50     }
51
52 }