org.opendaylight.transportpce.test.AbstractDeviceTest Maven / Gradle / Ivy
/*
* Copyright © 2016 AT&T and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.transportpce.test;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Base class for device tests, should be used, when device databroker is needed
* in tests.
*
*/
public abstract class AbstractDeviceTest {
private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceTest.class);
private final Map internalStorage;
/**
* Default constructor only initializes the inner
* {@link AbstractDeviceTest#internalStorage} as asynchronized {@link Map}.
*/
public AbstractDeviceTest() {
this.internalStorage = Collections.synchronizedMap(new HashMap<>());
}
/**
* Insert a created device into {@link AbstractDeviceTest#internalStorage}.
*
* @see DeviceWrapper#createDeviceWrapper(String, InputStream, QName)
* @param key identifier of device simulator (wrapper)
* @param initialDataXmlInputStream {@link InputStream} of xml with initial simulator data
* @param intialDataQName {@link QName} of initial simulator data
* @return device simulator (wrapper)
*/
public DeviceWrapper createDeviceWrapper(@NonNull String key, @NonNull InputStream initialDataXmlInputStream,
@NonNull QName intialDataQName) {
DeviceWrapper deviceWrapper =
DeviceWrapper.createDeviceWrapper(key, initialDataXmlInputStream, intialDataQName);
LOG.info("Creating a new device wrapper {}, {}", key, deviceWrapper);
internalStorage.put(key, deviceWrapper);
return deviceWrapper;
}
/**
* Returns the {@link DeviceWrapper} identified by the key provided as param.
*
* @param deviceIdentifier identifier of device simulator
* @return stored device or null if not found
*/
public DeviceWrapper getDevice(@NonNull String deviceIdentifier) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(deviceIdentifier));
return internalStorage.get(deviceIdentifier);
}
}