479e4db202870fda754dfb6b163e254305503ddc
[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.http.fd.io.hc2vpp.yang.gpe.rev170801.NativeForwardPathsTables;
35 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTable;
36 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableBuilder;
37 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.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         final NativeForwardPathsTable before = validTableBefore();
83         final NativeForwardPathsTable after = validTable();
84         // emulates what default update would do
85         customizer.deleteCurrentAttributes(validId, before, writeContext);
86         customizer.writeCurrentAttributes(validId, after, writeContext);
87         verify(api, times(2)).gpeAddDelIface(requestCaptor.capture());
88
89         final List<GpeAddDelIface> requests = requestCaptor.getAllValues();
90
91         // removes one from old data
92         assertEquals(desiredRequest(0, 1), requests.get(0));
93
94         // defines 3 new
95         assertEquals(desiredRequest(1, 1), requests.get(1));
96     }
97
98
99     private GpeAddDelIface desiredRequest(final int isAdd, final int tableId) {
100         GpeAddDelIface request = new GpeAddDelIface();
101
102         request.isL2 = 0;
103         request.dpTable = tableId;
104         request.vni = request.dpTable;
105         request.isAdd = (byte) isAdd;
106         return request;
107     }
108
109     private NativeForwardPathsTable validTableBefore() {
110         return new NativeForwardPathsTableBuilder()
111                 .setTableId(TABLE_ID)
112                 .build();
113     }
114
115     private NativeForwardPathsTable validTable() {
116         return new NativeForwardPathsTableBuilder()
117                 .setTableId(TABLE_ID)
118                 .build();
119     }
120 }