summaryrefslogtreecommitdiffstats
path: root/routing/routing-impl/src/test/java/io/fd/hc2vpp/routing/helpers/RoutingRequestTestHelper.java
blob: 47f53494b66d8b090a50d8cbc040211f37befb54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.routing.helpers;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import io.fd.hc2vpp.common.test.util.FutureProducer;
import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.routing.trait.RouteMapper;
import io.fd.hc2vpp.routing.write.trait.RouteRequestProducer;
import io.fd.honeycomb.translate.util.RWUtils;
import io.fd.vpp.jvpp.core.dto.IpAddDelRoute;
import io.fd.vpp.jvpp.core.dto.IpAddDelRouteReply;
import io.fd.vpp.jvpp.core.future.FutureJVppCore;
import java.util.List;
import org.mockito.ArgumentCaptor;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv4.unicast.routing.rev140524.StaticRoutes1;
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;
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.next.hop.options.next.hop.list.next.hop.list.NextHop;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.routing.routing.instance.routing.protocols.routing.protocol.StaticRoutes;

public interface RoutingRequestTestHelper extends ByteDataTranslator, FutureProducer, RouteMapper {

    String ROUTE_PROTOCOL_NAME = "tst-protocol";
    String ROUTE_PROTOCOL_NAME_2 = "tst-protocol-2";
    String ROUTE_NAME = "tst-route";
    String STATIC_ROUTE_PATH = "/hc2vpp-ietf-routing:routing" +
            "/hc2vpp-ietf-routing:routing-instance[hc2vpp-ietf-routing:name='" + ROUTE_PROTOCOL_NAME + "']" +
            "/hc2vpp-ietf-routing:routing-protocols" +
            "/hc2vpp-ietf-routing:routing-protocol[hc2vpp-ietf-routing:name='" + ROUTE_NAME + "']" +
            "/hc2vpp-ietf-routing:static-routes";

    default IpAddDelRoute desiredFlaglessResult(final int add, final int ipv6, final int isMultipath,
                                                final byte[] destinationAddress,
                                                final int destinationPrefixLength,
                                                final byte[] nextHopAddr,
                                                final int nextHopIndex,
                                                final int nextHopWeight,
                                                final int vrfId,
                                                final int createVrfIfNeeded,
                                                final int secondaryVrfId,
                                                final int classifyTableIndex,
                                                final int classifyTableIndexSet) {

        // verification of flagless request, so setting them to 0
        return desiredResult(add, ipv6, isMultipath, destinationAddress, destinationPrefixLength, nextHopAddr,
                nextHopIndex, nextHopWeight, vrfId, createVrfIfNeeded, secondaryVrfId, classifyTableIndex,
                classifyTableIndexSet, 0, 0, 0, 0);
    }

    default IpAddDelRoute desiredSpecialResult(final int add, final int ipv6,
                                               final byte[] destinationAddress,
                                               final int destinationPrefixLength,
                                               final int isDrop,
                                               final int isReceive,
                                               final int isUnreach,
                                               final int isProhibit,
                                               final int protocolTableId,
                                               final int secondaryTableId) {
        // verifiaction of special request that has only destination address and flag
        return desiredResult(add, ipv6, 0, destinationAddress, destinationPrefixLength, null, 0, 0, protocolTableId, 1, secondaryTableId, 0, 0,
                isDrop, isReceive, isUnreach, isProhibit);
    }

    default IpAddDelRoute desiredResult(final int add, final int ipv6, final int isMultipath,
                                        final byte[] destinationAddress,
                                        final int destinationPrefixLength,
                                        final byte[] nextHopAddr,
                                        final int nextHopIndex,
                                        final int nextHopWeight,
                                        final int vrfId,
                                        final int createVrfIfNeeded,
                                        final int secondaryVrfId,
                                        final int classifyTableIndex,
                                        final int classifyTableIndexSet,
                                        final int isDrop,
                                        final int isLocal,
                                        final int isUnreach,
                                        final int isProhibit) {
        final IpAddDelRoute request = new IpAddDelRoute();

        request.isAdd = toByte(add);
        request.isIpv6 = toByte(ipv6);
        request.isMultipath = toByte(isMultipath);
        request.dstAddress = destinationAddress;
        request.dstAddressLength = toByte(destinationPrefixLength);
        request.nextHopAddress = nextHopAddr;
        request.nextHopSwIfIndex = nextHopIndex;
        request.nextHopWeight = toByte(nextHopWeight);
        request.classifyTableIndex = classifyTableIndexSet;
        request.tableId = vrfId;
        request.nextHopTableId = secondaryVrfId;
        request.createVrfIfNeeded = toByte(createVrfIfNeeded);
        request.classifyTableIndex = classifyTableIndex;
        request.isClassify = toByte(classifyTableIndexSet);
        // special hop flags
        request.isDrop = toByte(isDrop);
        request.isLocal = toByte(isLocal);
        request.isUnreach = toByte(isUnreach);
        request.isProhibit = toByte(isProhibit);
        request.nextHopViaLabel = RouteRequestProducer.MPLS_LABEL_INVALID;
        return request;
    }

    default void verifyInvocation(final int nrOfInvocations, final List<IpAddDelRoute> desiredRequests, final
    FutureJVppCore api, final ArgumentCaptor<IpAddDelRoute> requestCaptor) {
        verify(api, times(nrOfInvocations)).ipAddDelRoute(requestCaptor.capture());

        final List<IpAddDelRoute> actualRequests = requestCaptor.getAllValues();

        for (int i = 0; i < nrOfInvocations; i++) {
            assertEquals(actualRequests.get(i), desiredRequests.get(i));
        }
    }

    default void verifyNotInvoked(final FutureJVppCore api) {
        verify(api, times(0)).ipAddDelRoute(any());
    }

    default void whenAddRouteThenSuccess(final FutureJVppCore api) {
        when(api.ipAddDelRoute(any())).thenReturn(future(new IpAddDelRouteReply()));
    }

    default Route getIpv4RouteWithId(final StaticRoutes staticRoutes, final long id) {
        return staticRoutes.getAugmentation(StaticRoutes1.class)
                .getIpv4()
                .getRoute()
                .stream()
                .filter(route -> route.getId() == id)
                .collect(RWUtils.singleItemCollector());
    }

    default NextHop getHopWithId(
            final Route route, final int id) {
        return 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.next.hop.options.NextHopList.class
                .cast(route.getNextHopOptions())
                .getNextHopList()
                .getNextHop()
                .stream()
                .filter(nextHop -> nextHop.getKey().getId().intValue() == id)
                .collect(RWUtils.singleItemCollector());
    }

    default org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev140525.routing.routing.instance.routing.protocols.routing.protocol._static.routes.ipv6.Route getIpv6RouteWithId(
            final StaticRoutes staticRoutes, final long id) {
        return staticRoutes.getAugmentation(
                org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ipv6.unicast.routing.rev140525.StaticRoutes1.class)
                .getIpv6()
                .getRoute()
                .stream()
                .filter(route -> route.getId() == id)
                .collect(RWUtils.singleItemCollector());
    }

}