}
}
- /**
- * Get extracted address or default.
- *
- * @throws java.lang.IllegalStateException if neither address is present.
- */
- private static InetSocketAddress getNetconfAddress(final InetSocketAddress defaultAddress, Optional<InetSocketAddress> extractedAddress, InfixProp infix) {
- InetSocketAddress inetSocketAddress;
-
- if (extractedAddress.isPresent() == false) {
- logger.debug("Netconf {} address not found, falling back to default {}", infix, defaultAddress);
-
- if (defaultAddress == null) {
- logger.warn("Netconf {} address not found, default address not provided", infix);
- throw new IllegalStateException("Netconf " + infix + " address not found, default address not provided");
- }
- inetSocketAddress = defaultAddress;
- } else {
- inetSocketAddress = extractedAddress.get();
- }
-
- return inetSocketAddress;
- }
-
public static String getPrivateKeyPath(final BundleContext context) {
return getPropertyValue(context, getPrivateKeyKey());
}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.mapping;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
+import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
+import org.opendaylight.controller.netconf.util.xml.XmlElement;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+public class AbstractLastNetconfOperationTest {
+ class LastNetconfOperationImplTest extends AbstractLastNetconfOperation {
+
+ boolean handleWithNoSubsequentOperationsRun;
+
+ protected LastNetconfOperationImplTest(String netconfSessionIdForReporting) {
+ super(netconfSessionIdForReporting);
+ handleWithNoSubsequentOperationsRun = false;
+ }
+
+ @Override
+ protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
+ handleWithNoSubsequentOperationsRun = true;
+ return null;
+ }
+
+ @Override
+ protected String getOperationName() {
+ return "";
+ }
+ }
+
+ LastNetconfOperationImplTest netconfOperation;
+
+ @Before
+ public void setUp() throws Exception {
+ netconfOperation = new LastNetconfOperationImplTest("");
+ }
+
+ @Test
+ public void testNetconfOperation() throws Exception {
+ netconfOperation.handleWithNoSubsequentOperations(null, null);
+ assertTrue(netconfOperation.handleWithNoSubsequentOperationsRun);
+ assertEquals(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY, netconfOperation.getHandlingPriority());
+ }
+
+ @Test(expected = NetconfDocumentedException.class)
+ public void testHandle() throws Exception {
+ NetconfOperationChainedExecution operation = mock(NetconfOperationChainedExecution.class);
+ doReturn("").when(operation).toString();
+
+ doReturn(false).when(operation).isExecutionTermination();
+ netconfOperation.handle(null, null, operation);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.mapping;
+
+import java.io.IOException;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
+import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
+import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
+import org.opendaylight.controller.netconf.util.xml.XmlElement;
+import org.opendaylight.controller.netconf.util.xml.XmlUtil;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public class AbstractNetconfOperationTest {
+
+ class NetconfOperationImpl extends AbstractNetconfOperation {
+
+ public boolean handleRun;
+
+ protected NetconfOperationImpl(String netconfSessionIdForReporting) {
+ super(netconfSessionIdForReporting);
+ this.handleRun = false;
+ }
+
+ @Override
+ protected String getOperationName() {
+ return null;
+ }
+
+ @Override
+ protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
+ this.handleRun = true;
+ try {
+ return XmlUtil.readXmlToElement("<element/>");
+ } catch (SAXException | IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ private NetconfOperationImpl netconfOperation;
+ private NetconfOperationChainedExecution operation;
+
+ @Before
+ public void setUp() throws Exception {
+ netconfOperation = new NetconfOperationImpl("str");
+ operation = mock(NetconfOperationChainedExecution.class);
+ }
+
+ @Test
+ public void testAbstractNetconfOperation() throws Exception {
+ Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/edit_config.xml");
+ assertEquals(netconfOperation.getNetconfSessionIdForReporting(), "str");
+ assertNotNull(netconfOperation.canHandle(helloMessage));
+ assertEquals(netconfOperation.getHandlingPriority(), HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY);
+
+ netconfOperation.handle(helloMessage, operation);
+ assertTrue(netconfOperation.handleRun);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.mapping;
+
+import org.junit.Test;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
+import org.opendaylight.controller.netconf.util.xml.XmlElement;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import static org.junit.Assert.assertEquals;
+
+public class AbstractSingletonNetconfOperationTest {
+ class SingletonNCOperationImpl extends AbstractSingletonNetconfOperation {
+
+ protected SingletonNCOperationImpl(String netconfSessionIdForReporting) {
+ super(netconfSessionIdForReporting);
+ }
+
+ @Override
+ protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
+ return null;
+ }
+
+ @Override
+ protected String getOperationName() {
+ return null;
+ }
+ }
+
+ @Test
+ public void testAbstractSingletonNetconfOperation() throws Exception {
+ SingletonNCOperationImpl operation = new SingletonNCOperationImpl("");
+ assertEquals(operation.getHandlingPriority(), HandlingPriority.HANDLE_WITH_MAX_PRIORITY);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.messages;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class NetconfHelloMessageAdditionalHeaderTest {
+
+
+ private String customHeader = "[user;1.1.1.1:40;tcp;client;]";
+ private NetconfHelloMessageAdditionalHeader header;
+
+ @Before
+ public void setUp() throws Exception {
+ header = new NetconfHelloMessageAdditionalHeader("user", "1.1.1.1", "40", "tcp", "client");
+ }
+
+ @Test
+ public void testGetters() throws Exception {
+ assertEquals(header.getAddress(), "1.1.1.1");
+ assertEquals(header.getUserName(), "user");
+ assertEquals(header.getPort(), "40");
+ assertEquals(header.getTransport(), "tcp");
+ assertEquals(header.getSessionIdentifier(), "client");
+ }
+
+ @Test
+ public void testStaticConstructor() throws Exception {
+ NetconfHelloMessageAdditionalHeader h = NetconfHelloMessageAdditionalHeader.fromString(customHeader);
+ assertEquals(h.toString(), header.toString());
+ assertEquals(h.toFormattedString(), header.toFormattedString());
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.messages;
+
+
+import com.google.common.base.Optional;
+import java.util.Set;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.internal.util.collections.Sets;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class NetconfHelloMessageTest {
+
+ Set<String> caps;
+
+ @Before
+ public void setUp() throws Exception {
+ caps = Sets.newSet("cap1");
+ }
+
+ @Test
+ public void testConstructor() throws Exception {
+ NetconfHelloMessageAdditionalHeader additionalHeader = new NetconfHelloMessageAdditionalHeader("name","host","1","transp","id");
+ NetconfHelloMessage message = NetconfHelloMessage.createClientHello(caps, Optional.of(additionalHeader));
+ assertTrue(message.isHelloMessage(message));
+ assertEquals(Optional.of(additionalHeader), message.getAdditionalHeader());
+
+ NetconfHelloMessage serverMessage = NetconfHelloMessage.createServerHello(caps, 100L);
+ assertTrue(serverMessage.isHelloMessage(serverMessage));
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.messages;
+
+import com.google.common.base.Charsets;
+import java.util.Arrays;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class NetconfMessageHeaderTest {
+ @Test
+ public void testGet() throws Exception {
+ NetconfMessageHeader header = new NetconfMessageHeader(10);
+ assertEquals(header.getLength(), 10);
+
+ byte[] expectedValue = "\n#10\n".getBytes(Charsets.US_ASCII);
+ assertArrayEquals(expectedValue, header.toBytes());
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.messages;
+
+import java.util.Collection;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.api.NetconfMessage;
+import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
+import org.w3c.dom.Document;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class NetconfMessageUtilTest {
+ @Test
+ public void testNetconfMessageUtil() throws Exception {
+ Document okMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
+ assertTrue(NetconfMessageUtil.isOKMessage(new NetconfMessage(okMessage)));
+ assertFalse(NetconfMessageUtil.isErrorMessage(new NetconfMessage(okMessage)));
+
+ Document errorMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/communicationError/testClientSendsRpcReply_expectedResponse.xml");
+ assertTrue(NetconfMessageUtil.isErrorMessage(new NetconfMessage(errorMessage)));
+ assertFalse(NetconfMessageUtil.isOKMessage(new NetconfMessage(errorMessage)));
+
+ Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/client_hello.xml");
+ Collection<String> caps = NetconfMessageUtil.extractCapabilitiesFromHello(new NetconfMessage(helloMessage).getDocument());
+ assertTrue(caps.contains("urn:ietf:params:netconf:base:1.0"));
+ assertTrue(caps.contains("urn:ietf:params:netconf:base:1.1"));
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.messages;
+
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.util.concurrent.GenericFutureListener;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
+import org.opendaylight.controller.netconf.api.NetconfMessage;
+import org.opendaylight.controller.netconf.api.NetconfSession;
+import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
+import org.w3c.dom.Document;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.*;
+
+public class SendErrorExceptionUtilTest {
+
+ NetconfSession netconfSession;
+ ChannelFuture channelFuture;
+ Channel channel;
+ private NetconfDocumentedException exception;
+
+ @Before
+ public void setUp() throws Exception {
+ netconfSession = mock(NetconfSession.class);
+ channelFuture = mock(ChannelFuture.class);
+ channel = mock(Channel.class);
+ doReturn(channelFuture).when(netconfSession).sendMessage(any(NetconfMessage.class));
+ doReturn(channelFuture).when(channelFuture).addListener(any(GenericFutureListener.class));
+ doReturn(channelFuture).when(channel).writeAndFlush(any(NetconfMessage.class));
+ exception = new NetconfDocumentedException("err");
+ }
+
+ @Test
+ public void testSendErrorMessage1() throws Exception {
+ SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception);
+ verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
+ verify(netconfSession, times(1)).sendMessage(any(NetconfMessage.class));
+ }
+
+ @Test
+ public void testSendErrorMessage2() throws Exception {
+ SendErrorExceptionUtil.sendErrorMessage(channel, exception);
+ verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
+ }
+
+ @Test
+ public void testSendErrorMessage3() throws Exception {
+ Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc.xml");
+ SendErrorExceptionUtil.sendErrorMessage(netconfSession, exception, new NetconfMessage(helloMessage));
+ verify(channelFuture, times(1)).addListener(any(GenericFutureListener.class));
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. 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.controller.netconf.util.osgi;
+
+import com.google.common.base.Optional;
+import io.netty.channel.local.LocalAddress;
+import java.net.InetSocketAddress;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.netconf.util.NetconfUtil;
+import org.osgi.framework.BundleContext;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+public class NetconfConfigUtilTest {
+
+ private BundleContext bundleContext;
+
+ @Before
+ public void setUp() throws Exception {
+ bundleContext = mock(BundleContext.class);
+ }
+
+ @Test
+ public void testNetconfConfigUtil() throws Exception {
+ assertEquals(NetconfConfigUtil.getNetconfLocalAddress(), new LocalAddress("netconf"));
+
+ doReturn("").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
+ assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
+
+ doReturn("a").when(bundleContext).getProperty("netconf.connectionTimeoutMillis");
+ assertEquals(NetconfConfigUtil.extractTimeoutMillis(bundleContext), 5000);
+ }
+
+ @Test
+ public void testgetPrivateKeyKey() throws Exception {
+ assertEquals(NetconfConfigUtil.getPrivateKeyKey(), "netconf.ssh.pk.path");
+ }
+
+ @Test
+ public void testgetNetconfServerAddressKey() throws Exception {
+ NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
+ assertEquals(NetconfConfigUtil.getNetconfServerAddressKey(prop), "netconf.tcp.address");
+ }
+
+ @Test
+ public void testExtractNetconfServerAddress() throws Exception {
+ NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
+ doReturn("").when(bundleContext).getProperty(anyString());
+ assertEquals(NetconfConfigUtil.extractNetconfServerAddress(bundleContext, prop), Optional.absent());
+ }
+
+ @Test
+ public void testExtractNetconfServerAddress2() throws Exception {
+ NetconfConfigUtil.InfixProp prop = NetconfConfigUtil.InfixProp.tcp;
+ doReturn("1.1.1.1").when(bundleContext).getProperty("netconf.tcp.address");
+ doReturn("20").when(bundleContext).getProperty("netconf.tcp.port");
+ Optional<InetSocketAddress> inetSocketAddressOptional = NetconfConfigUtil.extractNetconfServerAddress(bundleContext, prop);
+ assertTrue(inetSocketAddressOptional.isPresent());
+ assertEquals(inetSocketAddressOptional.get(), new InetSocketAddress("1.1.1.1", 20));
+ }
+
+ @Test
+ public void testGetPrivateKeyPath() throws Exception {
+ doReturn("path").when(bundleContext).getProperty("netconf.ssh.pk.path");
+ assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testGetPrivateKeyPath2() throws Exception {
+ doReturn(null).when(bundleContext).getProperty("netconf.ssh.pk.path");
+ assertEquals(NetconfConfigUtil.getPrivateKeyPath(bundleContext), "path");
+ }
+}