f4d45900cb2154bceaf5233e814d738ae9497ab6
[honeycomb.git] / nat / nat2vpp / src / main / java / io / fd / honeycomb / nat / read / ifc / AbstractInterfaceNatCustomizer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.nat.read.ifc;
18
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;
33
34 abstract class AbstractInterfaceNatCustomizer<C extends DataObject, B extends Builder<C>>
35         implements InitializingReaderCustomizer<C, B> {
36
37     private final DumpCacheManager<SnatInterfaceDetailsReplyDump, Void> dumpMgr;
38     private final NamingContext ifcContext;
39
40     AbstractInterfaceNatCustomizer(@Nonnull final DumpCacheManager<SnatInterfaceDetailsReplyDump, Void> dumpMgr,
41                                    @Nonnull final NamingContext ifcContext) {
42         this.dumpMgr = dumpMgr;
43         this.ifcContext = ifcContext;
44     }
45
46     @Override
47     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id,
48                                       @Nonnull final B builder,
49                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
50         // NOOP
51     }
52
53     @Override
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());
58
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);
62
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)
67                 .findFirst()
68                 .isPresent();
69         // Not setting data, just marking the builder to propagate empty container to indicate presence
70     }
71
72     protected abstract Logger getLog();
73
74     abstract boolean isExpectedNatType(final SnatInterfaceDetails snatInterfaceDetails);
75 }