66ddf48db93f3a559edc3c432bafcf5a699368dd
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2018 Bell Canada, Pantheon Technologies 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.srv6.util.function;
18
19 import static org.mockito.Mockito.when;
20
21 import com.google.common.base.Optional;
22 import io.fd.hc2vpp.fib.management.FibManagementIIds;
23 import io.fd.hc2vpp.srv6.util.JvppRequestTest;
24 import io.fd.hc2vpp.srv6.write.sid.request.LocalSidFunctionRequest;
25 import io.fd.hc2vpp.srv6.write.sid.request.NoProtocolLocalSidRequest;
26 import io.fd.hc2vpp.srv6.write.sid.request.TableLookupLocalSidRequest;
27 import io.fd.hc2vpp.srv6.write.sid.request.XConnectLocalSidRequest;
28 import io.fd.honeycomb.translate.read.ReadContext;
29 import java.util.Collections;
30 import org.junit.Assert;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.multi.paths.v6.PathsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.multi.paths.v6.paths.PathBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6._static.cfg.Sid;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6._static.cfg.SidBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.End;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndBuilder;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDt4;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDt4Builder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDt6;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDt6Builder;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx2;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx2Builder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx4;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx4Builder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx6;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndDx6Builder;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndT;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndTBuilder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndX;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.EndXBuilder;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.base.rev180301.PathAttrsCmn;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDT4;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDT6;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDX2;
62 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDX4;
63 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndDX6;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.TableId;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv4;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv6;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.VniReference;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.Table;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableKey;
71
72 public class LocalSidFunctionBindingRegistryTest extends JvppRequestTest {
73
74     private static final Ipv6Address A = new Ipv6Address("A::101");
75     private static final Ipv6AddressNoZone A_NO_ZONE = new Ipv6AddressNoZone("a::101");
76     private static final Ipv4Address V4HOP = new Ipv4Address("0.0.1.0");
77     private static final Ipv4AddressNoZone A_V4 = new Ipv4AddressNoZone("10.0.0.1");
78     private static final String LOCAL_0 = "local0";
79     private static final TableId TABLE_ID_4 = new TableId(4L);
80     private static final TableKey TABLE_4_IPV6_KEY = new TableKey(Ipv6.class, new VniReference(TABLE_ID_4.getValue()));
81     private static final TableKey TABLE_4_IPV4_KEY = new TableKey(Ipv4.class, new VniReference(TABLE_ID_4.getValue()));
82
83     @Mock
84     private ReadContext readCtx;
85
86     @Override
87     protected void init() {
88         MockitoAnnotations.initMocks(this);
89         defineMapping(mappingContext, "local0", 1, "interface-context");
90         defineMapping(mappingContext, "vlan0", 2, "interface-context");
91         when(ctx.getMappingContext()).thenReturn(mappingContext);
92         when(readCtx.getMappingContext()).thenReturn(mappingContext);
93         when(ctx.readAfter(FibManagementIIds.FM_FIB_TABLES.child(Table.class, TABLE_4_IPV6_KEY)))
94                 .thenReturn(Optional.of(
95                         new TableBuilder().setTableId(TABLE_4_IPV6_KEY.getTableId()).setKey(TABLE_4_IPV6_KEY)
96                                 .setAddressFamily(TABLE_4_IPV6_KEY.getAddressFamily()).build()));
97         when(ctx.readAfter(FibManagementIIds.FM_FIB_TABLES.child(Table.class, TABLE_4_IPV4_KEY)))
98                 .thenReturn(Optional.of(
99                         new TableBuilder().setTableId(TABLE_4_IPV4_KEY.getTableId()).setKey(TABLE_4_IPV4_KEY)
100                                 .setAddressFamily(TABLE_4_IPV4_KEY.getAddressFamily()).build()));
101     }
102
103     @Test
104     public void testEnd() {
105         End end = new EndBuilder().build();
106         Sid localSid = new SidBuilder()
107                 .setEnd(end)
108                 .setEndBehaviorType(
109                         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.End.class)
110                 .build();
111         LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
112         Assert.assertTrue(request instanceof NoProtocolLocalSidRequest);
113         Assert.assertEquals(1, request.getFunction());
114     }
115
116     @Test
117     public void testEndX() {
118         EndX endX = new EndXBuilder()
119                 .setPaths(new PathsBuilder().setPath(Collections.singletonList(
120                         new PathBuilder().setRole(PathAttrsCmn.Role.PRIMARY)
121                                 .setWeight(1L)
122                                 .setPathIndex((short) 1)
123                                 .setInterface(LOCAL_0)
124                                 .setNextHop(A)
125                                 .build())).build())
126                 .build();
127         Sid localSid = new SidBuilder()
128                 .setEndX(endX)
129                 .setEndBehaviorType(
130                         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndX.class)
131                 .build();
132         LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
133         Assert.assertTrue(request instanceof XConnectLocalSidRequest);
134         Assert.assertEquals(2, request.getFunction());
135         XConnectLocalSidRequest xConnectRequest = XConnectLocalSidRequest.class.cast(request);
136         Assert.assertEquals(A, xConnectRequest.getNextHopAddress().getIpv6Address());
137         Assert.assertEquals(1, xConnectRequest.getOutgoingInterfaceIndex());
138     }
139
140     @Test
141     public void testEndDX2() {
142         EndDx2 endDx2 = new EndDx2Builder().setPaths(
143                 new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.srv6.sid.config.end.dx2.PathsBuilder()
144                         .setInterface(LOCAL_0)
145                         .build()).build();
146         Sid localSid = new SidBuilder()
147                 .setEndDx2(endDx2)
148                 .setEndBehaviorType(EndDX2.class)
149                 .build();
150         LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
151         Assert.assertTrue(request instanceof XConnectLocalSidRequest);
152         Assert.assertEquals(5, request.getFunction());
153         XConnectLocalSidRequest xConnectRequest = XConnectLocalSidRequest.class.cast(request);
154         Assert.assertNull(xConnectRequest.getNextHopAddress());
155         Assert.assertEquals(1, xConnectRequest.getOutgoingInterfaceIndex());
156     }
157
158     @Test
159     public void testEndDX6() {
160         EndDx6 endDx6 = new EndDx6Builder()
161                 .setPaths(new PathsBuilder()
162                         .setPath(Collections.singletonList(new PathBuilder()
163                                 .setNextHop(A)
164                                 .setInterface(LOCAL_0)
165                                 .build()))
166                         .build())
167                 .build();
168         Sid localSid = new SidBuilder()
169                 .setEndDx6(endDx6)
170                 .setEndBehaviorType(EndDX6.class)
171                 .build();
172         LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
173         Assert.assertTrue(request instanceof XConnectLocalSidRequest);
174         Assert.assertEquals(6, request.getFunction());
175         XConnectLocalSidRequest xConnectRequest = XConnectLocalSidRequest.class.cast(request);
176         Assert.assertEquals(A, xConnectRequest.getNextHopAddress().getIpv6Address());
177         Assert.assertEquals(1, xConnectRequest.getOutgoingInterfaceIndex());
178     }
179
180     @Test
181     public void testEndDX4() {
182         EndDx4 endDx4 = new EndDx4Builder()
183                 .setPaths(
184                         new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.multi.paths.v4.PathsBuilder()
185                                 .setPath(Collections.singletonList(
186                                         new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6._static.rev180301.multi.paths.v4.paths.PathBuilder()
187                                                 .setNextHop(V4HOP)
188                                                 .setInterface(LOCAL_0)
189                                                 .build()))
190                                 .build())
191                 .build();
192         Sid localSid = new SidBuilder()
193                 .setEndDx4(endDx4)
194                 .setEndBehaviorType(EndDX4.class)
195                 .build();
196         LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
197         Assert.assertTrue(request instanceof XConnectLocalSidRequest);
198         Assert.assertEquals(7, request.getFunction());
199         XConnectLocalSidRequest xConnectRequest = XConnectLocalSidRequest.class.cast(request);
200         Assert.assertEquals(V4HOP, xConnectRequest.getNextHopAddress().getIpv4Address());
201         Assert.assertEquals(1, xConnectRequest.getOutgoingInterfaceIndex());
202     }
203
204     @Test
205     public void testEndT() {
206         EndT endT = new EndTBuilder().setLookupTableIpv6(TABLE_ID_4).build();
207         Sid localSid = new SidBuilder()
208                 .setEndT(endT)
209                 .setEndBehaviorType(
210                         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.srv6.types.rev180301.EndT.class)
211                 .build();
212         LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
213         Assert.assertTrue(request instanceof TableLookupLocalSidRequest);
214         Assert.assertEquals(3, request.getFunction());
215         TableLookupLocalSidRequest tableLookupRequest = TableLookupLocalSidRequest.class.cast(request);
216         Assert.assertEquals(TABLE_ID_4.getValue().intValue(), tableLookupRequest.getLookupFibTable());
217     }
218
219     @Test
220     public void testEndDT6() {
221         EndDt6 endDt6 = new EndDt6Builder().setLookupTableIpv6(TABLE_ID_4).build();
222         Sid localSid = new SidBuilder()
223                 .setEndDt6(endDt6)
224                 .setEndBehaviorType(EndDT6.class)
225                 .build();
226         LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
227         Assert.assertTrue(request instanceof TableLookupLocalSidRequest);
228         Assert.assertEquals(8, request.getFunction());
229         TableLookupLocalSidRequest tableLookupRequest = TableLookupLocalSidRequest.class.cast(request);
230         Assert.assertEquals(TABLE_ID_4.getValue().intValue(), tableLookupRequest.getLookupFibTable());
231     }
232
233     @Test
234     public void testEndDT4() {
235         EndDt4 endDt4 = new EndDt4Builder().setLookupTableIpv4(TABLE_ID_4).build();
236         Sid localSid = new SidBuilder()
237                 .setEndDt4(endDt4)
238                 .setEndBehaviorType(EndDT4.class)
239                 .build();
240         LocalSidFunctionRequest request = WRITE_REGISTRY.bind(localSid, ctx);
241         Assert.assertTrue(request instanceof TableLookupLocalSidRequest);
242         Assert.assertEquals(9, request.getFunction());
243         TableLookupLocalSidRequest tableLookupRequest = TableLookupLocalSidRequest.class.cast(request);
244         Assert.assertEquals(TABLE_ID_4.getValue().intValue(), tableLookupRequest.getLookupFibTable());
245     }
246 }