d63231a5bca72256a4e39bd3095517008fd17f36
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / L2CustomizerTest.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.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21 import static org.mockito.Matchers.any;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
25 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
29 import org.junit.Test;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2Builder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.Interconnection;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBased;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBasedBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.XconnectBased;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.XconnectBasedBuilder;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import io.fd.vpp.jvpp.VppBaseCallException;
43 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetL2Bridge;
44 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetL2BridgeReply;
45 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetL2Xconnect;
46 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetL2XconnectReply;
47
48 public class L2CustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
49     private static final String IFACE_CTX_NAME = "interface-ctx";
50     private static final String BD_CTX_NAME = "bd-ctx";
51     private static final String IF1_NAME = "eth1";
52     private static final int IF1_INDEX = 1;
53     private static final String IF2_NAME = "eth2";
54     private static final int IF2_INDEX = 2;
55     private static final InstanceIdentifier<L2> IID =
56         InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF1_NAME))
57             .augmentation(VppInterfaceAugmentation.class).child(L2.class);
58     private static final String BD_NAME = "test_bd";
59     private static final int BD_INDEX = 13;
60
61     private L2Customizer customizer;
62
63     @Override
64     protected void setUp() throws Exception {
65         customizer = new L2Customizer(api, new NamingContext("ifacePrefix", IFACE_CTX_NAME),
66             new NamingContext("bdPrefix", BD_CTX_NAME));
67         defineMapping(mappingContext, IF1_NAME, IF1_INDEX, IFACE_CTX_NAME);
68         defineMapping(mappingContext, IF2_NAME, IF2_INDEX, IFACE_CTX_NAME);
69         defineMapping(mappingContext, BD_NAME, BD_INDEX, BD_CTX_NAME);
70     }
71
72     @Test
73     public void testWrite() throws WriteFailedException {
74         when(api.swInterfaceSetL2Xconnect(any())).thenReturn(future(new SwInterfaceSetL2XconnectReply()));
75         customizer.writeCurrentAttributes(IID, l2(xconnectBased()), writeContext);
76         verify(api).swInterfaceSetL2Xconnect(xconnectRequest(true));
77     }
78
79     @Test
80     public void testWriteFailed() {
81         when(api.swInterfaceSetL2Bridge(any())).thenReturn(failedFuture());
82         try {
83             customizer.writeCurrentAttributes(IID, l2(bridgeBased(false)), writeContext);
84         } catch (WriteFailedException e) {
85             assertTrue(e.getCause() instanceof VppBaseCallException);
86             verify(api).swInterfaceSetL2Bridge(bridgeRequest(false, true));
87             return;
88         }
89         fail("WriteFailedException expected");
90     }
91
92     @Test
93     public void testUpdate() throws WriteFailedException {
94         when(api.swInterfaceSetL2Bridge(any())).thenReturn(future(new SwInterfaceSetL2BridgeReply()));
95         customizer.updateCurrentAttributes(IID, l2(bridgeBased(false)), l2(bridgeBased(true)), writeContext);
96         verify(api).swInterfaceSetL2Bridge(bridgeRequest(true, true));
97     }
98
99     @Test
100     public void testDelete() throws WriteFailedException {
101         when(api.swInterfaceSetL2Xconnect(any())).thenReturn(future(new SwInterfaceSetL2XconnectReply()));
102         customizer.deleteCurrentAttributes(IID, l2(xconnectBased()), writeContext);
103         verify(api).swInterfaceSetL2Xconnect(xconnectRequest(false));
104     }
105
106     @Test
107     public void testDeleteFailed() {
108         when(api.swInterfaceSetL2Bridge(any())).thenReturn(failedFuture());
109         try {
110             customizer.deleteCurrentAttributes(IID, l2(bridgeBased(true)), writeContext);
111         } catch (WriteFailedException e) {
112             assertTrue(e.getCause() instanceof VppBaseCallException);
113             verify(api).swInterfaceSetL2Bridge(bridgeRequest(true, false));
114             return;
115         }
116         fail("WriteFailedException expected");
117     }
118
119     private XconnectBased xconnectBased() {
120         return new XconnectBasedBuilder().setXconnectOutgoingInterface(IF2_NAME).build();
121     }
122
123     private SwInterfaceSetL2Xconnect xconnectRequest(final boolean enable) {
124         final SwInterfaceSetL2Xconnect request = new SwInterfaceSetL2Xconnect();
125         request.rxSwIfIndex = IF1_INDEX;
126         request.txSwIfIndex = IF2_INDEX;
127         request.enable = booleanToByte(enable);
128         return request;
129     }
130
131     private BridgeBased bridgeBased(final boolean bvi) {
132         return new BridgeBasedBuilder().setBridgedVirtualInterface(bvi)
133             .setBridgeDomain(BD_NAME).setSplitHorizonGroup((short) 123).build();
134     }
135
136     private SwInterfaceSetL2Bridge bridgeRequest(final boolean bvi, final boolean enable) {
137         final SwInterfaceSetL2Bridge request = new SwInterfaceSetL2Bridge();
138         request.bdId = BD_INDEX;
139         request.rxSwIfIndex = IF1_INDEX;
140         request.bvi = booleanToByte(bvi);
141         request.enable = booleanToByte(enable);
142         request.shg = 123;
143         return request;
144     }
145
146
147     private L2 l2(final Interconnection interconnection) {
148         return new L2Builder().setInterconnection(interconnection).build();
149     }
150 }