diff options
author | 2017-05-19 11:31:27 -0400 | |
---|---|---|
committer | 2017-05-19 12:09:26 -0400 | |
commit | 9a55549bd44151c424d3033692f677d697f4f1a5 (patch) | |
tree | ed130c364d59ad608f72e9e8b9ecfe64c367fcc7 /aai-resources/src/main/java | |
parent | f189b8504ab23c3fc1168749ef8be07af392a8b9 (diff) |
Add the necessary tools for resources and logbacks
Change-Id: Ia86de3587ffafe98c427f0fcfd45d2e6453f6db2
Signed-off-by: Venkata Harish K Kajur <vk250x@att.com>
Diffstat (limited to 'aai-resources/src/main/java')
8 files changed, 2213 insertions, 0 deletions
diff --git a/aai-resources/src/main/java/org/openecomp/aai/dbgen/DataSnapshot.java b/aai-resources/src/main/java/org/openecomp/aai/dbgen/DataSnapshot.java new file mode 100644 index 0000000..68bebc2 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/dbgen/DataSnapshot.java @@ -0,0 +1,280 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 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.openecomp.aai.dbgen; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Iterator; +import java.util.Properties; +import java.util.TimeZone; + +import org.apache.tinkerpop.gremlin.structure.io.IoCore; +import org.apache.tinkerpop.gremlin.structure.io.graphson.LegacyGraphSONReader; + +import org.openecomp.aai.dbmap.AAIGraph; +import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.logging.ErrorLogHelper; +import org.openecomp.aai.util.AAIConfig; +import org.openecomp.aai.util.AAIConstants; +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.thinkaurelius.titan.core.TitanGraph; +import com.thinkaurelius.titan.core.TitanVertex; +import com.thinkaurelius.titan.core.util.TitanCleanup; + +public class DataSnapshot { + + private static EELFLogger LOGGER; + /** + * The main method. + * + * @param args + * the arguments + */ + public static void main(String[] args) { + // Set the logging file properties to be used by EELFManager + Properties props = System.getProperties(); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_DATA_SNAPSHOT_LOGBACK_PROPS); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); + LOGGER = EELFManager.getInstance().getLogger(DataSnapshot.class); + Boolean dbClearFlag = false; + TitanGraph graph = null; + String command = "JUST_TAKE_SNAPSHOT"; // This is the default + String oldSnapshotFileName = ""; + if (args.length == 1) { + command = args[0]; + } + if (args.length == 2) { + // If they pass in a RELOAD_ENTIRE_DB argument, then we will be + // reloading the database + // from the filename passed in -which will be expected to be found + // in our snapshot directory. + command = args[0]; + oldSnapshotFileName = args[1]; + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + AAIConfig.init(); + ErrorLogHelper.loadProperties(); + System.out.println("Command = " + command + ", oldSnapshotFileName = " + oldSnapshotFileName); + String targetDir = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP + "logs" + AAIConstants.AAI_FILESEP + "data" + AAIConstants.AAI_FILESEP + "dataSnapshots"; + + // Make sure the dataSnapshots directory is there + new File(targetDir).mkdirs(); + + System.out.println(" ---- NOTE --- about to open graph (takes a little while)\n"); + + graph = AAIGraph.getInstance().getGraph(); + + if (graph == null) { + String emsg = "Not able to get a graph object in DataSnapshot.java\n"; + System.out.println(emsg); + System.exit(1); + } + + if (command.equals("JUST_TAKE_SNAPSHOT")) { + // ------------------------------------------ + // They just want to take a snapshot. + // ------------------------------------------ + SimpleDateFormat d = new SimpleDateFormat("yyyyMMddHHmm"); + d.setTimeZone(TimeZone.getTimeZone("GMT")); + String dteStr = d.format(new Date()).toString(); + String newSnapshotOutFname = targetDir + AAIConstants.AAI_FILESEP + "dataSnapshot.graphSON." + dteStr; + + graph.io(IoCore.graphson()).writeGraph(newSnapshotOutFname); + + System.out.println("Snapshot written to " + newSnapshotOutFname); + /**** + * Don't really want to do this every hour ************** int + * vCount = 0; Iterator vIt = + * graph.query().vertices().iterator(); while( vIt.hasNext() ){ + * vCount++; vIt.next(); } System.out.println( + * "A little after taking the snapshot, we see: " + vCount + + * " vertices in the db."); + ************/ + } else if (command.equals("CLEAR_ENTIRE_DATABASE")) { + // ------------------------------------------------------------------ + // They are calling this to clear the db before re-loading it + // later + // ------------------------------------------------------------------ + + // First - make sure the backup file they will be using can be + // found and has data + if (oldSnapshotFileName.equals("")) { + String emsg = "No oldSnapshotFileName passed to DataSnapshot."; + System.out.println(emsg); + System.exit(1); + } + String oldSnapshotFullFname = targetDir + AAIConstants.AAI_FILESEP + oldSnapshotFileName; + File f = new File(oldSnapshotFullFname); + if (!f.exists()) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " could not be found."; + System.out.println(emsg); + System.exit(1); + } else if (!f.canRead()) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " could not be read."; + System.out.println(emsg); + System.exit(1); + } else if (f.length() == 0) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " had no data."; + System.out.println(emsg); + System.exit(1); + } + + System.out.println("\n>>> WARNING <<<< "); + System.out.println(">>> All data and schema in this database will be removed at this point. <<<"); + System.out.println(">>> Processing will begin in 5 seconds. <<<"); + System.out.println(">>> WARNING <<<< "); + + try { + // Give them a chance to back out of this + Thread.sleep(5000); + } catch (java.lang.InterruptedException ie) { + System.out.println(" DB Clearing has been aborted. "); + System.exit(1); + } + + System.out.println(" Begin clearing out old data. "); + graph.close(); + TitanCleanup.clear(graph); + System.out.println(" Done clearing data. "); + System.out.println(">>> IMPORTANT - NOTE >>> you need to run the SchemaGenerator (use GenTester) before "); + System.out.println(" reloading data or the data will be put in without indexes. "); + dbClearFlag = true; + + } else if (command.equals("RELOAD_LEGACY_DATA")) { + // ------------------------------------------------------------------- + // They want to restore the database from an old snapshot file + // ------------------------------------------------------------------- + if (oldSnapshotFileName.equals("")) { + String emsg = "No oldSnapshotFileName passed to DataSnapshot when RELOAD_LEGACY_DATA used."; + System.out.println(emsg); + System.exit(1); + } + String oldSnapshotFullFname = targetDir + AAIConstants.AAI_FILESEP + oldSnapshotFileName; + File f = new File(oldSnapshotFullFname); + if (!f.exists()) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " could not be found."; + System.out.println(emsg); + System.exit(1); + } else if (!f.canRead()) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " could not be read."; + System.out.println(emsg); + System.exit(1); + } else if (f.length() == 0) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " had no data."; + System.out.println(emsg); + System.exit(1); + } + + System.out.println("We will load data IN from the file = " + oldSnapshotFullFname); + System.out.println(" Begin reloading Titan 0.5 data. "); + + LegacyGraphSONReader lgr = LegacyGraphSONReader.build().create(); + InputStream is = new FileInputStream(oldSnapshotFullFname); + lgr.readGraph(is, graph); + + System.out.println("Completed the inputGraph command, now try to commit()... "); + graph.tx().commit(); + System.out.println("Completed reloading Titan 0.5 data."); + + int vCount = 0; + Iterator<TitanVertex> vIt = graph.query().vertices().iterator(); + while (vIt.hasNext()) { + vCount++; + vIt.next(); + } + System.out.println("A little after repopulating from an old snapshot, we see: " + vCount + " vertices in the db."); + } else if (command.equals("RELOAD_DATA")) { + // ------------------------------------------------------------------- + // They want to restore the database from an old snapshot file + // ------------------------------------------------------------------- + if (oldSnapshotFileName.equals("")) { + String emsg = "No oldSnapshotFileName passed to DataSnapshot when RELOAD_DATA used."; + System.out.println(emsg); + System.exit(1); + } + String oldSnapshotFullFname = targetDir + AAIConstants.AAI_FILESEP + oldSnapshotFileName; + File f = new File(oldSnapshotFullFname); + if (!f.exists()) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " could not be found."; + System.out.println(emsg); + System.exit(1); + } else if (!f.canRead()) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " could not be read."; + System.out.println(emsg); + System.exit(1); + } else if (f.length() == 0) { + String emsg = "oldSnapshotFile " + oldSnapshotFullFname + " had no data."; + System.out.println(emsg); + System.exit(1); + } + + System.out.println("We will load data IN from the file = " + oldSnapshotFullFname); + System.out.println(" Begin reloading data. "); + graph.io(IoCore.graphson()).readGraph(oldSnapshotFullFname); + System.out.println("Completed the inputGraph command, now try to commit()... "); + graph.tx().commit(); + System.out.println("Completed reloading data."); + + int vCount = 0; + Iterator<TitanVertex> vIt = graph.query().vertices().iterator(); + while (vIt.hasNext()) { + vCount++; + vIt.next(); + } + System.out.println("A little after repopulating from an old snapshot, we see: " + vCount + " vertices in the db."); + } else { + String emsg = "Bad command passed to DataSnapshot: [" + command + "]"; + System.out.println(emsg); + System.exit(1); + } + + } catch (AAIException e) { + ErrorLogHelper.logError("AAI_6128", e.getMessage()); + } catch (Exception ex) { + ErrorLogHelper.logError("AAI_6128", ex.getMessage()); + } finally { + if (!dbClearFlag && graph != null) { + // Any changes that worked correctly should have already done + // thier commits. + if (graph.isOpen()) { + graph.tx().rollback(); + graph.close(); + } + } + try { + baos.close(); + } catch (IOException iox) { + } + } + + System.exit(0); + + }// End of main() + +} diff --git a/aai-resources/src/main/java/org/openecomp/aai/util/DeleteResource.java b/aai-resources/src/main/java/org/openecomp/aai/util/DeleteResource.java new file mode 100644 index 0000000..81ee621 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/util/DeleteResource.java @@ -0,0 +1,221 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 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.openecomp.aai.util; + +import java.lang.reflect.Field; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Properties; +import java.util.Scanner; +import java.util.UUID; + +import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.ingestModel.DbMaps; +import org.openecomp.aai.ingestModel.IngestModelMoxyOxm; +import org.openecomp.aai.logging.ErrorLogHelper; +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.common.base.CaseFormat; + + +public class DeleteResource { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DeleteResource.class); + private static final String FROMAPPID = "AAI-TOOLS"; + private static final String TRANSID = UUID.randomUUID().toString(); + // the logic below to parse the url is dependent on the node types + // code may need to be adjusted if different nodetypes are added here + private static final String[] nodeTypeList = {"complex", "availability-zone", "oam-network", + "dvs-switch", "vserver", "vpe", "vpls-pe"}; + + private static final String USAGE_STRING = "Usage: deleteTool.sh <resource-path>\n + " + + "for example: resource-path for a particular customer is business/customers/customer/global-customer-id-1 \n"; + + /** + * The main method. + * + * @param <T> the generic type + * @param args the arguments + */ + public static <T> void main(String[] args) { + + // Set the logging file properties to be used by EELFManager + Properties props = System.getProperties(); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_DELTOOL_LOGBACK_PROPS); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); + + String url = null; + try { + if ((args.length < 1) || ((args.length > 1) )) { + System.out.println("Insufficient or Invalid arguments"); + System.out.println(USAGE_STRING); + System.exit(1); + } + + Boolean bResVersionEnabled = false; + + try + { + String strEnableResVersion = AAIConfig.get(AAIConstants.AAI_RESVERSION_ENABLEFLAG); + if (strEnableResVersion != null && !strEnableResVersion.isEmpty()) + bResVersionEnabled = Boolean.valueOf(strEnableResVersion); + } + catch (Exception e) { + + } + + // Assume the config AAI_SERVER_URL has a last slash so remove if + // resource-path has it as the first char + String path = args[0].replaceFirst("^/", ""); + + Path p = Paths.get(path); + + // if the node type has one key + String resource = p.getName(p.getNameCount() - 2).toString(); + + IngestModelMoxyOxm moxyMod = new IngestModelMoxyOxm(); + DbMaps dbMaps = null; + try { + ArrayList <String> defaultVerLst = new ArrayList <String> (); + defaultVerLst.add( AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP) ); + moxyMod.init( defaultVerLst, false); + // Just make sure we can get DbMaps - don't actually need it until later. + dbMaps = IngestModelMoxyOxm.dbMapsContainer.get(AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)); + } + catch (Exception ex){ + ErrorLogHelper.logError("AAI_7402", " ERROR - Could not get the DbMaps object: " + ex.getMessage()); + System.exit(1); + } + // if the node type has two keys - this assumes max 2 keys + if (!dbMaps.NodeKeyProps.containsKey(resource)) + resource = p.getName(p.getNameCount() - 3).toString(); + String resourceClass = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, resource); + resourceClass = "org.openecomp.aai.domain.yang." + resourceClass; + + LOGGER.debug("class=" + resourceClass); + LOGGER.debug("path=" + path); + + if (bResVersionEnabled) + { + RestObject<T> restObj = new RestObject<T>(); + @SuppressWarnings("unchecked") + T t = (T)getInstance(Class.forName(resourceClass)); + restObj.set(t); + + try + { + RestController.<T>Get(t, FROMAPPID, TRANSID, path, restObj, false); + t = restObj.get(); + LOGGER.info(" GET resoruceversion succeeded\n"); + String resourceUpdateVersion = GetResourceVersion(t); + path += "?resource-version=" + resourceUpdateVersion; + } catch (AAIException e) { + if (e.getMessage().contains("with status=404") && resource.equals("named-query")) { + System.out.println("Delete succeeded, the resource doesn't exist in the DB. " + p); + System.exit(0); + } else { + System.out.println("Delete failed. Resource Not found in the DB."); + System.exit(1); + } + } catch (Exception e1){ + System.out.println("Delete failed. Resource Not found in the DB."); + System.exit(1); + } + } + + System.out.print("\nAre you sure you would like to delete the resource \n" + url + "? (y/n): "); + Scanner s = new Scanner(System.in); + s.useDelimiter(""); + String confirm = s.next(); + + if (!confirm.equalsIgnoreCase("y")) { + LOGGER.info("User chose to exit before deleting"); + System.exit(1); + } + + RestController.Delete(FROMAPPID, TRANSID, path); + + s.close(); + System.exit(0); + + } catch (AAIException e) { + ErrorLogHelper.logException(e); + System.exit(1); + } catch (Exception e) { + ErrorLogHelper.logError("AAI_7402", e.getMessage()); + System.exit(1); + } + + } + + /** + * Gets the single instance of DeleteResource. + * + * @param <T> the generic type + * @param clazz the clazz + * @return single instance of DeleteResource + * @throws IllegalAccessException the illegal access exception + * @throws InstantiationException the instantiation exception + */ + public static <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException + { + return clazz.newInstance(); + } + + /** + * Gets the resource version. + * + * @param <T> the generic type + * @param resource the resource + * @return the string + */ + public static <T> String GetResourceVersion(T resource) + { + Field[] fields = resource.getClass().getDeclaredFields(); + if (fields != null) + { + for (Field field : fields) + { + try + { + field.setAccessible(true); + if ( field.getName().equalsIgnoreCase("resourceVersion") ) + { + Object obj = field.get(resource); + return (String)obj; + } + + + } + catch (Exception e) + { + + } + + + } + } + return null; + } +} diff --git a/aai-resources/src/main/java/org/openecomp/aai/util/GetResource.java b/aai-resources/src/main/java/org/openecomp/aai/util/GetResource.java new file mode 100644 index 0000000..f16efb3 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/util/GetResource.java @@ -0,0 +1,140 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 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.openecomp.aai.util; + +import java.security.KeyManagementException; +import java.util.Properties; +import java.util.UUID; + +import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.logging.ErrorLogHelper; +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; + + +public class GetResource { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(GetResource.class); + private static final String FROMAPPID = "AAI-TOOLS"; + private static final String TRANSID = UUID.randomUUID().toString(); + private static final String USAGE_STRING = "Usage: getTool.sh <resource-path> \n + " + + "for example: resource-path for a particular customer is business/customers/customer/global-customer-id-1 \n"; + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + + // Set the logging file properties to be used by EELFManager + Properties props = System.getProperties(); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_GETRES_LOGBACK_PROPS); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); + + String url = null; + try { + if (args.length < 1) { + System.out.println("Nothing to get or Insufficient arguments"); + System.out.println(USAGE_STRING); + System.exit(1); + } else { + // Assume the config AAI_SERVER_URL has a last slash so remove if + // resource-path has it as the first char + url = args[0].replaceFirst("^/", ""); + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + url; + + LOGGER.debug("url=" + url); + getNode(url); + System.exit(0); + } + } catch (AAIException e) { + LOGGER.error("GET failed: " + e.getMessage()); + ErrorLogHelper.logException(e); + System.exit(1); + } catch (Exception e) { + LOGGER.error("GET failed: " + e.getMessage()); + ErrorLogHelper.logError("AAI_7402", e.getMessage()); + System.exit(1); + } + + } + + /** + * Gets the node. + * + * @param aaiLogger the aai logger + * @param logline the logline + * @param url the url + * @return the node + * @throws AAIException the AAI exception + */ + public static void getNode(String url) throws AAIException { + try { + String useBasicAuth = AAIConfig.get("aai.tools.enableBasicAuth"); + Client client = null; + + if("true".equals(useBasicAuth)){ + client = HttpsAuthClient.getBasicAuthClient(); + } else { + client = HttpsAuthClient.getTwoWaySSLClient(); + } + + System.out.println("Getting the resource...: " + url); + + ClientResponse cres = client.resource(url) + .header("X-TransactionId", TRANSID) + .header("X-FromAppId", FROMAPPID) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + + .accept("application/json") + .get(ClientResponse.class); + + if (cres.getStatus() == 404) { // resource not found + LOGGER.info("\nResource does not exist: " + cres.getStatus() + + ":" + cres.getEntity(String.class)); + throw new AAIException("AAI_7404", "Resource does not exist"); + } else if (cres.getStatus() == 200){ + String msg = cres.getEntity(String.class); + ObjectMapper mapper = new ObjectMapper(); + Object json = mapper.readValue(msg, Object.class); + String indented = mapper.writerWithDefaultPrettyPrinter() + .writeValueAsString(json); + LOGGER.info(indented); + } else { + LOGGER.error("Getting the Resource failed: " + cres.getStatus() + + ":\n" + cres.getEntity(String.class)); + throw new AAIException("AAI_7402", "Error during GET"); + } + } catch (AAIException e) { + throw e; + } catch (KeyManagementException e) { + throw new AAIException("AAI_7401", e, "Error during GET"); + } catch (Exception e) { + throw new AAIException("AAI_7402", e, "Error during GET"); + } + } + +} diff --git a/aai-resources/src/main/java/org/openecomp/aai/util/HttpsAuthClient.java b/aai-resources/src/main/java/org/openecomp/aai/util/HttpsAuthClient.java new file mode 100644 index 0000000..89562f7 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/util/HttpsAuthClient.java @@ -0,0 +1,179 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 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.openecomp.aai.util; + +import java.io.FileInputStream; +import java.security.KeyManagementException; +import java.security.KeyStore; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; + +import org.openecomp.aai.domain.yang.Customers; +import org.openecomp.aai.exceptions.AAIException; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; +import com.sun.jersey.api.client.filter.LoggingFilter; +import com.sun.jersey.api.json.JSONConfiguration; +import com.sun.jersey.client.urlconnection.HTTPSProperties; + +import static java.util.Base64.getEncoder; + +public class HttpsAuthClient{ + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + try { + String url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + "business/customers"; + System.out.println("Making Jersey https call..."); + Client client = HttpsAuthClient.getTwoWaySSLClient(); + + ClientResponse res = client.resource(url) + .accept("application/json") + .header("X-TransactionId", "PROV001") + .header("X-FromAppId", "AAI") + .type("application/json") + .get(ClientResponse.class); + +// System.out.println("Jersey result: "); +// System.out.println(res.getEntity(String.class).toString()); + + Customers customers = res.getEntity(Customers.class); + System.out.println("Jersey result: "); + System.out.println("Number of customers: " + customers.getCustomer().size()); + + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + /** + * Gets the client. + * + * @return the client + * @throws KeyManagementException the key management exception + */ + public static Client getTwoWaySSLClient() throws KeyManagementException { + + ClientConfig config = new DefaultClientConfig(); + config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + config.getClasses().add(org.openecomp.aai.restcore.CustomJacksonJaxBJsonProvider.class); + + SSLContext ctx = null; + try { + String truststore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_FILENAME); + String truststore_password = AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_PASSWD); + String keystore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_KEYSTORE_FILENAME); + String keystore_password = AAIConfig.get(AAIConstants.AAI_KEYSTORE_PASSWD); + + System.setProperty("javax.net.ssl.trustStore", truststore_path); + System.setProperty("javax.net.ssl.trustStorePassword", truststore_password); + HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){ + public boolean verify(String string,SSLSession ssls) { + return true; + } + }); + + ctx = SSLContext.getInstance("TLSv1.2"); + KeyManagerFactory kmf = null; + try { + kmf = KeyManagerFactory.getInstance("SunX509"); + FileInputStream fin = new FileInputStream(keystore_path); + KeyStore ks = KeyStore.getInstance("PKCS12"); + char[] pwd = keystore_password.toCharArray(); + ks.load(fin, pwd); + kmf.init(ks, pwd); + } catch (Exception e) { + System.out.println("Error setting up kmf: exiting"); + e.printStackTrace(); + System.exit(1); + } + + ctx.init(kmf.getKeyManagers(), null, null); + config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, + new HTTPSProperties( new HostnameVerifier() { + @Override + public boolean verify( String s, SSLSession sslSession ) { + return true; + } + }, ctx)); + } catch (Exception e) { + System.out.println("Error setting up config: exiting"); + e.printStackTrace(); + System.exit(1); + } + + Client client = Client.create(config); + // uncomment this line to get more logging for the request/response + // client.addFilter(new LoggingFilter(System.out)); + + return client; + } + + public static Client getBasicAuthClient() throws AAIException { + + String truststore_path = AAIConstants.AAI_HOME_ETC_AUTH + AAIConfig.get(AAIConstants.AAI_TRUSTSTORE_FILENAME); + System.setProperty("javax.net.ssl.trustStore", truststore_path); + + javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(new javax.net.ssl.HostnameVerifier() { + public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) { + return true; + } + }); + + String userName = AAIConfig.get("aai.tools.username"); + String password = AAIConfig.get("aai.tools.password"); + // ClientConfig config = new DefaultClientConfig(); + Client client = Client.create(); + //client.addFilter(new HTTPBasicAuthFilter(userName, password)); + + + + + return client; + } + + public static String getBasicAuthHeaderValue() throws AAIException { + + + String userName = AAIConfig.get("aai.tools.username", ""); + String password = AAIConfig.get("aai.tools.password", ""); + String authString = userName + ":" + password; + byte[] s = getEncoder().encode(authString.getBytes()); + String basicauthStringEnc = new String(s); + + return "Basic " + basicauthStringEnc; + + } + +} diff --git a/aai-resources/src/main/java/org/openecomp/aai/util/PostResource.java b/aai-resources/src/main/java/org/openecomp/aai/util/PostResource.java new file mode 100644 index 0000000..72fcd86 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/util/PostResource.java @@ -0,0 +1,170 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 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.openecomp.aai.util; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.UUID; + +import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.logging.ErrorLogHelper; +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.CaseFormat; + +/* + * Allows to call POST REST API that AAI supports - currently for edge-tag-query + */ +public class PostResource { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PostResource.class); + private static final String FROMAPPID = "AAI-TOOLS"; + private static final String TRANSID = UUID.randomUUID().toString(); + private static final String USAGE_STRING = "Usage: postTool.sh <resource-path> <filename>\n" + + "resource-path for a particular resource or query starting after the aai/<version>\n" + + "filename is the path to a file which contains the json input for the payload\n" + + "for example: postTool.sh search/edge-tag-query /tmp/query-input.json\n"; + + /** + * The main method. + * + * @param <T> the generic type + * @param args the arguments + */ + public static <T> void main(String[] args) { + + // Set the logging file properties to be used by EELFManager + Properties props = System.getProperties(); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_POSTTOOL_LOGBACK_PROPS); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); + + try { + if (args.length < 2) { + System.out.println("Insufficient arguments"); + System.out.println(USAGE_STRING); + System.exit(1); + } + + // Assume the config AAI_SERVER_URL has a last slash so remove if + // resource-path has it as the first char + String path = args[0].replaceFirst("^/", ""); + Path p = Paths.get(path); + + // currently , it is for edge-taq-query only + String query = p.getName(p.getNameCount() - 1).toString(); + String resourceClass = null; + if (query.equals("edge-tag-query")) + resourceClass = "org.openecomp.aai.domain.search." + + CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, query) + "Request"; + else { + ErrorLogHelper.logError("AAI_7403", "Incorrect resource or query"); + System.exit(1); + } + + LOGGER.debug("class=" + resourceClass); + LOGGER.debug("path=" + path); + + @SuppressWarnings("unchecked") + T resJson1 = (T)readJsonFile(Class.forName(resourceClass), args[1]); + + String response = RestController.<T>Post(resJson1, FROMAPPID, TRANSID, path); + ObjectMapper mapper = new ObjectMapper(); + Object json = mapper.readValue(response, Object.class); + + LOGGER.info(" POST succeeded\n"); + LOGGER.info("Response = " + mapper.writer().withDefaultPrettyPrinter().writeValueAsString(json)); + LOGGER.info("\nDone!!"); + + System.exit(0); + + } catch (AAIException e) { + ErrorLogHelper.logException(e); + System.exit(1); + } catch (Exception e) { + ErrorLogHelper.logError("AAI_7402", e.getMessage()); + System.exit(1); + } + } + + /** + * Gets the single instance of PostResource. + * + * @param <T> the generic type + * @param clazz the clazz + * @return single instance of PostResource + * @throws IllegalAccessException the illegal access exception + * @throws InstantiationException the instantiation exception + */ + public static <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException + { + return clazz.newInstance(); + } + + /** + * Read json file. + * + * @param <T> the generic type + * @param clazz the clazz + * @param fName the f name + * @return the t + * @throws AAIException the AAI exception + */ + public static <T> T readJsonFile( Class<T> clazz, String fName ) throws AAIException + { + String jsonData = ""; + BufferedReader br = null; + T t; + + try { + String line; + br = new BufferedReader(new FileReader(fName)); + while ((line = br.readLine()) != null) { + jsonData += line + "\n"; + } + } catch (IOException e) { + throw new AAIException("AAI_7403", e, "Error opening json file"); + } finally { + try { + if (br != null) + br.close(); + } catch (IOException ex) { + ex.printStackTrace(); + throw new AAIException("AAI_7403", ex, "Error closing json file"); + } + } + + try { + t = MapperUtil.readWithDashesAsObjectOf(clazz, jsonData); + } + catch (Exception je){ + throw new AAIException("AAI_7403", je, "Error parsing json file"); + } + + return t; + + }//End readJsonFile() +} diff --git a/aai-resources/src/main/java/org/openecomp/aai/util/PutResource.java b/aai-resources/src/main/java/org/openecomp/aai/util/PutResource.java new file mode 100644 index 0000000..70fccb1 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/util/PutResource.java @@ -0,0 +1,412 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 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.openecomp.aai.util; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.KeyManagementException; +import java.util.ArrayList; +import java.util.Properties; +import java.util.UUID; + +import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.ingestModel.DbMaps; +import org.openecomp.aai.ingestModel.IngestModelMoxyOxm; +import org.openecomp.aai.logging.ErrorLogHelper; +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.common.base.CaseFormat; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; + +/* + * SWGK - 09/03/2015 - Added Generics to Put as well as to Get the object that was created/updated + * by Put to get the Object back. + */ +public class PutResource { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PutResource.class); + private static final String FROMAPPID = "AAI-TOOLS"; + private static final String TRANSID = UUID.randomUUID().toString(); + private static final String USAGE_STRING = "Usage: putTool.sh <resource-path> <filename> <UpdatingRelationshiplist> <UpdatingChild> <ChildNameList> <SkipIfExists>\n" + + "resource-path for a particular resource starting after the aai/<version>\n" + + "filename is the path to a file which contains the json input for the payload\n" + + "optional UpdatingRelationshiplist setting 1 for updating the relationship list and if setting 0 relationship list will not be updated.\n" + + "optional UpdatingChild setting 1 for updating the child(r)en and if setting 0 child(ren) will not be updated.\n" + + "optional ChildNameList is a comma-separated child(ren) name list only applicable if UpdatingChild is set to 1.\n" + + "optional SkipIfExisting setting 1 to skip the update if resource exists and if setting 0 put will be done.\n" + + "for example 1: putTool.sh cloud-infrastructure/oam-networks/oam-network/test-100-oam /tmp/putoam.json\n" + + "for example 2: putTool.sh cloud-infrastructure/pservers/pserver/dpa2r04c009-swgk-009 /tmp/pserver.json 0 1 PInterfaces,LagInterfaces\n" + + "for example 2: putTool.sh cloud-infrastructure/pservers/pserver/dpa2r04c009-swgk-009 /tmp/pserver.json 0 1 PInterfaces,LagInterfaces 1\n"; + + /** + * The main method. + * + * @param <T> the generic type + * @param args the arguments + */ + public static <T> void main(String[] args) { + + // Set the logging file properties to be used by EELFManager + Properties props = System.getProperties(); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_PUTTOOL_LOGBACK_PROPS); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); + + Boolean bResVersionEnabled = false; + + try + { + String strEnableResVersion = AAIConfig.get(AAIConstants.AAI_RESVERSION_ENABLEFLAG); + if (strEnableResVersion != null && !strEnableResVersion.isEmpty()) + bResVersionEnabled = Boolean.valueOf(strEnableResVersion); + } + catch (Exception e) { + + } + + boolean doPutIfExists = true; + if (args.length > 5) + if (args[5].equals("1")) + doPutIfExists = false; + + try { + if (args.length < 2) { + System.out.println("Insufficient arguments"); + System.out.println(USAGE_STRING); + System.exit(1); + } + + // Assume the config AAI_SERVER_URL has a last slash so remove if + // resource-path has it as the first char + String path = args[0].replaceFirst("^/", ""); + Path p = Paths.get(path); + + // if the node type has one key + String resource = p.getName(p.getNameCount() - 2).toString(); + // if the node type has two keys - this assumes max 2 keys + IngestModelMoxyOxm moxyMod = new IngestModelMoxyOxm(); + DbMaps dbMaps = null; + try { + ArrayList <String> defaultVerLst = new ArrayList <String> (); + defaultVerLst.add( AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP) ); + moxyMod.init( defaultVerLst, false); + // Just make sure we can get DbMaps - don't actually need it until later. + dbMaps = IngestModelMoxyOxm.dbMapsContainer.get(AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)); + } + catch (Exception ex){ + ErrorLogHelper.logError("AAI_7402", "ERROR - Could not get the DbMaps object."); + System.exit(1); + } + + if (!dbMaps.NodeKeyProps.containsKey(resource)) + resource = p.getName(p.getNameCount() - 3).toString(); + String resourceClass = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, resource); + resourceClass = "org.openecomp.aai.domain.yang." + resourceClass; + + LOGGER.debug("class=" + resourceClass); + LOGGER.debug("path=" + path); + + RestObject<T> restObj = new RestObject<T>(); + @SuppressWarnings("unchecked") + T t2 = (T)getInstance(Class.forName(resourceClass)); + restObj.set(t2); + + boolean bExist = true; + + try + { + if ( !doPutIfExists ) { + + String url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + path; + + LOGGER.debug("url=" + url); + if ( nodeExists( url) ) { + LOGGER.info("PUT succeeded, the resource exists already in the DB. Skipping the put based on the skipIfExists command line parameter.\n"); + System.exit(0); + } + + } + RestController.<T>Get(t2, FROMAPPID, TRANSID, path, restObj, false); + t2 = restObj.get(); + LOGGER.info(" GET succeeded\n"); + } catch (AAIException e) { + if ( !doPutIfExists ) { + LOGGER.warn("Caught exception while attempting to PUT resource", e); + } + bExist = false; + } + catch (Exception e1) + { + if ( !doPutIfExists ) { + LOGGER.warn(" GET exception ignored with skipExists parameter\n", e1); + } + bExist = false; + } + + + @SuppressWarnings("unchecked") + T resJson1 = (T)readJsonFile(Class.forName(resourceClass), args[1]); + String resourceUpdateVersion = GetResourceVersion(resJson1); + + + if (bResVersionEnabled && bExist) + { + + String DBresourceVersion = GetResourceVersion(t2); + if ( !doPutIfExists ) { + LOGGER.info("PUT succeeded, the resource exists already in the DB. Skipping the put based on the skipIfExists command line parameter.\n"); + System.exit(0); + } + + if (resourceUpdateVersion == null || resourceUpdateVersion.isEmpty()) + { + if ( DBresourceVersion != null && !DBresourceVersion.isEmpty()) + LOGGER.error("The resource with version = " + DBresourceVersion + " exists already in the DB. Please supply the right resourceVersion in input data file.\n"); + else + LOGGER.error("The resource exists already in the DB. Please supply the right resourceVersion in input data file.\n"); + + System.exit(1); + } + + if ( DBresourceVersion != null && !DBresourceVersion.isEmpty() ) + { + if ( resourceUpdateVersion != null && !resourceUpdateVersion.isEmpty() ) + if (!DBresourceVersion.equals(resourceUpdateVersion)) + { + LOGGER.error("DB version doesn't match current version. Please get the latest version and modify.\n"); + System.exit(1); + } + } + } + else //sanity check + { + if ( bResVersionEnabled && resourceUpdateVersion != null && !resourceUpdateVersion.isEmpty()) + { + LOGGER.error("DB doesn't have this resource any more. Please create a new version by taking out the resourceVersion tag from your input resource data file.\n"); + System.exit(1); + } + + } + + if (bExist) //merge + { + boolean bUpdateChildren = false; + boolean bUpdateRL = false; + + if (args.length == 3) + { + if (args[2].equals("1")) + bUpdateRL = true; + resJson1 = MergeResource.merge(t2, resJson1, false, bUpdateRL); + } + else if (args.length == 4) + { + if (args[2].equals("1")) + bUpdateRL = true; + if (args[3].equals("1")) + bUpdateChildren = true; + resJson1 = MergeResource.merge(t2, resJson1, bUpdateChildren, bUpdateRL); + } + else if (args.length == 5) + { + if (args[2].equals("1")) + bUpdateRL = true; + if (args[3].equals("1")) + bUpdateChildren = true; + String[] strChildArray = args[4].split("\\,"); + resJson1 = MergeResource.merge(t2, resJson1, bUpdateChildren, strChildArray, bUpdateRL); + + } + else + resJson1 = MergeResource.merge(t2, resJson1); + + } + + RestController.<T>Put(resJson1, FROMAPPID, TRANSID, path, false); + + LOGGER.info(" PUT succeeded"); + LOGGER.info("Done!!"); + + System.exit(0); + + } catch (AAIException e) { + if ( !doPutIfExists ) { // ignore 412 failure + if ( e.getMessage().equals("AAI_7116") ) { + if ( e.getMessage().indexOf("status=412") > 0) { + LOGGER.info("PUT succeeded, return 412 ignored\n"); + LOGGER.info("\nDone!!"); + System.exit(0); + } + } + } + + ErrorLogHelper.logException(e); + System.exit(1); + } catch (Exception e) { + ErrorLogHelper.logError("AAI_7402", e.getMessage()); + System.exit(1); + } + } + + /** + * Gets the single instance of PutResource. + * + * @param <T> the generic type + * @param clazz the clazz + * @return single instance of PutResource + * @throws IllegalAccessException the illegal access exception + * @throws InstantiationException the instantiation exception + */ + public static <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException + { + return clazz.newInstance(); + } + + /** + * Read json file. + * + * @param <T> the generic type + * @param clazz the clazz + * @param fName the f name + * @return the t + * @throws AAIException the AAI exception + */ + public static <T> T readJsonFile( Class<T> clazz, String fName ) throws AAIException + { + String jsonData = ""; + BufferedReader br = null; + T t; + + try { + String line; + br = new BufferedReader(new FileReader(fName)); + while ((line = br.readLine()) != null) { + jsonData += line + "\n"; + } + } catch (IOException e) { + + throw new AAIException("AAI_7403", e, "Error opening json file"); + } finally { + try { + if (br != null) + br.close(); + } catch (IOException ex) { + throw new AAIException("AAI_7403", ex, "Error closing json file"); + } + } + + try { + t = MapperUtil.readWithDashesAsObjectOf(clazz, jsonData); + } + catch (Exception je){ + throw new AAIException("AAI_7403", je, "Error parsing json file"); + } + + return t; + + }//End readJsonFile() + + /** + * Gets the resource version. + * + * @param <T> the generic type + * @param resource the resource + * @return the string + */ + public static <T> String GetResourceVersion(T resource) + { + Field[] fields = resource.getClass().getDeclaredFields(); + if (fields != null) + { + for (Field field : fields) + { + try + { + field.setAccessible(true); + if ( field.getName().equalsIgnoreCase("resourceVersion") ) + { + Object obj = field.get(resource); + return (String)obj; + } + + + } + catch (Exception e) + { + + } + + + } + } + return null; + } + + /** + * Node exists. + * + * @param url the url + * @return true, if successful + * @throws AAIException the AAI exception + */ + public static boolean nodeExists(String url) throws AAIException { + try{ + String useBasicAuth = AAIConfig.get("aai.tools.enableBasicAuth"); + Client client = null; + + if("true".equals(useBasicAuth)){ + client = HttpsAuthClient.getBasicAuthClient(); + } else { + client = HttpsAuthClient.getTwoWaySSLClient(); + } + + LOGGER.info("Getting the resource...: " + url); + + ClientResponse cres = client.resource(url) + .header("X-TransactionId", TRANSID) + .header("X-FromAppId", FROMAPPID) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .accept("application/json") + .get(ClientResponse.class); + + + if (cres.getStatus() == 404) { // resource not found + return false; + } else if (cres.getStatus() == 200){ + return true; + } else { + LOGGER.error("Getting the Resource failed: " + cres.getStatus() + + ": " + cres.getEntity(String.class)); + return false; + } + } catch (KeyManagementException e) { + throw new AAIException("AAI_7401", e, "Error during GET"); + } catch (Exception e) { + throw new AAIException("AAI_7402", e, "Error during GET"); + } + } + +} diff --git a/aai-resources/src/main/java/org/openecomp/aai/util/RestController.java b/aai-resources/src/main/java/org/openecomp/aai/util/RestController.java new file mode 100644 index 0000000..e21fd90 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/util/RestController.java @@ -0,0 +1,685 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 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.openecomp.aai.util; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.security.KeyManagementException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.logging.LoggingContext; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientHandlerException; +import com.sun.jersey.api.client.ClientResponse; + +public class RestController { + + private static EELFLogger LOGGER = EELFManager.getInstance().getLogger(RestController.class); + + private static Client client = null; + + private String restSrvrBaseURL; + + //To do - Come up with helper function that will automatically + //generate the REST API path based on path parameter(s) and query parameter(s)! + public static final String REST_APIPATH_COMPLEXES = "cloud-infrastructure/complexes"; + public static final String REST_APIPATH_COMPLEX = "cloud-infrastructure/complexes/complex/"; + public static final String REST_APIPATH_PSERVERS = "cloud-infrastructure/pservers"; + public static final String REST_APIPATH_PSERVER = "cloud-infrastructure/pservers/pserver/"; + public static final String REST_APIPATH_PHYSICALLINKS = "network/physical-links/"; + public static final String REST_APIPATH_PHYSICALLINK = "network/physical-links/physical-link/"; + public static final String REST_APIPATH_PINTERFACES = "network/p-interfaces/"; + public static final String REST_APIPATH_PINTERFACE = "network/p-interfaces/p-interface/"; + public static final String REST_APIPATH_VPLSPES = "network/vpls-pes/"; + public static final String REST_APIPATH_VPLSPE = "network/vpls-pes/vpls-pe/"; + public static final String REST_APIPATH_UPDATE = "actions/update/"; + public static final String REST_APIPATH_SEARCH = "search/nodes-query?search-node-type="; + + public static final String REST_APIPATH_CLOUDREGION = "cloud-infrastructure/cloud-regions/cloud-region/"; + public static final String REST_APIPATH_TENANT = "cloud-infrastructure/tenants/tenant/"; + public static final String REST_APIPATH_VPE = "network/vpes/vpe/"; + public static final String REST_APIPATH_VIRTUAL_DATA_CENTER = "cloud-infrastructure/virtual-data-centers/virtual-data-center/"; + public static final String REST_APIPATH_VIRTUAL_DATA_CENTERS = "cloud-infrastructure/virtual-data-centers/"; + //network/generic-vnfs/generic-vnf/{vnf-id} + public static final String REST_APIPATH_GENERIC_VNF = "network/generic-vnfs/generic-vnf/"; + public static final String REST_APIPATH_GENERIC_VNFS = "network/generic-vnfs"; + public static final String REST_APIPATH_L3_NETWORK = "network/l3-networks/l3-network/"; + public static final String REST_APIPATH_L3_NETWORKS = "network/l3-networks"; + public static final String REST_APIPATH_INSTANCE_GROUP = "network/instance-groups/instance-group"; + public static final String REST_APIPATH_INSTANCE_GROUPS = "network/instance-groups"; + + public static final String REST_APIPATH_VCE = "network/vces/vce/"; + + public static final String REST_APIPATH_SERVICE = "service-design-and-creation/services/service/"; + public static final String REST_APIPATH_LOGICALLINKS = "network/logical-links/"; + public static final String REST_APIPATH_LOGICALLINK = "network/logical-links/logical-link/"; + + /** + * Inits the rest client. + * + * @throws AAIException the AAI exception + */ + private static void initRestClient() throws AAIException + { + if (client == null) { + try { + String useBasicAuth = AAIConfig.get("aai.tools.enableBasicAuth"); + if (useBasicAuth != null && useBasicAuth.equals("true")) { + + client = HttpsAuthClient.getBasicAuthClient(); + } else { + client = HttpsAuthClient.getTwoWaySSLClient(); + } + + } + catch (KeyManagementException e){ + throw new AAIException("AAI_7117", "KeyManagementException in REST call to DB: " + e.toString()); + } catch (Exception e) { + throw new AAIException("AAI_7117", " Exception in REST call to DB: " + e.toString()); + } + } + } + + /** + * Sets the rest srvr base URL. + * + * @param baseURL the base URL + * @throws AAIException the AAI exception + */ + public void SetRestSrvrBaseURL(String baseURL) throws AAIException + { + if (baseURL == null) + throw new AAIException("AAI_7117", "REST Server base URL cannot be null."); + restSrvrBaseURL = baseURL; + } + + /** + * Gets the rest srvr base URL. + * + * @return the rest srvr base URL + */ + public String getRestSrvrBaseURL() + { + return restSrvrBaseURL; + } + + /** + * To do - optimization and automation. Also make it as generic as possible. + * + * @param <T> the generic type + * @param t the t + * @param sourceID the source ID + * @param transId the trans id + * @param path the path + * @param restObject the rest object + * @param oldserver the oldserver + * @throws AAIException the AAI exception + */ + @SuppressWarnings("unchecked") + public static <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver) throws AAIException { + String methodName = "Get"; + String url=""; + transId += ":" + UUID.randomUUID().toString(); + + LoggingContext.save(); + LoggingContext.partnerName(sourceID); + + LOGGER.debug(methodName + " start"); + + restObject.set(t); + + if (oldserver) + url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path; + else + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + path; + initRestClient(); + LOGGER.debug(url + " for the get REST API"); + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", sourceID) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .get(ClientResponse.class); + +// System.out.println("cres.EntityInputSream()="+cres.getEntityInputStream().toString()); +// System.out.println("cres.tostring()="+cres.toString()); + + if (cres.getStatus() == 200) { +// System.out.println(methodName + ": url=" + url); + t = (T) cres.getEntity(t.getClass()); + restObject.set(t); + LOGGER.debug(methodName + "REST api GET was successfull!"); + } else { + LoggingContext.restore(); +// System.out.println(methodName + ": url=" + url + " failed with status=" + cres.getStatus()); + throw new AAIException("AAI_7116", methodName +" with status="+cres.getStatus()+", url="+url); + } + + LoggingContext.restore(); + } + + + /** + * + * @param <T> the generic type + * @param t the t + * @param sourceID the source ID + * @param transId the trans id + * @param path the path + * @param restObject the rest object + * @param apiVersion the api version + * @throws AAIException the AAI exception + */ + @SuppressWarnings("unchecked") + public static <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, String apiVersion) throws AAIException { + String methodName = "Get"; + String url=""; + transId += ":" + UUID.randomUUID().toString(); + LOGGER.debug(methodName + " start"); + + restObject.set(t); + + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + apiVersion + "/"+ path; + + initRestClient(); + LOGGER.debug(url + " for the get REST API"); + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", sourceID) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .get(ClientResponse.class); + +// System.out.println("cres.EntityInputSream()="+cres.getEntityInputStream().toString()); +// System.out.println("cres.tostring()="+cres.toString()); + + if (cres.getStatus() == 200) { +// System.out.println(methodName + ": url=" + url); + t = (T) cres.getEntity(t.getClass()); + restObject.set(t); + LOGGER.debug(methodName + "REST api GET was successfull!"); + + } else { +// System.out.println(methodName + ": url=" + url + " failed with status=" + cres.getStatus()); + throw new AAIException("AAI_7116", methodName +" with status="+cres.getStatus()+", url="+url); + } + + } + + /** + * Gets the. + * + * @param <T> the generic type + * @param t the t + * @param requestObj the request obj + * @throws AAIException the AAI exception + */ + public static <T> void Get(T t, Request<T> requestObj) throws AAIException { + String methodName = "Get"; + String url=""; + String transId = requestObj.transactionId; + transId += ":" + UUID.randomUUID().toString(); + LOGGER.debug(methodName + " start"); + + requestObj.restObj.set(t); + + if (requestObj.oldServer) + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + "server/" + requestObj.path; + else + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + requestObj.path; + initRestClient(); + + try { + URL urlObj= new URL(url); + URI uri = new URI(urlObj.getProtocol(), urlObj.getUserInfo(), urlObj.getHost(), urlObj.getPort(), urlObj.getPath(), urlObj.getQuery(), urlObj.getRef()); + url = uri.toASCIIString(); + } catch (URISyntaxException | MalformedURLException e) { + throw new AAIException("AAI_7116", "bad URL"); + + } + LOGGER.debug(url + " for the get REST API"); + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", requestObj.fromAppId) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .get(ClientResponse.class); + +// System.out.println("cres.EntityInputSream()="+cres.getEntityInputStream().toString()); +// System.out.println("cres.tostring()="+cres.toString()); + + if (cres.getStatus() == 200) { +// System.out.println(methodName + ": url=" + url); + t = (T) cres.getEntity(t.getClass()); + requestObj.restObj.set(t); + LOGGER.debug(methodName + "REST api GET was successfull!"); + + } else { +// System.out.println(methodName + ": url=" + url + " failed with status=" + cres.getStatus()); + throw new AAIException("AAI_7116", methodName +" with status="+cres.getStatus()+", url="+url); + } + + } + + /** + * Put. + * + * @param <T> the generic type + * @param t the t + * @param requestObj the request obj + * @throws AAIException the AAI exception + */ + public static <T> void Put(T t, Request<T> requestObj) throws AAIException { + String methodName = "Put"; + String url=""; + String transId = requestObj.transactionId; + transId += ":" + UUID.randomUUID().toString(); + LOGGER.debug(methodName + " start"); + + initRestClient(); + + if (requestObj.oldServer) + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + "server/" + requestObj.path; + else + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + requestObj.path; + + try { + URL urlObj= new URL(url); + URI uri = new URI(urlObj.getProtocol(), urlObj.getUserInfo(), urlObj.getHost(), urlObj.getPort(), urlObj.getPath(), urlObj.getQuery(), urlObj.getRef()); + url = uri.toASCIIString(); + } catch (URISyntaxException | MalformedURLException e) { + throw new AAIException("AAI_7116", "bad URL"); + + } + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", requestObj.fromAppId) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .entity(t) + .put(ClientResponse.class); + + int statuscode = cres.getStatus(); + if ( statuscode >= 200 && statuscode <= 299 ) { + LOGGER.debug(methodName+": url=" + url + ", request=" + url); + } else { + throw new AAIException("AAI_7116", methodName +" with status="+statuscode+", url="+url); + } + } + + /** + * Multiple Generic Get. + * + * @param <T> the generic type + * @param t the t + * @param sourceID the source ID + * @param transId the trans id + * @param path the path + * @param oldserver the oldserver + * @return the list + * @throws AAIException the AAI exception + */ + public static <T> List<T> Get(T t, String sourceID, String transId, String path, boolean oldserver) throws AAIException { + String methodName = "Get"; + String url=""; + transId += ":" + UUID.randomUUID().toString(); + LOGGER.debug(methodName + " start"); + + List<T> list; + + try { + if (oldserver) + url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path; + else + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + path; + initRestClient(); + LOGGER.debug(url + " for the get REST API"); + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", sourceID) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .get(ClientResponse.class); + + + //System.out.println("cres.EntityInputSream()="+cres.getEntityInputStream().toString()); + //System.out.println("cres.tostring()="+cres.toString()); + + if (cres.getStatus() == 200) { + //System.out.println(methodName + ": url=" + url); + //list = cres.getEntity(new GenericType<List<T>>() { }); + String datainJson = cres.getEntity(String.class); + list = mapJsonToObjectList(t, datainJson, t.getClass()); + + LOGGER.debug(methodName + "REST api GET was successfull!"); + return list; + + } else { + throw new AAIException("AAI_7116", methodName +" with status="+cres.getStatus()+", url="+url); + } + } catch (AAIException e) { + throw new AAIException("AAI_7116", methodName + " with url="+url+ ", Exception: " + e.toString()); + } catch (Exception e) + { + throw new AAIException("AAI_7116", methodName + " with url="+url+ ", Exception: " + e.toString()); + + } + + } + + /** + * Map json to object list. + * + * @param <T> the generic type + * @param typeDef the type def + * @param json the json + * @param clazz the clazz + * @return the list + * @throws Exception the exception + */ + private static <T> List<T> mapJsonToObjectList(T typeDef,String json, Class clazz) throws Exception + { + List<T> list; + ObjectMapper mapper = new ObjectMapper(); + System.out.println(json); + TypeFactory t = TypeFactory.defaultInstance(); + list = mapper.readValue(json, t.constructCollectionType(ArrayList.class,clazz)); + + return list; + } + + /** + * Put. + * + * @param <T> the generic type + * @param t the t + * @param sourceID the source ID + * @param transId the trans id + * @param path the path + * @throws AAIException the AAI exception + */ + public static <T> void Put(T t, String sourceID, String transId, String path) throws AAIException { + Put( t, sourceID, transId, path, false); + } + + /** + * Put. + * + * @param <T> the generic type + * @param t the t + * @param sourceID the source ID + * @param transId the trans id + * @param path the path + * @param oldserver the oldserver + * @throws AAIException the AAI exception + */ + public static <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver) throws AAIException { + String methodName = "Put"; + String url=""; + transId += ":" + UUID.randomUUID().toString(); + + LOGGER.debug(methodName + " start"); + + initRestClient(); + + if (oldserver) + url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path; + else + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + path; + + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", sourceID) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .entity(t) + .put(ClientResponse.class); + +// System.out.println("cres.tostring()="+cres.toString()); + + int statuscode = cres.getStatus(); + if ( statuscode >= 200 && statuscode <= 299 ) { + LOGGER.debug(methodName+": url=" + url + ", request=" + path); + } else { + throw new AAIException("AAI_7116", methodName +" with status="+statuscode+", url="+url + ", msg=" + cres.getEntity(String.class)); + } + } + + /** + * Delete. + * + * @param requestObj the request obj + * @throws AAIException the AAI exception + */ + public static void Delete(Request requestObj) throws AAIException { + String methodName = "Delete"; + + String url=""; + String transId = requestObj.transactionId; + transId += ":" + UUID.randomUUID().toString(); + + LOGGER.debug(methodName + " start"); + + initRestClient(); + + if (requestObj.oldServer) + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + "servers/" + requestObj.path; + else + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + requestObj.path; + + try { + URL urlObj= new URL(url); + URI uri = new URI(urlObj.getProtocol(), urlObj.getUserInfo(), urlObj.getHost(), urlObj.getPort(), urlObj.getPath(), urlObj.getQuery(), urlObj.getRef()); + url = uri.toASCIIString(); + } catch (URISyntaxException | MalformedURLException e) { + throw new AAIException("AAI_7116", "bad URL"); + + } + + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", requestObj.fromAppId) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .entity("{}") + .delete(ClientResponse.class); + + if (cres.getStatus() == 204) { + LOGGER.debug(methodName+": url=" + url); + } else { + throw new AAIException("AAI_7116", methodName +" with status="+cres.getStatus()+", url="+url); + } + + } + + /** + * Delete. + * + * @param sourceID the source ID + * @param transId the trans id + * @param path the path + * @throws AAIException the AAI exception + */ + public static void Delete(String sourceID, String transId, String path) throws AAIException { + String methodName = "Delete"; + String url=""; + transId += ":" + UUID.randomUUID().toString(); + LOGGER.debug(methodName + " start"); + + initRestClient(); + String request = "{}"; + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + path; + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", sourceID) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .entity(request) + .delete(ClientResponse.class); + + if (cres.getStatus() == 404) { // resource not found + LOGGER.info("Resource does not exist...: " + cres.getStatus() + + ":" + cres.getEntity(String.class)); + } else if (cres.getStatus() == 200 || cres.getStatus() == 204){ + LOGGER.info("Resource " + url + " deleted"); + } else { + LOGGER.error("Deleting Resource failed: " + cres.getStatus() + + ":" + cres.getEntity(String.class)); + throw new AAIException("AAI_7116", "Error during DELETE"); + } + } + + /** + * Post. + * + * @param <T> the generic type + * @param t the t + * @param sourceID the source ID + * @param transId the trans id + * @param path the path + * @return the string + * @throws Exception the exception + */ + public static <T> String Post(T t, String sourceID, String transId, String path) throws Exception { + String methodName = "Post"; + String url=""; + transId += ":" + UUID.randomUUID().toString(); + + LOGGER.debug(methodName + " start"); + + try { + + initRestClient(); + + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + path; + + ClientResponse cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", sourceID) + .header("Authorization", HttpsAuthClient.getBasicAuthHeaderValue()) + .type("application/json") + .entity(t) + .post(ClientResponse.class); + + int statuscode = cres.getStatus(); + if ( statuscode >= 200 && statuscode <= 299 ) { + LOGGER.debug(methodName + "REST api POST was successful!"); + return cres.getEntity(String.class); + } else { + throw new AAIException("AAI_7116", methodName +" with status="+statuscode+", url="+url + ", msg=" + cres.getEntity(String.class)); + } + + } catch (AAIException e) { + throw new AAIException("AAI_7116", methodName + " with url="+url+ ", Exception: " + e.toString()); + } catch (Exception e) + { + throw new AAIException("AAI_7116", methodName + " with url="+url+ ", Exception: " + e.toString()); + + } + } + + + /** + * Gets the single instance of RestController. + * + * @param <T> the generic type + * @param clazz the clazz + * @return single instance of RestController + * @throws IllegalAccessException the illegal access exception + * @throws InstantiationException the instantiation exception + */ + public static <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException + { + return clazz.newInstance(); + } + + /** + * Does resource exist. + * + * @param <T> the generic type + * @param resourcePath the resource path + * @param resourceClassName the resource class name + * @param fromAppId the from app id + * @param transId the trans id + * @return the t + */ + /* + * DoesResourceExist + * + * To check whether a resource exist or get a copy of the existing version of the resource + * + * Resourcepath: should contain the qualified resource path (including encoded unique key identifier value), + * resourceClassName: is the canonical name of the resource class name, + * fromAppId: + * transId: + * + * Will return null (if the resource doesn’t exist) (or) + * Will return the specified resource from the Graph. + * + * Example: + * LogicalLink llink = new LogicalLink(); + * String resourceClassName = llink.getClass().getCanonicalName(); + * llink = RestController.DoesResourceExist("network/logical-links/logical-link/" + <encoded-link-name>, resourceClassName, fromAppId, transId); + */ + public static <T> T DoesResourceExist(String resourcePath, String resourceClassName, String fromAppId, String transId) { + + try { + + RestObject<T> restObj = new RestObject<T>(); + @SuppressWarnings("unchecked") + T resourceObj = (T)getInstance(Class.forName(resourceClassName)); + restObj.set(resourceObj); + RestController.<T>Get(resourceObj, fromAppId, transId, resourcePath, restObj, false); + + resourceObj = restObj.get(); + if (resourceObj != null) + return resourceObj; + + } catch (AAIException e) { + + } catch (ClientHandlerException che) { + + }catch (Exception e) { + + } + + return null; + } + +} diff --git a/aai-resources/src/main/java/org/openecomp/aai/util/UpdateResource.java b/aai-resources/src/main/java/org/openecomp/aai/util/UpdateResource.java new file mode 100644 index 0000000..adecfb1 --- /dev/null +++ b/aai-resources/src/main/java/org/openecomp/aai/util/UpdateResource.java @@ -0,0 +1,126 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 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.openecomp.aai.util; + +import java.util.Properties; +import java.util.UUID; + +import org.openecomp.aai.domain.yang.Action; +import org.openecomp.aai.domain.yang.ActionData; +import org.openecomp.aai.domain.yang.Update; +import org.openecomp.aai.exceptions.AAIException; +import org.openecomp.aai.logging.ErrorLogHelper; +import com.att.eelf.configuration.Configuration; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + + +public class UpdateResource { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(UpdateResource.class.getName()); + private static final String FROMAPPID = "AAIUPDT"; + private static final String TRANSID = UUID.randomUUID().toString(); + private static final String UPDATE_URL = "actions/update"; + private static final String USAGE_STRING = "Usage: updateTool.sh \n" + + "<node type> <update node URI> <property name>:<property value>[,<property name>:<property value]* | \n" + + "where update node uri is the URI path for that node \n" + + " for ex. ./updateTool.sh pserver cloud-infrastructure/pservers/pserver/XXX prov-status:NEWSTATUS"; + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args) { + + String nodeType = null; + String nodeURI = null; + String updValueList = null; + + // Set the logging file properties to be used by EELFManager + Properties props = System.getProperties(); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_UPDTOOL_LOGBACK_PROPS); + props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); + + try { + if (args.length < 3) { + System.out.println("Nothing to update or Insufficient arguments"); + System.out.println(USAGE_STRING); + System.exit(1); + } else { + nodeType = args[0]; + nodeURI = args[1]; + updValueList = args[2]; + + update(nodeType, nodeURI, updValueList); + } + + System.exit(0); + + } catch (AAIException e) { + ErrorLogHelper.logException(e); + System.exit(1); + } catch (Exception e) { + ErrorLogHelper.logError("AAI_7402", "Update failed: " + e.getMessage()); + System.exit(1); + } + } + + /** + * Update. + * + * @param <T> the generic type + * @param aaiLogger the aai logger + * @param logline the logline + * @param nodeType the node type + * @param nodeURI the node URI + * @param updValueList the upd value list + * @throws AAIException the AAI exception + */ + public static <T> void update(String nodeType, String nodeURI, String updValueList) throws AAIException { + try { + Update update = new Update(); + update.setUpdateNodeType(nodeType); + update.setUpdateNodeUri(nodeURI); + + Action action = new Action(); + action.setActionType("replace"); + + for (String updValue: updValueList.split(",")) { + ActionData data = new ActionData(); + data.setPropertyName(updValue.substring(0, updValue.indexOf(':'))); + data.setPropertyValue(updValue.substring(updValue.indexOf(':') + 1)); + action.getActionData().add(data); + } + + update.getAction().add(action); + + LOGGER.info("updating the resource... "); + + RestController.<Update>Put(update, FROMAPPID, TRANSID, UPDATE_URL); + LOGGER.info("Update Successful"); + } catch (AAIException e) { + throw e; + } catch (Exception e) { + throw new AAIException("AAI_7402", e, "Error during UPDATE"); + } + } +} |