2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
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
8 package org.opendaylight.genius.testutils;
10 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
13 import java.util.Optional;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ExecutionException;
16 import org.mockito.Mockito;
17 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
18 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
19 import org.opendaylight.genius.testutils.interfacemanager.InterfaceHelper;
20 import org.opendaylight.genius.testutils.interfacemanager.InterfaceStateHelper;
21 import org.opendaylight.genius.testutils.interfacemanager.TunnelInterfaceDetails;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
26 import org.opendaylight.yangtools.yang.common.Uint64;
29 * IInterfaceManager implementation for tests.
31 * @author Michael Vorburger
33 public abstract class TestInterfaceManager implements IInterfaceManager {
35 private Map<String, InterfaceInfo> interfaceInfos;
36 private Map<String, Interface> interfaces;
37 private Map<String, Boolean> externalInterfaces;
38 private Optional<DataBroker> optDataBroker;
41 * Deprecated factory method.
42 * @deprecated Use {@link TestInterfaceManager#newInstance(DataBroker)} instead now.
45 public static TestInterfaceManager newInstance() {
46 TestInterfaceManager testInterfaceManager = Mockito.mock(TestInterfaceManager.class, realOrException());
47 testInterfaceManager.interfaceInfos = new ConcurrentHashMap<>();
48 testInterfaceManager.interfaces = new ConcurrentHashMap<>();
49 testInterfaceManager.externalInterfaces = new ConcurrentHashMap<>();
50 testInterfaceManager.optDataBroker = Optional.empty();
51 return testInterfaceManager;
54 public static TestInterfaceManager newInstance(DataBroker dataBroker) {
55 TestInterfaceManager testInterfaceManager = newInstance();
56 testInterfaceManager.optDataBroker = Optional.of(dataBroker);
57 return testInterfaceManager;
60 public void addInterfaceInfo(InterfaceInfo interfaceInfo)
61 throws ExecutionException, InterruptedException {
62 interfaceInfos.put(interfaceInfo.getInterfaceName(), interfaceInfo);
63 if (optDataBroker.isPresent()) {
64 // Can't use ifPresent() here because of checked exception from tx.commit().get();
65 DataBroker dataBroker = optDataBroker.get();
66 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
68 Interface iface = InterfaceHelper.buildVlanInterfaceFromInfo(interfaceInfo);
69 //Add the interface to config ds so that if the application reads from configds it finds it there
70 tx.put(LogicalDatastoreType.CONFIGURATION,
71 InterfaceHelper.buildIId(interfaceInfo.getInterfaceName()),
74 //Add the interface to oper ds so that if the application reads from configds it finds it there
75 tx.put(LogicalDatastoreType.OPERATIONAL,
76 InterfaceStateHelper.buildStateInterfaceIid(interfaceInfo.getInterfaceName()),
77 InterfaceStateHelper.buildStateFromInterfaceInfo(interfaceInfo));
83 public void addInterface(Interface iface) {
84 interfaces.put(iface.getName(), iface);
87 public void addTunnelInterface(TunnelInterfaceDetails tunnelInterfaceDetails)
88 throws ExecutionException, InterruptedException {
89 InterfaceInfo interfaceInfo = tunnelInterfaceDetails.getInterfaceInfo();
90 interfaceInfos.put(interfaceInfo.getInterfaceName(), interfaceInfo);
92 if (optDataBroker.isPresent()) {
93 // Can't use ifPresent() here because of checked exception from tx.commit().get();
94 DataBroker dataBroker = optDataBroker.get();
96 Interface iface = InterfaceHelper.buildVxlanTunnelInterfaceFromInfo(tunnelInterfaceDetails);
98 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
99 tx.put(LogicalDatastoreType.CONFIGURATION,
100 InterfaceHelper.buildIId(interfaceInfo.getInterfaceName()),
103 tx.put(LogicalDatastoreType.OPERATIONAL,
104 InterfaceStateHelper.buildStateInterfaceIid(interfaceInfo.getInterfaceName()),
105 InterfaceStateHelper.buildStateFromInterfaceInfo(interfaceInfo));
107 externalInterfaces.put(interfaceInfo.getInterfaceName(), true);
113 public InterfaceInfo getInterfaceInfo(String interfaceName) {
114 InterfaceInfo interfaceInfo = interfaceInfos.get(interfaceName);
115 if (interfaceInfo == null) {
116 throw new IllegalStateException(
117 "must addInterfaceInfo() to TestInterfaceManager before getInterfaceInfo: " + interfaceName);
119 return interfaceInfo;
123 public InterfaceInfo getInterfaceInfoFromOperationalDataStore(String interfaceName) {
124 return getInterfaceInfo(interfaceName);
128 public InterfaceInfo getInterfaceInfoFromOperationalDataStore(
129 String interfaceName, InterfaceInfo.InterfaceType interfaceType) {
130 return interfaceInfos.get(interfaceName);
134 public InterfaceInfo getInterfaceInfoFromOperationalDSCache(String interfaceName) {
135 return getInterfaceInfo(interfaceName);
139 public Interface getInterfaceInfoFromConfigDataStore(String interfaceName) {
140 Interface iface = interfaces.get(interfaceName);
142 throw new IllegalStateException(
143 "must addInterface() to TestInterfaceManager before getInterfaceInfoFromConfigDataStore: "
150 public Uint64 getDpnForInterface(String interfaceName) {
151 return interfaceInfos.get(interfaceName).getDpId();
155 public Uint64 getDpnForInterface(Interface intrface) {
156 return interfaceInfos.get(intrface.getName()).getDpId();
160 public boolean isExternalInterface(String interfaceName) {
161 return externalInterfaces.containsKey(interfaceName);
165 public boolean isItmDirectTunnelsEnabled() {