From ff3eecb980bfdc8d43d2ed3a4c786d634fa6f4e2 Mon Sep 17 00:00:00 2001 From: Jessica Wagantall Date: Tue, 1 Dec 2020 11:52:01 -0800 Subject: Migrate sli-northbound repo Migrate sli-northbound repo files into new directory "northbound". Signed-off-by: Jessica Wagantall --- northbound/daexim-offsite-backup/.gitignore | 38 ++ northbound/daexim-offsite-backup/README.md | 87 +++++ northbound/daexim-offsite-backup/installer/pom.xml | 129 +++++++ .../src/assembly/assemble_installer_zip.xml | 56 +++ .../src/assembly/assemble_mvnrepo_zip.xml | 47 +++ .../src/main/resources/scripts/install-feature.sh | 39 ++ northbound/daexim-offsite-backup/model/.gitignore | 1 + northbound/daexim-offsite-backup/model/pom.xml | 27 ++ .../model/scripts/python/yang2props.py | 52 +++ .../model/src/main/yang/daexim-offsite-backup.yang | 46 +++ northbound/daexim-offsite-backup/pom.xml | 47 +++ .../daexim-offsite-backup/provider/.gitignore | 3 + northbound/daexim-offsite-backup/provider/pom.xml | 95 +++++ .../DaeximOffsiteBackupProvider.java | 429 +++++++++++++++++++++ .../DaeximOffsiteBackupUtil.java | 78 ++++ .../OSGI-INF/blueprint/DaeximOffsiteBackup.xml | 20 + .../resources/daexim-offsite-backup.properties | 28 ++ .../opendaylight/blueprint/DaeximOffsiteBackup.xml | 20 + .../DaeximOffsiteBackupProviderTest.java | 216 +++++++++++ .../DaeximOffsiteBackupUtilTest.java | 40 ++ .../resources/daexim-offsite-backup.properties | 26 ++ 21 files changed, 1524 insertions(+) create mode 100755 northbound/daexim-offsite-backup/.gitignore create mode 100755 northbound/daexim-offsite-backup/README.md create mode 100755 northbound/daexim-offsite-backup/installer/pom.xml create mode 100755 northbound/daexim-offsite-backup/installer/src/assembly/assemble_installer_zip.xml create mode 100755 northbound/daexim-offsite-backup/installer/src/assembly/assemble_mvnrepo_zip.xml create mode 100644 northbound/daexim-offsite-backup/installer/src/main/resources/scripts/install-feature.sh create mode 100755 northbound/daexim-offsite-backup/model/.gitignore create mode 100755 northbound/daexim-offsite-backup/model/pom.xml create mode 100755 northbound/daexim-offsite-backup/model/scripts/python/yang2props.py create mode 100755 northbound/daexim-offsite-backup/model/src/main/yang/daexim-offsite-backup.yang create mode 100755 northbound/daexim-offsite-backup/pom.xml create mode 100755 northbound/daexim-offsite-backup/provider/.gitignore create mode 100755 northbound/daexim-offsite-backup/provider/pom.xml create mode 100755 northbound/daexim-offsite-backup/provider/src/main/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupProvider.java create mode 100755 northbound/daexim-offsite-backup/provider/src/main/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupUtil.java create mode 100755 northbound/daexim-offsite-backup/provider/src/main/resources/OSGI-INF/blueprint/DaeximOffsiteBackup.xml create mode 100755 northbound/daexim-offsite-backup/provider/src/main/resources/daexim-offsite-backup.properties create mode 100755 northbound/daexim-offsite-backup/provider/src/main/resources/org/opendaylight/blueprint/DaeximOffsiteBackup.xml create mode 100644 northbound/daexim-offsite-backup/provider/src/test/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupProviderTest.java create mode 100644 northbound/daexim-offsite-backup/provider/src/test/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupUtilTest.java create mode 100755 northbound/daexim-offsite-backup/provider/src/test/resources/daexim-offsite-backup.properties (limited to 'northbound/daexim-offsite-backup') diff --git a/northbound/daexim-offsite-backup/.gitignore b/northbound/daexim-offsite-backup/.gitignore new file mode 100755 index 000000000..a01e90efe --- /dev/null +++ b/northbound/daexim-offsite-backup/.gitignore @@ -0,0 +1,38 @@ +tandard .git ignore entries##### + +## IDE Specific Files ## +org.eclipse.core.resources.prefs +.classpath +.project +.settings +.idea +.externalToolBuilders +maven-eclipse.xml +workspace + +## Compilation Files ## +*.class +**/target +target +target-ide +MANIFEST.MF + +## Misc Ignores (OS specific etc) ## +bin/ +dist +*~ +*.ipr +*.iml +*.iws +classes +out/ +.DS_STORE +.metadata + +## Folders which contain auto generated source code ## +yang-gen-config +yang-gen-sal + +#####Archetype specific .git ignore entries####### +generate +Archetype_Next_Steps.README diff --git a/northbound/daexim-offsite-backup/README.md b/northbound/daexim-offsite-backup/README.md new file mode 100755 index 000000000..e3f13aee9 --- /dev/null +++ b/northbound/daexim-offsite-backup/README.md @@ -0,0 +1,87 @@ +Introduction +====================== +You have generated an MD-SAL module. + +* You should be able to successfully run ```mvn clean install``` on this project. + +Next Steps +====================== +* run a ```mvn clean install``` if you haven't already. This will generate some code from the yang models. +* Modify the model yang file under the model project. +* Follow the comments in the generated provider class to wire your new provider into the generated +code. +* Modify the generated provider model to respond to and handle the yang model. Depending on what +you added to your model you may need to inherit additional interfaces or make other changes to +the provider model. + +Generated Bundles +====================== +* model + - Provides the yang model for your application. This is your primary northbound interface. +* provider + - Provides a template implementation for a provider to respond to your yang model. +* features + - Defines a karaf feature. If you add dependencies on third-party bundles then you will need to + modify the features.xml to list out the dependencies. +* installer + - Bundles all of the jars and third party dependencies (minus ODL dependencies) into a single + .zip file. + +Usage +====================== +## Purpose +The purpose of this ODL feature is to support local and geo-redundancy by providing a way to +back up and retrieve MD-SAL data exports to and from a Sonatype Nexus server. In order to function, +this module requires the controller to have an installation of the ```data-export-import``` module and a valid export of MD-SAL data. + +## Backup +MD-SAL Backup can be achieved using the ```daexim-offsite-backup:backup-data``` RPC either through the +RESTConf portal or through a tool such as cURL or Postman. While no input is required for this RPC, +the RPC does require the operational, models, and config .JSONs to be present in the daexim directory of the controller. +```sh +export USER=user +export PASSWORD=password +export ODL_HOST=https://yourhost.com:8181 +curl -X POST -u$USER:$PASSWORD ${ODL_HOST}/restconf/operations/daexim-offsite-backup:backup-data +``` +Through this process a timestamped archive is created in the form of ```POD_NAME-yyyyMMdd_HH-odl_backup.zip``` +where ```POD_NAME``` is the name of the ODL, specified through the properties file or through an environment variable. + +## Retrieval +MD-SAL Retrieval can be achieved by using the ```daexim-offsite-backup:retrieve-data``` RPC either through the +RESTConf portal or through a tool such as cURL or Postman. This RPC requires timestamp information and may +be supplied with an optional podName. + +```sh +export USER=user +export PASSWORD=password +export ODL_HOST=https://yourhost.com:8181 +export TARGET_ODL=targetOdlPodName +export TIMESTAMP=yyyyMMdd_HH +export DATA= ' + { + "input": { + "pod-name": "'"$TARGET_ODL"'", + "timestamp": "'"$TIMESTAMP"'" + } + }' +curl -X POST -u$USER:$PASSWORD --data $DATA ${ODL_HOST}/restconf/operations/daexim-offsite-backup:retrieve-data +``` + +Through this process an archive with the specified timestamp (and optional pod name) is downloaded from +the Nexus server and extracted into the controller's daexim directory. After this it is up to the user +to trigger an MD-SAL import. + +## Properties File +Before each RPC execution this module pulls information from a user supplied properties file. The module expects to find: +- daeximDirectory +- credentials +- nexusUrl +- podName +- file.operational +- file.models +- file.config + +> Refer to the example properties file + +If the module cannot find the properties file it will default to generic values and attempt to move forward. diff --git a/northbound/daexim-offsite-backup/installer/pom.xml b/northbound/daexim-offsite-backup/installer/pom.xml new file mode 100755 index 000000000..6fbabb426 --- /dev/null +++ b/northbound/daexim-offsite-backup/installer/pom.xml @@ -0,0 +1,129 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + odlparent-lite + 2.1.0 + + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-installer + 1.1.1-SNAPSHOT + pom + + ccsdk-sli-northbound :: daexim-offsite-backup :: ${project.artifactId} + + + ccsdk-daexim-offsite-backup + ${application.name} + mvn:org.onap.ccsdk.sli.northbound/${features.boot}/${project.version}/xml/features + false + + + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-provider + ${project.version} + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-model + ${project.version} + + + + + + + maven-assembly-plugin + 2.6 + + + maven-repo-zip + + single + + package + + true + stage/${application.name}-${project.version} + + src/assembly/assemble_mvnrepo_zip.xml + + true + + + + installer-zip + + single + + package + + true + ${application.name}-${project.version}-installer + + src/assembly/assemble_installer_zip.xml + + false + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + + copy-dependencies + + prepare-package + + false + ${project.build.directory}/assembly/system + false + true + true + true + false + false + org.onap.ccsdk.sli.northbound + provided + + + + + + maven-resources-plugin + 2.6 + + + copy-version + + copy-resources + + validate + + ${basedir}/target/stage + + + src/main/resources/scripts + + install-feature.sh + + true + + + + + + + + + diff --git a/northbound/daexim-offsite-backup/installer/src/assembly/assemble_installer_zip.xml b/northbound/daexim-offsite-backup/installer/src/assembly/assemble_installer_zip.xml new file mode 100755 index 000000000..41d23e88a --- /dev/null +++ b/northbound/daexim-offsite-backup/installer/src/assembly/assemble_installer_zip.xml @@ -0,0 +1,56 @@ + + + + + + installer_zip + + zip + + + + false + + + + target/stage/ + ${application.name} + 755 + + *.sh + + + + target/stage/ + ${application.name} + 644 + + *.sh + + + + diff --git a/northbound/daexim-offsite-backup/installer/src/assembly/assemble_mvnrepo_zip.xml b/northbound/daexim-offsite-backup/installer/src/assembly/assemble_mvnrepo_zip.xml new file mode 100755 index 000000000..275060986 --- /dev/null +++ b/northbound/daexim-offsite-backup/installer/src/assembly/assemble_mvnrepo_zip.xml @@ -0,0 +1,47 @@ + + + + + + repo + + zip + + + + false + + + + target/assembly/ + . + + + + + + diff --git a/northbound/daexim-offsite-backup/installer/src/main/resources/scripts/install-feature.sh b/northbound/daexim-offsite-backup/installer/src/main/resources/scripts/install-feature.sh new file mode 100644 index 000000000..1d7be149e --- /dev/null +++ b/northbound/daexim-offsite-backup/installer/src/main/resources/scripts/install-feature.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +### +# ============LICENSE_START======================================================= +# openECOMP : SDN-C +# ================================================================================ +# Copyright (C) 2018 AT&T Intellectual Property. All rights +# reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= +### + +ODL_HOME=${ODL_HOME:-/opt/opendaylight/current} +ODL_KARAF_CLIENT=${ODL_KARAF_CLIENT:-${ODL_HOME}/bin/client} +INSTALLERDIR=$(dirname $0) + +REPOZIP=${INSTALLERDIR}/${features.boot}-${project.version}-repo.zip + +if [ -f ${REPOZIP} ] +then + unzip -d ${ODL_HOME} ${REPOZIP} +else + echo "ERROR : repo zip ($REPOZIP) not found" + exit 1 +fi + +${ODL_KARAF_CLIENT} feature:repo-add ${features.repositories} +${ODL_KARAF_CLIENT} feature:install ${features.boot} diff --git a/northbound/daexim-offsite-backup/model/.gitignore b/northbound/daexim-offsite-backup/model/.gitignore new file mode 100755 index 000000000..eacf31a67 --- /dev/null +++ b/northbound/daexim-offsite-backup/model/.gitignore @@ -0,0 +1 @@ +/target-ide/ diff --git a/northbound/daexim-offsite-backup/model/pom.xml b/northbound/daexim-offsite-backup/model/pom.xml new file mode 100755 index 000000000..4f338c786 --- /dev/null +++ b/northbound/daexim-offsite-backup/model/pom.xml @@ -0,0 +1,27 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + binding-parent + 2.1.0 + + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-model + 1.1.1-SNAPSHOT + bundle + + ccsdk-sli-northbound :: daexim-offsite-backup :: ${project.artifactId} + + + + org.opendaylight.mdsal.binding.model.ietf + rfc6991 + + + + + diff --git a/northbound/daexim-offsite-backup/model/scripts/python/yang2props.py b/northbound/daexim-offsite-backup/model/scripts/python/yang2props.py new file mode 100755 index 000000000..97ceaa760 --- /dev/null +++ b/northbound/daexim-offsite-backup/model/scripts/python/yang2props.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +import re +import sys + + +# Convert word from foo-bar to FooBar +# words begining with a digit will be converted to _digit +def to_enum(s): + if s[0].isdigit(): + s = "_" + s + else: + s = s[0].upper() + s[1:] + return re.sub(r'(?!^)-([a-zA-Z])', lambda m: m.group(1).upper(), s) + +leaf = "" +val = "" +li = [] + +if len(sys.argv) < 3: + print('yang2props.py ') + sys.exit(2) + +with open(sys.argv[1], "r") as ins: + for line in ins: + # if we see a leaf save the name for later + if "leaf " in line: + match = re.search(r'leaf (\S+)', line) + if match: + leaf = match.group(1) + + # if we see enum convert the value to enum format and see if it changed + # if the value is different write a property entry + if "enum " in line: + match = re.search(r'enum "(\S+)";', line) + if match: + val = match.group(1) + enum = to_enum(val) + + # see if converting to enum changed the string + if val != enum: + property = "yang."+leaf+"."+enum+"="+val + if property not in li: + li.append( property) + + +with open(sys.argv[2], "w") as fo: + fo.write("# yang conversion properties \n") + fo.write("# used to convert Enum back to the original yang value \n") + fo.write("\n".join(li)) + fo.write("\n") + diff --git a/northbound/daexim-offsite-backup/model/src/main/yang/daexim-offsite-backup.yang b/northbound/daexim-offsite-backup/model/src/main/yang/daexim-offsite-backup.yang new file mode 100755 index 000000000..363136caf --- /dev/null +++ b/northbound/daexim-offsite-backup/model/src/main/yang/daexim-offsite-backup.yang @@ -0,0 +1,46 @@ +module daexim-offsite-backup{ + namespace "org:onap:ccsdk:sli:northbound:daeximoffsitebackup"; + prefix daexim-offsite-backup; + + import ietf-inet-types { + prefix inet; + } + import ietf-yang-types { + prefix yang; + } + description + "This ODL feature is designed for transferring MD-SAL data + to an offsite location in the ECOMP-C containerized environments."; + revision "2018-09-26" { + description + "Release 19.02 draft"; + } + + rpc backup-data { + output { + leaf status { type string; } + leaf message { type string; } + } + } + + rpc retrieve-data { + input { + leaf pod-name { + type string; + description + "Name of the desired MD-SAL backup's pod. If not supplied will + default to the name of this pod."; + } + leaf timestamp { + type string; + description + "Timestamp of the desired backup. Format: yyyyMMdd_HH"; + mandatory true; + } + } + output { + leaf status { type string; } + leaf message { type string; } + } + } +}////closes the module \ No newline at end of file diff --git a/northbound/daexim-offsite-backup/pom.xml b/northbound/daexim-offsite-backup/pom.xml new file mode 100755 index 000000000..aa98f5137 --- /dev/null +++ b/northbound/daexim-offsite-backup/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + odlparent-lite + 2.1.0 + + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup + 1.1.1-SNAPSHOT + pom + + ccsdk-sli-northbound :: daexim-offsite-backup + ODL feature used for transferring MD-SAL data to an offsite location. + + + model + provider + installer + + + + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-features + ${project.version} + xml + features + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-model + ${project.version} + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-provider + ${project.version} + + + + diff --git a/northbound/daexim-offsite-backup/provider/.gitignore b/northbound/daexim-offsite-backup/provider/.gitignore new file mode 100755 index 000000000..527bb69aa --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/.gitignore @@ -0,0 +1,3 @@ +/target-ide/ +fileToZip1 +fileToZip2 \ No newline at end of file diff --git a/northbound/daexim-offsite-backup/provider/pom.xml b/northbound/daexim-offsite-backup/provider/pom.xml new file mode 100755 index 000000000..8d060bb94 --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + + + org.onap.ccsdk.parent + binding-parent + 2.1.0 + + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-provider + 1.1.1-SNAPSHOT + bundle + + ccsdk-sli-northbound :: daexim-offsite-backup :: ${project.artifactId} + + + + org.onap.ccsdk.sli.northbound + daexim-offsite-backup-model + ${project.version} + + + org.opendaylight.controller + sal-binding-api + + + org.opendaylight.controller + sal-common-util + + + org.opendaylight.controller + sal-test-model + test + + + org.opendaylight.controller + sal-binding-broker-impl + test + + + org.opendaylight.controller + sal-binding-broker-impl + test-jar + tests + test + + + org.apache.commons + commons-lang3 + + + org.onap.ccsdk.sli.core + sli-common + ${sdnctl.sli.version} + + + org.onap.ccsdk.sli.core + sli-provider + ${sdnctl.sli.version} + + + com.google.code.findbugs + annotations + + + junit + junit + test + + + org.mockito + mockito-core + test + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + org.opendaylight.controller.config.yang.config.daexim-offsite-backup_provider + * + + + + + + diff --git a/northbound/daexim-offsite-backup/provider/src/main/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupProvider.java b/northbound/daexim-offsite-backup/provider/src/main/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupProvider.java new file mode 100755 index 000000000..09c8f92f4 --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/src/main/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupProvider.java @@ -0,0 +1,429 @@ +/* + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= +*/ + +package org.onap.ccsdk.sli.northbound.daeximoffsitebackup; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.Base64; +import java.util.Collection; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; +import javax.annotation.Nonnull; + +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.BackupDataInput; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.BackupDataOutput; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.BackupDataOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.DaeximOffsiteBackupService; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataOutput; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataOutputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataInput; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.RpcResultBuilder; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DaeximOffsiteBackupProvider implements AutoCloseable, DaeximOffsiteBackupService, DataTreeChangeListener { + private static final Logger LOG = LoggerFactory.getLogger(DaeximOffsiteBackupProvider.class); + + private static String DAEXIM_DIR; + private static String CREDENTIALS; + private static String NEXUS_URL; + private static String POD_NAME; + private static String OPERATIONAL_JSON; + private static String MODELS_JSON; + private static String CONFIG_JSON; + private static String PROPERTIES_FILE = System.getenv("SDNC_CONFIG_DIR") + "/daexim-offsite-backup.properties"; + + private static final String BACKUP_ARCHIVE = "odl_backup.zip"; + private static final String appName = "daexim-offsite-backup"; + + private final ExecutorService executor; + private Properties properties; + private DataBroker dataBroker; + private RpcProviderRegistry rpcRegistry; + private BindingAwareBroker.RpcRegistration rpcRegistration; + + public DaeximOffsiteBackupProvider(DataBroker dataBroker, + RpcProviderRegistry rpcProviderRegistry) { + LOG.info("Creating provider for " + appName); + this.executor = Executors.newFixedThreadPool(1); + this.dataBroker = dataBroker; + this.rpcRegistry = rpcProviderRegistry; + initialize(); + } + + public void initialize() { + LOG.info("Initializing provider for " + appName); + // Create the top level containers + createContainers(); + try { + DaeximOffsiteBackupUtil.loadProperties(); + } catch (Exception e) { + LOG.error("Caught Exception while trying to load properties file", e); + } + rpcRegistration = rpcRegistry.addRpcImplementation(DaeximOffsiteBackupService.class, this); + LOG.info("Initialization complete for " + appName); + } + + private void loadProperties() { + LOG.info("Loading properties from " + PROPERTIES_FILE); + if(properties == null) + properties = new Properties(); + File propertiesFile = new File(PROPERTIES_FILE); + if(!propertiesFile.exists()) { + LOG.warn("Properties file (" + PROPERTIES_FILE + ") not found. Using default properties."); + properties.put("daeximDirectory", "/opt/opendaylight/current/daexim/"); + properties.put("credentials", "admin:enc:YWRtaW4xMjM="); + properties.put("nexusUrl", "http://localhost:8081/nexus/content/repositories/"); + properties.put("podName", "UNKNOWN_ODL"); + properties.put("file.operational", "odl_backup_operational.json"); + properties.put("file.models", "odl_backup_models.json"); + properties.put("file.config", "odl_backup_config.json"); + return; + } + FileInputStream fileInputStream; + try { + fileInputStream = new FileInputStream(propertiesFile); + properties.load(fileInputStream); + fileInputStream.close(); + LOG.info(properties.size() + " properties loaded."); + LOG.info("daeximDirectory: " + properties.getProperty("daeximDirectory")); + LOG.info("nexusUrl: " + properties.getProperty("nexusUrl")); + LOG.info("podName: " + properties.getProperty("podName")); + LOG.info("file.operational: " + properties.getProperty("file.operational")); + LOG.info("file.models: " + properties.getProperty("file.models")); + LOG.info("file.config: " + properties.getProperty("file.config")); + } catch(IOException e) { + LOG.error("Error loading properties.", e); + } + } + + private void applyProperties() { + LOG.info("Applying properties..."); + if(POD_NAME == null || POD_NAME.isEmpty()) { + LOG.warn("MY_POD_NAME environment variable not set. Using value from properties."); + POD_NAME = properties.getProperty("podName"); + } + DAEXIM_DIR = properties.getProperty("daeximDirectory"); + NEXUS_URL = properties.getProperty("nexusUrl"); + + OPERATIONAL_JSON = properties.getProperty("file.operational"); + MODELS_JSON = properties.getProperty("file.models"); + CONFIG_JSON = properties.getProperty("file.config"); + + if(!properties.getProperty("credentials").contains(":")) { //Entire thing is encoded + CREDENTIALS = new String(Base64.getDecoder().decode(properties.getProperty("credentials"))); + } + else { + String[] credentials = properties.getProperty("credentials").split(":", 2); + if(credentials[1].startsWith("enc:")) { // Password is encoded + credentials[1] = new String(Base64.getDecoder().decode(credentials[1].split(":")[1])); + } + CREDENTIALS = credentials[0] + ":" + credentials[1]; + } + LOG.info("Properties applied."); + } + + private void createContainers() { + final WriteTransaction t = dataBroker.newReadWriteTransaction(); + try { + CheckedFuture checkedFuture = t.submit(); + checkedFuture.get(); + LOG.info("Create Containers succeeded!: "); + } catch (InterruptedException | ExecutionException e) { + LOG.error("Create Containers Failed: " + e); + LOG.error("context", e); + } + } + + protected void initializeChild() { + + } + + @Override + public void close() throws Exception { + LOG.info("Closing provider for " + appName); + executor.shutdown(); + rpcRegistration.close(); + LOG.info("Successfully closed provider for " + appName); + } + + @Override + public void onDataTreeChanged(@Nonnull Collection changes) { + + } + + @Override + public ListenableFuture> backupData(BackupDataInput input) { + final String SVC_OPERATION = "backup-data"; + LOG.info(appName + ":" + SVC_OPERATION + " called."); + + String statusCode; + String message = "Data sent to offsite location."; + + loadProperties(); + applyProperties(); + + LOG.info("Pod Name: " + POD_NAME); + Instant timestamp = Instant.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd_HH").withZone(ZoneId.of("GMT")); + String timestampedArchive = DAEXIM_DIR + POD_NAME + '-' + formatter.format(timestamp) + "-" + BACKUP_ARCHIVE; + try { + LOG.info("Creating archive..."); + List daeximFiles = Arrays.asList(DAEXIM_DIR + OPERATIONAL_JSON,DAEXIM_DIR + MODELS_JSON, DAEXIM_DIR + CONFIG_JSON); + createArchive(daeximFiles, timestampedArchive); + LOG.info("Archive created."); + } catch(IOException e) { + LOG.error("Error creating archive " + timestampedArchive); + LOG.error(e.getMessage()); + statusCode = "500"; + message = "Archive creation failed."; + return buildBackupDataFuture(statusCode, message); + } + + try{ + LOG.info("Sending archive to Nexus server: " + NEXUS_URL); + statusCode = Integer.toString(putArchive(timestampedArchive)); + LOG.info("Archive sent to Nexus."); + } catch(IOException e) { + LOG.error("Nexus creation failed.", e); + statusCode = "500"; + message = "Nexus creation failed."; + } + + File archive = new File(timestampedArchive); + if(archive.exists()) { + archive.delete(); // Save some space on the ODL, keep them from piling up + } + + LOG.info("Sending Response statusCode=" + statusCode+ " message=" + message + " | " + SVC_OPERATION); + return buildBackupDataFuture(statusCode, message); + } + + @Override + public ListenableFuture> retrieveData(RetrieveDataInput input) { + final String SVC_OPERATION = "retrieve-data"; + LOG.info(appName + ":" + SVC_OPERATION + " called."); + + String statusCode = "200"; + String message = "Data retrieved from offsite location."; + + loadProperties(); + applyProperties(); + + LOG.info("Pod Name: " + POD_NAME); + String archiveIdentifier = POD_NAME + '-' + input.getTimestamp(); + String timestampedArchive = DAEXIM_DIR + archiveIdentifier + "-" + BACKUP_ARCHIVE; + LOG.info("Trying to retrieve " + timestampedArchive); + try { + statusCode = Integer.toString(getArchive(archiveIdentifier)); + } catch(IOException e) { + LOG.error("Could not retrieve archive.", e); + statusCode = "500"; + message = "Could not retrieve archive."; + return retrieveDataOutputRpcResult(statusCode, message); + } + LOG.info("Retrieved archive."); + + LOG.info("Extracting archive..."); + try { + extractArchive(DAEXIM_DIR + "-" + BACKUP_ARCHIVE); + } catch(IOException e) { + LOG.error("Could not extract archive.", e); + statusCode = "500"; + message = "Could not extract archive."; + return retrieveDataOutputRpcResult(statusCode, message); + } + LOG.info("Archive extracted."); + + return retrieveDataOutputRpcResult(statusCode, message); + } + + private boolean exportExists(List daeximFiles) { + File file; + for(String f : daeximFiles) { + file = new File(f); + if(!file.exists()) { + return false; + } + } + return true; + } + + private void createArchive(List daeximFiles, String timestampedArchive) throws IOException { + if(!exportExists(daeximFiles)) { + LOG.error("Daexim exports do not exist."); + throw new IOException(); + } + LOG.info("Creating " + timestampedArchive); + FileOutputStream fileOutputStream = new FileOutputStream(timestampedArchive); + ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream); + File targetZipFile; + FileInputStream fileInputStream; + ZipEntry zipEntry; + byte[] bytes; + int length; + for(String source : daeximFiles) { + LOG.info("Adding " + source + " to archive..."); + targetZipFile = new File(source); + fileInputStream = new FileInputStream(targetZipFile); + zipEntry = new ZipEntry(targetZipFile.getName()); + zipOutputStream.putNextEntry(zipEntry); + bytes = new byte[1024]; + + while((length = fileInputStream.read(bytes)) >= 0) { + zipOutputStream.write(bytes, 0, length); + } + fileInputStream.close(); + } + + zipOutputStream.close(); + fileOutputStream.close(); + } + + private void extractArchive(String timestampedArchive) throws IOException { + byte[] bytes = new byte[1024]; + ZipInputStream zis = new ZipInputStream(new FileInputStream(timestampedArchive)); + ZipEntry zipEntry = zis.getNextEntry(); + while(zipEntry != null){ + String fileName = zipEntry.getName(); + File newFile = new File(DAEXIM_DIR + fileName); + FileOutputStream fos = new FileOutputStream(newFile); + int len; + while ((len = zis.read(bytes)) > 0) { + fos.write(bytes, 0, len); + } + fos.close(); + LOG.info(zipEntry.getName() + " extracted."); + zipEntry = zis.getNextEntry(); + } + zis.closeEntry(); + zis.close(); + LOG.info(timestampedArchive + " extracted successfully."); + } + + private int putArchive(String timestampedArchive) throws IOException { + File archive = new File(timestampedArchive); + HttpURLConnection connection = getNexusConnection(archive.getName()); + connection.setRequestProperty("Content-Length", Long.toString(archive.length())); + connection.setRequestMethod("PUT"); + connection.setDoOutput(true); + + FileInputStream fileInputStream = new FileInputStream(archive); + OutputStream outputStream = connection.getOutputStream(); + + byte[] bytes = new byte[1024]; + int length; + while((length = fileInputStream.read(bytes)) >= 0) { + outputStream.write(bytes, 0, length); + } + + outputStream.flush(); + outputStream.close(); + fileInputStream.close(); + connection.disconnect(); + + LOG.info("Status: " + connection.getResponseCode()); + LOG.info("Message: " + connection.getResponseMessage()); + return connection.getResponseCode(); + } + + private HttpURLConnection getNexusConnection(String archive) throws IOException { + URL url = new URL(NEXUS_URL + archive); + String auth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(CREDENTIALS.getBytes()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.addRequestProperty("Authorization", auth); + connection.setRequestProperty("Connection", "keep-alive"); + connection.setRequestProperty("Proxy-Connection", "keep-alive"); + return connection; + } + + private int getArchive(String archiveIdentifier) throws IOException { + File archive = new File(DAEXIM_DIR + "backup.zip"); + if(archive.exists()) { + LOG.info("Recently retrieved archive found. Removing old archive..."); + archive.delete(); + LOG.info("Archive removed."); + } + HttpURLConnection connection = getNexusConnection( archiveIdentifier + "-" + BACKUP_ARCHIVE); + connection.setRequestMethod("GET"); + connection.setDoInput(true); + + InputStream connectionInputStream = connection.getInputStream(); + FileOutputStream fileOutputStream = new FileOutputStream(archive); + + byte[] bytes = new byte[1024]; + int length; + while((length = connectionInputStream.read(bytes)) >= 0) { // while connection has bytes + fileOutputStream.write(bytes, 0, length); // write to archive + } + connection.disconnect(); + + LOG.info("Status: " + connection.getResponseCode()); + LOG.info("Message: " + connection.getResponseMessage()); + LOG.info(archive.getName() + " successfully created."); + return connection.getResponseCode(); + } + + private ListenableFuture> buildBackupDataFuture(String statusCode, String message) { + BackupDataOutputBuilder outputBuilder = new BackupDataOutputBuilder(); + outputBuilder.setStatus(statusCode); + outputBuilder.setMessage(message); + RpcResult rpcResult = RpcResultBuilder. status(true).withResult(outputBuilder.build()).build(); + return Futures.immediateFuture(rpcResult); + } + + private ListenableFuture> retrieveDataOutputRpcResult(String status, String message) { + RetrieveDataOutputBuilder outputBuilder = new RetrieveDataOutputBuilder(); + outputBuilder.setStatus(status); + outputBuilder.setMessage(message); + RpcResult rpcResult = RpcResultBuilder. status(true).withResult(outputBuilder.build()).build(); + return Futures.immediateFuture(rpcResult); + } +} diff --git a/northbound/daexim-offsite-backup/provider/src/main/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupUtil.java b/northbound/daexim-offsite-backup/provider/src/main/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupUtil.java new file mode 100755 index 000000000..7adb2fa1b --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/src/main/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupUtil.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.northbound.daeximoffsitebackup; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.BackupDataOutputBuilder; + +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataInputBuilder; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataOutputBuilder; + +import org.onap.ccsdk.sli.core.sli.provider.MdsalHelper; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DaeximOffsiteBackupUtil extends MdsalHelper { + private static final Logger LOG = LoggerFactory.getLogger(DaeximOffsiteBackupUtil.class); + private static String PROPERTIES_FILE; + + public static void loadProperties() { + File file = new File(PROPERTIES_FILE); + Properties properties = new Properties(); + InputStream input = null; + if(file.isFile() && file.canRead()) { + try { + input = new FileInputStream(file); + properties.load(input); + LOG.info("Loaded properties from " + PROPERTIES_FILE); + setProperties(properties); + } catch (Exception e) { + LOG.error("Failed to load properties " + PROPERTIES_FILE + "\n", e); + } finally { + if(input != null) { + try { + input.close(); + } catch (IOException e) { + LOG.error("Failed to close properties file " + PROPERTIES_FILE + "\n", e); + } + } + } + } + } + + static { + // Trick class loader into loading builders. Some of + // these will be needed later by Reflection classes, but need + // to explicitly "new" them here to get class loader to load them. + + BackupDataOutputBuilder b1 = new BackupDataOutputBuilder(); + + RetrieveDataOutputBuilder b2 = new RetrieveDataOutputBuilder(); + RetrieveDataInputBuilder b3 = new RetrieveDataInputBuilder(); + } +} diff --git a/northbound/daexim-offsite-backup/provider/src/main/resources/OSGI-INF/blueprint/DaeximOffsiteBackup.xml b/northbound/daexim-offsite-backup/provider/src/main/resources/OSGI-INF/blueprint/DaeximOffsiteBackup.xml new file mode 100755 index 000000000..8e2101ad3 --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/src/main/resources/OSGI-INF/blueprint/DaeximOffsiteBackup.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + diff --git a/northbound/daexim-offsite-backup/provider/src/main/resources/daexim-offsite-backup.properties b/northbound/daexim-offsite-backup/provider/src/main/resources/daexim-offsite-backup.properties new file mode 100755 index 000000000..51efc1f1e --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/src/main/resources/daexim-offsite-backup.properties @@ -0,0 +1,28 @@ +# ============LICENSE_START======================================================= +# openECOMP : SDN-C +# ================================================================================ +# Copyright (C) 2019 AT&T Intellectual Property. All rights +# reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= + +# Example properties file + +daeximDirectory=/opt/opendaylight/current/daexim/ +credentials=admin:enc:YWRtaW4xMjM= +nexusUrl=http://localhost:8081/nexus/content/repositories/ +podName=UNKNOWN_ODL +file.operational=odl_backup_operational.json +file.models=odl_backup_models.json +file.config=odl_backup_config.json \ No newline at end of file diff --git a/northbound/daexim-offsite-backup/provider/src/main/resources/org/opendaylight/blueprint/DaeximOffsiteBackup.xml b/northbound/daexim-offsite-backup/provider/src/main/resources/org/opendaylight/blueprint/DaeximOffsiteBackup.xml new file mode 100755 index 000000000..8e2101ad3 --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/src/main/resources/org/opendaylight/blueprint/DaeximOffsiteBackup.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + diff --git a/northbound/daexim-offsite-backup/provider/src/test/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupProviderTest.java b/northbound/daexim-offsite-backup/provider/src/test/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupProviderTest.java new file mode 100644 index 000000000..d070c30e2 --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/src/test/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupProviderTest.java @@ -0,0 +1,216 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.northbound.daeximoffsitebackup; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import com.google.common.util.concurrent.CheckedFuture; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import java.util.concurrent.ExecutionException; +import org.eclipse.jdt.annotation.Nullable; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.DaeximOffsiteBackupService; +import org.opendaylight.yang.gen.v1.org.onap.ccsdk.sli.northbound.daeximoffsitebackup.rev180926.RetrieveDataInput; +import org.opendaylight.yangtools.yang.binding.Augmentation; + +public class DaeximOffsiteBackupProviderTest { + public DataBroker dataBroker; + public ReadWriteTransaction writeTransaction; + public CheckedFuture checkedFuture; + public RpcProviderRegistry rpcRegistry; + public DaeximOffsiteBackupProvider provider; + public Properties resProps; + + @Before + public void setup() { + resProps = new Properties(); + resProps.put("error-code", "200"); + resProps.put("error-message", "Success"); + dataBroker = mock(DataBroker.class); + writeTransaction = mock(ReadWriteTransaction.class); + checkedFuture = mock(CheckedFuture.class); + rpcRegistry = mock(RpcProviderRegistry.class); + when(rpcRegistry.addRoutedRpcImplementation(any(), any(DaeximOffsiteBackupService.class))).thenReturn(null); + try { + when(checkedFuture.get()).thenReturn(null); + } + catch(InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + when(writeTransaction.submit()).thenReturn(checkedFuture); + when(dataBroker.newReadWriteTransaction()).thenReturn(writeTransaction); + + provider = new DaeximOffsiteBackupProvider(dataBroker, rpcRegistry); + } + + @Test + public void initializeTest() { + provider.initialize(); + } + + @Test + public void closeTest() { + try { + provider.close(); + } + catch(Exception e) { + e.printStackTrace(); + } + } + + @Test + public void onDataTreeChangedTest() { + provider.onDataTreeChanged(null); + // onDataTreeChanged is an empty stub + } + + @Test + public void backupDataTest() { + try { + assertNotNull(provider.backupData(null)); + } + catch(Exception e) { + fail(); + } + try { + assertNotNull(provider.backupData(null)); + } + catch(Exception e) { + fail(); + } + try { + assertNotNull(provider.backupData(null)); + } + catch(Exception e) { + fail(); + } + try { + assertNotNull(provider.backupData(null)); + } + catch(Exception e) { + fail(); + } + try { + assertNotNull(provider.backupData(null)); + } + catch(Exception e) { + fail(); + } + } + + @Test + public void retrieveDataTest() { + RetrieveDataInput input = new RetrieveDataInput() { + @Override + public > @Nullable E augmentation(Class augmentationType) { + return null; + } + + @Override + public String getPodName() { + return "Some Pod"; + } + + @Override + public String getTimestamp() { + return "Some Timestamp"; + } + + + }; + try { + assertNotNull(provider.retrieveData(input)); + } + catch(Exception e) { + fail(); + } + try { + assertNotNull(provider.retrieveData(input)); + } + catch(Exception e) { + fail(); + } + try { + assertNotNull(provider.retrieveData(input)); + } + catch(Exception e) { + fail(); + } + try { + assertNotNull(provider.retrieveData(input)); + } + catch(Exception e) { + fail(); + } + try { + assertNotNull(provider.retrieveData(input)); + } + catch(Exception e) { + fail(); + } + } + + @Test + public void archiveOperationsTest() { + List files = Arrays.asList("src/test/resources/fileToZip1", "src/test/resources/fileToZip2"); + String archive = "src/test/resources/zippedArchive.zip"; + try { + Method method = provider.getClass().getDeclaredMethod("createArchive", List.class, String.class); + method.setAccessible(true); + method.invoke(provider, files, archive); + + } + catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + fail(); + } + + try { + Field field = provider.getClass().getDeclaredField("DAEXIM_DIR"); + field.setAccessible(true); + field.set(provider, ""); + Method method = provider.getClass().getDeclaredMethod("extractArchive", String.class); + method.setAccessible(true); + method.invoke(provider, archive); + } + catch(NoSuchMethodException | IllegalAccessException | InvocationTargetException | NoSuchFieldException e) { + fail(); + } + finally { + File zip = new File(archive); + zip.delete(); + } + } +} diff --git a/northbound/daexim-offsite-backup/provider/src/test/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupUtilTest.java b/northbound/daexim-offsite-backup/provider/src/test/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupUtilTest.java new file mode 100644 index 000000000..16ce68198 --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/src/test/java/org/onap/ccsdk/sli/northbound/daeximoffsitebackup/DaeximOffsiteBackupUtilTest.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.ccsdk.sli.northbound.daeximoffsitebackup; + +import org.junit.Test; + +import java.lang.reflect.Field; + +public class DaeximOffsiteBackupUtilTest { + @Test + public void loadProperties() { + try { + Field field = DaeximOffsiteBackupUtil.class.getDeclaredField("PROPERTIES_FILE"); + field.setAccessible(true); + field.set(new DaeximOffsiteBackupUtil(), "src/test/resources/daexim-offsite-backup.properties"); + DaeximOffsiteBackupUtil.loadProperties(); + } catch(Exception e) { + // Files don't exist on build server + } + } +} diff --git a/northbound/daexim-offsite-backup/provider/src/test/resources/daexim-offsite-backup.properties b/northbound/daexim-offsite-backup/provider/src/test/resources/daexim-offsite-backup.properties new file mode 100755 index 000000000..b69027906 --- /dev/null +++ b/northbound/daexim-offsite-backup/provider/src/test/resources/daexim-offsite-backup.properties @@ -0,0 +1,26 @@ +# ============LICENSE_START======================================================= +# openECOMP : SDN-C +# ================================================================================ +# Copyright (C) 2019 AT&T Intellectual Property. All rights +# reserved. +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============LICENSE_END========================================================= + +daeximDirectory=/opt/opendaylight/current/daexim/ +credentials=admin:admin123 +nexusUrl=http://localhost:8081/nexus/content/repositories/ +podName=UNKNOWN_ODL +file.operational=odl_backup_operational.json +file.models=odl_backup_models.json +file.config=odl_backup_config.json \ No newline at end of file -- cgit 1.2.3-korg