Move netconf-console to apps/
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / util / NetconfSalKeystoreServiceTest.java
1 /*
2  * Copyright (c) 2018 ZTE Corporation. 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.netconf.sal.connect.netconf.util;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.mdsal.common.api.CommitInfo.emptyFluentFuture;
16
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
27 import org.opendaylight.mdsal.binding.api.DataBroker;
28 import org.opendaylight.mdsal.binding.api.WriteTransaction;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.netconf.api.xml.XmlUtil;
31 import org.opendaylight.netconf.sal.connect.util.NetconfSalKeystoreService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddPrivateKeyInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddPrivateKeyInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.AddTrustedCertificateInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKeyBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKeyKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificate;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificateBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificateKey;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48
49 @RunWith(MockitoJUnitRunner.StrictStubs.class)
50 public class NetconfSalKeystoreServiceTest {
51     private static final String XML_ELEMENT_PRIVATE_KEY = "private-key";
52     private static final String XML_ELEMENT_NAME = "name";
53     private static final String XML_ELEMENT_DATA = "data";
54     private static final String XML_ELEMENT_CERT_CHAIN = "certificate-chain";
55     private static final String XML_ELEMENT_TRUSTED_CERT = "trusted-certificate";
56     private static final String XML_ELEMENT_CERT = "certificate";
57
58     @Mock
59     private WriteTransaction writeTx;
60     @Mock
61     private DataBroker dataBroker;
62     @Mock
63     private AAAEncryptionService encryptionService;
64
65     @Before
66     public void setUp() {
67         doReturn(writeTx).when(dataBroker).newWriteOnlyTransaction();
68         doNothing().when(writeTx)
69             .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class));
70     }
71
72     @Test
73     public void testAddPrivateKey() throws Exception {
74         doReturn(emptyFluentFuture()).when(writeTx).commit();
75         NetconfSalKeystoreService keystoreService = new NetconfSalKeystoreService(dataBroker, encryptionService);
76
77         final AddPrivateKeyInput input = getPrivateKeyInput();
78         keystoreService.addPrivateKey(input);
79
80         verify(writeTx, times(input.getPrivateKey().size() + 1))
81             .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class));
82     }
83
84     @Test
85     public void testAddTrustedCertificate() throws Exception {
86         doReturn(emptyFluentFuture()).when(writeTx).commit();
87         NetconfSalKeystoreService keystoreService = new NetconfSalKeystoreService(dataBroker, encryptionService);
88
89         final AddTrustedCertificateInput input = getTrustedCertificateInput();
90         keystoreService.addTrustedCertificate(input);
91
92         verify(writeTx, times(input.getTrustedCertificate().size() + 1))
93             .merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(DataObject.class));
94     }
95
96     private AddPrivateKeyInput getPrivateKeyInput() throws Exception {
97         final Map<PrivateKeyKey, PrivateKey> privateKeys = new HashMap<>();
98         final Document document = readKeystoreXML();
99         final NodeList nodeList = document.getElementsByTagName(XML_ELEMENT_PRIVATE_KEY);
100         for (int i = 0; i < nodeList.getLength(); i++) {
101             final Node node = nodeList.item(i);
102             if (node.getNodeType() != Node.ELEMENT_NODE) {
103                 continue;
104             }
105             final Element element = (Element)node;
106             final String keyName = element.getElementsByTagName(XML_ELEMENT_NAME).item(0).getTextContent();
107             final String keyData = element.getElementsByTagName(XML_ELEMENT_DATA).item(0).getTextContent();
108             final NodeList certNodes = element.getElementsByTagName(XML_ELEMENT_CERT_CHAIN);
109             final List<String> certChain = new ArrayList<>();
110             for (int j = 0; j < certNodes.getLength(); j++) {
111                 final Node certNode = certNodes.item(j);
112                 if (certNode.getNodeType() != Node.ELEMENT_NODE) {
113                     continue;
114                 }
115                 certChain.add(certNode.getTextContent());
116             }
117
118             final PrivateKeyKey key = new PrivateKeyKey(keyName);
119             privateKeys.put(key, new PrivateKeyBuilder()
120                 .withKey(key)
121                 .setData(keyData)
122                 .setCertificateChain(certChain)
123                 .build());
124         }
125
126         return new AddPrivateKeyInputBuilder().setPrivateKey(privateKeys).build();
127     }
128
129     private AddTrustedCertificateInput getTrustedCertificateInput() throws Exception {
130         final Map<TrustedCertificateKey, TrustedCertificate> trustedCertificates = new HashMap<>();
131         final Document document = readKeystoreXML();
132         final NodeList nodeList = document.getElementsByTagName(XML_ELEMENT_TRUSTED_CERT);
133         for (int i = 0; i < nodeList.getLength(); i++) {
134             final Node node = nodeList.item(i);
135             if (node.getNodeType() != Node.ELEMENT_NODE) {
136                 continue;
137             }
138             final Element element = (Element)node;
139             final String certName = element.getElementsByTagName(XML_ELEMENT_NAME).item(0).getTextContent();
140             final String certData = element.getElementsByTagName(XML_ELEMENT_CERT).item(0).getTextContent();
141
142             final TrustedCertificateKey key = new TrustedCertificateKey(certName);
143             trustedCertificates.put(key, new TrustedCertificateBuilder()
144                 .withKey(key)
145                 .setName(certName)
146                 .setCertificate(certData)
147                 .build());
148         }
149
150         return new AddTrustedCertificateInputBuilder().setTrustedCertificate(trustedCertificates).build();
151     }
152
153     private Document readKeystoreXML() throws Exception {
154         return XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/netconf-keystore.xml"));
155     }
156 }