30028143087ee69bb04c9901d0634ec6cbb30dff
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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.hc2vpp.routing.write;
18
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
25 import io.fd.hc2vpp.common.translate.util.NamingContext;
26 import io.fd.hc2vpp.routing.helpers.SchemaContextTestHelper;
27 import io.fd.honeycomb.test.tools.HoneycombTestRunner;
28 import io.fd.honeycomb.test.tools.annotations.InjectTestData;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import io.fd.jvpp.core.dto.SwInterfaceIp6NdRaConfig;
31 import io.fd.jvpp.core.dto.SwInterfaceIp6NdRaConfigReply;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv6;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev180313.Ipv61;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev180313.interfaces._interface.ipv6.Ipv6RouterAdvertisements;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42
43 @RunWith(HoneycombTestRunner.class)
44 public class RouterAdvertisementsCustomizerTest extends WriterCustomizerTest implements SchemaContextTestHelper {
45
46     private static final String CTX_NAME = "interface-context";
47     private static final String IFC_NAME = "eth0";
48     private static final int IFC_INDEX = 1;
49     private static final InstanceIdentifier<Ipv6RouterAdvertisements> IID = InstanceIdentifier
50         .create(Interfaces.class)
51         .child(Interface.class, new InterfaceKey(IFC_NAME))
52         .augmentation(Interface1.class)
53         .child(Ipv6.class)
54         .augmentation(Ipv61.class)
55         .child(Ipv6RouterAdvertisements.class);
56
57     private static final String RA_PATH = "/ietf-interfaces:interfaces";
58
59     private RouterAdvertisementsCustomizer customizer;
60     private NamingContext interfaceContext = new NamingContext("ifaces", CTX_NAME);
61
62
63     @Override
64     protected void setUpTest() {
65         customizer = new RouterAdvertisementsCustomizer(api, interfaceContext);
66         defineMapping(mappingContext, IFC_NAME, IFC_INDEX, CTX_NAME);
67         when(api.swInterfaceIp6NdRaConfig(any())).thenReturn(future(new SwInterfaceIp6NdRaConfigReply()));
68     }
69
70     @Test
71     public void testWrite(@InjectTestData(resourcePath = "/ra/simpleRa.json", id = RA_PATH) Interfaces ifc)
72         throws WriteFailedException {
73         final Ipv6RouterAdvertisements data = getRA(ifc);
74         customizer.writeCurrentAttributes(IID, data, writeContext);
75         final SwInterfaceIp6NdRaConfig request = new SwInterfaceIp6NdRaConfig();
76         request.swIfIndex = IFC_INDEX;
77         request.maxInterval = 600;
78         request.managed = 1;
79         verify(api).swInterfaceIp6NdRaConfig(request);
80     }
81
82     @Test
83     public void testUpdate(@InjectTestData(resourcePath = "/ra/complexRa.json", id = RA_PATH) Interfaces ifc)
84         throws WriteFailedException {
85         final Ipv6RouterAdvertisements data = getRA(ifc);
86         customizer.updateCurrentAttributes(IID, mock(Ipv6RouterAdvertisements.class), data, writeContext);
87         final SwInterfaceIp6NdRaConfig request = new SwInterfaceIp6NdRaConfig();
88         request.swIfIndex = IFC_INDEX;
89         request.initialCount = 2;
90         request.initialInterval = 15;
91         request.maxInterval = 100;
92         request.minInterval = 20;
93         request.lifetime = 601;
94         request.defaultRouter = 1;
95         verify(api).swInterfaceIp6NdRaConfig(request);
96     }
97
98     @Test
99     public void testDelete() throws WriteFailedException {
100         customizer.deleteCurrentAttributes(IID, mock(Ipv6RouterAdvertisements.class), writeContext);
101         final SwInterfaceIp6NdRaConfig request = new SwInterfaceIp6NdRaConfig();
102         request.swIfIndex = IFC_INDEX;
103         request.suppress = 1;
104         verify(api).swInterfaceIp6NdRaConfig(request);
105     }
106
107     private static Ipv6RouterAdvertisements getRA(final Interfaces ifc) {
108         return ifc.getInterface()
109             .get(0)
110             .augmentation(Interface1.class)
111             .getIpv6()
112             .augmentation(Ipv61.class)
113             .getIpv6RouterAdvertisements();
114     }
115 }