package org.opendaylight.controller.sal.restconf.impl.test;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+
import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.CheckedFuture;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.URISyntaxException;
import java.sql.Date;
import java.text.ParseException;
-import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.ext.MessageBodyReader;
-import javax.ws.rs.ext.MessageBodyWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
-import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
-import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
-import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
-import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
-import org.opendaylight.controller.sal.restconf.impl.NodeWrapper;
-import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
-import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
-import org.opendaylight.controller.sal.restconf.impl.RestconfError;
-import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
-import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
-import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
-import org.opendaylight.controller.sal.restconf.impl.StructuredData;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.opendaylight.yangtools.yang.data.api.Node;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.composite.node.schema.cnsn.parser.CnSnToNormalizedNodeParserFactory;
-import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
- private final static YangContextParser parser = new YangParserImpl();
+ private final static YangContextParser PARSER = new YangParserImpl();
private static Set<Module> loadModules(final String resourceDirectory) throws FileNotFoundException {
final File testDir = new File(resourceDirectory);
testFiles.add(new File(testDir, fileName));
}
}
- return parser.parseYangModels(testFiles);
+ return PARSER.parseYangModels(testFiles);
}
public static Set<Module> loadModulesFrom(final String yangPath) {
}
public static SchemaContext loadSchemaContext(final Set<Module> modules) {
- return parser.resolveSchemaContext(modules);
+ return PARSER.resolveSchemaContext(modules);
}
public static SchemaContext loadSchemaContext(final String resourceDirectory) throws FileNotFoundException {
- return parser.resolveSchemaContext(loadModulesFrom(resourceDirectory));
+ return PARSER.resolveSchemaContext(loadModulesFrom(resourceDirectory));
}
public static Module findModule(final Set<Module> modules, final String moduleName) {
}
- /**
- * @deprecated method will be removed in Lithium release
- * we don't wish to use Node and CompositeNode anywhere -
- *
- * Fill missing data (namespaces) and build correct data type in {@code compositeNode} according to
- * {@code dataSchemaNode}. The method {@link RestconfImpl#createConfigurationData createConfigurationData} is used
- * because it contains calling of method {code normalizeNode}
- */
- @Deprecated
- public static void normalizeCompositeNode(final Node<?> node, final Set<Module> modules, final String schemaNodePath) {
- final RestconfImpl restconf = RestconfImpl.getInstance();
- ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules));
- prepareMocksForRestconf(modules, restconf);
-
- final InstanceIdentifierContext iiContext = ControllerContext.getInstance().toInstanceIdentifier(schemaNodePath);
- final DOMMountPoint mountPoint = iiContext.getMountPoint();
- final CompositeNode value = RestconfImpl.getInstance().normalizeNode(node, (DataSchemaNode) iiContext.getSchemaNode(), mountPoint);
- final NormalizedNode<?, ?> normNodePayload = compositeNodeToDatastoreNormalizedNode(value, (DataSchemaNode) iiContext.getSchemaNode());
- final NormalizedNodeContext normlNodeContext = new NormalizedNodeContext(iiContext, normNodePayload);
-
- restconf.updateConfigurationData(schemaNodePath, normlNodeContext);
- }
-
/**
* Searches module with name {@code searchedModuleName} in {@code modules}. If module name isn't specified and
* module set has only one element then this element is returned.
return buildQName(name, "", null);
}
- private static void addDummyNamespaceToAllNodes(final NodeWrapper<?> wrappedNode) throws URISyntaxException {
- wrappedNode.setNamespace(new URI(""));
- if (wrappedNode instanceof CompositeNodeWrapper) {
- for (final NodeWrapper<?> childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) {
- addDummyNamespaceToAllNodes(childNodeWrapper);
- }
- }
- }
-
- private static void prepareMocksForRestconf(final Set<Module> modules, final RestconfImpl restconf) {
- final ControllerContext controllerContext = ControllerContext.getInstance();
- final BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class);
-
- controllerContext.setSchemas(TestUtils.loadSchemaContext(modules));
-
- when(mockedBrokerFacade.commitConfigurationDataPut(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
- .thenReturn(mock(CheckedFuture.class));
-
- restconf.setControllerContext(controllerContext);
- restconf.setBroker(mockedBrokerFacade);
- }
-
- public static Node<?> readInputToCnSn(final String path, final boolean dummyNamespaces,
- final MessageBodyReader<Node<?>> reader) throws WebApplicationException {
-
- final InputStream inputStream = TestUtils.class.getResourceAsStream(path);
- try {
- final Node<?> node = reader.readFrom(null, null, null, null, null, inputStream);
- assertTrue(node instanceof CompositeNodeWrapper);
- if (dummyNamespaces) {
- try {
- TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) node);
- return ((CompositeNodeWrapper) node).unwrap();
- } catch (final URISyntaxException e) {
- LOG.error(e.getMessage());
- assertTrue(e.getMessage(), false);
- }
- }
- return node;
- } catch (final IOException e) {
- LOG.error(e.getMessage());
- assertTrue(e.getMessage(), false);
- }
- return null;
- }
-
-// public static Node<?> readInputToCnSnNew(String path, MessageBodyReader<Node<?>> reader) throws WebApplicationException {
-// InputStream inputStream = TestUtils.class.getResourceAsStream(path);
-// try {
-// return reader.readFrom(null, null, null, null, null, inputStream);
-// } catch (IOException e) {
-// LOG.error(e.getMessage());
-// assertTrue(e.getMessage(), false);
-// }
-// return null;
-// }
-
- public static Node<?> readInputToCnSn(final String path, final MessageBodyReader<Node<?>> reader) {
- return readInputToCnSn(path, false, reader);
- }
-
- public static String writeCompNodeWithSchemaContextToOutput(final Node<?> node, final Set<Module> modules,
- final DataSchemaNode dataSchemaNode, final MessageBodyWriter<StructuredData> messageBodyWriter) throws IOException,
- WebApplicationException {
-
- assertNotNull(dataSchemaNode);
- assertNotNull("Composite node can't be null", node);
- final ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
-
- ControllerContext.getInstance().setSchemas(loadSchemaContext(modules));
-
- assertTrue(node instanceof CompositeNode);
- messageBodyWriter.writeTo(new StructuredData((CompositeNode)node, dataSchemaNode, null), null, null, null, null,
- null, byteArrayOS);
-
- return byteArrayOS.toString();
- }
-
public static String loadTextFile(final String filePath) throws IOException {
final FileReader fileReader = new FileReader(filePath);
final BufferedReader bufReader = new BufferedReader(fileReader);
return matcher.matches();
}
- public static NormalizedNode compositeNodeToDatastoreNormalizedNode(final CompositeNode compositeNode,
- final DataSchemaNode schema) {
- final List<Node<?>> lst = new ArrayList<Node<?>>();
- lst.add(compositeNode);
- if (schema instanceof ContainerSchemaNode) {
- return CnSnToNormalizedNodeParserFactory.getInstance().getContainerNodeParser()
- .parse(lst, (ContainerSchemaNode) schema);
- } else if (schema instanceof ListSchemaNode) {
- return CnSnToNormalizedNodeParserFactory.getInstance().getMapNodeParser()
- .parse(lst, (ListSchemaNode) schema);
- }
-
- LOG.error("Top level isn't of type container, list, leaf schema node but " + schema.getClass().getSimpleName());
-
- throw new RestconfDocumentedException(new RestconfError(ErrorType.APPLICATION, ErrorTag.INVALID_VALUE,
- "It wasn't possible to translate specified data to datastore readable form."));
- }
-
public static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName, final String namespace,
final String revision) throws ParseException {
return new YangInstanceIdentifier.NodeIdentifier(QName.create(namespace, revision, localName));
public static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(final String localName,
final String namespace, final String revision, final String... keysAndValues) throws ParseException {
- final java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(revision);
if (keysAndValues.length % 2 != 0) {
new IllegalArgumentException("number of keys argument have to be divisible by 2 (map)");
}
predicate);
}
- public static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
- final CompositeNodeBuilder<ImmutableCompositeNode> interfaceBuilder = ImmutableCompositeNode.builder();
- interfaceBuilder.addLeaf(buildQName("name", "dummy", "2014-07-29"), "eth0");
- interfaceBuilder.addLeaf(buildQName("type", "dummy", "2014-07-29"), "ethernetCsmacd");
- interfaceBuilder.addLeaf(buildQName("enabled", "dummy", "2014-07-29"), "false");
- interfaceBuilder.addLeaf(buildQName("description", "dummy", "2014-07-29"), "some interface");
- return interfaceBuilder.toInstance();
- }
-
static NormalizedNode<?,?> prepareNormalizedNodeWithIetfInterfacesInterfacesData() throws ParseException {
final String ietfInterfacesDate = "2013-07-04";
final String namespace = "urn:ietf:params:xml:ns:yang:ietf-interfaces";