dd93de59a830d7e2db1bd609d40364dddc794e1c
[hc2vpp.git] /
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.hc2vpp.routing.write.factory;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.SpecialNextHopGrouping.SpecialNextHop.Prohibit;
21 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.SpecialNextHopGrouping.SpecialNextHop.Receive;
22 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.SpecialNextHopGrouping.SpecialNextHop.Unreachable;
23
24 import io.fd.hc2vpp.routing.Ipv4RouteData;
25 import io.fd.hc2vpp.routing.helpers.ClassifyTableTestHelper;
26 import io.fd.hc2vpp.routing.helpers.RoutingRequestTestHelper;
27 import io.fd.hc2vpp.routing.helpers.SchemaContextTestHelper;
28 import io.fd.hc2vpp.routing.write.trait.RouteRequestProducer;
29 import io.fd.hc2vpp.v3po.vppclassifier.VppClassifierContextManager;
30 import io.fd.honeycomb.test.tools.HoneycombTestRunner;
31 import io.fd.honeycomb.test.tools.annotations.InjectTestData;
32 import io.fd.honeycomb.translate.MappingContext;
33 import io.fd.honeycomb.translate.util.RWUtils;
34 import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv4.unicast.routing.rev140524.StaticRoutes1;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv4.unicast.routing.rev140524.routing.routing.instance.routing.protocols.routing.protocol._static.routes.ipv4.Route;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.SpecialNextHopGrouping;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.routing.routing.instance.routing.protocols.routing.protocol.StaticRoutes;
44
45 @RunWith(HoneycombTestRunner.class)
46 public class SpecialNextHopRequestFactoryIpv4Test
47         implements RouteRequestProducer, RoutingRequestTestHelper, ClassifyTableTestHelper,
48         SchemaContextTestHelper {
49
50     @Mock
51     private VppClassifierContextManager classifierContextManager;
52
53     @Mock
54     private MappingContext mappingContext;
55
56     private SpecialNextHopRequestFactory factory;
57
58     @Before
59     public void init() {
60         MockitoAnnotations.initMocks(this);
61         factory = SpecialNextHopRequestFactory.forClassifierContext(classifierContextManager);
62
63         addMapping(classifierContextManager, CLASSIFY_TABLE_NAME, CLASSIFY_TABLE_INDEX, mappingContext);
64     }
65
66     @Test
67     public void testIpv4WithClassifierBlackhole(
68             @InjectTestData(resourcePath = "/ipv4/specialHopRouteBlackhole.json", id = STATIC_ROUTE_PATH)
69                     StaticRoutes routes) {
70         final IpAddDelRoute request =
71                 factory.createIpv4SpecialHopRequest(true, extractSingleRoute(routes, 1L), mappingContext,
72                         SpecialNextHopGrouping.SpecialNextHop.Blackhole);
73
74         assertEquals(desiredSpecialResult(1, 0, Ipv4RouteData.FIRST_ADDRESS_AS_ARRAY, 24, 1, 0, 0, 0), request);
75     }
76
77     @Test
78     public void testIpv4WithClassifierReceive(
79             @InjectTestData(resourcePath = "/ipv4/specialHopRouteReceive.json", id = STATIC_ROUTE_PATH)
80                     StaticRoutes routes) {
81         final IpAddDelRoute request =
82                 factory.createIpv4SpecialHopRequest(true, extractSingleRoute(routes, 1L), mappingContext, Receive);
83
84         assertEquals(desiredSpecialResult(1, 0, Ipv4RouteData.FIRST_ADDRESS_AS_ARRAY, 24, 0, 1, 0, 0), request);
85     }
86
87     @Test
88     public void testIpv4WithClassifierUnreach(
89             @InjectTestData(resourcePath = "/ipv4/specialHopRouteUnreachable.json", id = STATIC_ROUTE_PATH)
90                     StaticRoutes routes) {
91         final IpAddDelRoute request =
92                 factory.createIpv4SpecialHopRequest(true, extractSingleRoute(routes, 1L), mappingContext, Unreachable);
93
94         assertEquals(desiredSpecialResult(1, 0, Ipv4RouteData.FIRST_ADDRESS_AS_ARRAY, 24, 0, 0, 1, 0), request);
95     }
96
97     @Test
98     public void testIpv4WithClassifierProhibited(
99             @InjectTestData(resourcePath = "/ipv4/specialHopRouteProhibited.json", id = STATIC_ROUTE_PATH)
100                     StaticRoutes routes) {
101         final IpAddDelRoute request =
102                 factory.createIpv4SpecialHopRequest(true, extractSingleRoute(routes, 1L), mappingContext, Prohibit);
103
104         assertEquals(desiredSpecialResult(1, 0, Ipv4RouteData.FIRST_ADDRESS_AS_ARRAY, 24, 0, 0, 0, 1), request);
105     }
106
107     private Route extractSingleRoute(final StaticRoutes staticRoutes, final long id) {
108         return staticRoutes.getAugmentation(StaticRoutes1.class).getIpv4().getRoute().stream()
109                 .filter(route -> route.getId() == id)
110                 .collect(RWUtils.singleItemCollector());
111     }
112 }