3cef1880ef7d6a8784b402161c282fd637e6d6d5
[honeycomb.git] / nat / nat2vpp / src / test / java / io / fd / honeycomb / nat / read / ifc / InterfaceInboundNatCustomizerTest.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 static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21
22 import com.google.common.collect.Lists;
23 import io.fd.honeycomb.translate.impl.read.GenericReader;
24 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
25 import io.fd.honeycomb.translate.util.RWUtils;
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.NamingContext;
29 import io.fd.honeycomb.vpp.test.read.ReaderCustomizerTest;
30 import io.fd.vpp.jvpp.snat.dto.SnatInterfaceDetails;
31 import io.fd.vpp.jvpp.snat.dto.SnatInterfaceDetailsReplyDump;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceStateAugmentation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.Nat;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.NatBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Inbound;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.InboundBuilder;
43 import org.opendaylight.yangtools.yang.binding.ChildOf;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46 public class InterfaceInboundNatCustomizerTest
47         extends ReaderCustomizerTest<Inbound, InboundBuilder> {
48
49     private static final String IFC_NAME = "a";
50     private static final int IFC_IDX = 0;
51     private static final String CTX_NAME = "ifc";
52
53     @Mock
54     private EntityDumpExecutor<SnatInterfaceDetailsReplyDump, Void> natExecutor;
55     private DumpCacheManager<SnatInterfaceDetailsReplyDump, Void> dumpMgr;
56     private NamingContext ifcContext = new NamingContext(CTX_NAME, CTX_NAME);
57     private InstanceIdentifier<Inbound> id;
58
59     public InterfaceInboundNatCustomizerTest() {
60         super(Inbound.class, NatBuilder.class);
61     }
62
63     @Override
64     protected void setUp() throws Exception {
65         id = getId(Inbound.class);
66         defineMapping(mappingContext, IFC_NAME, IFC_IDX, CTX_NAME);
67         // empty dump
68         Mockito.doReturn(new SnatInterfaceDetailsReplyDump()).when(natExecutor).executeDump(id, null);
69         dumpMgr = new DumpCacheManager.DumpCacheManagerBuilder<SnatInterfaceDetailsReplyDump, Void>()
70                 .withExecutor(natExecutor)
71                 .build();
72     }
73
74     static <T extends ChildOf<Nat>> InstanceIdentifier<T> getId(Class<T> boundType) {
75         return InstanceIdentifier.create(InterfacesState.class)
76                 .child(Interface.class, new InterfaceKey(IFC_NAME))
77                 .augmentation(NatInterfaceStateAugmentation.class)
78                 .child(Nat.class)
79                 .child(boundType);
80     }
81
82     @Test
83     public void testNoPresence() throws Exception {
84         assertFalse(getReader().read(id, ctx).isPresent());
85     }
86
87     private GenericReader<Inbound, InboundBuilder> getReader() {
88         return new GenericReader<>(RWUtils.makeIidWildcarded(id), customizer);
89     }
90
91     @Test
92     public void testPresence() throws Exception {
93         final SnatInterfaceDetailsReplyDump details = new SnatInterfaceDetailsReplyDump();
94         final SnatInterfaceDetails detail = new SnatInterfaceDetails();
95         detail.isInside = 1;
96         detail.swIfIndex = IFC_IDX;
97         details.snatInterfaceDetails = Lists.newArrayList(detail);
98         Mockito.doReturn(details).when(natExecutor).executeDump(id, null);
99
100         assertTrue(getReader().read(id, ctx).isPresent());
101     }
102
103     @Override
104     protected ReaderCustomizer<Inbound, InboundBuilder> initCustomizer() {
105         return new InterfaceInboundNatCustomizer(dumpMgr, ifcContext);
106     }
107 }