2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.northbound.daeximoffsitebackup;
24 import com.google.common.util.concurrent.FluentFuture;
25 import com.google.common.util.concurrent.Futures;
26 import com.google.common.util.concurrent.ListenableFuture;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.net.HttpURLConnection;
35 import java.time.Instant;
36 import java.time.ZoneId;
37 import java.time.format.DateTimeFormatter;
38 import java.util.Arrays;
39 import java.util.Base64;
40 import java.util.Collection;
41 import java.util.List;
42 import java.util.Properties;
43 import java.util.concurrent.ExecutionException;
44 import java.util.concurrent.ExecutorService;
45 import java.util.concurrent.Executors;
46 import java.util.zip.ZipEntry;
47 import java.util.zip.ZipInputStream;
48 import java.util.zip.ZipOutputStream;
49 import javax.annotation.Nonnull;
50 import org.eclipse.jdt.annotation.NonNull;
51 import org.opendaylight.mdsal.binding.api.DataBroker;
52 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
53 import org.opendaylight.mdsal.binding.api.RpcProviderService;
54 import org.opendaylight.mdsal.binding.api.WriteTransaction;
55 import org.opendaylight.mdsal.common.api.CommitInfo;
56 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.BackupDataInput;
57 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.BackupDataOutput;
58 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.BackupDataOutputBuilder;
59 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.DaeximOffsiteBackupService;
60 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataInput;
61 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataOutput;
62 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataOutputBuilder;
63 import org.opendaylight.yangtools.concepts.ObjectRegistration;
64 import org.opendaylight.yangtools.yang.common.RpcResult;
65 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
69 public class DaeximOffsiteBackupProvider implements AutoCloseable, DaeximOffsiteBackupService, DataTreeChangeListener {
70 private static final Logger LOG = LoggerFactory.getLogger(DaeximOffsiteBackupProvider.class);
72 private static String DAEXIM_DIR;
73 private static String CREDENTIALS;
74 private static String NEXUS_URL;
75 private static String POD_NAME;
76 private static String OPERATIONAL_JSON;
77 private static String MODELS_JSON;
78 private static String CONFIG_JSON;
79 private static String PROPERTIES_FILE = System.getenv("SDNC_CONFIG_DIR") + "/daexim-offsite-backup.properties";
81 private static final String BACKUP_ARCHIVE = "odl_backup.zip";
82 private static final String appName = "daexim-offsite-backup";
84 private final ExecutorService executor;
85 private Properties properties;
86 private DataBroker dataBroker;
87 private RpcProviderService rpcRegistry;
88 private ObjectRegistration<DaeximOffsiteBackupService> rpcRegistration;
90 public DaeximOffsiteBackupProvider(DataBroker dataBroker,
91 RpcProviderService rpcProviderRegistry) {
92 LOG.info("Creating provider for " + appName);
93 this.executor = Executors.newFixedThreadPool(1);
94 this.dataBroker = dataBroker;
95 this.rpcRegistry = rpcProviderRegistry;
99 public void initialize() {
100 LOG.info("Initializing provider for " + appName);
101 // Create the top level containers
104 DaeximOffsiteBackupUtil.loadProperties();
105 } catch (Exception e) {
106 LOG.error("Caught Exception while trying to load properties file", e);
108 rpcRegistration = rpcRegistry.registerRpcImplementation(DaeximOffsiteBackupService.class, this);
109 LOG.info("Initialization complete for " + appName);
112 private void loadProperties() {
113 LOG.info("Loading properties from " + PROPERTIES_FILE);
114 if(properties == null)
115 properties = new Properties();
116 File propertiesFile = new File(PROPERTIES_FILE);
117 if(!propertiesFile.exists()) {
118 LOG.warn("Properties file (" + PROPERTIES_FILE + ") not found. Using default properties.");
119 properties.put("daeximDirectory", "/opt/opendaylight/current/daexim/");
120 properties.put("credentials", "admin:enc:YWRtaW4xMjM=");
121 properties.put("nexusUrl", "http://localhost:8081/nexus/content/repositories/");
122 properties.put("podName", "UNKNOWN_ODL");
123 properties.put("file.operational", "odl_backup_operational.json");
124 properties.put("file.models", "odl_backup_models.json");
125 properties.put("file.config", "odl_backup_config.json");
128 FileInputStream fileInputStream;
130 fileInputStream = new FileInputStream(propertiesFile);
131 properties.load(fileInputStream);
132 fileInputStream.close();
133 LOG.info(properties.size() + " properties loaded.");
134 LOG.info("daeximDirectory: " + properties.getProperty("daeximDirectory"));
135 LOG.info("nexusUrl: " + properties.getProperty("nexusUrl"));
136 LOG.info("podName: " + properties.getProperty("podName"));
137 LOG.info("file.operational: " + properties.getProperty("file.operational"));
138 LOG.info("file.models: " + properties.getProperty("file.models"));
139 LOG.info("file.config: " + properties.getProperty("file.config"));
140 } catch(IOException e) {
141 LOG.error("Error loading properties.", e);
145 private void applyProperties() {
146 LOG.info("Applying properties...");
147 if(POD_NAME == null || POD_NAME.isEmpty()) {
148 LOG.warn("MY_POD_NAME environment variable not set. Using value from properties.");
149 POD_NAME = properties.getProperty("podName");
151 DAEXIM_DIR = properties.getProperty("daeximDirectory");
152 NEXUS_URL = properties.getProperty("nexusUrl");
154 OPERATIONAL_JSON = properties.getProperty("file.operational");
155 MODELS_JSON = properties.getProperty("file.models");
156 CONFIG_JSON = properties.getProperty("file.config");
158 if(!properties.getProperty("credentials").contains(":")) { //Entire thing is encoded
159 CREDENTIALS = new String(Base64.getDecoder().decode(properties.getProperty("credentials")));
162 String[] credentials = properties.getProperty("credentials").split(":", 2);
163 if(credentials[1].startsWith("enc:")) { // Password is encoded
164 credentials[1] = new String(Base64.getDecoder().decode(credentials[1].split(":")[1]));
166 CREDENTIALS = credentials[0] + ":" + credentials[1];
168 LOG.info("Properties applied.");
171 private void createContainers() {
172 final WriteTransaction t = dataBroker.newReadWriteTransaction();
174 FluentFuture<? extends @NonNull CommitInfo> checkedFuture = t.commit();
176 LOG.info("Create Containers succeeded!: ");
177 } catch (InterruptedException | ExecutionException e) {
178 LOG.error("Create Containers Failed: " + e);
179 LOG.error("context", e);
183 protected void initializeChild() {
188 public void close() throws Exception {
189 LOG.info("Closing provider for " + appName);
191 rpcRegistration.close();
192 LOG.info("Successfully closed provider for " + appName);
196 public void onDataTreeChanged(@Nonnull Collection changes) {
201 public ListenableFuture<RpcResult<BackupDataOutput>> backupData(BackupDataInput input) {
202 final String SVC_OPERATION = "backup-data";
203 LOG.info(appName + ":" + SVC_OPERATION + " called.");
206 String message = "Data sent to offsite location.";
211 LOG.info("Pod Name: " + POD_NAME);
212 Instant timestamp = Instant.now();
213 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HH").withZone(ZoneId.of("GMT"));
214 String timestampedArchive = DAEXIM_DIR + POD_NAME + '-' + formatter.format(timestamp) + "-" + BACKUP_ARCHIVE;
216 LOG.info("Creating archive...");
217 List<String> daeximFiles = Arrays.asList(DAEXIM_DIR + OPERATIONAL_JSON,DAEXIM_DIR + MODELS_JSON, DAEXIM_DIR + CONFIG_JSON);
218 createArchive(daeximFiles, timestampedArchive);
219 LOG.info("Archive created.");
220 } catch(IOException e) {
221 LOG.error("Error creating archive " + timestampedArchive);
222 LOG.error(e.getMessage());
224 message = "Archive creation failed.";
225 return buildBackupDataFuture(statusCode, message);
229 LOG.info("Sending archive to Nexus server: " + NEXUS_URL);
230 statusCode = Integer.toString(putArchive(timestampedArchive));
231 LOG.info("Archive sent to Nexus.");
232 } catch(IOException e) {
233 LOG.error("Nexus creation failed.", e);
235 message = "Nexus creation failed.";
238 File archive = new File(timestampedArchive);
239 if(archive.exists()) {
240 archive.delete(); // Save some space on the ODL, keep them from piling up
243 LOG.info("Sending Response statusCode=" + statusCode+ " message=" + message + " | " + SVC_OPERATION);
244 return buildBackupDataFuture(statusCode, message);
248 public ListenableFuture<RpcResult<RetrieveDataOutput>> retrieveData(RetrieveDataInput input) {
249 final String SVC_OPERATION = "retrieve-data";
250 LOG.info(appName + ":" + SVC_OPERATION + " called.");
252 String statusCode = "200";
253 String message = "Data retrieved from offsite location.";
258 LOG.info("Pod Name: " + POD_NAME);
259 String archiveIdentifier = POD_NAME + '-' + input.getTimestamp();
260 String timestampedArchive = DAEXIM_DIR + archiveIdentifier + "-" + BACKUP_ARCHIVE;
261 LOG.info("Trying to retrieve " + timestampedArchive);
263 statusCode = Integer.toString(getArchive(archiveIdentifier));
264 } catch(IOException e) {
265 LOG.error("Could not retrieve archive.", e);
267 message = "Could not retrieve archive.";
268 return retrieveDataOutputRpcResult(statusCode, message);
270 LOG.info("Retrieved archive.");
272 LOG.info("Extracting archive...");
274 extractArchive(DAEXIM_DIR + "-" + BACKUP_ARCHIVE);
275 } catch(IOException e) {
276 LOG.error("Could not extract archive.", e);
278 message = "Could not extract archive.";
279 return retrieveDataOutputRpcResult(statusCode, message);
281 LOG.info("Archive extracted.");
283 return retrieveDataOutputRpcResult(statusCode, message);
286 private boolean exportExists(List<String> daeximFiles) {
288 for(String f : daeximFiles) {
297 private void createArchive(List<String> daeximFiles, String timestampedArchive) throws IOException {
298 if(!exportExists(daeximFiles)) {
299 LOG.error("Daexim exports do not exist.");
300 throw new IOException();
302 LOG.info("Creating " + timestampedArchive);
303 FileOutputStream fileOutputStream = new FileOutputStream(timestampedArchive);
304 ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
306 FileInputStream fileInputStream;
310 for(String source : daeximFiles) {
311 LOG.info("Adding " + source + " to archive...");
312 targetZipFile = new File(source);
313 fileInputStream = new FileInputStream(targetZipFile);
314 zipEntry = new ZipEntry(targetZipFile.getName());
315 zipOutputStream.putNextEntry(zipEntry);
316 bytes = new byte[1024];
318 while((length = fileInputStream.read(bytes)) >= 0) {
319 zipOutputStream.write(bytes, 0, length);
321 fileInputStream.close();
324 zipOutputStream.close();
325 fileOutputStream.close();
328 private void extractArchive(String timestampedArchive) throws IOException {
329 byte[] bytes = new byte[1024];
330 ZipInputStream zis = new ZipInputStream(new FileInputStream(timestampedArchive));
331 ZipEntry zipEntry = zis.getNextEntry();
332 while(zipEntry != null){
333 String fileName = zipEntry.getName();
334 File newFile = new File(DAEXIM_DIR + fileName);
335 FileOutputStream fos = new FileOutputStream(newFile);
337 while ((len = zis.read(bytes)) > 0) {
338 fos.write(bytes, 0, len);
341 LOG.info(zipEntry.getName() + " extracted.");
342 zipEntry = zis.getNextEntry();
346 LOG.info(timestampedArchive + " extracted successfully.");
349 private int putArchive(String timestampedArchive) throws IOException {
350 File archive = new File(timestampedArchive);
351 HttpURLConnection connection = getNexusConnection(archive.getName());
352 connection.setRequestProperty("Content-Length", Long.toString(archive.length()));
353 connection.setRequestMethod("PUT");
354 connection.setDoOutput(true);
356 FileInputStream fileInputStream = new FileInputStream(archive);
357 OutputStream outputStream = connection.getOutputStream();
359 byte[] bytes = new byte[1024];
361 while((length = fileInputStream.read(bytes)) >= 0) {
362 outputStream.write(bytes, 0, length);
365 outputStream.flush();
366 outputStream.close();
367 fileInputStream.close();
368 connection.disconnect();
370 LOG.info("Status: " + connection.getResponseCode());
371 LOG.info("Message: " + connection.getResponseMessage());
372 return connection.getResponseCode();
375 private HttpURLConnection getNexusConnection(String archive) throws IOException {
376 URL url = new URL(NEXUS_URL + archive);
377 String auth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(CREDENTIALS.getBytes());
378 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
379 connection.addRequestProperty("Authorization", auth);
380 connection.setRequestProperty("Connection", "keep-alive");
381 connection.setRequestProperty("Proxy-Connection", "keep-alive");
385 private int getArchive(String archiveIdentifier) throws IOException {
386 File archive = new File(DAEXIM_DIR + "backup.zip");
387 if(archive.exists()) {
388 LOG.info("Recently retrieved archive found. Removing old archive...");
390 LOG.info("Archive removed.");
392 HttpURLConnection connection = getNexusConnection( archiveIdentifier + "-" + BACKUP_ARCHIVE);
393 connection.setRequestMethod("GET");
394 connection.setDoInput(true);
396 InputStream connectionInputStream = connection.getInputStream();
397 FileOutputStream fileOutputStream = new FileOutputStream(archive);
399 byte[] bytes = new byte[1024];
401 while((length = connectionInputStream.read(bytes)) >= 0) { // while connection has bytes
402 fileOutputStream.write(bytes, 0, length); // write to archive
404 connection.disconnect();
406 LOG.info("Status: " + connection.getResponseCode());
407 LOG.info("Message: " + connection.getResponseMessage());
408 LOG.info(archive.getName() + " successfully created.");
409 return connection.getResponseCode();
412 private ListenableFuture<RpcResult<BackupDataOutput>> buildBackupDataFuture(String statusCode, String message) {
413 BackupDataOutputBuilder outputBuilder = new BackupDataOutputBuilder();
414 outputBuilder.setStatus(statusCode);
415 outputBuilder.setMessage(message);
416 RpcResult<BackupDataOutput> rpcResult = RpcResultBuilder.<BackupDataOutput> status(true).withResult(outputBuilder.build()).build();
417 return Futures.immediateFuture(rpcResult);
420 private ListenableFuture<RpcResult<RetrieveDataOutput>> retrieveDataOutputRpcResult(String status, String message) {
421 RetrieveDataOutputBuilder outputBuilder = new RetrieveDataOutputBuilder();
422 outputBuilder.setStatus(status);
423 outputBuilder.setMessage(message);
424 RpcResult<RetrieveDataOutput> rpcResult = RpcResultBuilder.<RetrieveDataOutput> status(true).withResult(outputBuilder.build()).build();
425 return Futures.immediateFuture(rpcResult);