2 * Copyright (c) 2017 Brocade Communication Systems 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.netconf.sal.connect.netconf.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.mockito.Mockito.when;
14 import java.lang.reflect.Method;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.netconf.sal.connect.util.NetconfTopologyRPCProvider;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.AddNetconfNodeInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.AddNetconfNodeInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 public class NetconfTopologyRPCProviderTest {
34 private static final NodeId NODE_ID = new NodeId("testing-node");
35 private static final String TOPOLOGY_ID = "testing-topology";
36 private static final String TEST_PWD = "test";
37 private static final String ENC_PWD = "4o9/Hn3Pi4150YrP12N/1g==";
40 private DataBroker dataBroker;
43 private AAAEncryptionService encryptionService;
45 NetconfTopologyRPCProvider rpcProvider ;
49 MockitoAnnotations.initMocks(this);
50 when(encryptionService.encrypt(TEST_PWD)).thenReturn(ENC_PWD);
51 rpcProvider = new NetconfTopologyRPCProvider(dataBroker, encryptionService, TOPOLOGY_ID);
55 public void testEncryptPassword() throws Exception {
57 NetconfNode node = invokeEncryption(true);
58 assertNotEquals(TEST_PWD, ((LoginPassword)node.getCredentials()).getPassword());
60 node = invokeEncryption(false);
61 assertEquals(TEST_PWD, ((LoginPassword)node.getCredentials()).getPassword());
64 private NetconfNode invokeEncryption(boolean encrypt) throws Exception {
67 method = NetconfTopologyRPCProvider.class.getDeclaredMethod("encryptPassword", AddNetconfNodeInput.class);
69 method.setAccessible(true);
70 NetconfNode node = null;
72 node = (NetconfNode)method.invoke(rpcProvider, getInput(encrypt));
77 private AddNetconfNodeInput getInput(boolean encrypt) {
78 AddNetconfNodeInputBuilder builder = new AddNetconfNodeInputBuilder();
79 builder.setCredentials(new LoginPasswordBuilder().setPassword(TEST_PWD).setUsername("test").build());
80 builder.setHost(new Host(new IpAddress(new Ipv4Address("10.18.16.188"))));
81 builder.setPort(new PortNumber(830));
82 builder.setTcpOnly(false);
83 builder.setNodeId(NODE_ID.toString());
84 builder.setEncrypt(encrypt);
85 return builder.build();