Fixup Augmentable and Identifiable methods changing
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfKeystoreAdapterTest.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.sal;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14
15 import java.security.KeyStoreException;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.List;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
29 import org.opendaylight.netconf.api.xml.XmlUtil;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.Keystore;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKeyBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017._private.keys.PrivateKeyKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificate;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificateBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.keystore.rev171017.trusted.certificates.TrustedCertificateKey;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.Node;
41 import org.w3c.dom.NodeList;
42
43 public class NetconfKeystoreAdapterTest {
44     private static final String XML_ELEMENT_PRIVATE_KEY = "private-key";
45     private static final String XML_ELEMENT_NAME = "name";
46     private static final String XML_ELEMENT_DATA = "data";
47     private static final String XML_ELEMENT_CERT_CHAIN = "certificate-chain";
48     private static final String XML_ELEMENT_TRUSTED_CERT = "trusted-certificate";
49     private static final String XML_ELEMENT_CERT = "certificate";
50
51     @Mock
52     private DataBroker dataBroker;
53     @Mock
54     private ListenerRegistration listenerRegistration;
55
56     @Before
57     public void setUp() {
58         MockitoAnnotations.initMocks(this);
59
60         doReturn(listenerRegistration).when(dataBroker).registerDataTreeChangeListener(
61                 any(DataTreeIdentifier.class), any(NetconfKeystoreAdapter.class));
62     }
63
64     @Test
65     public void testKeystoreAdapterInit() throws Exception {
66         NetconfKeystoreAdapter keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
67
68         try {
69             keystoreAdapter.getJavaKeyStore();
70             Assert.fail(IllegalStateException.class + "exception expected");
71         } catch (KeyStoreException e) {
72             assertTrue(e.getMessage().startsWith("No keystore private key found"));
73         }
74     }
75
76     @SuppressWarnings("unchecked")
77     @Test
78     public void testWritePrivateKey() throws Exception {
79         DataTreeModification<Keystore> dataTreeModification = mock(DataTreeModification.class);
80         DataObjectModification<Keystore> keystoreObjectModification = mock(DataObjectModification.class);
81         doReturn(keystoreObjectModification).when(dataTreeModification).getRootNode();
82
83         DataObjectModification<?> childObjectModification = mock(DataObjectModification.class);
84         doReturn(Collections.singletonList(childObjectModification))
85             .when(keystoreObjectModification).getModifiedChildren();
86         doReturn(PrivateKey.class).when(childObjectModification).getDataType();
87
88         doReturn(DataObjectModification.ModificationType.WRITE)
89             .when(childObjectModification).getModificationType();
90
91         PrivateKey privateKey = getPrivateKey();
92         doReturn(privateKey).when(childObjectModification).getDataAfter();
93
94         NetconfKeystoreAdapter keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
95         keystoreAdapter.onDataTreeChanged(Collections.singletonList(dataTreeModification));
96
97         java.security.KeyStore keyStore = keystoreAdapter.getJavaKeyStore();
98         assertTrue(keyStore.containsAlias(privateKey.getName()));
99     }
100
101     @SuppressWarnings("unchecked")
102     @Test
103     public void testWritePrivateKeyAndTrustedCertificate() throws Exception {
104         // Prepare PrivateKey configuration
105         DataTreeModification<Keystore> dataTreeModification1 = mock(DataTreeModification.class);
106         DataObjectModification<Keystore> keystoreObjectModification1 = mock(DataObjectModification.class);
107         doReturn(keystoreObjectModification1).when(dataTreeModification1).getRootNode();
108
109         DataObjectModification<?> childObjectModification1 = mock(DataObjectModification.class);
110         doReturn(Collections.singletonList(childObjectModification1))
111             .when(keystoreObjectModification1).getModifiedChildren();
112         doReturn(PrivateKey.class).when(childObjectModification1).getDataType();
113
114         doReturn(DataObjectModification.ModificationType.WRITE)
115             .when(childObjectModification1).getModificationType();
116
117         PrivateKey privateKey = getPrivateKey();
118         doReturn(privateKey).when(childObjectModification1).getDataAfter();
119
120         // Prepare TrustedCertificate configuration
121         DataTreeModification<Keystore> dataTreeModification2 = mock(DataTreeModification.class);
122         DataObjectModification<Keystore> keystoreObjectModification2 = mock(DataObjectModification.class);
123         doReturn(keystoreObjectModification2).when(dataTreeModification2).getRootNode();
124
125         DataObjectModification<?> childObjectModification2 = mock(DataObjectModification.class);
126         doReturn(Collections.singletonList(childObjectModification2))
127             .when(keystoreObjectModification2).getModifiedChildren();
128         doReturn(TrustedCertificate.class).when(childObjectModification2).getDataType();
129
130         doReturn(DataObjectModification.ModificationType.WRITE)
131             .when(childObjectModification2).getModificationType();
132
133         TrustedCertificate trustedCertificate = geTrustedCertificate();
134         doReturn(trustedCertificate).when(childObjectModification2).getDataAfter();
135
136         // Apply configurations
137         NetconfKeystoreAdapter keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
138         keystoreAdapter.onDataTreeChanged(Arrays.asList(dataTreeModification1, dataTreeModification2));
139
140         // Check result
141         java.security.KeyStore keyStore = keystoreAdapter.getJavaKeyStore();
142         assertTrue(keyStore.containsAlias(privateKey.getName()));
143         assertTrue(keyStore.containsAlias(trustedCertificate.getName()));
144     }
145
146     private PrivateKey getPrivateKey() throws Exception {
147         final List<PrivateKey> privateKeys = new ArrayList<>();
148         final Document document = readKeystoreXML();
149         final NodeList nodeList = document.getElementsByTagName(XML_ELEMENT_PRIVATE_KEY);
150         for (int i = 0; i < nodeList.getLength(); i++) {
151             final Node node = nodeList.item(i);
152             if (node.getNodeType() != Node.ELEMENT_NODE) {
153                 continue;
154             }
155             final Element element = (Element)node;
156             final String keyName = element.getElementsByTagName(XML_ELEMENT_NAME).item(0).getTextContent();
157             final String keyData = element.getElementsByTagName(XML_ELEMENT_DATA).item(0).getTextContent();
158             final NodeList certNodes = element.getElementsByTagName(XML_ELEMENT_CERT_CHAIN);
159             final List<String> certChain = new ArrayList<>();
160             for (int j = 0; j < certNodes.getLength(); j++) {
161                 final Node certNode = certNodes.item(j);
162                 if (certNode.getNodeType() != Node.ELEMENT_NODE) {
163                     continue;
164                 }
165                 certChain.add(certNode.getTextContent());
166             }
167
168             final PrivateKey privateKey = new PrivateKeyBuilder()
169                     .withKey(new PrivateKeyKey(keyName))
170                     .setName(keyName)
171                     .setData(keyData)
172                     .setCertificateChain(certChain)
173                     .build();
174             privateKeys.add(privateKey);
175         }
176
177         return privateKeys.get(0);
178     }
179
180     private TrustedCertificate geTrustedCertificate() throws Exception {
181         final List<TrustedCertificate> trustedCertificates = new ArrayList<>();
182         final Document document = readKeystoreXML();
183         final NodeList nodeList = document.getElementsByTagName(XML_ELEMENT_TRUSTED_CERT);
184         for (int i = 0; i < nodeList.getLength(); i++) {
185             final Node node = nodeList.item(i);
186             if (node.getNodeType() != Node.ELEMENT_NODE) {
187                 continue;
188             }
189             final Element element = (Element)node;
190             final String certName = element.getElementsByTagName(XML_ELEMENT_NAME).item(0).getTextContent();
191             final String certData = element.getElementsByTagName(XML_ELEMENT_CERT).item(0).getTextContent();
192
193             final TrustedCertificate certificate = new TrustedCertificateBuilder()
194                     .withKey(new TrustedCertificateKey(certName))
195                     .setName(certName)
196                     .setCertificate(certData)
197                     .build();
198             trustedCertificates.add(certificate);
199         }
200
201         return trustedCertificates.get(0);
202     }
203
204     private Document readKeystoreXML() throws Exception {
205         return XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/netconf-keystore.xml"));
206     }
207 }