NETVIRT-1637 CSIT failure
[netvirt.git] / sfc / classifier / impl / src / test / java / org / opendaylight / netvirt / sfc / classifier / providers / TestOdlInterfaceRpcService.java
1 /*
2  * Copyright © 2017 Ericsson, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netvirt.sfc.classifier.providers;
10
11 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
12
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.mockito.Mockito;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetDpidFromInterfaceOutputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEndpointIpForDpnInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEndpointIpForDpnOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetEndpointIpForDpnOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetNodeconnectorIdFromInterfaceInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetNodeconnectorIdFromInterfaceOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.GetNodeconnectorIdFromInterfaceOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
31 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
34
35 public abstract class TestOdlInterfaceRpcService implements OdlInterfaceRpcService {
36     public static TestOdlInterfaceRpcService newInstance() {
37         return Mockito.mock(TestOdlInterfaceRpcService.class, realOrException());
38     }
39
40     @Override
41     public ListenableFuture<RpcResult<GetEndpointIpForDpnOutput>> getEndpointIpForDpn(GetEndpointIpForDpnInput input) {
42         BigInteger dpnId = input.getDpid().toJava();
43
44         // if the dpnId is DPN_ID_NO_EXIST, then an empty response will be returned
45         GetEndpointIpForDpnOutputBuilder builder = new GetEndpointIpForDpnOutputBuilder();
46         if (GeniusProviderTestParams.DPN_ID.equals(dpnId)) {
47             List<IpAddress> localIpList = new ArrayList<>();
48             localIpList.add(new IpAddress(new Ipv4Address(GeniusProviderTestParams.IPV4_ADDRESS_STR)));
49             builder.setLocalIps(localIpList);
50         } else if (dpnId == GeniusProviderTestParams.DPN_ID_INVALID) {
51             return RpcResultBuilder.<GetEndpointIpForDpnOutput>failed()
52                     .withError(ErrorType.APPLICATION, "Invalid data.").buildFuture();
53         }
54
55         return RpcResultBuilder.success(builder.build()).buildFuture();
56     }
57
58     @Override
59     public ListenableFuture<RpcResult<GetDpidFromInterfaceOutput>> getDpidFromInterface(
60             GetDpidFromInterfaceInput input) {
61         String ifName = input.getIntfName();
62
63         // if the ifName is INTERFACE_NAME_NO_EXIST, then an empty response will be returned
64         GetDpidFromInterfaceOutputBuilder builder = new GetDpidFromInterfaceOutputBuilder();
65         if (ifName == GeniusProviderTestParams.INTERFACE_NAME) {
66             builder.setDpid(GeniusProviderTestParams.DPN_ID);
67         } else if (ifName == GeniusProviderTestParams.INTERFACE_NAME_INVALID) {
68             return RpcResultBuilder.<GetDpidFromInterfaceOutput>failed()
69                     .withError(ErrorType.APPLICATION, "Invalid data.").buildFuture();
70         }
71
72         return RpcResultBuilder.success(builder.build()).buildFuture();
73     }
74
75     @Override
76     public ListenableFuture<RpcResult<GetNodeconnectorIdFromInterfaceOutput>>
77         getNodeconnectorIdFromInterface(GetNodeconnectorIdFromInterfaceInput input)  {
78         String ifName = input.getIntfName();
79
80         // if the ifName is INTERFACE_NAME_NO_EXIST, then an empty response will be returned
81         GetNodeconnectorIdFromInterfaceOutputBuilder builder = new GetNodeconnectorIdFromInterfaceOutputBuilder();
82         if (ifName == GeniusProviderTestParams.INTERFACE_NAME) {
83             builder.setNodeconnectorId(new NodeConnectorId(
84                     GeniusProviderTestParams.NODE_CONNECTOR_ID_PREFIX
85                     + GeniusProviderTestParams.INTERFACE_NAME));
86         } else if (ifName == GeniusProviderTestParams.INTERFACE_NAME_INVALID) {
87             return RpcResultBuilder.<GetNodeconnectorIdFromInterfaceOutput>failed()
88                     .withError(ErrorType.APPLICATION, "Invalid data.").buildFuture();
89         }
90
91         return RpcResultBuilder.success(builder.build()).buildFuture();
92     }
93
94 }