2 * Copyright (c) 2017, 2018 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.genius.interfacemanager.listeners;
11 import com.google.common.base.Optional;
13 import java.util.Collections;
14 import java.util.List;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.genius.mdsalutil.cache.DataObjectCache;
22 import org.opendaylight.infrautils.caches.CacheProvider;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.InterfaceChildInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntryKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntry;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 public class InterfaceChildCache {
33 private static final Logger LOG = LoggerFactory.getLogger(InterfaceChildCache.class);
35 private final DataObjectCache<InterfaceParentEntry> dataObjectCache;
38 public InterfaceChildCache(final DataBroker dataBroker, final CacheProvider cacheProvider) {
39 dataObjectCache = new DataObjectCache<>(InterfaceParentEntry.class,
40 dataBroker, LogicalDatastoreType.CONFIGURATION,
41 InstanceIdentifier.create(InterfaceChildInfo.class).child(InterfaceParentEntry.class),
45 public Optional<List<InterfaceChildEntry>> getInterfaceChildEntries(String parentInterfaceName) {
47 Optional<InterfaceParentEntry> interfaceParentEntry = dataObjectCache.get(
48 getInterfaceParentEntryIdentifier(parentInterfaceName));
49 if (interfaceParentEntry.isPresent()) {
50 List<InterfaceChildEntry> interfaceChildEntries = (interfaceParentEntry.get()
51 .getInterfaceChildEntry() != null) ? interfaceParentEntry.get()
52 .getInterfaceChildEntry() : Collections.emptyList();
53 return Optional.of(Collections.unmodifiableList(interfaceChildEntries));
55 } catch (ReadFailedException ex) {
56 LOG.error("ReadFailedException exception", ex);
58 return Optional.absent();
61 private InstanceIdentifier<InterfaceParentEntry> getInterfaceParentEntryIdentifier(String parentInterfaceName) {
62 InstanceIdentifier.InstanceIdentifierBuilder<InterfaceParentEntry> intfIdBuilder =
63 InstanceIdentifier.builder(InterfaceChildInfo.class)
64 .child(InterfaceParentEntry.class, new InterfaceParentEntryKey(parentInterfaceName));
65 return intfIdBuilder.build();