7ee2a91d7c0201c257885ca1d32c534791a4e4a7
[ccsdk/sli.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights
6  *                      reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20 */
21
22 package org.onap.ccsdk.sli.northbound.daeximoffsitebackup;
23
24 import com.google.common.util.concurrent.FluentFuture;
25 import com.google.common.util.concurrent.Futures;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import java.io.File;
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;
34 import java.net.URL;
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;
68
69 public class DaeximOffsiteBackupProvider implements AutoCloseable, DaeximOffsiteBackupService, DataTreeChangeListener {
70     private static final Logger LOG = LoggerFactory.getLogger(DaeximOffsiteBackupProvider.class);
71
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";
80
81     private static final String BACKUP_ARCHIVE = "odl_backup.zip";
82     private static final String appName = "daexim-offsite-backup";
83
84     private final ExecutorService executor;
85     private Properties properties;
86     private DataBroker dataBroker;
87     private RpcProviderService rpcRegistry;
88     private ObjectRegistration<DaeximOffsiteBackupService> rpcRegistration;
89
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;
96         initialize();
97     }
98
99     public void initialize() {
100         LOG.info("Initializing provider for " + appName);
101         // Create the top level containers
102         createContainers();
103         try {
104             DaeximOffsiteBackupUtil.loadProperties();
105         } catch (Exception e) {
106             LOG.error("Caught Exception while trying to load properties file", e);
107         }
108         rpcRegistration = rpcRegistry.registerRpcImplementation(DaeximOffsiteBackupService.class, this);
109         LOG.info("Initialization complete for " + appName);
110     }
111
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");
126             return;
127         }
128         FileInputStream fileInputStream;
129         try {
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);
142         }
143     }
144
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");
150         }
151         DAEXIM_DIR =  properties.getProperty("daeximDirectory");
152         NEXUS_URL = properties.getProperty("nexusUrl");
153
154         OPERATIONAL_JSON = properties.getProperty("file.operational");
155         MODELS_JSON = properties.getProperty("file.models");
156         CONFIG_JSON = properties.getProperty("file.config");
157
158         if(!properties.getProperty("credentials").contains(":")) { //Entire thing is encoded
159             CREDENTIALS = new String(Base64.getDecoder().decode(properties.getProperty("credentials")));
160         }
161         else {
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]));
165             }
166             CREDENTIALS = credentials[0] + ":" + credentials[1];
167         }
168         LOG.info("Properties applied.");
169     }
170
171     private void createContainers() {
172         final WriteTransaction t = dataBroker.newReadWriteTransaction();
173         try {
174             FluentFuture<? extends @NonNull CommitInfo> checkedFuture = t.commit();
175             checkedFuture.get();
176             LOG.info("Create Containers succeeded!: ");
177         } catch (InterruptedException | ExecutionException e) {
178             LOG.error("Create Containers Failed: " + e);
179             LOG.error("context", e);
180         }
181     }
182
183     protected void initializeChild() {
184
185     }
186
187     @Override
188     public void close() throws Exception {
189         LOG.info("Closing provider for " + appName);
190         executor.shutdown();
191         rpcRegistration.close();
192         LOG.info("Successfully closed provider for " + appName);
193     }
194
195     @Override
196     public void onDataTreeChanged(@Nonnull Collection changes) {
197
198     }
199
200     @Override
201     public ListenableFuture<RpcResult<BackupDataOutput>> backupData(BackupDataInput input) {
202         final String SVC_OPERATION = "backup-data";
203         LOG.info(appName + ":" + SVC_OPERATION + " called.");
204
205         String statusCode;
206         String message = "Data sent to offsite location.";
207
208         loadProperties();
209         applyProperties();
210
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;
215         try {
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());
223             statusCode = "500";
224             message = "Archive creation failed.";
225             return buildBackupDataFuture(statusCode, message);
226         }
227
228         try{
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);
234             statusCode = "500";
235             message = "Nexus creation failed.";
236         }
237
238         File archive = new File(timestampedArchive);
239         if(archive.exists()) {
240             archive.delete(); // Save some space on the ODL, keep them from piling up
241         }
242
243         LOG.info("Sending Response statusCode=" + statusCode+ " message=" + message + " | " + SVC_OPERATION);
244         return buildBackupDataFuture(statusCode, message);
245     }
246
247     @Override
248     public ListenableFuture<RpcResult<RetrieveDataOutput>> retrieveData(RetrieveDataInput input) {
249         final String SVC_OPERATION = "retrieve-data";
250         LOG.info(appName + ":" + SVC_OPERATION + " called.");
251
252         String statusCode = "200";
253         String message = "Data retrieved from offsite location.";
254
255         loadProperties();
256         applyProperties();
257
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);
262         try {
263             statusCode = Integer.toString(getArchive(archiveIdentifier));
264         } catch(IOException e) {
265             LOG.error("Could not retrieve archive.", e);
266             statusCode = "500";
267             message = "Could not retrieve archive.";
268             return retrieveDataOutputRpcResult(statusCode, message);
269         }
270         LOG.info("Retrieved archive.");
271
272         LOG.info("Extracting archive...");
273         try {
274             extractArchive(DAEXIM_DIR + "-" + BACKUP_ARCHIVE);
275         } catch(IOException e) {
276             LOG.error("Could not extract archive.", e);
277             statusCode = "500";
278             message = "Could not extract archive.";
279             return retrieveDataOutputRpcResult(statusCode, message);
280         }
281         LOG.info("Archive extracted.");
282
283         return retrieveDataOutputRpcResult(statusCode, message);
284     }
285
286     private boolean exportExists(List<String> daeximFiles) {
287         File file;
288         for(String f : daeximFiles) {
289             file = new File(f);
290             if(!file.exists()) {
291                 return false;
292             }
293         }
294         return true;
295     }
296
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();
301         }
302         LOG.info("Creating " + timestampedArchive);
303         FileOutputStream fileOutputStream = new FileOutputStream(timestampedArchive);
304         ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
305         File targetZipFile;
306         FileInputStream fileInputStream;
307         ZipEntry zipEntry;
308         byte[] bytes;
309         int length;
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];
317
318             while((length = fileInputStream.read(bytes)) >= 0) {
319                 zipOutputStream.write(bytes, 0, length);
320             }
321             fileInputStream.close();
322         }
323
324         zipOutputStream.close();
325         fileOutputStream.close();
326     }
327
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);
336             int len;
337             while ((len = zis.read(bytes)) > 0) {
338                 fos.write(bytes, 0, len);
339             }
340             fos.close();
341             LOG.info(zipEntry.getName() + " extracted.");
342             zipEntry = zis.getNextEntry();
343         }
344         zis.closeEntry();
345         zis.close();
346         LOG.info(timestampedArchive + " extracted successfully.");
347     }
348
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);
355
356         FileInputStream fileInputStream = new FileInputStream(archive);
357         OutputStream outputStream = connection.getOutputStream();
358
359         byte[] bytes = new byte[1024];
360         int length;
361         while((length = fileInputStream.read(bytes)) >= 0) {
362             outputStream.write(bytes, 0, length);
363         }
364
365         outputStream.flush();
366         outputStream.close();
367         fileInputStream.close();
368         connection.disconnect();
369
370         LOG.info("Status: " + connection.getResponseCode());
371         LOG.info("Message: " + connection.getResponseMessage());
372         return connection.getResponseCode();
373     }
374
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");
382         return connection;
383     }
384
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...");
389             archive.delete();
390             LOG.info("Archive removed.");
391         }
392         HttpURLConnection connection = getNexusConnection( archiveIdentifier + "-" + BACKUP_ARCHIVE);
393         connection.setRequestMethod("GET");
394         connection.setDoInput(true);
395
396         InputStream connectionInputStream = connection.getInputStream();
397         FileOutputStream fileOutputStream = new FileOutputStream(archive);
398
399         byte[] bytes = new byte[1024];
400         int length;
401         while((length = connectionInputStream.read(bytes)) >= 0) { // while connection has bytes
402             fileOutputStream.write(bytes, 0, length); // write to archive
403         }
404         connection.disconnect();
405
406         LOG.info("Status: " + connection.getResponseCode());
407         LOG.info("Message: " + connection.getResponseMessage());
408         LOG.info(archive.getName() + " successfully created.");
409         return connection.getResponseCode();
410     }
411
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);
418     }
419
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);
426     }
427 }