HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / SubInterfaceL2CustomizerTest.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.translate.v3po.interfaces;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22
23 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
24 import io.fd.honeycomb.translate.vpp.util.NamingContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
27 import org.junit.Test;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBasedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.SubInterfaces;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterface;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.L2;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.L2Builder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetL2Bridge;
40 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetL2BridgeReply;
41
42 public class SubInterfaceL2CustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
43     private static final String IFACE_CTX_NAME = "interface-ctx";
44     private static final String IF_NAME = "local0";
45     private static final int IF_INDEX = 1;
46     private static final String SUBIF_NAME = "local0.0";
47     private static final int SUBIF_INDEX = 11;
48     private static final long SUBIF_ID = 0;
49
50     private static final InstanceIdentifier<L2> IID =
51         InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME)).augmentation(
52             SubinterfaceAugmentation.class).child(SubInterfaces.class)
53             .child(SubInterface.class, new SubInterfaceKey(SUBIF_ID)).child(L2.class);
54
55
56     private static final String BD_CTX_NAME = "bd-ctx";
57     private static final String BD_NAME = "test_bd";
58     private static final int BD_INDEX = 13;
59
60     private SubInterfaceL2Customizer customizer;
61
62     @Override
63     protected void setUp() throws Exception {
64         customizer = new SubInterfaceL2Customizer(api, new NamingContext("ifacePrefix", IFACE_CTX_NAME),
65             new NamingContext("bdPrefix", BD_CTX_NAME));
66         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFACE_CTX_NAME);
67         defineMapping(mappingContext, SUBIF_NAME, SUBIF_INDEX, IFACE_CTX_NAME);
68         defineMapping(mappingContext, BD_NAME, BD_INDEX, BD_CTX_NAME);
69     }
70
71     @Test
72     public void testWrite() throws WriteFailedException {
73         final boolean bvi = true;
74         when(api.swInterfaceSetL2Bridge(any())).thenReturn(future(new SwInterfaceSetL2BridgeReply()));
75         customizer.writeCurrentAttributes(IID, l2(bvi), writeContext);
76         verify(api).swInterfaceSetL2Bridge(bridgeRequest(bvi, true));
77     }
78
79     @Test
80     public void testUpdate() throws WriteFailedException {
81         final boolean bvi = false;
82         when(api.swInterfaceSetL2Bridge(any())).thenReturn(future(new SwInterfaceSetL2BridgeReply()));
83         customizer.updateCurrentAttributes(IID, l2(true), l2(bvi), writeContext);
84         verify(api).swInterfaceSetL2Bridge(bridgeRequest(bvi, true));
85     }
86
87     @Test
88     public void testDelete() throws WriteFailedException {
89         final boolean bvi = true;
90         when(api.swInterfaceSetL2Bridge(any())).thenReturn(future(new SwInterfaceSetL2BridgeReply()));
91         customizer.deleteCurrentAttributes(IID, l2(bvi), writeContext);
92         verify(api).swInterfaceSetL2Bridge(bridgeRequest(bvi, false));
93     }
94
95     private L2 l2(final boolean bvi) {
96         return new L2Builder().setInterconnection(new BridgeBasedBuilder().setBridgedVirtualInterface(bvi)
97             .setBridgeDomain(BD_NAME).setSplitHorizonGroup((short) 123).build()).build();
98     }
99
100     private SwInterfaceSetL2Bridge bridgeRequest(final boolean bvi, final boolean enable) {
101         final SwInterfaceSetL2Bridge request = new SwInterfaceSetL2Bridge();
102         request.bdId = BD_INDEX;
103         request.rxSwIfIndex = SUBIF_INDEX;
104         request.bvi = booleanToByte(bvi);
105         request.enable = booleanToByte(enable);
106         request.shg = 123;
107         return request;
108     }
109 }