761986fc18161b07a4baee3aea73471782b8aa87
[honeycomb.git] / nat / nat2vpp / src / main / java / io / fd / honeycomb / nat / read / ifc / IfcNatReaderFactory.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
20 import com.google.inject.Inject;
21 import com.google.inject.name.Named;
22 import io.fd.honeycomb.translate.impl.read.GenericInitReader;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.read.ReaderFactory;
25 import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
26 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
27 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
28 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
29 import io.fd.honeycomb.translate.vpp.util.NamingContext;
30 import io.fd.vpp.jvpp.snat.dto.SnatInterfaceDetailsReplyDump;
31 import io.fd.vpp.jvpp.snat.dto.SnatInterfaceDump;
32 import io.fd.vpp.jvpp.snat.future.FutureJVppSnatFacade;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceStateAugmentation;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceStateAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.Nat;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.NatBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Inbound;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Outbound;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43
44 /**
45  * Factory producing readers for nat plugin's data.
46  */
47 public final class IfcNatReaderFactory implements ReaderFactory {
48
49     private static final InstanceIdentifier<Interface>
50             IFC_ID = InstanceIdentifier.create(InterfacesState.class).child(Interface.class);
51     private static final InstanceIdentifier<NatInterfaceStateAugmentation> NAT_AUG_ID =
52             IFC_ID.augmentation(NatInterfaceStateAugmentation.class);
53     private static final InstanceIdentifier<Nat> NAT_AUG_CONTAINER_ID = NAT_AUG_ID.child(Nat.class);
54
55     private final DumpCacheManager<SnatInterfaceDetailsReplyDump, Void> snatIfcDumpMgr;
56     private final NamingContext ifcContext;
57
58     @Inject
59     public IfcNatReaderFactory(final FutureJVppSnatFacade jvppSnat,
60                                @Named("interface-context") final NamingContext ifcContext) {
61         this.snatIfcDumpMgr = new DumpCacheManager.DumpCacheManagerBuilder<SnatInterfaceDetailsReplyDump, Void>()
62                 .withExecutor(new SnatInterfaceExecutor(jvppSnat))
63                 .build();
64         this.ifcContext = ifcContext;
65     }
66
67     @Override
68     public void init(@Nonnull final ModifiableReaderRegistryBuilder registry) {
69         registry.addStructuralReader(NAT_AUG_ID, NatInterfaceStateAugmentationBuilder.class);
70         registry.addStructuralReader(NAT_AUG_CONTAINER_ID, NatBuilder.class);
71
72         registry.addAfter(new GenericInitReader<>(NAT_AUG_CONTAINER_ID.child(Inbound.class),
73                         new InterfaceInboundNatCustomizer(snatIfcDumpMgr, ifcContext)), IFC_ID);
74         registry.addAfter(new GenericInitReader<>(NAT_AUG_CONTAINER_ID.child(Outbound.class),
75                         new InterfaceOutboundNatCustomizer(snatIfcDumpMgr, ifcContext)), IFC_ID);
76     }
77
78     private static final class SnatInterfaceExecutor implements
79             EntityDumpExecutor<SnatInterfaceDetailsReplyDump, Void>,
80             JvppReplyConsumer {
81
82         private final FutureJVppSnatFacade jvppSnat;
83
84         SnatInterfaceExecutor(final FutureJVppSnatFacade jvppSnat) {
85             this.jvppSnat = jvppSnat;
86         }
87
88         @Nonnull
89         @Override
90         public SnatInterfaceDetailsReplyDump executeDump(final InstanceIdentifier<?> identifier, final Void params)
91                 throws ReadFailedException {
92             return getReplyForRead(
93                     jvppSnat.snatInterfaceDump(new SnatInterfaceDump()).toCompletableFuture(), identifier);
94         }
95     }
96 }