creates common test functions
[transportpce.git] / test-common / src / main / java / org / opendaylight / transportpce / test / AbstractDeviceTest.java
1 /*
2  * Copyright © 2016 AT&T 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 package org.opendaylight.transportpce.test;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Strings;
12 import com.google.common.collect.Maps;
13 import java.io.InputStream;
14 import java.util.Collections;
15 import java.util.Map;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Base class for device tests, should be used, when device databroker is needed
23  * in tests.
24  *
25  */
26 public abstract class AbstractDeviceTest {
27
28     private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceTest.class);
29     private final Map<String, DeviceWrapper> internalStorage;
30
31     /**
32      * Default constructor only initializes the inner
33      * {@link AbstractDeviceTest#internalStorage} as asynchronized {@link Map}.
34      */
35     public AbstractDeviceTest() {
36         this.internalStorage = Collections.synchronizedMap(Maps.newHashMap());
37     }
38
39     /**
40      * Insert a created device into {@link AbstractDeviceTest#internalStorage}.
41      *
42      * @see DeviceWrapper#createDeviceWrapper(String, InputStream, QName)
43      * @param key identifier of device simulator (wrapper)
44      * @param initialDataXmlInputStream {@link InputStream} of xml with initial simulator data
45      * @param intialDataQName {@link QName} of initial simulator data
46      * @return device simulator (wrapper)
47      */
48     public DeviceWrapper createDeviceWrapper(@Nonnull String key, @Nonnull InputStream initialDataXmlInputStream,
49             @Nonnull QName intialDataQName) {
50         DeviceWrapper deviceWrapper =
51                 DeviceWrapper.createDeviceWrapper(key, initialDataXmlInputStream, intialDataQName);
52         LOG.info("Creating a new device wrapper {}, {}", key, deviceWrapper);
53         internalStorage.put(key, deviceWrapper);
54         return deviceWrapper;
55     }
56
57     /**
58      * Returns the {@link DeviceWrapper} identified by the key provided as param.
59      *
60      * @param deviceIdentifier identifier of device simulator
61      * @return stored device or null if not found
62      */
63     public DeviceWrapper getDevice(@Nonnull String deviceIdentifier) {
64         Preconditions.checkArgument(!Strings.isNullOrEmpty(deviceIdentifier));
65         return internalStorage.get(deviceIdentifier);
66     }
67 }