HONEYCOMB-58 - Routing Api
[honeycomb.git] / v3po / v3po2vpp / src / test / java / io / fd / honeycomb / translate / v3po / interfaces / InterfaceCustomizerTest.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.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.verifyZeroInteractions;
25 import static org.mockito.Mockito.when;
26
27 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
28 import io.fd.honeycomb.translate.vpp.util.NamingContext;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import io.fd.honeycomb.vpp.test.write.WriterCustomizerTest;
31 import org.junit.Test;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import io.fd.vpp.jvpp.VppBaseCallException;
38 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlags;
39 import io.fd.vpp.jvpp.core.dto.SwInterfaceSetFlagsReply;
40
41 public class InterfaceCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator {
42     private static final String IFACE_CTX_NAME = "interface-ctx";
43     private static final String IF_NAME = "eth1";
44     private static final int IF_INDEX = 1;
45
46     private InterfaceCustomizer customizer;
47     private InstanceIdentifier<Interface> IID =
48         InstanceIdentifier.create(Interfaces.class).child(Interface.class, new InterfaceKey(IF_NAME));
49
50     @Override
51     protected void setUp() throws Exception {
52         customizer = new InterfaceCustomizer(api, new NamingContext("ifacePrefix", IFACE_CTX_NAME));
53         defineMapping(mappingContext, IF_NAME, IF_INDEX, IFACE_CTX_NAME);
54     }
55
56     @Test
57     public void testWrite() throws WriteFailedException {
58         final boolean enabled = true;
59         when(api.swInterfaceSetFlags(any())).thenReturn(future(new SwInterfaceSetFlagsReply()));
60         customizer.writeCurrentAttributes(IID, iface(enabled), writeContext);
61         verify(api).swInterfaceSetFlags(expectedRequest(enabled));
62     }
63
64     @Test
65     public void testWriteFailed() {
66         final boolean enabled = false;
67         when(api.swInterfaceSetFlags(any())).thenReturn(failedFuture());
68         try {
69             customizer.writeCurrentAttributes(IID, iface(enabled), writeContext);
70         } catch (WriteFailedException e) {
71             assertTrue(e.getCause() instanceof VppBaseCallException);
72             verify(api).swInterfaceSetFlags(expectedRequest(enabled));
73             return;
74         }
75         fail("WriteFailedException expected");
76     }
77
78     @Test
79     public void testUpdate() throws WriteFailedException {
80         when(api.swInterfaceSetFlags(any())).thenReturn(future(new SwInterfaceSetFlagsReply()));
81         customizer.updateCurrentAttributes(IID, iface(false), iface(true), writeContext);
82         verify(api).swInterfaceSetFlags(expectedRequest(true));
83     }
84
85     @Test
86     public void testUpdateFailed() {
87         when(api.swInterfaceSetFlags(any())).thenReturn(failedFuture());
88         try {
89             customizer.updateCurrentAttributes(IID, iface(false), iface(true), writeContext);
90         } catch (WriteFailedException e) {
91             assertTrue(e.getCause() instanceof VppBaseCallException);
92             verify(api).swInterfaceSetFlags(expectedRequest(true));
93             return;
94         }
95         fail("WriteFailedException expected");
96     }
97
98     @Test
99     public void testDelete() throws WriteFailedException {
100         customizer.deleteCurrentAttributes(IID, mock(Interface.class), writeContext);
101         verifyZeroInteractions(api);
102     }
103
104     private Interface iface(final boolean enabled) {
105         return new InterfaceBuilder().setName(IF_NAME).setEnabled(enabled).build();
106     }
107
108     private SwInterfaceSetFlags expectedRequest(final boolean enabled) {
109         final SwInterfaceSetFlags request = new SwInterfaceSetFlags();
110         request.deleted = 0;
111         request.adminUpDown = booleanToByte(enabled);
112         request.linkUpDown = booleanToByte(enabled);
113         request.swIfIndex = IF_INDEX;
114         return request;
115     }
116 }