80bb2c6004e138aa6d7458d7075059cbd8bc795c
[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.lisp.gpe.translate.write;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24
25 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.vpp.jvpp.core.dto.GpeAddDelIface;
28 import io.fd.vpp.jvpp.core.dto.GpeAddDelIfaceReply;
29 import io.fd.vpp.jvpp.core.dto.GpeAddDelNativeFwdRpathReply;
30 import java.util.List;
31 import org.junit.Test;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.Captor;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801.NativeForwardPathsTables;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTable;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 public class NativeForwardPathsTableCustomizerTest extends WriterCustomizerTest {
41
42     private static final long TABLE_ID = 1L;
43     private NativeForwardPathsTableCustomizer customizer;
44     private InstanceIdentifier<NativeForwardPathsTable> validId;
45
46     @Captor
47     private ArgumentCaptor<GpeAddDelIface> requestCaptor;
48
49     @Override
50     protected void setUpTest() throws Exception {
51         customizer = new NativeForwardPathsTableCustomizer(api);
52         validId = InstanceIdentifier.create(NativeForwardPathsTables.class)
53                 .child(NativeForwardPathsTable.class, new NativeForwardPathsTableKey(TABLE_ID));
54         when(api.gpeAddDelIface(any())).thenReturn(future(new GpeAddDelIfaceReply()));
55     }
56
57     @Test
58     public void testWriteValid() throws WriteFailedException {
59         when(api.gpeAddDelNativeFwdRpath(any())).thenReturn(future(new GpeAddDelNativeFwdRpathReply()));
60         customizer.writeCurrentAttributes(validId, validTable(), writeContext);
61         verify(api, times(1)).gpeAddDelIface(requestCaptor.capture());
62
63         final List<GpeAddDelIface> requests = requestCaptor.getAllValues();
64
65         assertEquals(desiredRequest(1, 1), requests.get(0));
66     }
67
68     @Test
69     public void testDeleteValid() throws WriteFailedException {
70         when(api.gpeAddDelNativeFwdRpath(any())).thenReturn(future(new GpeAddDelNativeFwdRpathReply()));
71         customizer.deleteCurrentAttributes(validId, validTable(), writeContext);
72         verify(api, times(1)).gpeAddDelIface(requestCaptor.capture());
73
74         final List<GpeAddDelIface> requests = requestCaptor.getAllValues();
75
76         assertEquals(desiredRequest(0, 1), requests.get(0));
77     }
78
79     @Test
80     public void testUpdateValid() throws WriteFailedException {
81         when(api.gpeAddDelNativeFwdRpath(any())).thenReturn(future(new GpeAddDelNativeFwdRpathReply()));
82         customizer.updateCurrentAttributes(validId, validTableBefore(), validTable(), writeContext);
83         verify(api, times(2)).gpeAddDelIface(requestCaptor.capture());
84
85         final List<GpeAddDelIface> requests = requestCaptor.getAllValues();
86
87         // removes one from old data
88         assertEquals(desiredRequest(0, 1), requests.get(0));
89
90         // defines 3 new
91         assertEquals(desiredRequest(1, 1), requests.get(1));
92     }
93
94
95     private GpeAddDelIface desiredRequest(final int isAdd, final int tableId) {
96         GpeAddDelIface request = new GpeAddDelIface();
97
98         request.isL2 = 0;
99         request.dpTable = tableId;
100         request.vni = request.dpTable;
101         request.isAdd = (byte) isAdd;
102         return request;
103     }
104
105     private NativeForwardPathsTable validTableBefore() {
106         return new NativeForwardPathsTableBuilder()
107                 .setTableId(TABLE_ID)
108                 .build();
109     }
110
111     private NativeForwardPathsTable validTable() {
112         return new NativeForwardPathsTableBuilder()
113                 .setTableId(TABLE_ID)
114                 .build();
115     }
116 }