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;
12 import java.util.Collections;
13 import java.util.List;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.genius.mdsalutil.cache.InstanceIdDataObjectCache;
20 import org.opendaylight.infrautils.caches.CacheProvider;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.InterfaceChildInfo;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info.InterfaceParentEntryKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406._interface.child.info._interface.parent.entry.InterfaceChildEntry;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 public class InterfaceChildCache {
31 private static final Logger LOG = LoggerFactory.getLogger(InterfaceChildCache.class);
33 private final InstanceIdDataObjectCache<InterfaceParentEntry> dataObjectCache;
36 public InterfaceChildCache(final DataBroker dataBroker, final CacheProvider cacheProvider) {
37 dataObjectCache = new InstanceIdDataObjectCache<>(InterfaceParentEntry.class,
38 dataBroker, LogicalDatastoreType.CONFIGURATION,
39 InstanceIdentifier.create(InterfaceChildInfo.class).child(InterfaceParentEntry.class),
43 public Optional<List<InterfaceChildEntry>> getInterfaceChildEntries(String parentInterfaceName) {
45 Optional<InterfaceParentEntry> interfaceParentEntry = dataObjectCache.get(
46 getInterfaceParentEntryIdentifier(parentInterfaceName));
47 if (interfaceParentEntry.isPresent()) {
48 List<InterfaceChildEntry> interfaceChildEntries = interfaceParentEntry.get()
49 .getInterfaceChildEntry() != null ? interfaceParentEntry.get()
50 .getInterfaceChildEntry() : Collections.emptyList();
51 return Optional.of(Collections.unmodifiableList(interfaceChildEntries));
53 } catch (ReadFailedException ex) {
54 LOG.error("ReadFailedException exception", ex);
56 return Optional.absent();
59 private InstanceIdentifier<InterfaceParentEntry> getInterfaceParentEntryIdentifier(String parentInterfaceName) {
60 InstanceIdentifier.InstanceIdentifierBuilder<InterfaceParentEntry> intfIdBuilder =
61 InstanceIdentifier.builder(InterfaceChildInfo.class)
62 .child(InterfaceParentEntry.class, new InterfaceParentEntryKey(parentInterfaceName));
63 return intfIdBuilder.build();