2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.nat.read.ifc;
19 import com.google.common.base.Optional;
20 import io.fd.honeycomb.translate.read.ReadContext;
21 import io.fd.honeycomb.translate.read.ReadFailedException;
22 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
23 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
24 import io.fd.honeycomb.translate.vpp.util.NamingContext;
25 import io.fd.vpp.jvpp.snat.dto.SnatInterfaceDetails;
26 import io.fd.vpp.jvpp.snat.dto.SnatInterfaceDetailsReplyDump;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
29 import org.opendaylight.yangtools.concepts.Builder;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
34 abstract class AbstractInterfaceNatCustomizer<C extends DataObject, B extends Builder<C>>
35 implements InitializingReaderCustomizer<C, B> {
37 private final DumpCacheManager<SnatInterfaceDetailsReplyDump, Void> dumpMgr;
38 private final NamingContext ifcContext;
40 AbstractInterfaceNatCustomizer(@Nonnull final DumpCacheManager<SnatInterfaceDetailsReplyDump, Void> dumpMgr,
41 @Nonnull final NamingContext ifcContext) {
42 this.dumpMgr = dumpMgr;
43 this.ifcContext = ifcContext;
47 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id,
48 @Nonnull final B builder,
49 @Nonnull final ReadContext ctx) throws ReadFailedException {
54 public boolean isPresent(final InstanceIdentifier<C> id, final C built, final ReadContext ctx) throws ReadFailedException {
55 final String ifcName = id.firstKeyOf(Interface.class).getName();
56 getLog().debug("Reading NAT features on interface: {}", ifcName);
57 final int index = ifcContext.getIndex(ifcName, ctx.getMappingContext());
59 // Cache dump for each interface under the same key since this is all ifc dump
60 final Optional<SnatInterfaceDetailsReplyDump> dump =
61 dumpMgr.getDump(id, ctx.getModificationCache(), null);
63 // Find entries for current ifc and if is marked as inside set the builder to return presence container
64 return dump.or(new SnatInterfaceDetailsReplyDump()).snatInterfaceDetails.stream()
65 .filter(snatIfcDetail -> snatIfcDetail.swIfIndex == index)
66 .filter(this::isExpectedNatType)
69 // Not setting data, just marking the builder to propagate empty container to indicate presence
72 protected abstract Logger getLog();
74 abstract boolean isExpectedNatType(final SnatInterfaceDetails snatInterfaceDetails);