diff options
Diffstat (limited to 'aai-core/src/main/java')
156 files changed, 6884 insertions, 10079 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/audit/ListEndpoints.java b/aai-core/src/main/java/org/onap/aai/audit/ListEndpoints.java index 3ae6d802..c56fdd7f 100644 --- a/aai-core/src/main/java/org/onap/aai/audit/ListEndpoints.java +++ b/aai-core/src/main/java/org/onap/aai/audit/ListEndpoints.java @@ -30,19 +30,21 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; - +import org.onap.aai.config.SpringContextAware; import org.onap.aai.db.props.AAIProperties; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.introspection.LoaderFactory; import org.onap.aai.introspection.ModelType; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.logging.LogFormatTools; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.common.base.CaseFormat; +import org.onap.aai.setup.SchemaVersions; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * The Class ListEndpoints. @@ -51,8 +53,8 @@ public class ListEndpoints { private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ListEndpoints.class); - private final static String start = "inventory"; - private final static String[] blacklist = { "search", "aai-internal" }; + private final String start = "inventory"; + private final String[] blacklist = { "search", "aai-internal" }; private List<String> endpoints = new ArrayList<>(); private Map<String, String> endpointToLogicalName = new HashMap<String, String>(); @@ -63,10 +65,24 @@ public class ListEndpoints { * @param args the arguments */ public static void main(String[] args) { - ListEndpoints endPoints = new ListEndpoints(AAIProperties.LATEST); - System.out.println(endPoints.toString("relationship-list")); + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( + "org.onap.aai.config", + "org.onap.aai.setup" + ); + + String schemaUriBasePath = context.getEnvironment().getProperty("schema.uri.base.path"); + + if(schemaUriBasePath == null){ + System.err.println("Unable to find the property schema.uri.base.path," + +" please check if specified in system property or in schema-ingest.properties" + ); + } + + SchemaVersions schemaVersions = context.getBean(SchemaVersions.class); + ListEndpoints endPoints = new ListEndpoints(schemaUriBasePath, schemaVersions.getDefaultVersion()); + LOGGER.info(endPoints.toString("relationship-list")); } /** @@ -74,14 +90,14 @@ public class ListEndpoints { * * @param version the version */ - public ListEndpoints(Version version) { + public ListEndpoints(String basePath, SchemaVersion version) { - Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version); + Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY, version); try { final Introspector start = loader.introspectorFromName(this.start); Set<String> startMap = new HashSet<>(); - beginAudit(start, "/aai/" + version, startMap); + beginAudit(start, basePath + "/" + version, startMap); } catch (AAIUnknownObjectException e) { throw new RuntimeException("Failed to find object " + this.start + ", cannot run ListEndpoints audit"); } @@ -97,18 +113,20 @@ public class ListEndpoints { String currentUri = ""; - if (!obj.getDbName().equals(start)) { + if (!obj.getDbName().equals("inventory")) { currentUri = uri + obj.getGenericURI(); } else { currentUri = uri; } - if ("relationship-data".equals(obj.getName()) || "related-to-property".equals(obj.getName())) { + if (obj.getName().equals("relationship-data") || obj.getName().equals("related-to-property")) { return; } if (!obj.isContainer()) { endpoints.add(currentUri); } + String dbName = obj.getDbName(); + populateLogicalName(obj, uri, currentUri); Set<String> properties = obj.getProperties(); @@ -175,7 +193,7 @@ public class ListEndpoints { */ private void populateLogicalName(Introspector obj, String uri, String currentUri) { - if (obj.getDbName().equals(start) || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) { + if (obj.getDbName().equals("inventory") || currentUri.split("/").length <= 4 || currentUri.endsWith("relationship-list")) { return; } @@ -247,7 +265,7 @@ public class ListEndpoints { List<String> result = new ArrayList<>(); Pattern p = null; Matcher m = null; - if (!filterOut.isEmpty()) { + if (!filterOut.equals("")) { p = Pattern.compile(filterOut); m = null; } diff --git a/aai-core/src/main/java/org/onap/aai/auth/AAIAuthCore.java b/aai-core/src/main/java/org/onap/aai/auth/AAIAuthCore.java new file mode 100644 index 00000000..1572ec72 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/auth/AAIAuthCore.java @@ -0,0 +1,350 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.auth; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.eclipse.jetty.util.security.Password; +import org.eclipse.persistence.internal.oxm.conversion.Base64; +import org.onap.aai.auth.exceptions.AAIUnrecognizedFunctionException; +import org.onap.aai.logging.ErrorLogHelper; +import org.onap.aai.logging.LoggingContext; +import org.onap.aai.logging.LoggingContext.StatusCode; +import org.onap.aai.util.AAIConfig; +import org.onap.aai.util.AAIConstants; +import org.onap.aai.util.FileWatcher; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.UnsupportedEncodingException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * The Class AAIAuthCore. + */ +public final class AAIAuthCore { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIAuthCore.class); + + private String globalAuthFileName = AAIConstants.AAI_AUTH_CONFIG_FILENAME; + + private final Pattern AUTH_POLICY_PATTERN; + private final Set<String> validFunctions = new HashSet<>(); + private Map<String, AAIUser> users; + private boolean timerSet = false; + private Timer timer = null; + + private String basePath; + /** + * Instantiates a new AAI auth core. + */ + public AAIAuthCore(String basePath) { + this.basePath = basePath; + AUTH_POLICY_PATTERN = Pattern.compile("^" + this.basePath +"/v\\d+/([\\w\\-]*)"); + init(); + } + + /** + * Inits the. + */ + private synchronized void init() { + + LOGGER.debug("Initializing Auth Policy Config"); + + reloadUsers(); + + /* + * this timer code is setting up a recurring task that checks if the + * auth config file has been updated and reloads the users if so to get + * the most up to date info (that update check logic is within + * FileWatcher) + * + * the timing this method uses is coarser than the frequency of requests + * AI&I gets so we're looking at better ways of doing this (TODO) + */ + TimerTask task = new FileWatcher(new File(globalAuthFileName)) { + @Override + protected void onChange(File file) { + reloadUsers(); + } + }; + + if (!timerSet) { + timerSet = true; + timer = new Timer(); + + // repeat the check every second + timer.schedule(task, new Date(), 10000); + } + LOGGER.debug("Static Initializiation complete"); + } + + /** + * Cleanup. + */ + // just ends the auth config file update checking timer + public void cleanup() { + timer.cancel(); + } + + /** + * Reload users. + */ + /* + * this essentially takes the data file, which is organized role-first with + * users under each role and converts it to data organized user-first with + * each user containing their role with its associated allowed functions + * this data stored in the class field users + */ + private synchronized void reloadUsers() { + + Map<String, AAIUser> tempUsers = new HashMap<>(); + + try { + LOGGER.debug("Reading from " + globalAuthFileName); + String authFile = new String(Files.readAllBytes(Paths.get(globalAuthFileName))); + + JsonParser parser = new JsonParser(); + JsonObject authObject = parser.parse(authFile).getAsJsonObject(); + if (authObject.has("roles")) { + JsonArray roles = authObject.getAsJsonArray("roles"); + for (JsonElement role : roles) { + if (role.isJsonObject()) { + JsonObject roleObject = role.getAsJsonObject(); + String roleName = roleObject.get("name").getAsString(); + Map<String, Boolean> usrs = this.getUsernamesFromRole(roleObject); + List<String> aaiFunctions = this.getAAIFunctions(roleObject); + + usrs.forEach((key, value) -> { + final AAIUser au = tempUsers.getOrDefault(key, new AAIUser(key, value)); + au.addRole(roleName); + aaiFunctions.forEach(f -> { + List<String> httpMethods = this.getRoleHttpMethods(f, roleObject); + httpMethods.forEach(hm -> au.setUserAccess(f, hm)); + this.validFunctions.add(f); + }); + + tempUsers.put(key, au); + + }); + } + } + if (!tempUsers.isEmpty()) { + users = tempUsers; + } + } + } catch (FileNotFoundException e) { + ErrorLogHelper.logError("AAI_4001", globalAuthFileName + ". Exception: " + e); + } catch (JsonProcessingException e) { + ErrorLogHelper.logError("AAI_4001", globalAuthFileName + ". Not valid JSON: " + e); + } catch (Exception e) { + ErrorLogHelper.logError("AAI_4001", globalAuthFileName + ". Exception caught: " + e); + } + } + + private List<String> getRoleHttpMethods(String aaiFunctionName, JsonObject roleObject) { + List<String> httpMethods = new ArrayList<>(); + + JsonArray ja = roleObject.getAsJsonArray("functions"); + for (JsonElement je : ja) { + if (je.isJsonObject() && je.getAsJsonObject().has("name") && je.getAsJsonObject().get("name").getAsString().equals(aaiFunctionName)) { + JsonArray jaMeth = je.getAsJsonObject().getAsJsonArray("methods"); + for (JsonElement jeMeth : jaMeth) { + if (jeMeth.isJsonObject() && jeMeth.getAsJsonObject().has("name")) { + httpMethods.add(jeMeth.getAsJsonObject().get("name").getAsString()); + } + } + } + } + + return httpMethods; + } + + private List<String> getAAIFunctions(JsonObject roleObject) { + List<String> aaiFunctions = new ArrayList<>(); + + JsonArray ja = roleObject.getAsJsonArray("functions"); + for (JsonElement je : ja) { + if (je.isJsonObject() && je.getAsJsonObject().has("name")) { + aaiFunctions.add(je.getAsJsonObject().get("name").getAsString()); + } + } + + return aaiFunctions; + } + + private Map<String, Boolean> getUsernamesFromRole(JsonObject roleObject) throws UnsupportedEncodingException { + Map<String, Boolean> usernames = new HashMap<>(); + + JsonArray uja = roleObject.getAsJsonArray("users"); + for (JsonElement je : uja) { + if (je.isJsonObject()) { + if (je.getAsJsonObject().has("username")) { + if (je.getAsJsonObject().has("is-wildcard-id")) { + usernames.put(je.getAsJsonObject().get("username").getAsString().toLowerCase(), je.getAsJsonObject().get("is-wildcard-id").getAsBoolean()); + } else { + usernames.put(je.getAsJsonObject().get("username").getAsString().toLowerCase(), false); + } + } else if (je.getAsJsonObject().has("user")) { + String auth = je.getAsJsonObject().get("user").getAsString() + ":" + Password.deobfuscate(je.getAsJsonObject().get("pass").getAsString()); + String authorizationCode = new String(Base64.base64Encode(auth.getBytes("utf-8"))); + usernames.put(authorizationCode, false); + } + } + } + + return usernames; + } + + public String getAuthPolicyFunctName(String uri) { + String authPolicyFunctionName = ""; + if (uri.startsWith(basePath + "/search")) { + authPolicyFunctionName = "search"; + } else if (uri.startsWith(basePath + "/recents")) { + authPolicyFunctionName = "recents"; + }else if (uri.startsWith(basePath + "/util/echo")) { + authPolicyFunctionName = "util"; + } else if (uri.startsWith(basePath + "/tools")) { + authPolicyFunctionName = "tools"; + } else { + Matcher match = AUTH_POLICY_PATTERN.matcher(uri); + if (match.find()) { + authPolicyFunctionName = match.group(1); + } + } + return authPolicyFunctionName; + } + + /** + * for backwards compatibility + * @param username + * @param uri + * @param httpMethod + * @param haProxyUser + * @return + * @throws AAIUnrecognizedFunctionException + */ + public boolean authorize(String username, String uri, String httpMethod, String haProxyUser) throws AAIUnrecognizedFunctionException { + return authorize(username, uri, httpMethod, haProxyUser, null); + } + + /** + * + * @param username + * @param uri + * @param httpMethod + * @param haProxyUser + * @param issuer issuer of the cert + * @return + * @throws AAIUnrecognizedFunctionException + */ + public boolean authorize(String username, String uri, String httpMethod, String haProxyUser, String issuer) throws AAIUnrecognizedFunctionException { + String aaiMethod = this.getAuthPolicyFunctName(uri); + if (!this.validFunctions.contains(aaiMethod)) { + throw new AAIUnrecognizedFunctionException(aaiMethod); + } + boolean wildcardCheck = isWildcardIssuer(issuer); + boolean authorized; + LOGGER.debug("Authorizing the user for the request cert {}, haproxy header {}, aai method {}, httpMethod {}, cert issuer {}", + username, haProxyUser, aaiMethod, httpMethod, issuer); + Optional<AAIUser> oau = this.getUser(username, wildcardCheck); + if (oau.isPresent()) { + AAIUser au = oau.get(); + if (au.hasRole("HAProxy")) { + LOGGER.debug("User has HAProxy role"); + if ("GET".equalsIgnoreCase(httpMethod) && "util".equalsIgnoreCase(aaiMethod) && haProxyUser.isEmpty()) { + LOGGER.debug("Authorized user has HAProxy role with echo request"); + authorized = this.authorize(au, aaiMethod, httpMethod); + } else { + authorized = this.authorize(haProxyUser, uri, httpMethod, "", issuer); + } + } else { + LOGGER.debug("User doesn't have HAProxy role so assuming its a regular client"); + authorized = this.authorize(au, aaiMethod, httpMethod); + } + } else { + LOGGER.debug("User not found: " + username + " on function " + aaiMethod + " request type " + httpMethod); + authorized = false; + } + + return authorized; + } + + private boolean isWildcardIssuer(String issuer) { + if (issuer != null && !issuer.isEmpty()) { + List<String> validIssuers = Arrays.asList(AAIConfig.get("aaf.valid.issuer.wildcard", UUID.randomUUID().toString()).split("\\|")); + for (String validIssuer : validIssuers) { + if (issuer.contains(validIssuer)) { + return true; + } + } + } + return false; + } + + /** + * returns aai user either matching the username or containing the wildcard. + * @param username + * @return + */ + public Optional<AAIUser> getUser(String username, boolean wildcardCheck) { + if (users.containsKey(username)) { + return Optional.of(users.get(username)); + } else if (wildcardCheck){ + List<AAIUser> laus = users.entrySet().stream().filter(e -> e.getValue().isWildcard() && username.contains(e.getKey())).map(Map.Entry::getValue).collect(Collectors.toList()); + if (!laus.isEmpty()) { + return Optional.of(laus.get(0)); + } + } + return Optional.empty(); + } + + /** + * + * @param aaiUser + * aai user with the username + * @param aaiMethod + * aai function the authorization is required on + * @param httpMethod + * http action user is attempting + * @return true, if successful + */ + private boolean authorize(AAIUser aaiUser, String aaiMethod, String httpMethod) { + if (aaiUser.hasAccess(aaiMethod, httpMethod)) { + LoggingContext.statusCode(StatusCode.COMPLETE); + LOGGER.debug("AUTH ACCEPTED: " + aaiUser.getUsername() + " on function " + aaiMethod + " request type " + httpMethod); + return true; + } else { + LoggingContext.statusCode(StatusCode.ERROR); + LOGGER.debug("AUTH FAILED: " + aaiUser.getUsername() + " on function " + aaiMethod + " request type " + httpMethod); + return false; + } + } +} diff --git a/aai-core/src/main/java/org/onap/aai/auth/AAIUser.java b/aai-core/src/main/java/org/onap/aai/auth/AAIUser.java new file mode 100644 index 00000000..d0f7456e --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/auth/AAIUser.java @@ -0,0 +1,78 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.auth; + +import java.util.*; + +public class AAIUser { + + private String username; + + private boolean isWildcard = false; + private Set<String> roles; + private Map<String, Set<String>> aaiFunctionToHttpMethod; + + public AAIUser(String username) { + this(username, false); + } + + public AAIUser(String username, boolean isWildcard) { + this.username = username; + this.roles = new HashSet<>(); + this.aaiFunctionToHttpMethod = new HashMap<>(); + this.isWildcard = isWildcard; + } + + public boolean isWildcard() { + return isWildcard; + } + + public String getUsername() { + return username; + } + + public void addRole(String role) { + this.roles.add(role); + } + + public boolean hasRole(String role) { + return this.roles.contains(role); + } + + public void setUserAccess(String aaiMethod, String... httpMethods) { + for (String httpMethod : httpMethods) { + this.addUserAccess(aaiMethod, httpMethod); + } + } + + private void addUserAccess(String aaiMethod, String httpMethod) { + Set<String> httpMethods = new HashSet<>(); + if (this.aaiFunctionToHttpMethod.containsKey(aaiMethod)) { + httpMethods = this.aaiFunctionToHttpMethod.get(aaiMethod); + } + httpMethods.add(httpMethod); + this.aaiFunctionToHttpMethod.put(aaiMethod, httpMethods); + } + + public boolean hasAccess(String aaiMethod, String httpMethod) { + return this.aaiFunctionToHttpMethod.getOrDefault(aaiMethod, Collections.EMPTY_SET).contains(httpMethod); + } + +} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/util/VersionChecker.java b/aai-core/src/main/java/org/onap/aai/concurrent/AaiCallable.java index a15fd22c..0f3469e9 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/util/VersionChecker.java +++ b/aai-core/src/main/java/org/onap/aai/concurrent/AaiCallable.java @@ -1,37 +1,48 @@ -/**
- * ============LICENSE_START=======================================================
- * org.onap.aai
- * ================================================================================
- * Copyright © 2017-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.aai.serialization.db.util;
-
-import org.onap.aai.introspection.Version;
-
-public class VersionChecker {
-
- private VersionChecker() {}
-
- /**
- * true if Version v is gt v12
- *
- * @param v - Version to be checked
- * @return
- */
- public static boolean apiVersionNeedsEdgeLabel(Version v) {
- return v.compareTo(Version.v12) >= 0 ;
- }
-}
+/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.concurrent; +import java.util.concurrent.Callable; +import java.util.Map; +import org.slf4j.MDC; + +/** + * The Class AaiCallable ensures that the Callable gets a copy of the MDC, so that any logging related fields are preserved + */ +public abstract class AaiCallable<T> implements Callable<T> { + private Map<String,String> mdcCopy; + /** + * The constructor. + */ + @SuppressWarnings("unchecked") + public AaiCallable() { + mdcCopy = MDC.getCopyOfContextMap(); + } + /** + * The call method + */ + public T call() throws Exception { + MDC.setContextMap(mdcCopy); + return process(); + } + /** + * The process method + */ + public abstract T process() throws Exception; +} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeProperty.java b/aai-core/src/main/java/org/onap/aai/config/AuthorizationConfiguration.java index de00594d..0efe1475 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeProperty.java +++ b/aai-core/src/main/java/org/onap/aai/config/AuthorizationConfiguration.java @@ -17,22 +17,23 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.aai.serialization.db; +package org.onap.aai.config; -public enum EdgeProperty { - CONTAINS("contains-other-v"), - DELETE_OTHER_V("delete-other-v"), - SVC_INFRA("SVC-INFRA"), - PREVENT_DELETE("prevent-delete"), - DESCRIPTION("description"); - private final String name; +import org.onap.aai.auth.AAIAuthCore; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; - private EdgeProperty(String name) { - this.name = name; - } +@Profile("two-way-ssl") +@Configuration +public class AuthorizationConfiguration { - @Override - public String toString() { - return name; - } + @Value("${schema.uri.base.path}") + private String basePath; + + @Bean + public AAIAuthCore aaiAuthCore(){ + return new AAIAuthCore(basePath); + } } diff --git a/aai-core/src/main/java/org/onap/aai/config/IntrospectionConfig.java b/aai-core/src/main/java/org/onap/aai/config/IntrospectionConfig.java new file mode 100644 index 00000000..e0ce203d --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/config/IntrospectionConfig.java @@ -0,0 +1,57 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.config; + +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.MoxyLoader; +import org.onap.aai.nodes.NodeIngestor; + +@Configuration +public class IntrospectionConfig { + + private Map<SchemaVersion, MoxyLoader> MOXYINSTANCEMAP = new ConcurrentHashMap<>(); + @Autowired + NodeIngestor nodeIngestor; + + @Bean + public LoaderFactory loaderFactory(SchemaVersions schemaVersions) { + LoaderFactory loaderFactory = new LoaderFactory(moxyLoaderInstance(schemaVersions)); + return loaderFactory; + } + + @Bean + public Map<SchemaVersion, MoxyLoader> moxyLoaderInstance(SchemaVersions schemaVersions) { + for(SchemaVersion version : schemaVersions.getVersions()){ + if (!MOXYINSTANCEMAP.containsKey(version)) { + MOXYINSTANCEMAP.put(version, new MoxyLoader(version, nodeIngestor)); + } + } + return MOXYINSTANCEMAP; + } +} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/EdgeProperty.java b/aai-core/src/main/java/org/onap/aai/config/RestBeanConfig.java index 6d10232d..29763291 100644 --- a/aai-core/src/main/java/org/onap/aai/db/schema/EdgeProperty.java +++ b/aai-core/src/main/java/org/onap/aai/config/RestBeanConfig.java @@ -17,52 +17,35 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.aai.db.schema; +package org.onap.aai.config; -import org.janusgraph.core.Multiplicity; -import org.codehaus.jackson.annotate.JsonProperty; -import org.codehaus.jackson.annotate.JsonPropertyOrder; -@JsonPropertyOrder({ "label", "multiplicity" }) -public class EdgeProperty implements Named { +import org.onap.aai.introspection.ModelType; - private String name = null; - private Multiplicity multiplicity = null; - - /** - * Gets the name - */ - @JsonProperty("label") - public String getName() { - return name; - } - - /** - * Sets the name. - * - * @param name the new name - */ - @JsonProperty("label") - public void setName(String name) { - this.name = name; +import org.onap.aai.rest.db.HttpEntry; +import org.onap.aai.serialization.engines.QueryStyle; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +@Configuration +public class RestBeanConfig { + @Bean(name = "traversalUriHttpEntry") + @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public HttpEntry traversalUriHttpEntry() { + HttpEntry httpEntry = new HttpEntry(ModelType.MOXY, QueryStyle.TRAVERSAL_URI); + return httpEntry; } - /** - * Gets the multiplicity. - * - * @return the multiplicity - */ - public Multiplicity getMultiplicity() { - return multiplicity; + @Bean(name = "traversalHttpEntry") + @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public HttpEntry traversalHttpEntry() { + HttpEntry httpEntry = new HttpEntry(ModelType.MOXY, QueryStyle.TRAVERSAL); + + return httpEntry; } - /** - * Sets the multiplicity. - * - * @param multiplicity the new multiplicity - */ - public void setMultiplicity(Multiplicity multiplicity) { - this.multiplicity = multiplicity; - } + } diff --git a/aai-core/src/main/java/org/onap/aai/config/SchemaConfiguration.java b/aai-core/src/main/java/org/onap/aai/config/SchemaConfiguration.java new file mode 100644 index 00000000..20112bde --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/config/SchemaConfiguration.java @@ -0,0 +1,62 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.config; + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.AAIConfigTranslator; +import org.onap.aai.serialization.db.EdgeSerializer; +import org.onap.aai.setup.ConfigTranslator; +import org.onap.aai.setup.SchemaLocationsBean; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.validation.CheckEverythingStrategy; +import org.onap.aai.validation.SchemaErrorStrategy; +import org.springframework.context.annotation.*; + +@Configuration +public class SchemaConfiguration { + + @Bean + public EdgeIngestor edgeIngestor(SchemaLocationsBean schemaLocationsBean, SchemaVersions schemaVersions){ + return new EdgeIngestor(configTranslator(schemaLocationsBean, schemaVersions), schemaVersions); + } + + @Bean + public EdgeSerializer edgeSerializer(EdgeIngestor edgeIngestor){ + return new EdgeSerializer(edgeIngestor); + } + + @Bean(name = "nodeIngestor") + public NodeIngestor nodeIngestor(ConfigTranslator configTranslator) { + NodeIngestor nodeIngestor = new NodeIngestor(configTranslator); + return nodeIngestor; + } + + @Bean(name = "configTranslator") + public ConfigTranslator configTranslator(SchemaLocationsBean schemaLocationsBean, SchemaVersions schemaVersions) { + ConfigTranslator aaiConfigTranslator = new AAIConfigTranslator(schemaLocationsBean, schemaVersions); + return aaiConfigTranslator; + } + + @Bean + public SchemaErrorStrategy schemaErrorStrategy(){ + return new CheckEverythingStrategy(); + } +} diff --git a/aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java b/aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java index d8043d4c..c58d57e9 100644 --- a/aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java +++ b/aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java @@ -19,6 +19,7 @@ */ package org.onap.aai.config; + import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -30,11 +31,34 @@ public class SpringContextAware implements ApplicationContextAware { private static ApplicationContext context = null; public static ApplicationContext getApplicationContext() { - return context; + return context; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - context = applicationContext; + context = applicationContext; + } + + + public static <T> T getBean(String beanName, Class<T> requiredType) { + if(context != null){ + return context.getBean(beanName, requiredType); + } + return null; + } + + public static <T> T getBean(Class<T> clazz){ + if(context != null){ + return context.getBean(clazz); + } + return null; } -}
\ No newline at end of file + + public static Object getBean(String bean){ + if(context != null){ + return context.getBean(bean); + } + return null; + } + +} diff --git a/aai-core/src/main/java/org/onap/aai/config/SwaggerGenerationConfiguration.java b/aai-core/src/main/java/org/onap/aai/config/SwaggerGenerationConfiguration.java new file mode 100644 index 00000000..a09018ad --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/config/SwaggerGenerationConfiguration.java @@ -0,0 +1,62 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.config; + + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.util.genxsd.HTMLfromOXM; +import org.onap.aai.util.genxsd.NodesYAMLfromOXM; +import org.onap.aai.util.genxsd.YAMLfromOXM; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +@Configuration +public class SwaggerGenerationConfiguration { + + @Value("${schema.uri.base.path}") + private String basePath; + + @Bean + @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public NodesYAMLfromOXM nodesYamlFromOXM(SchemaVersions schemaVersions, NodeIngestor nodeIngestor, EdgeIngestor edgeIngestor) { + NodesYAMLfromOXM nodesYamlFromOXM = new NodesYAMLfromOXM(basePath, schemaVersions, nodeIngestor, edgeIngestor); + return nodesYamlFromOXM; + } + + @Bean + @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public HTMLfromOXM htmlFromOXM(SchemaVersions schemaVersions, NodeIngestor nodeIngestor, EdgeIngestor edgeIngestor) { + HTMLfromOXM htmlFromOXM = new HTMLfromOXM(schemaVersions, nodeIngestor, edgeIngestor); + return htmlFromOXM; + } + + @Bean + @Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public YAMLfromOXM yamlFromOXM(SchemaVersions schemaVersions, NodeIngestor nodeIngestor, EdgeIngestor edgeIngestor) { + YAMLfromOXM yamlFromOXM = new YAMLfromOXM(basePath, schemaVersions, nodeIngestor, edgeIngestor); + return yamlFromOXM; + } + +} diff --git a/aai-core/src/main/java/org/onap/aai/db/props/AAIProperties.java b/aai-core/src/main/java/org/onap/aai/db/props/AAIProperties.java index e1de3ff6..47e79a39 100644 --- a/aai-core/src/main/java/org/onap/aai/db/props/AAIProperties.java +++ b/aai-core/src/main/java/org/onap/aai/db/props/AAIProperties.java @@ -19,8 +19,6 @@ */ package org.onap.aai.db.props; -import org.onap.aai.introspection.Version; - public class AAIProperties { public static final String NODE_TYPE = "aai-node-type"; public static final String LAST_MOD_SOURCE_OF_TRUTH = "last-mod-source-of-truth"; @@ -30,7 +28,6 @@ public class AAIProperties { public static final String CREATED_TS = "aai-created-ts"; public static final String RESOURCE_VERSION = "resource-version"; public static final String AAI_URI = "aai-uri"; - public static final Version LATEST = Version.v13; public static final Integer MAXIMUM_DEPTH = 10000; public static final String LINKED = "linked"; public static final String DB_ALIAS_SUFFIX = "-local"; diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/AuditDoc.java b/aai-core/src/main/java/org/onap/aai/db/schema/AuditDoc.java deleted file mode 100644 index 078f0634..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/AuditDoc.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - -import java.util.List; - -import org.codehaus.jackson.annotate.JsonProperty; - -public class AuditDoc { - - private List<DBProperty> properties; - private List<DBIndex> indexes; - private List<EdgeProperty> edgeLabels; - - /** - * Gets the properties. - * - * @return the properties - */ - public List<DBProperty> getProperties() { - return properties; - } - - /** - * Sets the properties. - * - * @param properties the new properties - */ - public void setProperties(List<DBProperty> properties) { - this.properties = properties; - } - - /** - * Gets the indexes. - * - * @return the indexes - */ - public List<DBIndex> getIndexes() { - return indexes; - } - - /** - * Sets the indexes. - * - * @param indexes the new indexes - */ - public void setIndexes(List<DBIndex> indexes) { - this.indexes = indexes; - } - - /** - * Gets the edge labels. - * - * @return the edge labels - */ - @JsonProperty("edge-labels") - public List<EdgeProperty> getEdgeLabels() { - return edgeLabels; - } - - /** - * Sets the edge labels. - * - * @param edgeLabels the new edge labels - */ - public void setEdgeLabels(List<EdgeProperty> edgeLabels) { - this.edgeLabels = edgeLabels; - } - - -} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/AuditJanusGraph.java b/aai-core/src/main/java/org/onap/aai/db/schema/AuditJanusGraph.java deleted file mode 100644 index e8ac6ae0..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/AuditJanusGraph.java +++ /dev/null @@ -1,122 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - - -import java.util.Iterator; -import java.util.LinkedHashSet; - -import org.apache.tinkerpop.gremlin.structure.Vertex; - -import org.janusgraph.core.EdgeLabel; -import org.janusgraph.core.PropertyKey; -import org.janusgraph.core.JanusGraph; -import org.janusgraph.core.schema.JanusGraphIndex; -import org.janusgraph.core.schema.JanusGraphManagement; - -public class AuditJanusGraph extends Auditor { - - private final JanusGraph graph; - - /** - * Instantiates a new audit JanusGraph. - * - * @param g the g - */ - public AuditJanusGraph (JanusGraph g) { - this.graph = g; - buildSchema(); - } - - /** - * Builds the schema. - */ - private void buildSchema() { - populateProperties(); - populateIndexes(); - populateEdgeLabels(); - } - - /** - * Populate properties. - */ - private void populateProperties() { - JanusGraphManagement mgmt = graph.openManagement(); - Iterable<PropertyKey> iterable = mgmt.getRelationTypes(PropertyKey.class); - Iterator<PropertyKey> JanusGraphProperties = iterable.iterator(); - PropertyKey propKey; - while (JanusGraphProperties.hasNext()) { - propKey = JanusGraphProperties.next(); - DBProperty prop = new DBProperty(); - - prop.setName(propKey.name()); - prop.setCardinality(propKey.cardinality()); - prop.setTypeClass(propKey.dataType()); - - this.properties.put(prop.getName(), prop); - } - } - - /** - * Populate indexes. - */ - private void populateIndexes() { - JanusGraphManagement mgmt = graph.openManagement(); - Iterable<JanusGraphIndex> iterable = mgmt.getGraphIndexes(Vertex.class); - Iterator<JanusGraphIndex> JanusGraphIndexes = iterable.iterator(); - JanusGraphIndex JanusGraphIndex; - while (JanusGraphIndexes.hasNext()) { - JanusGraphIndex = JanusGraphIndexes.next(); - if (JanusGraphIndex.isCompositeIndex()) { - DBIndex index = new DBIndex(); - LinkedHashSet<DBProperty> dbProperties = new LinkedHashSet<>(); - index.setName(JanusGraphIndex.name()); - index.setUnique(JanusGraphIndex.isUnique()); - PropertyKey[] keys = JanusGraphIndex.getFieldKeys(); - for (PropertyKey key : keys) { - dbProperties.add(this.properties.get(key.name())); - } - index.setProperties(dbProperties); - index.setStatus(JanusGraphIndex.getIndexStatus(keys[0])); - this.indexes.put(index.getName(), index); - } - } - } - - /** - * Populate edge labels. - */ - private void populateEdgeLabels() { - JanusGraphManagement mgmt = graph.openManagement(); - Iterable<EdgeLabel> iterable = mgmt.getRelationTypes(EdgeLabel.class); - Iterator<EdgeLabel> JanusGraphEdgeLabels = iterable.iterator(); - EdgeLabel edgeLabel; - while (JanusGraphEdgeLabels.hasNext()) { - edgeLabel = JanusGraphEdgeLabels.next(); - EdgeProperty edgeProperty = new EdgeProperty(); - - edgeProperty.setName(edgeLabel.name()); - edgeProperty.setMultiplicity(edgeLabel.multiplicity()); - - this.edgeLabels.put(edgeProperty.getName(), edgeProperty); - } - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/AuditOXM.java b/aai-core/src/main/java/org/onap/aai/db/schema/AuditOXM.java deleted file mode 100644 index 167b26d0..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/AuditOXM.java +++ /dev/null @@ -1,242 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -import javax.xml.XMLConstants; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.onap.aai.serialization.db.EdgeRule; -import org.onap.aai.serialization.db.EdgeRules; -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import org.onap.aai.db.props.AAIProperties; -import org.onap.aai.introspection.Introspector; -import org.onap.aai.introspection.Loader; -import org.onap.aai.introspection.LoaderFactory; -import org.onap.aai.introspection.ModelType; -import org.onap.aai.introspection.Version; -import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; -import org.onap.aai.logging.LogFormatTools; -import org.onap.aai.schema.enums.ObjectMetadata; -import org.onap.aai.util.AAIConstants; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.google.common.collect.Multimap; -import org.janusgraph.core.Cardinality; -import org.janusgraph.core.Multiplicity; -import org.janusgraph.core.schema.SchemaStatus; - -public class AuditOXM extends Auditor { - - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AuditOXM.class); - - private Set<Introspector> allObjects; - - /** - * Instantiates a new audit OXM. - * - * @param version the version - */ - public AuditOXM(Version version) { - Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version); - Set<String> objectNames = getAllObjects(version); - allObjects = new HashSet<>(); - for (String key : objectNames) { - try { - final Introspector temp = loader.introspectorFromName(key); - allObjects.add(temp); - this.createDBProperties(temp); - } catch (AAIUnknownObjectException e) { - LOGGER.warn("Skipping audit for object " + key + " (Unknown Object) " + LogFormatTools.getStackTop(e)); - } - } - for (Introspector temp : allObjects) { - this.createDBIndexes(temp); - } - createEdgeLabels(); - - } - - /** - * Gets the all objects. - * - * @param version the version - * @return the all objects - */ - private Set<String> getAllObjects(Version version) { - String fileName = AAIConstants.AAI_HOME_ETC_OXM + "aai_oxm_" + version.toString() + ".xml"; - Set<String> result = new HashSet<>(); - DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - try { - docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); - Document doc = docBuilder.parse(fileName); - NodeList list = doc.getElementsByTagName("java-type"); - for (int i = 0; i < list.getLength(); i++) { - result.add(list.item(i).getAttributes().getNamedItem("name").getNodeValue()); - } - } catch (ParserConfigurationException | SAXException | IOException e) { - LOGGER.error(e.getMessage()); - } - - result.remove("EdgePropNames"); - return result; - - } - - /** - * Creates the DB properties. - * - * @param temp the temp - */ - private void createDBProperties(Introspector temp) { - Set<String> objectProperties = temp.getProperties(); - - for (String prop : objectProperties) { - if (!properties.containsKey(prop)) { - DBProperty dbProperty = new DBProperty(); - dbProperty.setName(prop); - if (temp.isListType(prop)) { - dbProperty.setCardinality(Cardinality.SET); - if (temp.isSimpleGenericType(prop)) { - Class<?> clazz = null; - try { - clazz = Class.forName(temp.getGenericType(prop)); - } catch (ClassNotFoundException e) { - clazz = Object.class; - } - dbProperty.setTypeClass(clazz); - properties.put(prop, dbProperty); - } - } else { - dbProperty.setCardinality(Cardinality.SINGLE); - if (temp.isSimpleType(prop)) { - Class<?> clazz = null; - try { - clazz = Class.forName(temp.getType(prop)); - } catch (ClassNotFoundException e) { - clazz = Object.class; - } - dbProperty.setTypeClass(clazz); - properties.put(prop, dbProperty); - } - } - } - } - - } - - /** - * Creates the DB indexes. - * - * @param temp the temp - */ - private void createDBIndexes(Introspector temp) { - String uniqueProps = temp.getMetadata(ObjectMetadata.UNIQUE_PROPS); - String namespace = temp.getMetadata(ObjectMetadata.NAMESPACE); - if (uniqueProps == null) { - uniqueProps = ""; - } - if (namespace == null) { - namespace = ""; - } - boolean isTopLevel = namespace != ""; - List<String> unique = Arrays.asList(uniqueProps.split(",")); - Set<String> indexed = temp.getIndexedProperties(); - Set<String> keys = temp.getKeys(); - - for (String prop : indexed) { - DBIndex dbIndex = new DBIndex(); - LinkedHashSet<DBProperty> properties = new LinkedHashSet<>(); - if (!this.indexes.containsKey(prop)) { - dbIndex.setName(prop); - dbIndex.setUnique(unique.contains(prop)); - properties.add(this.properties.get(prop)); - dbIndex.setProperties(properties); - dbIndex.setStatus(SchemaStatus.ENABLED); - this.indexes.put(prop, dbIndex); - } - } - if (keys.size() > 1 || isTopLevel) { - DBIndex dbIndex = new DBIndex(); - LinkedHashSet<DBProperty> properties = new LinkedHashSet<>(); - dbIndex.setName("key-for-" + temp.getDbName()); - if (!this.indexes.containsKey(dbIndex.getName())) { - boolean isUnique = false; - if (isTopLevel) { - properties.add(this.properties.get(AAIProperties.NODE_TYPE)); - } - for (String key : keys) { - properties.add(this.properties.get(key)); - - if (unique.contains(key) && !isUnique) { - isUnique = true; - } - } - dbIndex.setUnique(isUnique); - dbIndex.setProperties(properties); - dbIndex.setStatus(SchemaStatus.ENABLED); - this.indexes.put(dbIndex.getName(), dbIndex); - } - } - - } - - /** - * Creates the edge labels. - */ - private void createEdgeLabels() { - Multimap<String, EdgeRule> edgeRules = EdgeRules.getInstance().getAllRules(); - for (String key : edgeRules.keySet()) { - Collection<EdgeRule> collection = edgeRules.get(key); - EdgeProperty prop = new EdgeProperty(); - //there is only ever one, they used the wrong type for EdgeRules - String label = ""; - for (EdgeRule item : collection) { - label = item.getLabel(); - } - prop.setName(label); - prop.setMultiplicity(Multiplicity.MULTI); - this.edgeLabels.put(label, prop); - } - } - - /** - * Gets the all introspectors. - * - * @return the all introspectors - */ - public Set<Introspector> getAllIntrospectors() { - return this.allObjects; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/Auditor.java b/aai-core/src/main/java/org/onap/aai/db/schema/Auditor.java deleted file mode 100644 index 5dc8c6cc..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/Auditor.java +++ /dev/null @@ -1,53 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - -import java.util.*; - -public abstract class Auditor { - - protected Map<String, DBProperty> properties = new HashMap<>(); - protected Map<String, DBIndex> indexes = new HashMap<>(); - protected Map<String, EdgeProperty> edgeLabels = new HashMap<>(); - - /** - * Gets the audit doc. - * - * @return the audit doc - */ - public AuditDoc getAuditDoc() { - AuditDoc doc = new AuditDoc(); - List<DBProperty> propertyList = new ArrayList<>(); - List<DBIndex> indexList = new ArrayList<>(); - List<EdgeProperty> edgeLabelList = new ArrayList<>(); - propertyList.addAll(this.properties.values()); - indexList.addAll(this.indexes.values()); - edgeLabelList.addAll(this.edgeLabels.values()); - Collections.sort(propertyList, new CompareByName()); - Collections.sort(indexList, new CompareByName()); - Collections.sort(edgeLabelList, new CompareByName()); - - doc.setProperties(propertyList); - doc.setIndexes(indexList); - doc.setEdgeLabels(edgeLabelList); - - return doc; - } -} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/DBIndex.java b/aai-core/src/main/java/org/onap/aai/db/schema/DBIndex.java deleted file mode 100644 index 9fd0eda0..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/DBIndex.java +++ /dev/null @@ -1,104 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - -import java.util.LinkedHashSet; -import java.util.Set; - -import org.janusgraph.core.schema.SchemaStatus; - -public class DBIndex implements Named { - - private String name = null; - private boolean unique = false; - private LinkedHashSet<DBProperty> properties = new LinkedHashSet<>(); - private SchemaStatus status = null; - - /** - * Gets the name - */ - public String getName() { - return name; - } - - /** - * Sets the name. - * - * @param name the new name - */ - public void setName(String name) { - this.name = name; - } - - /** - * Checks if is unique. - * - * @return true, if is unique - */ - public boolean isUnique() { - return unique; - } - - /** - * Sets the unique. - * - * @param unique the new unique - */ - public void setUnique(boolean unique) { - this.unique = unique; - } - - /** - * Gets the properties. - * - * @return the properties - */ - public Set<DBProperty> getProperties() { - return properties; - } - - /** - * Sets the properties. - * - * @param properties the new properties - */ - public void setProperties(LinkedHashSet<DBProperty> properties) { - this.properties = properties; - } - - /** - * Gets the status. - * - * @return the status - */ - public SchemaStatus getStatus() { - return status; - } - - /** - * Sets the status. - * - * @param status the new status - */ - public void setStatus(SchemaStatus status) { - this.status = status; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/DBProperty.java b/aai-core/src/main/java/org/onap/aai/db/schema/DBProperty.java deleted file mode 100644 index fd19f72d..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/DBProperty.java +++ /dev/null @@ -1,84 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - -import org.onap.aai.introspection.Introspector; -import org.janusgraph.core.Cardinality; - -public class DBProperty implements Named { - - - private String name = null; - private Cardinality cardinality = null; - private Class<?> typeClass = null; - - /** - * Gets the name - */ - public String getName() { - return name; - } - - /** - * Sets the name. - * - * @param name the new name - */ - public void setName(String name) { - this.name = name; - } - - /** - * Gets the cardinality. - * - * @return the cardinality - */ - public Cardinality getCardinality() { - return cardinality; - } - - /** - * Sets the cardinality. - * - * @param cardinality the new cardinality - */ - public void setCardinality(Cardinality cardinality) { - this.cardinality = cardinality; - } - - /** - * Gets the type class. - * - * @return the type class - */ - public Class<?> getTypeClass() { - return typeClass; - } - - /** - * Sets the type class. - * - * @param type the new type class - */ - public void setTypeClass(Class<?> type) { - this.typeClass = type; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/ManageJanusGraphSchema.java b/aai-core/src/main/java/org/onap/aai/db/schema/ManageJanusGraphSchema.java deleted file mode 100644 index c4061b3f..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/ManageJanusGraphSchema.java +++ /dev/null @@ -1,277 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.apache.tinkerpop.gremlin.structure.Vertex; - -import org.onap.aai.introspection.Version; -import org.janusgraph.core.PropertyKey; -import org.janusgraph.core.JanusGraph; -import org.janusgraph.core.schema.SchemaStatus; -import org.janusgraph.core.schema.JanusGraphIndex; -import org.janusgraph.core.schema.JanusGraphManagement; -import org.janusgraph.core.schema.JanusGraphManagement.IndexBuilder; - -public class ManageJanusGraphSchema { - - private static final EELFLogger logger = EELFManager.getInstance().getLogger(ManageJanusGraphSchema.class); - - private JanusGraphManagement graphMgmt; - private JanusGraph graph; - private List<DBProperty> aaiProperties; - private List<DBIndex> aaiIndexes; - private List<EdgeProperty> aaiEdgeProperties; - private Auditor oxmInfo = null; - private Auditor graphInfo = null; - - /** - * Instantiates a new manage JanusGraph schema. - * - * @param graph the graph - */ - public ManageJanusGraphSchema(final JanusGraph graph) { - this.graph = graph; - oxmInfo = AuditorFactory.getOXMAuditor(Version.v8); - graphInfo = AuditorFactory.getGraphAuditor(graph); - } - - - /** - * Builds the schema. - */ - public void buildSchema() { - - this.graphMgmt = graph.openManagement(); - aaiProperties = new ArrayList<>(); - aaiEdgeProperties = new ArrayList<>(); - aaiIndexes = new ArrayList<>(); - aaiProperties.addAll(oxmInfo.getAuditDoc().getProperties()); - aaiIndexes.addAll(oxmInfo.getAuditDoc().getIndexes()); - aaiEdgeProperties.addAll(oxmInfo.getAuditDoc().getEdgeLabels()); - try { - createPropertyKeys(); - createIndexes(); - createEdgeLabels(); - } catch (Exception e) { - logger.error(e.getMessage(),e); - graphMgmt.rollback(); - } - graphMgmt.commit(); - } - - /** - * Creates the property keys. - */ - private void createPropertyKeys() { - - for (DBProperty prop : aaiProperties) { - this.createProperty(graphMgmt, prop); - } - - } - - /** - * Creates the indexes. - */ - private void createIndexes() { - - for (DBIndex index : aaiIndexes) { - Set<DBProperty> props = index.getProperties(); - boolean isChanged = false; - boolean isNew = false; - List<PropertyKey> keyList = new ArrayList<>(); - for (DBProperty prop : props) { - keyList.add(graphMgmt.getPropertyKey(prop.getName())); - } - if (graphMgmt.containsGraphIndex(index.getName())) { - JanusGraphIndex janusGraphIndex = graphMgmt.getGraphIndex(index.getName()); - PropertyKey[] dbKeys = janusGraphIndex.getFieldKeys(); - if (dbKeys.length != keyList.size()) { - isChanged = true; - } else { - int i = 0; - for (PropertyKey key : keyList) { - if (!dbKeys[i].equals(key)) { - isChanged = true; - break; - } - i++; - } - } - } else { - isNew = true; - } - if (!keyList.isEmpty()) { - this.createIndex(graphMgmt, index.getName(), keyList, index.isUnique(), isNew, isChanged); - } - } - } - - // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is - // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One" - // We use the same edge-label for edges between many different types of nodes and our internal - // multiplicty definitions depends on which two types of nodes are being connected. - /** - * Creates the edge labels. - */ - private void createEdgeLabels() { - - - for (EdgeProperty prop : aaiEdgeProperties) { - - if (graphMgmt.containsEdgeLabel(prop.getName())) { - // see what changed - } else { - graphMgmt.makeEdgeLabel(prop.getName()).multiplicity(prop.getMultiplicity()).make(); - } - - } - - - } - - /** - * Creates the property. - * - * @param mgmt the mgmt - * @param prop the prop - */ - private void createProperty(JanusGraphManagement mgmt, DBProperty prop) { - if (mgmt.containsPropertyKey(prop.getName())) { - PropertyKey key = mgmt.getPropertyKey(prop.getName()); - boolean isChanged = false; - if (!prop.getCardinality().equals(key.cardinality())) { - isChanged = true; - } - if (!prop.getTypeClass().equals(key.dataType())) { - isChanged = true; - } - if (isChanged) { - //must modify! - this.replaceProperty(prop); - } - } else { - //create a new property key - logger.info("Key: " + prop.getName() + " not found - adding"); - mgmt.makePropertyKey(prop.getName()).dataType(prop.getTypeClass()).cardinality(prop.getCardinality()).make(); - } - } - - /** - * Creates the index. - * - * @param mgmt the mgmt - * @param indexName the index name - * @param keys the keys - * @param isUnique the is unique - * @param isNew the is new - * @param isChanged the is changed - */ - private void createIndex(JanusGraphManagement mgmt, String indexName, List<PropertyKey> keys, boolean isUnique, boolean isNew, boolean isChanged) { - - if (isNew || isChanged) { - - if (isNew) { - IndexBuilder builder = mgmt.buildIndex(indexName,Vertex.class); - for (PropertyKey k : keys) { - builder.addKey(k); - } - if (isUnique) { - builder.unique(); - } - builder.buildCompositeIndex(); - logger.info("Built index for " + indexName + " with keys: " + keys); - } - } - } - - /** - * Wait for completion. - * - * @param name the name - * @throws InterruptedException the interrupted exception - */ - private void waitForCompletion(String name) throws InterruptedException { - - boolean registered = false; - long before = System.currentTimeMillis(); - while (!registered) { - Thread.sleep(500L); - JanusGraphManagement mgmt = graph.openManagement(); - JanusGraphIndex idx = mgmt.getGraphIndex(name); - registered = true; - for (PropertyKey k : idx.getFieldKeys()) { - SchemaStatus s = idx.getIndexStatus(k); - registered &= s.equals(SchemaStatus.REGISTERED); - } - mgmt.rollback(); - } - logger.info("Index REGISTERED in " + (System.currentTimeMillis() - before) + " ms"); - } - - /** - * Replace property. - * - * @param key the key - */ - private void replaceProperty(DBProperty key) { - - } - - /** - * Update index. - * - * @param index the index - */ - public void updateIndex(DBIndex index) { - - JanusGraphManagement mgmt = graph.openManagement(); - List<PropertyKey> keys = new ArrayList<>(); - boolean isNew = false; - boolean isChanged = false; - for (DBProperty prop : index.getProperties()) { - createProperty(mgmt, prop); - keys.add(mgmt.getPropertyKey(prop.getName())); - } - if (mgmt.containsGraphIndex(index.getName())) { - logger.info("index already exists"); - isNew = false; - isChanged = true; - } else { - isNew = true; - isChanged = false; - } - this.createIndex(mgmt, index.getName(), keys, index.isUnique(), isNew, isChanged); - - mgmt.commit(); - - } - - - - - -} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/Named.java b/aai-core/src/main/java/org/onap/aai/db/schema/Named.java deleted file mode 100644 index f12699b6..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/Named.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - -public interface Named { - - /** - * Gets the name. - * - * @return the name - */ - public String getName(); -} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/ScriptDriver.java b/aai-core/src/main/java/org/onap/aai/db/schema/ScriptDriver.java deleted file mode 100644 index d6e32d2d..00000000 --- a/aai-core/src/main/java/org/onap/aai/db/schema/ScriptDriver.java +++ /dev/null @@ -1,106 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.db.schema; - -import java.io.IOException; -import java.util.UUID; - -import org.apache.commons.configuration.ConfigurationException; -import org.codehaus.jackson.JsonGenerationException; -import org.codehaus.jackson.map.JsonMappingException; -import org.codehaus.jackson.map.ObjectMapper; -import org.onap.aai.dbmap.AAIGraphConfig; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Version; -import org.onap.aai.logging.LoggingContext; -import org.onap.aai.logging.LoggingContext.StatusCode; -import org.onap.aai.util.AAIConfig; -import com.beust.jcommander.JCommander; -import com.beust.jcommander.Parameter; -import org.janusgraph.core.JanusGraphFactory; -import org.janusgraph.core.JanusGraph; - -public class ScriptDriver { - - - /** - * The main method. - * - * @param args the arguments - * @throws AAIException the AAI exception - * @throws JsonGenerationException the json generation exception - * @throws JsonMappingException the json mapping exception - * @throws IOException Signals that an I/O exception has occurred. - */ - public static void main (String[] args) throws AAIException, IOException, ConfigurationException { - CommandLineArgs cArgs = new CommandLineArgs(); - - LoggingContext.init(); - LoggingContext.component("DBSchemaScriptDriver"); - LoggingContext.partnerName("NA"); - LoggingContext.targetEntity("AAI"); - LoggingContext.requestId(UUID.randomUUID().toString()); - LoggingContext.serviceName("AAI"); - LoggingContext.targetServiceName("main"); - LoggingContext.statusCode(StatusCode.COMPLETE); - LoggingContext.responseCode(LoggingContext.SUCCESS); - - new JCommander(cArgs, args); - - if (cArgs.help) { - System.out.println("-c [path to graph configuration] -type [what you want to audit - oxm or graph]"); - } - String config = cArgs.config; - AAIConfig.init(); - try (JanusGraph graph = JanusGraphFactory.open(new AAIGraphConfig.Builder(config).forService(ScriptDriver.class.getSimpleName()).withGraphType("NA").buildConfiguration())) { - if (!("oxm".equals(cArgs.type) || "graph".equals(cArgs.type))) { - System.out.println("type: " + cArgs.type + " not recognized."); - System.exit(1); - } - - AuditDoc doc = null; - if ("oxm".equals(cArgs.type)) { - doc = AuditorFactory.getOXMAuditor(Version.v8).getAuditDoc(); - } else if ("graph".equals(cArgs.type)) { - doc = AuditorFactory.getGraphAuditor(graph).getAuditDoc(); - } - - ObjectMapper mapper = new ObjectMapper(); - - String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(doc); - System.out.println(json); - } - } - -} - -class CommandLineArgs { - - @Parameter(names = "--help", description = "Help") - public boolean help = false; - - @Parameter(names = "-c", description = "Configuration", required=true) - public String config; - - @Parameter(names = "-type", description = "Type", required=true) - public String type = "graph"; - - -} diff --git a/aai-core/src/main/java/org/onap/aai/dbgen/DataGrooming.java b/aai-core/src/main/java/org/onap/aai/dbgen/DataGrooming.java deleted file mode 100644 index d46aa7ae..00000000 --- a/aai-core/src/main/java/org/onap/aai/dbgen/DataGrooming.java +++ /dev/null @@ -1,2645 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.dbgen; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Properties; -import java.util.Set; -import java.util.UUID; - -import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; -import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; -import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; -import org.apache.tinkerpop.gremlin.structure.Direction; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.apache.tinkerpop.gremlin.structure.Graph; -import org.apache.tinkerpop.gremlin.structure.Property; -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.apache.tinkerpop.gremlin.structure.VertexProperty; -import org.onap.aai.db.props.AAIProperties; -import org.onap.aai.dbmap.AAIGraph; -import org.onap.aai.dbmap.AAIGraphConfig; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Introspector; -import org.onap.aai.introspection.Loader; -import org.onap.aai.introspection.LoaderFactory; -import org.onap.aai.introspection.ModelType; -import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; -import org.onap.aai.logging.ErrorLogHelper; -import org.onap.aai.logging.LogFormatTools; -import org.onap.aai.logging.LoggingContext; -import org.onap.aai.serialization.db.AAIDirection; -import org.onap.aai.serialization.db.EdgeProperty; -import org.onap.aai.util.*; -import org.onap.aai.logging.LoggingContext.StatusCode; - -import com.att.eelf.configuration.Configuration; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.janusgraph.core.JanusGraphFactory; -import org.janusgraph.core.JanusGraph; - - -public class DataGrooming { - - public static final String AAI_EXCEPTION_NUMBER = "AAI_6101"; - private static EELFLogger logger; - private static final String FROMAPPID = "AAI-DB"; - private static final String TRANSID = UUID.randomUUID().toString(); - private static int dupeGrpsDeleted = 0; - private static final String AAI_NODE_TYPE = "aai-node-type"; - private static final String KEEP_VID_UNDETERMINED ="KeepVid=UNDETERMINED"; - - /** - * The main method. - * - * @param args the arguments - */ - public static void main(String[] args) { - - // Set the logging file properties to be used by EELFManager - System.setProperty("aai.service.name", DataGrooming.class.getSimpleName()); - Properties props = System.getProperties(); - props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_DATA_GROOMING_LOGBACK_PROPS); - props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); - logger = EELFManager.getInstance().getLogger(DataGrooming.class); - String ver = "version"; // Placeholder - Boolean doAutoFix = false; - Boolean edgesOnlyFlag = false; - Boolean dontFixOrphansFlag = false; - Boolean skipHostCheck = false; - Boolean singleCommits = false; - Boolean dupeCheckOff = false; - Boolean dupeFixOn = false; - Boolean ghost2CheckOff = false; - Boolean ghost2FixOn = false; - Boolean neverUseCache = false; - Boolean skipEdgeCheckFlag = false; - - LoggingContext.init(); - LoggingContext.partnerName(FROMAPPID); - LoggingContext.serviceName(AAIConstants.AAI_RESOURCES_MS); - LoggingContext.component("dataGrooming"); - LoggingContext.targetEntity(AAIConstants.AAI_RESOURCES_MS); - LoggingContext.targetServiceName("main"); - LoggingContext.requestId(TRANSID); - LoggingContext.statusCode(StatusCode.COMPLETE); - LoggingContext.responseCode(LoggingContext.SUCCESS); - - int timeWindowMinutes = 0; // A value of 0 means that we will not have a time-window -- we will look - // at all nodes of the passed-in nodeType. - - - int maxRecordsToFix = AAIConstants.AAI_GROOMING_DEFAULT_MAX_FIX; - int sleepMinutes = AAIConstants.AAI_GROOMING_DEFAULT_SLEEP_MINUTES; - try { - String maxFixStr = AAIConfig.get("aai.grooming.default.max.fix"); - if( maxFixStr != null && !maxFixStr.isEmpty() ){ - maxRecordsToFix = Integer.parseInt(maxFixStr); - } - String sleepStr = AAIConfig.get("aai.grooming.default.sleep.minutes"); - if( sleepStr != null && !sleepStr.isEmpty() ){ - sleepMinutes = Integer.parseInt(sleepStr); - } - } - catch ( Exception e ){ - // Don't worry, we'll just use the defaults that we got from AAIConstants - logger.warn("WARNING - could not pick up aai.grooming values from aaiconfig.properties file. "); - } - - String prevFileName = ""; - dupeGrpsDeleted = 0; - FormatDate fd = new FormatDate("yyyyMMddHHmm", "GMT"); - String dteStr = fd.getDateTime(); - - if (args.length > 0) { - // They passed some arguments in that will affect processing - - List<String> arguments = Arrays.asList(args); - Iterator<String> argsIterator = arguments.iterator(); - while (argsIterator.hasNext()){ - String argument = argsIterator.next(); - String nextArg; - switch(argument){ - case "-edgesOnly": - edgesOnlyFlag = true; - break; - case "-autoFix": - doAutoFix = true; - break; - case "-skipHostCheck": - skipHostCheck = true; - break; - case "-dontFixOrphans": - dontFixOrphansFlag = true; - break; - case "-singleCommits": - singleCommits = true; - break; - case "-dupeCheckOff": - dupeCheckOff = true; - break; - case "-dupeFixOn": - dupeFixOn = true; - break; - case "-ghost2CheckOff": - ghost2CheckOff = true; - break; - case "-neverUseCache": - neverUseCache = true; - break; - case "-ghost2FixOn": - ghost2FixOn = true; - break; - case "-skipEdgeChecks": - skipEdgeCheckFlag = true; - break; - case "-maxFix": - handleNoPassedArgument(argsIterator, argument); - nextArg = argsIterator.next(); - maxRecordsToFix = handleNumericArgument(maxRecordsToFix, nextArg, argument); - break; - case "-sleepMinutes": - handleNoPassedArgument(argsIterator, argument); - nextArg = argsIterator.next(); - sleepMinutes = handleNumericArgument(sleepMinutes, nextArg, argument); - break; - case "-timeWindowMinutes": - handleNoPassedArgument(argsIterator, argument); - nextArg = argsIterator.next(); - timeWindowMinutes = handleNumericArgument(timeWindowMinutes, nextArg, argument); - break; - case "-f": - handleNoPassedArgument(argsIterator, argument); - prevFileName = argsIterator.next(); - break; - default: - handleUnrecognizedArguments(argument); - } - } - } - - String windowTag = "FULL"; - if( timeWindowMinutes > 0 ){ - windowTag = "PARTIAL"; - } - String groomOutFileName = "dataGrooming." + windowTag + "." + dteStr + ".out"; - - try { - LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST); - - } - catch (Exception ex){ - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error("ERROR - Could not create loader " + LogFormatTools.getStackTop(ex)); - AAISystemExitUtil.systemExitCloseAAIGraph(1); - } - - if (skipHostCheck) { - logger.info(" We will skip the HostCheck as requested. "); - } - - try { - if (!prevFileName.isEmpty()) { - // They are trying to fix some data based on a data in a - // previous file. - logger.info(" Call doTheGrooming() with a previous fileName [" - + prevFileName + "] for cleanup. "); - Boolean finalShutdownFlag = true; - Boolean cacheDbOkFlag = false; - doTheGrooming(prevFileName, edgesOnlyFlag, dontFixOrphansFlag, - maxRecordsToFix, groomOutFileName, ver, singleCommits, - dupeCheckOff, dupeFixOn, ghost2CheckOff, ghost2FixOn, - finalShutdownFlag, cacheDbOkFlag, - skipEdgeCheckFlag, timeWindowMinutes); - } else if (doAutoFix) { - // They want us to run the processing twice -- first to look for - // delete candidates, then after - // napping for a while, run it again and delete any candidates - // that were found by the first run. - // Note: we will produce a separate output file for each of the - // two runs. - logger.info(" Doing an auto-fix call to Grooming. "); - logger.info(" First, Call doTheGrooming() to look at what's out there. "); - Boolean finalShutdownFlag = false; - Boolean cacheDbOkFlag = true; - int fixCandCount = doTheGrooming("", edgesOnlyFlag, - dontFixOrphansFlag, maxRecordsToFix, groomOutFileName, - ver, singleCommits, dupeCheckOff, dupeFixOn, ghost2CheckOff, ghost2FixOn, - finalShutdownFlag, cacheDbOkFlag, - skipEdgeCheckFlag, timeWindowMinutes); - if (fixCandCount == 0) { - logger.info(" No fix-Candidates were found by the first pass, so no second/fix-pass is needed. "); - } else { - // We'll sleep a little and then run a fix-pass based on the - // first-run's output file. - try { - logger.info("About to sleep for " + sleepMinutes - + " minutes."); - int sleepMsec = sleepMinutes * 60 * 1000; - Thread.sleep(sleepMsec); - } catch (InterruptedException ie) { - logger.info("\n >>> Sleep Thread has been Interrupted <<< "); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - - dteStr = fd.getDateTime(); - String secondGroomOutFileName = "dataGrooming." + dteStr - + ".out"; - logger.info(" Now, call doTheGrooming() a second time and pass in the name of the file " - + "generated by the first pass for fixing: [" - + groomOutFileName + "]"); - finalShutdownFlag = true; - cacheDbOkFlag = false; - doTheGrooming(groomOutFileName, edgesOnlyFlag, - dontFixOrphansFlag, maxRecordsToFix, - secondGroomOutFileName, ver, singleCommits, - dupeCheckOff, dupeFixOn, ghost2CheckOff, ghost2FixOn, - finalShutdownFlag, cacheDbOkFlag, - skipEdgeCheckFlag, timeWindowMinutes); - } - } else { - // Do the grooming - plain vanilla (no fix-it-file, no - // auto-fixing) - Boolean finalShutdownFlag = true; - logger.info(" Call doTheGrooming() "); - Boolean cacheDbOkFlag = true; - if( neverUseCache ){ - // They have forbidden us from using a cached db connection. - cacheDbOkFlag = false; - } - doTheGrooming("", edgesOnlyFlag, dontFixOrphansFlag, - maxRecordsToFix, groomOutFileName, ver, singleCommits, - dupeCheckOff, dupeFixOn, ghost2CheckOff, ghost2FixOn, - finalShutdownFlag, cacheDbOkFlag, - skipEdgeCheckFlag, timeWindowMinutes); - } - } catch (Exception ex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("Exception while grooming data " + LogFormatTools.getStackTop(ex)); - } - - logger.info(" Done! "); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - - }// End of main() - - private static void handleUnrecognizedArguments(String argument) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error(" Unrecognized argument passed to DataGrooming: [" - + argument + "]. "); - logger.error( - " Valid values are: -f -autoFix -maxFix -edgesOnly -skipEdgeChecks -dupeFixOn -donFixOrphans -timeWindowMinutes -sleepMinutes -neverUseCache"); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - - private static int handleNumericArgument(int numericArgumentValue, String nextArg, String argument) { - try { - numericArgumentValue = Integer.parseInt(nextArg); - } catch (Exception e) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error("Bad value passed with" + argument + "option: [" - + nextArg + "]"); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - return numericArgumentValue; - } - - private static void handleNoPassedArgument(Iterator argsIterator, String argument) { - if (!argsIterator.hasNext()) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logger.error("No value passed with" + argument + "option."); - AAISystemExitUtil.systemExitCloseAAIGraph(0); - } - } - - /** - * Do the grooming. - * - * @param fileNameForFixing the file name for fixing - * @param edgesOnlyFlag the edges only flag - * @param dontFixOrphansFlag the dont fix orphans flag - * @param maxRecordsToFix the max records to fix - * @param groomOutFileName the groom out file name - * @param version the version - * @param singleCommits the single commits - * @param dupeCheckOff the dupe check off - * @param dupeFixOn the dupe fix on - * @param ghost2CheckOff the ghost 2 check off - * @param ghost2FixOn the ghost 2 fix on - * @param finalShutdownFlag the final shutdown flag - * @param cacheDbOkFlag the cacheDbOk flag - * @return the int - */ - private static int doTheGrooming(String fileNameForFixing, - Boolean edgesOnlyFlag, Boolean dontFixOrphansFlag, - int maxRecordsToFix, String groomOutFileName, String version, - Boolean singleCommits, - Boolean dupeCheckOff, Boolean dupeFixOn, - Boolean ghost2CheckOff, Boolean ghost2FixOn, - Boolean finalShutdownFlag, Boolean cacheDbOkFlag, - Boolean skipEdgeCheckFlag, int timeWindowMinutes) { - - logger.debug(" Entering doTheGrooming \n"); - - int cleanupCandidateCount = 0; - long windowStartTime = 0; // Translation of the window into a starting timestamp - BufferedWriter bw = null; - JanusGraph graph = null; - JanusGraph graph2 = null; - int deleteCount = 0; - boolean executeFinalCommit = false; - Set<String> deleteCandidateList = new LinkedHashSet<>(); - Set<String> processedVertices = new LinkedHashSet<>(); - Graph g = null; - Graph g2 = null; - try { - if( timeWindowMinutes > 0 ){ - // Translate the window value (ie. 30 minutes) into a unix timestamp like - // we use in the db - so we can select data created after that time. - windowStartTime = figureWindowStartTime( timeWindowMinutes ); - } - - AAIConfig.init(); - String targetDir = AAIConstants.AAI_HOME + AAIConstants.AAI_FILESEP - + "logs" + AAIConstants.AAI_FILESEP + "data" - + AAIConstants.AAI_FILESEP + "dataGrooming"; - - // Make sure the target directory exists - new File(targetDir).mkdirs(); - - if (!fileNameForFixing.isEmpty()) { - deleteCandidateList = getDeleteList(targetDir, - fileNameForFixing, edgesOnlyFlag, dontFixOrphansFlag, - dupeFixOn); - } - - if (deleteCandidateList.size() > maxRecordsToFix) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(" >> WARNING >> Delete candidate list size (" - + deleteCandidateList.size() - + ") is too big. The maxFix we are using is: " - + maxRecordsToFix - + ". No candidates will be deleted. "); - // Clear out the list so it won't be processed below. - deleteCandidateList = new LinkedHashSet<>(); - } - - String fullOutputFileName = targetDir + AAIConstants.AAI_FILESEP - + groomOutFileName; - File groomOutFile = new File(fullOutputFileName); - try { - groomOutFile.createNewFile(); - } catch (IOException e) { - String emsg = " Problem creating output file [" - + fullOutputFileName + "], exception=" + e.getMessage(); - throw new AAIException("AAI_6124", emsg); - } - - logger.info(" Will write to " + fullOutputFileName ); - bw = new BufferedWriter(new FileWriter(groomOutFile.getAbsoluteFile())); - ErrorLogHelper.loadProperties(); - - logger.info(" ---- NOTE --- about to open graph (takes a little while)--------\n"); - - if( cacheDbOkFlag ){ - // Since we're just reading (not deleting/fixing anything), we can use - // a cached connection to the DB - graph = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.CACHED_DB_CONFIG).forService(DataGrooming.class.getSimpleName()).withGraphType("cached").buildConfiguration()); - } - else { - graph = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG).forService(DataGrooming.class.getSimpleName()).withGraphType("realtime1").buildConfiguration()); - } - if (graph == null) { - String emsg = "null graph object in DataGrooming\n"; - throw new AAIException(AAI_EXCEPTION_NUMBER, emsg); - } - - logger.debug(" Got the graph object. "); - - g = graph.newTransaction(); - if (g == null) { - String emsg = "null graphTransaction object in DataGrooming\n"; - throw new AAIException(AAI_EXCEPTION_NUMBER, emsg); - } - GraphTraversalSource source1 = g.traversal(); - - ArrayList<String> errArr = new ArrayList<>(); - int totalNodeCount = 0; - HashMap<String, String> misMatchedHash = new HashMap<>(); - HashMap<String, Vertex> orphanNodeHash = new HashMap<>(); - HashMap<String, Vertex> missingDepNodeHash = new HashMap<>(); - HashMap<String, Edge> oneArmedEdgeHash = new HashMap<>(); - HashMap<String, String> emptyVertexHash = new HashMap<>(); - HashMap<String, Vertex> ghostNodeHash = new HashMap<>(); - ArrayList<String> dupeGroups = new ArrayList<>(); - - Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST); - - Set<Entry<String, Introspector>> entrySet = loader.getAllObjects().entrySet(); - String ntList = ""; - - logger.info(" Starting DataGrooming Processing "); - - if (edgesOnlyFlag) { - logger.info(" NOTE >> Skipping Node processing as requested. Will only process Edges. << "); - } - else { - for (Entry<String, Introspector> entry : entrySet) { - String nType = entry.getKey(); - int thisNtCount = 0; - int thisNtDeleteCount = 0; - - logger.debug(" > Look at : [" + nType + "] ..."); - ntList = ntList + "," + nType; - - // Get a collection of the names of the key properties for this nodeType to use later - // Determine what the key fields are for this nodeType - use an arrayList so they - // can be gotten out in a consistent order. - Set <String> keyPropsSet = entry.getValue().getKeys(); - ArrayList <String> keyProps = new ArrayList<>(); - keyProps.addAll(keyPropsSet); - - // Get the types of nodes that this nodetype depends on for uniqueness (if any) - Collection <String> depNodeTypes = loader.introspectorFromName(nType).getDependentOn(); - - // Loop through all the nodes of this Node type - int lastShownForNt = 0; - ArrayList <Vertex> tmpList = new ArrayList <> (); - Iterator <Vertex> iterv = source1.V().has(AAI_NODE_TYPE,nType); - while (iterv.hasNext()) { - // We put the nodes into an ArrayList because the graph.query iterator can time out - tmpList.add(iterv.next()); - } - - Iterator <Vertex> iter = tmpList.iterator(); - while (iter.hasNext()) { - try { - thisNtCount++; - if( thisNtCount == lastShownForNt + 250 ){ - lastShownForNt = thisNtCount; - logger.debug("count for " + nType + " so far = " + thisNtCount ); - } - Vertex thisVtx = iter.next(); - if( windowStartTime > 0 ){ - // They are using the time-window, so we only want nodes that are updated after a - // passed-in timestamp OR that have no last-modified-timestamp which means they are suspicious. - Object objModTimeStamp = thisVtx.property("aai-last-mod-ts").orElse(null); - if( objModTimeStamp != null ){ - long thisNodeModTime = (long)objModTimeStamp; - if( thisNodeModTime < windowStartTime ){ - // It has a last modified ts and is NOT in our window, so we can pass over it - continue; - } - } - } - - String thisVid = thisVtx.id().toString(); - if (processedVertices.contains(thisVid)) { - logger.debug("skipping already processed vertex: " + thisVid); - continue; - } - totalNodeCount++; - List <Vertex> secondGetList = new ArrayList <> (); - // ----------------------------------------------------------------------- - // For each vertex of this nodeType, we want to: - // a) make sure that it can be retrieved using it's AAI defined key - // b) make sure that it is not a duplicate - // ----------------------------------------------------------------------- - - // For this instance of this nodeType, get the key properties - HashMap<String, Object> propHashWithKeys = new HashMap<>(); - Iterator<String> keyPropI = keyProps.iterator(); - while (keyPropI.hasNext()) { - String propName = keyPropI.next(); - String propVal = ""; - //delete an already deleted vertex - Object obj = thisVtx.<Object>property(propName).orElse(null); - if (obj != null) { - propVal = obj.toString(); - } - propHashWithKeys.put(propName, propVal); - } - try { - // If this node is dependent on another for uniqueness, then do the query from that parent node - // Note - all of our nodes that are dependent on others for uniqueness are - // "children" of that node. - boolean depNodeOk = true; - if( depNodeTypes.isEmpty() ){ - // This kind of node is not dependent on any other. - // Make sure we can get it back using it's key properties (that is the - // phantom checking) and that we only get one. Note - we also need - // to collect data for a second type of dupe-checking which is done later. - secondGetList = getNodeJustUsingKeyParams( TRANSID, FROMAPPID, source1, nType, - propHashWithKeys, version ); - } - else { - // This kind of node is dependent on another for uniqueness. - // Start at it's parent (the dependent vertex) and make sure we can get it - // back using it's key properties and that we only get one. - Iterator <Vertex> vertI2 = source1.V(thisVtx).union(__.inE().has(EdgeProperty.CONTAINS.toString(), AAIDirection.OUT.toString()).outV(), __.outE().has(EdgeProperty.CONTAINS.toString(), AAIDirection.IN.toString()).inV()); - Vertex parentVtx = null; - int pCount = 0; - while( vertI2 != null && vertI2.hasNext() ){ - parentVtx = vertI2.next(); - pCount++; - } - if( pCount <= 0 ){ - - // It's Missing it's dependent/parent node - depNodeOk = false; - boolean zeroEdges = false; - try { - Iterator<Edge> tmpEdgeIter = thisVtx.edges(Direction.BOTH); - int edgeCount = 0; - while( tmpEdgeIter.hasNext() ){ - edgeCount++; - tmpEdgeIter.next(); - } - if( edgeCount == 0 ){ - zeroEdges = true; - } - } catch (Exception ex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn("WARNING from inside the for-each-vid-loop orphan-edges-check " + LogFormatTools.getStackTop(ex) ); - } - - if (deleteCandidateList.contains(thisVid)) { - boolean okFlag = true; - try { - processedVertices.add(thisVtx.id().toString()); - thisVtx.remove(); - deleteCount++; - thisNtDeleteCount++; - } catch (Exception e) { - okFlag = false; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("ERROR trying to delete missing-dep-node VID = " + thisVid + " " + LogFormatTools.getStackTop(e)); - } - if (okFlag) { - logger.info(" DELETED missing-dep-node VID = " + thisVid); - } - } else { - // We count nodes missing their depNodes two ways - the first if it has - // at least some edges, and the second if it has zero edges. Either - // way, they are effectively orphaned. - // NOTE - Only nodes that have dependent nodes are ever considered "orphaned". - if( zeroEdges ){ - missingDepNodeHash.put(thisVid, thisVtx); - } - else { - orphanNodeHash.put(thisVid, thisVtx); - } - } - } - else if ( pCount > 1 ){ - // Not sure how this could happen? Should we do something here? - depNodeOk = false; - } - else { - // We found the parent - so use it to do the second-look. - // NOTE --- We're just going to do the same check from the other direction - because - // there could be duplicates or the pointer going the other way could be broken - ArrayList <Vertex> tmpListSec = new ArrayList <> (); - - tmpListSec = getConnectedChildrenOfOneType( source1, parentVtx, nType ) ; - Iterator<Vertex> vIter = tmpListSec.iterator(); - while (vIter.hasNext()) { - Vertex tmpV = vIter.next(); - if( vertexHasTheseKeys(tmpV, propHashWithKeys) ){ - secondGetList.add(tmpV); - } - } - } - }// end of -- else this is a dependent node -- piece - - if( depNodeOk && (secondGetList == null || secondGetList.isEmpty()) ){ - // We could not get the node back using it's own key info. - // So, it's a PHANTOM - if (deleteCandidateList.contains(thisVid)) { - boolean okFlag = true; - try { - thisVtx.remove(); - deleteCount++; - thisNtDeleteCount++; - } catch (Exception e) { - okFlag = false; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("ERROR trying to delete phantom VID = " + thisVid + " " + LogFormatTools.getStackTop(e)); - } - if (okFlag) { - logger.info(" DELETED VID = " + thisVid); - } - } else { - ghostNodeHash.put(thisVid, thisVtx); - } - } - else if( (secondGetList.size() > 1) && depNodeOk && !dupeCheckOff ){ - // Found some DUPLICATES - need to process them - logger.info(" - now check Dupes for this guy - "); - List<String> tmpDupeGroups = checkAndProcessDupes( - TRANSID, FROMAPPID, g, source1, version, - nType, secondGetList, dupeFixOn, - deleteCandidateList, singleCommits, dupeGroups, loader); - Iterator<String> dIter = tmpDupeGroups.iterator(); - while (dIter.hasNext()) { - // Add in any newly found dupes to our running list - String tmpGrp = dIter.next(); - logger.info("Found set of dupes: [" + tmpGrp + "]"); - dupeGroups.add(tmpGrp); - } - } - } - catch (AAIException e1) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(" For nodeType = " + nType + " Caught exception", e1); - errArr.add(e1.getErrorObject().toString()); - } - catch (Exception e2) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(" For nodeType = " + nType - + " Caught exception", e2); - errArr.add(e2.getMessage()); - } - }// try block to enclose looping over each single vertex - catch (Exception exx) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn("WARNING from inside the while-verts-loop ", exx); - } - - } // while loop for each record of a nodeType - - if( depNodeTypes.isEmpty() && !dupeCheckOff ){ - // For this nodeType, we haven't looked at the possibility of a - // non-dependent node where two verts have same key info - ArrayList<ArrayList<Vertex>> nonDependentDupeSets = new ArrayList<ArrayList<Vertex>>(); - nonDependentDupeSets = getDupeSets4NonDepNodes( - TRANSID, FROMAPPID, g, - version, nType, tmpList, - keyProps, loader ); - // For each set found (each set is for a unique instance of key-values), - // process the dupes found - Iterator<ArrayList<Vertex>> dsItr = nonDependentDupeSets.iterator(); - while( dsItr.hasNext() ){ - ArrayList<Vertex> dupeList = dsItr.next(); - logger.info(" - now check Dupes for some non-dependent guys - "); - List<String> tmpDupeGroups = checkAndProcessDupes( - TRANSID, FROMAPPID, g, source1, version, - nType, dupeList, dupeFixOn, - deleteCandidateList, singleCommits, dupeGroups, loader); - Iterator<String> dIter = tmpDupeGroups.iterator(); - while (dIter.hasNext()) { - // Add in any newly found dupes to our running list - String tmpGrp = dIter.next(); - logger.info("Found set of dupes: [" + tmpGrp + "]"); - dupeGroups.add(tmpGrp); - } - } - - }// end of extra dupe check for non-dependent nodes - - if ( (thisNtDeleteCount > 0) && singleCommits ) { - // NOTE - the singleCommits option is not used in normal processing - g.tx().commit(); - g = AAIGraph.getInstance().getGraph().newTransaction(); - - } - thisNtDeleteCount = 0; - logger.info( " Processed " + thisNtCount + " records for [" + nType + "], " + totalNodeCount + " total overall. " ); - - }// While-loop for each node type - - }// end of check to make sure we weren't only supposed to do edges - - - if( !skipEdgeCheckFlag ){ - // -------------------------------------------------------------------------------------- - // Now, we're going to look for one-armed-edges. Ie. an edge that - // should have - // been deleted (because a vertex on one side was deleted) but - // somehow was not deleted. - // So the one end of it points to a vertexId -- but that vertex is - // empty. - // -------------------------------------------------------------------------------------- - - // To do some strange checking - we need a second graph object - logger.debug(" ---- DEBUG --- about to open a SECOND graph (takes a little while)--------\n"); - // Note - graph2 just reads - but we want it to use a fresh connection to - // the database, so we are NOT using the CACHED DB CONFIG here. - graph2 = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG).forService(DataGrooming.class.getSimpleName()).withGraphType("realtime2").buildConfiguration()); - if (graph2 == null) { - String emsg = "null graph2 object in DataGrooming\n"; - throw new AAIException(AAI_EXCEPTION_NUMBER, emsg); - } else { - logger.debug("Got the graph2 object... \n"); - } - g2 = graph2.newTransaction(); - if (g2 == null) { - String emsg = "null graphTransaction2 object in DataGrooming\n"; - throw new AAIException(AAI_EXCEPTION_NUMBER, emsg); - } - - ArrayList<Vertex> vertList = new ArrayList<>(); - Iterator<Vertex> vItor3 = g.traversal().V(); - // Gotta hold these in a List - or else HBase times out as you cycle - // through these - while (vItor3.hasNext()) { - Vertex v = vItor3.next(); - vertList.add(v); - } - int counter = 0; - int lastShown = 0; - Iterator<Vertex> vItor2 = vertList.iterator(); - logger.info(" Checking for bad edges --- "); - - while (vItor2.hasNext()) { - Vertex v = null; - try { - try { - v = vItor2.next(); - } catch (Exception vex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(">>> WARNING trying to get next vertex on the vItor2 "); - continue; - } - - counter++; - String thisVertId = ""; - try { - thisVertId = v.id().toString(); - } catch (Exception ev) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn("WARNING when doing getId() on a vertex from our vertex list. "); - continue; - } - if (ghostNodeHash.containsKey(thisVertId)) { - // This is a phantom node, so don't try to use it - logger.info(" >> Skipping edge check for edges from vertexId = " - + thisVertId - + ", since that guy is a Phantom Node"); - continue; - } - - if( windowStartTime > 0 ){ - // They are using the time-window, so we only want nodes that are updated after a - // passed-in timestamp OR that have no last-modified-timestamp which means they are suspicious. - Object objModTimeStamp = v.property("aai-last-mod-ts").orElse(null); - if( objModTimeStamp != null ){ - long thisNodeModTime = (long)objModTimeStamp; - if( thisNodeModTime < windowStartTime ){ - // It has a last modified ts and is NOT in our window, so we can pass over it - continue; - } - } - } - - if (counter == lastShown + 250) { - lastShown = counter; - logger.info("... Checking edges for vertex # " - + counter); - } - Iterator<Edge> eItor = v.edges(Direction.BOTH); - while (eItor.hasNext()) { - Edge e = null; - Vertex vIn = null; - Vertex vOut = null; - try { - e = eItor.next(); - } catch (Exception iex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(">>> WARNING trying to get next edge on the eItor ", iex); - continue; - } - - try { - vIn = e.inVertex(); - } catch (Exception err) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(">>> WARNING trying to get edge's In-vertex ", err); - } - String vNtI = ""; - String vIdI = ""; - Vertex ghost2 = null; - - Boolean keysMissing = true; - Boolean cantGetUsingVid = false; - if (vIn != null) { - try { - Object ob = vIn.<Object>property(AAI_NODE_TYPE).orElse(null); - if (ob != null) { - vNtI = ob.toString(); - keysMissing = anyKeyFieldsMissing(vNtI, vIn, loader); - } - ob = vIn.id(); - long vIdLong = 0L; - if (ob != null) { - vIdI = ob.toString(); - vIdLong = Long.parseLong(vIdI); - } - - if( ! ghost2CheckOff ){ - Vertex connectedVert = g2.traversal().V(vIdLong).next(); - if( connectedVert == null ) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn( "GHOST2 -- got NULL when doing getVertex for vid = " + vIdLong); - cantGetUsingVid = true; - - // If we can NOT get this ghost with the SECOND graph-object, - // it is still a ghost since even though we can get data about it using the FIRST graph - // object. - try { - ghost2 = g.traversal().V(vIdLong).next(); - } - catch( Exception ex){ - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn( "GHOST2 -- Could not get the ghost info for a bad edge for vtxId = " + vIdLong, ex); - } - if( ghost2 != null ){ - ghostNodeHash.put(vIdI, ghost2); - } - } - }// end of the ghost2 checking - } - catch (Exception err) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(">>> WARNING trying to get edge's In-vertex props ", err); - } - } - if (keysMissing || vIn == null || vNtI.equals("") - || cantGetUsingVid) { - // this is a bad edge because it points to a vertex - // that isn't there anymore or is corrupted - String thisEid = e.id().toString(); - if (deleteCandidateList.contains(thisEid) || deleteCandidateList.contains(vIdI)) { - boolean okFlag = true; - if (!vIdI.equals("")) { - // try to get rid of the corrupted vertex - try { - if( (ghost2 != null) && ghost2FixOn ){ - ghost2.remove(); - } - else { - vIn.remove(); - } - if (singleCommits) { - // NOTE - the singleCommits option is not used in normal processing - g.tx().commit(); - g = AAIGraph.getInstance().getGraph().newTransaction(); - } - deleteCount++; - } catch (Exception e1) { - okFlag = false; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn("WARNING when trying to delete bad-edge-connected VERTEX VID = " - + vIdI, e1); - } - if (okFlag) { - logger.info(" DELETED vertex from bad edge = " - + vIdI); - } - } else { - // remove the edge if we couldn't get the - // vertex - try { - e.remove(); - if (singleCommits) { - // NOTE - the singleCommits option is not used in normal processing - g.tx().commit(); - g = AAIGraph.getInstance().getGraph().newTransaction(); - } - deleteCount++; - } catch (Exception ex) { - // NOTE - often, the exception is just - // that this edge has already been - // removed - okFlag = false; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn("WARNING when trying to delete edge = " - + thisEid); - } - if (okFlag) { - logger.info(" DELETED edge = " + thisEid); - } - } - } else { - oneArmedEdgeHash.put(thisEid, e); - if ((vIn != null) && (vIn.id() != null)) { - emptyVertexHash.put(thisEid, vIn.id() - .toString()); - } - } - } - - try { - vOut = e.outVertex(); - } catch (Exception err) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(">>> WARNING trying to get edge's Out-vertex "); - } - String vNtO = ""; - String vIdO = ""; - ghost2 = null; - keysMissing = true; - cantGetUsingVid = false; - if (vOut != null) { - try { - Object ob = vOut.<Object>property(AAI_NODE_TYPE).orElse(null); - if (ob != null) { - vNtO = ob.toString(); - keysMissing = anyKeyFieldsMissing(vNtO, - vOut, loader); - } - ob = vOut.id(); - long vIdLong = 0L; - if (ob != null) { - vIdO = ob.toString(); - vIdLong = Long.parseLong(vIdO); - } - - if( ! ghost2CheckOff ){ - Vertex connectedVert = g2.traversal().V(vIdLong).next(); - if( connectedVert == null ) { - cantGetUsingVid = true; - logger.info( "GHOST2 -- got NULL when doing getVertex for vid = " + vIdLong); - // If we can get this ghost with the other graph-object, then get it -- it's still a ghost - try { - ghost2 = g.traversal().V(vIdLong).next(); - } - catch( Exception ex){ - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn( "GHOST2 -- Could not get the ghost info for a bad edge for vtxId = " + vIdLong, ex); - } - if( ghost2 != null ){ - ghostNodeHash.put(vIdO, ghost2); - } - } - } - } catch (Exception err) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(">>> WARNING trying to get edge's Out-vertex props ", err); - } - } - if (keysMissing || vOut == null || vNtO.isEmpty() - || cantGetUsingVid) { - // this is a bad edge because it points to a vertex - // that isn't there anymore - String thisEid = e.id().toString(); - if (deleteCandidateList.contains(thisEid) || deleteCandidateList.contains(vIdO)) { - boolean okFlag = true; - if (!vIdO.isEmpty()) { - // try to get rid of the corrupted vertex - try { - if( (ghost2 != null) && ghost2FixOn ){ - ghost2.remove(); - } - else if (vOut != null) { - vOut.remove(); - } - if (singleCommits) { - // NOTE - the singleCommits option is not used in normal processing - g.tx().commit(); - g = AAIGraph.getInstance().getGraph().newTransaction(); - } - deleteCount++; - } catch (Exception e1) { - okFlag = false; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn("WARNING when trying to delete bad-edge-connected VID = " - + vIdO, e1); - } - if (okFlag) { - logger.info(" DELETED vertex from bad edge = " - + vIdO); - } - } else { - // remove the edge if we couldn't get the - // vertex - try { - e.remove(); - if (singleCommits) { - // NOTE - the singleCommits option is not used in normal processing - g.tx().commit(); - g = AAIGraph.getInstance().getGraph().newTransaction(); - } - deleteCount++; - } catch (Exception ex) { - // NOTE - often, the exception is just - // that this edge has already been - // removed - okFlag = false; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn("WARNING when trying to delete edge = " - + thisEid, ex); - } - if (okFlag) { - logger.info(" DELETED edge = " + thisEid); - } - } - } else { - oneArmedEdgeHash.put(thisEid, e); - if ((vOut != null) && (vOut.id() != null)) { - emptyVertexHash.put(thisEid, vOut.id() - .toString()); - } - } - } - }// End of while-edges-loop - } catch (Exception exx) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn("WARNING from in the while-verts-loop ", exx); - } - }// End of while-vertices-loop (the edge-checking) - } // end of -- if we're not skipping the edge-checking - - - deleteCount = deleteCount + dupeGrpsDeleted; - if (!singleCommits && deleteCount > 0) { - try { - logger.info("About to do the commit for " - + deleteCount + " removes. "); - executeFinalCommit = true; - logger.info("Commit was successful "); - } catch (Exception excom) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error(" >>>> ERROR <<<< Could not commit changes. " + LogFormatTools.getStackTop(excom)); - deleteCount = 0; - } - } - - int ghostNodeCount = ghostNodeHash.size(); - int orphanNodeCount = orphanNodeHash.size(); - int missingDepNodeCount = missingDepNodeHash.size(); - int oneArmedEdgeCount = oneArmedEdgeHash.size(); - int dupeCount = dupeGroups.size(); - - deleteCount = deleteCount + dupeGrpsDeleted; - - bw.write("\n\n ============ Summary ==============\n"); - if( timeWindowMinutes == 0 ){ - bw.write("Ran FULL data grooming (no time-window). \n"); - } - else { - bw.write("Ran PARTIAL data grooming just looking at data added/updated in the last " + timeWindowMinutes + " minutes. \n"); - } - - bw.write("\nRan these nodeTypes: " + ntList + "\n\n"); - bw.write("There were this many delete candidates from previous run = " - + deleteCandidateList.size() + "\n"); - if (dontFixOrphansFlag) { - bw.write(" Note - we are not counting orphan nodes since the -dontFixOrphans parameter was used. \n"); - } - bw.write("Deleted this many delete candidates = " + deleteCount - + "\n"); - bw.write("Total number of nodes looked at = " + totalNodeCount - + "\n"); - bw.write("Ghost Nodes identified = " + ghostNodeCount + "\n"); - bw.write("Orphan Nodes identified = " + orphanNodeCount + "\n"); - bw.write("Bad Edges identified = " + oneArmedEdgeCount + "\n"); - bw.write("Missing Dependent Edge (but not orphaned) node count = " - + missingDepNodeCount + "\n"); - bw.write("Duplicate Groups count = " + dupeCount + "\n"); - bw.write("MisMatching Label/aai-node-type count = " - + misMatchedHash.size() + "\n"); - - bw.write("\n ------------- Delete Candidates ---------\n"); - for (Map.Entry<String, Vertex> entry : ghostNodeHash - .entrySet()) { - String vid = entry.getKey(); - bw.write("DeleteCandidate: Phantom Vid = [" + vid + "]\n"); - cleanupCandidateCount++; - } - for (Map.Entry<String, Vertex> entry : orphanNodeHash - .entrySet()) { - String vid = entry.getKey(); - bw.write("DeleteCandidate: OrphanDepNode Vid = [" + vid + "]\n"); - if (!dontFixOrphansFlag) { - cleanupCandidateCount++; - } - } - for (Map.Entry<String, Edge> entry : oneArmedEdgeHash.entrySet()) { - String eid = entry.getKey(); - bw.write("DeleteCandidate: Bad EDGE Edge-id = [" + eid + "]\n"); - cleanupCandidateCount++; - } - for (Map.Entry<String, Vertex> entry : missingDepNodeHash - .entrySet()) { - String vid = entry.getKey(); - bw.write("DeleteCandidate: (maybe) missingDepNode Vid = [" - + vid + "]\n"); - cleanupCandidateCount++; - } - bw.write("\n-- NOTE - To see DeleteCandidates for Duplicates, you need to look in the Duplicates Detail section below.\n"); - - bw.write("\n ------------- GHOST NODES - detail "); - for (Map.Entry<String, Vertex> entry : ghostNodeHash - .entrySet()) { - try { - String vid = entry.getKey(); - bw.write("\n ==> Phantom Vid = " + vid + "\n"); - ArrayList<String> retArr = showPropertiesForNode( - TRANSID, FROMAPPID, entry.getValue()); - for (String info : retArr) { - bw.write(info + "\n"); - } - - retArr = showAllEdgesForNode(TRANSID, FROMAPPID, - entry.getValue()); - for (String info : retArr) { - bw.write(info + "\n"); - } - } catch (Exception dex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("error trying to print detail info for a ghost-node: " + LogFormatTools.getStackTop(dex)); - } - } - - bw.write("\n ------------- Missing Dependent Edge ORPHAN NODES - detail: "); - for (Map.Entry<String, Vertex> entry : orphanNodeHash - .entrySet()) { - try { - String vid = entry.getKey(); - bw.write("\n> Orphan Node Vid = " + vid + "\n"); - ArrayList<String> retArr = showPropertiesForNode( - TRANSID, FROMAPPID, entry.getValue()); - for (String info : retArr) { - bw.write(info + "\n"); - } - - retArr = showAllEdgesForNode(TRANSID, FROMAPPID, - entry.getValue()); - for (String info : retArr) { - bw.write(info + "\n"); - } - } catch (Exception dex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("error trying to print detail info for a Orphan Node /missing dependent edge " + LogFormatTools.getStackTop(dex)); - } - } - - bw.write("\n ------------- Missing Dependent Edge (but not orphan) NODES: "); - for (Map.Entry<String, Vertex> entry : missingDepNodeHash - .entrySet()) { - try { - String vid = entry.getKey(); - bw.write("\n> Missing edge to Dependent Node (but has edges) Vid = " - + vid + "\n"); - ArrayList<String> retArr = showPropertiesForNode( - TRANSID, FROMAPPID, entry.getValue()); - for (String info : retArr) { - bw.write(info + "\n"); - } - - retArr = showAllEdgesForNode(TRANSID, FROMAPPID, - entry.getValue()); - for (String info : retArr) { - bw.write(info + "\n"); - } - } catch (Exception dex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("error trying to print detail info for a node missing its dependent edge but not an orphan " - + LogFormatTools.getStackTop(dex)); - } - } - - bw.write("\n ------------- EDGES pointing to empty/bad vertices: "); - for (Map.Entry<String, Edge> entry : oneArmedEdgeHash.entrySet()) { - try { - String eid = entry.getKey(); - Edge thisE = entry.getValue(); - String badVid = emptyVertexHash.get(eid); - bw.write("\n> Edge pointing to bad vertex (Vid = " - + badVid + ") EdgeId = " + eid + "\n"); - bw.write("Label: [" + thisE.label() + "]\n"); - Iterator<Property<Object>> pI = thisE.properties(); - while (pI.hasNext()) { - Property<Object> propKey = pI.next(); - bw.write("Prop: [" + propKey + "], val = [" - + propKey.value() + "]\n"); - } - } catch (Exception pex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("error trying to print empty/bad vertex data: " + LogFormatTools.getStackTop(pex)); - } - } - - bw.write("\n ------------- Duplicates: "); - Iterator<String> dupeIter = dupeGroups.iterator(); - int dupeSetCounter = 0; - while (dupeIter.hasNext()) { - dupeSetCounter++; - String dset = (String) dupeIter.next(); - - bw.write("\n --- Duplicate Group # " + dupeSetCounter - + " Detail -----------\n"); - try { - // We expect each line to have at least two vid's, followed - // by the preferred one to KEEP - String[] dupeArr = dset.split("\\|"); - ArrayList<String> idArr = new ArrayList<>(); - int lastIndex = dupeArr.length - 1; - for (int i = 0; i <= lastIndex; i++) { - if (i < lastIndex) { - // This is not the last entry, it is one of the - // dupes, so we want to show all its info - bw.write(" >> Duplicate Group # " - + dupeSetCounter + " Node # " + i - + " ----\n"); - String vidString = dupeArr[i]; - idArr.add(vidString); - long longVertId = Long.parseLong(vidString); - Iterator<Vertex> vtxIterator = g.vertices(longVertId); - Vertex vtx = null; - if (vtxIterator.hasNext()) { - vtx = vtxIterator.next(); - } - ArrayList<String> retArr = showPropertiesForNode(TRANSID, FROMAPPID, vtx); - for (String info : retArr) { - bw.write(info + "\n"); - } - - retArr = showAllEdgesForNode(TRANSID, - FROMAPPID, vtx); - for (String info : retArr) { - bw.write(info + "\n"); - } - } else { - // This is the last entry which should tell us if we - // have a preferred keeper - String prefString = dupeArr[i]; - if (KEEP_VID_UNDETERMINED.equals(prefString)) { - bw.write("\n For this group of duplicates, could not tell which one to keep.\n"); - bw.write(" >>> This group needs to be taken care of with a manual/forced-delete.\n"); - } else { - // If we know which to keep, then the prefString - // should look like, "KeepVid=12345" - String[] prefArr = prefString.split("="); - if (prefArr.length != 2 - || (!"KeepVid".equals(prefArr[0]))) { - throw new Exception("Bad format. Expecting KeepVid=999999"); - } else { - String keepVidStr = prefArr[1]; - if (idArr.contains(keepVidStr)) { - bw.write("\n The vertex we want to KEEP has vertexId = " - + keepVidStr); - bw.write("\n The others become delete candidates: \n"); - idArr.remove(keepVidStr); - for (int x = 0; x < idArr.size(); x++) { - cleanupCandidateCount++; - bw.write("DeleteCandidate: Duplicate Vid = [" - + idArr.get(x) + "]\n"); - } - } else { - throw new Exception("ERROR - Vertex Id to keep not found in list of dupes. dset = [" - + dset + "]"); - } - } - }// else we know which one to keep - }// else last entry - }// for each vertex in a group - } catch (Exception dex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("error trying to print duplicate vertex data " + LogFormatTools.getStackTop(dex)); - } - - }// while - work on each group of dupes - - bw.write("\n ------------- Mis-matched Label/aai-node-type Nodes: \n "); - for (Map.Entry<String, String> entry : misMatchedHash.entrySet()) { - String msg = entry.getValue(); - bw.write("MixedMsg = " + msg + "\n"); - } - - bw.write("\n ------------- Got these errors while processing: \n"); - Iterator<String> errIter = errArr.iterator(); - while (errIter.hasNext()) { - String line = errIter.next(); - bw.write(line + "\n"); - } - - bw.close(); - - logger.info("\n ------------- Done doing all the checks ------------ "); - logger.info("Output will be written to " + fullOutputFileName); - - if (cleanupCandidateCount > 0) { - // Technically, this is not an error -- but we're throwing this - // error so that hopefully a - // monitoring system will pick it up and do something with it. - throw new AAIException("AAI_6123", "See file: [" + fullOutputFileName - + "] and investigate delete candidates. "); - } - } catch (AAIException e) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("Caught AAIException while grooming data"); - ErrorLogHelper.logException(e); - } catch (Exception ex) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("Caught exception while grooming data"); - ErrorLogHelper.logError("AAI_6128", ex.getMessage() + ", resolve and rerun dataGrooming"); - } finally { - - if (bw != null) { - try { - bw.close(); - } catch (IOException iox) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR); - logger.warn("Got an IOException trying to close bufferedWriter() \n", iox); - } - } - - if (g != null && g.tx().isOpen()) { - // Any changes that worked correctly should have already done - // their commits. - try { - if (executeFinalCommit) { - g.tx().commit(); - } - g.tx().rollback(); - } catch (Exception ex) { - // Don't throw anything because JanusGraph sometimes is just saying that the graph is already closed - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR); - logger.warn("WARNING from final graphTransaction.rollback()", ex); - } - } - - if (g2 != null && g2.tx().isOpen()) { - // Any changes that worked correctly should have already done - // their commits. - try { - g2.tx().rollback(); - } catch (Exception ex) { - // Don't throw anything because JanusGraph sometimes is just saying that the graph is already closed - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR); - logger.warn("WARNING from final graphTransaction2.rollback()", ex); - } - } - - if( finalShutdownFlag ){ - try { - if( graph != null && graph.isOpen() ){ - graph.tx().close(); - graph.close(); - } - } catch (Exception ex) { - // Don't throw anything because JanusGraph sometimes is just saying that the graph is already closed{ - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR); - logger.warn("WARNING from final graph.shutdown()", ex); - } - - try { - if( graph2 != null && graph2.isOpen() ){ - graph2.tx().close(); - graph2.close(); - } - } catch (Exception ex) { - // Don't throw anything because JanusGraph sometimes is just saying that the graph is already closed{ - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR); - logger.warn("WARNING from final graph2.shutdown()", ex); - } - } - - } - - return cleanupCandidateCount; - - }// end of doTheGrooming() - - - /** - * Vertex has these keys. - * - * @param tmpV the tmp V - * @param propHashWithKeys the prop hash with keys - * @return the boolean - */ - private static Boolean vertexHasTheseKeys( Vertex tmpV, HashMap <String, Object> propHashWithKeys) { - Iterator <?> it = propHashWithKeys.entrySet().iterator(); - while( it.hasNext() ){ - String propName = ""; - String propVal = ""; - Map.Entry <?,?>propEntry = (Map.Entry<?,?>)it.next(); - Object propNameObj = propEntry.getKey(); - if( propNameObj != null ){ - propName = propNameObj.toString(); - } - Object propValObj = propEntry.getValue(); - if( propValObj != null ){ - propVal = propValObj.toString(); - } - Object checkValObj = tmpV.<Object>property(propName).orElse(null); - if( checkValObj == null ) { - return false; - } - else if( !propVal.equals(checkValObj.toString()) ){ - return false; - } - } - return true; - } - - - /** - * Any key fields missing. - * - * @param nType the n type - * @param v the v - * @return the boolean - */ - private static Boolean anyKeyFieldsMissing(String nType, Vertex v, Loader loader) { - - try { - Introspector obj = null; - try { - obj = loader.introspectorFromName(nType); - } catch (AAIUnknownObjectException e) { - // They gave us a non-empty nodeType but our NodeKeyProps does - // not have data for it. Since we do not know what the - // key params are for this type of node, we will just - // return "false". - String emsg = " -- WARNING -- Unrecognized nodeType: [" + nType - + "]. We cannot determine required keys for this nType. "; - // NOTE - this will be caught below and a "false" returned - throw new AAIException("AAI_6121", emsg); - } - - // Determine what the key fields are for this nodeType - Collection <String> keyPropNamesColl = obj.getKeys(); - Iterator<String> keyPropI = keyPropNamesColl.iterator(); - while (keyPropI.hasNext()) { - String propName = keyPropI.next(); - Object ob = v.<Object>property(propName).orElse(null); - if (ob == null || ob.toString().isEmpty()) { - // It is missing a key property - return true; - } - } - } catch (AAIException e) { - // Something was wrong -- but since we weren't able to check - // the keys, we will not declare that it is missing keys. - return false; - } - return false; - } - - - /** - * Gets the delete list. - * - * @param targetDir the target dir - * @param fileName the file name - * @param edgesOnlyFlag the edges only flag - * @param dontFixOrphans the dont fix orphans - * @param dupeFixOn the dupe fix on - * @return the delete list - * @throws AAIException the AAI exception - */ - private static Set<String> getDeleteList(String targetDir, - String fileName, Boolean edgesOnlyFlag, Boolean dontFixOrphans, - Boolean dupeFixOn) throws AAIException { - - // Look in the file for lines formated like we expect - pull out any - // Vertex Id's to delete on this run - Set<String> delList = new LinkedHashSet<>(); - String fullFileName = targetDir + AAIConstants.AAI_FILESEP + fileName; - - try(BufferedReader br = new BufferedReader(new FileReader(fullFileName))) { - String line = br.readLine(); - while (line != null) { - if (!"".equals(line) && line.startsWith("DeleteCandidate")) { - if (edgesOnlyFlag && (!line.contains("Bad Edge"))) { - // We're not going to process edge guys - } else if (dontFixOrphans && line.contains("Orphan")) { - // We're not going to process orphans - } else if (!dupeFixOn && line.contains("Duplicate")) { - // We're not going to process Duplicates - } else { - int begIndex = line.indexOf("id = "); - int endIndex = line.indexOf("]"); - String vidVal = line.substring(begIndex + 6, endIndex); - delList.add(vidVal); - } - } - line = br.readLine(); - } - br.close(); - } catch (IOException e) { - throw new AAIException("AAI_6124", e, "Could not open input-file [" + fullFileName - + "], exception= " + e.getMessage()); - } - - return delList; - - }// end of getDeleteList - - /** - * Gets the preferred dupe. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param g the g - * @param dupeVertexList the dupe vertex list - * @param ver the ver - * @return Vertex - * @throws AAIException the AAI exception - */ - public static Vertex getPreferredDupe(String transId, - String fromAppId, GraphTraversalSource g, - ArrayList<Vertex> dupeVertexList, String ver, Loader loader) - throws AAIException { - - // This method assumes that it is being passed a List of vertex objects - // which - // violate our uniqueness constraints. - - Vertex nullVtx = null; - - if (dupeVertexList == null) { - return nullVtx; - } - int listSize = dupeVertexList.size(); - if (listSize == 0) { - return nullVtx; - } - if (listSize == 1) { - return (dupeVertexList.get(0)); - } - - Vertex vtxPreferred = null; - Vertex currentFaveVtx = dupeVertexList.get(0); - for (int i = 1; i < listSize; i++) { - Vertex vtxB = dupeVertexList.get(i); - vtxPreferred = pickOneOfTwoDupes(transId, fromAppId, g, - currentFaveVtx, vtxB, ver, loader); - if (vtxPreferred == null) { - // We couldn't choose one - return nullVtx; - } else { - currentFaveVtx = vtxPreferred; - } - } - - return (currentFaveVtx); - - } // end of getPreferredDupe() - - /** - * Pick one of two dupes. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param g the g - * @param vtxA the vtx A - * @param vtxB the vtx B - * @param ver the ver - * @return Vertex - * @throws AAIException the AAI exception - */ - public static Vertex pickOneOfTwoDupes(String transId, - String fromAppId, GraphTraversalSource g, Vertex vtxA, - Vertex vtxB, String ver, Loader loader) throws AAIException { - - Vertex nullVtx = null; - Vertex preferredVtx = null; - - Long vidA = new Long(vtxA.id().toString()); - Long vidB = new Long(vtxB.id().toString()); - - String vtxANodeType = ""; - String vtxBNodeType = ""; - Object objType = vtxA.<Object>property(AAI_NODE_TYPE).orElse(null); - if (objType != null) { - vtxANodeType = objType.toString(); - } - objType = vtxB.<Object>property(AAI_NODE_TYPE).orElse(null); - if (objType != null) { - vtxBNodeType = objType.toString(); - } - - if (vtxANodeType.isEmpty() || (!vtxANodeType.equals(vtxBNodeType))) { - // Either they're not really dupes or there's some bad data - so - // don't pick one - return nullVtx; - } - - // Check that node A and B both have the same key values (or else they - // are not dupes) - // (We'll check dep-node later) - // Determine what the key fields are for this nodeType - Collection <String> keyProps = new ArrayList <>(); - HashMap <String,Object> keyPropValsHash = new HashMap <String,Object>(); - try { - keyProps = loader.introspectorFromName(vtxANodeType).getKeys(); - } catch (AAIUnknownObjectException e) { - logger.warn("Required property not found", e); - throw new AAIException("AAI_6105", "Required Property name(s) not found for nodeType = " + vtxANodeType + ")"); - } - - Iterator<String> keyPropI = keyProps.iterator(); - while (keyPropI.hasNext()) { - String propName = keyPropI.next(); - String vtxAKeyPropVal = ""; - objType = vtxA.<Object>property(propName).orElse(null); - if (objType != null) { - vtxAKeyPropVal = objType.toString(); - } - String vtxBKeyPropVal = ""; - objType = vtxB.<Object>property(propName).orElse(null); - if (objType != null) { - vtxBKeyPropVal = objType.toString(); - } - - if (vtxAKeyPropVal.equals("") - || (!vtxAKeyPropVal.equals(vtxBKeyPropVal))) { - // Either they're not really dupes or they are missing some key - // data - so don't pick one - return nullVtx; - } - else { - // Keep these around for (potential) use later - keyPropValsHash.put(propName, vtxAKeyPropVal); - } - - } - - // Collect the vid's and aai-node-types of the vertices that each vertex - // (A and B) is connected to. - ArrayList<String> vtxIdsConn2A = new ArrayList<>(); - ArrayList<String> vtxIdsConn2B = new ArrayList<>(); - HashMap<String, String> nodeTypesConn2A = new HashMap<>(); - HashMap<String, String> nodeTypesConn2B = new HashMap<>(); - - ArrayList<Vertex> vertListA = getConnectedNodes( g, vtxA ); - if (vertListA != null) { - Iterator<Vertex> iter = vertListA.iterator(); - while (iter.hasNext()) { - Vertex tvCon = iter.next(); - String conVid = tvCon.id().toString(); - String nt = ""; - objType = tvCon.<Object>property(AAI_NODE_TYPE).orElse(null); - if (objType != null) { - nt = objType.toString(); - } - nodeTypesConn2A.put(nt, conVid); - vtxIdsConn2A.add(conVid); - } - } - - ArrayList<Vertex> vertListB = getConnectedNodes( g, vtxB ); - if (vertListB != null) { - Iterator<Vertex> iter = vertListB.iterator(); - while (iter.hasNext()) { - Vertex tvCon = iter.next(); - String conVid = tvCon.id().toString(); - String nt = ""; - objType = tvCon.<Object>property(AAI_NODE_TYPE).orElse(null); - if (objType != null) { - nt = objType.toString(); - } - nodeTypesConn2B.put(nt, conVid); - vtxIdsConn2B.add(conVid); - } - } - - // 1 - If this kind of node needs a dependent node for uniqueness, then - // verify that they both nodes point to the same dependent - // node (otherwise they're not really duplicates) - // Note - there are sometimes more than one dependent node type since - // one nodeType can be used in different ways. But for a - // particular node, it will only have one dependent node that - // it's connected to. - String onlyNodeThatIndexPointsToVidStr = ""; - Collection<String> depNodeTypes = loader.introspectorFromName(vtxANodeType).getDependentOn(); - if (depNodeTypes.isEmpty()) { - // This kind of node is not dependent on any other. That is ok. - // We need to find out if the unique index info is good or not and - // use that later when deciding if we can delete one. - onlyNodeThatIndexPointsToVidStr = findJustOneUsingIndex( transId, - fromAppId, g, keyPropValsHash, vtxANodeType, vidA, vidB, ver ); - } else { - String depNodeVtxId4A = ""; - String depNodeVtxId4B = ""; - Iterator<String> iter = depNodeTypes.iterator(); - while (iter.hasNext()) { - String depNodeType = iter.next(); - if (nodeTypesConn2A.containsKey(depNodeType)) { - // This is the dependent node type that vertex A is using - depNodeVtxId4A = nodeTypesConn2A.get(depNodeType); - } - if (nodeTypesConn2B.containsKey(depNodeType)) { - // This is the dependent node type that vertex B is using - depNodeVtxId4B = nodeTypesConn2B.get(depNodeType); - } - } - if (depNodeVtxId4A.isEmpty() - || (!depNodeVtxId4A.equals(depNodeVtxId4B))) { - // Either they're not really dupes or there's some bad data - so - // don't pick either one - return nullVtx; - } - } - - if (vtxIdsConn2A.size() == vtxIdsConn2B.size()) { - // 2 - If they both have edges to all the same vertices, - // then return the one that can be reached uniquely via the - // key if that is the case or - // else the one with the lower vertexId - - boolean allTheSame = true; - Iterator<String> iter = vtxIdsConn2A.iterator(); - while (iter.hasNext()) { - String vtxIdConn2A = iter.next(); - if (!vtxIdsConn2B.contains(vtxIdConn2A)) { - allTheSame = false; - break; - } - } - - if (allTheSame) { - // If everything is the same, but one of the two has a good - // pointer to it, then save that one. Otherwise, take the - // older one. - if( !onlyNodeThatIndexPointsToVidStr.isEmpty() ){ - // only one is reachable via the index - choose that one. - if( onlyNodeThatIndexPointsToVidStr.equals(vidA.toString()) ){ - preferredVtx = vtxA; - } - else if( onlyNodeThatIndexPointsToVidStr.equals(vidB.toString()) ){ - preferredVtx = vtxB; - } - } - else if (vidA < vidB) { - preferredVtx = vtxA; - } else { - preferredVtx = vtxB; - } - } - } else if (vtxIdsConn2A.size() > vtxIdsConn2B.size()) { - // 3 - VertexA is connected to more things than vtxB. - // We'll pick VtxA if its edges are a superset of vtxB's edges - // and it doesn't contradict the check for the index/key pointer. - boolean missingOne = false; - Iterator<String> iter = vtxIdsConn2B.iterator(); - while (iter.hasNext()) { - String vtxIdConn2B = iter.next(); - if (!vtxIdsConn2A.contains(vtxIdConn2B)) { - missingOne = true; - break; - } - } - if (!missingOne) { - if( onlyNodeThatIndexPointsToVidStr.isEmpty() - || onlyNodeThatIndexPointsToVidStr.equals(vidA.toString()) ){ - preferredVtx = vtxA; - } - } - } else if (vtxIdsConn2B.size() > vtxIdsConn2A.size()) { - // 4 - VertexB is connected to more things than vtxA. - // We'll pick VtxB if its edges are a superset of vtxA's edges - // and it doesn't contradict the check for the index/key pointer. - boolean missingOne = false; - Iterator<String> iter = vtxIdsConn2A.iterator(); - while (iter.hasNext()) { - String vtxIdConn2A = iter.next(); - if (!vtxIdsConn2B.contains(vtxIdConn2A)) { - missingOne = true; - break; - } - } - if (!missingOne) { - if( onlyNodeThatIndexPointsToVidStr.isEmpty() - || onlyNodeThatIndexPointsToVidStr.equals(vidB.toString()) ){ - preferredVtx = vtxB; - } - } - } else { - preferredVtx = nullVtx; - } - - return (preferredVtx); - - } // end of pickOneOfTwoDupes() - - /** - * Check and process dupes. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param g the g - * @param version the version - * @param nType the n type - * @param passedVertList the passed vert list - * @param dupeFixOn the dupe fix on - * @param deleteCandidateList the delete candidate list - * @param singleCommits the single commits - * @param alreadyFoundDupeGroups the already found dupe groups - * @return the array list - */ - private static List<String> checkAndProcessDupes(String transId, - String fromAppId, Graph g, GraphTraversalSource source, String version, String nType, - List<Vertex> passedVertList, Boolean dupeFixOn, - Set<String> deleteCandidateList, Boolean singleCommits, - ArrayList<String> alreadyFoundDupeGroups, Loader loader ) { - - ArrayList<String> returnList = new ArrayList<>(); - ArrayList<Vertex> checkVertList = new ArrayList<>(); - ArrayList<String> alreadyFoundDupeVidArr = new ArrayList<>(); - Boolean noFilterList = true; - Iterator<String> afItr = alreadyFoundDupeGroups.iterator(); - while (afItr.hasNext()) { - String dupeGrpStr = afItr.next(); - String[] dupeArr = dupeGrpStr.split("\\|"); - int lastIndex = dupeArr.length - 1; - for (int i = 0; i < lastIndex; i++) { - // Note: we don't want the last one... - String vidString = dupeArr[i]; - alreadyFoundDupeVidArr.add(vidString); - noFilterList = false; - } - } - - // For a given set of Nodes that were found with a set of KEY - // Parameters, (nodeType + key data) we will - // see if we find any duplicate nodes that need to be cleaned up. Note - - // it's legit to have more than one - // node with the same key data if the nodes depend on a parent for - // uniqueness -- as long as the two nodes - // don't hang off the same Parent. - // If we find duplicates, and we can figure out which of each set of - // duplicates is the one that we - // think should be preserved, we will record that. Whether we can tell - // which one should be - // preserved or not, we will return info about any sets of duplicates - // found. - // - // Each element in the returned arrayList might look like this: - // "1234|5678|keepVid=UNDETERMINED" (if there were 2 dupes, and we - // couldn't figure out which one to keep) - // or, "100017|200027|30037|keepVid=30037" (if there were 3 dupes and we - // thought the third one was the one that should survive) - - // Because of the way the calling code loops over stuff, we can get the - // same data multiple times - so we should - // not process any vertices that we've already seen. - - try { - Iterator<Vertex> pItr = passedVertList.iterator(); - while (pItr.hasNext()) { - Vertex tvx = pItr.next(); - String passedId = tvx.id().toString(); - if (noFilterList || !alreadyFoundDupeVidArr.contains(passedId)) { - // We haven't seen this one before - so we should check it. - checkVertList.add(tvx); - } - } - - if (checkVertList.size() < 2) { - // Nothing new to check. - return returnList; - } - - if (loader.introspectorFromName(nType).isTopLevel()) { - // If this was a node that does NOT depend on other nodes for - // uniqueness, and we - // found more than one node using its key -- record the found - // vertices as duplicates. - String dupesStr = ""; - for (int i = 0; i < checkVertList.size(); i++) { - dupesStr = dupesStr - + checkVertList.get(i).id().toString() + "|"; - } - if (dupesStr != "") { - Vertex prefV = getPreferredDupe(transId, fromAppId, - source, checkVertList, version, loader); - if (prefV == null) { - // We could not determine which duplicate to keep - dupesStr = dupesStr + KEEP_VID_UNDETERMINED; - returnList.add(dupesStr); - } else { - dupesStr = dupesStr + "KeepVid=" + prefV.id(); - Boolean didRemove = false; - if (dupeFixOn) { - didRemove = deleteNonKeepersIfAppropriate(g, - dupesStr, prefV.id().toString(), - deleteCandidateList, singleCommits); - } - if (didRemove) { - dupeGrpsDeleted++; - } else { - // keep them on our list - returnList.add(dupesStr); - } - } - } - } else { - // More than one node have the same key fields since they may - // depend on a parent node for uniqueness. Since we're finding - // more than one, we want to check to see if any of the - // vertices that have this set of keys (and are the same nodeType) - // are also pointing at the same 'parent' node. - // Note: for a given set of key data, it is possible that there - // could be more than one set of duplicates. - HashMap<String, ArrayList<Vertex>> vertsGroupedByParentHash = groupVertsByDepNodes( - transId, fromAppId, source, version, nType, - checkVertList, loader); - for (Map.Entry<String, ArrayList<Vertex>> entry : vertsGroupedByParentHash - .entrySet()) { - ArrayList<Vertex> thisParentsVertList = entry - .getValue(); - if (thisParentsVertList.size() > 1) { - // More than one vertex found with the same key info - // hanging off the same parent/dependent node - String dupesStr = ""; - for (int i = 0; i < thisParentsVertList.size(); i++) { - dupesStr = dupesStr - + thisParentsVertList.get(i).id() + "|"; - } - if (dupesStr != "") { - Vertex prefV = getPreferredDupe(transId, - fromAppId, source, thisParentsVertList, - version, loader); - - if (prefV == null) { - // We could not determine which duplicate to - // keep - dupesStr = dupesStr + KEEP_VID_UNDETERMINED; - returnList.add(dupesStr); - } else { - Boolean didRemove = false; - dupesStr = dupesStr + "KeepVid=" - + prefV.id().toString(); - if (dupeFixOn) { - didRemove = deleteNonKeepersIfAppropriate( - g, dupesStr, prefV.id() - .toString(), - deleteCandidateList, singleCommits); - } - if (didRemove) { - dupeGrpsDeleted++; - } else { - // keep them on our list - returnList.add(dupesStr); - } - } - } - } - } - } - } catch (Exception e) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.warn(" >>> Threw an error in checkAndProcessDupes - just absorb this error and move on. ", e); - } - - return returnList; - - }// End of checkAndProcessDupes() - - /** - * Group verts by dep nodes. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param g the g - * @param version the version - * @param nType the n type - * @param passedVertList the passed vert list - * @return the hash map - * @throws AAIException the AAI exception - */ - private static HashMap<String, ArrayList<Vertex>> groupVertsByDepNodes( - String transId, String fromAppId, GraphTraversalSource g, String version, - String nType, ArrayList<Vertex> passedVertList, Loader loader) - throws AAIException { - // Given a list of JanusGraph Vertices of one nodeType (see AAI-8956), group - // them together by the parent node they depend on. - // Ie. if given a list of ip address nodes (assumed to all have the - // same key info) they might sit under several different parent vertices. - // Under Normal conditions, there would only be one per parent -- but - // we're trying to find duplicates - so we - // allow for the case where more than one is under the same parent node. - - HashMap<String, ArrayList<Vertex>> retHash = new HashMap<String, ArrayList<Vertex>>(); - if (loader.introspectorFromName(nType).isTopLevel()) { - // This method really should not have been called if this is not the - // kind of node - // that depends on a parent for uniqueness, so just return the empty - // hash. - return retHash; - } - - // Find out what types of nodes the passed in nodes can depend on - ArrayList<String> depNodeTypeL = new ArrayList<>(); - Collection<String> depNTColl = loader.introspectorFromName(nType).getDependentOn(); - Iterator<String> ntItr = depNTColl.iterator(); - while (ntItr.hasNext()) { - depNodeTypeL.add(ntItr.next()); - } - // For each vertex, we want find its depended-on/parent vertex so we - // can track what other vertexes that are dependent on that same guy. - if (passedVertList != null) { - Iterator<Vertex> iter = passedVertList.iterator(); - while (iter.hasNext()) { - Vertex thisVert = iter.next(); - Vertex tmpParentVtx = getConnectedParent( g, thisVert ); - if( tmpParentVtx != null ) { - String parentNt = null; - Object obj = tmpParentVtx.<Object>property(AAI_NODE_TYPE).orElse(null); - if (obj != null) { - parentNt = obj.toString(); - } - if (depNTColl.contains(parentNt)) { - // This must be the parent/dependent node - String parentVid = tmpParentVtx.id().toString(); - if (retHash.containsKey(parentVid)) { - // add this vert to the list for this parent key - retHash.get(parentVid).add(thisVert); - } else { - // This is the first one we found on this parent - ArrayList<Vertex> vList = new ArrayList<>(); - vList.add(thisVert); - retHash.put(parentVid, vList); - } - } - } - } - } - - return retHash; - - }// end of groupVertsByDepNodes() - - /** - * Delete non keepers if appropriate. - * - * @param g the g - * @param dupeInfoString the dupe info string - * @param vidToKeep the vid to keep - * @param deleteCandidateList the delete candidate list - * @param singleCommits the single commits - * @return the boolean - */ - private static Boolean deleteNonKeepersIfAppropriate(Graph g, - String dupeInfoString, String vidToKeep, - Set<String> deleteCandidateList, Boolean singleCommits) { - - Boolean deletedSomething = false; - // This assumes that the dupeInfoString is in the format of - // pipe-delimited vid's followed by - // ie. "3456|9880|keepVid=3456" - if (deleteCandidateList == null || deleteCandidateList.isEmpty()) { - // No vid's on the candidate list -- so no deleting will happen on - // this run - return false; - } - - String[] dupeArr = dupeInfoString.split("\\|"); - ArrayList<String> idArr = new ArrayList<>(); - int lastIndex = dupeArr.length - 1; - for (int i = 0; i <= lastIndex; i++) { - if (i < lastIndex) { - // This is not the last entry, it is one of the dupes, - String vidString = dupeArr[i]; - idArr.add(vidString); - } else { - // This is the last entry which should tell us if we have a - // preferred keeper - String prefString = dupeArr[i]; - if (prefString.equals(KEEP_VID_UNDETERMINED)) { - // They sent us a bad string -- nothing should be deleted if - // no dupe could be tagged as preferred - return false; - } else { - // If we know which to keep, then the prefString should look - // like, "KeepVid=12345" - String[] prefArr = prefString.split("="); - if (prefArr.length != 2 || (!"KeepVid".equals(prefArr[0]))) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("Bad format. Expecting KeepVid=999999"); - return false; - } else { - String keepVidStr = prefArr[1]; - if (idArr.contains(keepVidStr)) { - idArr.remove(keepVidStr); - - // So now, the idArr should just contain the vid's - // that we want to remove. - for (int x = 0; x < idArr.size(); x++) { - boolean okFlag = true; - String thisVid = idArr.get(x); - if (deleteCandidateList.contains(thisVid)) { - // This vid is a valid delete candidate from - // a prev. run, so we can remove it. - try { - long longVertId = Long - .parseLong(thisVid); - Vertex vtx = g - .traversal().V(longVertId).next(); - vtx.remove(); - if (singleCommits) { - // NOTE - the singleCommits option is not used in normal processing - g.tx().commit(); - g = AAIGraph.getInstance().getGraph().newTransaction(); - } - } catch (Exception e) { - okFlag = false; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("ERROR trying to delete VID = " + thisVid + " " + LogFormatTools.getStackTop(e)); - } - if (okFlag) { - logger.info(" DELETED VID = " + thisVid); - deletedSomething = true; - } - } - } - } else { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error("ERROR - Vertex Id to keep not found in list of dupes. dupeInfoString = [" - + dupeInfoString + "]"); - return false; - } - } - }// else we know which one to keep - }// else last entry - }// for each vertex in a group - - return deletedSomething; - - }// end of deleteNonKeepersIfAppropriate() - - - /** - * Gets the node just using key params. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param graph the graph - * @param nodeType the node type - * @param keyPropsHash the key props hash - * @param apiVersion the api version - * @return the node just using key params - * @throws AAIException the AAI exception - */ - public static List <Vertex> getNodeJustUsingKeyParams( String transId, String fromAppId, GraphTraversalSource graph, String nodeType, - HashMap<String,Object> keyPropsHash, String apiVersion ) throws AAIException{ - - List <Vertex> retVertList = new ArrayList <> (); - - // We assume that all NodeTypes have at least one key-property defined. - // Note - instead of key-properties (the primary key properties), a user could pass - // alternate-key values if they are defined for the nodeType. - List<String> kName = new ArrayList<>(); - List<Object> kVal = new ArrayList<>(); - if( keyPropsHash == null || keyPropsHash.isEmpty() ) { - throw new AAIException("AAI_6120", " NO key properties passed for this getNodeJustUsingKeyParams() request. NodeType = [" + nodeType + "]. "); - } - - int i = -1; - for( Map.Entry<String, Object> entry : keyPropsHash.entrySet() ){ - i++; - kName.add(i, entry.getKey()); - kVal.add(i, entry.getValue()); - } - int topPropIndex = i; - Vertex tiV = null; - String propsAndValuesForMsg = ""; - Iterator <Vertex> verts = null; - - try { - if( topPropIndex == 0 ){ - propsAndValuesForMsg = " (" + kName.get(0) + " = " + kVal.get(0) + ") "; - verts= graph.V().has(kName.get(0),kVal.get(0)).has(AAI_NODE_TYPE,nodeType); - } - else if( topPropIndex == 1 ){ - propsAndValuesForMsg = " (" + kName.get(0) + " = " + kVal.get(0) + ", " - + kName.get(1) + " = " + kVal.get(1) + ") "; - verts = graph.V().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has(AAI_NODE_TYPE,nodeType); - } - else if( topPropIndex == 2 ){ - propsAndValuesForMsg = " (" + kName.get(0) + " = " + kVal.get(0) + ", " - + kName.get(1) + " = " + kVal.get(1) + ", " - + kName.get(2) + " = " + kVal.get(2) + ") "; - verts= graph.V().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has(kName.get(2),kVal.get(2)).has(AAI_NODE_TYPE,nodeType); - } - else if( topPropIndex == 3 ){ - propsAndValuesForMsg = " (" + kName.get(0) + " = " + kVal.get(0) + ", " - + kName.get(1) + " = " + kVal.get(1) + ", " - + kName.get(2) + " = " + kVal.get(2) + ", " - + kName.get(3) + " = " + kVal.get(3) + ") "; - verts= graph.V().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has(kName.get(2),kVal.get(2)).has(kName.get(3),kVal.get(3)).has(AAI_NODE_TYPE,nodeType); - } - else { - throw new AAIException("AAI_6114", " We only support 4 keys per nodeType for now \n"); - } - } - catch( Exception ex ){ - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logger.error( " ERROR trying to get node for: [" + propsAndValuesForMsg + "]" + LogFormatTools.getStackTop(ex)); - } - - if( verts != null ){ - while( verts.hasNext() ){ - tiV = verts.next(); - retVertList.add(tiV); - } - } - - if( retVertList.isEmpty() ){ - logger.debug("DEBUG No node found for nodeType = [" + nodeType + - "], propsAndVal = " + propsAndValuesForMsg ); - } - - return retVertList; - - }// End of getNodeJustUsingKeyParams() - - /** - * Show all edges for node. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param tVert the t vert - * @return the array list - */ - private static ArrayList <String> showAllEdgesForNode( String transId, String fromAppId, Vertex tVert ){ - - ArrayList <String> retArr = new ArrayList <> (); - Iterator <Edge> eI = tVert.edges(Direction.IN); - if( ! eI.hasNext() ){ - retArr.add("No IN edges were found for this vertex. "); - } - while( eI.hasNext() ){ - Edge ed = eI.next(); - String lab = ed.label(); - Vertex vtx; - if (tVert.equals(ed.inVertex())) { - vtx = ed.outVertex(); - } else { - vtx = ed.inVertex(); - } - if( vtx == null ){ - retArr.add(" >>> COULD NOT FIND VERTEX on the other side of this edge edgeId = " + ed.id() + " <<< "); - } - else { - String nType = vtx.<String>property(AAI_NODE_TYPE).orElse(null); - String vid = vtx.id().toString(); - retArr.add("Found an IN edge (" + lab + ") to this vertex from a [" + nType + "] node with VtxId = " + vid ); - - } - } - - eI = tVert.edges(Direction.OUT); - if( ! eI.hasNext() ){ - retArr.add("No OUT edges were found for this vertex. "); - } - while( eI.hasNext() ){ - Edge ed = eI.next(); - String lab = ed.label(); - Vertex vtx; - if (tVert.equals(ed.inVertex())) { - vtx = ed.outVertex(); - } else { - vtx = ed.inVertex(); - } - if( vtx == null ){ - retArr.add(" >>> COULD NOT FIND VERTEX on the other side of this edge edgeId = " + ed.id() + " <<< "); - } - else { - String nType = vtx.<String>property(AAI_NODE_TYPE).orElse(null); - String vid = vtx.id().toString(); - retArr.add("Found an OUT edge (" + lab + ") from this vertex to a [" + nType + "] node with VtxId = " + vid ); - } - } - return retArr; - } - - - /** - * Show properties for node. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param tVert the t vert - * @return the array list - */ - private static ArrayList <String> showPropertiesForNode( String transId, String fromAppId, Vertex tVert ){ - - ArrayList <String> retArr = new ArrayList <> (); - if( tVert == null ){ - retArr.add("null Node object passed to showPropertiesForNode()\n"); - } - else { - String nodeType = ""; - Object ob = tVert.<Object>property(AAI_NODE_TYPE).orElse(null); - if( ob == null ){ - nodeType = "null"; - } - else{ - nodeType = ob.toString(); - } - - retArr.add(" AAINodeType/VtxID for this Node = [" + nodeType + "/" + tVert.id() + "]"); - retArr.add(" Property Detail: "); - Iterator<VertexProperty<Object>> pI = tVert.properties(); - while( pI.hasNext() ){ - VertexProperty<Object> tp = pI.next(); - Object val = tp.value(); - retArr.add("Prop: [" + tp.key() + "], val = [" + val + "] "); - } - } - return retArr; - } - - - private static ArrayList <Vertex> getConnectedNodes(GraphTraversalSource g, Vertex startVtx ) - throws AAIException { - - ArrayList <Vertex> retArr = new ArrayList <> (); - if( startVtx == null ){ - return retArr; - } - else { - GraphTraversal<Vertex, Vertex> modPipe = null; - modPipe = g.V(startVtx).both(); - if( modPipe != null && modPipe.hasNext() ){ - while( modPipe.hasNext() ){ - Vertex conVert = modPipe.next(); - retArr.add(conVert); - } - } - } - return retArr; - - }// End of getConnectedNodes() - - - private static ArrayList <Vertex> getConnectedChildrenOfOneType( GraphTraversalSource g, - Vertex startVtx, String childNType ) { - - ArrayList <Vertex> childList = new ArrayList <> (); - Iterator <Vertex> vertI = g.V(startVtx).union(__.outE().has(EdgeProperty.CONTAINS.toString(), AAIDirection.OUT.toString()).inV(), __.inE().has(EdgeProperty.CONTAINS.toString(), AAIDirection.IN.toString()).outV()); - - Vertex tmpVtx = null; - while( vertI != null && vertI.hasNext() ){ - tmpVtx = vertI.next(); - Object ob = tmpVtx.<Object>property(AAI_NODE_TYPE).orElse(null); - if (ob != null) { - String tmpNt = ob.toString(); - if( tmpNt.equals(childNType)){ - childList.add(tmpVtx); - } - } - } - - return childList; - - }// End of getConnectedChildrenOfOneType() - - - private static Vertex getConnectedParent( GraphTraversalSource g, - Vertex startVtx ) { - - Vertex parentVtx = null; - Iterator <Vertex> vertI = g.V(startVtx).union(__.inE().has(EdgeProperty.CONTAINS.toString(), AAIDirection.OUT.toString()).outV(), __.outE().has(EdgeProperty.CONTAINS.toString(), AAIDirection.IN.toString()).inV()); - - while( vertI != null && vertI.hasNext() ){ - // Note - there better only be one! - parentVtx = vertI.next(); - } - - return parentVtx; - - }// End of getConnectedParent() - - - private static long figureWindowStartTime( int timeWindowMinutes ){ - // Given a window size, calculate what the start-timestamp would be. - - if( timeWindowMinutes <= 0 ){ - // This just means that there is no window... - return 0; - } - long unixTimeNow = System.currentTimeMillis(); - long windowInMillis = timeWindowMinutes * 60L * 1000; - - return unixTimeNow - windowInMillis; - } // End of figureWindowStartTime() - - - /** - * Collect Duplicate Sets for nodes that are NOT dependent on parent nodes. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param g the g - * @param version the version - * @param nType the n type - * @param passedVertList the passed vert list - * @return the array list - */ - private static ArrayList<ArrayList<Vertex>> getDupeSets4NonDepNodes( String transId, - String fromAppId, Graph g, String version, String nType, - ArrayList<Vertex> passedVertList, - ArrayList <String> keyPropNamesArr, - Loader loader ) { - - ArrayList<ArrayList<Vertex>> returnList = new ArrayList<ArrayList<Vertex>>(); - - // We've been passed a set of nodes that we want to check. - // They are all NON-DEPENDENT nodes of the same nodeType meaning that they should be - // unique in the DB based on their KEY DATA alone. So, if - // we group them by their key data - if any key has more than one - // vertex mapped to it, those vertices are dupes. - // - // When we find duplicates, we group them in an ArrayList (there can be - // more than one duplicate for one set of key data) - // Then these dupeSets are grouped up and returned. - // - - HashMap <String, ArrayList<String>> keyVals2VidHash = new HashMap <String, ArrayList<String>>(); - HashMap <String,Vertex> vtxHash = new HashMap <String,Vertex>(); - Iterator<Vertex> pItr = passedVertList.iterator(); - while (pItr.hasNext()) { - try { - Vertex tvx = pItr.next(); - String thisVid = tvx.id().toString(); - vtxHash.put(thisVid, tvx); - - // if there are more than one vertexId mapping to the same keyProps -- they are dupes - // we dont check till later since a set can contain more than 2. - String hKey = getNodeKeyValString( tvx, keyPropNamesArr ); - if( keyVals2VidHash.containsKey(hKey) ){ - // We've already seen this key - ArrayList <String> tmpVL = (ArrayList <String>)keyVals2VidHash.get(hKey); - tmpVL.add(thisVid); - keyVals2VidHash.put(hKey, tmpVL); - } - else { - // First time for this key - ArrayList <String> tmpVL = new ArrayList <String>(); - tmpVL.add(thisVid); - keyVals2VidHash.put(hKey, tmpVL); - } - } - catch (Exception e) { - logger.warn(" >>> Threw an error in getDupeSets4NonDepNodes - just absorb this error and move on. ", e); - } - } - - for( Map.Entry<String, ArrayList<String>> entry : keyVals2VidHash.entrySet() ){ - ArrayList <String> vidList = entry.getValue(); - try { - if( !vidList.isEmpty() && vidList.size() > 1 ){ - // There are more than one vertex id's using the same key info - ArrayList <Vertex> vertList = new ArrayList <Vertex> (); - for (int i = 0; i < vidList.size(); i++) { - String tmpVid = vidList.get(i); - vertList.add(vtxHash.get(tmpVid)); - } - returnList.add(vertList); - } - } - catch (Exception e) { - logger.warn(" >>> Threw an error in getDupeSets4NonDepNodes - just absorb this error and move on. ", e); - } - - } - return returnList; - - }// End of getDupeSets4NonDepNodes() - - - /** - * Get values of the key properties for a node as a single string - * - * @param tvx the vertex to pull the properties from - * @param keyPropNamesArr collection of key prop names - * @return a String of concatenated values - */ - private static String getNodeKeyValString( Vertex tvx, - ArrayList <String> keyPropNamesArr ) { - - String retString = ""; - Iterator <String> propItr = keyPropNamesArr.iterator(); - while( propItr.hasNext() ){ - String propName = propItr.next(); - if( tvx != null ){ - Object propValObj = tvx.property(propName).orElse(null); - retString = " " + retString + propValObj.toString(); - } - } - return retString; - - }// End of getNodeKeyValString() - - - private static String findJustOneUsingIndex( String transId, String fromAppId, - GraphTraversalSource gts, HashMap <String,Object> keyPropValsHash, - String nType, Long vidAL, Long vidBL, String apiVer){ - - // See if querying by JUST the key params (which should be indexed) brings back - // ONLY one of the two vertices. Ie. the db still has a pointer to one of them - // and the other one is sort of stranded. - String returnVid = ""; - - try { - List <Vertex> tmpVertList = getNodeJustUsingKeyParams( transId, fromAppId, gts, - nType, keyPropValsHash, apiVer ); - if( tmpVertList != null && tmpVertList.size() == 1 ){ - // We got just one - if it matches one of the ones we're looking - // for, then return that VID - Vertex tmpV = tmpVertList.get(0); - String thisVid = tmpV.id().toString(); - if( thisVid.equals(vidAL.toString()) || thisVid.equals(vidBL.toString()) ){ - String msg = " vid = " + thisVid + " is one of two that the DB can retrieve directly ------"; - - logger.info(msg); - returnVid = thisVid; - } - } - } - catch ( AAIException ae ){ - String emsg = "Error trying to get node just by key " + ae.getMessage(); - - logger.error(emsg); - } - - return returnVid; - - }// End of findJustOneUsingIndex() - -} diff --git a/aai-core/src/main/java/org/onap/aai/dbgen/DynamicPayloadGenerator.java b/aai-core/src/main/java/org/onap/aai/dbgen/DynamicPayloadGenerator.java deleted file mode 100644 index cd6838c8..00000000 --- a/aai-core/src/main/java/org/onap/aai/dbgen/DynamicPayloadGenerator.java +++ /dev/null @@ -1,496 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.dbgen; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Scanner; -import java.util.Set; -import java.util.UUID; - -import org.apache.commons.lang.exception.ExceptionUtils; -import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; -import org.apache.tinkerpop.gremlin.structure.Element; -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.onap.aai.db.props.AAIProperties; -import org.onap.aai.dbmap.DBConnectionType; -import org.onap.aai.dbmap.InMemoryGraph; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Introspector; -import org.onap.aai.introspection.Loader; -import org.onap.aai.introspection.LoaderFactory; -import org.onap.aai.introspection.ModelType; -import org.onap.aai.introspection.Version; -import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; -import org.onap.aai.logging.LogFormatTools; -import org.onap.aai.parsers.uri.URIToObject; -import org.onap.aai.serialization.engines.InMemoryDBEngine; -import org.onap.aai.serialization.engines.QueryStyle; -import org.onap.aai.serialization.db.DBSerializer; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -import org.slf4j.MDC; - -import org.codehaus.jackson.JsonNode; -import org.codehaus.jackson.map.ObjectMapper; -import org.codehaus.jackson.node.ObjectNode; -import org.codehaus.jackson.type.TypeReference; - -import java.util.Date; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import org.onap.aai.serialization.tinkerpop.TreeBackedVertex; -import org.onap.aai.util.AAIConfig; -import org.onap.aai.util.AAIConstants; - -import com.beust.jcommander.JCommander; -import com.beust.jcommander.Parameter; - -/** - * The Class ListEndpoints. - */ -public class DynamicPayloadGenerator { - - /* - * Create a Dynamic memory graph instance which should not affect the - * AAIGraph - */ - private InMemoryGraph inMemGraph = null; - private InMemoryDBEngine dbEngine; - - /* - * Loader, QueryStyle, ConnectionType for the Serializer - */ - private Loader loader; - private String urlBase; - private BufferedWriter bw = null; - - private CommandLineArgs cArgs; - - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DynamicPayloadGenerator.class); - - private static final QueryStyle queryStyle = QueryStyle.TRAVERSAL; - private static final DBConnectionType type = DBConnectionType.CACHED; - private static final ModelType introspectorFactoryType = ModelType.MOXY; - - /* - * Version - */ - private static final Version version = Version.getLatest(); - - /** - * The main method. - * - * @param args - * the arguments - * @throws AAIException - * @throws Exception - */ - public static void main(String[] args) { - - MDC.put("logFilenameAppender", DynamicPayloadGenerator.class.getSimpleName()); - DynamicPayloadGenerator payloadgen = new DynamicPayloadGenerator(); - try { - payloadgen.init(args); - - payloadgen.generatePayloads(); - } catch (AAIException e) { - LOGGER.error("Exception " + LogFormatTools.getStackTop(e)); - } catch (IOException e) { - LOGGER.error("Exception " + LogFormatTools.getStackTop(e)); - } - System.exit(0); - } - - public void init(String[] args) throws AAIException { - cArgs = new CommandLineArgs(); - JCommander jCommander = new JCommander(cArgs, args); - jCommander.setProgramName(DynamicPayloadGenerator.class.getSimpleName()); - LOGGER.info("Snapshot file" + cArgs.dataSnapshot); - //TODO- How to add dynamic.properties - - - LOGGER.info("output file" + cArgs.output); - - - LOGGER.info("format file" + cArgs.format); - LOGGER.info("format file" + cArgs.schemaEnabled); - if(cArgs.config.isEmpty()) - cArgs.config = AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "dynamic.properties"; - - LOGGER.info("config file" + cArgs.config); - if(cArgs.nodePropertyFile.isEmpty()) - cArgs.nodePropertyFile = AAIConstants.AAI_HOME_ETC_SCRIPT + "/tenant_isolation/nodes.json"; - LOGGER.info("nodePropertyFile file" + cArgs.nodePropertyFile); - AAIConfig.init(); - - urlBase = AAIConfig.get("aai.server.url.base", ""); - - } - - public void generatePayloads() throws AAIException, IOException{ - - List<Map<String, List<String>>> nodeFilters = readFile(cArgs.nodePropertyFile); - LOGGER.info("Load the Graph"); - - this.loadGraph(); - LOGGER.info("Generate payload"); - this.generatePayload(nodeFilters); - LOGGER.info("Close graph"); - this.closeGraph(); - - } - - private List<Map<String, List<String>>> readFile(String inputFile) throws IOException { - - InputStream is = new FileInputStream(inputFile); - Scanner scanner = new Scanner(is); - String jsonFile = scanner.useDelimiter("\\Z").next(); - scanner.close(); - - List<Map<String, List<String>>> allNodes = new ArrayList<>(); - Map<String, List<String>> filterCousins = new HashMap<>(); - Map<String, List<String>> filterParents = new HashMap<>(); - - ObjectMapper mapper = new ObjectMapper(); - - JsonNode rootNode = mapper.readTree(jsonFile); - - Iterator<Entry<String, JsonNode>> nodeFields = rootNode.getFields(); - - while (nodeFields.hasNext()) { - Entry<String, JsonNode> entry = nodeFields.next(); - String nodeType = entry.getKey(); - JsonNode nodeProperty = entry.getValue(); - - JsonNode cousinFilter = nodeProperty.path("cousins"); - JsonNode parentFilter = nodeProperty.path("parents"); - List<String> cousins = new ObjectMapper().readValue(cousinFilter.traverse(), - new TypeReference<ArrayList<String>>() { - }); - - List<String> parents = new ObjectMapper().readValue(parentFilter.traverse(), - new TypeReference<ArrayList<String>>() { - }); - for (String cousin : cousins) { - LOGGER.info("Cousins-Filtered" + cousin); - } - for (String parent : parents) { - LOGGER.info("Parents-Filtered" + parent); - } - filterCousins.put(nodeType, cousins); - filterParents.put(nodeType, parents); - - } - - allNodes.add(filterCousins); - allNodes.add(filterParents); - return allNodes; - - } - - private void loadGraph() throws IOException { - - loadGraphIntoMemory(); - buildDbEngine(); - - } - - private void loadGraphIntoMemory() throws IOException { - - inMemGraph = new InMemoryGraph.Builder().build(cArgs.dataSnapshot, cArgs.config, cArgs.schemaEnabled); - - } - - private void buildDbEngine() { - // TODO : parametrise version - loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version); - - dbEngine = new InMemoryDBEngine(queryStyle, type, loader, inMemGraph.getGraph()); - dbEngine.startTransaction(); - } - - private void generatePayload(List<Map<String, List<String>>> nodeFilters) throws AAIException, IOException { - - Map<String, List<String>> filterCousinsMap = nodeFilters.get(0); - Map<String, List<String>> filterParentsMap = nodeFilters.get(1); - - Set<String> nodeTypes = filterCousinsMap.keySet(); - - for (String nodeType : nodeTypes) { - if ("DMAAP-MR".equals(cArgs.format)) { - bw = createFile(nodeType + ".json"); - } - List<String> filterCousins = filterCousinsMap.get(nodeType); - List<String> filterParents = filterParentsMap.get(nodeType); - readVertices(nodeType, filterCousins, filterParents); - bw.close(); - LOGGER.info("All Done-" + nodeType); - } - - } - - private BufferedWriter createFile(String outfileName) throws IOException { - // FileLocation - String fileName = outfileName; - - File outFile = new File(fileName); - LOGGER.info(" Will write to " + outFile); - FileWriter fw = new FileWriter(outFile.getAbsoluteFile()); - return new BufferedWriter(fw); - - } - - private void createDirectory(String dirName) throws IOException { - // FileLocation - Path pathDir = Paths.get(dirName); - Files.createDirectories(pathDir); - - } - - public void readVertices(String nodeType, List<String> filterCousins, List<String> filterParents) - throws AAIException, IOException { - - DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "sourceOfTruth"); - List<Vertex> nodes = inMemGraph.getGraph().traversal().V().has("aai-node-type", nodeType).toList(); - - LOGGER.info("Number of nodes" + nodes.size()); - String dirName = cArgs.output + AAIConstants.AAI_FILESEP + nodeType + AAIConstants.AAI_FILESEP; - createDirectory( dirName); - // TODO: Formatter - if ("DMAAP-MR".equals(cArgs.format)) { - for (Vertex node : nodes) { - - Introspector nodeObj = serializer.getLatestVersionView(node); - createPayloadForDmaap(node, nodeObj); - - } - } - int counter = 0; - if ("PAYLOAD".equals(cArgs.format)) { - for (Vertex node : nodes) { - - counter++; - String filename = dirName + counter + "-" + nodeType + ".json"; - bw = createFile(filename); - Introspector obj = loader.introspectorFromName(nodeType); - Set<Vertex> seen = new HashSet<>(); - int depth = AAIProperties.MAXIMUM_DEPTH; - boolean nodeOnly = false; - - Tree<Element> tree = dbEngine.getQueryEngine().findSubGraph(node, depth, nodeOnly); - TreeBackedVertex treeVertex = new TreeBackedVertex(node, tree); - serializer.dbToObjectWithFilters(obj, treeVertex, seen, depth, nodeOnly, filterCousins, filterParents); - createPayloadForPut(obj); - bw.close(); - - URI uri = serializer.getURIForVertex(node); - String filenameWithUri = dirName + counter + "-" + nodeType + ".txt"; - bw = createFile(filenameWithUri); - bw.write(uri.toString()); - bw.newLine(); - bw.close(); - } - } - - } - - public void createPayloadForPut(Introspector nodeObj) throws IOException { - - String entityJson = nodeObj.marshal(false); - ObjectMapper mapper = new ObjectMapper(); - - ObjectNode rootNode = (ObjectNode) mapper.readTree(entityJson); - rootNode.remove("resource-version"); - - bw.newLine(); - bw.write(rootNode.toString()); - bw.newLine(); - } - - public void createPayloadForDmaap(Vertex node, Introspector nodeObj) - throws AAIException, UnsupportedEncodingException { - - DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, "sourceOfTruth"); - - URI uri = serializer.getURIForVertex(node); - - String sourceOfTruth = ""; - HashMap<String, Introspector> relatedVertices = new HashMap<>(); - List<Vertex> vertexChain = dbEngine.getQueryEngine().findParents(node); - - for (Vertex vertex : vertexChain) { - try { - - Introspector vertexObj = serializer.getVertexProperties(vertex); - - relatedVertices.put(vertexObj.getObjectId(), vertexObj); - } catch (AAIUnknownObjectException e) { - LOGGER.warn("Unable to get vertex properties, partial list of related vertices returned"); - } - - } - - String transactionId = "TXID"; - createNotificationEvent(transactionId, sourceOfTruth, uri, nodeObj, relatedVertices); - - } - - public void createNotificationEvent(String transactionId, String sourceOfTruth, URI uri, Introspector obj, - Map<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException { - - String action = "CREATE"; - final Introspector notificationEvent = loader.introspectorFromName("notification-event"); - - try { - Introspector eventHeader = loader.introspectorFromName("notification-event-header"); - URIToObject parser = new URIToObject(loader, uri, (HashMap) relatedObjects); - - String entityLink = urlBase + version + uri; - - notificationEvent.setValue("cambria-partition", "AAI"); - - eventHeader.setValue("entity-link", entityLink); - eventHeader.setValue("action", action); - eventHeader.setValue("entity-type", obj.getDbName()); - eventHeader.setValue("top-entity-type", parser.getTopEntityName()); - eventHeader.setValue("source-name", sourceOfTruth); - eventHeader.setValue("version", version.toString()); - eventHeader.setValue("id", transactionId); - eventHeader.setValue("event-type", "AAI-BASELINE"); - if (eventHeader.getValue("domain") == null) { - eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK")); - } - - if (eventHeader.getValue("sequence-number") == null) { - eventHeader.setValue("sequence-number", - AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK")); - } - - if (eventHeader.getValue("severity") == null) { - eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK")); - } - - if (eventHeader.getValue("id") == null) { - eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString()); - - } - - if (eventHeader.getValue("timestamp") == null) { - eventHeader.setValue("timestamp", genDate()); - } - - List<Object> parentList = parser.getParentList(); - parentList.clear(); - - if (!parser.getTopEntity().equals(parser.getEntity())) { - Introspector child; - String json = obj.marshal(false); - child = parser.getLoader().unmarshal(parser.getEntity().getName(), json); - parentList.add(child.getUnderlyingObject()); - } - - final Introspector eventObject; - - String json = ""; - if (parser.getTopEntity().equals(parser.getEntity())) { - json = obj.marshal(false); - eventObject = loader.unmarshal(obj.getName(), json); - } else { - json = parser.getTopEntity().marshal(false); - - eventObject = loader.unmarshal(parser.getTopEntity().getName(), json); - } - notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject()); - notificationEvent.setValue("entity", eventObject.getUnderlyingObject()); - - String entityJson = notificationEvent.marshal(false); - - bw.newLine(); - bw.write(entityJson); - - } catch (AAIUnknownObjectException e) { - LOGGER.error("Fatal error - notification-event-header object not found!"); - } catch (Exception e) { - LOGGER.error("Unmarshalling error occurred while generating Notification " + LogFormatTools.getStackTop(e)); - } - } - - private void closeGraph() { - inMemGraph.getGraph().tx().rollback(); - inMemGraph.getGraph().close(); - } - - public static String genDate() { - Date date = new Date(); - DateFormat formatter = new SimpleDateFormat("yyyyMMdd-HH:mm:ss:SSS"); - return formatter.format(date); - } - - public static String genDate2() { - Date date = new Date(); - DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); - return formatter.format(date); - } - -} - -class CommandLineArgs { - - @Parameter(names = "--help", help = true) - public boolean help; - - @Parameter(names = "-d", description = "snapshot file to be loaded") - public String dataSnapshot; - - @Parameter(names = "-s", description = "is schema to be enabled ", arity = 1) - public boolean schemaEnabled = true; - - @Parameter(names = "-c", description = "location of configuration file") - public String config = ""; - - @Parameter(names = "-o", description = "output location") - public String output = ""; - - @Parameter(names = "-f", description = "format of output") - public String format = "PAYLOAD"; - - @Parameter(names = "-n", description = "Node input file") - public String nodePropertyFile = ""; - -} diff --git a/aai-core/src/main/java/org/onap/aai/dbgen/GenTester.java b/aai-core/src/main/java/org/onap/aai/dbgen/GenTester.java deleted file mode 100644 index 1abd4708..00000000 --- a/aai-core/src/main/java/org/onap/aai/dbgen/GenTester.java +++ /dev/null @@ -1,156 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.dbgen; - -import com.att.eelf.configuration.Configuration; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.janusgraph.core.JanusGraph; -import org.janusgraph.core.schema.JanusGraphManagement; -import org.onap.aai.dbmap.AAIGraph; -import org.onap.aai.logging.ErrorLogHelper; -import org.onap.aai.logging.LoggingContext; -import org.onap.aai.logging.LoggingContext.StatusCode; -import org.onap.aai.util.AAIConfig; -import org.onap.aai.util.AAIConstants; - -import java.util.Properties; -import java.util.UUID; - - -public class GenTester { - - private static EELFLogger LOGGER; - - /** - * The main method. - * - * @param args the arguments - */ - public static void main(String[] args) { - - JanusGraph graph = null; - System.setProperty("aai.service.name", GenTester.class.getSimpleName()); - // Set the logging file properties to be used by EELFManager - Properties props = System.getProperties(); - props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_CREATE_DB_SCHEMA_LOGBACK_PROPS); - props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); - LOGGER = EELFManager.getInstance().getLogger(GenTester.class); - boolean addDefaultCR = true; - - LoggingContext.init(); - LoggingContext.component("DBGenTester"); - LoggingContext.partnerName("AAI-TOOLS"); - LoggingContext.targetEntity("AAI"); - LoggingContext.requestId(UUID.randomUUID().toString()); - LoggingContext.serviceName("AAI"); - LoggingContext.targetServiceName("main"); - LoggingContext.statusCode(StatusCode.COMPLETE); - LoggingContext.responseCode(LoggingContext.SUCCESS); - try { - AAIConfig.init(); - if (args != null && args.length > 0 ){ - if( "genDbRulesOnly".equals(args[0]) ){ - ErrorLogHelper.logError("AAI_3100", - " This option is no longer supported. What was in DbRules is now derived from the OXM files. "); - return; - } - else if ( "GEN_DB_WITH_NO_SCHEMA".equals(args[0]) ){ - // Note this is done to create an empty DB with no Schema so that - // an HBase copyTable can be used to set up a copy of the db. - String imsg = " ---- NOTE --- about to load a graph without doing any schema processing (takes a little while) -------- "; - System.out.println(imsg); - LOGGER.info(imsg); - graph = AAIGraph.getInstance().getGraph(); - - if( graph == null ){ - ErrorLogHelper.logError("AAI_5102", "Error creating JanusGraph graph."); - return; - } - else { - String amsg = "Successfully loaded a JanusGraph graph without doing any schema work. "; - System.out.println(amsg); - LOGGER.auditEvent(amsg); - return; - } - } else if ("GEN_DB_WITH_NO_DEFAULT_CR".equals(args[0])) { - addDefaultCR = false; - } - else { - ErrorLogHelper.logError("AAI_3000", "Unrecognized argument passed to GenTester.java: [" + args[0] + "]. "); - - String emsg = "Unrecognized argument passed to GenTester.java: [" + args[0] + "]. "; - System.out.println(emsg); - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - LOGGER.error(emsg); - - emsg = "Either pass no argument for normal processing, or use 'GEN_DB_WITH_NO_SCHEMA'."; - System.out.println(emsg); - LOGGER.error(emsg); - - return; - } - } - - //AAIConfig.init(); - ErrorLogHelper.loadProperties(); - String imsg = " ---- NOTE --- about to open graph (takes a little while)--------;"; - System.out.println(imsg); - LOGGER.info(imsg); - graph = AAIGraph.getInstance().getGraph(); - - if( graph == null ){ - ErrorLogHelper.logError("AAI_5102", "Error creating JanusGraph graph. "); - return; - } - - // Load the propertyKeys, indexes and edge-Labels into the DB - JanusGraphManagement graphMgt = graph.openManagement(); - - imsg = "-- Loading new schema elements into JanusGraph --"; - System.out.println(imsg); - LOGGER.info(imsg); - SchemaGenerator.loadSchemaIntoJanusGraph(graphMgt, addDefaultCR ); - - } catch(Exception ex) { - ErrorLogHelper.logError("AAI_4000", ex.getMessage()); - } - - - if( graph != null ){ - String imsg = "-- graph commit"; - System.out.println(imsg); - LOGGER.info(imsg); - graph.tx().commit(); - - imsg = "-- graph shutdown "; - System.out.println(imsg); - LOGGER.info(imsg); - graph.close(); - } - - LOGGER.auditEvent("-- all done, if program does not exit, please kill."); - System.exit(0); - } - -} - - diff --git a/aai-core/src/main/java/org/onap/aai/dbgen/SchemaGenerator.java b/aai-core/src/main/java/org/onap/aai/dbgen/SchemaGenerator.java index c9813384..d2f60f11 100644 --- a/aai-core/src/main/java/org/onap/aai/dbgen/SchemaGenerator.java +++ b/aai-core/src/main/java/org/onap/aai/dbgen/SchemaGenerator.java @@ -8,7 +8,7 @@ * 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 + * 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, @@ -19,187 +19,179 @@ */ package org.onap.aai.dbgen; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.google.common.collect.Multimap; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Optional; import java.util.Set; + import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.janusgraph.core.Cardinality; -import org.janusgraph.core.Multiplicity; -import org.janusgraph.core.PropertyKey; -import org.janusgraph.core.schema.JanusGraphManagement; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.introspection.LoaderFactory; import org.onap.aai.introspection.ModelType; import org.onap.aai.logging.LogFormatTools; import org.onap.aai.schema.enums.PropertyMetadata; -import org.onap.aai.serialization.db.EdgeRule; -import org.onap.aai.serialization.db.EdgeRules; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.setup.SchemaVersions; import org.onap.aai.util.AAIConfig; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.common.collect.Multimap; +import org.janusgraph.core.Cardinality; +import org.janusgraph.core.Multiplicity; +import org.janusgraph.core.PropertyKey; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.schema.JanusGraphManagement; public class SchemaGenerator { - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaGenerator.class); - private static boolean addDefaultCR = true; - - private SchemaGenerator() { - - } - - /** - * Load schema into JanusGraph. - * @param graphMgmt the graph mgmt - * @param addDefaultCloudRegion the add default cloud region - */ - public static void loadSchemaIntoJanusGraph(final JanusGraphManagement graphMgmt, - boolean addDefaultCloudRegion) { - addDefaultCR = addDefaultCloudRegion; - loadSchemaIntoJanusGraph(graphMgmt); - } - - /** - * Load schema into JanusGraph. - * - * @param graphMgmt the graph mgmt - */ - public static void loadSchemaIntoJanusGraph(final JanusGraphManagement graphMgmt) { - - try { - AAIConfig.init(); - } catch (Exception ex) { - LOGGER.error(" ERROR - Could not run AAIConfig.init(). " + LogFormatTools.getStackTop(ex)); - System.exit(1); - } - - // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels. - // They are only used when a vertex is actually being created. JanusGraph 1.1 will keep track (we think). - - // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: the multiplicty used here is - // always "MULTI". This is not the same as our internal "Many2Many", "One2One", "One2Many" or "Many2One" - // We use the same edge-label for edges between many different types of nodes and our internal - // multiplicty definitions depends on which two types of nodes are being connected. - - Multimap<String, EdgeRule> edges = null; - Set<String> labels = new HashSet<>(); - - edges = EdgeRules.getInstance().getAllRules(); - for (EdgeRule rule : edges.values()) { - labels.add(rule.getLabel()); - } - - for (String label : labels) { - addEdgeLabel(graphMgmt, label); - } - - Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST); - Map<String, Introspector> objs = loader.getAllObjects(); - Map<String, PropertyKey> seenProps = new HashMap<>(); - - for (Introspector obj : objs.values()) { - for (String propertyName : obj.getProperties()) { - String dbPropertyName = propertyName; - Optional<String> alias = obj.getPropertyMetadata(propertyName, PropertyMetadata.DB_ALIAS); - if (alias.isPresent()) { - dbPropertyName = alias.get(); - } - if (graphMgmt.containsRelationType(propertyName)) { - handleExistingProperty(propertyName); - } else { - handleUnknownProperty(graphMgmt, seenProps, obj, propertyName, dbPropertyName); - } - } - } - - String imsg = "-- About to call graphMgmt commit"; - LOGGER.info(imsg); - - graphMgmt.commit(); - }// End of loadSchemaIntoJanusGraph() - - private static void handleUnknownProperty(JanusGraphManagement graphMgmt, Map<String, PropertyKey> seenProps, - Introspector obj, String propertyName, String dbPropertyName) { - - Class<?> type = obj.getClass(propertyName); - Cardinality cardinality = Cardinality.SINGLE; - boolean process = false; - if (obj.isListType(propertyName) && obj.isSimpleGenericType(propertyName)) { - cardinality = Cardinality.SET; - type = obj.getGenericTypeClass(propertyName); - process = true; - } else if (obj.isSimpleType(propertyName)) { - process = true; - } - - if (process) { - String imsg = - "Creating PropertyKey: [" + dbPropertyName + "], [" + type.getSimpleName() + "], [" + cardinality + "]"; - LOGGER.info(imsg); - PropertyKey propK; - propK = getPropertyKey(graphMgmt, seenProps, dbPropertyName, type, cardinality); - buildIndex(graphMgmt, obj, propertyName, dbPropertyName, propK); - } - } - - private static void buildIndex(JanusGraphManagement graphMgmt, Introspector obj, String propertyName, - String dbPropertyName, PropertyKey propK) { - String imsg; - if (graphMgmt.containsGraphIndex(dbPropertyName)) { - String dmsg = " Index [" + dbPropertyName + "] already existed in the DB. "; - LOGGER.debug(dmsg); - } else { - if (obj.getIndexedProperties().contains(propertyName)) { - if (obj.getUniqueProperties().contains(propertyName)) { - imsg = "Add Unique index for PropertyKey: [" + dbPropertyName + "]"; - LOGGER.info(imsg); - graphMgmt.buildIndex(dbPropertyName, Vertex.class).addKey(propK).unique().buildCompositeIndex(); - } else { - imsg = "Add index for PropertyKey: [" + dbPropertyName + "]"; - LOGGER.info(imsg); - graphMgmt.buildIndex(dbPropertyName, Vertex.class).addKey(propK).buildCompositeIndex(); - } - } else { - imsg = "No index added for PropertyKey: [" + dbPropertyName + "]"; - LOGGER.info(imsg); - } - } - } - - private static PropertyKey getPropertyKey(JanusGraphManagement graphMgmt, Map<String, PropertyKey> seenProps, - String dbPropertyName, Class<?> type, Cardinality cardinality) { - PropertyKey propK; - if (!seenProps.containsKey(dbPropertyName)) { - propK = graphMgmt.makePropertyKey(dbPropertyName).dataType(type).cardinality(cardinality).make(); - seenProps.put(dbPropertyName, propK); - } else { - propK = seenProps.get(dbPropertyName); - } - return propK; - } - - private static void handleExistingProperty(String propertyName) { - String dmsg = " PropertyKey [" + propertyName + "] already existed in the DB. "; - LOGGER.debug(dmsg); - } - - private static void addEdgeLabel(JanusGraphManagement graphMgmt, String label) { - if (graphMgmt.containsRelationType(label)) { - String dmsg = " EdgeLabel [" + label + "] already existed. "; - LOGGER.debug(dmsg); - } else { - String dmsg = "Making EdgeLabel: [" + label + "]"; - LOGGER.debug(dmsg); - graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make(); - } - } + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(SchemaGenerator.class); + + /** + * Load schema into JanusGraph. + * + * @param graph + * the graph + * @param graphMgmt + * the graph mgmt + */ + public static void loadSchemaIntoJanusGraph(final JanusGraph graph, final JanusGraphManagement graphMgmt, + String backend) { + + try { + AAIConfig.init(); + } catch (Exception ex) { + LOGGER.error(" ERROR - Could not run AAIConfig.init(). " + LogFormatTools.getStackTop(ex)); + System.out.println(" ERROR - Could not run AAIConfig.init(). "); + System.exit(1); + } + + // NOTE - JanusGraph 0.5.3 doesn't keep a list of legal node Labels. + // They are only used when a vertex is actually being created. + // JanusGraph 1.1 will keep track (we think). + + // Use EdgeRules to make sure edgeLabels are defined in the db. NOTE: + // the multiplicty used here is + // always "MULTI". This is not the same as our internal "Many2Many", + // "One2One", "One2Many" or "Many2One" + // We use the same edge-label for edges between many different types of + // nodes and our internal + // multiplicty definitions depends on which two types of nodes are being + // connected. + + Multimap<String, EdgeRule> edges = null; + Set<String> labels = new HashSet<>(); + + EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class); + + try { + edges = edgeIngestor.getAllCurrentRules(); + } catch (EdgeRuleNotFoundException e) { + e.printStackTrace(); + LOGGER.error("Unable to find all rules {}", LogFormatTools.getStackTop(e)); + } + + for (EdgeRule rule : edges.values()) { + labels.add(rule.getLabel()); + } + + for (String label : labels) { + if (graphMgmt.containsRelationType(label)) { + String dmsg = " EdgeLabel [" + label + "] already existed. "; + LOGGER.debug(dmsg); + } else { + String dmsg = "Making EdgeLabel: [" + label + "]"; + LOGGER.debug(dmsg); + graphMgmt.makeEdgeLabel(label).multiplicity(Multiplicity.valueOf("MULTI")).make(); + } + } + + // ApplicationContext ctx = SpringContextAware.getApplicationContext(); + // Loader loader = + // ctx.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY, + // AAIProperties.LATEST); + LoaderFactory loaderFactory = SpringContextAware.getBean(LoaderFactory.class); + SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + + Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion()); + // Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, + // AAIProperties.LATEST); + Map<String, Introspector> objs = loader.getAllObjects(); + Map<String, PropertyKey> seenProps = new HashMap<>(); + + for (Introspector obj : objs.values()) { + for (String propName : obj.getProperties()) { + String dbPropName = propName; + Optional<String> alias = obj.getPropertyMetadata(propName, PropertyMetadata.DB_ALIAS); + if (alias.isPresent()) { + dbPropName = alias.get(); + } + if (graphMgmt.containsRelationType(propName)) { + String dmsg = " PropertyKey [" + propName + "] already existed in the DB. "; + LOGGER.debug(dmsg); + } else { + Class<?> type = obj.getClass(propName); + Cardinality cardinality = Cardinality.SINGLE; + boolean process = false; + if (obj.isListType(propName) && obj.isSimpleGenericType(propName)) { + cardinality = Cardinality.SET; + type = obj.getGenericTypeClass(propName); + process = true; + } else if (obj.isSimpleType(propName)) { + process = true; + } + + if (process) { + + String imsg = "Creating PropertyKey: [" + dbPropName + "], [" + type.getSimpleName() + "], [" + + cardinality + "]"; + LOGGER.info(imsg); + PropertyKey propK; + if (!seenProps.containsKey(dbPropName)) { + propK = graphMgmt.makePropertyKey(dbPropName).dataType(type).cardinality(cardinality) + .make(); + seenProps.put(dbPropName, propK); + } else { + propK = seenProps.get(dbPropName); + } + if (graphMgmt.containsGraphIndex(dbPropName)) { + String dmsg = " Index [" + dbPropName + "] already existed in the DB. "; + LOGGER.debug(dmsg); + } else { + if (obj.getIndexedProperties().contains(propName)) { + if (obj.getUniqueProperties().contains(propName)) { + imsg = "Add Unique index for PropertyKey: [" + dbPropName + "]"; + LOGGER.info(imsg); + graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).unique() + .buildCompositeIndex(); + } else { + imsg = "Add index for PropertyKey: [" + dbPropName + "]"; + LOGGER.info(imsg); + graphMgmt.buildIndex(dbPropName, Vertex.class).addKey(propK).buildCompositeIndex(); + } + } else { + imsg = "No index added for PropertyKey: [" + dbPropName + "]"; + LOGGER.info(imsg); + } + } + } + } + } + } + + String imsg = "-- About to call graphMgmt commit"; + LOGGER.info(imsg); + if(backend != null){ + System.out.println("Successfully loaded the schema to " + backend); + } + + graphMgmt.commit(); + } } - - diff --git a/aai-core/src/main/java/org/onap/aai/dbmap/AAIGraph.java b/aai-core/src/main/java/org/onap/aai/dbmap/AAIGraph.java index b8596eb9..aae3aaae 100644 --- a/aai-core/src/main/java/org/onap/aai/dbmap/AAIGraph.java +++ b/aai-core/src/main/java/org/onap/aai/dbmap/AAIGraph.java @@ -19,8 +19,10 @@ */ package org.onap.aai.dbmap; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; @@ -145,7 +147,7 @@ public class AAIGraph { logAndPrint(logger, "ERROR: Could not load datasnapshot to in memory graph. \n" + ExceptionUtils.getFullStackTrace(e)); - System.exit(0); + throw new RuntimeException(e); } } } @@ -156,7 +158,7 @@ public class AAIGraph { JanusGraphManagement graphMgt = graph.openManagement(); System.out.println("-- loading schema into JanusGraph"); - SchemaGenerator.loadSchemaIntoJanusGraph(graphMgt ); + SchemaGenerator.loadSchemaIntoJanusGraph( graph, graphMgt, "inmemory"); } /** diff --git a/aai-core/src/main/java/org/onap/aai/dbmap/InMemoryGraph.java b/aai-core/src/main/java/org/onap/aai/dbmap/InMemoryGraph.java deleted file mode 100644 index 3f002ff5..00000000 --- a/aai-core/src/main/java/org/onap/aai/dbmap/InMemoryGraph.java +++ /dev/null @@ -1,103 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.dbmap; - -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; - -import java.util.Properties; - -import org.apache.tinkerpop.gremlin.structure.io.IoCore; -import org.onap.aai.dbgen.SchemaGenerator; -import org.onap.aai.logging.LogFormatTools; - -import org.janusgraph.core.JanusGraphFactory; -import org.janusgraph.core.JanusGraph; -import org.janusgraph.core.JanusGraphTransaction; -import org.janusgraph.core.schema.JanusGraphManagement; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -public class InMemoryGraph { - - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class); - private JanusGraph graph = null; - - - public InMemoryGraph(Builder builder) throws IOException { - /* - * Create a In-memory graph - */ - InputStream is = new FileInputStream(builder.propertyFile); - try { - graph = JanusGraphFactory.open(builder.propertyFile); - - Properties graphProps = new Properties(); - graphProps.load(is); - JanusGraphManagement graphMgt = graph.openManagement(); - if(builder.isSchemaEnabled){ - LOGGER.info("Schema Enabled"); - SchemaGenerator.loadSchemaIntoJanusGraph(graphMgt); - } - JanusGraphTransaction transaction = graph.newTransaction(); - LOGGER.info("Loading snapshot"); - transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation); - transaction.commit(); - - } catch (Exception e) { - // TODO : Changesysout to logger - LOGGER.error( - "ERROR: Could not load datasnapshot to in memory graph. \n" + LogFormatTools.getStackTop(e)); - throw new IllegalStateException("Could not load datasnapshot to in memory graph"); - - } - finally{ - is.close(); - } - - } - - public static class Builder { - private String graphsonLocation = ""; - private String propertyFile = ""; - private boolean isSchemaEnabled = false; - - /* - * Builder constructor doesnt do anything - */ - public Builder() { - //Do nothing - } - - public InMemoryGraph build(String graphsonFile, String propertyFile, boolean isSchemaEnabled) throws IOException { - this.graphsonLocation = graphsonFile; - this.propertyFile = propertyFile; - this.isSchemaEnabled = isSchemaEnabled; - return new InMemoryGraph(this); - } - } - - public JanusGraph getGraph() { - return graph; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/domain/model/AAIResource.java b/aai-core/src/main/java/org/onap/aai/domain/model/AAIResource.java new file mode 100644 index 00000000..3048c119 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/domain/model/AAIResource.java @@ -0,0 +1,674 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.domain.model; + +import com.google.common.collect.Multimap; + +import java.util.ArrayList; +import java.util.Map; + + +public class AAIResource { + private AAIResource parent; + private AAIResources children; + + private AAIResourceKeys aaiResourceKeys; + + private String namespace; // /Network/Vces/Vce/PortGroups/PortGroup/CvlanTags/CvlanTag -> "Network" + + private String resourceType; // node or container + private String resourceClassName; + private String simpleName; // Vce + private String fullName; // /Network/Vces/Vce/PortGroups/PortGroup/CvlanTags/CvlanTag + private String uri; // /network/vces/vce/{vnf-id}/port-groups/port-group/{interface-name}/cvlan-tags/cvlan-tag/{cvlan-tag} + private String apiVersion; + private String relationshipListClass; + private String relationshipUtils; + + private Map<String, String> PropertyDataTypeMap; + private Multimap<String, String> NodeMapIndexedProps; + private Multimap<String, String> NodeAltKey1Props; + private Multimap<String, String> NodeDependencies; + private Multimap<String, String> NodeKeyProps; + private Multimap<String, String> NodeReqProps; + private Multimap<String, String> NodeNameProps; + private Multimap<String, String> NodeUniqueProps; + + // if new dataTypes are added - make sure to update getAllFields() method below + private ArrayList<String> stringFields; + private ArrayList<String> stringListFields; + private ArrayList<String> longFields; + private ArrayList<String> intFields; + private ArrayList<String> shortFields; + private ArrayList<String> booleanFields; + + private ArrayList<String> requiredFields; + private ArrayList<String> orderedFields; + private AAIResource recurseToResource; + private boolean allowDirectWrite; + private boolean allowDirectRead; + private ArrayList<String> autoGenUuidFields; + + /** + * Gets the parent. + * + * @return the parent + */ + public AAIResource getParent() { + return parent; + } + + /** + * Sets the parent. + * + * @param parent the new parent + */ + public void setParent(AAIResource parent) { + this.parent = parent; + } + + /** + * Gets the children. + * + * @return the children + */ + public AAIResources getChildren() { + if (this.children == null) { + this.children = new AAIResources(); + } + return this.children; + } + + /** + * Gets the aai resource keys. + * + * @return the aai resource keys + */ + public AAIResourceKeys getAaiResourceKeys() { + if (aaiResourceKeys == null) { + aaiResourceKeys = new AAIResourceKeys(); + } + return aaiResourceKeys; + } + + /** + * Gets the namespace. + * + * @return the namespace + */ + public String getNamespace() { + return namespace; + } + + /** + * Sets the namespace. + * + * @param namespace the new namespace + */ + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + /** + * Gets the resource type. + * + * @return the resource type + */ + public String getResourceType() { + return resourceType; + } + + /** + * Sets the resource type. + * + * @param resourceType the new resource type + */ + public void setResourceType(String resourceType) { + this.resourceType = resourceType; + } + + /** + * Gets the simple name. + * + * @return the simple name + */ + public String getSimpleName() { + return simpleName; + } + + /** + * Sets the simple name. + * + * @param simpleName the new simple name + */ + public void setSimpleName(String simpleName) { + this.simpleName = simpleName; + } + + /** + * Gets the full name. + * + * @return the full name + */ + public String getFullName() { + return fullName; + } + + /** + * Sets the full name. + * + * @param fullName the new full name + */ + public void setFullName(String fullName) { + this.fullName = fullName; + } + + /** + * Gets the uri. + * + * @return the uri + */ + public String getUri() { + return uri; + } + + /** + * Sets the uri. + * + * @param uri the new uri + */ + public void setUri(String uri) { + this.uri = uri; + } + + /** + * Gets the resource class name. + * + * @return the resource class name + */ + public String getResourceClassName() { + return resourceClassName; + } + + /** + * Sets the resource class name. + * + * @param resourceClassName the new resource class name + */ + public void setResourceClassName(String resourceClassName) { + this.resourceClassName = resourceClassName; + } + + /** + * Gets the property data type map. + * + * @return the property data type map + */ + public Map<String, String> getPropertyDataTypeMap() { + return PropertyDataTypeMap; + } + + /** + * Sets the property data type map. + * + * @param propertyDataTypeMap the property data type map + */ + public void setPropertyDataTypeMap(Map<String, String> propertyDataTypeMap) { + PropertyDataTypeMap = propertyDataTypeMap; + } + + /** + * Gets the node map indexed props. + * + * @return the node map indexed props + */ + public Multimap<String, String> getNodeMapIndexedProps() { + return NodeMapIndexedProps; + } + + /** + * Sets the node map indexed props. + * + * @param nodeMapIndexedProps the node map indexed props + */ + public void setNodeMapIndexedProps(Multimap<String, String> nodeMapIndexedProps) { + NodeMapIndexedProps = nodeMapIndexedProps; + } + + /** + * Gets the node key props. + * + * @return the node key props + */ + public Multimap<String, String> getNodeKeyProps() { + return NodeKeyProps; + } + + /** + * Sets the node key props. + * + * @param nodeKeyProps the node key props + */ + public void setNodeKeyProps(Multimap<String, String> nodeKeyProps) { + this.NodeKeyProps = nodeKeyProps; + } + + /** + * Gets the node name props. + * + * @return the node name props + */ + public Multimap<String, String> getNodeNameProps() { + return NodeNameProps; + } + + /** + * Sets the node name props. + * + * @param nodeNameProps the node name props + */ + public void setNodeNameProps(Multimap<String, String> nodeNameProps) { + + NodeNameProps = nodeNameProps; + } + + /** + * Gets the node unique props. + * + * @return the node unique props + */ + public Multimap<String, String> getNodeUniqueProps() { + return NodeUniqueProps; + } + + /** + * Sets the node unique props. + * + * @param nodeUniqueProps the node unique props + */ + public void setNodeUniqueProps(Multimap<String, String> nodeUniqueProps) { + NodeUniqueProps = nodeUniqueProps; + } + + /** + * Gets the node req props. + * + * @return the node req props + */ + public Multimap<String, String> getNodeReqProps() { + return NodeReqProps; + } + + /** + * Sets the node req props. + * + * @param nodeReqProps the node req props + */ + public void setNodeReqProps(Multimap<String, String> nodeReqProps) { + NodeReqProps = nodeReqProps; + } + + /** + * Gets the api version. + * + * @return the api version + */ + public String getApiVersion() { + return apiVersion; + } + + /** + * Sets the api version. + * + * @param apiVersion the new api version + */ + public void setApiVersion(String apiVersion) { + this.apiVersion = apiVersion; + } + + /** + * Gets the relationship list class. + * + * @return the relationship list class + */ + public String getRelationshipListClass() { + return relationshipListClass; + } + + /** + * Sets the relationship list class. + * + * @param relationshipListClass the new relationship list class + */ + public void setRelationshipListClass(String relationshipListClass) { + this.relationshipListClass = relationshipListClass; + } + + /** + * Gets the relationship utils. + * + * @return the relationship utils + */ + public String getRelationshipUtils() { + return relationshipUtils; + } + + /** + * Sets the relationship utils. + * + * @param relationshipUtils the new relationship utils + */ + public void setRelationshipUtils(String relationshipUtils) { + this.relationshipUtils = relationshipUtils; + } + + /** + * Gets the string fields. + * + * @return the string fields + */ + public ArrayList<String> getStringFields() { + if (this.stringFields == null) { + this.stringFields = new ArrayList<String>(); + } + return this.stringFields; + } + + /** + * Sets the string fields. + * + * @param stringFields the new string fields + */ + public void setStringFields(ArrayList<String> stringFields) { + this.stringFields = stringFields; + } + + /** + * Gets the string list fields. + * + * @return the string list fields + */ + public ArrayList<String> getStringListFields() { + if (this.stringListFields == null) { + this.stringListFields = new ArrayList<String>(); + } + return this.stringListFields; + } + + /** + * Sets the string list fields. + * + * @param stringListFields the new string list fields + */ + public void setStringListFields(ArrayList<String> stringListFields) { + this.stringListFields = stringListFields; + } + + /** + * Gets the long fields. + * + * @return the long fields + */ + public ArrayList<String> getLongFields() { + if (this.longFields == null) { + this.longFields = new ArrayList<String>(); + } + return longFields; + } + + /** + * Sets the long fields. + * + * @param longFields the new long fields + */ + public void setLongFields(ArrayList<String> longFields) { + this.longFields = longFields; + } + + /** + * Gets the int fields. + * + * @return the int fields + */ + public ArrayList<String> getIntFields() { + if (this.intFields == null) { + this.intFields = new ArrayList<String>(); + } + return intFields; + } + + /** + * Sets the int fields. + * + * @param intFields the new int fields + */ + public void setIntFields(ArrayList<String> intFields) { + this.intFields = intFields; + } + + /** + * Gets the short fields. + * + * @return the short fields + */ + public ArrayList<String> getShortFields() { + if (this.shortFields == null) { + this.shortFields = new ArrayList<String>(); + } + return shortFields; + } + + /** + * Sets the short fields. + * + * @param shortFields the new short fields + */ + public void setShortFields(ArrayList<String> shortFields) { + this.shortFields = shortFields; + } + + /** + * Gets the boolean fields. + * + * @return the boolean fields + */ + public ArrayList<String> getBooleanFields() { + if (this.booleanFields == null) { + this.booleanFields = new ArrayList<String>(); + } + return booleanFields; + } + + /** + * Sets the boolean fields. + * + * @param booleanFields the new boolean fields + */ + public void setBooleanFields(ArrayList<String> booleanFields) { + this.booleanFields = booleanFields; + } + + /** + * Gets the required fields. + * + * @return the required fields + */ + public ArrayList<String> getRequiredFields() { + if (this.requiredFields == null) { + this.requiredFields = new ArrayList<String>(); + } + return requiredFields; + } + + /** + * Sets the required fields. + * + * @param requiredFields the new required fields + */ + public void setRequiredFields(ArrayList<String> requiredFields) { + this.requiredFields = requiredFields; + } + + /** + * Gets the ordered fields. + * + * @return the ordered fields + */ + public ArrayList<String> getOrderedFields() { + if (this.orderedFields == null) { + this.orderedFields = new ArrayList<String>(); + } + return this.orderedFields; + } + + /** + * Gets the all fields. + * + * @return the all fields + */ + public ArrayList<String> getAllFields() { + + ArrayList<String> allFields = new ArrayList<String>(); + allFields.addAll(getBooleanFields()); + allFields.addAll(getStringListFields()); + allFields.addAll(getStringFields()); + allFields.addAll(getIntFields()); + allFields.addAll(getLongFields()); + allFields.addAll(getShortFields()); + + return allFields; + } + + /** + * Gets the plural name. + * + * @return the plural name + */ + public String getPluralName() { + + if (simpleName.contains("List") || simpleName.contains("-list") ) + return ""; + String[] fullNameList = getFullName().split("/"); + return fullNameList[fullNameList.length - 2]; + } + + /** + * Sets the node alt key 1 props. + * + * @param _dbRulesNodeAltKey1Props the db rules node alt key 1 props + */ + public void setNodeAltKey1Props(Multimap<String, String> _dbRulesNodeAltKey1Props) { + this.NodeAltKey1Props = _dbRulesNodeAltKey1Props; + } + + /** + * Gets the node alt key 1 props. + * + * @return the node alt key 1 props + */ + public Multimap<String,String> getNodeAltKey1Props() { + return this.NodeAltKey1Props; + } + + /** + * Sets the node dependencies. + * + * @param _dbRulesNodeDependencies the db rules node dependencies + */ + public void setNodeDependencies(Multimap<String, String> _dbRulesNodeDependencies) { + this.NodeDependencies = _dbRulesNodeDependencies; + } + + /** + * Gets the node dependencies. + * + * @return the node dependencies + */ + public Multimap<String,String> getNodeDependencies() { + return this.NodeDependencies; + } + + /** + * Gets the recurse to resource. + * + * @return the recurse to resource + */ + public AAIResource getRecurseToResource() { + return this.recurseToResource; + } + + /** + * Sets the recurse to resource. + * + * @param ancestor the new recurse to resource + */ + public void setRecurseToResource(AAIResource ancestor) { + this.recurseToResource = ancestor; + + } + + /** + * Sets the allow direct write. + * + * @param allowDirectWrite the new allow direct write + */ + public void setAllowDirectWrite(boolean allowDirectWrite) { + this.allowDirectWrite = allowDirectWrite; + } + + /** + * Checks if is allow direct write. + * + * @return true, if is allow direct write + */ + public boolean isAllowDirectWrite() { + return this.allowDirectWrite; + } + + /** + * Sets the allow direct read. + * + * @param allowDirectRead the new allow direct read + */ + public void setAllowDirectRead(boolean allowDirectRead) { + this.allowDirectRead = allowDirectRead; + } + + /** + * Checks if is allow direct read. + * + * @return true, if is allow direct read + */ + public boolean isAllowDirectRead() { + return this.allowDirectRead; + } + + /** + * Gets the auto gen uuid fields. + * + * @return the auto gen uuid fields + */ + public ArrayList<String> getAutoGenUuidFields() { + if (this.autoGenUuidFields == null) { + this.autoGenUuidFields = new ArrayList<String>(); + } + return this.autoGenUuidFields; + } +}
\ No newline at end of file diff --git a/aai-core/src/main/java/org/onap/aai/domain/model/AAIResourceKey.java b/aai-core/src/main/java/org/onap/aai/domain/model/AAIResourceKey.java new file mode 100644 index 00000000..c85a17db --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/domain/model/AAIResourceKey.java @@ -0,0 +1,100 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.domain.model; + +public class AAIResourceKey { + private String keyName; + private String keyType; + private String pathParamName; + private String dnCamKeyName; + + /** + * Gets the key name. + * + * @return the key name + */ + public String getKeyName() { + return keyName; + } + + /** + * Sets the key name. + * + * @param keyName the new key name + */ + public void setKeyName(String keyName) { + this.keyName = keyName; + } + + /** + * Gets the key type. + * + * @return the key type + */ + public String getKeyType() { + return keyType; + } + + /** + * Sets the key type. + * + * @param t the new key type + */ + public void setKeyType(String t) { + this.keyType = t; + } + + /** + * Gets the path param name. + * + * @return the path param name + */ + public String getPathParamName() { + return pathParamName; + } + + /** + * Sets the path param name. + * + * @param pathParamName the new path param name + */ + public void setPathParamName(String pathParamName) { + this.pathParamName = pathParamName; + } + + /** + * Gets the dn cam key name. + * + * @return the dn cam key name + */ + public String getDnCamKeyName() { + return dnCamKeyName; + } + + /** + * Sets the dn cam key name. + * + * @param dnCamKeyName the new dn cam key name + */ + public void setDnCamKeyName(String dnCamKeyName) { + this.dnCamKeyName = dnCamKeyName; + } + +} diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/CompareByName.java b/aai-core/src/main/java/org/onap/aai/domain/model/AAIResourceKeys.java index 829239da..a863eb47 100644 --- a/aai-core/src/main/java/org/onap/aai/db/schema/CompareByName.java +++ b/aai-core/src/main/java/org/onap/aai/domain/model/AAIResourceKeys.java @@ -17,19 +17,23 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.aai.db.schema; +package org.onap.aai.domain.model; -import java.util.Comparator; +import java.util.ArrayList; +import java.util.List; -public class CompareByName implements Comparator<Named>{ +public class AAIResourceKeys { + private List<AAIResourceKey> aaiResourceKey; /** - * {@inheritDoc} + * Gets the aai resource key. + * + * @return the aai resource key */ - @Override - public int compare(Named o1, Named o2) { - return o1.getName().compareTo(o2.getName()); + public List<AAIResourceKey> getAaiResourceKey() { + if (aaiResourceKey == null) { + aaiResourceKey = new ArrayList<AAIResourceKey>(); + } + return aaiResourceKey; } - - } diff --git a/aai-core/src/main/java/org/onap/aai/domain/model/AAIResources.java b/aai-core/src/main/java/org/onap/aai/domain/model/AAIResources.java new file mode 100644 index 00000000..15a8852a --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/domain/model/AAIResources.java @@ -0,0 +1,85 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.domain.model; + +import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; + +import java.util.HashMap; + +public class AAIResources { + + private DynamicJAXBContext jaxbContext; + + private HashMap<String, AAIResource> aaiResources; + private HashMap<String, AAIResource> resourceLookup; + + + /** + * Gets the aai resources. + * + * @return the aai resources + */ + public HashMap<String, AAIResource> getAaiResources() { + if (aaiResources == null) { + aaiResources = new HashMap<String, AAIResource>(); + } + return aaiResources; + } + + /** + * Gets the jaxb context. + * + * @return the jaxb context + */ + public DynamicJAXBContext getJaxbContext() { + return jaxbContext; + } + + /** + * Sets the jaxb context. + * + * @param jaxbContext the new jaxb context + */ + public void setJaxbContext(DynamicJAXBContext jaxbContext) { + this.jaxbContext = jaxbContext; + } + + /** + * Gets the resource lookup. + * + * @return the resource lookup + */ + public HashMap<String, AAIResource> getResourceLookup() { + if (resourceLookup == null) { + resourceLookup = new HashMap<String, AAIResource>(); + } + return resourceLookup; + } + + /** + * Sets the resource lookup. + * + * @param resourceLookup the resource lookup + */ + public void setResourceLookup(HashMap<String, AAIResource> resourceLookup) { + this.resourceLookup = resourceLookup; + } + +}
\ No newline at end of file diff --git a/aai-core/src/main/java/org/onap/aai/domain/notificationEvent/NotificationEvent.java b/aai-core/src/main/java/org/onap/aai/domain/notificationEvent/NotificationEvent.java index 15044dc5..dca48496 100644 --- a/aai-core/src/main/java/org/onap/aai/domain/notificationEvent/NotificationEvent.java +++ b/aai-core/src/main/java/org/onap/aai/domain/notificationEvent/NotificationEvent.java @@ -91,7 +91,7 @@ public class NotificationEvent { @XmlElement(name = "cambria.partition") protected String cambriaPartition; @XmlElement(name = "event-header") - protected NotificationEvent.EventHeader eventHeader; + protected EventHeader eventHeader; @XmlAnyElement(lax = true) protected Object entity; @@ -100,10 +100,10 @@ public class NotificationEvent { * * @return * possible object is - * {@link NotificationEvent.EventHeader } + * {@link EventHeader } * */ - public NotificationEvent.EventHeader getEventHeader() { + public EventHeader getEventHeader() { return eventHeader; } @@ -112,10 +112,10 @@ public class NotificationEvent { * * @param value * allowed object is - * {@link NotificationEvent.EventHeader } + * {@link EventHeader } * */ - public void setEventHeader(NotificationEvent.EventHeader value) { + public void setEventHeader(EventHeader value) { this.eventHeader = value; } diff --git a/aai-core/src/main/java/org/onap/aai/domain/restPolicyException/Fault.java b/aai-core/src/main/java/org/onap/aai/domain/restPolicyException/Fault.java index f84d4418..69856873 100644 --- a/aai-core/src/main/java/org/onap/aai/domain/restPolicyException/Fault.java +++ b/aai-core/src/main/java/org/onap/aai/domain/restPolicyException/Fault.java @@ -95,17 +95,17 @@ import javax.xml.bind.annotation.XmlType; public class Fault { @XmlElement(required = true) - protected Fault.RequestError requestError; + protected RequestError requestError; /** * Gets the value of the requestError property. * * @return * possible object is - * {@link Fault.RequestError } + * {@link RequestError } * */ - public Fault.RequestError getRequestError() { + public RequestError getRequestError() { return requestError; } @@ -114,10 +114,10 @@ public class Fault { * * @param value * allowed object is - * {@link Fault.RequestError } + * {@link RequestError } * */ - public void setRequestError(Fault.RequestError value) { + public void setRequestError(RequestError value) { this.requestError = value; } @@ -170,17 +170,17 @@ public class Fault { public static class RequestError { @XmlElement(required = true) - protected Fault.RequestError.PolicyException policyException; + protected PolicyException policyException; /** * Gets the value of the policyException property. * * @return * possible object is - * {@link Fault.RequestError.PolicyException } + * {@link PolicyException } * */ - public Fault.RequestError.PolicyException getPolicyException() { + public PolicyException getPolicyException() { return policyException; } @@ -189,10 +189,10 @@ public class Fault { * * @param value * allowed object is - * {@link Fault.RequestError.PolicyException } + * {@link PolicyException } * */ - public void setPolicyException(Fault.RequestError.PolicyException value) { + public void setPolicyException(PolicyException value) { this.policyException = value; } @@ -241,7 +241,7 @@ public class Fault { @XmlElement(required = true) protected String text; @XmlElement(required = true) - protected Fault.RequestError.PolicyException.Variables variables; + protected Variables variables; /** * Gets the value of the messageId property. @@ -296,10 +296,10 @@ public class Fault { * * @return * possible object is - * {@link Fault.RequestError.PolicyException.Variables } + * {@link Variables } * */ - public Fault.RequestError.PolicyException.Variables getVariables() { + public Variables getVariables() { return variables; } @@ -308,10 +308,10 @@ public class Fault { * * @param value * allowed object is - * {@link Fault.RequestError.PolicyException.Variables } + * {@link Variables } * */ - public void setVariables(Fault.RequestError.PolicyException.Variables value) { + public void setVariables(Variables value) { this.variables = value; } diff --git a/aai-core/src/main/java/org/onap/aai/domain/restResponseInfo/Info.java b/aai-core/src/main/java/org/onap/aai/domain/restResponseInfo/Info.java index aec2ffb4..49d6190e 100644 --- a/aai-core/src/main/java/org/onap/aai/domain/restResponseInfo/Info.java +++ b/aai-core/src/main/java/org/onap/aai/domain/restResponseInfo/Info.java @@ -94,17 +94,17 @@ import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "Info") public class Info { - protected Info.ResponseMessages responseMessages; + protected ResponseMessages responseMessages; /** * Gets the value of the responseMessages property. * * @return * possible object is - * {@link Info.ResponseMessages } + * {@link ResponseMessages } * */ - public Info.ResponseMessages getResponseMessages() { + public ResponseMessages getResponseMessages() { return responseMessages; } @@ -113,10 +113,10 @@ public class Info { * * @param value * allowed object is - * {@link Info.ResponseMessages } + * {@link ResponseMessages } * */ - public void setResponseMessages(Info.ResponseMessages value) { + public void setResponseMessages(ResponseMessages value) { this.responseMessages = value; } @@ -168,7 +168,7 @@ public class Info { }) public static class ResponseMessages { - protected List<Info.ResponseMessages.ResponseMessage> responseMessage; + protected List<ResponseMessage> responseMessage; /** * Gets the value of the responseMessage property. @@ -188,13 +188,13 @@ public class Info { * * <p> * Objects of the following type(s) are allowed in the list - * {@link Info.ResponseMessages.ResponseMessage } + * {@link ResponseMessage } * * @return the response message */ - public List<Info.ResponseMessages.ResponseMessage> getResponseMessage() { + public List<ResponseMessage> getResponseMessage() { if (responseMessage == null) { - responseMessage = new ArrayList<Info.ResponseMessages.ResponseMessage>(); + responseMessage = new ArrayList<ResponseMessage>(); } return this.responseMessage; } @@ -244,7 +244,7 @@ public class Info { @XmlElement(required = true) protected String text; @XmlElement(required = true) - protected Info.ResponseMessages.ResponseMessage.Variables variables; + protected Variables variables; /** * Gets the value of the messageId property. @@ -299,10 +299,10 @@ public class Info { * * @return * possible object is - * {@link Info.ResponseMessages.ResponseMessage.Variables } + * {@link Variables } * */ - public Info.ResponseMessages.ResponseMessage.Variables getVariables() { + public Variables getVariables() { return variables; } @@ -311,10 +311,10 @@ public class Info { * * @param value * allowed object is - * {@link Info.ResponseMessages.ResponseMessage.Variables } + * {@link Variables } * */ - public void setVariables(Info.ResponseMessages.ResponseMessage.Variables value) { + public void setVariables(Variables value) { this.variables = value; } diff --git a/aai-core/src/main/java/org/onap/aai/domain/restServiceException/Fault.java b/aai-core/src/main/java/org/onap/aai/domain/restServiceException/Fault.java index 0f7cc48c..dd0df4f0 100644 --- a/aai-core/src/main/java/org/onap/aai/domain/restServiceException/Fault.java +++ b/aai-core/src/main/java/org/onap/aai/domain/restServiceException/Fault.java @@ -95,17 +95,17 @@ import javax.xml.bind.annotation.XmlType; public class Fault { @XmlElement(required = true) - protected Fault.RequestError requestError; + protected RequestError requestError; /** * Gets the value of the requestError property. * * @return * possible object is - * {@link Fault.RequestError } + * {@link RequestError } * */ - public Fault.RequestError getRequestError() { + public RequestError getRequestError() { return requestError; } @@ -114,10 +114,10 @@ public class Fault { * * @param value * allowed object is - * {@link Fault.RequestError } + * {@link RequestError } * */ - public void setRequestError(Fault.RequestError value) { + public void setRequestError(RequestError value) { this.requestError = value; } @@ -170,17 +170,17 @@ public class Fault { public static class RequestError { @XmlElement(required = true) - protected Fault.RequestError.ServiceException serviceException; + protected ServiceException serviceException; /** * Gets the value of the serviceException property. * * @return * possible object is - * {@link Fault.RequestError.ServiceException } + * {@link ServiceException } * */ - public Fault.RequestError.ServiceException getServiceException() { + public ServiceException getServiceException() { return serviceException; } @@ -189,10 +189,10 @@ public class Fault { * * @param value * allowed object is - * {@link Fault.RequestError.ServiceException } + * {@link ServiceException } * */ - public void setServiceException(Fault.RequestError.ServiceException value) { + public void setServiceException(ServiceException value) { this.serviceException = value; } @@ -241,7 +241,7 @@ public class Fault { @XmlElement(required = true) protected String text; @XmlElement(required = true) - protected Fault.RequestError.ServiceException.Variables variables; + protected Variables variables; /** * Gets the value of the messageId property. @@ -296,10 +296,10 @@ public class Fault { * * @return * possible object is - * {@link Fault.RequestError.ServiceException.Variables } + * {@link Variables } * */ - public Fault.RequestError.ServiceException.Variables getVariables() { + public Variables getVariables() { return variables; } @@ -308,10 +308,10 @@ public class Fault { * * @param value * allowed object is - * {@link Fault.RequestError.ServiceException.Variables } + * {@link Variables } * */ - public void setVariables(Fault.RequestError.ServiceException.Variables value) { + public void setVariables(Variables value) { this.variables = value; } diff --git a/aai-core/src/main/java/org/onap/aai/exceptions/AAIException.java b/aai-core/src/main/java/org/onap/aai/exceptions/AAIException.java index 375e00fe..f2253a30 100644 --- a/aai-core/src/main/java/org/onap/aai/exceptions/AAIException.java +++ b/aai-core/src/main/java/org/onap/aai/exceptions/AAIException.java @@ -84,6 +84,7 @@ public class AAIException extends Exception { try { this.errorObject = ErrorLogHelper.getErrorObject(getCode()); + errorObject.setDetails(details); } catch (ErrorObjectNotFoundException e) { throw new RuntimeException("Failed to instantiate AAIException with code=" + getCode() + " - update error.properties before using this exception code"); diff --git a/aai-core/src/main/java/org/onap/aai/extensions/OrphanLInterfaceHandler.java b/aai-core/src/main/java/org/onap/aai/extensions/OrphanLInterfaceHandler.java deleted file mode 100644 index 7a14a24d..00000000 --- a/aai-core/src/main/java/org/onap/aai/extensions/OrphanLInterfaceHandler.java +++ /dev/null @@ -1,110 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.extensions; - -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; - -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Introspector; -import org.onap.aai.introspection.Loader; -import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; -import org.onap.aai.parsers.query.QueryParser; -import org.onap.aai.query.builder.QueryBuilder; -import org.onap.aai.rest.db.DBRequest; -import org.onap.aai.restcore.HttpMethod; -import org.onap.aai.serialization.db.EdgeType; - -public class OrphanLInterfaceHandler { - - private QueryBuilder<Vertex> createLInterfaceQuery(AAIExtensionMap aaiReqMap, Introspector newvceObj) throws AAIException { - Introspector uplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface"); - - Introspector customerUplinkLInterfaceTraversalIntro = aaiReqMap.getLoader().introspectorFromName("l-interface"); - - Introspector logLinkIntroForTraversal = aaiReqMap.getLoader().introspectorFromName("logical-link"); - - QueryBuilder<Vertex> query = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder() - .exactMatchQuery(newvceObj) - .createEdgeTraversal(EdgeType.TREE, newvceObj, uplinkLInterfaceTraversalIntro) - .getVerticesByProperty("interface-role", "UPLINK") - .createEdgeTraversal(EdgeType.COUSIN, uplinkLInterfaceTraversalIntro, logLinkIntroForTraversal) - .createEdgeTraversal(EdgeType.COUSIN, logLinkIntroForTraversal, customerUplinkLInterfaceTraversalIntro) - .getVerticesByProperty("interface-role", "CUSTOMER-UPLINK").dedup(); - return query; - } - - private URI buildLInterfaceURI(Vertex linterface, AAIExtensionMap aaiReqMap) throws UnsupportedEncodingException, AAIException, URISyntaxException { - Loader loader = aaiReqMap.getLoader(); - Introspector lint = loader.introspectorFromName("l-interface"); - lint.setValue("interface-name", (String)linterface.property("interface-name").value()); - String lintSegment = lint.getURI(); - - Introspector lagInterfaceForTrav = loader.introspectorFromName("lag-interface"); - QueryBuilder<Vertex> lagIntQuery = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder() - .exactMatchQuery(lint) - .createEdgeTraversal(EdgeType.TREE, linterface, lagInterfaceForTrav).dedup(); - List<Vertex> lagInterfaces = lagIntQuery.toList(); - if (lagInterfaces.isEmpty()) { - throw new AAIException("AAI_6114"); - } else if (lagInterfaces.size() > 1) { - throw new AAIException("AAI_6140"); - } - Vertex lagInt = lagInterfaces.get(0); - lagInterfaceForTrav.setValue("interface-name", (String)lagInt.property("interface-name").value()); - String lagSegment = lagInterfaceForTrav.getURI(); - - Introspector gvVPEforTrav = loader.introspectorFromName("generic-vnf"); - QueryBuilder<Vertex> gvVPEquery = aaiReqMap.getTransactionalGraphEngine().getQueryBuilder() - .exactMatchQuery(lagInterfaceForTrav) - .createEdgeTraversal(EdgeType.TREE, lagInterfaceForTrav, gvVPEforTrav).dedup(); - List<Vertex> genvnfs = gvVPEquery.toList(); - if (genvnfs.isEmpty()) { - throw new AAIException("AAI_6114"); - } else if (genvnfs.size() > 1) { - throw new AAIException("AAI_6140"); - } - Vertex genvnf = genvnfs.get(0); - gvVPEforTrav.setValue("vnf-id", (String)genvnf.property("vnf-id").value()); - String gvSegment = gvVPEforTrav.getURI(); - - return new URI(gvSegment + lagSegment + lintSegment); - } - - public List<DBRequest> createOrphanLInterfaceDelRequests(AAIExtensionMap aaiReqMap, Introspector newvce) throws AAIException, UnsupportedEncodingException, URISyntaxException{ - List<DBRequest> requests = new ArrayList<>(); - QueryBuilder<Vertex> query = createLInterfaceQuery(aaiReqMap, newvce); - List<Vertex> linterfaces = query.toList(); - - for (Vertex lint : linterfaces) { - URI lintURI = buildLInterfaceURI(lint, aaiReqMap); - QueryParser parser = createLInterfaceQuery(aaiReqMap, newvce).createQueryFromObjectName("l-interface"); - DBRequest originalDbRequest = aaiReqMap.getDbRequest(); - DBRequest request = new DBRequest.Builder(HttpMethod.DELETE, lintURI, parser, newvce, originalDbRequest.getHeaders(), originalDbRequest.getInfo(), originalDbRequest.getTransactionId()).build(); - requests.add(request); - } - - return requests; - } -} diff --git a/aai-core/src/main/java/org/onap/aai/ingestModel/CreateWidgetModels.java b/aai-core/src/main/java/org/onap/aai/ingestModel/CreateWidgetModels.java index 2c13799d..d5921c19 100644 --- a/aai-core/src/main/java/org/onap/aai/ingestModel/CreateWidgetModels.java +++ b/aai-core/src/main/java/org/onap/aai/ingestModel/CreateWidgetModels.java @@ -28,11 +28,12 @@ import java.util.UUID; import javax.xml.transform.stream.StreamSource; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.introspection.LoaderFactory; import org.onap.aai.introspection.ModelType; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.util.AAIConfig; import org.onap.aai.util.AAIConstants; @@ -73,8 +74,7 @@ public class CreateWidgetModels System.exit(0); } - - Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.valueOf(_apiVersion)); + Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY, new SchemaVersion(_apiVersion)); // iterate the collection of resources diff --git a/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java b/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java index 70721217..c7520dd4 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/Introspector.java @@ -30,12 +30,14 @@ import org.onap.aai.logging.LogFormatTools; import org.onap.aai.restcore.MediaType; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.workarounds.NamingExceptions; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.util.*; + public abstract class Introspector implements Cloneable { private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(Introspector.class); @@ -605,7 +607,7 @@ public abstract class Introspector implements Cloneable { } - public abstract Version getVersion(); + public abstract SchemaVersion getVersion(); public Loader getLoader() { return this.loader; } diff --git a/aai-core/src/main/java/org/onap/aai/introspection/IntrospectorFactory.java b/aai-core/src/main/java/org/onap/aai/introspection/IntrospectorFactory.java index 4bcbd1d1..93cd2ee1 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/IntrospectorFactory.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/IntrospectorFactory.java @@ -26,17 +26,12 @@ public class IntrospectorFactory { * * @param type the type * @param o the o - * @param llBuilder the ll builder * @return the introspector */ public static Introspector newInstance(ModelType type, Object o) { if (type.equals(ModelType.MOXY)) { return new MoxyStrategy(o); - } else if (type.equals(ModelType.POJO)) { - return new PojoStrategy(o); - } else if (type.equals(ModelType.JSON)) { - return new JSONStrategy(o); } else { throw new IllegalArgumentException("Unknown class type: " + type); } @@ -49,7 +44,6 @@ public class IntrospectorFactory { * @param type the type * @param o the o * @param namedType the named type - * @param llBuilder the ll builder * @return the introspector */ public static Introspector newInstance(ModelType type, Object o, String namedType) { diff --git a/aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java b/aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java index 09ff7d34..b1447d8f 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java @@ -19,11 +19,10 @@ */ package org.onap.aai.introspection; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import org.json.simple.JSONObject; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; +import org.onap.aai.setup.SchemaVersion; import java.io.UnsupportedEncodingException; import java.util.List; @@ -32,7 +31,6 @@ import java.util.Set; import java.util.UUID; public class JSONStrategy extends Introspector { - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(JSONStrategy.class); private JSONObject json = null; private String namedType = ""; @@ -159,7 +157,6 @@ public class JSONStrategy extends Introspector { try { return this.getClass(name).newInstance(); } catch (InstantiationException | IllegalAccessException e) { - LOGGER.error(e.getMessage(),e); return null; } } @@ -169,7 +166,6 @@ public class JSONStrategy extends Introspector { try { return this.getGenericTypeClass(name).newInstance(); } catch (InstantiationException | IllegalAccessException e) { - LOGGER.error(e.getMessage(),e); return null; } } @@ -355,7 +351,7 @@ public class JSONStrategy extends Introspector { } @Override - public Version getVersion() { + public SchemaVersion getVersion() { // TODO Auto-generated method stub return null; } diff --git a/aai-core/src/main/java/org/onap/aai/introspection/Loader.java b/aai-core/src/main/java/org/onap/aai/introspection/Loader.java index ea323c5b..3a69e56e 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/Loader.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/Loader.java @@ -22,12 +22,13 @@ package org.onap.aai.introspection; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.introspection.exceptions.AAIUnmarshallingException; import org.onap.aai.restcore.MediaType; +import org.onap.aai.setup.SchemaVersion; import java.util.Map; public abstract class Loader { - private final Version version; + private final SchemaVersion version; private final ModelType modelType; /** @@ -35,9 +36,8 @@ public abstract class Loader { * * @param version the version * @param modelType the model type - * @param llBuilder the ll builder */ - public Loader (Version version, ModelType modelType) { + public Loader (SchemaVersion version, ModelType modelType) { this.version = version; this.modelType = modelType; } @@ -47,7 +47,7 @@ public abstract class Loader { * * @param version the version */ - protected abstract void process(Version version); + protected abstract void process(SchemaVersion version); /** * Object from name. @@ -103,7 +103,7 @@ public abstract class Loader { * * @return the version */ - public Version getVersion() { + public SchemaVersion getVersion() { return this.version; } diff --git a/aai-core/src/main/java/org/onap/aai/introspection/LoaderFactory.java b/aai-core/src/main/java/org/onap/aai/introspection/LoaderFactory.java index 12255d80..96fd7dbd 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/LoaderFactory.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/LoaderFactory.java @@ -19,25 +19,56 @@ */ package org.onap.aai.introspection; +import java.util.Map; + +import org.onap.aai.setup.SchemaVersion; +import org.springframework.beans.factory.annotation.Autowired; + public class LoaderFactory { + @Autowired + public Map<SchemaVersion, MoxyLoader> moxyLoaderInstance; + + public LoaderFactory(Map<SchemaVersion, MoxyLoader> moxyLoaderInstance) { + this.moxyLoaderInstance = moxyLoaderInstance; + } + /** * Creates a new Loader object. * - * @param type the type - * @param version the version - * @param llBuilder the ll builder + * @param type + * the type + * @param version + * the version + * @param llBuilder + * the ll builder * @return the loader */ - public static Loader createLoaderForVersion(ModelType type, Version version) { - + public Loader createLoaderForVersion(ModelType type, SchemaVersion version) { + + if (type.equals(ModelType.MOXY)) { + return getMoxyLoaderInstance().get(version); + } + + return null; + + } + + public Loader getLoaderStrategy(ModelType type, SchemaVersion version) { + if (type.equals(ModelType.MOXY)) { - return new MoxyLoader(version); - } else if (type.equals(ModelType.POJO)) { - return new PojoLoader(version); + return getMoxyLoaderInstance().get(version); } - return null; - + } + + public Map<SchemaVersion, MoxyLoader> getMoxyLoaderInstance() { + return moxyLoaderInstance; + } + + public void setMoxyLoaderInstance(Map<SchemaVersion, MoxyLoader> moxyLoaderInstance) { + this.moxyLoaderInstance = moxyLoaderInstance; + } + } diff --git a/aai-core/src/main/java/org/onap/aai/introspection/ModelInjestor.java b/aai-core/src/main/java/org/onap/aai/introspection/ModelInjestor.java deleted file mode 100644 index 788500ab..00000000 --- a/aai-core/src/main/java/org/onap/aai/introspection/ModelInjestor.java +++ /dev/null @@ -1,197 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.introspection; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.eclipse.persistence.dynamic.DynamicType; -import org.eclipse.persistence.jaxb.JAXBContextProperties; -import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; -import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory; -import org.onap.aai.util.AAIConstants; - -import javax.xml.bind.JAXBException; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class ModelInjestor { - - private Map<Version, DynamicJAXBContext> versionContextMap = new HashMap<>(); - private static final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\."); - private static final Pattern uriPattern = Pattern.compile("(v\\d+)\\/"); - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ModelInjestor.class); - - - /** - * Instantiates a new model injestor. - */ - private ModelInjestor() { - try { - injestModels(); - } catch (FileNotFoundException | JAXBException e) { - throw new RuntimeException(e); - } - } - - private static class Helper { - private static final ModelInjestor INSTANCE = new ModelInjestor(); - } - - /** - * Gets the single instance of ModelInjestor. - * - * @return single instance of ModelInjestor - */ - public synchronized static ModelInjestor getInstance() { - return Helper.INSTANCE; - } - - /** - * Injest models. - * - * @throws FileNotFoundException the file not found exception - * @throws JAXBException the JAXB exception - */ - private void injestModels() throws FileNotFoundException, JAXBException { - - for (Version version : Version.values()) { - this.injestModel(version); - } - } - - /** - * Injest model. - * - * @param version the version - * @throws JAXBException the JAXB exception - * @throws FileNotFoundException the file not found exception - */ - private void injestModel (Version version) throws JAXBException, FileNotFoundException { - String fileName = this.getOXMFileName(version); - - File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName); - - // Check if the file exists on the path and if it doesn't exist then - // Using classloader makes it easy to have a different oxm file - // for unit testing the oxm files otherwise, you will be - // stuck with using the main oxm for even the testing - - InputStream iStream; - if(oxmFile.exists()){ - LOGGER.info("Oxm file exists on the system {}", oxmFile); - iStream = new FileInputStream(oxmFile); - } else { - LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile); - iStream = getClass().getClassLoader().getResourceAsStream(fileName); - } - - Map<String, Object> properties = new HashMap<String, Object>(); - properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, iStream); - final DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromOXM(this.getClass().getClassLoader(), properties); - versionContextMap.put(version, jaxbContext); - } - - /** - * Gets the version from class name. - * - * @param classname the classname - * @return the version from class name - */ - public Version getVersionFromClassName (String classname) { - Matcher m = classNamePattern.matcher(classname); - String version = "v12"; - if (m.find()) { - version = m.group(1); - } - - return Version.valueOf(version); - } - - /** - * Gets the context for URI. - * - * @param uri the uri - * @return the context for URI - */ - public DynamicJAXBContext getContextForURI(String uri) { - DynamicJAXBContext result = null; - Matcher m = uriPattern.matcher(uri); - Version version = null; - if (m.find()) { - version = Version.valueOf(m.group(1)); - result = versionContextMap.get(version); - } - - return result; - } - - /** - * Gets the context for version. - * - * @param version the version - * @return the context for version - */ - public DynamicJAXBContext getContextForVersion(Version version) { - DynamicJAXBContext result = null; - - result = versionContextMap.get(version); - - - return result; - } - - /** - * Gets the dynamic type for class name. - * - * @param classname the classname - * @return the dynamic type for class name - */ - public DynamicType getDynamicTypeForClassName(String classname) { - DynamicType result = null; - DynamicJAXBContext context = null; - - Version version = this.getVersionFromClassName(classname); - - context = versionContextMap.get(version); - - if (context != null) { - result = context.getDynamicType(classname); - } - - return result; - } - - public String getOXMFileName(Version v) { - // Changed the /oxm/aai_oxm_*.xml to oxm/aai_oxm_*.xml - // Modified to load from input stream using getClass().getClassLoader() - // As this will be able to relatively get the oxm if the oxm file is there - // So if there is a src/main/resources/oxm/ and src/test/resources/oxm, the - // test oxm will end up being used for tests and src oxm for source files - // Don't change this unless you understand the details specified - return "oxm/aai_oxm_" + v.toString() + ".xml"; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java b/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java index 63b9a12c..82504550 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/MoxyLoader.java @@ -23,6 +23,7 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.common.base.CaseFormat; import com.google.common.collect.ImmutableMap; + import org.eclipse.persistence.dynamic.DynamicEntity; import org.eclipse.persistence.jaxb.UnmarshallerProperties; import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; @@ -31,19 +32,13 @@ import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.introspection.exceptions.AAIUnmarshallingException; import org.onap.aai.logging.ErrorLogHelper; import org.onap.aai.logging.LogFormatTools; +import org.onap.aai.nodes.NodeIngestor; import org.onap.aai.restcore.MediaType; -import org.onap.aai.util.AAIConstants; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.workarounds.NamingExceptions; -import org.w3c.dom.Document; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import javax.xml.XMLConstants; +import org.springframework.stereotype.Component; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.stream.StreamSource; import java.io.*; import java.util.HashSet; @@ -56,17 +51,20 @@ public class MoxyLoader extends Loader { private EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyLoader.class); private Map<String, Introspector> allObjs = null; - /** - * Instantiates a new moxy loader. - * - * @param version the version - * @param llBuilder the ll builder - */ - protected MoxyLoader(Version version) { + private Map<SchemaVersion, MoxyLoader> moxyLoaderFactory; + + private NodeIngestor nodeIngestor; + + public MoxyLoader(SchemaVersion version, NodeIngestor nodeIngestor) { super(version, ModelType.MOXY); + this.nodeIngestor = nodeIngestor; process(version); } + public MoxyLoader getMoxyLoader(SchemaVersion v) { + return moxyLoaderFactory.get(v); + + } /** * {@inheritDoc} * @throws AAIUnknownObjectException @@ -112,9 +110,11 @@ public class MoxyLoader extends Loader { * {@inheritDoc} */ @Override - protected void process(Version version) { - ModelInjestor injestor = ModelInjestor.getInstance(); - jaxbContext = injestor.getContextForVersion(version); + protected void process(SchemaVersion version) { + /* + * We need to have just same JaxbContext for each version + */ + jaxbContext = nodeIngestor.getContextForVersion(version); } @@ -165,34 +165,12 @@ public class MoxyLoader extends Loader { } private Set<String> objectsInVersion() { - final Set<String> result = new HashSet<>(); + Set<String> result = new HashSet<>(); try { - final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); - final String fileName = ModelInjestor.getInstance().getOXMFileName(getVersion()); - - docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - - final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); + result = nodeIngestor.getObjectsInVersion(getVersion()); - InputStream inputStream; - File oxmFile = new File(AAIConstants.AAI_HOME_ETC + fileName); - - if(oxmFile.exists()){ - LOGGER.info("Oxm file exists on the system {}", oxmFile); - inputStream = new FileInputStream(oxmFile); - } else { - LOGGER.warn("Unable to find oxm file {} on the system so using classloader", oxmFile); - inputStream = getClass().getClassLoader().getResourceAsStream(fileName); - } - - final Document doc = docBuilder.parse(inputStream); - final NodeList list = doc.getElementsByTagName("java-type"); - - for (int i = 0; i < list.getLength(); i++) { - result.add(list.item(i).getAttributes().getNamedItem("name").getNodeValue()); - } - } catch (ParserConfigurationException | SAXException | IOException e) { + } catch (Exception e) { LOGGER.warn("Exception while enumerating objects for API version " + getVersion() + " (returning partial results) " + LogFormatTools.getStackTop(e)); } @@ -204,4 +182,19 @@ public class MoxyLoader extends Loader { return this.jaxbContext; } + /* + * Im keeping this for now - Just in case + */ + /*private static class Helper { + private static final Map<SchemaVersion, MoxyLoader> INSTANCEMAP = new ConcurrentHashMap<>(); + + private Helper() {} + + private static MoxyLoader getLoaderBySchemaVersion(SchemaVersion v) { + if (!INSTANCEMAP.containsKey(v)) { + INSTANCEMAP.put(v, new MoxyLoader(v, nodeIngestor)); + } + return INSTANCEMAP.get(v); + } + }*/ } diff --git a/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java b/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java index 05d4bcaf..ecf31253 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/MoxyStrategy.java @@ -23,6 +23,8 @@ import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.common.base.CaseFormat; import com.google.common.base.Joiner; + + import org.eclipse.persistence.descriptors.ClassDescriptor; import org.eclipse.persistence.dynamic.DynamicEntity; import org.eclipse.persistence.dynamic.DynamicType; @@ -33,11 +35,13 @@ import org.eclipse.persistence.mappings.DatabaseMapping; import org.eclipse.persistence.oxm.XMLField; import org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping; import org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping; +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.nodes.NodeIngestor; import org.onap.aai.restcore.MediaType; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; +import org.onap.aai.setup.SchemaVersion; import org.springframework.web.util.UriUtils; - import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; @@ -49,43 +53,47 @@ import java.util.*; import java.util.Map.Entry; public class MoxyStrategy extends Introspector { - - private static final EELFLogger LOGGER2 = EELFManager.getInstance().getLogger(MoxyStrategy.class); - + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(MoxyStrategy.class); private DynamicEntity internalObject = null; private DynamicType internalType = null; private DynamicJAXBContext jaxbContext = null; private ClassDescriptor cd = null; private Marshaller marshaller = null; private Unmarshaller unmarshaller = null; - private Version version = null; + private SchemaVersion version = null; private Set<String> properties = null; private Set<String> keys = null; private Set<String> requiredProperties = null; private boolean isInitialized = false; - + + private NodeIngestor nodeIngestor; + protected MoxyStrategy(Object obj) { super(obj); /* must look up the correct jaxbcontext for this object */ className = MoxyStrategy.class.getSimpleName(); internalObject = (DynamicEntity)obj; - ModelInjestor injestor = ModelInjestor.getInstance(); - version = injestor.getVersionFromClassName(internalObject.getClass().getName()); - jaxbContext = injestor.getContextForVersion(version); - super.loader = LoaderFactory.createLoaderForVersion(getModelType(), version); + nodeIngestor = SpringContextAware.getBean(NodeIngestor.class); + version = nodeIngestor.getVersionFromClassName(internalObject.getClass().getName()); + super.loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(getModelType(), version); + jaxbContext = nodeIngestor.getContextForVersion(version); String simpleName = internalObject.getClass().getName(); internalType = jaxbContext.getDynamicType(simpleName); + cd = internalType.getDescriptor(); try { marshaller = jaxbContext.createMarshaller(); + unmarshaller = jaxbContext.createUnmarshaller(); + } catch (JAXBException e) { - LOGGER2.error(e.getMessage(),e); + } } - + private void init() { isInitialized = true; @@ -96,13 +104,13 @@ public class MoxyStrategy extends Introspector { } props = Collections.unmodifiableSet(props); this.properties = props; - + Set<String> requiredProps = new LinkedHashSet<>(); requiredProps = new LinkedHashSet<>(); for (DatabaseMapping dm : cd.getMappings()) { - if (dm.getField() instanceof XMLField) { + if (dm.getField() instanceof XMLField) { XMLField x = (XMLField)dm.getField(); - if (x != null) { + if (x != null) { if (x.isRequired()) { requiredProps.add(this.removeXPathDescriptor(x.getName())); } @@ -111,25 +119,25 @@ public class MoxyStrategy extends Introspector { } requiredProps = Collections.unmodifiableSet(requiredProps); this.requiredProperties = requiredProps; - + Set<String> keys = new LinkedHashSet<>(); - + for (String name : internalType.getDescriptor().getPrimaryKeyFieldNames()) { keys.add(this.removeXPathDescriptor(name)); } keys = Collections.unmodifiableSet(keys); this.keys = keys; - - + + } - + @Override public boolean hasProperty(String name) { String convertedName = convertPropertyName(name); - return internalType.containsProperty(convertedName); + return internalType.containsProperty(convertedName); } - + @Override public Object get(String name) { return internalObject.get(name); @@ -137,7 +145,7 @@ public class MoxyStrategy extends Introspector { @Override public void set(String name, Object obj) throws IllegalArgumentException { - + internalObject.set(name, obj); } @@ -149,7 +157,7 @@ public class MoxyStrategy extends Introspector { } return this.properties; - + } @Override @@ -171,7 +179,7 @@ public class MoxyStrategy extends Introspector { return this.keys; } - + @Override public Map<PropertyMetadata, String> getPropertyMetadata(String prop) { String propName = this.convertPropertyName(prop); @@ -184,7 +192,7 @@ public class MoxyStrategy extends Introspector { PropertyMetadata.valueOf(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, (String)entry.getKey())), (String)entry.getValue()); } } - + return result; } @@ -192,8 +200,8 @@ public class MoxyStrategy extends Introspector { public String getJavaClassName() { return internalObject.getClass().getName(); } - - + + @Override public Class<?> getClass(String name) { @@ -203,7 +211,7 @@ public class MoxyStrategy extends Introspector { if (internalType.getPropertyType(name) == null) { if (cd.getMappingForAttributeName(name) instanceof XMLCompositeDirectCollectionMapping) { resultClass = cd.getMappingForAttributeName(name).getContainerPolicy().getContainerClass(); - + } else if (cd.getMappingForAttributeName(name) instanceof XMLCompositeCollectionMapping) { resultClass = cd.getMappingForAttributeName(name).getContainerPolicy().getContainerClass(); } else { @@ -219,7 +227,6 @@ public class MoxyStrategy extends Introspector { } } catch (DynamicException e) { //property doesn't exist - LOGGER2.error(e.getMessage(),e); } return resultClass; } @@ -236,7 +243,7 @@ public class MoxyStrategy extends Introspector { resultClass = cd.getMappingForAttributeName(name).getReferenceDescriptor().getJavaClass(); } } - + return resultClass; } @@ -244,20 +251,20 @@ public class MoxyStrategy extends Introspector { public Object getUnderlyingObject() { return this.internalObject; } - + @Override public String getChildName() { - + String className = internalObject.getClass().getSimpleName(); String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className); - + if (this.isContainer()) { lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName()); } - + return lowerHyphen; } - + @Override public String getName() { String className = internalObject.getClass().getSimpleName(); @@ -266,11 +273,11 @@ public class MoxyStrategy extends Introspector { if (this.isContainer()) { lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().get(0)).getSimpleName()); }*/ - + return lowerHyphen; } - + @Override public String getObjectId() throws UnsupportedEncodingException { String result = ""; @@ -278,17 +285,17 @@ public class MoxyStrategy extends Introspector { if (this.isContainer()) { result += "/" + this.getName(); } else { - + if (container != null) { result += "/" + container; } result += "/" + this.getDbName() + "/" + this.findKey(); - + } - + return result; } - + @Override protected String findKey() throws UnsupportedEncodingException { Set<String> keys = null; @@ -298,10 +305,10 @@ public class MoxyStrategy extends Introspector { String value = UriUtils.encode(this.getValue(key).toString(), "UTF-8"); results.add(value); } - + return Joiner.on("/").join(results); } - + @Override public String preProcessKey (String key) { String result = ""; @@ -309,19 +316,19 @@ public class MoxyStrategy extends Introspector { String[] split = key.split("/"); int i = 0; for (i = split.length-1; i >= 0; i--) { - + if (jaxbContext.getDynamicType(split[i]) != null) { break; - + } - + } result = Joiner.on("/").join(Arrays.copyOfRange(split, 0, i)); - + return result; - + } - + @Override public String marshal(MarshallerProperties properties) { StringWriter result = new StringWriter(); @@ -332,15 +339,16 @@ public class MoxyStrategy extends Introspector { marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, properties.getWrapperAsArrayName()); marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, false); } - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted()); + + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted()); marshaller.marshal(this.internalObject, result); } catch (JAXBException e) { - LOGGER2.error(e.getMessage(),e); + //e.printStackTrace(); } return result.toString(); } - + @Override public Object clone() { Object result = null; @@ -350,10 +358,11 @@ public class MoxyStrategy extends Introspector { unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false); unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true); - + result = unmarshaller.unmarshal(new StreamSource(new StringReader(this.marshal(true))), this.internalObject.getClass()).getValue(); } catch (JAXBException e) { - LOGGER2.error(e.getMessage(),e); + // TODO Auto-generated catch block + //e.printStackTrace(); } result = IntrospectorFactory.newInstance(getModelType(), result); return result; @@ -375,7 +384,7 @@ public class MoxyStrategy extends Introspector { } @Override - public Version getVersion() { + public SchemaVersion getVersion() { return this.version; } diff --git a/aai-core/src/main/java/org/onap/aai/introspection/PojoInjestor.java b/aai-core/src/main/java/org/onap/aai/introspection/PojoInjestor.java deleted file mode 100644 index 3f009df6..00000000 --- a/aai-core/src/main/java/org/onap/aai/introspection/PojoInjestor.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.introspection; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.eclipse.persistence.jaxb.JAXBContextFactory; -import org.onap.aai.db.props.AAIProperties; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class PojoInjestor { - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PojoInjestor.class); - - private String POJO_CLASSPATH = "org.onap.aai.domain.yang"; - private final Pattern classNamePattern = Pattern.compile("\\.(v\\d+)\\."); - - public PojoInjestor() { - } - - public JAXBContext getContextForVersion(Version v) { - JAXBContext context = null; - try { - if (!v.equals(AAIProperties.LATEST)) { - POJO_CLASSPATH += "." + v; - } - context = JAXBContextFactory.createContext(POJO_CLASSPATH, this.getClass().getClassLoader()); - } catch (JAXBException e) { - LOGGER.error(e.getMessage(),e); - } - - return context; - } - public Version getVersion (String classname) { - Matcher m = classNamePattern.matcher(classname); - String version; - if (m.find()) { - version = m.group(1); - } else { - //only POJOs of old versions have the version number in their classnames - //so if we can't find a version, default to the latest - version = AAIProperties.LATEST.toString(); - } - - return Version.valueOf(version); - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/introspection/PojoLoader.java b/aai-core/src/main/java/org/onap/aai/introspection/PojoLoader.java deleted file mode 100644 index c471c7fe..00000000 --- a/aai-core/src/main/java/org/onap/aai/introspection/PojoLoader.java +++ /dev/null @@ -1,137 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.introspection; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.google.common.base.CaseFormat; -import org.eclipse.persistence.jaxb.JAXBContextFactory; -import org.eclipse.persistence.jaxb.UnmarshallerProperties; -import org.onap.aai.db.props.AAIProperties; -import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; -import org.onap.aai.introspection.exceptions.AAIUnmarshallingException; -import org.onap.aai.logging.ErrorLogHelper; -import org.onap.aai.logging.LogFormatTools; -import org.onap.aai.restcore.MediaType; -import org.onap.aai.workarounds.NamingExceptions; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; -import java.io.StringReader; -import java.util.Map; - -public class PojoLoader extends Loader { - - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(PojoLoader.class); - private static final String POJO_BASE_PACKAGE_NAME = "org.onap.aai.domain.yang"; - - protected JAXBContext context; - private final String pojoPackageName; - - protected PojoLoader(Version version) { - super(version, ModelType.POJO); - - if (!version.equals(AAIProperties.LATEST)) { - pojoPackageName = POJO_BASE_PACKAGE_NAME + "." + version; - } else { - pojoPackageName = POJO_BASE_PACKAGE_NAME; - } - - try { - context = JAXBContextFactory.createContext(pojoPackageName, this.getClass().getClassLoader()); - } catch (JAXBException e) { - LOGGER.error("JAXBException while instantiation contect for PojoLoader " + LogFormatTools.getStackTop(e)); - } - } - - @Override - public Introspector introspectorFromName(String name) throws AAIUnknownObjectException { - return IntrospectorFactory.newInstance(ModelType.POJO, objectFromName(name)); - } - - @Override - public Object objectFromName(String name) throws AAIUnknownObjectException { - - if (name == null) { - throw new AAIUnknownObjectException("null name passed in"); - } - final String sanitizedName = NamingExceptions.getInstance().getObjectName(name); - final String upperCamel; - - //Contains any uppercase, then assume it's upper camel - if (sanitizedName.matches(".*[A-Z].*")) { - upperCamel = sanitizedName; - } else { - upperCamel = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, sanitizedName); - } - - final String objectClassName; - - if (!upperCamel.contains(pojoPackageName)) { - objectClassName = pojoPackageName + "." + upperCamel; - } else { - objectClassName = upperCamel; - } - - try { - return Class.forName(objectClassName).newInstance(); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { - throw new AAIUnknownObjectException("Unrecognized AAI object " + name); - } - } - - @Override - protected void process(Version version) { - LOGGER.warn("PojoLoader.process(Version) has not been implemented"); - } - - @Override - public Introspector unmarshal(String type, String json, MediaType mediaType) throws AAIUnmarshallingException { - - try { - final Unmarshaller unmarshaller = context.createUnmarshaller(); - - if (mediaType.equals(MediaType.APPLICATION_JSON_TYPE)) { - unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); - unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false); - unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true); - } - - final Object clazz = objectFromName(type); - final Object obj = unmarshaller.unmarshal(new StreamSource(new StringReader(json)), clazz.getClass()).getValue(); - - return IntrospectorFactory.newInstance(ModelType.POJO, obj); - } catch (JAXBException e) { - ErrorLogHelper.logError("AAI_4007", "Could not unmarshall: " + e.getMessage()); - throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage()); - } catch (AAIUnknownObjectException e) { - throw new AAIUnmarshallingException("Could not unmarshall: " + e.getMessage(), e); - } - } - - @Override - public Map<String, Introspector> getAllObjects() { - //TODO - return null; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/introspection/PojoStrategy.java b/aai-core/src/main/java/org/onap/aai/introspection/PojoStrategy.java deleted file mode 100644 index 487bc40e..00000000 --- a/aai-core/src/main/java/org/onap/aai/introspection/PojoStrategy.java +++ /dev/null @@ -1,379 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.introspection; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.google.common.base.CaseFormat; -import com.google.common.base.Joiner; -import com.google.common.collect.Multimap; -import org.eclipse.persistence.jaxb.UnmarshallerProperties; -import org.onap.aai.annotations.Metadata; -import org.onap.aai.logging.ErrorLogHelper; -import org.onap.aai.restcore.MediaType; -import org.onap.aai.schema.enums.ObjectMetadata; -import org.onap.aai.schema.enums.PropertyMetadata; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import javax.xml.bind.Unmarshaller; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.transform.stream.StreamSource; -import java.io.StringReader; -import java.io.StringWriter; -import java.lang.reflect.*; -import java.util.*; - -public class PojoStrategy extends Introspector { - private static final EELFLogger LOGGER2 = EELFManager.getInstance().getLogger(PojoStrategy.class); - - private Object internalObject = null; - private PojoInjestor injestor = null; - private Multimap<String, String> keyProps = null; - private Metadata classLevelMetadata = null; - private Version version; - private JAXBContext jaxbContext; - private Marshaller marshaller; - private Unmarshaller unmarshaller; - private Set<String> properties = null; - private Set<String> keys = null; - private Set<String> requiredProperties = null; - - private boolean isInitialized = false; - - protected PojoStrategy(Object obj) { - super(obj); - className = PojoStrategy.class.getSimpleName(); - this.internalObject = obj; - injestor = new PojoInjestor(); - classLevelMetadata = obj.getClass().getAnnotation(Metadata.class); - - version = injestor.getVersion(obj.getClass().getName()); - jaxbContext = injestor.getContextForVersion(version); - super.loader = LoaderFactory.createLoaderForVersion(getModelType(), version); - try { - marshaller = jaxbContext.createMarshaller(); - unmarshaller = jaxbContext.createUnmarshaller(); - } catch (JAXBException e) { - LOGGER2.error(e.getMessage(),e); - } - - } - - private void init() { - - isInitialized = true; - - Set<String> properties = new LinkedHashSet<>(); - Set<String> keys = new LinkedHashSet<>(); - Set<String> required = new LinkedHashSet<>(); - - Field[] fields = this.internalObject.getClass().getDeclaredFields(); - - for (Field field : fields) { - if (!field.getName().equals("any")) { - properties.add(covertFieldToOutputFormat(field.getName())); - Metadata annotation = field.getAnnotation(Metadata.class); - XmlElement xmlAnnotation = field.getAnnotation(XmlElement.class); - if (annotation != null) { - if (annotation.isKey()) { - keys.add(covertFieldToOutputFormat(field.getName())); - } - } - if (xmlAnnotation != null) { - if (xmlAnnotation.required()) { - required.add(covertFieldToOutputFormat(field.getName())); - } - } - } - } - properties = Collections.unmodifiableSet(properties); - this.properties = properties; - - keys = Collections.unmodifiableSet(keys); - this.keys = keys; - - required = Collections.unmodifiableSet(required); - this.requiredProperties = required; - - } - private String covertFieldToOutputFormat(String propName) { - return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, propName); - } - - @Override - public boolean hasProperty(String name) { - //TODO - return true; - } - - @Override - /** - * Gets the value of the property via reflection - */ - public Object get(String name) { - String getMethodName = "get" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name); - try { - return this.internalObject.getClass().getDeclaredMethod(getMethodName).invoke(this.internalObject); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { - LOGGER2.error(e.getMessage(),e); - return null; - } - } - - @Override - public void set(String name, Object value) { - String setMethodName = "set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, name); - try { - this.internalObject.getClass().getDeclaredMethod(setMethodName, value.getClass()).invoke(this.internalObject, value); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { - ErrorLogHelper.logError("AAI_4017", "Error setting name/value pair on POJO: " + e.getMessage()); - } - } - - @Override - public Set<String> getProperties() { - - if(!isInitialized){ - this.init(); - } - return this.properties; - } - - - @Override - public Set<String> getRequiredProperties() { - - if(!isInitialized) { - this.init(); - } - return this.requiredProperties; - } - - @Override - public Set<String> getKeys() { - - if(!isInitialized){ - this.init(); - } - return this.keys; - } - - public Class<?> getClass(String name) { - - Field field = null; - try { - field = this.internalObject.getClass().getDeclaredField(CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name)); - } catch (NoSuchFieldException | SecurityException e) { - LOGGER2.error(e.getMessage(),e); - return null; - } - - return field.getType(); - } - - public Class<?> getGenericTypeClass(String name) { - - try { - String getMethodName = "get" + CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name); - Method method = internalObject.getClass().getDeclaredMethod(getMethodName); - Type t = method.getGenericReturnType(); - if(t instanceof ParameterizedType) { - ParameterizedType pt = (ParameterizedType)t; - return ((Class<?>)pt.getActualTypeArguments()[0]); - } else { - return null; - } - - } catch (Exception e) { - LOGGER2.error(e.getMessage(),e); - return null; - } - } - - @Override - public String getJavaClassName() { - return internalObject.getClass().getName(); - } - - @Override - public Object getUnderlyingObject() { - return this.internalObject; - } - - @Override - public String getName() { - String className = internalObject.getClass().getSimpleName(); - - return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className); - } - - @Override - protected String findKey() { - Set<String> keys = null; - keys = this.getKeys(); - List<String> results = new ArrayList<>(); - for (String key : keys) { - if (this.getType(key).toLowerCase().contains("long")) { - key = ((Long)this.getValue(key)).toString(); - } else { - key = (String)this.getValue(key); - } - results.add(key); - } - - return Joiner.on("/").join(results); - } - - @Override - public String marshal(MarshallerProperties properties) { - StringWriter result = new StringWriter(); - try { - if (properties.getMediaType().equals(MediaType.APPLICATION_JSON_TYPE)) { - marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.MEDIA_TYPE, "application/json"); - marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_INCLUDE_ROOT, properties.getIncludeRoot()); - marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, properties.getWrapperAsArrayName()); - } - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, properties.getFormatted()); - marshaller.marshal(this.internalObject, result); - } catch (JAXBException e) { - LOGGER2.error(e.getMessage(),e); - } - - return result.toString(); - } - - @Override - public Object clone() { - Object result = null; - try { - unmarshaller = jaxbContext.createUnmarshaller(); - - unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json"); - unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false); - unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true); - - result = unmarshaller.unmarshal(new StreamSource(new StringReader(this.marshal(true))), this.internalObject.getClass()).getValue(); - } catch (JAXBException e) { - LOGGER2.error(e.getMessage(),e); - } - result = IntrospectorFactory.newInstance(getModelType(), result); - return result; - } - - @Override - public String preProcessKey (String key) { - String result = ""; - //String trimmedRestURI = restURI.replaceAll("/[\\w\\-]+?/[\\w\\-]+?$", ""); - String[] split = key.split("/"); - int i = 0; - for (i = split.length-1; i >= 0; i--) { - - if (keyProps.containsKey(split[i])) { - break; - - } - - } - result = Joiner.on("/").join(Arrays.copyOfRange(split, 0, i)); - - return result; - - } - - @Override - public ModelType getModelType() { - return ModelType.POJO; - } - - @Override - public String getChildName() { - String className = internalObject.getClass().getSimpleName(); - String lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, className); - - if (this.isContainer()) { - lowerHyphen = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,this.getGenericTypeClass(this.getProperties().iterator().next()).getSimpleName()); - } - - return lowerHyphen; - } - - @Override - public Map<PropertyMetadata, String> getPropertyMetadata(String prop) { - Field f; - Map<PropertyMetadata, String> result = new HashMap<>(); - try { - f = internalObject.getClass().getField(prop); - Metadata m = f.getAnnotation(Metadata.class); - if (m != null) { - Field[] fields = m.getClass().getFields(); - String fieldName; - for (Field field : fields) { - fieldName = field.getName(); - if (fieldName.equals("isAbstract")) { - fieldName = "abstract"; - } else if (fieldName.equals("extendsFrom")) { - fieldName = "extends"; - } - fieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, fieldName); - result.put(PropertyMetadata.valueOf(fieldName), (String)field.get(m)); - } - } - } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { - LOGGER2.error(e.getMessage(),e); - } - - return result; - } - - @Override - public String getObjectId() { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getMetadata(ObjectMetadata metadataName) { - String value = null; - String methodName; - if (ObjectMetadata.ABSTRACT.equals(metadataName)) { - methodName = "isAbstract"; - } else if (ObjectMetadata.EXTENDS.equals(metadataName)) { - methodName = "extendsFrom"; - } else { - methodName = metadataName.toString(); - } - - try { - value = (String)this.classLevelMetadata.getClass().getMethod(methodName).invoke(classLevelMetadata); - } catch (IllegalArgumentException | IllegalAccessException | SecurityException | InvocationTargetException | NoSuchMethodException e) { - LOGGER2.error(e.getMessage(),e); - } - - return value; - } - - @Override - public Version getVersion() { - // TODO Auto-generated method stub - return null; - } -} diff --git a/aai-core/src/main/java/org/onap/aai/introspection/Version.java b/aai-core/src/main/java/org/onap/aai/introspection/Version.java deleted file mode 100644 index 2f826e9b..00000000 --- a/aai-core/src/main/java/org/onap/aai/introspection/Version.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.introspection; - -/** - * Requires the order to be in ascending order - */ -public enum Version { - v8, - v9, - v10, - v11, - v12, - v13, - v14; - - public static final String VERSION_EXPRESSION = "v8|v9|v10|v11|v12|v13|v14|latest"; - public static final String VERSION_EXPRESSION_V8_PLUS = "v8|v9|v10|v11|v12|v13|v14|latest"; - public static final String VERSION_EXPRESSION_V9_PLUS = "v9|v10|v11|v12|v13|v14|latest"; - - /** - * Checks if v is the latest version - * @param v - * @return - */ - public static boolean isLatest(Version v) { - return Version.getLatest().equals(v); - } - - /** - * Gets the latest version - * @return - */ - public static Version getLatest(){ - return Version.values()[Version.values().length-1]; - } - - /** - * To be used inplace of <b>Version.getVersion(String)</b> to correctly get version of "latest" - * @param v - * @return - */ - public static Version getVersion(String v) { - if ("latest".equals(v)) { - return Version.getLatest(); - } - return Version.valueOf(v); - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/PrivateEdge.java b/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/PrivateEdge.java new file mode 100644 index 00000000..5b719e33 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/PrivateEdge.java @@ -0,0 +1,195 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.introspection.sideeffect; + +import com.google.common.collect.Multimap; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.VertexProperty; +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.introspection.*; +import org.onap.aai.introspection.sideeffect.exceptions.AAIMultiplePropertiesException; +import org.onap.aai.parsers.query.QueryParser; +import org.onap.aai.restcore.util.URITools; +import org.onap.aai.schema.enums.PropertyMetadata; +import org.onap.aai.serialization.db.DBSerializer; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.serialization.db.EdgeSerializer; +import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import javax.ws.rs.core.MultivaluedMap; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.*; +import java.util.Map.Entry; + + +public class PrivateEdge extends SideEffect { + + public PrivateEdge(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) { + super(obj, self, dbEngine, serializer); + } + + @Override + protected void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException { + if (completeUri.isPresent()) { + process(completeUri, entry); + } else { + // Check if the vertex self has the template keys or the db aliased keys + // If it does check if the self vertex has a edge to that model + // If it does, then remove the edge since the update happened and doesn't have required props anymore + // "service-design-and-creation/models/model/{model-invariant-id}/model-vers/model-ver/{model-version-id}" + // If the vertex does have + Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY, obj.getVersion()); + Introspector introspector = loader.introspectorFromName(obj.getDbName()); + List<Vertex> vertices = new ArrayList<>(); + vertices.add(self); + Introspector curObj = serializer.dbToObject(vertices, introspector, 0, true, "false"); + Optional<String> populatedUri = this.replaceTemplates(curObj, entry.getValue()); + process(populatedUri, entry); + } + } + + private void process(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException { + if(completeUri.isPresent()){ + URI uri = new URI(completeUri.get()); + MultivaluedMap<String, String> map = URITools.getQueryMap(uri); + QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map); + List<Vertex> results = uriQuery.getQueryBuilder().toList(); + if (results.size() == 1) { + Vertex otherVertex = results.get(0); + VertexProperty otherVProperty = otherVertex.property("aai-node-type"); + + if (otherVProperty.isPresent()) { + + EdgeRuleQuery edgeQuery = new EdgeRuleQuery.Builder(obj.getName(), otherVProperty.value().toString()).edgeType(EdgeType.COUSIN).setPrivate(true).build(); + EdgeIngestor edgeIngestor = serializer.getEdgeIngestor(); + EdgeSerializer edgeSerializer = serializer.getEdgeSeriailizer(); + + Multimap<String, EdgeRule> edgeRulesMap = edgeIngestor.getRules(edgeQuery); + + if (edgeRulesMap.isEmpty()) { + String message = String.format("Unable to find edge between %s and %s", obj.getName(), otherVProperty.value().toString()); + throw new AAIException("AAI_6127", message); + } else if (edgeRulesMap.size() > 1) { + String message = String.format("Found multiple edges between %s and %s", obj.getName(), otherVProperty.value().toString()); + throw new EdgeMultiplicityException(message); + } + + + for (Entry<String, EdgeRule> edgeEntry : edgeRulesMap.entries()) { + EdgeRule edgeRule = edgeIngestor.getRule(edgeQuery); + Iterator<Edge> edges = self.edges(edgeRule.getDirection(), edgeRule.getLabel().toString()); + if(edges.hasNext()){ + Edge edge = edges.next(); + EdgeStatus status = checkStatus(obj, self); + switch(status){ + case CREATED: + edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self, otherVertex, edgeRule.getLabel()); + break; + case MODIFIED: + edge.remove(); + edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self, otherVertex, edgeRule.getLabel()); + break; + case REMOVED: + edge.remove(); + break; + case UNCHANGED: + break; + } + } else { + edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self, otherVertex, edgeRule.getLabel()); + } + } + } + } else { + if (results.isEmpty()) { + throw new AAIException("AAI_6114", "object located at " + uri + " not found"); + } else if (results.size() > 1) { + throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri); + } + } + } + } + + public enum EdgeStatus { + CREATED, + REMOVED, + MODIFIED, + UNCHANGED + } + + private EdgeStatus checkStatus(Introspector obj, Vertex self) { + + for(String key : templateKeys){ + String currentObjValue = obj.getValue(key); + Map<PropertyMetadata, String> map = obj.getPropertyMetadata(key); + String oldVertexValue = null; + + if(map.containsKey(PropertyMetadata.DB_ALIAS)){ + oldVertexValue = self.<String>property(key + AAIProperties.DB_ALIAS_SUFFIX).orElse(null); + } else { + oldVertexValue = self.<String>property(key).orElse(null); + } + + if(currentObjValue == null && oldVertexValue == null){ + continue; + } + + if(currentObjValue == null){ + if(oldVertexValue != null){ + return EdgeStatus.REMOVED; + } + } + + if(oldVertexValue == null){ + if(currentObjValue != null){ + return EdgeStatus.CREATED; + } + } + + if(!oldVertexValue.equals(currentObjValue)){ + return EdgeStatus.MODIFIED; + } + } + + return EdgeStatus.UNCHANGED; + } + + @Override + protected PropertyMetadata getPropertyMetadata() { + return PropertyMetadata.PRIVATE_EDGE; + } + + @Override + protected boolean replaceWithWildcard() { + return false; + } + +} diff --git a/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/SideEffect.java b/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/SideEffect.java index d279a4a4..6bf03c17 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/SideEffect.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/SideEffect.java @@ -19,8 +19,13 @@ */ package org.onap.aai.introspection.sideeffect; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; @@ -30,6 +35,8 @@ import org.onap.aai.introspection.sideeffect.exceptions.AAIMissingRequiredProper import org.onap.aai.schema.enums.PropertyMetadata; import org.onap.aai.serialization.db.DBSerializer; import org.onap.aai.serialization.engines.TransactionalGraphEngine; +import org.onap.aai.setup.SchemaVersions; + import java.io.UnsupportedEncodingException; import java.net.URISyntaxException; @@ -41,16 +48,24 @@ import java.util.regex.Pattern; public abstract class SideEffect { protected static final Pattern template = Pattern.compile("\\{(.*?)\\}"); + private static final EELFLogger logger = EELFManager.getInstance().getLogger(SideEffect.class); + protected final Introspector obj; protected final TransactionalGraphEngine dbEngine; protected final DBSerializer serializer; - protected final Loader latestLoader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST); + protected final Loader latestLoader; protected final Vertex self; + + protected Set<String> templateKeys = new HashSet<>(); + public SideEffect (Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) { this.obj = obj; this.dbEngine = dbEngine; this.serializer = serializer; this.self = self; + LoaderFactory loaderFactory = SpringContextAware.getBean(LoaderFactory.class); + SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + this.latestLoader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion()) ; } protected void execute() throws UnsupportedEncodingException, URISyntaxException, AAIException { @@ -58,7 +73,11 @@ public abstract class SideEffect { for (Entry<String, String> entry : properties.entrySet()) { Optional<String> populatedUri = this.replaceTemplates(obj, entry.getValue()); Optional<String> completeUri = this.resolveRelativePath(populatedUri); - this.processURI(completeUri, entry); + try { + this.processURI(completeUri, entry); + } catch (EdgeRuleNotFoundException | AmbiguousRuleChoiceException e) { + logger.warn("Unable to execute the side effect {} due to ", e, this.getClass().getName()); + } } } @@ -99,13 +118,14 @@ public abstract class SideEffect { return result; } - private Optional<String> replaceTemplates(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException { + protected Optional<String> replaceTemplates(Introspector obj, String uriString) throws AAIMissingRequiredPropertyException { String result = uriString; final Map<String, String> propMap = this.findProperties(obj, uriString); if (propMap.isEmpty()) { return Optional.empty(); } for (Entry<String, String> entry : propMap.entrySet()) { + templateKeys.add(entry.getKey()); result = result.replaceAll("\\{" + entry.getKey() + "\\}", entry.getValue()); } //drop out wildcards if they exist @@ -123,5 +143,5 @@ public abstract class SideEffect { protected abstract boolean replaceWithWildcard(); protected abstract PropertyMetadata getPropertyMetadata(); - protected abstract void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException; + protected abstract void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException; } diff --git a/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/SideEffectRunner.java b/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/SideEffectRunner.java index 8636084f..0d70c382 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/SideEffectRunner.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/sideeffect/SideEffectRunner.java @@ -19,6 +19,8 @@ */ package org.onap.aai.introspection.sideeffect; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; diff --git a/aai-core/src/main/java/org/onap/aai/introspection/tools/InjectKeysFromURI.java b/aai-core/src/main/java/org/onap/aai/introspection/tools/InjectKeysFromURI.java index 114cd67c..7f6bb39f 100644 --- a/aai-core/src/main/java/org/onap/aai/introspection/tools/InjectKeysFromURI.java +++ b/aai-core/src/main/java/org/onap/aai/introspection/tools/InjectKeysFromURI.java @@ -19,8 +19,6 @@ */ package org.onap.aai.introspection.tools; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.parsers.uri.URIToObject; @@ -28,7 +26,6 @@ import org.onap.aai.parsers.uri.URIToObject; import java.net.URI; public class InjectKeysFromURI implements IssueResolver { - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InjectKeysFromURI.class); private URI uri = null; private Loader loader = null; @@ -60,7 +57,7 @@ public class InjectKeysFromURI implements IssueResolver { result = true; } } catch (Exception e) { - LOGGER.error(e.getMessage(),e); + //log something probably result = false; } } diff --git a/aai-core/src/main/java/org/onap/aai/logging/EcompPatternLayout.java b/aai-core/src/main/java/org/onap/aai/logging/EcompPatternLayout.java index 88506ee4..65374312 100644 --- a/aai-core/src/main/java/org/onap/aai/logging/EcompPatternLayout.java +++ b/aai-core/src/main/java/org/onap/aai/logging/EcompPatternLayout.java @@ -29,5 +29,7 @@ public class EcompPatternLayout extends PatternLayout { PatternLayout.defaultConverterMap.put("ecompErrorCategory", EcompErrorCategory.class.getName()); PatternLayout.defaultConverterMap.put("ecompResponseCode", EcompResponseCode.class.getName()); PatternLayout.defaultConverterMap.put("ecompResponseDescription", EcompResponseDescription.class.getName()); + PatternLayout.defaultConverterMap.put("ecompStatusCode", EcompStatusCode.class.getName()); + PatternLayout.defaultConverterMap.put("ecompServiceName", EcompServiceName.class.getName()); } } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/LegacyDBSerializer.java b/aai-core/src/main/java/org/onap/aai/logging/EcompServiceName.java index b433fde5..e20c841b 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/LegacyDBSerializer.java +++ b/aai-core/src/main/java/org/onap/aai/logging/EcompServiceName.java @@ -17,17 +17,19 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.aai.serialization.db; +package org.onap.aai.logging; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.ModelType; -import org.onap.aai.introspection.Version; -import org.onap.aai.serialization.engines.TransactionalGraphEngine; +import ch.qos.logback.classic.pattern.ClassicConverter; +import ch.qos.logback.classic.spi.ILoggingEvent; +import org.onap.aai.logging.LoggingContext.LoggingField; -public class LegacyDBSerializer extends DBSerializer { - - public LegacyDBSerializer(Version version, TransactionalGraphEngine engine, ModelType introspectionType, String sourceOfTruth) throws AAIException { - super(version, engine, introspectionType, sourceOfTruth); +public class EcompServiceName extends ClassicConverter { + @Override + public String convert(ILoggingEvent event) { + if (!event.getMDCPropertyMap().containsKey(LoggingField.SERVICE_NAME.toString())) { + return "AAI"; + } + return event.getMDCPropertyMap().get(LoggingField.SERVICE_NAME.toString()); } - } + diff --git a/aai-core/src/main/java/org/onap/aai/db/schema/AuditorFactory.java b/aai-core/src/main/java/org/onap/aai/logging/EcompStatusCode.java index 67bc0398..1ac4606b 100644 --- a/aai-core/src/main/java/org/onap/aai/db/schema/AuditorFactory.java +++ b/aai-core/src/main/java/org/onap/aai/logging/EcompStatusCode.java @@ -17,30 +17,19 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.aai.db.schema; +package org.onap.aai.logging; -import org.onap.aai.introspection.Version; -import org.janusgraph.core.JanusGraph; +import org.onap.aai.logging.LoggingContext.LoggingField; +import ch.qos.logback.classic.pattern.ClassicConverter; +import ch.qos.logback.classic.spi.ILoggingEvent; -public class AuditorFactory { - - /** - * Gets the OXM auditor. - * - * @param v the v - * @return the OXM auditor - */ - public static Auditor getOXMAuditor (Version v) { - return new AuditOXM(v); - } - - /** - * Gets the graph auditor. - * - * @param g the g - * @return the graph auditor - */ - public static Auditor getGraphAuditor (JanusGraph g) { - return new AuditJanusGraph(g); - } +public class EcompStatusCode extends ClassicConverter { + @Override + public String convert(ILoggingEvent event) { + if (!event.getMDCPropertyMap().containsKey(LoggingField.STATUS_CODE.toString())) { + return LoggingContext.StatusCode.COMPLETE.toString(); + } + return event.getMDCPropertyMap().get(LoggingField.STATUS_CODE.toString()); + } } + diff --git a/aai-core/src/main/java/org/onap/aai/logging/EelfClassOfCaller.java b/aai-core/src/main/java/org/onap/aai/logging/EelfClassOfCaller.java index cb84ba9c..f7ae2e93 100644 --- a/aai-core/src/main/java/org/onap/aai/logging/EelfClassOfCaller.java +++ b/aai-core/src/main/java/org/onap/aai/logging/EelfClassOfCaller.java @@ -29,7 +29,7 @@ public class EelfClassOfCaller extends NamedConverter { StackTraceElement[] cda = event.getCallerData(); //If using the EELFLogger, it "hides" the calling class because it wraps the logging calls - //Without this, you'd only ever see "com.att.eelf.configuration.SLF4jWrapper" when using the + //Without this, you'd only ever see "EELF SLF4jWrapper" when using the // %C pattern converter if (cda != null && cda.length > 2) { return cda[2].getClassName(); diff --git a/aai-core/src/main/java/org/onap/aai/logging/ErrorLogHelper.java b/aai-core/src/main/java/org/onap/aai/logging/ErrorLogHelper.java index 06362813..7d97ffbf 100644 --- a/aai-core/src/main/java/org/onap/aai/logging/ErrorLogHelper.java +++ b/aai-core/src/main/java/org/onap/aai/logging/ErrorLogHelper.java @@ -363,10 +363,10 @@ public class ErrorLogHelper { org.onap.aai.domain.restResponseInfo.ObjectFactory factory = new org.onap.aai.domain.restResponseInfo.ObjectFactory(); org.onap.aai.domain.restResponseInfo.Info info = factory.createInfo(); org.onap.aai.domain.restResponseInfo.Info.ResponseMessages responseMessages = factory.createInfoResponseMessages(); - Iterator<Map.Entry<AAIException, ArrayList<String>>> it = areList.entrySet().iterator(); + Iterator<Entry<AAIException, ArrayList<String>>> it = areList.entrySet().iterator(); while (it.hasNext()) { - Map.Entry<AAIException,ArrayList<String>> pair = (Map.Entry<AAIException, ArrayList<String>>)it.next(); + Entry<AAIException,ArrayList<String>> pair = (Entry<AAIException, ArrayList<String>>)it.next(); AAIException are = pair.getKey(); ArrayList<String> variables = pair.getValue(); @@ -584,7 +584,8 @@ public class ErrorLogHelper { .append(errorObject.getHTTPResponseCode()) .append(":") .append(e.getMessage()) - .toString(); + .toString() + .replaceAll("\\n", "^"); LoggingContext.responseCode(Integer.toString(errorObject.getHTTPResponseCode().getStatusCode())); LoggingContext.responseDescription(errorMessage); diff --git a/aai-core/src/main/java/org/onap/aai/logging/ErrorObject.java b/aai-core/src/main/java/org/onap/aai/logging/ErrorObject.java index c15fed34..c39b5a61 100644 --- a/aai-core/src/main/java/org/onap/aai/logging/ErrorObject.java +++ b/aai-core/src/main/java/org/onap/aai/logging/ErrorObject.java @@ -36,6 +36,7 @@ public class ErrorObject { private String restErrorCode = "3002"; private String errorCode; private String errorText; + private String details; /** * Instantiates a new error object. @@ -275,6 +276,24 @@ public class ErrorObject { } /** + * Gets the details. + * + * @return the details + */ + public String getDetails() { + return details; + } + + /** + * Sets the details. + * + * @param details the new details + */ + public void setDetails(String details) { + this.details = details == null ? "" : details; + } + + /** * Gets the error code string. This is also the string * configured in Nagios to alert on * diff --git a/aai-core/src/main/java/org/onap/aai/logging/LogFormatTools.java b/aai-core/src/main/java/org/onap/aai/logging/LogFormatTools.java index 482b445c..d91ba19d 100644 --- a/aai-core/src/main/java/org/onap/aai/logging/LogFormatTools.java +++ b/aai-core/src/main/java/org/onap/aai/logging/LogFormatTools.java @@ -54,7 +54,10 @@ public class LogFormatTools { * @throws AAIException the AAI exception */ public static String getStackTop(Throwable e) { - StringBuffer stackMessage = new StringBuffer(); + // StringBuilder is more efficient than StringBuffer and should only + // StringBuffer is only supposed to be used if multiple threads are modifying + // the same object and since this object is created locally not necessary + StringBuilder stackMessage = new StringBuilder(); int maxStackTraceEntries = 10; try { maxStackTraceEntries = Integer.valueOf(AAIConfig.get(AAIConstants.LOGGING_MAX_STACK_TRACE_ENTRIES)); diff --git a/aai-core/src/main/java/org/onap/aai/logging/LoggingContext.java b/aai-core/src/main/java/org/onap/aai/logging/LoggingContext.java index 8b8cb179..627a9d2e 100644 --- a/aai-core/src/main/java/org/onap/aai/logging/LoggingContext.java +++ b/aai-core/src/main/java/org/onap/aai/logging/LoggingContext.java @@ -47,7 +47,7 @@ public class LoggingContext { private static final String PREVIOUS_CONTEXTS_KEY = "_PREVIOUS_CONTEXTS"; - //Response codes from ECOMP Logging Guidelines + //Response codes from Logging Guidelines public static final String SUCCESS = "0"; public static final String PERMISSION_ERROR = "100"; public static final String AVAILABILITY_TIMEOUT_ERROR = "200"; @@ -62,7 +62,7 @@ public class LoggingContext { responseMap.put(SUCCESS, "Success"); responseMap.put(UNKNOWN_ERROR, "Unknown error"); } - //ECOMP Specific Log Event Fields + // Specific Log Event Fields public static enum LoggingField { START_TIME("startTime"), REQUEST_ID("requestId"), @@ -86,7 +86,7 @@ public class LoggingContext { CUSTOM_FIELD_3("customField3"), CUSTOM_FIELD_4("customField4"), - //ECOMP Specific Metric Log Event Fields + // Specific Metric Log Event Fields TARGET_ENTITY("targetEntity"), TARGET_SERVICE_NAME("targetServiceName"), //A&AI Specific Log Event Fields @@ -167,7 +167,7 @@ public class LoggingContext { MDC.put(LoggingField.PARTNER_NAME.toString(), partnerName); } - public static void statusCode(LoggingContext.StatusCode statusCode) { + public static void statusCode(StatusCode statusCode) { MDC.put(LoggingField.STATUS_CODE.toString(), statusCode.toString()); } @@ -197,7 +197,7 @@ public class LoggingContext { public static void successStatusFields() { responseCode(SUCCESS); - statusCode(LoggingContext.StatusCode.COMPLETE); + statusCode(StatusCode.COMPLETE); responseDescription("Success"); } private static void serverIpAddress() { diff --git a/aai-core/src/main/java/org/onap/aai/parsers/query/LegacyQueryParser.java b/aai-core/src/main/java/org/onap/aai/parsers/query/LegacyQueryParser.java index c99b842c..d20915c4 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/query/LegacyQueryParser.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/query/LegacyQueryParser.java @@ -32,7 +32,7 @@ import org.onap.aai.parsers.uri.URIToObject; import org.onap.aai.query.builder.QueryBuilder; import org.onap.aai.restcore.util.URITools; import org.onap.aai.schema.enums.PropertyMetadata; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.enums.EdgeType; import javax.ws.rs.core.MultivaluedMap; import java.io.UnsupportedEncodingException; diff --git a/aai-core/src/main/java/org/onap/aai/parsers/query/QueryParser.java b/aai-core/src/main/java/org/onap/aai/parsers/query/QueryParser.java index c7917698..9129911a 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/query/QueryParser.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/query/QueryParser.java @@ -20,10 +20,13 @@ package org.onap.aai.parsers.query; import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.db.props.AAIProperties; import org.onap.aai.introspection.Loader; import org.onap.aai.introspection.LoaderFactory; import org.onap.aai.query.builder.QueryBuilder; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; import java.net.URI; @@ -57,7 +60,9 @@ public abstract class QueryParser { this.uri = uri; this.queryBuilder = queryBuilder; this.loader = loader; - this.latestLoader = LoaderFactory.createLoaderForVersion(loader.getModelType(), AAIProperties.LATEST); + LoaderFactory loaderFactory = SpringContextAware.getBean(LoaderFactory.class); + SchemaVersion latest = SpringContextAware.getBean(SchemaVersions.class).getDefaultVersion(); + this.latestLoader = loaderFactory.createLoaderForVersion(loader.getModelType(), latest); } /** @@ -69,7 +74,9 @@ public abstract class QueryParser { protected QueryParser(Loader loader, QueryBuilder<Vertex> queryBuilder) { this.queryBuilder = queryBuilder; this.loader = loader; - this.latestLoader = LoaderFactory.createLoaderForVersion(loader.getModelType(), AAIProperties.LATEST); + LoaderFactory loaderFactory = SpringContextAware.getBean(LoaderFactory.class); + SchemaVersion latest = SpringContextAware.getBean(SchemaVersions.class).getDefaultVersion(); + this.latestLoader = loaderFactory.createLoaderForVersion(loader.getModelType(), latest); } /** diff --git a/aai-core/src/main/java/org/onap/aai/parsers/query/RelationshipQueryParser.java b/aai-core/src/main/java/org/onap/aai/parsers/query/RelationshipQueryParser.java index cdc0b1a9..c3ca5cba 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/query/RelationshipQueryParser.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/query/RelationshipQueryParser.java @@ -19,6 +19,10 @@ */ package org.onap.aai.parsers.query; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.edges.EdgeIngestor; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; @@ -26,7 +30,7 @@ import org.onap.aai.introspection.ModelType; import org.onap.aai.parsers.relationship.RelationshipToURI; import org.onap.aai.parsers.uri.URIParser; import org.onap.aai.query.builder.QueryBuilder; -import org.onap.aai.serialization.db.EdgeRules; +import org.springframework.context.ApplicationContext; import java.io.UnsupportedEncodingException; @@ -35,11 +39,13 @@ import java.io.UnsupportedEncodingException; */ public class RelationshipQueryParser extends LegacyQueryParser { + private static final EELFLogger logger = EELFManager.getInstance().getLogger(RelationshipQueryParser.class); + private Introspector relationship = null; private ModelType modelType = null; - private EdgeRules edgeRules = null; + private EdgeIngestor edgeRules = null; /** * Instantiates a new relationship query parser. @@ -54,11 +60,21 @@ public class RelationshipQueryParser extends LegacyQueryParser { super(loader, queryBuilder); this.relationship = obj; this.modelType = obj.getModelType(); - this.edgeRules = EdgeRules.getInstance(); + initBeans(); RelationshipToURI rToUri = new RelationshipToURI(loader, obj); this.uri = rToUri.getUri(); URIParser parser = new URIParser(loader, uri); parser.parse(this); } - + + private void initBeans() { + ApplicationContext ctx = SpringContextAware.getApplicationContext(); + if (ctx == null) { + logger.warn("Unable to retrieve the spring context"); + } else { + EdgeIngestor ei = ctx.getBean(EdgeIngestor.class); + this.edgeRules = ei; + } + } + } diff --git a/aai-core/src/main/java/org/onap/aai/parsers/query/UniqueURIQueryParser.java b/aai-core/src/main/java/org/onap/aai/parsers/query/UniqueURIQueryParser.java index 8517d454..3f14eecb 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/query/UniqueURIQueryParser.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/query/UniqueURIQueryParser.java @@ -26,7 +26,7 @@ import org.onap.aai.parsers.uri.Parsable; import org.onap.aai.parsers.uri.URIParser; import org.onap.aai.parsers.uri.URIToDBKey; import org.onap.aai.query.builder.QueryBuilder; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.enums.EdgeType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriBuilder; diff --git a/aai-core/src/main/java/org/onap/aai/parsers/relationship/RelationshipToURI.java b/aai-core/src/main/java/org/onap/aai/parsers/relationship/RelationshipToURI.java index b78a95c9..5ab38544 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/relationship/RelationshipToURI.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/relationship/RelationshipToURI.java @@ -22,18 +22,24 @@ package org.onap.aai.parsers.relationship; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import org.apache.tinkerpop.gremlin.structure.Direction; +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.*; +import org.onap.aai.setup.SchemaVersions; + import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.parsers.exceptions.AAIIdentityMapParseException; import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException; import org.onap.aai.parsers.uri.URIParser; import org.onap.aai.schema.enums.ObjectMetadata; -import org.onap.aai.serialization.db.AAIDirection; -import org.onap.aai.serialization.db.EdgeRule; -import org.onap.aai.serialization.db.EdgeRules; -import org.onap.aai.serialization.db.EdgeType; -import org.onap.aai.workarounds.LegacyURITransformer; +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.enums.EdgeType; +import org.springframework.context.ApplicationContext; import javax.ws.rs.core.UriBuilder; import java.io.UnsupportedEncodingException; @@ -57,11 +63,11 @@ public class RelationshipToURI { private ModelType modelType = null; - private EdgeRules edgeRules = null; - + private EdgeIngestor edgeRules = null; + private URI uri = null; - - private LegacyURITransformer urlTransform = null; + + private SchemaVersions schemaVersions; /** * Instantiates a new relationship to URI. @@ -74,14 +80,19 @@ public class RelationshipToURI { public RelationshipToURI(Loader loader, Introspector relationship) throws UnsupportedEncodingException, AAIException { this.relationship = relationship; this.modelType = relationship.getModelType(); - this.edgeRules = EdgeRules.getInstance(); this.loader = loader; - this.urlTransform = LegacyURITransformer.getInstance(); - + this.initEdgeIngestor(); this.parse(); } - + + protected void initEdgeIngestor() { + //TODO proper spring wiring, but that requires a lot of refactoring so for now we have this + ApplicationContext ctx = SpringContextAware.getApplicationContext(); + edgeRules = ctx.getBean(EdgeIngestor.class); + schemaVersions = ctx.getBean(SchemaVersions.class); + } + /** * Parses the. * @throws @@ -93,7 +104,7 @@ public class RelationshipToURI { String relatedLink = (String)relationship.getValue("related-link"); Optional<URI> result; try { - if (loader.getVersion().compareTo(Version.v10) >= 0) { + if (loader.getVersion().compareTo(schemaVersions.getRelatedLinkVersion()) >= 0) { result = processRelatedLink(relatedLink); if (!result.isPresent()) { result = processRelationshipData(); @@ -245,7 +256,7 @@ public class RelationshipToURI { for (j = (data.size() - i) - 1; j >= 0; j--) { objectType = data.get(j); try { - rule = edgeRules.getEdgeRule(EdgeType.TREE, startType, objectType); + rule = edgeRules.getRule(new EdgeRuleQuery.Builder(startType, objectType).edgeType(EdgeType.TREE).build()); direction = rule.getDirection(); if (direction != null) { if ((rule.getContains().equals(AAIDirection.OUT.toString()) && direction.equals(Direction.IN)) || (rule.getContains().equals(AAIDirection.IN.toString()) && direction.equals(Direction.OUT))) { @@ -258,7 +269,7 @@ public class RelationshipToURI { } } } - } catch (AAIException e) { + } catch (AAIException | EdgeRuleNotFoundException | AmbiguousRuleChoiceException e ) { //ignore exceptions generated continue; } diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/Parsable.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/Parsable.java index aa2db5bd..f35b4fde 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/Parsable.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/Parsable.java @@ -21,7 +21,7 @@ package org.onap.aai.parsers.uri; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.enums.EdgeType; import javax.ws.rs.core.MultivaluedMap; @@ -37,7 +37,7 @@ public interface Parsable { * @param uriKeys * @throws AAIException */ - public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys) throws AAIException; + void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys) throws AAIException; /** * Process container. * @@ -46,26 +46,26 @@ public interface Parsable { * @param isFinalContainer the is final container * @throws AAIException the AAI exception */ - public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys, boolean isFinalContainer) throws AAIException; + void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys, boolean isFinalContainer) throws AAIException; /** * Process namespace. * * @param obj the obj */ - public void processNamespace(Introspector obj); + void processNamespace(Introspector obj); /** * Gets the cloud region transform. * * @return the cloud region transform */ - public String getCloudRegionTransform(); + String getCloudRegionTransform(); /** * Use original loader. * * @return true, if successful */ - public boolean useOriginalLoader(); + boolean useOriginalLoader(); } diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIParser.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIParser.java index 2ce198e0..6e6ba844 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIParser.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIParser.java @@ -19,16 +19,17 @@ */ package org.onap.aai.parsers.uri; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.introspection.LoaderFactory; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.logging.ErrorLogHelper; import org.onap.aai.parsers.exceptions.DoesNotStartWithValidNamespaceException; import org.onap.aai.rest.RestTokens; import org.onap.aai.schema.enums.ObjectMetadata; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.enums.EdgeType; import org.onap.aai.util.AAIConfig; import org.springframework.web.util.UriUtils; @@ -76,7 +77,7 @@ public class URIParser { //Load the latest version because we need it for cloud region this.loader = loader; - } + } /** * Instantiates a new URI parser. @@ -268,8 +269,9 @@ public class URIParser { if (result.endsWith("/")) { result = result.substring(0, result.length() - 1); } - - result = result.replaceFirst("aai/v\\d+/", ""); + + // TODO - Check if this makes to do for model driven for base uri path + result = result.replaceFirst("[a-z][a-z]*/v\\d+/", ""); return UriBuilder.fromPath(result).build(); } diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToDBKey.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToDBKey.java index 4dbac7f0..007f1b3c 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToDBKey.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToDBKey.java @@ -23,7 +23,7 @@ import com.google.common.base.Joiner; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.enums.EdgeType; import javax.ws.rs.core.MultivaluedMap; import java.io.UnsupportedEncodingException; @@ -51,25 +51,10 @@ public class URIToDBKey implements Parsable { * @throws UnsupportedEncodingException the unsupported encoding exception */ public URIToDBKey(Loader loader, URI uri) throws IllegalArgumentException, AAIException, UnsupportedEncodingException { - URIParser parser = new URIParser(loader, uri); parser.parse(this); } - /* - public URIToDBKey(Version version, String uri) throws IllegalArgumentException { - - super(version, uri); - try { - context = ModelInjestor.getInstance().getContextForVersion(version); - if (context == null) { - throw new IllegalArgumentException("could not find a context for version: " + version); - } - this.parse(); - } catch (Exception e) { - throw new IllegalArgumentException("uri not valid against our model: " + uri); - } - }*/ - + /** * @{inheritDoc} */ @@ -116,6 +101,6 @@ public class URIToDBKey implements Parsable { @Override public void processContainer(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys, - boolean isFinalContainer) throws AAIException { + boolean isFinalContainer) { } } diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToExtensionInformation.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToExtensionInformation.java index ad8a30ab..1ecb3b4c 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToExtensionInformation.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToExtensionInformation.java @@ -25,7 +25,7 @@ import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.restcore.HttpMethod; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.enums.EdgeType; import javax.ws.rs.core.MultivaluedMap; import java.io.UnsupportedEncodingException; diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToObject.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToObject.java index f1456120..d8bdab83 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToObject.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToObject.java @@ -19,14 +19,12 @@ */ package org.onap.aai.parsers.uri; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; -import org.onap.aai.introspection.Version; import org.onap.aai.schema.enums.ObjectMetadata; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.setup.SchemaVersion; import javax.ws.rs.core.MultivaluedMap; import java.io.UnsupportedEncodingException; @@ -39,12 +37,9 @@ import java.util.List; * with their keys populated based off the values in the URI. * * It populates the keys in the order they are listed in the model. - * - - * */ public class URIToObject implements Parsable { - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(URIToObject.class); + private Introspector topEntity = null; @@ -58,7 +53,7 @@ public class URIToObject implements Parsable { private List<Object> parentList = null; - private Version version = null; + private SchemaVersion version = null; private Loader loader = null; private final HashMap<String, Introspector> relatedObjects; @@ -166,7 +161,7 @@ public class URIToObject implements Parsable { * * @return the object version */ - public Version getObjectVersion() { + public SchemaVersion getObjectVersion() { return this.loader.getVersion(); } public Loader getLoader() { @@ -203,7 +198,6 @@ public class URIToObject implements Parsable { } } } catch (UnsupportedEncodingException e) { - LOGGER.error(e.getMessage(),e); } this.previous = entity; } diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToRelationshipObject.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToRelationshipObject.java index 6acf8e51..cbb6ccdb 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToRelationshipObject.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIToRelationshipObject.java @@ -19,14 +19,16 @@ */ package org.onap.aai.parsers.uri; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; -import org.onap.aai.serialization.db.EdgeType; -import org.onap.aai.util.AAIApiServerURLBase; -import org.onap.aai.workarounds.LegacyURITransformer; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.util.AAIConfig; +import org.onap.aai.util.AAIConstants; import javax.ws.rs.core.MultivaluedMap; import java.io.UnsupportedEncodingException; @@ -47,9 +49,7 @@ public class URIToRelationshipObject implements Parsable { private Introspector result = null; - private LegacyURITransformer uriTransformer = null; - - private Version originalVersion = null; + private SchemaVersion originalVersion = null; private Introspector relationship = null; @@ -71,7 +71,6 @@ public class URIToRelationshipObject implements Parsable { public URIToRelationshipObject(Loader loader, URI uri) throws AAIException { this.loader = loader; - uriTransformer = LegacyURITransformer.getInstance(); originalVersion = loader.getVersion(); try { @@ -80,7 +79,7 @@ public class URIToRelationshipObject implements Parsable { throw new RuntimeException("Fatal error - could not load relationship object!", e1); } - this.baseURL = AAIApiServerURLBase.get(originalVersion); + this.baseURL = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE); this.uri = uri; } @@ -132,7 +131,8 @@ public class URIToRelationshipObject implements Parsable { URI originalUri = parser.getOriginalURI(); URI relatedLink = new URI(this.baseURL + this.originalVersion + "/" + originalUri); - if (this.originalVersion.compareTo(Version.v10) >= 0) { + SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + if (this.originalVersion.compareTo(schemaVersions.getRelatedLinkVersion()) >= 0) { //only return the path section of the URI past v10 relatedLink = new URI(relatedLink.getRawPath()); } @@ -144,8 +144,7 @@ public class URIToRelationshipObject implements Parsable { } @Override - public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys) - throws AAIException { + public void processObject(Introspector obj, EdgeType type, MultivaluedMap<String, String> uriKeys) { for (String key : obj.getKeys()) { try { Introspector data = loader.introspectorFromName("relationship-data"); diff --git a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIValidate.java b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIValidate.java index 12b6aea7..6af835ee 100644 --- a/aai-core/src/main/java/org/onap/aai/parsers/uri/URIValidate.java +++ b/aai-core/src/main/java/org/onap/aai/parsers/uri/URIValidate.java @@ -21,7 +21,7 @@ package org.onap.aai.parsers.uri; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.enums.EdgeType; import javax.ws.rs.core.MultivaluedMap; diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java b/aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java index b298dbb9..d5faf052 100644 --- a/aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java +++ b/aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java @@ -19,12 +19,6 @@ */ package org.onap.aai.query.builder; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; - import org.apache.tinkerpop.gremlin.process.traversal.P; import org.apache.tinkerpop.gremlin.process.traversal.Path; import org.apache.tinkerpop.gremlin.process.traversal.Traversal; @@ -32,6 +26,7 @@ import org.apache.tinkerpop.gremlin.process.traversal.Traversal.Admin; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; @@ -42,11 +37,17 @@ import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; -import org.onap.aai.serialization.db.EdgeRule; -import org.onap.aai.serialization.db.EdgeRules; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; + +import java.util.*; + /** * The Class GraphTraversalBuilder. */ @@ -54,12 +55,7 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { protected GraphTraversal<Vertex, E> traversal = null; protected Admin<Vertex, E> completeTraversal = null; - private EdgeRules edgeRules = EdgeRules.getInstance(); - - protected int parentStepIndex = 0; - protected int containerStepIndex = 0; - protected int stepIndex = 0; - + /** * Instantiates a new graph traversal builder. * @@ -76,18 +72,6 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { * Instantiates a new graph traversal builder. * * @param loader the loader - */ - public GraphTraversalBuilder(Loader loader, GraphTraversalSource source, EdgeRules edgeRules) { - super(loader, source); - this.edgeRules = edgeRules; - traversal = (GraphTraversal<Vertex, E>) __.<E>start(); - - } - - /** - * Instantiates a new graph traversal builder. - * - * @param loader the loader * @param start the start */ public GraphTraversalBuilder(Loader loader, GraphTraversalSource source, Vertex start) { @@ -98,19 +82,6 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { } /** - * Instantiates a new graph traversal builder. - * - * @param loader the loader - * @param start the start - */ - public GraphTraversalBuilder(Loader loader, GraphTraversalSource source, Vertex start, EdgeRules edgeRules) { - super(loader, source, start); - this.edgeRules = edgeRules; - traversal = (GraphTraversal<Vertex, E>) __.__(start); - - } - - /** * @{inheritDoc} */ @Override @@ -145,6 +116,42 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { * @{inheritDoc} */ @Override + public QueryBuilder<Vertex> getVerticesStartsWithProperty(String key, Object value) { + + // correct value call because the index is registered as an Integer + traversal.has(key, org.janusgraph.core.attribute.Text.textPrefix(value)); + + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + /** + * @{inheritDoc} + */ + @Override + public QueryBuilder<Vertex> getVerticesByProperty(String key) { + + traversal.has(key); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + /** + * @{inheritDoc} + */ + @Override + public QueryBuilder<Vertex> getVerticesExcludeByProperty(String key) { + + traversal.hasNot(key); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + + /** + * @{inheritDoc} + */ + @Override public QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, Object value) { // correct value call because the index is registered as an Integer @@ -172,6 +179,25 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { return (QueryBuilder<Vertex>) this; } + @Override + public QueryBuilder<Vertex> getVerticesGreaterThanProperty(final String key, Object value) { + + traversal.has(key, P.gte(this.correctObjectType(value))); + + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + @Override + public QueryBuilder<Vertex> getVerticesLessThanProperty(final String key, Object value) { + + traversal.has(key, P.lte(this.correctObjectType(value))); + + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + /** * @{inheritDoc} */ @@ -197,6 +223,22 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { return (QueryBuilder<Vertex>) this; } + @Override + public QueryBuilder<Vertex> getVerticesByBooleanProperty(String key, Object value) { + + boolean bValue = false; + + if(value instanceof String){ + bValue = Boolean.valueOf(value.toString()); + } else if(value instanceof Boolean){ + bValue = (Boolean) value; + } + + traversal.has(key, bValue); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + /** * @{inheritDoc} */ @@ -283,16 +325,26 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { */ @Override public QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException { + createTraversal(type, parent, child, false); + return (QueryBuilder<Vertex>) this; + + } + + @Override + public QueryBuilder<Vertex> createPrivateEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException { + this.createTraversal(type, parent, child, true); + return (QueryBuilder<Vertex>) this; + } + + private void createTraversal(EdgeType type, Introspector parent, Introspector child, boolean isPrivateEdge) throws AAIException { String isAbstractType = parent.getMetadata(ObjectMetadata.ABSTRACT); if ("true".equals(isAbstractType)) { markParentBoundary(); - traversal.union(handleAbstractEdge(type, parent, child)); + traversal.union(handleAbstractEdge(type, parent, child, isPrivateEdge)); stepIndex++; } else { this.edgeQueryToVertex(type, parent, child, null); } - return (QueryBuilder<Vertex>) this; - } /** @@ -305,8 +357,7 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { return (QueryBuilder<Vertex>) this; } - - private Traversal<Vertex, Vertex>[] handleAbstractEdge(EdgeType type, Introspector abstractParent, Introspector child) throws AAIException, NoEdgeRuleFoundException { + private Traversal<Vertex, Vertex>[] handleAbstractEdge(EdgeType type, Introspector abstractParent, Introspector child, boolean isPrivateEdge) throws AAIException { String childName = child.getDbName(); String inheritorMetadata = abstractParent.getMetadata(ObjectMetadata.INHERITORS); String[] inheritors = inheritorMetadata.split(","); @@ -314,38 +365,44 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { for (int i = 0; i < inheritors.length; i++) { String inheritor = inheritors[i]; - if (edgeRules.hasEdgeRule(inheritor, childName) || edgeRules.hasEdgeRule(childName, inheritor)) { - Map<String, EdgeRule> rules = edgeRules.getEdgeRules(type, inheritor, childName); + EdgeRuleQuery.Builder qB = new EdgeRuleQuery.Builder(inheritor, childName); + if (edgeRules.hasRule(qB.build())) { + Multimap<String, EdgeRule> rules = ArrayListMultimap.create(); + try { + rules = edgeRules.getRules(qB.edgeType(type).build()); + } catch (EdgeRuleNotFoundException e) { + throw new NoEdgeRuleFoundException(e); + } + GraphTraversal<Vertex, Vertex> innerTraversal = __.start(); - + final List<String> inLabels = new ArrayList<>(); final List<String> outLabels = new ArrayList<>(); - rules.forEach((k,v) -> { - if (v.getDirection().equals(Direction.IN)) { - inLabels.add(k); + rules.values().forEach(rule -> { + if (rule.getDirection().equals(Direction.IN)) { + inLabels.add(rule.getLabel()); } else { - outLabels.add(k); + outLabels.add(rule.getLabel()); } } ); - + if (inLabels.isEmpty() && !outLabels.isEmpty()) { innerTraversal.out(outLabels.toArray(new String[outLabels.size()])); } else if (outLabels.isEmpty() && !inLabels.isEmpty()) { innerTraversal.in(inLabels.toArray(new String[inLabels.size()])); - } else { + } else { innerTraversal.union(__.out(outLabels.toArray(new String[outLabels.size()])), __.in(inLabels.toArray(new String[inLabels.size()]))); } - + innerTraversal.has(AAIProperties.NODE_TYPE, childName); unionTraversals.add(innerTraversal); } } - + return unionTraversals.toArray(new Traversal[unionTraversals.size()]); } - @Override public QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType, List<String> labels) throws AAIException { Introspector outObj = loader.introspectorFromName(outNodeType); Introspector inObj = loader.introspectorFromName(inNodeType); @@ -375,12 +432,26 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { */ @Override public QueryBuilder<E> where(QueryBuilder... builder) { - GraphTraversal<Vertex, Vertex>[] traversals = new GraphTraversal[builder.length]; for (int i = 0; i < builder.length; i++) { this.traversal.where((GraphTraversal<Vertex, Vertex>)builder[i].getQuery()); stepIndex++; } - + + return this; + } + + /** + * @{inheritDoc} + */ + @Override + public QueryBuilder<E> or(QueryBuilder... builder) { + GraphTraversal<Vertex, Vertex>[] traversals = new GraphTraversal[builder.length]; + for (int i = 0; i < builder.length; i++) { + traversals[i] = (GraphTraversal<Vertex, Vertex>)builder[i].getQuery(); + } + this.traversal.or(traversals); + stepIndex++; + return this; } @@ -462,6 +533,15 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { } @Override + public QueryBuilder<Tree> tree() { + + this.traversal.tree(); + stepIndex++; + + return (QueryBuilder<Tree>)this; + } + + @Override public QueryBuilder<E> by(String name) { this.traversal.by(name); stepIndex++; @@ -561,27 +641,45 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { inType = inObj.getChildDBName(); } markParentBoundary(); - Map<String, EdgeRule> rules; + Multimap<String, EdgeRule> rules = ArrayListMultimap.create(); + EdgeRuleQuery.Builder qB = new EdgeRuleQuery.Builder(outType, inType).edgeType(type); + if (labels == null) { - rules = edgeRules.getEdgeRules(type, outType, inType); + try { + rules.putAll(edgeRules.getRules(qB.build())); + } catch (EdgeRuleNotFoundException e) { + //is ok per original functioning of this section + //TODO add "best try" sort of flag to the EdgeRuleQuery + // to indicate if the exception should be thrown or not + } } else { - rules = edgeRules.getEdgeRulesWithLabels(type, outType, inType, labels); + for (String label : labels) { + try { + rules.putAll(edgeRules.getRules(qB.label(label).build())); + } catch (EdgeRuleNotFoundException e) { + throw new NoEdgeRuleFoundException(e); + } + } + if (rules.isEmpty()) { + throw new NoEdgeRuleFoundException("No edge rules found for " + outType + " and " + inType + " of type " + type.toString()); + } } - + + final List<String> inLabels = new ArrayList<>(); final List<String> outLabels = new ArrayList<>(); - rules.forEach((k, edgeRule) -> { - if (labels != null && !labels.contains(k)) { + for (EdgeRule rule : rules.values()) { + if (labels != null && !labels.contains(rule.getLabel())) { return; } else { - if (edgeRule.getDirection().equals(Direction.IN)) { - inLabels.add(edgeRule.getLabel()); + if (Direction.IN.equals(rule.getDirection())) { + inLabels.add(rule.getLabel()); } else { - outLabels.add(edgeRule.getLabel()); + outLabels.add(rule.getLabel()); } } - }); + } if (inLabels.isEmpty() && !outLabels.isEmpty()) { traversal.out(outLabels.toArray(new String[outLabels.size()])); @@ -617,27 +715,35 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { } markParentBoundary(); - Map<String, EdgeRule> rules; - if (labels == null) { - rules = edgeRules.getEdgeRules(type, outType, inType); - } else { - rules = edgeRules.getEdgeRulesWithLabels(type, outType, inType, labels); + Multimap<String, EdgeRule> rules = ArrayListMultimap.create(); + EdgeRuleQuery.Builder qB = new EdgeRuleQuery.Builder(outType, inType).edgeType(type); + + try { + if (labels == null) { + rules.putAll(edgeRules.getRules(qB.build())); + } else { + for (String label : labels) { + rules.putAll(edgeRules.getRules(qB.label(label).build())); + } + } + } catch (EdgeRuleNotFoundException e) { + throw new NoEdgeRuleFoundException(e); } final List<String> inLabels = new ArrayList<>(); final List<String> outLabels = new ArrayList<>(); - rules.forEach((k, edgeRule) -> { - if (labels != null && !labels.contains(k)) { + for (EdgeRule rule : rules.values()) { + if (labels != null && !labels.contains(rule.getLabel())) { return; } else { - if (edgeRule.getDirection().equals(Direction.IN)) { - inLabels.add(edgeRule.getLabel()); + if (Direction.IN.equals(rule.getDirection())) { + inLabels.add(rule.getLabel()); } else { - outLabels.add(edgeRule.getLabel()); + outLabels.add(rule.getLabel()); } } - }); + } if (inLabels.isEmpty() && !outLabels.isEmpty()) { traversal.outE(outLabels.toArray(new String[outLabels.size()])); @@ -715,7 +821,6 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { return stepIndex; } - protected abstract QueryBuilder<E> cloneQueryAtStep(int index); /** * end is exclusive * @@ -724,23 +829,22 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { * @return */ protected abstract QueryBuilder<E> removeQueryStepsBetween(int start, int end); - - private void executeQuery() { - + + protected void executeQuery() { + Admin admin; if (start != null) { this.completeTraversal = traversal.asAdmin(); } else { admin = source.V().asAdmin(); TraversalHelper.insertTraversal(admin.getEndStep(), traversal.asAdmin(), admin); - + this.completeTraversal = (Admin<Vertex, E>) admin; } - - + } - + @Override public boolean hasNext() { if (this.completeTraversal == null) { @@ -764,7 +868,6 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> { if (this.completeTraversal == null) { executeQuery(); } - return this.completeTraversal.toList(); } diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java b/aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java index 010dfa50..683f4729 100644 --- a/aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java +++ b/aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java @@ -19,16 +19,14 @@ */ package org.onap.aai.query.builder; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - import com.google.common.base.Joiner; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; + import org.apache.tinkerpop.gremlin.process.traversal.Path; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; @@ -38,27 +36,23 @@ import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.restcore.search.GremlinGroovyShellSingleton; import org.onap.aai.schema.enums.ObjectMetadata; -import org.onap.aai.serialization.db.EdgeRule; -import org.onap.aai.serialization.db.EdgeRules; -import org.onap.aai.serialization.db.EdgeType; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException; -import com.google.common.base.Joiner; +import java.util.*; /** * The Class GremlinQueryBuilder. */ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { - - private EdgeRules edgeRules = EdgeRules.getInstance(); + private GremlinGroovyShellSingleton gremlinGroovy = GremlinGroovyShellSingleton.getInstance(); private GraphTraversal<?, ?> completeTraversal = null; protected List<String> list = null; - - protected int parentStepIndex = 0; - protected int containerStepIndex = 0; - protected int stepIndex = 0; - + /** * Instantiates a new gremlin query builder. * @@ -68,19 +62,7 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { super(loader, source); list = new ArrayList<>(); } - - /** - * Instantiates a new graph gremlin builder. - * - * @param loader the loader - */ - public GremlinQueryBuilder(Loader loader, GraphTraversalSource source, EdgeRules edgeRules) { - super(loader, source); - this.edgeRules = edgeRules; - list = new ArrayList<>(); - - } - + /** * Instantiates a new gremlin query builder. * @@ -91,20 +73,7 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { super(loader, source, start); list = new ArrayList<>(); } - - /** - * Instantiates a new graph gremlin builder. - * - * @param loader the loader - * @param start the start - */ - public GremlinQueryBuilder(Loader loader, GraphTraversalSource source, Vertex start, EdgeRules edgeRules) { - super(loader, source, start); - this.edgeRules = edgeRules; - list = new ArrayList<>(); - - } - + @Override public QueryBuilder<Vertex> exactMatchQuery(Introspector obj) { // TODO not implemented because this is implementation is no longer used @@ -129,14 +98,28 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { stepIndex++; return (QueryBuilder<Vertex>) this; } - + + @Override + public QueryBuilder<Vertex> getVerticesByBooleanProperty(String key, Object value) { + boolean bValue = false; + + if(value instanceof String){ + bValue = Boolean.valueOf(value.toString()); + } else if(value instanceof Boolean){ + bValue = (Boolean) value; + } + + list.add(".has('" + key + "', " + bValue + ")"); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + /** * @{inheritDoc} */ @Override public QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values) { - String term = ""; String predicate = "P.within(#!#argument#!#)"; List<String> arguments = new ArrayList<>(); for (Object item : values) { @@ -154,6 +137,48 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { } /** + * @{inheritDoc} + */ + @Override + public QueryBuilder<Vertex> getVerticesByProperty(String key) { + + list.add(".has('" + key + "')"); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + /** + * @{inheritDoc} + */ + @Override + public QueryBuilder<Vertex> getVerticesExcludeByProperty(String key) { + + String term = ""; + list.add(".hasNot('" + key + "')"); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + /** + * @{inheritDoc} + */ + @Override + public QueryBuilder<Vertex> getVerticesStartsWithProperty(String key, Object value) { + + String term = ""; + String predicate = "org.janusgraph.core.attribute.Text.textPrefix(#!#argument#!#)"; + if (value != null && !(value instanceof String) ) { + term = value.toString(); + } else { + term = "'" + value + "'"; + } + predicate = predicate.replace("#!#argument#!#", term); + list.add(".has('" + key + "', " + predicate + ")"); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + /** * @{inheritDoc} */ @Override @@ -178,7 +203,6 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { @Override public QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, List<?> values) { - String term = ""; String predicate = "P.without(#!#argument#!#)"; List<String> arguments = new ArrayList<>(); for (Object item : values) { @@ -195,6 +219,39 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { return (QueryBuilder<Vertex>) this; } + @Override + public QueryBuilder<Vertex> getVerticesGreaterThanProperty(String key, Object value) { + String predicate = "P.gte(#!#argument1#!#)"; + String term; + if (value != null && !(value instanceof String) ) { + term = value.toString(); + } else { + term = "'" + value + "'"; + } + predicate = predicate.replace("#!#argument1#!#", term); + list.add(".has('" + key + "', " + predicate + ")"); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + @Override + public QueryBuilder<Vertex> getVerticesLessThanProperty(String key, Object value) { + String predicate = "P.lte(#!#argument1#!#)"; + String term; + if (value != null && !(value instanceof String) ) { + term = value.toString(); + } else { + term = "'" + value + "'"; + } + predicate = predicate.replace("#!#argument1#!#", term); + list.add(".has('" + key + "', " + predicate + ")"); + stepIndex++; + return (QueryBuilder<Vertex>) this; + } + + + + /** * @{inheritDoc} */ @@ -259,6 +316,20 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { } + @Override + public QueryBuilder createPrivateEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException{ + String parentName = parent.getDbName(); + String childName = child.getDbName(); + if (parent.isContainer()) { + parentName = parent.getChildDBName(); + } + if (child.isContainer()) { + childName = child.getChildDBName(); + } + this.edgeQueryToVertex(type, parentName, childName, null, true); + return this; + } + /** * * @{inheritDoc} @@ -283,6 +354,10 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { return (QueryBuilder<Edge>)this; } + private void edgeQueryToVertex(EdgeType type, String outType, String inType, List<String> labels) throws AAIException { + this.edgeQueryToVertex(type, outType, inType, labels, false); + } + /** * Edge query. * @@ -291,29 +366,37 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { * @throws NoEdgeRuleFoundException * @throws AAIException */ - private void edgeQueryToVertex(EdgeType type, String outType, String inType, List<String> labels) throws AAIException { + private void edgeQueryToVertex(EdgeType type, String outType, String inType, List<String> labels, boolean isPrivateEdge) throws AAIException { markParentBoundary(); - Map<String, EdgeRule> rules; - if (labels == null) { - rules = edgeRules.getEdgeRules(type, outType, inType); - } else { - rules = edgeRules.getEdgeRulesWithLabels(type, outType, inType, labels); + Multimap<String, EdgeRule> rules = ArrayListMultimap.create(); + EdgeRuleQuery.Builder qB = new EdgeRuleQuery.Builder(outType, inType).edgeType(type).setPrivate(isPrivateEdge); + + try { + if (labels == null) { + rules.putAll(edgeRules.getRules(qB.build())); + } else { + for (String label : labels) { + rules.putAll(edgeRules.getRules(qB.label(label).build())); + } + } + } catch (EdgeRuleNotFoundException e) { + throw new NoEdgeRuleFoundException(e); } final List<String> inLabels = new ArrayList<>(); final List<String> outLabels = new ArrayList<>(); - rules.forEach((k, edgeRule) -> { - if (labels != null && !labels.contains(k)) { + for (EdgeRule rule : rules.values()) { + if (labels != null && !labels.contains(rule.getLabel())) { return; } else { - if (edgeRule.getDirection().equals(Direction.IN)) { - inLabels.add(edgeRule.getLabel()); + if (Direction.IN.equals(rule.getDirection())) { + inLabels.add(rule.getLabel()); } else { - outLabels.add(edgeRule.getLabel()); + outLabels.add(rule.getLabel()); } } - }); + } if(inLabels.isEmpty() && outLabels.isEmpty()) { throw new NoEdgeRuleFoundException("no " + type.toString() + " edge rule between " + outType + " and " + inType ); @@ -340,27 +423,34 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { */ private void edgeQuery(EdgeType type, String outType, String inType, List<String> labels) throws AAIException { markParentBoundary(); - Map<String, EdgeRule> rules; - if (labels == null) { - rules = edgeRules.getEdgeRules(type, outType, inType); - } else { - rules = edgeRules.getEdgeRulesWithLabels(type, outType, inType, labels); + Multimap<String, EdgeRule> rules = ArrayListMultimap.create(); + EdgeRuleQuery.Builder qB = new EdgeRuleQuery.Builder(outType, inType).edgeType(type); + try { + if (labels == null) { + rules.putAll(edgeRules.getRules(qB.build())); + } else { + for (String label : labels) { + rules.putAll(edgeRules.getRules(qB.label(label).build())); + } + } + } catch (EdgeRuleNotFoundException e) { + throw new NoEdgeRuleFoundException(e); } final List<String> inLabels = new ArrayList<>(); final List<String> outLabels = new ArrayList<>(); - rules.forEach((k, edgeRule) -> { - if (labels != null && !labels.contains(k)) { + for (EdgeRule rule : rules.values()) { + if (labels != null && !labels.contains(rule.getLabel())) { return; } else { - if (edgeRule.getDirection().equals(Direction.IN)) { - inLabels.add(edgeRule.getLabel()); + if (Direction.IN.equals(rule.getDirection())) { + inLabels.add(rule.getLabel()); } else { - outLabels.add(edgeRule.getLabel()); + outLabels.add(rule.getLabel()); } } - }); + } if(inLabels.isEmpty() && outLabels.isEmpty()) { throw new NoEdgeRuleFoundException("no " + type.toString() + " edge rule between " + outType + " and " + inType ); @@ -413,7 +503,7 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { String[] traversals = new String[builder.length]; StringBuilder command = new StringBuilder(); for (int i = 0; i < builder.length; i++) { - traversals[i] = "__" + (String)builder[i].getQuery(); + traversals[i] = "__" + builder[i].getQuery(); } command.append(".union("); command.append(Joiner.on(",").join(traversals)); @@ -429,7 +519,7 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { markParentBoundary(); List<String> traversals = new ArrayList<>(); for (int i = 0; i < builder.length; i++) { - traversals.add(".where(__" + (String)builder[i].getQuery() + ")"); + traversals.add(".where(__" + builder[i].getQuery() + ")"); stepIndex++; } list.addAll(traversals); @@ -437,6 +527,23 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { return this; } + + @Override + public QueryBuilder<E> or(QueryBuilder<E>... builder) { + markParentBoundary(); + String[] traversals = new String[builder.length]; + StringBuilder command = new StringBuilder(); + for (int i = 0; i < builder.length; i++) { + traversals[i] = "__" + builder[i].getQuery(); + } + command.append(".or("); + command.append(Joiner.on(",").join(traversals)); + command.append(")"); + list.add(command.toString()); + stepIndex++; + + return this; + } @Override public QueryBuilder<E> store(String name) { @@ -511,6 +618,14 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { } @Override + public QueryBuilder<Tree> tree() { + this.list.add(".tree()"); + stepIndex++; + + return (QueryBuilder<Tree>)this; + } + + @Override public QueryBuilder<E> by(String name) { this.list.add(".by('"+ name + "')"); stepIndex++; @@ -633,7 +748,6 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> { this.containerStepIndex = stepIndex; } - protected abstract QueryBuilder<E> cloneQueryAtStep(int index); /** * @{inheritDoc} */ diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/GremlinTraversal.java b/aai-core/src/main/java/org/onap/aai/query/builder/GremlinTraversal.java index b24c7af3..22103bda 100644 --- a/aai-core/src/main/java/org/onap/aai/query/builder/GremlinTraversal.java +++ b/aai-core/src/main/java/org/onap/aai/query/builder/GremlinTraversal.java @@ -33,22 +33,12 @@ import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.parsers.query.QueryParser; import org.onap.aai.parsers.query.TraversalStrategy; -import org.onap.aai.serialization.db.EdgeRules; /** * The Class GremlinTraversal. */ public class GremlinTraversal<E> extends GremlinQueryBuilder<E> { - /** - * Instantiates a new gremlin traversal. - * - * @param loader the loader - */ - public GremlinTraversal(Loader loader, GraphTraversalSource source, EdgeRules ers) { - super(loader, source, ers); - this.factory = new TraversalStrategy(this.loader, this); - } /** * Instantiates a new gremlin traversal. @@ -71,16 +61,6 @@ public class GremlinTraversal<E> extends GremlinQueryBuilder<E> { this.factory = new TraversalStrategy(this.loader, this); } - /** - * Instantiates a new gremlin traversal. - * - * @param loader the loader - * @param start the start - */ - public GremlinTraversal(Loader loader, GraphTraversalSource source, Vertex start, EdgeRules ers) { - super(loader, source, start, ers); - this.factory = new TraversalStrategy(this.loader, this); - } protected GremlinTraversal(List<String> traversal, Loader loader, GraphTraversalSource source, GremlinQueryBuilder<E> gtb) { super(loader, source); diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java b/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java index 0350aba2..f4ffac0e 100644 --- a/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java +++ b/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java @@ -19,27 +19,31 @@ */ package org.onap.aai.query.builder; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.ws.rs.core.MultivaluedMap; - import org.apache.tinkerpop.gremlin.process.traversal.Path; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.db.props.AAIProperties; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.parsers.query.QueryParser; import org.onap.aai.parsers.query.QueryParserStrategy; -import org.onap.aai.serialization.db.AAIDirection; -import org.onap.aai.serialization.db.EdgeProperty; -import org.onap.aai.serialization.db.EdgeType; +import org.springframework.context.ApplicationContext; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.edges.enums.EdgeProperty; +import org.onap.aai.edges.enums.EdgeType; + +import javax.ws.rs.core.MultivaluedMap; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.util.Iterator; +import java.util.List; +import java.util.Map; /** * The Class QueryBuilder. @@ -49,9 +53,14 @@ public abstract class QueryBuilder<E> implements Iterator<E> { protected final GraphTraversalSource source; protected QueryParserStrategy factory = null; protected Loader loader = null; + protected EdgeIngestor edgeRules; protected boolean optimize = false; protected Vertex start = null; - + + protected int parentStepIndex = 0; + protected int containerStepIndex = 0; + protected int stepIndex = 0; + /** * Instantiates a new query builder. * @@ -60,6 +69,7 @@ public abstract class QueryBuilder<E> implements Iterator<E> { public QueryBuilder(Loader loader, GraphTraversalSource source) { this.loader = loader; this.source = source; + initEdgeIngestor(); } /** @@ -72,8 +82,15 @@ public abstract class QueryBuilder<E> implements Iterator<E> { this.loader = loader; this.start = start; this.source = source; + initEdgeIngestor(); } - + + public void changeLoader(Loader loader) { + this.loader = loader; + } + + protected abstract QueryBuilder<E> cloneQueryAtStep(int index); + /** * Gets the vertices by indexed property. * @@ -111,6 +128,32 @@ public abstract class QueryBuilder<E> implements Iterator<E> { * @return vertices that match these values */ public abstract QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values); + + /** + * Gets the vertices that have this property key. + * + * @param key the key + * @param value the value + * @return the vertices by property + */ + public abstract QueryBuilder<Vertex> getVerticesByProperty(String key); + + /** + * Gets the vertices that do not have this property key. + * + * @param key the key + * @param value the value + * @return the vertices by property + */ + public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key); + + /** + * filters by elements that start with the value for this property + * @param key + * @param value + * @return vertices that match these values + */ + public abstract QueryBuilder<Vertex> getVerticesStartsWithProperty(String key, Object value); /** * Gets the vertices that are excluded by property. @@ -140,6 +183,23 @@ public abstract class QueryBuilder<E> implements Iterator<E> { public abstract QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, List<?> values); /** + * filters by all the values greater than for this property + * @param key + * @param values + * @return vertices that match these values + */ + public abstract QueryBuilder<Vertex> getVerticesGreaterThanProperty(String key, Object value) ; + + /** + * filters by all the values less than for this property + * @param key + * @param values + * @return vertices that match these values + */ + + public abstract QueryBuilder<Vertex> getVerticesLessThanProperty(String key, Object value) ; + + /** * Gets the child vertices from parent. * * @param parentKey the parent key @@ -194,7 +254,17 @@ public abstract class QueryBuilder<E> implements Iterator<E> { * @return the query builder */ public abstract QueryBuilder<Vertex> createEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException; - + + public abstract QueryBuilder<Vertex> getVerticesByBooleanProperty(String key, Object value); + /** + * Creates the private edge traversal. + * + * @param parent the parent + * @param child the child + * @return the query builder + */ + public abstract QueryBuilder<Vertex> createPrivateEdgeTraversal(EdgeType type, Introspector parent, Introspector child) throws AAIException; + /** * Creates the edge traversal. * @@ -206,7 +276,6 @@ public abstract class QueryBuilder<E> implements Iterator<E> { String nodeType = parent.<String>property(AAIProperties.NODE_TYPE).orElse(null); this.createEdgeTraversal(type, nodeType, child.getDbName()); return (QueryBuilder<Vertex>) this; - } /** @@ -224,6 +293,45 @@ public abstract class QueryBuilder<E> implements Iterator<E> { return createEdgeTraversal(type, out, in); } + public QueryBuilder<Vertex> createEdgeTraversal(String outNodeType, String inNodeType) throws AAIException { + + Introspector out = loader.introspectorFromName(outNodeType); + Introspector in = loader.introspectorFromName(inNodeType); + + QueryBuilder<Vertex> cousinBuilder = null; + QueryBuilder<Vertex> treeBuilder = null; + QueryBuilder<Vertex> queryBuilder = null; + + try { + cousinBuilder = this.newInstance().createEdgeTraversal(EdgeType.COUSIN, out, in); + } catch (AAIException e) { + } + + if(cousinBuilder != null){ + try { + treeBuilder = this.newInstance().createEdgeTraversal(EdgeType.TREE, out, in); + } catch (AAIException e) { + } + if(treeBuilder != null){ + queryBuilder = this.union(new QueryBuilder[]{cousinBuilder, treeBuilder}); + } else { + queryBuilder = this.union(new QueryBuilder[]{cousinBuilder}); + } + } else { + treeBuilder = this.newInstance().createEdgeTraversal(EdgeType.TREE, out, in); + queryBuilder = this.union(new QueryBuilder[]{treeBuilder}); + } + + + return queryBuilder; + } + + public QueryBuilder<Vertex> createPrivateEdgeTraversal(EdgeType type, String outNodeType, String inNodeType) throws AAIException { + Introspector out = loader.introspectorFromName(outNodeType); + Introspector in = loader.introspectorFromName(inNodeType); + return createPrivateEdgeTraversal(type, out, in); + } + /** * * @param type @@ -383,6 +491,8 @@ public abstract class QueryBuilder<E> implements Iterator<E> { public abstract QueryBuilder<E> where(QueryBuilder<E>... builder); + public abstract QueryBuilder<E> or(QueryBuilder<E>... builder); + public abstract QueryBuilder<E> store(String name); public abstract QueryBuilder<E> cap(String name); public abstract QueryBuilder<E> unfold(); @@ -400,6 +510,7 @@ public abstract class QueryBuilder<E> implements Iterator<E> { public abstract QueryBuilder<E> groupCount(); public abstract QueryBuilder<E> by(String name); public abstract QueryBuilder<E> both(); + public abstract QueryBuilder<Tree> tree(); /** * Used to prevent the traversal from repeating its path through the graph. @@ -452,4 +563,15 @@ public abstract class QueryBuilder<E> implements Iterator<E> { } protected abstract QueryBuilder<Edge> has(String key, String value); + + protected void initEdgeIngestor() { + //TODO proper spring wiring, but that requires a lot of refactoring so for now we have this + ApplicationContext ctx = SpringContextAware.getApplicationContext(); + EdgeIngestor ei = ctx.getBean(EdgeIngestor.class); + setEdgeIngestor(ei); + } + + protected void setEdgeIngestor(EdgeIngestor ei) { + this.edgeRules = ei; + } } diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/TraversalQuery.java b/aai-core/src/main/java/org/onap/aai/query/builder/TraversalQuery.java index 677f6586..21d8eb50 100644 --- a/aai-core/src/main/java/org/onap/aai/query/builder/TraversalQuery.java +++ b/aai-core/src/main/java/org/onap/aai/query/builder/TraversalQuery.java @@ -19,12 +19,6 @@ */ package org.onap.aai.query.builder; -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.util.List; - -import javax.ws.rs.core.MultivaluedMap; - import org.apache.tinkerpop.gremlin.process.traversal.Step; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; @@ -34,7 +28,12 @@ import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.parsers.query.QueryParser; import org.onap.aai.parsers.query.TraversalStrategy; -import org.onap.aai.serialization.db.EdgeRules; +import org.onap.aai.edges.enums.EdgeType; + +import javax.ws.rs.core.MultivaluedMap; +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.util.List; /** * The Class TraversalQuery. @@ -55,19 +54,6 @@ public class TraversalQuery<E> extends GraphTraversalBuilder<E> { * Instantiates a new traversal query. * * @param loader the loader - * @param source graph traversal source - * @param edgeRules the edgeRules to use - */ - public TraversalQuery(Loader loader, GraphTraversalSource source, EdgeRules edgeRules) { - super(loader, source, edgeRules); - this.factory = new TraversalStrategy(this.loader, this); - - } - - /** - * Instantiates a new traversal query. - * - * @param loader the loader * @param start the start */ public TraversalQuery(Loader loader, GraphTraversalSource source, Vertex start) { @@ -75,17 +61,6 @@ public class TraversalQuery<E> extends GraphTraversalBuilder<E> { this.factory = new TraversalStrategy(this.loader, this); } - /** - * Instantiates a new traversal query. - * - * @param loader the loader - * @param start the start - */ - public TraversalQuery(Loader loader, GraphTraversalSource source, Vertex start, EdgeRules edgeRules) { - super(loader, source, start, edgeRules); - this.factory = new TraversalStrategy(this.loader, this); - } - protected TraversalQuery(GraphTraversal<Vertex, E> traversal, Loader loader, GraphTraversalSource source, GraphTraversalBuilder<E> gtb) { super(loader, source); this.traversal = traversal; @@ -147,8 +122,13 @@ public class TraversalQuery<E> extends GraphTraversalBuilder<E> { @Override protected QueryBuilder<E> cloneQueryAtStep(int index) { + GraphTraversal.Admin<Vertex, E> cloneAdmin = getCloneAdmin(index); + return new TraversalQuery<>(cloneAdmin, loader, source, this); + } + + protected GraphTraversal.Admin<Vertex, E> getCloneAdmin(int index) { int idx = index; - + if (idx == 0) { idx = stepIndex; } @@ -160,9 +140,9 @@ public class TraversalQuery<E> extends GraphTraversalBuilder<E> { for (int i = steps.size()-1; i >= idx; i--) { cloneAdmin.removeStep(i); } - return new TraversalQuery<>(cloneAdmin, loader, source, this); + return cloneAdmin; } - + @Override protected QueryBuilder<E> removeQueryStepsBetween(int start, int end) { GraphTraversal<Vertex, E> clone = this.traversal.asAdmin().clone(); diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/TraversalURIOptimizedQuery.java b/aai-core/src/main/java/org/onap/aai/query/builder/TraversalURIOptimizedQuery.java new file mode 100644 index 00000000..6a247c2a --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/query/builder/TraversalURIOptimizedQuery.java @@ -0,0 +1,180 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.query.builder; + +import org.apache.tinkerpop.gremlin.process.traversal.Step; +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.util.TraversalHelper; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.introspection.Introspector; +import org.onap.aai.introspection.Loader; +import org.onap.aai.schema.enums.ObjectMetadata; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; + +public class TraversalURIOptimizedQuery<E> extends TraversalQuery { + + protected Map<Integer, String> stepToAaiUri = new HashMap<>(); + + public TraversalURIOptimizedQuery(Loader loader, GraphTraversalSource source) { + super(loader, source); + optimize = true; + } + + public TraversalURIOptimizedQuery(Loader loader, GraphTraversalSource source, Vertex start) { + super(loader, source, start); + optimize = true; + } + + protected TraversalURIOptimizedQuery(GraphTraversal traversal, Loader loader, GraphTraversalSource source, GraphTraversalBuilder gtb) { + super(traversal, loader, source, gtb); + optimize = true; + } + + protected TraversalURIOptimizedQuery(GraphTraversal traversal, Loader loader, GraphTraversalSource source, GraphTraversalBuilder gtb, Map<Integer, String> stepToAaiUri) { + super(traversal, loader, source, gtb); + optimize = gtb.optimize; + this.stepToAaiUri = stepToAaiUri; + } + + @Override + protected void executeQuery() { + + this.completeTraversal = this.traversal.asAdmin().clone(); + + if (this.optimize) { + this.completeTraversal = this.pivotTraversal(this.completeTraversal); + } + + if (start == null) { + Traversal.Admin admin = source.V().asAdmin(); + TraversalHelper.insertTraversal(admin.getEndStep(), completeTraversal, admin); + + this.completeTraversal = (Traversal.Admin<Vertex, E>) admin; + + } + + } + + private Traversal.Admin<Vertex,E> pivotTraversal(Traversal.Admin<Vertex, E> traversalAdmin) { + + List<Step> steps = traversalAdmin.getSteps(); + + Traversal.Admin<Vertex, E> traversalAdminStart = traversalAdmin.clone(); + + //if we do not have an index or other conditions do no optimization + if (stepToAaiUri.isEmpty()) { + return traversalAdmin; + } + + int lastURIStepKey = getLastURIStepKey(); + + //clean up traversal steps + for (int i = 0; i < steps.size(); i++) { + traversalAdminStart.removeStep(0); + } + + ((GraphTraversal<Vertex, E>)traversalAdminStart).has(AAIProperties.AAI_URI, stepToAaiUri.get(lastURIStepKey)); + for (int i = lastURIStepKey; i < steps.size(); i++) { + traversalAdminStart.addStep(steps.get(i)); + } + + return traversalAdminStart; + } + + @Override + public QueryBuilder<Vertex> createKeyQuery(Introspector obj) { + super.createKeyQuery(obj); + + if (shouldAddStepUri(obj)) { + Optional<String> uri = getStepUriFromIntrospector(obj); + if (uri.isPresent()) { + if (stepToAaiUri.isEmpty()) { + stepToAaiUri.put(stepIndex + 1, uri.get()); + } else { + stepToAaiUri.put(stepIndex, uri.get()); + } + } + } else { + optimize = false; + stepToAaiUri = new HashMap<>(); + } + return (QueryBuilder<Vertex>) this; + } + + private boolean shouldAddStepUri(Introspector obj) { + boolean shouldOptimize = optimize; + + if (shouldOptimize && start != null) { + shouldOptimize = false; + } + + if (shouldOptimize && stepToAaiUri.isEmpty() && !obj.isTopLevel()) { + shouldOptimize = false; + } + + if (shouldOptimize && obj.getMetadata(ObjectMetadata.ABSTRACT) != null) { + shouldOptimize = false; + } + + return shouldOptimize; + + } + + private Optional<String> getStepUriFromIntrospector(Introspector obj) { + String uri = ""; + try { + uri = obj.getURI(); + } catch (Exception e) { + } + + if ("".equals(uri)) { + return Optional.empty(); + } + + if (!stepToAaiUri.isEmpty()) { + uri = stepToAaiUri.get(getLastURIStepKey()) + uri; + } + + return Optional.of(uri); + } + + private int getLastURIStepKey() { + return stepToAaiUri.keySet().stream().mapToInt(Integer::intValue).max().getAsInt(); + } + + private Map<Integer, String> getStepToAaiUriWithoutStepGreaterThan(final int index) { + return stepToAaiUri.entrySet().stream().filter(kv -> kv.getKey() <= index).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + + @Override + protected QueryBuilder<E> cloneQueryAtStep(int index) { + GraphTraversal.Admin<Vertex, E> cloneAdmin = getCloneAdmin(index); + return new TraversalURIOptimizedQuery<>(cloneAdmin, loader, source, this, getStepToAaiUriWithoutStepGreaterThan(index)); + } + +} diff --git a/aai-core/src/main/java/org/onap/aai/rest/db/HttpEntry.java b/aai-core/src/main/java/org/onap/aai/rest/db/HttpEntry.java index 28af6a55..dd5d42c6 100644 --- a/aai-core/src/main/java/org/onap/aai/rest/db/HttpEntry.java +++ b/aai-core/src/main/java/org/onap/aai/rest/db/HttpEntry.java @@ -19,6 +19,7 @@ */ package org.onap.aai.rest.db; + import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; @@ -30,8 +31,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.Map.Entry; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; @@ -40,9 +41,16 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriBuilder; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.fge.jsonpatch.JsonPatchException; +import com.github.fge.jsonpatch.mergepatch.JsonMergePatch; import org.apache.commons.lang.StringUtils; import org.apache.tinkerpop.gremlin.structure.Graph; import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraphException; import org.javatuples.Pair; import org.onap.aai.db.props.AAIProperties; import org.onap.aai.dbmap.DBConnectionType; @@ -51,37 +59,30 @@ import org.onap.aai.domain.responseMessage.AAIResponseMessageDatum; import org.onap.aai.exceptions.AAIException; import org.onap.aai.extensions.AAIExtensionMap; import org.onap.aai.extensions.ExtensionController; -import org.onap.aai.introspection.Introspector; -import org.onap.aai.introspection.Loader; -import org.onap.aai.introspection.LoaderFactory; -import org.onap.aai.introspection.MarshallerProperties; -import org.onap.aai.introspection.ModelInjestor; -import org.onap.aai.introspection.ModelType; -import org.onap.aai.introspection.Version; +import org.onap.aai.introspection.*; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.logging.ErrorLogHelper; import org.onap.aai.logging.LoggingContext; +import org.onap.aai.nodes.NodeIngestor; import org.onap.aai.parsers.query.QueryParser; import org.onap.aai.parsers.uri.URIToExtensionInformation; + import org.onap.aai.rest.ueb.UEBNotification; import org.onap.aai.restcore.HttpMethod; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.serialization.db.DBSerializer; -import org.onap.aai.serialization.engines.QueryStyle; import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.QueryStyle; import org.onap.aai.serialization.engines.TransactionalGraphEngine; import org.onap.aai.serialization.engines.query.QueryEngine; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.github.fge.jsonpatch.JsonPatchException; -import com.github.fge.jsonpatch.mergepatch.JsonMergePatch; -import org.janusgraph.core.JanusGraphException; import org.onap.aai.serialization.queryformats.Format; import org.onap.aai.serialization.queryformats.FormatFactory; import org.onap.aai.serialization.queryformats.Formatter; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.util.AAIConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; /** * The Class HttpEntry. @@ -91,40 +92,77 @@ public class HttpEntry { private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(HttpEntry.class); private static final String TARGET_ENTITY = "DB"; - private final ModelType introspectorFactoryType; + private ModelType introspectorFactoryType; - private final QueryStyle queryStyle; + private QueryStyle queryStyle; - private final Version version; + private SchemaVersion version; - private final Loader loader; + private Loader loader; - private final TransactionalGraphEngine dbEngine; + private TransactionalGraphEngine dbEngine; private boolean processSingle = true; + private int paginationBucket = -1; + private int paginationIndex = -1; + private int totalVertices = 0; + private int totalPaginationBuckets = 0; + + @Autowired + private NodeIngestor nodeIngestor; + + @Autowired + private LoaderFactory loaderFactory; + + @Autowired + private SchemaVersions schemaVersions; + + @Value("${schema.uri.base.path}") + private String basePath; + + private UEBNotification notification; + /** * Instantiates a new http entry. * - * @param version the version * @param modelType the model type * @param queryStyle the query style - * @param llBuilder the ll builder */ - public HttpEntry(Version version, ModelType modelType, QueryStyle queryStyle, DBConnectionType connectionType) { + public HttpEntry(ModelType modelType, QueryStyle queryStyle) { this.introspectorFactoryType = modelType; this.queryStyle = queryStyle; + } + + public HttpEntry setHttpEntryProperties(SchemaVersion version, DBConnectionType connectionType){ this.version = version; - this.loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version); + this.loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version); this.dbEngine = new JanusGraphDBEngine( queryStyle, connectionType, loader); - //start transaction on creation - dbEngine.startTransaction(); + getDbEngine().startTransaction(); + this.notification = new UEBNotification(loader, loaderFactory, schemaVersions); + return this; + } + + + public HttpEntry setHttpEntryProperties(SchemaVersion version, DBConnectionType connectionType, UEBNotification notification){ + this.version = version; + this.loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version); + this.dbEngine = new JanusGraphDBEngine( + queryStyle, + connectionType, + loader); + + this.notification = notification; + //start transaction on creation + getDbEngine().startTransaction(); + return this; } + /** * Gets the introspector factory type. * @@ -148,7 +186,7 @@ public class HttpEntry { * * @return the version */ - public Version getVersion() { + public SchemaVersion getVersion() { return version; } @@ -173,6 +211,84 @@ public class HttpEntry { public Pair<Boolean, List<Pair<URI, Response>>> process (List<DBRequest> requests, String sourceOfTruth) throws AAIException { return this.process(requests, sourceOfTruth, true); } + + + /** + * Checks the pagination bucket and pagination index variables to determine whether or not the user + * requested paginated results + * + * @return a boolean true/false of whether the user requested paginated results + */ + public boolean isPaginated(){ + return this.paginationBucket > -1 && this.paginationIndex > -1; + } + + /** + * Setter for the pagination bucket variable which stores in this object the size of results to return + * @param pb + */ + public void setPaginationBucket(int pb){ + this.paginationBucket = pb; + } + + /** + * Returns the pagination size + * @return integer of the size of results to be returned when paginated + */ + public int getPaginationBucket(){ + return this.paginationBucket; + } + + /** + * Sets the pagination index that was passed in by the user, to determine which index or results to retrieve when + * paginated + * @param pi + */ + public void setPaginationIndex(int pi){ + if(pi == 0){ + pi = 1; + } + this.paginationIndex = pi; + } + + /** + * Getter to return the pagination index requested by the user when requesting paginated results + * @return + */ + public int getPaginationIndex(){ + return this.paginationIndex; + } + + /** + * Sets the total vertices variables and calculates the amount of pages based on size and total vertices + * @param totalVertices + * @param paginationBucketSize + */ + public void setTotalsForPaging(int totalVertices, int paginationBucketSize){ + this.totalVertices = totalVertices; + //set total number of buckets equal to full pages + this.totalPaginationBuckets = totalVertices/paginationBucketSize; + //conditionally add a page for the remainder + if(totalVertices % paginationBucketSize > 0){ + this.totalPaginationBuckets++; + } + } + + /** + * @return the total amount of pages + */ + public int getTotalPaginationBuckets(){ + return this.totalPaginationBuckets; + } + + /** + * + * @return the total number of vertices when paginated + */ + public int getTotalVertices(){ + return this.totalVertices; + } + /** * Process. * @param requests the requests @@ -182,16 +298,16 @@ public class HttpEntry { * @throws AAIException the AAI exception */ public Pair<Boolean, List<Pair<URI, Response>>> process (List<DBRequest> requests, String sourceOfTruth, boolean enableResourceVersion) throws AAIException { + DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth); String methodName = "process"; - Response response = null; - Status status = Status.NOT_FOUND; + Response response; Introspector obj = null; QueryParser query = null; URI uri = null; String transactionId = null; - UEBNotification notification = new UEBNotification(loader); int depth = AAIProperties.MAXIMUM_DEPTH; + Format format = null; List<Pair<URI,Response>> responses = new ArrayList<>(); MultivaluedMap<String, String> params = null; HttpMethod method = null; @@ -200,13 +316,15 @@ public class HttpEntry { QueryEngine queryEngine = dbEngine.getQueryEngine(); int maxRetries = 10; int retry = 0; - + LoggingContext.save(); for (DBRequest request : requests) { + response = null; + Status status = Status.NOT_FOUND; + method = request.getMethod(); try { for (retry = 0; retry < maxRetries; ++retry) { try { - method = request.getMethod(); LoggingContext.targetEntity(TARGET_ENTITY); LoggingContext.targetServiceName(methodName + " " + method); @@ -216,15 +334,24 @@ public class HttpEntry { transactionId = request.getTransactionId(); uriTemp = request.getUri().getRawPath().replaceFirst("^v\\d+/", ""); uri = UriBuilder.fromPath(uriTemp).build(); - LoggingContext.startTime(); - - List<Vertex> vertices = query.getQueryBuilder().toList(); + List<Vertex> vertTemp; + List<Vertex> vertices; + if(this.isPaginated()) { + vertTemp = query.getQueryBuilder().toList(); + this.setTotalsForPaging(vertTemp.size(), this.paginationBucket); + vertices = vertTemp.subList(((this.paginationIndex - 1) * this.paginationBucket), Math.min((this.paginationBucket * this.paginationIndex), vertTemp.size())); + }else{ + vertices = query.getQueryBuilder().toList(); + } boolean isNewVertex = false; String outputMediaType = getMediaType(request.getHeaders().getAcceptableMediaTypes()); String result = null; params = request.getInfo().getQueryParameters(false); depth = setDepth(obj, params.getFirst("depth")); + if (params.containsKey("format")) { + format = Format.getFormat(params.getFirst("format")); + } String cleanUp = params.getFirst("cleanup"); String requestContext = ""; List<String> requestContextList = request.getHeaders().getRequestHeader("aai-request-context"); @@ -253,7 +380,7 @@ public class HttpEntry { isNewVertex = true; } else { if (enableResourceVersion) { - serializer.verifyResourceVersion("update", query.getResultType(), (String)vertices.get(0).<String>property("resource-version").orElse(null), resourceVersion, obj.getURI()); + serializer.verifyResourceVersion("update", query.getResultType(), vertices.get(0).<String>property("resource-version").orElse(null), resourceVersion, obj.getURI()); } isNewVertex = false; } @@ -274,30 +401,40 @@ public class HttpEntry { case GET: String nodeOnly = params.getFirst("nodes-only"); boolean isNodeOnly = nodeOnly != null; - obj = this.getObjectFromDb(vertices, serializer, query, obj, request.getUri(), depth, isNodeOnly, cleanUp); - LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs(),TimeUnit.MILLISECONDS); - LOGGER.info ("Completed"); - LoggingContext.restoreIfPossible(); + if (format == null) { + obj = this.getObjectFromDb(vertices, serializer, query, obj, request.getUri(), depth, isNodeOnly, cleanUp); - if (obj != null) { - status = Status.OK; - MarshallerProperties properties; - if (!request.getMarshallerProperties().isPresent()) { - properties = - new MarshallerProperties.Builder(org.onap.aai.restcore.MediaType.getEnum(outputMediaType)).build(); - } else { - properties = request.getMarshallerProperties().get(); + + LoggingContext.elapsedTime((long) serializer.getDBTimeMsecs(), TimeUnit.MILLISECONDS); + LOGGER.info ("Completed"); + LoggingContext.restoreIfPossible(); + + if (obj != null) { + status = Status.OK; + MarshallerProperties properties; + if (!request.getMarshallerProperties().isPresent()) { + properties = new MarshallerProperties.Builder(org.onap.aai.restcore.MediaType.getEnum(outputMediaType)).build(); + } else { + properties = request.getMarshallerProperties().get(); + } + result = obj.marshal(properties); } - result = obj.marshal(properties); + } else { + FormatFactory ff = new FormatFactory(loader, serializer, schemaVersions, basePath + "/"); + Formatter formatter = ff.get(format, params); + result = formatter.output(vertices.stream().map(vertex -> (Object) vertex).collect(Collectors.toList())).toString(); + status = Status.OK; } break; case PUT: + response = this.invokeExtension(dbEngine, this.dbEngine.tx(), method, request, sourceOfTruth, version, loader, obj, uri, true); if (isNewVertex) { v = serializer.createNewVertex(obj); } serializer.serializeToDb(obj, v, query, uri.getRawPath(), requestContext); + this.invokeExtension(dbEngine, this.dbEngine.tx(), HttpMethod.PUT, request, sourceOfTruth, version, loader, obj, uri, false); status = Status.OK; if (isNewVertex) { status = Status.CREATED; @@ -306,24 +443,26 @@ public class HttpEntry { if (query.isDependent()) { relatedObjects = this.getRelatedObjects(serializer, queryEngine, v); } - LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() + + LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() + (long)queryEngine.getDBTimeMsecs(), TimeUnit.MILLISECONDS); - LOGGER.info ("Completed"); + LOGGER.info ("Completed "); LoggingContext.restoreIfPossible(); - notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects); + notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects, basePath); break; case PUT_EDGE: serializer.touchStandardVertexProperties(v, false); + this.invokeExtension(dbEngine, this.dbEngine.tx(), method, request, sourceOfTruth, version, loader, obj, uri, true); serializer.createEdge(obj, v); - + LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs(),TimeUnit.MILLISECONDS); LOGGER.info ("Completed"); LoggingContext.restoreIfPossible(); status = Status.OK; + notification.createNotificationEvent(transactionId, sourceOfTruth, status, new URI(uri.toString().replace("/relationship-list/relationship", "")), serializer.getLatestVersionView(v), relatedObjects, basePath); break; case MERGE_PATCH: - Introspector existingObj = (Introspector) obj.clone(); + Introspector existingObj = loader.introspectorFromName(obj.getDbName()); existingObj = this.getObjectFromDb(vertices, serializer, query, existingObj, request.getUri(), 0, false, cleanUp); String existingJson = existingObj.marshal(false); String newJson; @@ -352,13 +491,13 @@ public class HttpEntry { if (query.isDependent()) { relatedObjects = this.getRelatedObjects(serializer, queryEngine, v); } - LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() + + LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() + (long)queryEngine.getDBTimeMsecs(), TimeUnit.MILLISECONDS); LOGGER.info ("Completed"); LoggingContext.restoreIfPossible(); - notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, patchedObj, relatedObjects); + notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, patchedObj, relatedObjects, basePath); } catch (IOException | JsonPatchException e) { - + LOGGER.info ("Caught exception: " + e.getMessage()); LoggingContext.restoreIfPossible(); throw new AAIException("AAI_3000", "could not perform patch operation"); @@ -372,14 +511,14 @@ public class HttpEntry { } /* * Find all Delete-other-vertex vertices and create structure for notify - * findDeleatble also returns the startVertex v and we dont want to create + * findDeleatble also returns the startVertex v and we dont want to create * duplicate notification events for the same * So remove the startvertex first */ - + List<Vertex> deletableVertices = dbEngine.getQueryEngine().findDeletable(v); Long vId = (Long) v.id(); - + /* * I am assuming vertexId cant be null */ @@ -388,41 +527,44 @@ public class HttpEntry { Map<Vertex, Introspector> deleteObjects = new HashMap<>(); Map<String, URI> uriMap = new HashMap<>(); Map<String, HashMap<String, Introspector>> deleteRelatedObjects = new HashMap<>(); - + if(isDelVerticesPresent){ deleteObjects = this.buildIntrospectorObjects(serializer, deletableVertices); - + uriMap = this.buildURIMap(serializer, deleteObjects); deleteRelatedObjects = this.buildRelatedObjects(serializer, queryEngine, deleteObjects); } - - serializer.delete(v, resourceVersion, enableResourceVersion); - - LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() + + + this.invokeExtension(dbEngine, this.dbEngine.tx(), method, request, sourceOfTruth, version, loader, obj, uri, true); + serializer.delete(v, deletableVertices, resourceVersion, enableResourceVersion); + this.invokeExtension(dbEngine, this.dbEngine.tx(), method, request, sourceOfTruth, version, loader, obj, uri, false); + + LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() + (long)queryEngine.getDBTimeMsecs(), TimeUnit.MILLISECONDS); LOGGER.info ("Completed"); LoggingContext.restoreIfPossible(); status = Status.NO_CONTENT; - notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects); - + notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects, basePath); + /* * Notify delete-other-v candidates */ - + if(isDelVerticesPresent){ this.buildNotificationEvent(sourceOfTruth, status, transactionId, notification, deleteObjects, - uriMap, deleteRelatedObjects); + uriMap, deleteRelatedObjects, basePath); } - + break; case DELETE_EDGE: serializer.touchStandardVertexProperties(v, false); serializer.deleteEdge(obj, v); - + LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs(),TimeUnit.MILLISECONDS); LOGGER.info ("Completed"); LoggingContext.restoreIfPossible(); status = Status.NO_CONTENT; + notification.createNotificationEvent(transactionId, sourceOfTruth, Status.OK, new URI(uri.toString().replace("/relationship-list/relationship", "")), serializer.getLatestVersionView(v), relatedObjects, basePath); break; default: break; @@ -439,10 +581,19 @@ public class HttpEntry { || method.equals(HttpMethod.MERGE_PATCH)) ) { String myvertid = v.id().toString(); - response = Response.status(status) - .header("vertex-id", myvertid) - .entity(result) - .type(outputMediaType).build(); + if(this.isPaginated()){ + response = Response.status(status) + .header("vertex-id", myvertid) + .header("total-results", this.getTotalVertices()) + .header("total-pages", this.getTotalPaginationBuckets()) + .entity(result) + .type(outputMediaType).build(); + }else { + response = Response.status(status) + .header("vertex-id", myvertid) + .entity(result) + .type(outputMediaType).build(); + } } else if (response == null) { response = Response.status(status) .type(outputMediaType).build(); @@ -455,7 +606,7 @@ public class HttpEntry { break; } catch (JanusGraphException e) { this.dbEngine.rollback(); - + LOGGER.info ("Caught exception: " + e.getMessage()); LoggingContext.restoreIfPossible(); AAIException ex = new AAIException("AAI_6142", e); @@ -500,12 +651,11 @@ public class HttpEntry { continue; } } - notification.triggerEvents(); return Pair.with(success, responses); } - + /** * Gets the media type. * @@ -526,7 +676,6 @@ public class HttpEntry { * Gets the object from db. * * @param serializer the serializer - * @param g the g * @param query the query * @param obj the obj * @param uri the uri @@ -557,6 +706,100 @@ public class HttpEntry { } + /** + * Invoke extension. + * + * @param dbEngine the db engine + * @param g the g + * @param httpMethod the http method + * @param fromAppId the from app id + * @param apiVersion the api version + * @param loader the loader + * @param obj the obj + * @param uri the uri + * @param isPreprocess the is preprocess + * @return the response + * @throws IllegalArgumentException the illegal argument exception + * @throws UnsupportedEncodingException the unsupported encoding exception + * @throws AAIException the AAI exception + */ + private Response invokeExtension(TransactionalGraphEngine dbEngine, Graph g, HttpMethod httpMethod, DBRequest request, String fromAppId, SchemaVersion apiVersion, Loader loader, Introspector obj, URI uri, boolean isPreprocess) throws IllegalArgumentException, UnsupportedEncodingException, AAIException { + AAIExtensionMap aaiExtMap = new AAIExtensionMap(); + //ModelInjestor injestor = ModelInjestor.getInstance(); + Response response = null; + URIToExtensionInformation extensionInformation = new URIToExtensionInformation(loader, uri); + aaiExtMap.setHttpEntry(this); + aaiExtMap.setDbRequest(request); + aaiExtMap.setTransId(request.getTransactionId()); + aaiExtMap.setFromAppId(fromAppId); + aaiExtMap.setGraph(g); + aaiExtMap.setApiVersion(apiVersion.toString()); + aaiExtMap.setObjectFromRequest(obj); + aaiExtMap.setObjectFromRequestType(obj.getJavaClassName()); + aaiExtMap.setObjectFromResponse(obj); + aaiExtMap.setObjectFromResponseType(obj.getJavaClassName()); + aaiExtMap.setJaxbContext(nodeIngestor.getContextForVersion(apiVersion)); + aaiExtMap.setUri(uri.getRawPath()); + aaiExtMap.setTransactionalGraphEngine(dbEngine); + aaiExtMap.setLoader(loader); + aaiExtMap.setNamespace(extensionInformation.getNamespace()); + + ExtensionController ext = new ExtensionController(); + ext.runExtension(aaiExtMap.getApiVersion(), + extensionInformation.getNamespace(), + extensionInformation.getTopObject(), + extensionInformation.getMethodName(httpMethod, isPreprocess), + aaiExtMap, + isPreprocess); + + if (aaiExtMap.getPrecheckAddedList().size() > 0) { + response = notifyOnSkeletonCreation(aaiExtMap, obj, request.getHeaders()); + } + + return response; + } + + /** + * Notify on skeleton creation. + * + * @param aaiExtMap the aai ext map + * @param input the input + * @param headers the headers + * @return the response + */ + //Legacy support + private Response notifyOnSkeletonCreation(AAIExtensionMap aaiExtMap, Introspector input, HttpHeaders headers) { + Response response = null; + HashMap<AAIException, ArrayList<String>> exceptionList = new HashMap<AAIException, ArrayList<String>>(); + + StringBuilder keyString = new StringBuilder(); + + Set<String> resourceKeys = input.getKeys(); + for (String key : resourceKeys) { + keyString.append(key).append("=").append(input.getValue(key).toString()).append(" "); + } + + for (AAIResponseMessage msg : aaiExtMap.getPrecheckResponseMessages().getAAIResponseMessage()) { + ArrayList<String> templateVars = new ArrayList<>(); + + templateVars.add("PUT " + input.getDbName()); + templateVars.add(keyString.toString()); + List<String> keys = new ArrayList<>(); + templateVars.add(msg.getAaiResponseMessageResourceType()); + for (AAIResponseMessageDatum dat : msg.getAaiResponseMessageData().getAAIResponseMessageDatum()) { + keys.add(dat.getAaiResponseMessageDatumKey() + "=" + dat.getAaiResponseMessageDatumValue()); + } + templateVars.add(StringUtils.join(keys, ", ")); + exceptionList.put(new AAIException("AAI_0004", msg.getAaiResponseMessageResourceType()), + templateVars); + } + response = Response + .status(Status.ACCEPTED).entity(ErrorLogHelper + .getRESTAPIInfoResponse(headers.getAcceptableMediaTypes(), exceptionList)) + .build(); + + return response; + } /** * Creates the not found message. @@ -580,10 +823,16 @@ public class HttpEntry { * @throws AAIException the AAI exception */ protected int setDepth(Introspector obj, String depthParam) throws AAIException { - int depth = AAIProperties.MAXIMUM_DEPTH; + int depth = AAIProperties.MAXIMUM_DEPTH; + + String getAllRandomStr = AAIConfig.get("aai.rest.getall.depthparam", ""); + if (depthParam != null && getAllRandomStr != null && !getAllRandomStr.isEmpty() + && getAllRandomStr.equals(depthParam)) { + return depth; + } if(depthParam == null){ - if(this.version.compareTo(Version.v9) >= 0){ + if(this.version.compareTo(schemaVersions.getDepthVersion()) >= 0){ depth = 0; } else { depth = AAIProperties.MAXIMUM_DEPTH; @@ -649,7 +898,7 @@ public class HttpEntry { return relatedVertices; } - + private Map<Vertex, Introspector> buildIntrospectorObjects(DBSerializer serializer, Iterable<Vertex> vertices) { Map<Vertex, Introspector> deleteObjectMap = new HashMap<>(); for (Vertex vertex : vertices) { @@ -710,10 +959,10 @@ public class HttpEntry { return relatedObjectsMap; } - + private void buildNotificationEvent(String sourceOfTruth, Status status, String transactionId, UEBNotification notification, Map<Vertex, Introspector> deleteObjects, Map<String, URI> uriMap, - Map<String, HashMap<String, Introspector>> deleteRelatedObjects) { + Map<String, HashMap<String, Introspector>> deleteRelatedObjects, String basePath) { for (Map.Entry<Vertex, Introspector> entry : deleteObjects.entrySet()) { try { String vertexObjectId = ""; @@ -723,14 +972,12 @@ public class HttpEntry { if (uriMap.containsKey(vertexObjectId) && deleteRelatedObjects.containsKey(vertexObjectId)) notification.createNotificationEvent(transactionId, sourceOfTruth, status, - uriMap.get(vertexObjectId), entry.getValue(), deleteRelatedObjects.get(vertexObjectId)); + uriMap.get(vertexObjectId), entry.getValue(), deleteRelatedObjects.get(vertexObjectId), basePath); } } catch (UnsupportedEncodingException | AAIException e) { - LOGGER.warn("Error in sending otification"); + LOGGER.warn("Error in sending notification"); } } } - - } diff --git a/aai-core/src/main/java/org/onap/aai/rest/ueb/NotificationEvent.java b/aai-core/src/main/java/org/onap/aai/rest/ueb/NotificationEvent.java index 5be7818c..5dafebd6 100644 --- a/aai-core/src/main/java/org/onap/aai/rest/ueb/NotificationEvent.java +++ b/aai-core/src/main/java/org/onap/aai/rest/ueb/NotificationEvent.java @@ -22,7 +22,7 @@ package org.onap.aai.rest.ueb; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.util.StoreNotificationEvent; /** @@ -40,7 +40,6 @@ public class NotificationEvent { /** * Instantiates a new notification event. * - * @param version the version * @param eventHeader the event header * @param obj the obj */ @@ -60,7 +59,7 @@ public class NotificationEvent { public void trigger() throws AAIException { StoreNotificationEvent sne = new StoreNotificationEvent(transactionId, sourceOfTruth); - + sne.storeEvent(loader, eventHeader, obj); } @@ -70,7 +69,7 @@ public class NotificationEvent { * * @return the notification version */ - public Version getNotificationVersion() { + public SchemaVersion getNotificationVersion() { return loader.getVersion(); } diff --git a/aai-core/src/main/java/org/onap/aai/rest/ueb/UEBNotification.java b/aai-core/src/main/java/org/onap/aai/rest/ueb/UEBNotification.java index d05b2122..f5f1e14a 100644 --- a/aai-core/src/main/java/org/onap/aai/rest/ueb/UEBNotification.java +++ b/aai-core/src/main/java/org/onap/aai/rest/ueb/UEBNotification.java @@ -27,16 +27,18 @@ import java.util.List; import javax.ws.rs.core.Response.Status; -import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.config.SpringContextAware; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.Loader; import org.onap.aai.introspection.LoaderFactory; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.introspection.exceptions.AAIUnmarshallingException; +import org.onap.aai.logging.LogFormatTools; import org.onap.aai.parsers.uri.URIToObject; -import org.onap.aai.util.AAIConfig; + import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; @@ -49,17 +51,17 @@ public class UEBNotification { private Loader currentVersionLoader = null; protected List<NotificationEvent> events = null; - private Version notificationVersion = null; - + private SchemaVersion notificationVersion = null; /** * Instantiates a new UEB notification. * * @param loader the loader */ - public UEBNotification(Loader loader) { + public UEBNotification(Loader loader, LoaderFactory loaderFactory, SchemaVersions schemaVersions) { events = new ArrayList<>(); - currentVersionLoader = LoaderFactory.createLoaderForVersion(loader.getModelType(), AAIProperties.LATEST); - notificationVersion = Version.valueOf(AAIConfig.get("aai.notification.current.version","v12")); + SchemaVersion defaultVersion = schemaVersions.getDefaultVersion(); + currentVersionLoader = loaderFactory.createLoaderForVersion(loader.getModelType(), defaultVersion); + notificationVersion = defaultVersion; } @@ -71,11 +73,12 @@ public class UEBNotification { * @param status the status * @param uri the uri * @param obj the obj + * @param basePath base URI path * @throws AAIException the AAI exception * @throws IllegalArgumentException the illegal argument exception * @throws UnsupportedEncodingException the unsupported encoding exception */ - public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri, Introspector obj, HashMap<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException { + public void createNotificationEvent(String transactionId, String sourceOfTruth, Status status, URI uri, Introspector obj, HashMap<String, Introspector> relatedObjects, String basePath) throws AAIException, UnsupportedEncodingException { String action = "UPDATE"; @@ -91,12 +94,27 @@ public class UEBNotification { Introspector eventHeader = currentVersionLoader.introspectorFromName("notification-event-header"); URIToObject parser = new URIToObject(currentVersionLoader, uri, relatedObjects); - String entityLink = ""; - if (uri.toString().startsWith("/")) { - entityLink = "/aai/" + notificationVersion + uri; - } else { - entityLink = "/aai/" + notificationVersion + "/" + uri; - } + String entityLink = ""; + if ((basePath != null) && (!basePath.isEmpty())) { + if (!(basePath.startsWith("/"))) { + basePath = "/" + basePath; + } + if (!(basePath.endsWith("/"))) { + basePath = basePath + "/"; + } + } else { + // default + basePath = "/aai/"; + if(LOGGER.isDebugEnabled()){ + LOGGER.debug("Please check the schema.uri.base.path as it didn't seem to be set"); + } + } + + if (uri.toString().startsWith("/")) { + entityLink = basePath + notificationVersion + uri; + } else { + entityLink = basePath + notificationVersion + "/" + uri; + } eventHeader.setValue("entity-link", entityLink); @@ -144,13 +162,12 @@ public class UEBNotification { eventObject = parser.getTopEntity(); } } - final NotificationEvent event = new NotificationEvent(currentVersionLoader, eventHeader, eventObject, transactionId, sourceOfTruth); events.add(event); } catch (AAIUnknownObjectException e) { throw new RuntimeException("Fatal error - notification-event-header object not found!"); } catch (AAIUnmarshallingException e) { - LOGGER.error("Unmarshalling error occurred while generating UEBNotification", e); + LOGGER.error("Unmarshalling error occurred while generating UEBNotification " + LogFormatTools.getStackTop(e)); } } diff --git a/aai-core/src/main/java/org/onap/aai/restcore/search/AAIAbstractGroovyShell.java b/aai-core/src/main/java/org/onap/aai/restcore/search/AAIAbstractGroovyShell.java new file mode 100644 index 00000000..4703331c --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/restcore/search/AAIAbstractGroovyShell.java @@ -0,0 +1,80 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.restcore.search; + +import groovy.lang.GroovyShell; +import groovy.transform.TimedInterrupt; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; +import org.codehaus.groovy.ast.ClassHelper; +import org.codehaus.groovy.ast.expr.ClassExpression; +import org.codehaus.groovy.ast.expr.PropertyExpression; +import org.codehaus.groovy.control.CompilerConfiguration; +import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer; +import org.codehaus.groovy.control.customizers.ImportCustomizer; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +public abstract class AAIAbstractGroovyShell { + + protected final GroovyShell shell; + + public AAIAbstractGroovyShell() { + Map<String, Object> parameters = new HashMap<>(); + parameters.put("value", 30000); + parameters.put("unit", new PropertyExpression(new ClassExpression(ClassHelper.make(TimeUnit.class)),"MILLISECONDS")); + + ASTTransformationCustomizer custom = new ASTTransformationCustomizer(parameters, TimedInterrupt.class); + ImportCustomizer imports = new ImportCustomizer(); + imports.addStaticStars( + "org.apache.tinkerpop.gremlin.process.traversal.P", + "org.apache.tinkerpop.gremlin.process.traversal.Order" + ); + imports.addImports( + "org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__", + "org.apache.tinkerpop.gremlin.structure.T", + "org.apache.tinkerpop.gremlin.process.traversal.P", + "org.onap.aai.edges.enums.EdgeType", + "java.util.Map.Entry"); + imports.addStarImports("java.util"); + CompilerConfiguration config = new CompilerConfiguration(); + config.addCompilationCustomizers(custom, imports); + + this.shell = new GroovyShell(config); + } + + /** + * + * @param engine + * @param traversal + * @param params + * @return result of graph traversal + */ + public abstract String executeTraversal (TransactionalGraphEngine engine, String traversal, Map<String, Object> params); + + /** + * @param traversal + * @param params + * @return result of graph traversal + */ + public abstract GraphTraversal<?, ?> executeTraversal (String traversal, Map<String, Object> params); +} diff --git a/aai-core/src/main/java/org/onap/aai/restcore/search/GremlinGroovyShellSingleton.java b/aai-core/src/main/java/org/onap/aai/restcore/search/GremlinGroovyShellSingleton.java index 521a5bea..328429b1 100644 --- a/aai-core/src/main/java/org/onap/aai/restcore/search/GremlinGroovyShellSingleton.java +++ b/aai-core/src/main/java/org/onap/aai/restcore/search/GremlinGroovyShellSingleton.java @@ -19,54 +19,25 @@ */ package org.onap.aai.restcore.search; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeUnit; - -import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; -import org.codehaus.groovy.ast.ClassHelper; -import org.codehaus.groovy.ast.expr.ClassExpression; -import org.codehaus.groovy.ast.expr.PropertyExpression; -import org.codehaus.groovy.control.CompilerConfiguration; -import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer; -import org.codehaus.groovy.control.customizers.ImportCustomizer; - import groovy.lang.Binding; -import groovy.lang.GroovyShell; import groovy.lang.Script; -import groovy.transform.TimedInterrupt; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import java.util.Map; /** * Creates and returns a groovy shell with the * configuration to statically import graph classes * */ -public class GremlinGroovyShellSingleton { +public class GremlinGroovyShellSingleton extends AAIAbstractGroovyShell { - private final GroovyShell shell; private GremlinGroovyShellSingleton() { - Map<String, Object> parameters = new HashMap<>(); - parameters.put("value", 30000); - parameters.put("unit", new PropertyExpression(new ClassExpression(ClassHelper.make(TimeUnit.class)),"MILLISECONDS")); - - ASTTransformationCustomizer custom = new ASTTransformationCustomizer(parameters, TimedInterrupt.class); - ImportCustomizer imports = new ImportCustomizer(); - imports.addStaticStars( - "org.apache.tinkerpop.gremlin.process.traversal.P" - ); - imports.addImports( - "org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__", - "org.apache.tinkerpop.gremlin.structure.T", - "org.apache.tinkerpop.gremlin.process.traversal.P", - "java.util.Map.Entry"); - imports.addStarImports("java.util"); - CompilerConfiguration config = new CompilerConfiguration(); - config.addCompilationCustomizers(custom, imports); - - this.shell = new GroovyShell(config); + super(); } - - private static class Helper { + + private static class Helper { private static final GremlinGroovyShellSingleton INSTANCE = new GremlinGroovyShellSingleton(); } @@ -76,14 +47,21 @@ public class GremlinGroovyShellSingleton { } /** - * @param traversal - * @param params - * @return result of graph traversal + * {@inheritDoc} */ + @Override public GraphTraversal<?, ?> executeTraversal (String traversal, Map<String, Object> params) { Binding binding = new Binding(params); Script script = shell.parse(traversal); script.setBinding(binding); return (GraphTraversal<?, ?>) script.run(); } + + /** + * @throws UnsupportedOperationException + */ + @Override + public String executeTraversal(TransactionalGraphEngine engine, String traversal, Map<String, Object> params) { + throw new UnsupportedOperationException(); + } } diff --git a/aai-core/src/main/java/org/onap/aai/restcore/search/GroovyQueryBuilderSingleton.java b/aai-core/src/main/java/org/onap/aai/restcore/search/GroovyQueryBuilderSingleton.java new file mode 100644 index 00000000..03a8bfc3 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/restcore/search/GroovyQueryBuilderSingleton.java @@ -0,0 +1,84 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.restcore.search; + +import groovy.lang.Binding; +import groovy.lang.Script; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.query.builder.QueryBuilder; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; + +import java.util.Map; + +/** + * Creates and returns a groovy shell with the + * configuration to statically import graph classes + * + */ +public class GroovyQueryBuilderSingleton extends AAIAbstractGroovyShell { + + private GroovyQueryBuilderSingleton() { + super(); + } + + private static class Helper { + private static final GroovyQueryBuilderSingleton INSTANCE = new GroovyQueryBuilderSingleton(); + } + + public static GroovyQueryBuilderSingleton getInstance() { + + return Helper.INSTANCE; + } + + /** + * {@inheritDoc} + */ + @Override + public String executeTraversal (TransactionalGraphEngine engine, String traversal, Map<String, Object> params) { + QueryBuilder<Vertex> builder = engine.getQueryBuilder(QueryStyle.GREMLIN_TRAVERSAL); + SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion()); + + builder.changeLoader(loader); + Binding binding = new Binding(params); + binding.setVariable("builder", builder); + Script script = shell.parse(traversal); + script.setBinding(binding); + script.run(); + + return builder.getQuery(); + } + + /** + * @throws UnsupportedOperationException + */ + @Override + public GraphTraversal<?, ?> executeTraversal(String traversal, Map<String, Object> params) { + throw new UnsupportedOperationException(); + } +} diff --git a/aai-core/src/main/java/org/onap/aai/restcore/util/EdgeRuleBean.java b/aai-core/src/main/java/org/onap/aai/restcore/util/EdgeRuleBean.java deleted file mode 100644 index 69332f32..00000000 --- a/aai-core/src/main/java/org/onap/aai/restcore/util/EdgeRuleBean.java +++ /dev/null @@ -1,94 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.restcore.util; - -public class EdgeRuleBean { - private String from; - private String to; - private String label; - private String direction; - private String multiplicity; - private String lineage; - private String preventDelete; - private String deleteOtherV; - private String svcInfra; - private String defaultVal; - - public String getFrom() { - return from; - } - public void setFrom(String from) { - this.from = from; - } - public String getTo() { - return to; - } - public void setTo(String to) { - this.to = to; - } - public String getLabel() { - return label; - } - public void setLabel(String label) { - this.label = label; - } - public String getDirection() { - return direction; - } - public void setDirection(String direction) { - this.direction = direction; - } - public String getMultiplicity() { - return multiplicity; - } - public void setMultiplicity(String multiplicity) { - this.multiplicity = multiplicity; - } - public String getDeleteOtherV() { - return deleteOtherV; - } - public void setDeleteOtherV(String deleteOtherV) { - this.deleteOtherV = deleteOtherV; - } - public String getPreventDelete() { - return preventDelete; - } - public void setPreventDelete(String preventDelete) { - this.preventDelete = preventDelete; - } - public String getSvcInfra() { - return svcInfra; - } - public void setSvcInfra(String svcInfra) { - this.svcInfra = svcInfra; - } - public String getLineage() { - return lineage; - } - public void setLineage(String lineage) { - this.lineage = lineage; - } - public String getDefault() { - return defaultVal; - } - public void setDefault(String defaultVal) { - this.defaultVal = defaultVal; - } -} diff --git a/aai-core/src/main/java/org/onap/aai/restcore/util/GenerateEdgeRules.java b/aai-core/src/main/java/org/onap/aai/restcore/util/GenerateEdgeRules.java deleted file mode 100644 index 4884f400..00000000 --- a/aai-core/src/main/java/org/onap/aai/restcore/util/GenerateEdgeRules.java +++ /dev/null @@ -1,187 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.restcore.util; - -import freemarker.template.Configuration; -import freemarker.template.Template; -import freemarker.template.TemplateException; - -import java.io.*; -import java.util.*; - -import org.onap.aai.serialization.db.EdgeRules; -import org.onap.aai.introspection.Version; - -public class GenerateEdgeRules { - - public static void main(String[] args) throws IOException, TemplateException { - - String filename = "/edgeLabelMigration.csv"; - InputStream inputStream = GenerateEdgeRules.class.getResourceAsStream(filename); - Map<String, Integer> headers = new HashMap<>(); - - List<EdgeRuleBean> rulesToWriteV12 = new ArrayList<>(); - List<EdgeRuleBean> rulesToWriteV7 = new ArrayList<>(); - List<EdgeRuleBean> rulesToWriteV8 = new ArrayList<>(); - List<EdgeRuleBean> rulesToWriteV9 = new ArrayList<>(); - List<EdgeRuleBean> rulesToWriteV10 = new ArrayList<>(); - List<EdgeRuleBean> rulesToWriteV11 = new ArrayList<>(); - - ArrayList <String> rulesWeAlreadyHave = new ArrayList <String>(); - - EdgeRules rulesV8 = EdgeRules.getInstance(Version.v8); - EdgeRules rulesV9 = EdgeRules.getInstance(Version.v9); - EdgeRules rulesV10 = EdgeRules.getInstance(Version.v10); - EdgeRules rulesV11 = EdgeRules.getInstance(Version.v11); - - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { - - String line = null; - int rowNum = 0; - while ((line = reader.readLine()) != null) { - if (rowNum == 0) { - headers = retrieveHeaderMap(line); - } - else { - EdgeRuleBean data = new EdgeRuleBean(); - String[] columns = line.split(","); - String oldNodeA = columns[headers.get("from")]; - String oldNodeB = columns[headers.get("to")]; - String oldEdgeLabel = columns[headers.get("label")]; - - String nodeA = columns[headers.get("new from")]; - data.setFrom(nodeA); - String nodeB = columns[headers.get("new to")]; - data.setTo(nodeB); - - String edgeLabel = columns[headers.get("new label")]; - data.setLabel( edgeLabel ); - - - // Note: it is assumed that if we know the two NodeTypes and the edgeLabel, we can - // uniquely identify an edgeRule -- so if that key is found twice, it is a - // problem with our CSV file. Note -we check with the nodeTypes in both directions. - String key1 = nodeA + "|" + nodeB + "|" + edgeLabel; - String key2 = nodeB + "|" + nodeA + "|" + edgeLabel; - if( rulesWeAlreadyHave.contains(key1) ){ - throw new Exception ("Duplicate rule found for [" + key1 + "] -- please fix the CSV file. "); - } - else if( rulesWeAlreadyHave.contains(key2) ){ - throw new Exception ("Duplicate rule found for [" + key2 + "] -- please fix the CSV file. "); - } - else { - rulesWeAlreadyHave.add(key1); - rulesWeAlreadyHave.add(key2); - } - - String direction = columns[headers.get("new direction")]; - data.setDirection(direction); - - String multiplicity = columns[headers.get("new multiplicity")]; - data.setMultiplicity(multiplicity); - - String lineage = columns[headers.get("new contains-other-v")]; - data.setLineage(lineage); - - String deleteOtherV = columns[headers.get("new delete-other-v")]; - data.setDeleteOtherV(deleteOtherV); - - String svcInfra = columns[headers.get("new SVC-INFRA")]; - data.setSvcInfra(svcInfra); - - String prevDel = columns[headers.get("new prevent-delete")]; - data.setPreventDelete(prevDel); - - String defaultVal = columns[headers.get("new default")]; - if( defaultVal.equals("T") ){ - data.setDefault("true"); - } - else if( defaultVal.equals("F") ){ - data.setDefault("false"); - } - - rulesToWriteV12.add(data); - - if( rulesV8.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){ - rulesToWriteV8.add(data); - } - - if( rulesV9.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){ - rulesToWriteV9.add(data); - } - - if( rulesV10.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){ - rulesToWriteV10.add(data); - } - - if( rulesV11.hasEdgeRule(oldNodeA, oldNodeB, oldEdgeLabel) ){ - rulesToWriteV11.add(data); - } - } - ++rowNum; - } - - Configuration configuration = new Configuration(); - Template template = configuration.getTemplate("src/main/resources/edgerulesTemplate.ftlh"); - - saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v12.json", template, rulesToWriteV12); - saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v7.json", template, rulesToWriteV7); - saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v8.json", template, rulesToWriteV8); - saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v9.json", template, rulesToWriteV9); - saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v10.json", template, rulesToWriteV10); - saveRulesIntoTheFile("src/main/resources/EdgeRulesWithNewLabels_v11.json", template, rulesToWriteV11); - - } catch(Exception ex){ - ex.printStackTrace(); - } - - - } - - private static void saveRulesIntoTheFile(String filePath, Template fileTemplate, List<EdgeRuleBean> rulesToWrite) - throws IOException, TemplateException { - - - try (Writer file = new FileWriter(new File(filePath))) { - Map<String, List<EdgeRuleBean>> wrappedRules = new HashMap<>(); - wrappedRules.put("wrappedRules", rulesToWrite); - fileTemplate.process(wrappedRules, file); - } - } - - private static Map<String, Integer> retrieveHeaderMap(String line){ - - if(line == null) - throw new NullPointerException(); - - String[] columnNames = line.split(","); - - Map<String, Integer> map = new HashMap<String, Integer>(); - - int index = 0; - - for(String columnName : columnNames){ - map.put(columnName, index++); - } - - return map; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/restcore/util/URITools.java b/aai-core/src/main/java/org/onap/aai/restcore/util/URITools.java index 8e8af55b..c12c080b 100644 --- a/aai-core/src/main/java/org/onap/aai/restcore/util/URITools.java +++ b/aai-core/src/main/java/org/onap/aai/restcore/util/URITools.java @@ -54,6 +54,9 @@ public class URITools { for (String section : sections) { query = section.split("="); key = UriUtils.decode(query[0], "UTF-8"); + if(query[1] != null){ + query[1] = query[1].replaceAll("\\+", "%20"); + } value = UriUtils.decode(query[1], "UTF-8"); if (result.containsKey(key)) { result.add(key, value); diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/AAIDirection.java b/aai-core/src/main/java/org/onap/aai/serialization/db/AAIDirection.java deleted file mode 100644 index 0a2b1ab0..00000000 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/AAIDirection.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.serialization.db; - -public enum AAIDirection { - IN, OUT, BOTH, NONE; - - public AAIDirection opposite() { - if (this.equals(OUT)) - return IN; - else if (this.equals(IN)) - return OUT; - else - return BOTH; - } -} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java b/aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java index 67a5edb3..e8c03bdd 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/db/DBSerializer.java @@ -32,30 +32,40 @@ import org.apache.tinkerpop.gremlin.structure.*; import org.javatuples.Pair; import org.javatuples.Triplet; import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.edges.enums.EdgeField; +import org.onap.aai.edges.enums.EdgeProperty; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.*; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; -import org.onap.aai.introspection.sideeffect.DataCopy; -import org.onap.aai.introspection.sideeffect.DataLinkReader; -import org.onap.aai.introspection.sideeffect.DataLinkWriter; -import org.onap.aai.introspection.sideeffect.SideEffectRunner; +import org.onap.aai.introspection.sideeffect.*; import org.onap.aai.logging.ErrorLogHelper; import org.onap.aai.logging.LogFormatTools; +import org.onap.aai.logging.StopWatch; import org.onap.aai.parsers.query.QueryParser; import org.onap.aai.parsers.uri.URIParser; import org.onap.aai.parsers.uri.URIToRelationshipObject; import org.onap.aai.query.builder.QueryBuilder; import org.onap.aai.schema.enums.ObjectMetadata; import org.onap.aai.schema.enums.PropertyMetadata; +import org.onap.aai.serialization.db.exceptions.MultipleEdgeRuleFoundException; import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException; -import org.onap.aai.serialization.db.util.VersionChecker; import org.onap.aai.serialization.engines.TransactionalGraphEngine; import org.onap.aai.serialization.tinkerpop.TreeBackedVertex; -import org.onap.aai.util.AAIApiServerURLBase; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; import org.onap.aai.util.AAIConfig; import org.onap.aai.util.AAIConstants; import org.onap.aai.workarounds.NamingExceptions; -import org.onap.aai.logging.StopWatch; +import org.springframework.context.ApplicationContext; +import org.onap.aai.concurrent.AaiCallable; +import org.onap.aai.config.SpringContextAware; import javax.ws.rs.core.UriBuilder; import java.io.UnsupportedEncodingException; @@ -65,12 +75,12 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.util.*; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.onap.aai.serialization.engines.query.QueryEngine; public class DBSerializer { @@ -79,33 +89,66 @@ public class DBSerializer { private final TransactionalGraphEngine engine; private final String sourceOfTruth; private final ModelType introspectionType; - private final Version version; + private final SchemaVersion version; private final Loader latestLoader; - private final EdgeRules edgeRules = EdgeRules.getInstance(); + private EdgeSerializer edgeSer; + private EdgeIngestor edgeRules; private final Loader loader; private final String baseURL; private double dbTimeMsecs = 0; + + private SchemaVersions schemaVersions; /** * Instantiates a new DB serializer. * * @param version the version * @param engine the engine - * @param g the g * @param introspectionType the introspection type * @param sourceOfTruth the source of truth - * @param llBuilder the ll builder - * @throws AAIException + * @throws AAIException */ - public DBSerializer(Version version, TransactionalGraphEngine engine, ModelType introspectionType, String sourceOfTruth) throws AAIException { + public DBSerializer(SchemaVersion version, TransactionalGraphEngine engine, ModelType introspectionType, String sourceOfTruth) throws AAIException { this.engine = engine; this.sourceOfTruth = sourceOfTruth; this.introspectionType = introspectionType; - this.latestLoader = LoaderFactory.createLoaderForVersion(introspectionType, AAIProperties.LATEST); + this.schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + SchemaVersion LATEST = schemaVersions.getDefaultVersion(); + this.latestLoader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectionType, LATEST); this.version = version; - this.loader = LoaderFactory.createLoaderForVersion(introspectionType, version); - this.baseURL = AAIApiServerURLBase.get(version); + this.loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectionType, version); + this.baseURL = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE); + initBeans(); } - + + private void initBeans() { + //TODO proper spring wiring, but that requires a lot of refactoring so for now we have this + ApplicationContext ctx = SpringContextAware.getApplicationContext(); + EdgeIngestor ei = ctx.getBean(EdgeIngestor.class); + setEdgeIngestor(ei); + EdgeSerializer es = ctx.getBean(EdgeSerializer.class); + setEdgeSerializer(es); + } + + private void backupESInit() { + setEdgeSerializer(new EdgeSerializer(this.edgeRules)); + } + + public void setEdgeSerializer(EdgeSerializer edgeSer) { + this.edgeSer = edgeSer; + } + + public EdgeSerializer getEdgeSeriailizer() { + return this.edgeSer; + } + + public void setEdgeIngestor(EdgeIngestor ei) { + this.edgeRules = ei; + } + + public EdgeIngestor getEdgeIngestor(){ + return this.edgeRules; + } + /** * Touch standard vertex properties. * @@ -223,7 +266,7 @@ public class DBSerializer { try { boolean isTopLevel = obj.isTopLevel(); if (isTopLevel) { - v.property(AAIProperties.AAI_URI, obj.getURI()); + addUriIfNeeded(v, obj.getURI()); } processObject(obj, v, requestContext); @@ -231,10 +274,7 @@ public class DBSerializer { URI uri = this.getURIForVertex(v); URIParser parser = new URIParser(this.loader, uri); if (parser.validate()) { - VertexProperty<String> uriProp = v.property(AAIProperties.AAI_URI); - if (!uriProp.isPresent() || uriProp.isPresent() && !uriProp.value().equals(uri.toString())) { - v.property(AAIProperties.AAI_URI, uri.toString()); - } + addUriIfNeeded(v, uri.toString()); } } } catch (SchemaViolationException e) { @@ -244,7 +284,14 @@ public class DBSerializer { dbTimeMsecs += StopWatch.stopIfStarted(); } } - + + private void addUriIfNeeded(Vertex v, String uri) { + VertexProperty<String> uriProp = v.property(AAIProperties.AAI_URI); + if (!uriProp.isPresent() || (uriProp.isPresent() && !uriProp.value().equals(uri))) { + v.property(AAIProperties.AAI_URI, uri); + } + } + /** * Process object. * @@ -433,11 +480,15 @@ public class DBSerializer { } if (cousinVertex != null) { - - if (!edgeRules.hasEdgeRule(v, cousinVertex, label)) { + String vType = (String)v.property(AAIProperties.NODE_TYPE).value(); + String cousinType = (String)cousinVertex.property(AAIProperties.NODE_TYPE).value(); + EdgeRuleQuery.Builder baseQ = new EdgeRuleQuery.Builder(vType, cousinType).label(label); + + + if (!edgeRules.hasRule(baseQ.build())) { throw new AAIException("AAI_6120", "No EdgeRule found for passed nodeTypes: " + v.property(AAIProperties.NODE_TYPE).value().toString() + ", " + cousinVertex.property(AAIProperties.NODE_TYPE).value().toString() + (label != null ? (" with label " + label):"") +"."); - } else if (edgeRules.hasTreeEdgeRule(v, cousinVertex) && !edgeRules.hasCousinEdgeRule(v, cousinVertex, label)) { + } else if (edgeRules.hasRule(baseQ.edgeType(EdgeType.TREE).build()) && !edgeRules.hasRule(baseQ.edgeType(EdgeType.COUSIN).build())) { throw new AAIException("AAI_6145"); } @@ -456,7 +507,7 @@ public class DBSerializer { } for (Triplet<Vertex, Vertex, String> triplet : addEdges) { try { - edgeRules.addEdge(this.engine.asAdmin().getTraversalSource(), triplet.getValue0(), triplet.getValue1(), triplet.getValue2()); + edgeSer.addEdge(this.engine.asAdmin().getTraversalSource(), triplet.getValue0(), triplet.getValue1(), triplet.getValue2()); } catch (NoEdgeRuleFoundException e) { throw new AAIException("AAI_6129", e); } @@ -554,7 +605,7 @@ public class DBSerializer { if (parentUri != null) { String uri; uri = obj.getURI(); - child.property(AAIProperties.AAI_URI, parentUri + uri); + addUriIfNeeded(child, parentUri + uri); } processObject(obj, child, requestContext); @@ -563,25 +614,25 @@ public class DBSerializer { if (e == null) { String canBeLinked = obj.getMetadata(ObjectMetadata.CAN_BE_LINKED); if (canBeLinked != null && canBeLinked.equals("true")) { - Loader ldrForCntxt = LoaderFactory.createLoaderForVersion(introspectionType, getVerForContext(requestContext)); + Loader ldrForCntxt = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectionType, getVerForContext(requestContext)); boolean isFirst = !this.engine.getQueryBuilder(ldrForCntxt, parent).createEdgeTraversal(EdgeType.TREE, parent, obj).hasNext(); if (isFirst) { child.property(AAIProperties.LINKED, true); } } - edgeRules.addTreeEdge(this.engine.asAdmin().getTraversalSource(), parent, child); + edgeSer.addTreeEdge(this.engine.asAdmin().getTraversalSource(), parent, child); } return child; } - private Version getVerForContext(String requestContext) { + private SchemaVersion getVerForContext(String requestContext) { Pattern pattern = Pattern.compile("v[0-9]+"); Matcher m = pattern.matcher(requestContext); if (!m.find()) { return this.version; } else { - return Version.valueOf(requestContext); + return new SchemaVersion(requestContext); } } @@ -635,16 +686,31 @@ public class DBSerializer { List<Future<Object>> futures = new ArrayList<>(); - + QueryEngine tgEngine = this.engine.getQueryEngine(); for (Vertex v : vertices) { - Callable<Object> task = () -> { - Set<Vertex> seen = new HashSet<>(); - Introspector childObject = obj.newIntrospectorInstanceOfNestedProperty(propertyName); - Tree<Element> tree = this.engine.getQueryEngine().findSubGraph(v, internalDepth, nodeOnly); - TreeBackedVertex treeVertex = new TreeBackedVertex(v, tree); - dbToObject(childObject, treeVertex, seen, internalDepth, nodeOnly, cleanUp); - return childObject.getUnderlyingObject(); - //getList.add(childObject.getUnderlyingObject()); + + AaiCallable<Object> task = new AaiCallable<Object>() { + @Override + public Object process() throws UnsupportedEncodingException, AAIException { + Set<Vertex> seen = new HashSet<>(); + Introspector childObject; + try { + childObject = obj.newIntrospectorInstanceOfNestedProperty(propertyName); + } catch (AAIUnknownObjectException e) { + throw e; + } + Tree<Element> tree = tgEngine.findSubGraph(v, internalDepth, nodeOnly); + TreeBackedVertex treeVertex = new TreeBackedVertex(v, tree); + try { + dbToObject(childObject, treeVertex, seen, internalDepth, nodeOnly, cleanUp); + } catch (UnsupportedEncodingException e) { + throw e; + } catch (AAIException e) { + throw e; + } + return childObject.getUnderlyingObject(); + //getList.add(childObject.getUnderlyingObject()); + } }; futures.add(pool.submit(task)); } @@ -743,7 +809,13 @@ public class DBSerializer { String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null); EdgeRule rule; - rule = edgeRules.getEdgeRule(EdgeType.TREE, vType, childDbName); + try { + rule = edgeRules.getRule(new EdgeRuleQuery.Builder(vType, childDbName).edgeType(EdgeType.TREE).build()); + } catch (EdgeRuleNotFoundException e) { + throw new NoEdgeRuleFoundException(e); + } catch (AmbiguousRuleChoiceException e) { + throw new MultipleEdgeRuleFoundException(e); + } if (!rule.getContains().equals(AAIDirection.NONE.toString())) { //vertices = this.queryEngine.findRelatedVertices(v, Direction.OUT, rule.getLabel(), childDbName); Direction ruleDirection = rule.getDirection(); @@ -854,14 +926,18 @@ public class DBSerializer { * @throws SecurityException the security exception */ private void copySimpleProperty(String property, Introspector obj, Vertex v) { + final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(property); String dbPropertyName = property; + if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) { dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS); } + + + final Object temp = v.<Object>property(dbPropertyName).orElse(null); if (temp != null) { - obj.setValue(property, temp); } } @@ -913,7 +989,7 @@ public class DBSerializer { List<Object> relationshipObjList = obj.getValue("relationship"); for (Vertex cousin : cousins) { - if (VersionChecker.apiVersionNeedsEdgeLabel(obj.getVersion())) { + if (obj.getVersion().compareTo(schemaVersions.getEdgeLabelVersion()) >= 0) { List<Edge> edges = this.getEdgesBetween(EdgeType.COUSIN, v, cousin); for (Edge e : edges) { Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship"); @@ -944,7 +1020,6 @@ public class DBSerializer { * * @param relationshipObj the relationship obj * @param edge the edge - * @param direction the direction * @param cleanUp the clean up * @return the object * @throws InstantiationException the instantiation exception @@ -1108,38 +1183,7 @@ public class DBSerializer { return Optional.of(new Pair<>(results.get(results.size()-1), objs)); } - /** - * Takes a list of vertices and a list of objs and assumes they are in - * the order you want the URIs to be nested. - * [A,B,C] creates uris [A, AB, ABC] - * @param vertices - * @param objs - * @throws UnsupportedEncodingException - * @throws URISyntaxException - */ - public void setCachedURIs(List<Vertex> vertices, List<Introspector> objs) throws UnsupportedEncodingException, URISyntaxException { - - StringBuilder uriChain = new StringBuilder(); - for (int i = 0; i < vertices.size(); i++) { - String aaiUri = ""; - Vertex v = null; - v = vertices.get(i); - aaiUri = v.<String>property(AAIProperties.AAI_URI).orElse(null); - if (aaiUri != null) { - uriChain.append(aaiUri); - } else { - URI uri = UriBuilder.fromPath(objs.get(i).getURI()).build(); - aaiUri = uri.toString(); - uriChain.append(aaiUri); - v.property(AAIProperties.AAI_URI, uriChain.toString()); - } - } - - - - } - - + /** * Adds the r * @throws AAIUnknownObjectException @@ -1209,7 +1253,7 @@ public class DBSerializer { try { e = this.getEdgeBetween(EdgeType.COUSIN, inputVertex, relatedVertex, label); if (e == null) { - edgeRules.addEdge(this.engine.asAdmin().getTraversalSource(), inputVertex, relatedVertex, label); + edgeSer.addEdge(this.engine.asAdmin().getTraversalSource(), inputVertex, relatedVertex, label); } else { //attempted to link two vertexes already linked } @@ -1239,9 +1283,19 @@ public class DBSerializer { GraphTraversal<Vertex, Edge> findEdgesBetween = null; findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE(); if (EdgeType.TREE.equals(type)) { - findEdgesBetween = findEdgesBetween.not(__.has(EdgeProperty.CONTAINS.toString(), "NONE")); + findEdgesBetween = findEdgesBetween + .not( + __.or( + __.has(EdgeProperty.CONTAINS.toString(), "NONE"), + __.has(EdgeField.PRIVATE.toString(), true) + ) + ); } else { - findEdgesBetween = findEdgesBetween.has(EdgeProperty.CONTAINS.toString(), "NONE"); + findEdgesBetween = findEdgesBetween + .has(EdgeProperty.CONTAINS.toString(), "NONE") + .not( + __.has(EdgeField.PRIVATE.toString(), true) + ); } findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id())); result = findEdgesBetween.toList(); @@ -1263,7 +1317,17 @@ public class DBSerializer { List<Edge> result = new ArrayList<>(); if (bVertex != null) { - EdgeRule rule = edgeRules.getEdgeRule(type, aVertex, bVertex, label); + String aType = aVertex.<String>property(AAIProperties.NODE_TYPE).value(); + String bType = bVertex.<String>property(AAIProperties.NODE_TYPE).value(); + EdgeRuleQuery q = new EdgeRuleQuery.Builder(aType, bType).edgeType(type).label(label).build(); + EdgeRule rule; + try { + rule = edgeRules.getRule(q); + } catch (EdgeRuleNotFoundException e) { + throw new NoEdgeRuleFoundException(e); + } catch (AmbiguousRuleChoiceException e) { + throw new MultipleEdgeRuleFoundException(e); + } List<Edge> edges = this.getEdgesBetween(type, aVertex, bVertex); for (Edge edge : edges) { if (edge.label().equals(rule.getLabel())) { @@ -1393,10 +1457,45 @@ public class DBSerializer { * @throws AAIException the AAI exception * @throws InterruptedException the interrupted exception */ + public void delete(Vertex v, List<Vertex> deletableVertices, String resourceVersion, boolean enableResourceVersion) throws IllegalArgumentException, AAIException { + + boolean result = verifyDeleteSemantics(v, resourceVersion, enableResourceVersion); + /* + * The reason why I want to call PreventDeleteSemantics second time is to catch the prevent-deletes in a chain + * These are far-fewer than seeing a prevnt-delete on the vertex to be deleted + * So its better to make these in 2 steps + */ + if(result && !deletableVertices.isEmpty()){ + result = verifyPreventDeleteSemantics(deletableVertices); + } + if (result) { + + try { + deleteWithTraversal(v); + } catch (IllegalStateException e) { + throw new AAIException("AAI_6110", e); + } + + } + + } + + + /** + * Delete. + * + * @param v the v + * @param resourceVersion the resource version + * @throws IllegalArgumentException the illegal argument exception + * @throws AAIException the AAI exception + * @throws InterruptedException the interrupted exception + */ public void delete(Vertex v, String resourceVersion, boolean enableResourceVersion) throws IllegalArgumentException, AAIException { boolean result = verifyDeleteSemantics(v, resourceVersion, enableResourceVersion); + if (result) { + try { deleteWithTraversal(v); } catch (IllegalStateException e) { @@ -1406,8 +1505,6 @@ public class DBSerializer { } } - - /** * Verify delete semantics. * @@ -1424,8 +1521,37 @@ public class DBSerializer { nodeType = vertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); if (enableResourceVersion && !this.verifyResourceVersion("delete", nodeType, vertex.<String>property(AAIProperties.RESOURCE_VERSION).orElse(null), resourceVersion, nodeType)) { } + List<Vertex> vertices = new ArrayList<Vertex>(); + vertices.add(vertex); + result = verifyPreventDeleteSemantics(vertices); + + return result; + } + + /** + * Verify Prevent delete semantics. + * @param vertices the list of vertices + * @return true, if successful + * @throws AAIException the AAI exception + */ + private boolean verifyPreventDeleteSemantics(List<Vertex> vertices) throws AAIException { + boolean result = true; + String nodeType = ""; + String errorDetail = " unknown delete semantic found"; + String aaiExceptionCode = ""; + StopWatch.conditionalStart(); - List<Object> preventDeleteVertices = this.engine.asAdmin().getReadOnlyTraversalSource().V(vertex).union(__.inE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.IN.toString()).outV().values(AAIProperties.NODE_TYPE), __.outE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.OUT.toString()).inV().values(AAIProperties.NODE_TYPE)).dedup().toList(); + /* + * This takes in all the vertices in a cascade-delete-chain and checks if there is any edge with a "prevent-delete" condition + * If yes - that should prevent the deletion of the vertex + * Dedup makes sure we dont capture the prevent-delete vertices twice + * The prevent-delete vertices are stored so that the error message displays what prevents the delete + */ + + List<Object> preventDeleteVertices = this.engine.asAdmin().getReadOnlyTraversalSource().V(vertices). + union(__.inE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.IN.toString()).outV().values(AAIProperties.NODE_TYPE), + __.outE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.OUT.toString()).inV().values(AAIProperties.NODE_TYPE)) + .dedup().toList(); dbTimeMsecs += StopWatch.stopIfStarted(); if (!preventDeleteVertices.isEmpty()) { @@ -1466,7 +1592,6 @@ public class DBSerializer { } catch (AAIException e) { ErrorLogHelper.logException(e); } - // We're only doing the resource version checks for v5 and later if (enabled.equals("true")) { if (!currentResourceVersion.equals(resourceVersion)) { if (action.equals("create") && !resourceVersion.equals("")) { @@ -1519,7 +1644,7 @@ public class DBSerializer { private void executePreSideEffects(Introspector obj, Vertex self) throws AAIException { SideEffectRunner runner = new SideEffectRunner - .Builder(this.engine, this).addSideEffect(DataCopy.class).build(); + .Builder(this.engine, this).addSideEffect(DataCopy.class).addSideEffect(PrivateEdge.class).build(); runner.execute(obj, self); } @@ -1549,10 +1674,11 @@ public class DBSerializer { * This is for a one-time run with Tenant Isloation to only filter relationships * TODO: Chnage the original dbToObject to take filter parent/cousins * - * @param vertices the vertices * @param obj the obj + * @param v the vertex from the graph * @param depth the depth - * @param cleanUp the clean up + * @param nodeOnly specify if to exclude relationships or not + * @param filterCousinNodes * @return the introspector * @throws AAIException the AAI exception * @throws IllegalAccessException the illegal access exception @@ -1617,9 +1743,15 @@ public class DBSerializer { boolean isthisParentRequired = filterParentNodes.parallelStream().anyMatch(childDbName::contains); - - - rule = edgeRules.getEdgeRule(EdgeType.TREE, vType, childDbName); + EdgeRuleQuery q = new EdgeRuleQuery.Builder(vType, childDbName).edgeType(EdgeType.TREE).build(); + + try { + rule = edgeRules.getRule(q); + } catch (EdgeRuleNotFoundException e) { + throw new NoEdgeRuleFoundException(e); + } catch (AmbiguousRuleChoiceException e) { + throw new MultipleEdgeRuleFoundException(e); + } if (!rule.getContains().equals(AAIDirection.NONE.toString()) && isthisParentRequired) { //vertices = this.queryEngine.findRelatedVertices(v, Direction.OUT, rule.getLabel(), childDbName); Direction ruleDirection = rule.getDirection(); diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeProperties.java b/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeProperties.java index 2c9d6e9a..c8340a7c 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeProperties.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeProperties.java @@ -19,6 +19,8 @@ */ package org.onap.aai.serialization.db; +import org.onap.aai.edges.enums.EdgeProperty; + public class EdgeProperties { public static String out(EdgeProperty prop) { diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeRule.java b/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeRule.java deleted file mode 100644 index 2a0a0460..00000000 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeRule.java +++ /dev/null @@ -1,240 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.serialization.db; - -import org.apache.tinkerpop.gremlin.structure.Direction; - -import java.util.EnumMap; -import java.util.Map; - -public class EdgeRule { - - private String label = ""; - private MultiplicityRule multiplicityRule = null; - private Direction direction = null; - private Map<EdgeProperty, String> edgeProperties = null; - private boolean isDefaultEdge = false; - private String from; - private String to; - - /** - * Instantiates a new edge rule. - */ - public EdgeRule() { - edgeProperties = new EnumMap<>(EdgeProperty.class); - } - - public String getFrom() { - return from; - } - - public void setFrom(String from) { - this.from = from; - } - - public String getTo() { - return to; - } - - public void setTo(String to) { - this.to = to; - } - - /** - * Gets the label. - * - * @return the label - */ - public String getLabel() { - return label; - } - - /** - * Sets the label. - * - * @param label the new label - */ - public void setLabel(String label) { - this.label = label; - } - - /** - * Gets the multiplicity rule. - * - * @return the multiplicity rule - */ - public MultiplicityRule getMultiplicityRule() { - return multiplicityRule; - } - - public void setMultiplicityRule(String multiplicity){ - if ("Many2Many".equalsIgnoreCase(multiplicity)) { - this.multiplicityRule = MultiplicityRule.MANY2MANY; - } else if ("One2Many".equalsIgnoreCase(multiplicity)) { - this.multiplicityRule = MultiplicityRule.ONE2MANY; - } else if ("One2One".equalsIgnoreCase(multiplicity)) { - this.multiplicityRule = MultiplicityRule.ONE2ONE; - } else { //should be "Many2One" - this.multiplicityRule = MultiplicityRule.MANY2ONE; - } - } - - /** - * Sets the multiplicity rule. - * - * @param multiplicityRule the new multiplicity rule - */ - public void setMultiplicityRule(MultiplicityRule multiplicityRule) { - this.multiplicityRule = multiplicityRule; - } - - /** - * Gets the direction. - * - * @return the direction - */ - public Direction getDirection() { - return direction; - } - - public void setDirection(String direction){ - if ("OUT".equalsIgnoreCase(direction)) { - this.direction = Direction.OUT; - } else if ("IN".equalsIgnoreCase(direction)) { - this.direction = Direction.IN; - } else { - this.direction = Direction.BOTH; - } - } - - /** - * Sets the direction. - * - * @param direction the new direction - */ - public void setDirection(Direction direction) { - this.direction = direction; - } - - /** - * Gets the checks if is parent. - * - * @return the checks if is parent - */ - public String getContains() { - return this.getProp(EdgeProperty.CONTAINS); - } - - /** - * Sets the checks if is parent. - * - * @param isParent the new checks if is parent - */ - public void setContains(String isParent) { - this.setProp(EdgeProperty.CONTAINS, isParent); - } - - /** - * Gets the checks for del target. - * - * @return the checks for del target - */ - public String getDeleteOtherV() { - return this.getProp(EdgeProperty.DELETE_OTHER_V); - } - - /** - * Sets the checks for del target. - * - * @param hasDelTarget the new checks for del target - */ - public void setDeleteOtherV(String hasDelTarget) { - this.setProp(EdgeProperty.DELETE_OTHER_V, hasDelTarget); - } - - /** - * Gets the service infrastructure. - * - * @return the service infrastructure - */ - public String getServiceInfrastructure() { - return this.getProp(EdgeProperty.SVC_INFRA); - } - - /** - * Sets the service infrastructure. - * - * @param serviceInfrastructure the new service infrastructure - */ - public void setServiceInfrastructure(String serviceInfrastructure) { - this.setProp(EdgeProperty.SVC_INFRA, serviceInfrastructure); - } - - public String getPreventDelete() { - return this.getProp(EdgeProperty.PREVENT_DELETE); - } - - public void setPreventDelete(String preventDelete) { - this.setProp(EdgeProperty.PREVENT_DELETE, preventDelete); - } - - /** - * Gets the edge properties. - * - * @return the edge properties - */ - public Map<EdgeProperty, String> getEdgeProperties() { - return this.edgeProperties; - } - - /** - * Sets the prop. - * - * @param key the key - * @param value the value - */ - private void setProp(EdgeProperty key, String value) { - this.edgeProperties.put(key, value); - } - - /** - * Gets the prop. - * - * @param key the key - * @return the prop - */ - private String getProp(EdgeProperty key) { - return this.edgeProperties.get(key); - } - - public boolean isDefault() { - return isDefaultEdge; - } - - public void setIsDefault(boolean isDefault) { - this.isDefaultEdge = isDefault; - } - - public void setIsDefault(String isDefault) { - this.isDefaultEdge = "true".equals(isDefault); - } - - -} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeRules.java b/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeRules.java deleted file mode 100644 index 8d94b84b..00000000 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeRules.java +++ /dev/null @@ -1,893 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.serialization.db; - -import static com.jayway.jsonpath.Criteria.where; -import static com.jayway.jsonpath.Filter.filter; - -import java.io.InputStream; -import java.util.*; -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentHashMap; - -import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; -import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; -import org.apache.tinkerpop.gremlin.structure.Direction; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.onap.aai.db.props.AAIProperties; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Version; -import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException; -import org.onap.aai.serialization.db.exceptions.MultipleEdgeRuleFoundException; -import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.Multimap; -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.Filter; -import com.jayway.jsonpath.JsonPath; - -public class EdgeRules { - - private static final String LABEL = "label"; - - private static final String NOT_DIRECTION_NOTATION = "!${direction}"; - - private static final String DIRECTION_NOTATION = "${direction}"; - - private EELFLogger logger = EELFManager.getInstance().getLogger(EdgeRules.class); - - private DocumentContext rulesDoc; - - /** - * Loads the most recent DbEdgeRules json file for later parsing. - * Only need most recent version for actual A&AI operations that call this class; - * the old ones are only used in tests. - */ - private EdgeRules() { - - String json = this.getEdgeRuleJson(Version.getLatest()); - rulesDoc = JsonPath.parse(json); - - } - - private EdgeRules(String rulesFilename) { - String json = this.getEdgeRuleJson(rulesFilename); - rulesDoc = JsonPath.parse(json); - } - - /** - * Loads the versioned DbEdgeRules json file for later parsing. - */ - private EdgeRules(Version version) { - String json = this.getEdgeRuleJson(version); - rulesDoc = JsonPath.parse(json); - } - - /** - * Gets the single instance of EdgeRules. - * - * @return single instance of EdgeRules - */ - public static EdgeRules getInstance() { - return Helper.INSTANCE; - - } - - /** - * Gets the versioned instance of EdgeRules. - * - * @return versioned instance of EdgeRules - */ - public static EdgeRules getInstance(Version v) { - return Helper.getVersionedEdgeRules(v); - - } - - /** - * Loads edge rules from the given file. - * - * @param rulesFilename - name of the file to load rules from - * @return the EdgeRules instance - */ - public static EdgeRules getInstance(String rulesFilename) { - return Helper.getEdgeRulesByFilename(rulesFilename); - } - - private String getEdgeRuleJson(String rulesFilename) { - InputStream is = getClass().getResourceAsStream(rulesFilename); - - Scanner scanner = new Scanner(is); - String json = scanner.useDelimiter("\\Z").next(); - scanner.close(); - - return json; - } - - private String getEdgeRuleJson(Version version) { - return this.getEdgeRuleJson("/dbedgerules/DbEdgeRules_" + version.toString() + ".json"); - } - - /** - * Adds the tree edge. - * - * @param aVertex the out vertex - * @param bVertex the in vertex - * @return the edge - * @throws AAIException the AAI exception - */ - public Edge addTreeEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException { - return this.addEdge(EdgeType.TREE, traversalSource, aVertex, bVertex, false, null); - } - - /** - * Adds the edge. - * - * @param aVertex the out vertex - * @param bVertex the in vertex - * @return the edge - * @throws AAIException the AAI exception - */ - public Edge addEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException { - return this.addEdge(traversalSource, aVertex, bVertex, null); - } - - public Edge addEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label) throws AAIException { - return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, false, label); - } - - /** - * Adds the tree edge. - * - * @param aVertex the out vertex - * @param bVertex the in vertex - * @return the edge - * @throws AAIException the AAI exception - */ - public Edge addTreeEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException { - return this.addEdge(EdgeType.TREE, traversalSource, aVertex, bVertex, true, null); - } - - /** - * Adds the edge. - * - * @param aVertex the out vertex - * @param bVertex the in vertex - * @return the edge - * @throws AAIException the AAI exception - */ - public Edge addEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException { - return this.addEdgeIfPossible(traversalSource, aVertex, bVertex, null); - } - - public Edge addEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label) throws AAIException { - return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, true, label); - } - - /** - * Adds the edge. - * - * @param type the type - * @param aVertex the out vertex - * @param bVertex the in vertex - * @return the edge - * @throws AAIException the AAI exception - */ - private Edge addEdge(EdgeType type, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, boolean isBestEffort, String label) throws AAIException { - - EdgeRule rule = this.getEdgeRule(type, aVertex, bVertex, label); - - Edge e = null; - - Optional<String> message = this.validateMultiplicity(rule, traversalSource, aVertex, bVertex); - - if (message.isPresent() && !isBestEffort) { - throw new EdgeMultiplicityException(message.get()); - } - if (!message.isPresent()) { - if (rule.getDirection().equals(Direction.OUT)) { - e = aVertex.addEdge(rule.getLabel(), bVertex); - } else if (rule.getDirection().equals(Direction.IN)) { - e = bVertex.addEdge(rule.getLabel(), aVertex); - } - - this.addProperties(e, rule); - } - return e; - } - - /** - * Adds the properties. - * - * @param edge the edge - * @param rule the rule - */ - public void addProperties(Edge edge, EdgeRule rule) { - - // In DbEdgeRules.EdgeRules -- What we have as "edgeRule" is a comma-delimited set of strings. - // The first item is the edgeLabel. - // The second in the list is always "direction" which is always OUT for the way we've implemented it. - // Items starting at "firstTagIndex" and up are all assumed to be booleans that map according to - // tags as defined in EdgeInfoMap. - // Note - if they are tagged as 'reverse', that means they get the tag name with "-REV" on it - Map<EdgeProperty, String> propMap = rule.getEdgeProperties(); - - for (Entry<EdgeProperty, String> entry : propMap.entrySet()) { - edge.property(entry.getKey().toString(), entry.getValue()); - } - - edge.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString()); - } - - /** - * Checks if any edge rules exist between the two given node types, in either A|B or B|A order. - * - * @param nodeA - node at one end of the edge - * @param nodeB - node at the other end - * @return true, if any such rules exist - */ - public boolean hasEdgeRule(String nodeA, String nodeB) { - return this.hasEdgeRule(nodeA, nodeB, null); - } - - /** - * Checks if any edge rules exist between the two given node types with contains-other-v !NONE, in either A|B or B|A order. - * - * @param nodeA - node at one end of the edge - * @param nodeB - node at the other end - * @return true, if any such rules exist - */ - public boolean hasTreeEdgeRule(String nodeA, String nodeB) { - return this.hasEdgeRule(EdgeType.TREE, nodeA, nodeB, null); - } - - /** - * Checks if any edge rules exist between the two given node types with contains-other-v NONE, in either A|B or B|A order. - * - * @param nodeA - node at one end of the edge - * @param nodeB - node at the other end - * @param label - edge label - * @return true, if any such rules exist - */ - public boolean hasCousinEdgeRule(String nodeA, String nodeB, String label) { - return this.hasEdgeRule(EdgeType.COUSIN, nodeA, nodeB, label); - } - - /** - * Checks if any edge rules exist between the two given nodes with contains-other-v !NONE, in either A|B or B|A order. - * - * @param aVertex - node at one end of the edge - * @param bVertex - node at the other end - * @return true, if any such rules exist - */ - public boolean hasTreeEdgeRule(Vertex aVertex, Vertex bVertex) { - String outType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); - String inType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); - return this.hasTreeEdgeRule(outType, inType); - } - - /** - * Checks if any edge rules exist between the two given nodes with contains-other-v NONE with edge label, in either A|B or B|A order. - * - * @param aVertex - node at one end of the edge - * @param bVertex - node at the other end - * @param label - edge label - * @return true, if any such rules exist - */ - public boolean hasCousinEdgeRule(Vertex aVertex, Vertex bVertex, String label) { - String outType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); - String inType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); - return this.hasCousinEdgeRule(outType, inType, label); - } - - /** - * Checks if any edge rules exist between the two given nodes w/ edge label, in either A|B or B|A order. - * - * @param nodeA - node at one end of the edge - * @param nodeB - node at the other end - * @param label - edge label - * @return true, if any such rules exist - */ - public boolean hasEdgeRule(String nodeA, String nodeB, String label) { - return this.hasEdgeRule(null, nodeA, nodeB, label); - } - - /** - * Checks if any edge rules exist between the two given nodes, in either A|B or B|A order. - * - * @param aVertex - node at one end of the edge - * @param bVertex - node at the other end - * @return true, if any such rules exist - */ - public boolean hasEdgeRule(Vertex aVertex, Vertex bVertex) { - return this.hasEdgeRule(aVertex, bVertex, null); - - } - - /** - * Checks if any edge rules exist between the two given nodes with label, in either A|B or B|A order with edge label. - * - * @param aVertex - node at one end of the edge - * @param bVertex - node at the other end - * @param label - edge label - * @return true, if any such rules exist - */ - public boolean hasEdgeRule(Vertex aVertex, Vertex bVertex, String label) { - String outType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); - String inType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); - - if (label == null) { - return this.hasEdgeRule(outType, inType); - } else { - return this.hasEdgeRule(outType, inType, label); - } - } - - /** - * Checks if any edge rules exist between the two given node types, in either A|B or B|A order with edge label and edge type. - * - * @param type - type of edge EdgeType.COUSIN | EdgeType.TREE - * @param nodeA - node at one end of the edge - * @param nodeB - node at the other end - * @param label - edge label - * @return true, if any such rules exist - */ - public boolean hasEdgeRule(EdgeType type, String nodeA, String nodeB, String label) { - Filter aToB = filter( - where("from").is(nodeA) - .and("to").is(nodeB) - ); - Filter bToA = filter( - where("from").is(nodeB) - .and("to").is(nodeA) - ); - - if (EdgeType.TREE.equals(type)) { - aToB = aToB.and(where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString())); - bToA = bToA.and(where(EdgeProperty.CONTAINS.toString()).ne(AAIDirection.NONE.toString())); - } else if (EdgeType.COUSIN.equals(type)) { - aToB = aToB.and(where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString())); - bToA = bToA.and(where(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString())); - } - - if (label != null) { - aToB = aToB.and(where(LABEL).is(label)); - bToA = bToA.and(where(LABEL).is(label)); - } - - List<Object> results = rulesDoc.read("$.rules.[?]", aToB); - results.addAll(rulesDoc.read("$.rules.[?]", bToA)); - - return !results.isEmpty(); - } - - /** - * Gets all the edge rules that exist between the given node types. - * The rules will be phrased in terms of out|in, though this will - * also find rules defined as in|out (it will flip the direction in - * the EdgeRule object returned accordingly to match out|in). - * - * @param outType - * @param inType - * @return Map<String edgeLabel, EdgeRule rule> where edgeLabel is the label name - * @throws AAIException - */ - public Map<String, EdgeRule> getEdgeRules(String outType, String inType) { - return this.getEdgeRules(outType, inType, null); - } - - /** - * Gets all the edge rules that exist between the given node types with given label. - * The rules will be phrased in terms of out|in, though this will - * also find rules defined as in|out (it will flip the direction in - * the EdgeRule object returned accordingly to match out|in). - * - * @param outType - * @param inType - * @param label - * @return Map<String edgeLabel, EdgeRule rule> where edgeLabel is the label name - * @throws AAIException - */ - public Map<String, EdgeRule> getEdgeRules(String outType, String inType, String label) { - final Map<String, EdgeRule> result = new HashMap<>(); - - for (EdgeType type : EdgeType.values()) { - result.putAll(this.getEdgeRules(type, outType, inType, label)); - } - - return result; - } - - /** - * Looks up edge rules for the given node types and the labels specified - * @param type - * @param outType - * @param inType - * @param labels - * @return - * @throws NoEdgeRuleFoundException - * @throws MultipleEdgeRuleFoundException - */ - public Map<String, EdgeRule> getEdgeRulesWithLabels(EdgeType type, String outType, String inType, List<String> labels) throws NoEdgeRuleFoundException, MultipleEdgeRuleFoundException { - final Map<String, EdgeRule> result = new HashMap<>(); - - if (labels == null || labels.isEmpty()) { - throw new NoEdgeRuleFoundException("No labels specified"); - } - for (String label : labels) { - EdgeRule er = this.getEdgeRule(type, outType, inType, label); - result.put(er.getLabel(), er); - } - - return result; - } - - /** - * Gets all the edge rules of that edge type that exist between the given node types with given label. - * The rules will be phrased in terms of out|in, though this will - * also find rules defined as in|out (it will flip the direction in - * the EdgeRule object returned accordingly to match out|in). - * - * @param type - * @param outType - * @param inType - * @param label - * @return - * @throws AAIException - */ - public Map<String, EdgeRule> getEdgeRules(EdgeType type, String outType, String inType, String label) { - final Map<String, EdgeRule> result = new HashMap<>(); - - this.getEdgeRulesFromJson(type, outType, inType, label).forEach(edgeRuleJson -> { - EdgeRule edgeRule = this.buildRule(edgeRuleJson); - result.put(edgeRule.getLabel(), edgeRule); - }); - this.getEdgeRulesFromJson(type, inType, outType, label).forEach(erj -> { - EdgeRule edgeRule = this.flipDirection(this.buildRule(erj)); - if (!result.containsKey(edgeRule.getLabel())) { - result.put(edgeRule.getLabel(), edgeRule); - } - }); - - - return result; - } - - /** - * Gets all the edge rules of that edge type that exist between the given node types. - * The rules will be phrased in terms of out|in, though this will - * also find rules defined as in|out (it will flip the direction in - * the EdgeRule object returned accordingly to match out|in). - * - * @param type - * @param outType - * @param inType - * @return - * @throws AAIException - */ - public Map<String, EdgeRule> getEdgeRules(EdgeType type, String outType, String inType) { - return this.getEdgeRules(type, outType, inType, null); - } - - /** - * Gets the edge rule of the given type that exists between A and B. - * Will check B|A as well, and flips the direction accordingly if that succeeds - * to match the expected A|B return. - * - * @param type - the type of edge you're looking for - * @param nodeA - first node type - * @param nodeB - second node type - * @return EdgeRule describing the rule in terms of A|B, if there is any such rule - * @throws AAIException if no such edge exists - */ - public EdgeRule getEdgeRule(EdgeType type, String nodeA, String nodeB) throws AAIException { - return this.getEdgeRule(type, nodeA, nodeB, null); - } - - /** - * Gets the edge rule of the given type that exists between A and B with edge label. - * Will check B|A as well, and flips the direction accordingly if that succeeds - * to match the expected A|B return. - * - * @param type - the type of edge you're looking for - * @param nodeA - first node type - * @param nodeB - second node type - * @param label - edge label - * @return EdgeRule describing the rule in terms of A|B, if there is any such rule - * @throws MultipleEdgeRuleFoundException - * @throws AAIException if no such edge exists - */ - public EdgeRule getEdgeRule(EdgeType type, String nodeA, String nodeB, String label) throws NoEdgeRuleFoundException, MultipleEdgeRuleFoundException { - - final StringBuilder errorMsg = new StringBuilder(); - errorMsg.append(type.toString()) - .append(" edge rule between ") - .append(nodeA).append(" and ").append(nodeB); - if (label != null) { - errorMsg.append(" with label ").append(label); - } - - EdgeRule edgeRule; - Map<String, EdgeRule> edgeRules = this.getEdgeRules(type, nodeA, nodeB, label); - - //found none - if (edgeRules.isEmpty()) { - - throw new NoEdgeRuleFoundException("no " + errorMsg); - - } else if (edgeRules.size() == 1) { - - edgeRule = edgeRules.values().iterator().next(); - - } else { - - Optional<EdgeRule> optionalEdgeRule = Optional.empty(); - - try { - optionalEdgeRule = this.getDefaultEdgeRule(edgeRules); - } catch (MultipleEdgeRuleFoundException e) { - throw new MultipleEdgeRuleFoundException("multiple default edge rule exists " + errorMsg); - } - - edgeRule = optionalEdgeRule.orElseThrow(() -> new MultipleEdgeRuleFoundException("multiple edge rule exists with no default " + errorMsg)); - - } - - return edgeRule; - } - - private Optional<EdgeRule> getDefaultEdgeRule(Map<String, EdgeRule> edgeRules) throws MultipleEdgeRuleFoundException { - - EdgeRule edgeRule = null; - int numDefaults = 0; - - for (Map.Entry<String, EdgeRule> entry : edgeRules.entrySet()) { - if (entry.getValue().isDefault()) { - edgeRule = entry.getValue(); - numDefaults++; - } - } - - if (numDefaults > 1) { - throw new MultipleEdgeRuleFoundException(""); - } - - if (edgeRule == null) { - return Optional.empty(); - } else { - return Optional.of(edgeRule); - } - } - - /** - * Gets the rules from the edge rules Json - * - * @param type - type - * @param nodeA - start node - * @param nodeB - end node - * @param label - edge label to filter on - * @return - */ - private List<Map<String, String>> getEdgeRulesFromJson(EdgeType type, String nodeA, String nodeB, String label) { - if (label == null) { - return rulesDoc.read("$.rules.[?]", buildFilter(type, nodeA, nodeB)); - } else { - return rulesDoc.read("$.rules.[?]", buildFilter(type, nodeA, nodeB, label)); - } - } - - /** - * Builds a JsonPath filter to search for an edge from nodeA to nodeB with the given edge type (cousin or parent/child) - * - * @param type - * @param nodeA - start node - * @param nodeB - end node - * @return - */ - private Filter buildFilter(EdgeType type, String nodeA, String nodeB) { - return this.buildFilter(type, nodeA, nodeB, null); - } - - private Filter buildFilter(EdgeType type, String nodeA, String nodeB, String label) { - if (EdgeType.COUSIN.equals(type)) { - Filter f = filter( - where("from").is(nodeA) - .and("to").is(nodeB) - .and(EdgeProperty.CONTAINS.toString()).is(AAIDirection.NONE.toString()) - ); - if (label != null) { - f = f.and(where(LABEL).is(label)); - } - - return f; - } else { - return filter( - where("from").is(nodeA).and("to").is(nodeB).and(EdgeProperty.CONTAINS.toString()).is(DIRECTION_NOTATION)).or( - where("from").is(nodeA).and("to").is(nodeB).and(EdgeProperty.CONTAINS.toString()).is(NOT_DIRECTION_NOTATION) - ); - } - } - - /** - * Puts the give edge rule information into an EdgeRule object. - * - * @param map edge rule property map - * @return EdgeRule containing that information - */ - private EdgeRule buildRule(Map<String, String> map) { - Map<String, String> edge = new EdgePropertyMap<>(); - edge.putAll(map); - - EdgeRule rule = new EdgeRule(); - rule.setLabel(edge.get(LABEL)); - rule.setDirection(edge.get("direction")); - rule.setMultiplicityRule(edge.get("multiplicity")); - rule.setContains(edge.get(EdgeProperty.CONTAINS.toString())); - rule.setDeleteOtherV(edge.get(EdgeProperty.DELETE_OTHER_V.toString())); - rule.setServiceInfrastructure(edge.get(EdgeProperty.SVC_INFRA.toString())); - rule.setPreventDelete(edge.get(EdgeProperty.PREVENT_DELETE.toString())); - rule.setTo(edge.get("to")); - rule.setFrom(edge.get("from")); - if (edge.containsKey("default")) { - rule.setIsDefault(edge.get("default")); - } - - if(rule.getFrom().equals(rule.getTo())){ - return this.flipDirection(rule); - } else { - return rule; - } - } - - /** - * If getEdgeRule gets a request for A|B, and it finds something as B|A, the caller still expects - * the returned EdgeRule to reflect A|B directionality. This helper method flips B|A direction to - * match this expectation. - * - * @param rule whose direction needs flipped - * @return the updated rule - */ - private EdgeRule flipDirection(EdgeRule rule) { - if (Direction.IN.equals(rule.getDirection())) { - rule.setDirection(Direction.OUT); - return rule; - } else if (Direction.OUT.equals(rule.getDirection())) { - rule.setDirection(Direction.IN); - return rule; - } else { //direction is BOTH, flipping both is still both - return rule; - } - } - - /** - * Gets the edge rule of the given type that exists between A and B. - * Will check B|A as well, and flips the direction accordingly if that succeeds - * to match the expected A|B return. - * - * @param type - the type of edge you're looking for - * @param aVertex - first node type - * @param bVertex - second node type - * @return EdgeRule describing the rule in terms of A|B, if there is any such rule - * @throws AAIException if no such edge exists - */ - public EdgeRule getEdgeRule(EdgeType type, Vertex aVertex, Vertex bVertex) throws AAIException { - return this.getEdgeRule(type, aVertex, bVertex, null); - } - - /** - * Gets the edge rule of the given type that exists between A and B with label. - * Will check B|A as well, and flips the direction accordingly if that succeeds - * to match the expected A|B return. - * - * @param type - the type of edge you're looking for - * @param aVertex - first node type - * @param bVertex - second node type - * @param label - edge label - * @return EdgeRule describing the rule in terms of A|B, if there is any such rule - * @throws AAIException if no such edge exists - */ - public EdgeRule getEdgeRule(EdgeType type, Vertex aVertex, Vertex bVertex, String label) throws AAIException { - String outType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); - String inType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); - - return this.getEdgeRule(type, outType, inType, label); - - - } - - /** - * Validate multiplicity. - * - * @param rule the rule - * @param aVertex the out vertex - * @param bVertex the in vertex - * @return true, if successful - * @throws AAIException the AAI exception - */ - private Optional<String> validateMultiplicity(EdgeRule rule, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) { - - Vertex a = aVertex; - Vertex b = bVertex; - - if (rule.getDirection().equals(Direction.OUT)) { - a = aVertex; - b = bVertex; - } else if (rule.getDirection().equals(Direction.IN)) { - a = bVertex; - b = aVertex; - } - - String aVertexType = a.<String>property(AAIProperties.NODE_TYPE).orElse(null); - String bVertexType = b.<String>property(AAIProperties.NODE_TYPE).orElse(null); - String label = rule.getLabel(); - MultiplicityRule multiplicityRule = rule.getMultiplicityRule(); - List<Edge> outEdges = traversalSource.V(a).outE(label).where(__.inV().has(AAIProperties.NODE_TYPE, bVertexType)).toList(); - List<Edge> inEdges = traversalSource.V(b).inE(label).where(__.outV().has(AAIProperties.NODE_TYPE, aVertexType)).toList(); - String detail = ""; - final String msg = "multiplicity rule violated: only one edge can exist with label: "; - if (multiplicityRule.equals(MultiplicityRule.ONE2ONE)) { - if (!inEdges.isEmpty() || !outEdges.isEmpty() ) { - detail = msg + label + " between " + aVertexType + " and " + bVertexType; - } - } else if (multiplicityRule.equals(MultiplicityRule.ONE2MANY)) { - if (!inEdges.isEmpty()) { - detail = msg + label + " between " + aVertexType + " and " + bVertexType; - } - } else if (multiplicityRule.equals(MultiplicityRule.MANY2ONE)) { - if (!outEdges.isEmpty()) { - detail = msg + label + " between " + aVertexType + " and " + bVertexType; - } - } - - if (!"".equals(detail)) { - return Optional.of(detail); - } else { - return Optional.empty(); - } - - - } - - /** - * Verifies that all required properties are defined in the given edge rule. - * If they are not, throws a RuntimeException. - * - * @param rule - Map<String edge property, String edge property value> representing - * an edge rule - */ - private void verifyRule(Map<String, String> rule) { - for (EdgeProperty prop : EdgeProperty.values()) { - - // Description is not required as it is only set for v12 versions - if("description".equals(prop.toString())){ - continue; - } - if (!rule.containsKey(prop.toString())) { - /* Throws RuntimeException as rule definition errors - * cannot be recovered from, and should never happen anyway - * because these are configuration files, so requiring all - * downstream code to check for this exception seems inappropriate. - * It's instantiated with an AAIException to make sure all - * relevant information is present in the error message. - */ - throw new RuntimeException(new AAIException("AAI_4005", - "Rule between " + rule.get("from") + " and " + rule.get("to") + - " is missing property " + prop + ".")); - } - } - } - - /** - * Reads all the edge rules from the loaded json file. - * - * @return List<Map<String edge property, String edge property value>> - * Each map represents a rule read from the json. - */ - private List<Map<String, String>> readRules() { - return readRules(null); - } - - /** - * Reads the edge rules from the loaded json file, using the given filter - * to get specific rules. If filter is null, will get all rules. - * - * @param filter - may be null to indicate get all - * @return List<Map<String edge property, String edge property value>> - * Each map represents a rule read from the json. - */ - private List<Map<String, String>> readRules(Filter filter) { - List<Map<String, String>> results; - if (filter == null) { //no filter means get all - results = rulesDoc.read("$.rules.*"); - } else { - results = rulesDoc.read("$.rules.[?]", filter); - } - for (Map<String, String> result : results) { - verifyRule(result); - } - return results; - } - - /** - * Gets all the edge rules we define. - * - * @return Multimap<String "from|to", EdgeRule rule> - */ - public Multimap<String, EdgeRule> getAllRules() { - Multimap<String, EdgeRule> result = ArrayListMultimap.create(); - - List<Map<String, String>> rules = readRules(); - for (Map<String, String> rule : rules) { - EdgeRule er = buildRule(rule); - String name = rule.get("from") + "|" + rule.get("to"); - result.put(name, er); - } - - return result; - } - - /** - * Gets all edge rules that define a child relationship from - * the given node type. - * - * @param nodeType - * @return - */ - public Set<EdgeRule> getChildren(String nodeType) { - - final Filter filter = filter( - where("from").is(nodeType).and(EdgeProperty.CONTAINS.toString()).is(DIRECTION_NOTATION) - ).or(where("to").is(nodeType).and(EdgeProperty.CONTAINS.toString()).is(NOT_DIRECTION_NOTATION)); - - final List<Map<String, String>> rules = readRules(filter); - final Set<EdgeRule> result = new HashSet<>(); - rules.forEach(item -> { - verifyRule(item); - result.add(buildRule(item)); - }); - - return result; - - } - - private static class Helper { - private static final EdgeRules INSTANCE = new EdgeRules(); - private static final Map<Version, EdgeRules> INSTANCEMAP = new ConcurrentHashMap<>(); - - private Helper() {} - - private static EdgeRules getEdgeRulesByFilename(String rulesFilename) { - return new EdgeRules(rulesFilename); - } - - private static EdgeRules getVersionedEdgeRules(Version v) { - if (Version.isLatest(v)) { - return INSTANCE; - } - if (!INSTANCEMAP.containsKey(v)) { - INSTANCEMAP.put(v, new EdgeRules(v)); - } - return INSTANCEMAP.get(v); - } - } -} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeSerializer.java b/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeSerializer.java new file mode 100644 index 00000000..d4a2b51b --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeSerializer.java @@ -0,0 +1,272 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.serialization.db; + +import java.util.EnumMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.Map.Entry; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.enums.EdgeField; +import org.onap.aai.edges.enums.EdgeProperty; +import org.onap.aai.edges.enums.EdgeType; +import org.onap.aai.edges.enums.MultiplicityRule; +import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException; +import org.onap.aai.serialization.db.exceptions.MultipleEdgeRuleFoundException; +import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class EdgeSerializer { + + @Autowired + private EdgeIngestor edgerules; + + public EdgeSerializer(EdgeIngestor ei) { + this.edgerules = ei; + } + + /** + * Adds the tree edge. + * + * @param aVertex the out vertex + * @param bVertex the in vertex + * @return the edge + * @throws AAIException the AAI exception + */ + public Edge addTreeEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException { + return this.addEdge(EdgeType.TREE, traversalSource, aVertex, bVertex, false, null); + } + + /** + * Adds the edge. + * + * @param aVertex the out vertex + * @param bVertex the in vertex + * @return the edge + * @throws AAIException the AAI exception + */ + public Edge addEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException { + return this.addEdge(traversalSource, aVertex, bVertex, null); + } + + public Edge addEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label) throws AAIException { + return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, false, label); + } + + public Edge addPrivateEdge(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label) throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException { + return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, false, label, true); + } + + private Edge addEdge(EdgeType type, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, boolean isBestEffort, String label, boolean isPrivateEdge) throws AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException { + + EdgeRule rule = null; + + String aType = aVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); + String bType = bVertex.<String>property(AAIProperties.NODE_TYPE).orElse(null); + EdgeRuleQuery edgeQuery = new EdgeRuleQuery.Builder(aType, bType).label(label).setPrivate(isPrivateEdge).build(); + + rule = edgerules.getRule(edgeQuery); + + if(rule.isPrivateEdge() != isPrivateEdge){ + return null; + } + + Edge e = null; + + Optional<String> message = this.validateMultiplicity(rule, traversalSource, aVertex, bVertex); + + if (message.isPresent() && !isBestEffort) { + throw new EdgeMultiplicityException(message.get()); + } + if (!message.isPresent()) { + if (rule.getDirection().equals(Direction.OUT)) { + e = aVertex.addEdge(rule.getLabel(), bVertex); + } else if (rule.getDirection().equals(Direction.IN)) { + e = bVertex.addEdge(rule.getLabel(), aVertex); + } + + this.addProperties(e, rule); + } + return e; + } + + /** + * Adds the tree edge. + * + * @param aVertex the out vertex + * @param bVertex the in vertex + * @return the edge + * @throws AAIException the AAI exception + */ + public Edge addTreeEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException { + return this.addEdge(EdgeType.TREE, traversalSource, aVertex, bVertex, true, null); + } + + /** + * Adds the edge. + * + * @param aVertex the out vertex + * @param bVertex the in vertex + * @return the edge + * @throws AAIException the AAI exception + */ + public Edge addEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) throws AAIException { + return this.addEdgeIfPossible(traversalSource, aVertex, bVertex, null); + } + + public Edge addEdgeIfPossible(GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, String label) throws AAIException { + return this.addEdge(EdgeType.COUSIN, traversalSource, aVertex, bVertex, true, label); + } + + /** + * Adds the edge. + * + * @param type the type + * @param aVertex the out vertex + * @param bVertex the in vertex + * @return the edge + * @throws AAIException the AAI exception + */ + private Edge addEdge(EdgeType type, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex, boolean isBestEffort, String label) throws AAIException { + String aNodeType = (String)aVertex.property(AAIProperties.NODE_TYPE).value(); + String bNodeType = (String)bVertex.property(AAIProperties.NODE_TYPE).value(); + EdgeRuleQuery q = new EdgeRuleQuery.Builder(aNodeType, bNodeType).label(label).edgeType(type).build(); + EdgeRule rule; + try { + rule = edgerules.getRule(q); + } catch (EdgeRuleNotFoundException e1) { + throw new NoEdgeRuleFoundException(e1); + } catch (AmbiguousRuleChoiceException e1) { + throw new MultipleEdgeRuleFoundException(e1); + } + + Edge e = null; + + Optional<String> message = this.validateMultiplicity(rule, traversalSource, aVertex, bVertex); + + if (message.isPresent() && !isBestEffort) { + throw new EdgeMultiplicityException(message.get()); + } + if (!message.isPresent()) { + if (rule.getDirection().equals(Direction.OUT)) { + e = aVertex.addEdge(rule.getLabel(), bVertex); + } else if (rule.getDirection().equals(Direction.IN)) { + e = bVertex.addEdge(rule.getLabel(), aVertex); + } + + this.addProperties(e, rule); + } + return e; + } + + /** + * Adds the properties. + * + * @param edge the edge + * @param rule the rule + */ + public void addProperties(Edge edge, EdgeRule rule) { + Map<EdgeProperty, String> propMap = new EnumMap<>(EdgeProperty.class); + propMap.put(EdgeProperty.CONTAINS, rule.getContains()); + propMap.put(EdgeProperty.DELETE_OTHER_V, rule.getDeleteOtherV()); + propMap.put(EdgeProperty.PREVENT_DELETE, rule.getPreventDelete()); + + for (Entry<EdgeProperty, String> entry : propMap.entrySet()) { + edge.property(entry.getKey().toString(), entry.getValue()); + } + + edge.property(EdgeField.PRIVATE.toString(), rule.isPrivateEdge()); + edge.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString()); + } + + /** + * Validate multiplicity. + * + * @param rule the rule + * @param aVertex the out vertex + * @param bVertex the in vertex + * @return true, if successful + * @throws AAIException the AAI exception + */ + private Optional<String> validateMultiplicity(EdgeRule rule, GraphTraversalSource traversalSource, Vertex aVertex, Vertex bVertex) { + + Vertex a = aVertex; + Vertex b = bVertex; + + if (rule.getDirection().equals(Direction.OUT)) { + a = aVertex; + b = bVertex; + } else if (rule.getDirection().equals(Direction.IN)) { + a = bVertex; + b = aVertex; + } + + String aVertexType = a.<String>property(AAIProperties.NODE_TYPE).orElse(null); + String bVertexType = b.<String>property(AAIProperties.NODE_TYPE).orElse(null); + String label = rule.getLabel(); + + MultiplicityRule multiplicityRule = rule.getMultiplicityRule(); + + String detail = ""; + final String msg = "multiplicity rule violated: only one edge can exist with label: "; + + if (multiplicityRule.equals(MultiplicityRule.ONE2ONE)) { + Long outEdgesCnt = traversalSource.V(a).out(label).has(AAIProperties.NODE_TYPE, bVertexType).count().next(); + Long inEdgesCnt = traversalSource.V(b).in(label).has(AAIProperties.NODE_TYPE, aVertexType).count().next(); + if (aVertexType.equals(bVertexType)) { + inEdgesCnt = inEdgesCnt + traversalSource.V(a).in(label).has(AAIProperties.NODE_TYPE, aVertexType).count().next(); + outEdgesCnt = outEdgesCnt + traversalSource.V(b).out(label).has(AAIProperties.NODE_TYPE, bVertexType).count().next(); + } + if ( (inEdgesCnt != 0) || (outEdgesCnt != 0) ) { + detail = msg + label + " between " + aVertexType + " and " + bVertexType; + } + } else if (multiplicityRule.equals(MultiplicityRule.ONE2MANY)) { + Long inEdgesCnt = traversalSource.V(b).in(label).has(AAIProperties.NODE_TYPE, aVertexType).count().next(); + if (inEdgesCnt != 0) { + detail = msg + label + " between " + aVertexType + " and " + bVertexType; + } + } else if (multiplicityRule.equals(MultiplicityRule.MANY2ONE)) { + Long outEdgesCnt = traversalSource.V(a).out(label).has(AAIProperties.NODE_TYPE, bVertexType).count().next(); + if (outEdgesCnt != 0) { + detail = msg + label + " between " + aVertexType + " and " + bVertexType; + } + } + + if (!"".equals(detail)) { + return Optional.of(detail); + } else { + return Optional.empty(); + } + } +} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeType.java b/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeType.java deleted file mode 100644 index 90705781..00000000 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/EdgeType.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.serialization.db; - -public enum EdgeType { - COUSIN, - TREE; -} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/db/MultiplicityRule.java b/aai-core/src/main/java/org/onap/aai/serialization/db/MultiplicityRule.java deleted file mode 100644 index ce8d90b3..00000000 --- a/aai-core/src/main/java/org/onap/aai/serialization/db/MultiplicityRule.java +++ /dev/null @@ -1,27 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.serialization.db; - -public enum MultiplicityRule { - MANY2ONE, - ONE2MANY, - ONE2ONE, - MANY2MANY -} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/engines/InMemoryDBEngine.java b/aai-core/src/main/java/org/onap/aai/serialization/engines/InMemoryDBEngine.java index ec52a002..afc91bd3 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/engines/InMemoryDBEngine.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/engines/InMemoryDBEngine.java @@ -19,28 +19,22 @@ */ package org.onap.aai.serialization.engines; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - +import org.janusgraph.core.JanusGraph; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy; import org.apache.tinkerpop.gremlin.structure.Graph; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.VertexProperty; - import org.onap.aai.dbmap.DBConnectionType; import org.onap.aai.introspection.Loader; -import org.onap.aai.query.builder.GremlinTraversal; -import org.onap.aai.query.builder.GremlinUnique; -import org.onap.aai.query.builder.QueryBuilder; -import org.onap.aai.query.builder.TraversalQuery; +import org.onap.aai.query.builder.*; import org.onap.aai.serialization.db.InMemoryGraphSingleton; import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine; import org.onap.aai.serialization.engines.query.QueryEngine; -import org.onap.aai.serialization.engines.TransactionalGraphEngine; -import org.janusgraph.core.JanusGraph; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; public class InMemoryDBEngine extends TransactionalGraphEngine { @@ -82,7 +76,7 @@ public class InMemoryDBEngine extends TransactionalGraphEngine { @Override public QueryEngine getQueryEngine() { - if (style.equals(QueryStyle.TRAVERSAL)) { + if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) { GraphTraversalSource traversalSource = graph.traversal(); return new GraphTraversalQueryEngine(traversalSource); @@ -99,7 +93,8 @@ public class InMemoryDBEngine extends TransactionalGraphEngine { return new GremlinTraversal<>(loader, graph.traversal()); } else if (style.equals(QueryStyle.TRAVERSAL)) { return new TraversalQuery<>(loader, graph.traversal()); - + } else if (style.equals(QueryStyle.TRAVERSAL_URI)) { + return new TraversalURIOptimizedQuery<>(loader, graph.traversal()); } else { throw new IllegalArgumentException("Query Builder type is Not recognized"); } @@ -161,6 +156,8 @@ public class InMemoryDBEngine extends TransactionalGraphEngine { return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource()); } else if (style.equals(QueryStyle.TRAVERSAL)) { return new TraversalQuery<>(loader, graph.traversal()); + } else if (style.equals(QueryStyle.TRAVERSAL_URI)) { + return new TraversalURIOptimizedQuery<>(loader, graph.traversal()); } else { throw new IllegalArgumentException("Query Builder type not recognized"); } @@ -184,6 +181,8 @@ public class InMemoryDBEngine extends TransactionalGraphEngine { return new GremlinUnique<>(loader, this.asAdmin().getTraversalSource(), start); } else if (style.equals(QueryStyle.TRAVERSAL)) { return new TraversalQuery<>(loader, graph.traversal(), start); + } else if (style.equals(QueryStyle.TRAVERSAL_URI)) { + return new TraversalURIOptimizedQuery<>(loader, graph.traversal(), start); } else { throw new IllegalArgumentException("Query Builder type not recognized"); } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/engines/QueryStyle.java b/aai-core/src/main/java/org/onap/aai/serialization/engines/QueryStyle.java index 74c74d2a..2a069588 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/engines/QueryStyle.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/engines/QueryStyle.java @@ -20,5 +20,5 @@ package org.onap.aai.serialization.engines; public enum QueryStyle { - GREMLIN_TRAVERSAL, GREMLIN_UNIQUE, GREMLINPIPELINE_TRAVERSAL, TRAVERSAL + GREMLIN_TRAVERSAL, GREMLIN_UNIQUE, GREMLINPIPELINE_TRAVERSAL, TRAVERSAL, TRAVERSAL_URI } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/engines/TransactionalGraphEngine.java b/aai-core/src/main/java/org/onap/aai/serialization/engines/TransactionalGraphEngine.java index 8eedf00d..eaf6829c 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/engines/TransactionalGraphEngine.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/engines/TransactionalGraphEngine.java @@ -19,23 +19,19 @@ */ package org.onap.aai.serialization.engines; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.schema.JanusGraphManagement; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy; import org.apache.tinkerpop.gremlin.structure.Graph; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.onap.aai.dbmap.DBConnectionType; import org.onap.aai.introspection.Loader; -import org.onap.aai.query.builder.GremlinTraversal; -import org.onap.aai.query.builder.GremlinUnique; -import org.onap.aai.query.builder.QueryBuilder; -import org.onap.aai.query.builder.TraversalQuery; +import org.onap.aai.query.builder.*; import org.onap.aai.serialization.db.GraphSingleton; import org.onap.aai.serialization.engines.query.GraphTraversalQueryEngine; import org.onap.aai.serialization.engines.query.QueryEngine; -import org.janusgraph.core.JanusGraph; -import org.janusgraph.core.schema.JanusGraphManagement; - import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -63,7 +59,6 @@ public abstract class TransactionalGraphEngine { this.singleton = singleton; this.connectionType = connectionType; admin = new Admin(); - } public TransactionalGraphEngine (QueryStyle style, Loader loader) { @@ -71,6 +66,7 @@ public abstract class TransactionalGraphEngine { this.style = style; this.connectionType = DBConnectionType.REALTIME; admin = new Admin(); + } /** @@ -124,7 +120,7 @@ public abstract class TransactionalGraphEngine { //this.queryEngine = new GremlinQueryEngine(this); } else if (style.equals(QueryStyle.GREMLINPIPELINE_TRAVERSAL)) { //this.queryEngine = new GremlinPipelineQueryEngine(this); - } else if (style.equals(QueryStyle.TRAVERSAL)) { + } else if (style.equals(QueryStyle.TRAVERSAL) || style.equals(QueryStyle.TRAVERSAL_URI)) { return new GraphTraversalQueryEngine(this.asAdmin().getTraversalSource()); @@ -161,6 +157,8 @@ public abstract class TransactionalGraphEngine { //return new GremlinPipelineTraversal(loader); } else if (style.equals(QueryStyle.TRAVERSAL)) { return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource()); + } else if (style.equals(QueryStyle.TRAVERSAL_URI)) { + return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource()); } else { throw new IllegalArgumentException("Query Builder type not recognized"); } @@ -189,6 +187,8 @@ public abstract class TransactionalGraphEngine { //return new GremlinPipelineTraversal(loader,start); } else if (style.equals(QueryStyle.TRAVERSAL)) { return new TraversalQuery<>(loader, this.asAdmin().getTraversalSource(), start); + } else if (style.equals(QueryStyle.TRAVERSAL_URI)) { + return new TraversalURIOptimizedQuery<>(loader, this.asAdmin().getTraversalSource(), start); } else { throw new IllegalArgumentException("Query Builder type not recognized"); } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/engines/query/GraphTraversalQueryEngine.java b/aai-core/src/main/java/org/onap/aai/serialization/engines/query/GraphTraversalQueryEngine.java index d2c68fcc..2158c894 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/engines/query/GraphTraversalQueryEngine.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/engines/query/GraphTraversalQueryEngine.java @@ -20,11 +20,12 @@ package org.onap.aai.serialization.engines.query; -import static org.onap.aai.serialization.db.AAIDirection.IN; -import static org.onap.aai.serialization.db.AAIDirection.NONE; -import static org.onap.aai.serialization.db.AAIDirection.OUT; -import static org.onap.aai.serialization.db.EdgeProperty.CONTAINS; -import static org.onap.aai.serialization.db.EdgeProperty.DELETE_OTHER_V; +import static org.onap.aai.edges.enums.AAIDirection.IN; +import static org.onap.aai.edges.enums.AAIDirection.NONE; +import static org.onap.aai.edges.enums.AAIDirection.OUT; +import static org.onap.aai.edges.enums.EdgeField.PRIVATE; +import static org.onap.aai.edges.enums.EdgeProperty.CONTAINS; +import static org.onap.aai.edges.enums.EdgeProperty.DELETE_OTHER_V; import java.util.List; import java.util.Set; @@ -169,7 +170,7 @@ public class GraphTraversalQueryEngine extends QueryEngine { __.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()) ); - + if (!nodeOnly) { t.union( __.identity(), @@ -186,11 +187,23 @@ public class GraphTraversalQueryEngine extends QueryEngine { @Override public List<Edge> findEdgesForVersion(Vertex start, Loader loader) { + // From the given start vertex find both the + // out edges that has property CONTAINS set to NONE + // whose in vertexes has an object that is declared in the oxm + // And do the same thing vice versa to get a list of edges + // Then check that the edge should not have the property private set to true + // and remove the duplicates and return the list of edges final Set<String> objects = loader.getAllObjects().keySet(); - GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union( + GraphTraversal<Vertex, Edge> pipeline = this.g + .V(start) + .union( __.inE().has(CONTAINS.toString(), NONE.toString()).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))), __.outE().has(CONTAINS.toString(), NONE.toString()).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects))) - ).dedup(); + ) + .not( + __.has("private", true) + ) + .dedup(); return pipeline.toList(); } @@ -198,10 +211,27 @@ public class GraphTraversalQueryEngine extends QueryEngine { @Override public List<Vertex> findCousinVertices(Vertex start) { - GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union( - __.inE().has(CONTAINS.toString(), NONE.toString()), - __.outE().has(CONTAINS.toString(), NONE.toString())).otherV().dedup(); - + // Start at the given vertex + // Do a union to copy the start vertex to be run against all + // so for the start vertex it gets all of in edges that contains other v set to none + // and also all the other out edges with contains other v set to none + // And filter the edges based on the property private not set + // so that means it will be a regular edge + // and find the other end of the vertex so if setup like this: + // v2 -> e1 -> v3 + // It will return v3 + GraphTraversal<Vertex, Vertex> pipeline = this.g + .V(start) + .union( + __.inE().has(CONTAINS.toString(), NONE.toString()), + __.outE().has(CONTAINS.toString(), NONE.toString()) + ) + .not( + __.has(PRIVATE.toString(), true) + ) + .otherV() + .dedup(); + return pipeline.toList(); } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Console.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Console.java index 18fed756..d42f33f1 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Console.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Console.java @@ -22,15 +22,17 @@ package org.onap.aai.serialization.queryformats; import com.google.gson.JsonObject; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; +import java.util.Optional; + public class Console implements FormatMapper { @Override - public JsonObject formatObject(Object v) throws AAIFormatVertexException { + public Optional<JsonObject> formatObject(Object v) throws AAIFormatVertexException { JsonObject json = new JsonObject(); json.addProperty("result", v.toString()); - return json; + return Optional.of(json); } @Override diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Count.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Count.java index 87ffd3ad..053c8def 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Count.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Count.java @@ -19,10 +19,7 @@ */ package org.onap.aai.serialization.queryformats; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - +import com.google.gson.JsonObject; import org.apache.tinkerpop.gremlin.process.traversal.Path; import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; import org.apache.tinkerpop.gremlin.structure.Vertex; @@ -31,24 +28,26 @@ import org.onap.aai.db.props.AAIProperties; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; -import com.google.gson.JsonObject; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; public class Count implements FormatMapper { @Override - public JsonObject formatObject(Object o) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported { + public Optional<JsonObject> formatObject(Object o) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported { @SuppressWarnings("unchecked") List<Object> list = (List<Object>) o; final JsonObject countResult = new JsonObject(); list.stream().map(this::getCount) - .filter( Optional<Pair<String, Long>>::isPresent ) - .map(Optional<Pair<String, Long>>::get) + .filter( Optional::isPresent ) + .map(Optional::get) .collect( Collectors.toConcurrentMap( Pair::getValue0, Pair::getValue1, Long::sum ) ) .forEach( (k,v) -> countResult.addProperty(k, v) ); - return countResult; + return Optional.of(countResult); } @Override diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Format.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Format.java index be57fb92..abd261ed 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Format.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Format.java @@ -19,14 +19,25 @@ */ package org.onap.aai.serialization.queryformats; +import org.onap.aai.exceptions.AAIException; + public enum Format { graphson, pathed, + pathed_resourceversion, id, resource, simple, resource_and_url, console, raw, - count + count; + + public static Format getFormat(String format) throws AAIException { + try { + return Format.valueOf(format); + } catch (IllegalArgumentException e) { + throw new AAIException("AAI_6120", "Unsupported format query parameter " + format + " in request."); + } + } } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/FormatFactory.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/FormatFactory.java index 416ff9fe..29ae8512 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/FormatFactory.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/FormatFactory.java @@ -28,6 +28,7 @@ import org.onap.aai.serialization.db.DBSerializer; import org.onap.aai.serialization.queryformats.exceptions.QueryParamInjectionException; import org.onap.aai.serialization.queryformats.utils.QueryParamInjector; import org.onap.aai.serialization.queryformats.utils.UrlBuilder; +import org.onap.aai.setup.SchemaVersions; public class FormatFactory { @@ -35,10 +36,11 @@ public class FormatFactory { private final DBSerializer serializer; private final UrlBuilder urlBuilder; private final QueryParamInjector injector; - public FormatFactory (Loader loader, DBSerializer serializer) throws AAIException { + + public FormatFactory (Loader loader, DBSerializer serializer, SchemaVersions schemaVersions, String basePath) throws AAIException { this.loader = loader; this.serializer = serializer; - this.urlBuilder = new UrlBuilder(loader.getVersion(), serializer); + this.urlBuilder = new UrlBuilder(loader.getVersion(), serializer, schemaVersions, basePath); this.injector = QueryParamInjector.getInstance(); } @@ -57,6 +59,9 @@ public class FormatFactory { case pathed : formatter = new Formatter(inject(new PathedURL(loader, urlBuilder), params)); break; + case pathed_resourceversion : + formatter = new Formatter(inject(new PathedURL(loader, urlBuilder).includeUrl(), params)); + break; case id : formatter = new Formatter(inject(new IdURL(loader, urlBuilder), params)); break; diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/FormatMapper.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/FormatMapper.java index a98b4235..287170d0 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/FormatMapper.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/FormatMapper.java @@ -23,9 +23,11 @@ import com.google.gson.JsonObject; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; +import java.util.Optional; + public interface FormatMapper { - public JsonObject formatObject(Object o) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported; + Optional<JsonObject> formatObject(Object o) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported; - public int parallelThreshold(); + int parallelThreshold(); } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Formatter.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Formatter.java index 123a1b2e..72847149 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Formatter.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Formatter.java @@ -24,7 +24,6 @@ import com.att.eelf.configuration.EELFManager; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; - import org.onap.aai.logging.LogFormatTools; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; @@ -46,14 +45,14 @@ public class Formatter { public JsonObject output(List<Object> queryResults) { - Stream<Object> stream = null; + Stream<Object> stream; JsonObject result = new JsonObject(); JsonArray body = new JsonArray(); if (this.format instanceof Count) { JsonObject countResult; try { - countResult = format.formatObject(queryResults); + countResult = format.formatObject(queryResults).orElseThrow(() -> new AAIFormatVertexException("")); body.add(countResult); } catch (Exception e) { LOGGER.warn("Failed to format result type of the query " + LogFormatTools.getStackTop(e)); @@ -69,7 +68,7 @@ public class Formatter { stream.map(o -> { try { - return Optional.<JsonObject>of(format.formatObject(o)); + return format.formatObject(o); } catch (AAIFormatVertexException e) { LOGGER.warn("Failed to format vertex, returning a partial list " + LogFormatTools.getStackTop(e)); } catch (AAIFormatQueryResultFormatNotSupported e) { @@ -78,8 +77,8 @@ public class Formatter { return Optional.<JsonObject>empty(); }) - .filter(Optional<JsonObject>::isPresent) - .map(Optional<JsonObject>::get) + .filter(Optional::isPresent) + .map(Optional::get) .forEach(json -> { if (isParallel) { synchronized (body) { diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/GraphSON.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/GraphSON.java index 4e69c9c2..7e0f3f6c 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/GraphSON.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/GraphSON.java @@ -19,17 +19,23 @@ */ package org.onap.aai.serialization.queryformats; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper; import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter; +import org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Optional; public class GraphSON implements FormatMapper { @@ -38,7 +44,7 @@ public class GraphSON implements FormatMapper { protected JsonParser parser = new JsonParser(); @Override - public JsonObject formatObject(Object v) { + public Optional<JsonObject> formatObject(Object v) { OutputStream os = new ByteArrayOutputStream(); String result = ""; try { @@ -49,11 +55,73 @@ public class GraphSON implements FormatMapper { // TODO Auto-generated catch block e.printStackTrace(); } - - return parser.parse(result).getAsJsonObject(); + + JsonObject jsonObject = parser.parse(result).getAsJsonObject(); + + if(jsonObject != null){ + + if(jsonObject.has("outE")){ + JsonObject outEdges = jsonObject.get("outE").getAsJsonObject(); + removePrivateEdges(jsonObject, outEdges, "outE"); + } + + if(jsonObject.has("inE")){ + JsonObject inEdges = jsonObject.get("inE").getAsJsonObject(); + removePrivateEdges(jsonObject, inEdges, "inE"); + } + + } + + return Optional.of(jsonObject); } - + + /** + * Removes the private edges from the json object + * + * Please note that the reason to choose to remove the private + * edges from the json object instead of removing it from the vertex + * itself is the fact that even though the transaction will be rolled back + * is because of the possible incosistent behavior where the actual edge + * might actually be removed in a long running transaction and is not worth the risk + * + * @param jsonObject - JSON Object from which we are removing the private edges for + * @param edges - JSONObject HashMap representing all of the edges + * @param edgeDirection - a string indicating the direction of the edge + */ + private void removePrivateEdges(JsonObject jsonObject, JsonObject edges, String edgeDirection) { + + Iterator it = edges.entrySet().iterator(); + while(it.hasNext()){ + Map.Entry<String, JsonElement> outEntry = (Map.Entry<String, JsonElement>) it.next(); + JsonArray edgePropertiesArray = outEntry.getValue().getAsJsonArray(); + for(int index = 0; index < edgePropertiesArray.size(); ++index){ + JsonElement jsonElement = edgePropertiesArray.get(index); + JsonObject obj = jsonElement.getAsJsonObject(); + if (obj.has("properties")) { + JsonObject objProperties = obj.get("properties").getAsJsonObject(); + if (objProperties.has("private")) { + boolean isPrivate = objProperties.get("private").getAsBoolean(); + if (isPrivate) { + if (edges.size() == 1) { + if (edgePropertiesArray.size() == 1) { + jsonObject.remove(edgeDirection); + } else { + edgePropertiesArray.remove(jsonElement); + } + } else { + edgePropertiesArray.remove(jsonElement); + } + } + } + } + } + if(edgePropertiesArray.size() == 0){ + it.remove(); + } + } + } + @Override public int parallelThreshold() { return 50; diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/IdURL.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/IdURL.java index 8fd196d8..6d344780 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/IdURL.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/IdURL.java @@ -30,6 +30,8 @@ import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; import org.onap.aai.serialization.queryformats.utils.UrlBuilder; +import java.util.Optional; + public class IdURL extends MultiFormatMapper { private final UrlBuilder urlBuilder; @@ -48,7 +50,7 @@ public class IdURL extends MultiFormatMapper { } @Override - protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException { + protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException { try { final Introspector searchResult = this.loader.introspectorFromName("result-data"); @@ -58,7 +60,7 @@ public class IdURL extends MultiFormatMapper { final String json = searchResult.marshal(false); - return parser.parse(json).getAsJsonObject(); + return Optional.of(parser.parse(json).getAsJsonObject()); } catch (AAIUnknownObjectException e) { throw new RuntimeException("Fatal error - result-data object does not exist!"); diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/MultiFormatMapper.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/MultiFormatMapper.java index 8ee52a10..944c14c4 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/MultiFormatMapper.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/MultiFormatMapper.java @@ -1,108 +1,108 @@ -/**
- * ============LICENSE_START=======================================================
- * org.onap.aai
- * ================================================================================
- * Copyright © 2017-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.aai.serialization.queryformats;
-
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.tinkerpop.gremlin.process.traversal.Path;
-import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
-import org.apache.tinkerpop.gremlin.structure.Vertex;
-import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
-import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
-
-import com.google.gson.JsonArray;
-import com.google.gson.JsonObject;
-
-public abstract class MultiFormatMapper implements FormatMapper {
-
- @Override
- public JsonObject formatObject(Object input) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
- if (input instanceof Vertex) {
- return this.getJsonFromVertex((Vertex) input);
- } else if (input instanceof Tree) {
- return this.getJsonFomTree((Tree<?>) input);
- } else if (input instanceof Path) {
- return this.getJsonFromPath((Path) input);
- } else {
- throw new AAIFormatQueryResultFormatNotSupported();
- }
- }
-
- protected abstract JsonObject getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
-
- protected JsonObject getJsonFromPath(Path input) throws AAIFormatVertexException {
- List<Object> path = input.objects();
-
- JsonObject jo = new JsonObject();
- JsonArray ja = new JsonArray();
-
- for (Object o : path) {
- if (o instanceof Vertex) {
- ja.add(this.getJsonFromVertex((Vertex)o));
- }
- }
-
- jo.add("path", ja);
- return jo;
- }
-
- protected JsonObject getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
-
- if (tree.isEmpty()) {
- return new JsonObject();
- }
-
- JsonObject t = new JsonObject();
- JsonArray ja = this.getNodesArray(tree);
- if (ja.size() > 0) {
- t.add("nodes", ja);
- }
-
- return t;
- }
-
- private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {
-
- JsonArray nodes = new JsonArray();
- Iterator<?> it = tree.keySet().iterator();
-
- while (it.hasNext()) {
- Object o = it.next();
- JsonObject me = new JsonObject();
- if (o instanceof Vertex) {
- me = this.getJsonFromVertex((Vertex) o);
- }
- JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));
- if (ja.size() > 0) {
- me.add("nodes", ja);
- }
- nodes.add(me);
- }
- return nodes;
- }
-
-
- @Override
- public int parallelThreshold() {
- return 100;
- }
-
-}
+/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.serialization.queryformats; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import org.apache.tinkerpop.gremlin.process.traversal.Path; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported; +import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; + +import java.util.Iterator; +import java.util.List; +import java.util.Optional; + +public abstract class MultiFormatMapper implements FormatMapper { + + @Override + public Optional<JsonObject> formatObject(Object input) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported { + if (input instanceof Vertex) { + return this.getJsonFromVertex((Vertex) input); + } else if (input instanceof Tree) { + return this.getJsonFomTree((Tree<?>) input); + } else if (input instanceof Path) { + return this.getJsonFromPath((Path) input); + } else { + throw new AAIFormatQueryResultFormatNotSupported(); + } + } + + protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException; + + protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException { + List<Object> path = input.objects(); + + JsonObject jo = new JsonObject(); + JsonArray ja = new JsonArray(); + + for (Object o : path) { + if (o instanceof Vertex) { + ja.add(this.getJsonFromVertex((Vertex)o).get()); + } + } + + jo.add("path", ja); + return Optional.of(jo); + } + + protected Optional<JsonObject> getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException { + + if (tree.isEmpty()) { + return Optional.of(new JsonObject()); + } + + JsonObject t = new JsonObject(); + JsonArray ja = this.getNodesArray(tree); + if (ja.size() > 0) { + t.add("nodes", ja); + } + + return Optional.of(t); + } + + private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException { + + JsonArray nodes = new JsonArray(); + Iterator<?> it = tree.keySet().iterator(); + + while (it.hasNext()) { + Object o = it.next(); + JsonObject me = new JsonObject(); + if (o instanceof Vertex) { + me = this.getJsonFromVertex((Vertex) o).get(); + } + JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o)); + if (ja.size() > 0) { + me.add("nodes", ja); + } + nodes.add(me); + } + return nodes; + } + + + @Override + public int parallelThreshold() { + return 100; + } + +} diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/PathedURL.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/PathedURL.java index f8bdcd7d..d2180e38 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/PathedURL.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/PathedURL.java @@ -30,12 +30,16 @@ import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; import org.onap.aai.serialization.queryformats.utils.UrlBuilder; +import java.util.Optional; + public final class PathedURL extends MultiFormatMapper { private final UrlBuilder urlBuilder; private final JsonParser parser; private final Loader loader; - + private boolean includeUrl = false; + + public PathedURL (Loader loader, UrlBuilder urlBuilder) throws AAIException { this.urlBuilder = urlBuilder; this.parser = new JsonParser(); @@ -47,8 +51,14 @@ public final class PathedURL extends MultiFormatMapper { return 20; } + public PathedURL includeUrl() { + this.includeUrl = true; + return this; + } + + @Override - protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException { + protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException { try { final Introspector searchResult = this.loader.introspectorFromName("result-data"); @@ -56,8 +66,12 @@ public final class PathedURL extends MultiFormatMapper { searchResult.setValue("resource-type", v.value(AAIProperties.NODE_TYPE)); searchResult.setValue("resource-link", this.urlBuilder.pathed(v)); + + if(includeUrl) + searchResult.setValue("resource-version", v.value(AAIProperties.RESOURCE_VERSION)); + final String json = searchResult.marshal(false); - return this.parser.parse(json).getAsJsonObject(); + return Optional.of(this.parser.parse(json).getAsJsonObject()); } catch (AAIUnknownObjectException e) { throw new RuntimeException("Fatal error - result-data does not exist!", e); diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/RawFormat.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/RawFormat.java index fc68def4..e90ee6b1 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/RawFormat.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/RawFormat.java @@ -19,9 +19,10 @@ */ package org.onap.aai.serialization.queryformats; -import java.util.Iterator; -import java.util.List; - +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Vertex; @@ -34,10 +35,9 @@ import org.onap.aai.serialization.queryformats.params.Depth; import org.onap.aai.serialization.queryformats.params.NodesOnly; import org.onap.aai.serialization.queryformats.utils.UrlBuilder; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; public class RawFormat extends MultiFormatMapper { @@ -61,7 +61,7 @@ public class RawFormat extends MultiFormatMapper { } - public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException { + public Optional<JsonObject> createPropertiesObject(Vertex v) throws AAIFormatVertexException { JsonObject json = new JsonObject(); Iterator<VertexProperty<Object>> iter = v.properties(); @@ -84,7 +84,7 @@ public class RawFormat extends MultiFormatMapper { } } - return json; + return Optional.of(json); } protected JsonArray createRelationshipObject(Vertex v) throws AAIFormatVertexException { @@ -94,18 +94,24 @@ public class RawFormat extends MultiFormatMapper { while (inIter.hasNext()) { Edge e = inIter.next(); - jarray.add(this.getRelatedObject(e.label(), e.outVertex())); + Vertex outVertex = e.outVertex(); + this.addEdge(e, outVertex, jarray); } while (outIter.hasNext()) { Edge e = outIter.next(); - jarray.add(this.getRelatedObject(e.label(), e.inVertex())); + Vertex inVertex = e.inVertex(); + this.addEdge(e, inVertex, jarray); } return jarray; } + + protected void addEdge(Edge e, Vertex vertex, JsonArray array) throws AAIFormatVertexException { + array.add(this.getRelatedObject(e.label(), vertex)); + } - private JsonObject getRelatedObject(String label, Vertex related) throws AAIFormatVertexException { + protected JsonObject getRelatedObject(String label, Vertex related) throws AAIFormatVertexException { JsonObject json = new JsonObject(); json.addProperty("id", related.id().toString()); json.addProperty("relationship-label", label); @@ -186,16 +192,21 @@ public class RawFormat extends MultiFormatMapper { } @Override - protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException { + protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException { JsonObject json = new JsonObject(); json.addProperty("id", v.id().toString()); json.addProperty("node-type", v.<String>value(AAIProperties.NODE_TYPE)); json.addProperty("url", this.urlBuilder.pathed(v)); - json.add("properties", this.createPropertiesObject(v)); + Optional<JsonObject> properties = this.createPropertiesObject(v); + if (properties.isPresent()) { + json.add("properties", properties.get()); + } else { + return Optional.empty(); + } if (!nodesOnly) { json.add("related-to", this.createRelationshipObject(v)); } - return json; + return Optional.of(json); } } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Resource.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Resource.java index 2940adbf..3a4fdf89 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Resource.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/Resource.java @@ -19,10 +19,8 @@ */ package org.onap.aai.serialization.queryformats; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.List; - +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.onap.aai.db.props.AAIProperties; import org.onap.aai.exceptions.AAIException; @@ -35,8 +33,10 @@ import org.onap.aai.serialization.queryformats.params.Depth; import org.onap.aai.serialization.queryformats.params.NodesOnly; import org.onap.aai.serialization.queryformats.utils.UrlBuilder; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; public class Resource extends MultiFormatMapper { @@ -58,20 +58,23 @@ public class Resource extends MultiFormatMapper { } @Override - protected JsonObject getJsonFromVertex(Vertex v) throws AAIFormatVertexException { + protected Optional<JsonObject> getJsonFromVertex(Vertex v) throws AAIFormatVertexException { JsonObject json = new JsonObject(); - + if (this.includeUrl) { json.addProperty("url", this.urlBuilder.pathed(v)); } - json.add(v.<String>property(AAIProperties.NODE_TYPE) - .orElse(null), this.vertexToJsonObject(v)); - - return json; + Optional<JsonObject> jsonObject = this.vertexToJsonObject(v); + if (jsonObject.isPresent()) { + json.add(v.<String>property(AAIProperties.NODE_TYPE).orElse(null), jsonObject.get()); + } else { + return Optional.empty(); + } + return Optional.of(json); } - protected JsonObject vertexToJsonObject(Vertex v) throws AAIFormatVertexException { + protected Optional<JsonObject> vertexToJsonObject(Vertex v) throws AAIFormatVertexException { try { final Introspector obj = getLoader().introspectorFromName( v.<String>property(AAIProperties.NODE_TYPE) @@ -90,9 +93,9 @@ public class Resource extends MultiFormatMapper { final String json = obj.marshal(false); - return getParser().parse(json).getAsJsonObject(); + return Optional.of(getParser().parse(json).getAsJsonObject()); } catch (AAIUnknownObjectException e) { - throw new AAIFormatVertexException("Failed to format vertex - unknown object", e); + return Optional.empty(); } } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/SimpleFormat.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/SimpleFormat.java index 2efb657e..c9d3a026 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/SimpleFormat.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/SimpleFormat.java @@ -19,10 +19,10 @@ */ package org.onap.aai.serialization.queryformats; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.List; - +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Property; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.onap.aai.db.props.AAIProperties; import org.onap.aai.exceptions.AAIException; @@ -30,7 +30,10 @@ import org.onap.aai.introspection.Introspector; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; -import com.google.gson.JsonObject; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; public class SimpleFormat extends RawFormat { @@ -46,7 +49,7 @@ public class SimpleFormat extends RawFormat { } @Override - public JsonObject createPropertiesObject(Vertex v) throws AAIFormatVertexException { + public Optional<JsonObject> createPropertiesObject(Vertex v) throws AAIFormatVertexException { try { final Introspector obj = loader.introspectorFromName( v.<String>property(AAIProperties.NODE_TYPE) @@ -64,11 +67,25 @@ public class SimpleFormat extends RawFormat { } final String json = obj.marshal(false); - return parser.parse(json).getAsJsonObject(); + return Optional.of(parser.parse(json).getAsJsonObject()); } catch (AAIUnknownObjectException e) { - throw new AAIFormatVertexException("Failed to format vertex - unknown object", e); + return Optional.empty(); } } + + @Override + protected void addEdge(Edge e, Vertex v, JsonArray array) throws AAIFormatVertexException { + + Property property = e.property("private"); + + if(property.isPresent()){ + if("true".equals(e.property("private").value().toString())){ + return; + } + } + + super.addEdge(e, v, array); + } } diff --git a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/utils/UrlBuilder.java b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/utils/UrlBuilder.java index b252cbb8..30459364 100644 --- a/aai-core/src/main/java/org/onap/aai/serialization/queryformats/utils/UrlBuilder.java +++ b/aai-core/src/main/java/org/onap/aai/serialization/queryformats/utils/UrlBuilder.java @@ -21,33 +21,46 @@ package org.onap.aai.serialization.queryformats.utils; import java.io.UnsupportedEncodingException; import java.net.URI; -import java.net.URISyntaxException; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.serialization.db.DBSerializer; import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException; -import org.onap.aai.util.AAIApiServerURLBase; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.util.AAIConfig; import org.onap.aai.util.AAIConstants; -import org.onap.aai.workarounds.LegacyURITransformer; public class UrlBuilder { private final DBSerializer serializer; - private final Version version; + private final SchemaVersion version; private final String serverBase; - - public UrlBuilder (Version version, DBSerializer serializer) throws AAIException { + private final SchemaVersions schemaVersions; + private final String basePath; + + public UrlBuilder (SchemaVersion version, DBSerializer serializer, SchemaVersions schemaVersions, String basePath) throws AAIException { this.serializer = serializer; this.version = version; - this.serverBase = this.getServerBase(version); + this.serverBase = this.getServerBase(); + this.schemaVersions = schemaVersions; + if(!basePath.endsWith("/")){ + this.basePath = basePath + "/"; + } else { + this.basePath = basePath; + } } - public UrlBuilder (Version version, DBSerializer serializer, String serverBase) { + public UrlBuilder (SchemaVersion version, DBSerializer serializer, String serverBase, SchemaVersions schemaVersions, String basePath) { this.serializer = serializer; this.version = version; this.serverBase = serverBase; + this.schemaVersions = schemaVersions; + if(!basePath.endsWith("/")){ + this.basePath = basePath + "/"; + } else { + this.basePath = basePath; + } } public String pathed(Vertex v) throws AAIFormatVertexException { @@ -55,17 +68,17 @@ public class UrlBuilder { try { final StringBuilder result = new StringBuilder(); final URI uri = this.serializer.getURIForVertex(v); - - if (this.version.compareTo(Version.v11) >= 0) { - result.append(AAIConstants.AAI_APP_ROOT); + + if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) { + result.append(basePath); } else { result.append(this.serverBase); } result.append(this.version); - result.append(uri); - - return result.toString(); - } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) { + result.append(uri.getRawPath()); + + return result.toString(); + } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) { throw new AAIFormatVertexException(e); } } @@ -75,8 +88,8 @@ public class UrlBuilder { result.append("/resources/id/" + v.id()); result.insert(0, this.version); - if (this.version.compareTo(Version.v11) >= 0) { - result.insert(0, AAIConstants.AAI_APP_ROOT); + if (this.version.compareTo(schemaVersions.getAppRootVersion()) >= 0) { + result.insert(0, basePath); } else { result.insert(0, this.serverBase); } @@ -84,7 +97,7 @@ public class UrlBuilder { return result.toString(); } - protected String getServerBase(Version v) throws AAIException { - return AAIApiServerURLBase.get(v); + protected String getServerBase() throws AAIException { + return AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE); } } diff --git a/aai-core/src/main/java/org/onap/aai/tasks/ScheduledTasks.java b/aai-core/src/main/java/org/onap/aai/tasks/ScheduledTasks.java index 04fffc82..81c81f55 100644 --- a/aai-core/src/main/java/org/onap/aai/tasks/ScheduledTasks.java +++ b/aai-core/src/main/java/org/onap/aai/tasks/ScheduledTasks.java @@ -56,7 +56,8 @@ public class ScheduledTasks { public void loadAAIProperties() { final UUID transId = UUID.randomUUID(); - LoggingContext.init(); + //LoggingContext.init(); + LoggingContext.save(); LoggingContext.requestId(transId); LoggingContext.partnerName(FROM_APP_ID); LoggingContext.component(COMPONENT); @@ -92,5 +93,6 @@ public class ScheduledTasks { break; } } + LoggingContext.restoreIfPossible(); } } diff --git a/aai-core/src/main/java/org/onap/aai/util/AAIApiServerURLBase.java b/aai-core/src/main/java/org/onap/aai/util/AAIApiServerURLBase.java deleted file mode 100644 index 113c0b88..00000000 --- a/aai-core/src/main/java/org/onap/aai/util/AAIApiServerURLBase.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.util; - -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Version; - -public class AAIApiServerURLBase { - - /** - * Gets the. - * - * @return the string - * @throws AAIException the AAI exception - */ - public static String get() throws AAIException { - - String hostName = null; -// try { -// Message message = PhaseInterceptorChain.getCurrentMessage(); -// Map<String, List<String>> headers = CastUtils.cast((Map) message.get(Message.PROTOCOL_HEADERS)); -// List sa = null; -// if (headers != null) { -// sa = headers.get("host"); -// } -// -// if (sa != null && sa.size() == 1) { -// hostName = "https://"+ sa.get(0).toString() + "/aai/"; -// } -// } catch (Exception e) { -// // TODO: we may want to log an error here -// } - // TODO: should this check the value a little closer and look for a pattern? - if (hostName == null) { - hostName = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE); - //AAIConstants.AAI_SERVER_URL_BASE; - } - return hostName; - } - - /** - * Gets the. - * - * @param v the v - * @return the string - * @throws AAIException the AAI exception - */ - public static String get(Version v) throws AAIException { - String hostName = null; - hostName = AAIApiServerURLBase.get(); - return hostName; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/util/AAIConfig.java b/aai-core/src/main/java/org/onap/aai/util/AAIConfig.java index 505e9940..5b48127c 100644 --- a/aai-core/src/main/java/org/onap/aai/util/AAIConfig.java +++ b/aai-core/src/main/java/org/onap/aai/util/AAIConfig.java @@ -48,8 +48,6 @@ public class AAIConfig { private static Properties serverProps; private static boolean propsInitialized = false; - // this (probably) won't change between releases, put it in the config if it gets annoying... - private static HashMap<String,ArrayList<String>> defaultBools = new HashMap<String,ArrayList<String>>(); /** * Instantiates a new AAI config. */ @@ -74,30 +72,6 @@ public class AAIConfig { LOGGER.info("Initializing AAIConfig"); - ArrayList<String> genericVnfBools = new ArrayList<String>(); - ArrayList<String> l3NetworkBools = new ArrayList<String>(); - ArrayList<String> pserverBools = new ArrayList<String>(); - ArrayList<String> subnetBools = new ArrayList<String>(); - ArrayList<String> vserverBools = new ArrayList<String>(); - ArrayList<String> vnfcBools = new ArrayList<String>(); - - genericVnfBools.add("in-maint"); - genericVnfBools.add("is-closed-loop-disabled"); - l3NetworkBools.add("is-bound-to-vpn"); - pserverBools.add("in-maint"); - subnetBools.add("dhcp-enabled"); - vserverBools.add("in-maint"); - vserverBools.add("is-closed-loop-disabled"); - vnfcBools.add("in-maint"); - vnfcBools.add("is-closed-loop-disabled"); - - defaultBools.put("generic-vnf", genericVnfBools); - defaultBools.put("l3-network", l3NetworkBools); - defaultBools.put("pserver", pserverBools); - defaultBools.put("subnet", subnetBools); - defaultBools.put("vserver", vserverBools); - defaultBools.put("vnfc", vnfcBools); - AAIConfig.getConfigFile(); AAIConfig.reloadConfig(); @@ -110,26 +84,11 @@ public class AAIConfig { } /** - * Gets the default bools. - * - * @return the default bools - */ - public static HashMap<String,ArrayList<String>> getDefaultBools() { - return defaultBools; - } - - /** * Gets the config file. * * @return the config file */ public static String getConfigFile() { -// if (GlobalPropFileName == null) { -// String nc = System.getProperty("aaiconfig"); -// if (nc == null) nc = "/home/aaiadmin/etc/aaiconfig.props"; -// logger.info( "aaiconfig = " + nc==null?"null":nc); -// GlobalPropFileName = nc; -// } return GLOBAL_PROP_FILE_NAME; } @@ -143,14 +102,11 @@ public class AAIConfig { LOGGER.debug("Reloading config from " + propFileName); - try(InputStream is = new FileInputStream(propFileName)){ + try(InputStream is = new FileInputStream(propFileName)) { newServerProps = new Properties(); newServerProps.load(is); propsInitialized = true; - serverProps = newServerProps; - newServerProps = null; - } catch (FileNotFoundException fnfe) { ErrorLogHelper.logError("AAI_4001", " " + propFileName + ". Exception: "+fnfe.getMessage()); } catch (IOException e) { @@ -168,10 +124,11 @@ public class AAIConfig { public static String get(String key, String defaultValue) { String result = defaultValue; try { - result = get (key); + result = get (key); + } catch ( AAIException a ) { } - catch ( AAIException a ) { - + if (result == null || result.isEmpty()) { + result = defaultValue; } return ( result ); } @@ -266,5 +223,4 @@ public class AAIConfig { { return (s == null || s.length() == 0); } - } diff --git a/aai-core/src/main/java/org/onap/aai/util/AAIConfigCommandLinePropGetter.java b/aai-core/src/main/java/org/onap/aai/util/AAIConfigCommandLinePropGetter.java index 54ca943b..8f4b1105 100644 --- a/aai-core/src/main/java/org/onap/aai/util/AAIConfigCommandLinePropGetter.java +++ b/aai-core/src/main/java/org/onap/aai/util/AAIConfigCommandLinePropGetter.java @@ -43,8 +43,8 @@ public class AAIConfigCommandLinePropGetter { */ public static void main(String[] args) { if (args.length != 1) { - System.out.println("only one property may be requested at a time"); - System.out.println("usage: AAIConfigCommandLinePropGetter propertyname"); + // System.out.println("only one property may be requested at a time"); + // System.out.println("usage: AAIConfigCommandLinePropGetter propertyname"); } try { AAIConfig.init(); @@ -55,7 +55,7 @@ public class AAIConfigCommandLinePropGetter { System.out.println("requested property could not be found"); } } catch(AAIException e) { - System.out.println("exception:" + e.toString()); //TODO is this reasonable? + //System.out.println("exception:" + e.toString()); //TODO is this reasonable? } finally { System.exit(0); } diff --git a/aai-core/src/main/java/org/onap/aai/util/AAIConstants.java b/aai-core/src/main/java/org/onap/aai/util/AAIConstants.java index f438d16b..cbcbd46e 100644 --- a/aai-core/src/main/java/org/onap/aai/util/AAIConstants.java +++ b/aai-core/src/main/java/org/onap/aai/util/AAIConstants.java @@ -19,10 +19,7 @@ */ package org.onap.aai.util; -import org.onap.aai.db.props.AAIProperties; - public final class AAIConstants { - // // /** Default to unix file separator if system property file.separator is null */ @@ -39,50 +36,26 @@ public final class AAIConstants { public static final String AAI_HOME_ETC_AUTH = AAI_HOME_ETC + "auth" + AAI_FILESEP; public static final String AAI_CONFIG_FILENAME = AAI_HOME_ETC_APP_PROPERTIES + "aaiconfig.properties"; public static final String AAI_AUTH_CONFIG_FILENAME = AAI_HOME_ETC_AUTH + "aai_policy.json"; - public static final String AAI_MECHID_CONFIG_FILENAME = AAI_HOME_ETC_APP_PROPERTIES + "mechIds.json"; - public static final String AAI_HOME_ETC_QUERY = AAI_HOME_ETC + "query" + AAI_FILESEP + "stored-queries.properties"; public static final String REALTIME_DB_CONFIG = AAI_HOME_ETC_APP_PROPERTIES + "janusgraph-realtime.properties"; public static final String CACHED_DB_CONFIG = AAI_HOME_ETC_APP_PROPERTIES + "janusgraph-cached.properties"; public static final String AAI_HOME_ETC_OXM = AAI_HOME_ETC + "oxm" + AAI_FILESEP; public static final String AAI_EVENT_DMAAP_PROPS = AAI_HOME_ETC_APP_PROPERTIES + "aaiEventDMaaPPublisher.properties"; public static final String AAI_HOME_ETC_SCRIPT = AAI_HOME_ETC + AAI_FILESEP + "scriptdata" + AAI_FILESEP; - - public static final String AAI_PROV_LOGBACK_PROPS = "prov-logback.xml"; - public static final String AAI_GETRES_LOGBACK_PROPS = "getres-logback.xml"; - public static final String AAI_DELTOOL_LOGBACK_PROPS = "deltool-logback.xml"; - public static final String AAI_UPDTOOL_LOGBACK_PROPS = "updtool-logback.xml"; - public static final String AAI_PUTTOOL_LOGBACK_PROPS = "puttool-logback.xml"; - public static final String AAI_POSTTOOL_LOGBACK_PROPS = "posttool-logback.xml"; - public static final String AAI_NOTIFYSDNCTOOL_LOGBACK_PROPS = "notifysdnctool-logback.xml"; - public static final String AAI_RSHIPTOOL_LOGBACK_PROPS = "rshiptool-logback.xml"; public static final String AAI_LOGBACK_PROPS = "logback.xml"; - - - public static final String AAI_CREATE_DB_SCHEMA_LOGBACK_PROPS = "createDBSchema-logback.xml"; - public static final String AAI_PULL_INV_DATA_LOGBACK_PROPS = "pullInvData-logback.xml"; - public static final String AAI_DATA_GROOMING_LOGBACK_PROPS = "dataGrooming-logback.xml"; - public static final String AAI_DATA_SNAPSHOT_LOGBACK_PROPS = "dataSnapshot-logback.xml"; public static final String AAI_SCHEMA_MOD_LOGBACK_PROPS = "schemaMod-logback.xml"; public static final String AAI_FORCE_DELETE_LOGBACK_PROPS = "forceDelete-logback.xml"; - public static final String AAI_LOAD_DATA_DHV_LOGBACK_PROPS = "loadDataForDHV-logback.xml"; - - public static final String AVPN_INTERIM_LAG_INTERFACE = "aai.avpn.interim.laginterface"; - public static final String AAI_TRUSTSTORE_FILENAME = "aai.truststore.filename"; public static final String AAI_TRUSTSTORE_PASSWD = "aai.truststore.passwd"; public static final String AAI_KEYSTORE_FILENAME = "aai.keystore.filename"; public static final String AAI_KEYSTORE_PASSWD = "aai.keystore.passwd"; - - public static final String AAI_OLDSERVER_URL_BASE = "aai.oldserver.url.base"; + public static final String AAI_SERVER_URL_BASE = "aai.server.url.base"; public static final String AAI_SERVER_URL = "aai.server.url"; public static final String AAI_OLDSERVER_URL = "aai.oldserver.url"; - public static final String AAI_GLOBAL_CALLBACK_URL = "aai.global.callback.url"; - public static final String AAI_LOCAL_REST = "https://localhost:%d/aai/" + AAIProperties.LATEST + "/"; - public static final String AAI_APP_ROOT = "/aai/"; - + public static final String AAI_LOCAL_REST = "https://localhost:%d/aai/%s/"; + public static final int AAI_RESOURCES_PORT = 8447; public static final int AAI_QUERY_PORT = 8446; public static final int AAI_LEGACY_PORT = 8443; @@ -100,58 +73,29 @@ public final class AAIConstants { public static final String AAI_TRAVERSAL_TIMEOUT_ENABLED = "aai.traversal.timeoutenabled"; public static final String AAI_TRAVERSAL_TIMEOUT_APP = "aai.traversal.timeout.appspecific"; + public static final String AAI_GRAPHADMIN_TIMEOUT_LIMIT = "aai.graphadmin.timeoutlimit"; + public static final String AAI_GRAPHADMIN_TIMEOUT_ENABLED = "aai.graphadmin.timeoutenabled"; + public static final String AAI_GRAPHADMIN_TIMEOUT_APP = "aai.graphadmin.timeout.appspecific"; + public static final String AAI_CRUD_TIMEOUT_LIMIT = "aai.crud.timeoutlimit"; public static final String AAI_CRUD_TIMEOUT_ENABLED = "aai.crud.timeoutenabled"; public static final String AAI_CRUD_TIMEOUT_APP = "aai.crud.timeout.appspecific"; - public static final String AAI_LOGGING_HBASE_INTERCEPTOR = "aai.logging.hbase.interceptor"; - public static final String AAI_LOGGING_HBASE_ENABLED = "aai.logging.hbase.enabled"; - public static final String AAI_LOGGING_HBASE_LOGREQUEST = "aai.logging.hbase.logrequest"; - public static final String AAI_LOGGING_HBASE_LOGRESPONSE = "aai.logging.hbase.logresponse"; - - public static final String AAI_LOGGING_TRACE_ENABLED = "aai.logging.trace.enabled"; - public static final String AAI_LOGGING_TRACE_LOGREQUEST = "aai.logging.trace.logrequest"; - public static final String AAI_LOGGING_TRACE_LOGRESPONSE = "aai.logging.trace.logresponse"; - - public static final String AAI_CONFIG_CHECKINGTIME = "aai.config.checktime"; - public static final String AAI_DBMODEL_FILENAME = "aai.dbmodel.filename"; public static final String AAI_RESVERSION_ENABLEFLAG = "aai.resourceversion.enableflag"; - public static final int AAI_MAX_TRANS_RETRIES = 5; - public static final long AAI_TRANS_RETRY_SLEEP_MSEC = 500; - public static final int AAI_GROOMING_DEFAULT_MAX_FIX = 150; public static final int AAI_GROOMING_DEFAULT_SLEEP_MINUTES = 7; public static final int AAI_DUPETOOL_DEFAULT_MAX_FIX = 25; public static final int AAI_DUPETOOL_DEFAULT_SLEEP_MINUTES = 7; - public static final String LOGGING_MAX_STACK_TRACE_ENTRIES = "aai.logging.maxStackTraceEntries"; - - /** Default to skipping real-time grooming unless system property aai.skiprealtime.grooming is set to "false" */ - public static final String AAI_SKIPREALTIME_GROOMING = (System.getProperty("aai.skiprealtime.grooming") == null) ? "true" : System.getProperty("aai.skiprealtime.grooming"); + public static final long HISTORY_MAX_HOURS = 192; + public static final String LOGGING_MAX_STACK_TRACE_ENTRIES = "aai.logging.maxStackTraceEntries"; /*** UEB ***/ public static final String UEB_PUB_PARTITION_AAI = "AAI"; - public static final String UEB_PUB_AAI_VCE_INTERFACE_DATA_TOPIC = "ueb.pub.aai.vce.interface.data.topic"; - - /** Service description for Hosted Communications */ - public static final String AAI_SERVICEDESCRIPTION_HOSTEDCOMM = "aai.servicedescription.hostedcomm"; - - /** Service description for Mobility */ - public static final String AAI_SERVICEDESCRIPTION_MOBILITY = "aai.servicedescription.mobility"; - - /** Service description for Mobility */ - public static final String AAI_SERVICEDESCRIPTION_VUSP = "aai.servicedescription.vusp"; - - /** Service description for Vvig */ - public static final String AAI_SERVICEDESCRIPTION_VVIG = "aai.servicedescription.vvig"; - - /** Service description for LRSI */ - public static final String AAI_SERVICEDESCRIPTION_LRSI = "aai.servicedescription.lrsi"; - /** Micro-service Names */ public static final String AAI_TRAVERSAL_MS = "aai-traversal"; public static final String AAI_RESOURCES_MS = "aai-resources"; diff --git a/aai-core/src/main/java/org/onap/aai/util/AAIMechIdConfig.java b/aai-core/src/main/java/org/onap/aai/util/AAIMechIdConfig.java deleted file mode 100644 index 2aa33165..00000000 --- a/aai-core/src/main/java/org/onap/aai/util/AAIMechIdConfig.java +++ /dev/null @@ -1,128 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.util; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Set; - -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; - -import org.onap.aai.logging.ErrorLogHelper; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -public class AAIMechIdConfig { - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIMechIdConfig.class); - private static final String mechIdConfigFileName = AAIConstants.AAI_HOME_ETC_APP_PROPERTIES + "mechid-config.json"; - - public static final String SYSTEM_GFP_IP = "GFP"; - public static final String SYSTEM_GCP = "GCP"; - public static final String SYSTEM_DCAE = "DCAE"; - public static final String SYSTEM_RUBY = "RUBY"; - public static final String SYSTEM_ACTION = "ACTION"; - public static final String SYSTEM_INSTAR = "INSTAR-LPP-AMS"; - public static final String FILE_CLASS_GFP_IP = "GFP-IP"; - public static final String FILE_CLASS_INSTAR = "INSTAR-LPP-AMS"; - - public static HashMap<String, String> mechIdtoSystem = new HashMap<String, String>(); - public static HashMap<String, ArrayList<String>> fileClassToMechId = new HashMap<String, ArrayList<String>>(); - - /** - * Inits the. - * - * @param tId the t id - * @param appId the app id - * @param logger the logger - */ - public static void init() { - LOGGER.debug("Initializing AAIMechIdConfig"); - Boolean enable; - String systemMechId = ""; - JSONParser parser = new JSONParser(); - - try { - Object obj = parser.parse(new FileReader(mechIdConfigFileName)); - JSONObject jsonObject = (JSONObject) obj; - JSONObject mechIds = (JSONObject) jsonObject.get("mech-ids"); - - @SuppressWarnings("unchecked") - Set<String> systemSet = mechIds.keySet(); - for (String system : systemSet) { - JSONObject systemJsonObj = (JSONObject) mechIds.get(system); - systemMechId = (String) systemJsonObj.get("mechid"); - enable = (Boolean) systemJsonObj.get("enable"); - if (systemMechId != null && !systemMechId.isEmpty() && enable != null && enable == true) { - mechIdtoSystem.put(systemMechId, system); - JSONArray fileClasses = (JSONArray) systemJsonObj.get("file-classes"); - if (fileClasses != null ) { - String fileClass = ""; - for (Object fileClassObj : fileClasses) { - fileClass = (String) fileClassObj; - - if (!fileClassToMechId.containsKey(fileClass)) { - fileClassToMechId.put(fileClass, new ArrayList<String>()); - fileClassToMechId.get(fileClass).add(systemMechId); - } else { - if(!fileClassToMechId.get(fileClass).contains(systemMechId)){ - fileClassToMechId.get(fileClass).add(systemMechId); - - } - } - } - } - } - } - - } catch (FileNotFoundException fnfe) { - ErrorLogHelper.logError("AAI_4001", - " " + mechIdConfigFileName + ". Exception: " + fnfe.getMessage()); - } catch (Exception e) { - ErrorLogHelper.logError("AAI_4004", - " " + mechIdConfigFileName + ". Exception: " + e.getMessage()); - } - } - - - /** - * Transform mech id to pickup dir. - * - * @param systemMechId the system mech id - * @return the string - */ - public static String transformMechIdToPickupDir(String systemMechId) { - String pickupDir = ""; - if (systemMechId != null && !systemMechId.isEmpty()) { - pickupDir = "/opt/aaihome/" + systemMechId + "/pickup"; - - if (pickupDir != null && !pickupDir.isEmpty() && new File(pickupDir).isDirectory()) { - return pickupDir; - } - - } - return null; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/util/AAIRSyncUtility.java b/aai-core/src/main/java/org/onap/aai/util/AAIRSyncUtility.java deleted file mode 100644 index 64d92244..00000000 --- a/aai-core/src/main/java/org/onap/aai/util/AAIRSyncUtility.java +++ /dev/null @@ -1,196 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.util; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; -import java.util.StringTokenizer; - -import org.onap.aai.exceptions.AAIException; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.onap.aai.logging.ErrorLogHelper; - -public class AAIRSyncUtility { - - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIRSyncUtility.class); - private final String DEFAULT_CHECK = new String("aai.primary.filetransfer."); - - /** - * Instantiates a new AAIR sync utility. - */ - public AAIRSyncUtility() { - - } - - /** - * Do command. - * - * @param command the command - * @return the int - * @throws Exception the exception - */ - public int doCommand(List<String> command) - throws Exception - { - String s = null; - - ProcessBuilder pb = new ProcessBuilder(command); - Process process = pb.start(); - - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - - LOGGER.debug("Here is the standard output of the command:\n"); - while ((s = stdInput.readLine()) != null) - { - LOGGER.debug(s); - } - - LOGGER.debug("Here is the standard error of the command (if any):\n"); - while ((s = stdError.readLine()) != null) - { - LOGGER.debug(s); - } - return process.waitFor(); - } - - - /** - * Method sendRsyncCommand. - * - * @param transId the trans id - * @param fileName the file name - */ - public void sendRsyncCommand(String transId, String fileName) - { - String aaiServerList = null; - String rsyncOptionsList = null; - - try { - aaiServerList = AAIConfig.get(DEFAULT_CHECK + "serverlist"); - rsyncOptionsList = AAIConfig.get("aai.rsync.options.list"); - String enableRsync = AAIConfig.get("aai.rsync.enabled"); - - if (!AAIConfig.isEmpty(enableRsync) && "n".equalsIgnoreCase(enableRsync)){ - LOGGER.info("rsync not invoked for " + fileName + ": rsync is not enabled in aaiconfig.properties"); - return; - } - } catch ( Exception e ) { - LOGGER.warn( "rsync not invoked: missing aaiconfig.properties entries for rsync" ); - } - - LOGGER.info("rsync to copy files started...."); - - ArrayList<String> remoteHostList = new ArrayList<String>(); - StringTokenizer serverList = new StringTokenizer( aaiServerList, "|" ); - String host = null; - try { - host = getHost(); - String remoteConnString = null; - - remoteHostList = getRemoteHostList(serverList, host); - LOGGER.debug("This host:" + host); - String pickUpDirectory = AAIConfig.get("instar.pickup.dir"); - String user = AAIConfig.get("aai.rsync.remote.user"); - String rsyncCmd = AAIConfig.get("aai.rsync.command"); - - //Push: rsync [OPTION...] SRC... [USER@]HOST:DEST - - java.util.Iterator<String> remoteHostItr = remoteHostList.iterator(); - while (!remoteHostList.isEmpty() && remoteHostItr.hasNext()) { - String remoteHost = remoteHostItr.next(); - remoteConnString =user+"@"+remoteHost+":"+pickUpDirectory; - - List<String> commands = new ArrayList<String>(); - commands.add(rsyncCmd); - StringTokenizer optionTks = new StringTokenizer( rsyncOptionsList, "|" ); - while (optionTks.hasMoreTokens()){ - commands.add(optionTks.nextToken()); - } - commands.add(fileName); // src directory/fileName - commands.add(remoteConnString); // target username/host/path - LOGGER.debug("Commands: " + commands.toString()); - int rsyncResult = doCommand(commands); - if ( rsyncResult == 0 ) { - LOGGER.info("rsync completed for "+remoteHost); - }else { - LOGGER.error("rsync failed for "+ remoteHost+ " with response code "+rsyncResult ); - } - } - } catch ( Exception e) { - ErrorLogHelper.logError("AAI_4000", "no server found processing serverList for host " + host + ": " + e.getMessage()); - } - } - - /** - * Gets the remote host list. - * - * @param serverList the server list - * @param host the host - * @return the remote host list - */ - private ArrayList<String> getRemoteHostList(StringTokenizer serverList, String host) { - ArrayList<String> remoteHostList = new ArrayList<String>(); - String remoteHost = null; - while ( serverList.hasMoreTokens() ) { - remoteHost = serverList.nextToken(); - if (!host.equalsIgnoreCase(remoteHost)){ - remoteHostList.add(remoteHost); - } - } - return remoteHostList; - } - - /** - * Gets the host. - * - * @return the host - * @throws AAIException the AAI exception - */ - private String getHost() throws AAIException { - String aaiServerList = AAIConfig.get(DEFAULT_CHECK + "serverlist"); - String hostname = null; - try { - InetAddress ip = InetAddress.getLocalHost(); - if ( ip != null ) { - hostname = ip.getHostName(); - if ( hostname != null ) { - if ( !( aaiServerList.contains(hostname) ) ) - LOGGER.warn("Host name not found in server list " + hostname); - } else - LOGGER.warn("InetAddress returned null hostname"); - } - - } catch (UnknownHostException e) { - LOGGER.warn("InetAddress getLocalHost exception " + e.getMessage()); - } - - return hostname; - } - -} diff --git a/aai-core/src/main/java/org/onap/aai/util/AAISystemExitUtil.java b/aai-core/src/main/java/org/onap/aai/util/AAISystemExitUtil.java index c76b5421..1d275784 100644 --- a/aai-core/src/main/java/org/onap/aai/util/AAISystemExitUtil.java +++ b/aai-core/src/main/java/org/onap/aai/util/AAISystemExitUtil.java @@ -24,9 +24,13 @@ import org.onap.aai.dbmap.AAIGraph; public class AAISystemExitUtil { public static void systemExitCloseAAIGraph(int code) { + if ("true".equals(System.getProperty("org.onap.aai.graphadmin.started"))) { + return; + } if (AAIGraph.isInit()) { AAIGraph.getInstance().graphShutdown(); } System.exit(code); + } } diff --git a/aai-core/src/main/java/org/onap/aai/util/AAIApiVersion.java b/aai-core/src/main/java/org/onap/aai/util/AAIUtils.java index b3e4add7..ce53d86e 100644 --- a/aai-core/src/main/java/org/onap/aai/util/AAIApiVersion.java +++ b/aai-core/src/main/java/org/onap/aai/util/AAIUtils.java @@ -19,29 +19,34 @@ */ package org.onap.aai.util; -import org.onap.aai.exceptions.AAIException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Collections; +import java.util.Date; +import java.util.TimeZone; -import java.util.regex.Pattern; +public class AAIUtils { -public class AAIApiVersion { - - private static final Pattern versionPattern = Pattern.compile("(^|\\/)(v\\d+)\\/"); - - private static final Pattern latestVersionPattern = Pattern.compile("(^|\\/)(latest)\\/"); - /** - * Gets the. + * Null check. * - * @return the string - * @throws AAIException the AAI exception + * @param <T> the generic type + * @param iterable the iterable + * @return the iterable */ - public static String get() throws AAIException { - - String apiVersion = null; - if (apiVersion == null || !apiVersion.startsWith("v")) { - apiVersion = AAIConfig.get (AAIConstants.AAI_DEFAULT_API_VERSION_PROP, AAIConstants.AAI_DEFAULT_API_VERSION); - //apiVersion = AAIConstants.AAI_DEFAULT_API_VERSION; - } - return apiVersion; + public static <T> Iterable<T> nullCheck(Iterable<T> iterable) { + return iterable == null ? Collections.<T>emptyList() : iterable; } + + /** + * Gen date. + * + * @return the string + */ + public static String genDate() { + Date date = new Date(); + DateFormat formatter = new SimpleDateFormat("YYMMdd-HH:mm:ss:SSS"); + formatter.setTimeZone(TimeZone.getTimeZone("GMT")); + return formatter.format(date); + } } diff --git a/aai-core/src/main/java/org/onap/aai/util/AutoGenerateHtml.java b/aai-core/src/main/java/org/onap/aai/util/AutoGenerateHtml.java index 3f64c0f5..c54f85d1 100644 --- a/aai-core/src/main/java/org/onap/aai/util/AutoGenerateHtml.java +++ b/aai-core/src/main/java/org/onap/aai/util/AutoGenerateHtml.java @@ -21,15 +21,16 @@ package org.onap.aai.util; import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.ListIterator; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; import org.onap.aai.util.swagger.GenerateSwagger; import freemarker.template.TemplateException; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class AutoGenerateHtml { @@ -41,10 +42,19 @@ public class AutoGenerateHtml { public static void main(String[] args) throws IOException, TemplateException { String savedProperty = System.getProperty("aai.generate.version"); - List<Version> versionsToGen = Arrays.asList(Version.values()); + + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( + "org.onap.aai.config", + "org.onap.aai.setup" + ); + + + SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class); + + List<SchemaVersion> versionsToGen = schemaVersions.getVersions(); Collections.sort(versionsToGen); Collections.reverse(versionsToGen); - ListIterator<Version> versionIterator = versionsToGen.listIterator(); + ListIterator<SchemaVersion> versionIterator = versionsToGen.listIterator(); String schemaDir; if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) { schemaDir = ALT_SCHEMA_DIR; @@ -52,11 +62,13 @@ public class AutoGenerateHtml { else { schemaDir = DEFAULT_SCHEMA_DIR; } + String release = System.getProperty("aai.release", "onap"); while (versionIterator.hasNext()) { - System.setProperty("aai.generate.version", versionIterator.next().toString()); - String yamlFile = schemaDir + "/src/main/resources/aai_swagger_yaml/aai_swagger_" + System.getProperty("aai.generate.version")+ ".yaml"; + System.setProperty("aai.generate.version", versionIterator.next().toString()); + String yamlFile = schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" + System.getProperty("aai.generate.version")+ ".yaml"; File swaggerYamlFile = new File(yamlFile); if(swaggerYamlFile.exists()) { + GenerateSwagger.schemaVersions = schemaVersions; GenerateSwagger.main(args); } } diff --git a/aai-core/src/main/java/org/onap/aai/util/FileWatcher.java b/aai-core/src/main/java/org/onap/aai/util/FileWatcher.java index 16e239a8..cb8074de 100644 --- a/aai-core/src/main/java/org/onap/aai/util/FileWatcher.java +++ b/aai-core/src/main/java/org/onap/aai/util/FileWatcher.java @@ -38,7 +38,7 @@ public abstract class FileWatcher extends TimerTask { /** * runs a timer task - * @see java.util.TimerTask.run + * @see TimerTask.run */ public final void run() { long timeStamp = file.lastModified(); diff --git a/aai-core/src/main/java/org/onap/aai/util/GenerateXsd.java b/aai-core/src/main/java/org/onap/aai/util/GenerateXsd.java index 63318da3..d94457e5 100644 --- a/aai-core/src/main/java/org/onap/aai/util/GenerateXsd.java +++ b/aai-core/src/main/java/org/onap/aai/util/GenerateXsd.java @@ -7,50 +7,70 @@ * 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 - * + * + * 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========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. */ - package org.onap.aai.util; -import org.onap.aai.introspection.Version; -import org.onap.aai.util.genxsd.EdgeRuleSet; + +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; import org.onap.aai.util.genxsd.HTMLfromOXM; +import org.onap.aai.util.genxsd.NodesYAMLfromOXM; + import org.onap.aai.util.genxsd.YAMLfromOXM; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.w3c.dom.*; + -import java.io.BufferedWriter; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class GenerateXsd { - private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class"); + private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class"); protected static String apiVersion = null; + public static AnnotationConfigApplicationContext ctx = null; static String apiVersionFmt = null; static boolean useAnnotationsInXsd = false; static String responsesUrl = null; static String responsesLabel = null; static String jsonEdges = null; - static EdgeRuleSet edgeRuleSet = null; - static Map<String, String> generatedJavaType; static Map<String, String> appliedPaths; + static String RELEASE = System.getProperty("aai.release", "onap"); + + + static NodeList javaTypeNodes; + static Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions(); + private static Map<String, String> createJavaTypeDefinitions() + { + StringBuffer aaiInternal = new StringBuffer(); + Map<String,String> javaTypeDefinitions = new HashMap<String, String>(); + aaiInternal.append(" aai-internal:\n"); + aaiInternal.append(" properties:\n"); + aaiInternal.append(" property-name:\n"); + aaiInternal.append(" type: string\n"); + aaiInternal.append(" property-value:\n"); + aaiInternal.append(" type: string\n"); +// javaTypeDefinitions.put("aai-internal", aaiInternal.toString()); + return javaTypeDefinitions; + } public static final int VALUE_NONE = 0; public static final int VALUE_DESCRIPTION = 1; @@ -60,26 +80,33 @@ public class GenerateXsd { private static final String generateTypeXSD = "xsd"; private static final String generateTypeYAML = "yaml"; - private static final String root = "../aai-schema/src/main/resources"; - private static final String autoGenRoot = "aai-schema/src/main/resources"; - private static final String normalStartDir = "aai-core"; - private static final String xsd_dir = root + "/aai_schema"; - private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/aai_swagger_yaml"; + private final static String nodeDir = System.getProperty("nodes.configuration.location"); + private final static String edgeDir = System.getProperty("edges.configuration.location"); + private static final String baseRoot = "aai-schema/"; + private static final String baseAutoGenRoot = "aai-schema/"; + + private static final String root = baseRoot + "src/main/resources"; + private static final String autoGenRoot = baseAutoGenRoot + "src/main/resources"; + private static final String normalStartDir = "aai-core"; + private static final String xsd_dir = root + "/" + RELEASE +"/aai_schema"; + + private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/" + RELEASE + "/aai_swagger_yaml"; + /* These three strings are for yaml auto-generation from aai-common class*/ -// private static final String alt_yaml_dir = autoGenRoot + "/aai_swagger_yaml"; - private static int annotationsStartVersion = 9; // minimum version to support annotations in xsd - private static int swaggerSupportStartsVersion = 7; // minimum version to support swagger documentation + private static int swaggerSupportStartsVersion = 1; // minimum version to support swagger documentation + private static boolean validVersion(String versionToGen) { if ("ALL".equalsIgnoreCase(versionToGen)) { return true; } - - for (Version v : Version.values()) { - if (v.name().equals(versionToGen)) { + + SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + for (SchemaVersion v : schemaVersions.getVersions()) { + if (v.equals(versionToGen)) { return true; } } @@ -87,13 +114,6 @@ public class GenerateXsd { return false; } - private static boolean versionUsesAnnotations( String version) { - if (new Integer(version.substring(1)).intValue() >= annotationsStartVersion ) { - return true; - } - return false; - } - private static boolean versionSupportsSwagger( String version) { if (new Integer(version.substring(1)).intValue() >= swaggerSupportStartsVersion ) { return true; @@ -112,10 +132,17 @@ public class GenerateXsd { public static String getResponsesUrl() { return responsesUrl; } - public static void main(String[] args) throws IOException { String versionToGen = System.getProperty("gen_version").toLowerCase(); String fileTypeToGen = System.getProperty("gen_type").toLowerCase(); + + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext( + "org.onap.aai.config", + "org.onap.aai.setup" + ); + + SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class); + if ( fileTypeToGen == null ) { fileTypeToGen = generateTypeXSD; } @@ -125,11 +152,10 @@ public class GenerateXsd { System.exit(1); } - String responsesLabel = System.getProperty("yamlresponses_url"); responsesUrl = responsesLabel; - List<Version> versionsToGen = new ArrayList<>(); + List<SchemaVersion> versionsToGen = new ArrayList<>(); if ( versionToGen == null ) { System.err.println("Version is required, ie v<n> or ALL."); System.exit(1); @@ -139,11 +165,11 @@ public class GenerateXsd { System.exit(1); } else if ("ALL".equalsIgnoreCase(versionToGen)) { - versionsToGen = Arrays.asList(Version.values()); + versionsToGen = schemaVersions.getVersions(); Collections.sort(versionsToGen); Collections.reverse(versionsToGen); } else { - versionsToGen.add(Version.getVersion(versionToGen)); + versionsToGen.add(new SchemaVersion(versionToGen)); } //process file type System property @@ -160,40 +186,51 @@ public class GenerateXsd { responsesUrl = "description: "+ "Response codes found in [response codes]("+responsesLabel+ ").\n"; } } + /* + * TODO: Oxm Path is config driveb + */ String oxmPath; if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) { - oxmPath = autoGenRoot + "/oxm/"; + oxmPath = baseAutoGenRoot + nodeDir; } else { - oxmPath = root + "/oxm/"; + oxmPath = baseRoot + nodeDir; } - String outfileName; + String outfileName = null; File outfile; + String nodesfileName = null; + File nodesfile; String fileContent = null; - - for (Version v : versionsToGen) { + String nodesContent = null; + + + for (SchemaVersion v : versionsToGen) { apiVersion = v.toString(); logger.debug("YAMLdir = "+yaml_dir); logger.debug("Generating " + apiVersion + " " + fileTypeToGen); - File oxm_file = new File(oxmPath + "aai_oxm_" + apiVersion + ".xml"); apiVersionFmt = "." + apiVersion + "."; generatedJavaType = new HashMap<String, String>(); appliedPaths = new HashMap<String, String>(); File edgeRuleFile = null; + String fileName = edgeDir + "DbEdgeRules_" + apiVersion + ".json"; logger.debug("user.dir = "+System.getProperty("user.dir")); if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) { - edgeRuleFile = new File(normalStartDir + "/src/main/resources/dbedgerules/DbEdgeRules_" + apiVersion + ".json"); + fileName = baseAutoGenRoot + fileName; + } else { - edgeRuleFile = new File("src/main/resources/dbedgerules/DbEdgeRules_" + apiVersion + ".json"); + fileName = baseRoot + fileName; + } - + edgeRuleFile = new File( fileName); +// Document doc = ni.getSchema(translateVersion(v)); + if ( fileTypeToGen.equals(generateTypeXSD) ) { - useAnnotationsInXsd = versionUsesAnnotations(apiVersion); outfileName = xsd_dir + "/aai_schema_" + apiVersion + "." + generateTypeXSD; try { - HTMLfromOXM swagger = new HTMLfromOXM(oxm_file, v); + HTMLfromOXM swagger = ctx.getBean(HTMLfromOXM.class); + swagger.setVersion(v); fileContent = swagger.process(); } catch(Exception e) { logger.error( "Exception creating output file " + outfileName); @@ -202,12 +239,16 @@ public class GenerateXsd { } } else if ( versionSupportsSwagger(apiVersion )) { outfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + generateTypeYAML; - try { - YAMLfromOXM swagger = new YAMLfromOXM(oxm_file, v, edgeRuleFile); + nodesfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + "nodes"+"."+generateTypeYAML; + try { + YAMLfromOXM swagger = (YAMLfromOXM) ctx.getBean(YAMLfromOXM.class); + swagger.setVersion(v); fileContent = swagger.process(); + NodesYAMLfromOXM nodesSwagger = ctx.getBean(NodesYAMLfromOXM.class); + nodesSwagger.setVersion(v); + nodesContent = nodesSwagger.process(); } catch(Exception e) { logger.error( "Exception creating output file " + outfileName); - logger.error( e.getMessage()); e.printStackTrace(); } } else { @@ -217,7 +258,33 @@ public class GenerateXsd { File parentDir = outfile.getParentFile(); if(! parentDir.exists()) parentDir.mkdirs(); - + if(nodesfileName != null) { + BufferedWriter nodesBW = null; + nodesfile = new File(nodesfileName); + parentDir = nodesfile.getParentFile(); + if(! parentDir.exists()) + parentDir.mkdirs(); + try { + nodesfile.createNewFile(); + } catch (IOException e) { + logger.error( "Exception creating output file " + nodesfileName); + e.printStackTrace(); + } + try { + Charset charset = Charset.forName("UTF-8"); + Path path = Paths.get(nodesfileName); + nodesBW = Files.newBufferedWriter(path, charset); + nodesBW.write(nodesContent); + } catch ( IOException e) { + logger.error( "Exception writing output file " + outfileName); + e.printStackTrace(); + } finally { + if ( nodesBW != null ) { + nodesBW.close(); + } + } + } + try { outfile.createNewFile(); } catch (IOException e) { @@ -242,4 +309,7 @@ public class GenerateXsd { } } -}
\ No newline at end of file + +} + +
\ No newline at end of file diff --git a/aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java b/aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java index 3cfcbcdf..f092eac2 100644 --- a/aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java +++ b/aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java @@ -104,13 +104,13 @@ public class HttpsAuthClient{ char[] pwd = keystore_password.toCharArray(); ks.load(fin, pwd); kmf.init(ks, pwd); - ctx.init(kmf.getKeyManagers(), null, null); } 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 diff --git a/aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java b/aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java index 888b04f2..624a394c 100644 --- a/aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java +++ b/aai-core/src/main/java/org/onap/aai/util/HttpsAuthExternalClient.java @@ -20,7 +20,6 @@ package org.onap.aai.util; import java.io.FileInputStream; -import java.security.KeyManagementException; import java.security.KeyStore; import javax.net.ssl.HostnameVerifier; @@ -30,9 +29,7 @@ import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; -import org.onap.aai.domain.yang.Customers; 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.json.JSONConfiguration; @@ -40,41 +37,7 @@ import com.sun.jersey.client.urlconnection.HTTPSProperties; public class HttpsAuthExternalClient { - /** - * 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..."); - String keystore = args[0]; - String keypasswd = args[1]; - Client client = HttpsAuthExternalClient.getClient(keystore, keypasswd); - - 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. * @@ -119,12 +82,11 @@ public class HttpsAuthExternalClient { String alg = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(alg); - try(FileInputStream tin = new FileInputStream(truststore_path)) { - KeyStore ts = KeyStore.getInstance("PKCS12"); - char[] tpwd = truststore_password.toCharArray(); - ts.load(tin, tpwd); - tmf.init(ts); - } + FileInputStream tin = new FileInputStream(truststore_path); + KeyStore ts = KeyStore.getInstance("PKCS12"); + char[] tpwd = truststore_password.toCharArray(); + ts.load(tin, tpwd); + tmf.init(ts); //ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); // Updating key manager to null, to disable two way SSL diff --git a/aai-core/src/main/java/org/onap/aai/util/JettyObfuscationConversionCommandLineUtil.java b/aai-core/src/main/java/org/onap/aai/util/JettyObfuscationConversionCommandLineUtil.java new file mode 100644 index 00000000..e8bd8af6 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/util/JettyObfuscationConversionCommandLineUtil.java @@ -0,0 +1,96 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.util; + +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.eclipse.jetty.util.security.Password; + +/* + * The purpose of this class is to be a tool for + * manually applying jetty obfuscation/deobfuscation + * so that one can obfuscate the various passwords/secrets + * in aaiIncomingAdapterConfig.properties. + * + * Originally, they were being encrypted by a similar + * command line utility, however the encryption key + * was being hardcoded in the src package + * which is a security violation. + * Since this ultimately just moved the problem of how + * to hide secrets to a different secret in a different file, + * and since that encryption was really just being done to + * obfuscate those values in case someone needed to look at + * properties with others looking at their screen, + * we decided that jetty obfuscation would be adequate + * for that task as well as + * removing the "turtles all the way down" secret-to-hide- + * the-secret-to-hide-the-secret problem. + */ +public class JettyObfuscationConversionCommandLineUtil { + + /** + * The main method. + * + * @param args the arguments + */ + public static void main(String[] args){ + Options options = new Options(); + options.addOption("e", true, "obfuscate the given string"); + options.addOption("d", true, "deobfuscate the given string"); + + CommandLineParser parser = new DefaultParser(); + + try { + CommandLine cmd = parser.parse(options, args); + String toProcess = null; + + if (cmd.hasOption("e")){ + toProcess = cmd.getOptionValue("e"); + String encoded = Password.obfuscate(toProcess); + System.out.println(encoded); + } else if (cmd.hasOption("d")) { + toProcess = cmd.getOptionValue("d"); + String decoded_str = Password.deobfuscate(toProcess); + System.out.println(decoded_str); + } else { + usage(); + } + } catch (ParseException e) { + System.out.println("failed to parse input"); + System.out.println(e.toString()); + usage(); + } catch (Exception e) { + System.out.println("exception:" + e.toString()); + } + } + + /** + * Usage. + */ + private static void usage(){ + System.out.println("usage:");; + System.out.println("-e [string] to obfuscate"); + System.out.println("-d [string] to deobfuscate"); + System.out.println("-h help"); + } +} diff --git a/aai-core/src/main/java/org/onap/aai/util/MapperUtil.java b/aai-core/src/main/java/org/onap/aai/util/MapperUtil.java index 8280ce0d..70534d82 100644 --- a/aai-core/src/main/java/org/onap/aai/util/MapperUtil.java +++ b/aai-core/src/main/java/org/onap/aai/util/MapperUtil.java @@ -27,76 +27,75 @@ import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; public class MapperUtil { - /** - * Read as object of. - * - * @param <T> the generic type - * @param clazz the clazz - * @param value the value - * @return the t - * @throws AAIException the AAI exception - */ - public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException { - com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper(); - try { - return MAPPER.readValue(value, clazz); - } catch (Exception e) { - throw new AAIException("AAI_4007", e); + /** + * Read as object of. + * + * @param <T> the generic type + * @param clazz the clazz + * @param value the value + * @return the t + * @throws AAIException the AAI exception + */ + public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException { + ObjectMapper MAPPER = new ObjectMapper(); + try { + return MAPPER.readValue(value, clazz); + } catch (Exception e) { + throw new AAIException("AAI_4007", e); + } } - } - - /** - * Read with dashes as object of. - * - * @param <T> the generic type - * @param clazz the clazz - * @param value the value - * @return the t - * @throws AAIException the AAI exception - */ - public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException { - com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper(); - try { - MAPPER.registerModule(new JaxbAnnotationModule()); - MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false); - - return MAPPER.readValue(value, clazz); - } catch (Exception e) { - throw new AAIException("AAI_4007", e); - } - } - /** - * Write as JSON string. - * - * @param obj the obj - * @return the string - * @throws AAIException the AAI exception - */ - public static String writeAsJSONString(Object obj) throws AAIException { - com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper(); - try { - String s = MAPPER.writeValueAsString(obj); - return s; - //readValue(value, clazz); - } catch (Exception e) { - throw new AAIException("AAI_4008", e); - } - } - - /** - * Write as JSON string with dashes. - * - * @param obj the obj - * @return the string - * @throws AAIException the AAI exception - */ - public static String writeAsJSONStringWithDashes(Object obj) throws AAIException { - com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper(); - try { - MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); - + /** + * Read with dashes as object of. + * + * @param <T> the generic type + * @param clazz the clazz + * @param value the value + * @return the t + * @throws AAIException the AAI exception + */ + public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException { + ObjectMapper MAPPER = new ObjectMapper(); + try { + MAPPER.registerModule(new JaxbAnnotationModule()); + MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false); + + return MAPPER.readValue(value, clazz); + } catch (Exception e) { + throw new AAIException("AAI_4007", e); + } + } + + /** + * Write as JSON string. + * + * @param obj the obj + * @return the string + * @throws AAIException the AAI exception + */ + public static String writeAsJSONString(Object obj) throws AAIException { + ObjectMapper MAPPER = new ObjectMapper(); + try { + String s = MAPPER.writeValueAsString(obj); + return s; + } catch (Exception e) { + throw new AAIException("AAI_4008", e); + } + } + + /** + * Write as JSON string with dashes. + * + * @param obj the obj + * @return the string + * @throws AAIException the AAI exception + */ + public static String writeAsJSONStringWithDashes(Object obj) throws AAIException { + ObjectMapper MAPPER = new ObjectMapper(); + try { + MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); + MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); MAPPER.configure(SerializationFeature.INDENT_OUTPUT, false); MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, false); @@ -105,10 +104,10 @@ public class MapperUtil { MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false); MAPPER.registerModule(new JaxbAnnotationModule()); - String s = MAPPER.writeValueAsString(obj); - return s; - } catch (Exception e) { - throw new AAIException("AAI_4008", e); - } - } + String s = MAPPER.writeValueAsString(obj); + return s; + } catch (Exception e) { + throw new AAIException("AAI_4008", e); + } + } } diff --git a/aai-core/src/main/java/org/onap/aai/util/PojoUtils.java b/aai-core/src/main/java/org/onap/aai/util/PojoUtils.java index 8f0469de..8354d841 100644 --- a/aai-core/src/main/java/org/onap/aai/util/PojoUtils.java +++ b/aai-core/src/main/java/org/onap/aai/util/PojoUtils.java @@ -19,10 +19,6 @@ */ package org.onap.aai.util; -import java.io.IOException; - -import org.apache.commons.io.output.ByteArrayOutputStream; - import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -30,8 +26,63 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; +import com.google.common.base.CaseFormat; +import com.google.common.collect.Multimap; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.eclipse.persistence.dynamic.DynamicEntity; +import org.eclipse.persistence.jaxb.JAXBMarshaller; +import org.eclipse.persistence.jaxb.MarshallerProperties; +import org.onap.aai.domain.model.AAIResource; +import org.onap.aai.exceptions.AAIException; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import java.io.IOException; +import java.io.StringWriter; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.security.SecureRandom; +import java.util.*; public class PojoUtils { + + /** + * Gets the key value list. + * + * @param <T> the generic type + * @param e the e + * @param clazz the clazz + * @return the key value list + * @throws IllegalAccessException the illegal access exception + * @throws IllegalArgumentException the illegal argument exception + * @throws InvocationTargetException the invocation target exception + */ + public <T> List<KeyValueList> getKeyValueList(Entity e, T clazz) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException { + List<KeyValueList> kvList = e.getKeyValueList(); + Object value = null; + Method[] methods = clazz.getClass().getDeclaredMethods(); + String propertyName = ""; + + for (Method method : methods) { + if (method.getName().startsWith("get")) { + propertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(3)); + if (!(method.getReturnType().getName().contains("aai")) || method.getReturnType().getName().contains("java.util.List")) { + value = method.invoke(clazz); + KeyValueList kv = new KeyValueList(); + kv.setKey(propertyName); + if (value != null) { + kv.setValue(value.toString()); + } else { + kv.setValue(""); + } + kvList.add(kv); + } + } + } + return kvList; + } + /** * Gets the json from object. * @@ -45,7 +96,7 @@ public class PojoUtils { public <T> String getJsonFromObject(T clazz) throws JsonGenerationException, JsonMappingException, IOException { return getJsonFromObject(clazz, false, true); } - + /** * Gets the json from object. * @@ -61,21 +112,237 @@ public class PojoUtils { public <T> String getJsonFromObject(T clazz, boolean wrapRoot, boolean indent) throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - - mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); - mapper.configure(SerializationFeature.INDENT_OUTPUT, indent); - mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, wrapRoot); - - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, wrapRoot); - - mapper.registerModule(new JaxbAnnotationModule()); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - mapper.writeValue(baos, clazz); - - return baos.toString(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + + mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + mapper.configure(SerializationFeature.INDENT_OUTPUT, indent); + mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, wrapRoot); + + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, wrapRoot); + + mapper.registerModule(new JaxbAnnotationModule()); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + mapper.writeValue(baos, clazz); + + return baos.toString(); + } + + /** + * Gets the json from dynamic object. + * + * @param ent the ent + * @param jaxbContext the jaxb context + * @param includeRoot the include root + * @return the json from dynamic object + * @throws JsonGenerationException the json generation exception + * @throws JsonMappingException the json mapping exception + * @throws IOException Signals that an I/O exception has occurred. + * @throws JAXBException the JAXB exception + */ + public String getJsonFromDynamicObject(DynamicEntity ent, org.eclipse.persistence.jaxb.JAXBContext jaxbContext, boolean includeRoot) throws JsonGenerationException, JsonMappingException, IOException, JAXBException { + JAXBMarshaller marshaller = jaxbContext.createMarshaller(); + + marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, false); + marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, Boolean.FALSE) ; + marshaller.setProperty("eclipselink.json.include-root", includeRoot); + marshaller.setProperty("eclipselink.media-type", "application/json"); + StringWriter writer = new StringWriter(); + marshaller.marshal(ent, writer); + + return writer.toString(); + } + + /** + * Gets the xml from object. + * + * @param <T> the generic type + * @param clazz the clazz + * @return the xml from object + * @throws JAXBException the JAXB exception + */ + public <T> String getXmlFromObject(T clazz) throws JAXBException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + JAXBContext jc = JAXBContext.newInstance(clazz.getClass().getPackage().getName()); + + Marshaller marshaller = jc.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.marshal(clazz, baos); + + return baos.toString(); + } + + /** + * Gets the lookup key. + * + * @param baseKey the base key + * @param lookupHash the lookup hash + * @param keyProps the key props + * @return the lookup key + */ + public String getLookupKey (String baseKey, HashMap<String,Object> lookupHash, Collection<String> keyProps) { + int baseKeyLen = baseKey.length(); + StringBuffer newKey = new StringBuffer(); + if (baseKeyLen > 0) { + newKey.append(baseKey); + } + + Iterator <String> keyPropI = keyProps.iterator(); + while( keyPropI.hasNext() ){ + String keyProp = keyPropI.next(); + if (baseKeyLen > 0) { + newKey.append("&"); + } + newKey.append(keyProp + "=" + lookupHash.get(keyProp)); + } + return newKey.toString(); + } + + /** + * Gets the lookup keys. + * + * @param lookupHashes the lookup hashes + * @param _dbRulesNodeKeyProps the db rules node key props + * @return the lookup keys + */ + public String getLookupKeys (LinkedHashMap<String,HashMap<String,Object>> lookupHashes, Multimap<String, String> _dbRulesNodeKeyProps) { + Iterator<String> it = lookupHashes.keySet().iterator(); + String lookupKeys = ""; + while (it.hasNext()) { + String objectType = (String)it.next(); + HashMap<String,Object> lookupHash = lookupHashes.get(objectType); + + Collection<String> keyProps = _dbRulesNodeKeyProps.get(objectType); + Iterator <String> keyPropI = keyProps.iterator(); + while( keyPropI.hasNext() ){ + lookupKeys += lookupHash.get(keyPropI.next()); + } + } + return lookupKeys; + } + + /** + * Gets the example object. + * + * @param <T> the generic type + * @param clazz the clazz + * @param singleton the singleton + * @return the example object + * @throws IllegalAccessException the illegal access exception + * @throws IllegalArgumentException the illegal argument exception + * @throws InvocationTargetException the invocation target exception + * @throws NoSuchMethodException the no such method exception + * @throws SecurityException the security exception + * @throws AAIException the AAI exception + */ + public <T> void getExampleObject(T clazz, boolean singleton) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, AAIException { + Method[] methods = clazz.getClass().getDeclaredMethods(); + String dnHypPropertyName = ""; + String upCamPropertyName = ""; + Random rand = new SecureRandom(); + int randInt = rand.nextInt(10000000); + + for (Method method : methods) { + boolean go = false; + if (method.getName().startsWith("get")) { + dnHypPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(3)); + upCamPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL,method.getName().substring(3)); + go = true; + } else if (method.getName().startsWith("is")) { + dnHypPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN,method.getName().substring(2)); + upCamPropertyName = CaseFormat.UPPER_CAMEL.to(CaseFormat.UPPER_CAMEL,method.getName().substring(2)); + go = true; + } + // don't return resource-version on a singleton + if (singleton && dnHypPropertyName.equals("resource-version")) { + go = false; + } + if (go) { + String retType = method.getReturnType().getName(); + if (!retType.contains("aai") && !retType.contains("java.util.List")) { + // get the setter + Method meth = clazz.getClass().getMethod("set" + upCamPropertyName, method.getReturnType()); + + if (retType.contains("String")) { + String val = "example-" + dnHypPropertyName + "-val-" + randInt; + if (val != null) { + meth.invoke(clazz, val); + } + } else if (retType.toLowerCase().contains("long")) { + Integer foo = rand.nextInt(100000); + meth.invoke(clazz, foo.longValue()); + } else if (retType.toLowerCase().contains("int")) { + meth.invoke(clazz, rand.nextInt(100000)); + } else if (retType.toLowerCase().contains("short")) { + Integer randShort = rand.nextInt(10000); + meth.invoke(clazz, randShort.shortValue()); + } else if (retType.toLowerCase().contains("boolean")) { + meth.invoke(clazz, true); + } + // i think max has a list in license-management + } + } + } + } + + /** + * Gets the dynamic example object. + * + * @param childObject the child object + * @param aaiRes the aai res + * @param singleton the singleton + * @return the dynamic example object + */ + public void getDynamicExampleObject(DynamicEntity childObject, AAIResource aaiRes, boolean singleton) { + // TODO Auto-generated method stub + + Random rand = new SecureRandom(); + Integer randInt = rand.nextInt(100000); + long range = 100000000L; + long randLong = (long)(rand.nextDouble()*range); + Integer randShrt = rand.nextInt(20000); + short randShort = randShrt.shortValue(); + + for (String dnHypAttrName : aaiRes.getStringFields()) { + + if (singleton && ("resource-version").equals(dnHypAttrName)) { + continue; + } + + String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName); + childObject.set(dnCamAttrName, "example-" + dnHypAttrName + "-val-" + randInt); + + } + + for (String dnHypAttrName : aaiRes.getStringListFields()) { + ArrayList<String> exampleList = new ArrayList<String>(); + exampleList.add("example-" + dnHypAttrName + "-val-" + randInt + "-" + 1); + exampleList.add("example-" + dnHypAttrName + "-val-" + randInt + "-" + 2); + String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName); + childObject.set(dnCamAttrName, exampleList); + } + + // the attrName might need to be converted to camel case!!! + for (String dnHypAttrName : aaiRes.getLongFields()) { + String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName); + childObject.set(dnCamAttrName, randLong); + } + + for (String dnHypAttrName : aaiRes.getIntFields()) { + String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName); + childObject.set(dnCamAttrName, randInt); + } + + for (String dnHypAttrName : aaiRes.getShortFields()) { + String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName); + childObject.set(dnCamAttrName, randShort); + } + + for (String dnHypAttrName : aaiRes.getBooleanFields()) { + String dnCamAttrName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL,dnHypAttrName); + childObject.set(dnCamAttrName, Boolean.TRUE); + } } } diff --git a/aai-core/src/main/java/org/onap/aai/util/Request.java b/aai-core/src/main/java/org/onap/aai/util/Request.java index 868f0588..fb695b2f 100644 --- a/aai-core/src/main/java/org/onap/aai/util/Request.java +++ b/aai-core/src/main/java/org/onap/aai/util/Request.java @@ -31,7 +31,7 @@ import org.onap.aai.exceptions.AAIException; public class Request<T> { - public static final String V12 = "v12"; + public static final String V14 = "v14"; public final String fromAppId; public final String transactionId; public final String path; @@ -68,7 +68,7 @@ public class Request<T> { private String path; private RestObject<T> restObj; private boolean oldServer; - private String apiVersion = Request.V12; + private String apiVersion = Request.V14; /** diff --git a/aai-core/src/main/java/org/onap/aai/util/RestController.java b/aai-core/src/main/java/org/onap/aai/util/RestController.java index b7be8db1..ff725bb5 100644 --- a/aai-core/src/main/java/org/onap/aai/util/RestController.java +++ b/aai-core/src/main/java/org/onap/aai/util/RestController.java @@ -35,7 +35,7 @@ import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientHandlerException; import com.sun.jersey.api.client.ClientResponse; -public class RestController { +public class RestController implements RestControllerInterface { private static final String TARGET_NAME = "AAI"; private static EELFLogger LOGGER = EELFManager.getInstance().getLogger(RestController.class); @@ -63,30 +63,33 @@ public class RestController { public static final String REST_APIPATH_TENANT = "cloud-infrastructure/tenants/tenant/"; 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_VFMODULE = "nodes/vf-modules/vf-module/"; 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/"; - + + public RestController() throws AAIException { + this.initRestClient(); + } /** * Inits the rest client. * * @throws AAIException the AAI exception */ - private static void initRestClient() throws AAIException + public void initRestClient() throws AAIException { if (client == null) { try { - client = HttpsAuthClient.getClient(); + client = getHttpsAuthClient(); } catch (KeyManagementException e){ throw new AAIException("AAI_7117", "KeyManagementException in REST call to DB: " + e.toString()); @@ -96,6 +99,11 @@ public class RestController { } } + public Client getHttpsAuthClient() throws KeyManagementException { + return HttpsAuthClient.getClient(); + } + + /** * Sets the rest srvr base URL. * @@ -120,8 +128,8 @@ public class RestController { } - public static <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver) throws AAIException { - RestController.<T>Get(t, sourceID, transId, path, restObject, oldserver, AAIConstants.AAI_RESOURCES_PORT); + public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver) throws AAIException { + Get(t, sourceID, transId, path, restObject, oldserver, AAIConstants.AAI_RESOURCES_PORT); } /** * To do - optimization and automation. Also make it as generic as possible. @@ -136,7 +144,7 @@ public class RestController { * @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, int port) throws AAIException { + public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver, int port) throws AAIException { String methodName = "Get"; String url=""; transId += ":" + UUID.randomUUID().toString(); @@ -155,7 +163,65 @@ public class RestController { if (oldserver) url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path; else - url = String.format(AAIConstants.AAI_LOCAL_REST, port) + path; + url = String.format(AAIConstants.AAI_LOCAL_REST, port, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + 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("Real-Time", "true") + .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(); + } + + /** + * 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 <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(); + + LoggingContext.save(); + LoggingContext.partnerName(sourceID); + LoggingContext.targetEntity(TARGET_NAME); + LoggingContext.requestId(transId); + LoggingContext.serviceName(methodName); + LoggingContext.targetServiceName(methodName); + + 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) @@ -193,7 +259,7 @@ public class RestController { * @return the list * @throws Exception the exception */ - private static <T> List<T> mapJsonToObjectList(T typeDef,String json, Class clazz) throws Exception + private <T> List<T> mapJsonToObjectList(T typeDef,String json, Class clazz) throws Exception { List<T> list; ObjectMapper mapper = new ObjectMapper(); @@ -214,7 +280,7 @@ public class RestController { * @param path the path * @throws AAIException the AAI exception */ - public static <T> void Put(T t, String sourceID, String transId, String path) throws AAIException { + public <T> void Put(T t, String sourceID, String transId, String path) throws AAIException { Put( t, sourceID, transId, path, false, AAIConstants.AAI_RESOURCES_PORT); } @@ -228,7 +294,7 @@ public class RestController { * @param path the path * @throws AAIException the AAI exception */ - public static <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver) throws AAIException { + public <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver) throws AAIException { Put( t, sourceID, transId, path, oldserver, AAIConstants.AAI_RESOURCES_PORT); } @@ -243,7 +309,7 @@ public class RestController { * @param oldserver the oldserver * @throws AAIException the AAI exception */ - public static <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver, int port) throws AAIException { + public <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver, int port) throws AAIException { String methodName = "Put"; String url=""; transId += ":" + UUID.randomUUID().toString(); @@ -262,7 +328,7 @@ public class RestController { if (oldserver) url = AAIConfig.get(AAIConstants.AAI_OLDSERVER_URL) + path; else - url = String.format(AAIConstants.AAI_LOCAL_REST, port) + path; + url = String.format(AAIConstants.AAI_LOCAL_REST, port, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path; ClientResponse cres = client.resource(url) .accept("application/json") @@ -285,8 +351,8 @@ public class RestController { } } - public static void Delete(String sourceID, String transId, String path) throws AAIException { - RestController.Delete(sourceID, transId, path, AAIConstants.AAI_RESOURCES_PORT); + public void Delete(String sourceID, String transId, String path) throws AAIException { + Delete(sourceID, transId, path, AAIConstants.AAI_RESOURCES_PORT); } /** * Delete. @@ -296,7 +362,7 @@ public class RestController { * @param path the path * @throws AAIException the AAI exception */ - public static void Delete(String sourceID, String transId, String path, int port) throws AAIException { + public void Delete(String sourceID, String transId, String path, int port) throws AAIException { String methodName = "Delete"; String url=""; transId += ":" + UUID.randomUUID().toString(); @@ -312,7 +378,7 @@ public class RestController { initRestClient(); String request = "{}"; - url = String.format(AAIConstants.AAI_LOCAL_REST, port) + path; + url = String.format(AAIConstants.AAI_LOCAL_REST, port, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path; ClientResponse cres = client.resource(url) .accept("application/json") .header("X-TransactionId", transId) @@ -337,6 +403,9 @@ public class RestController { } } + public <T> String Post(T t, String sourceID, String transId, String path) throws Exception { + return Post(t, sourceID, transId, path, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)); + } /** * Post. * @@ -345,10 +414,11 @@ public class RestController { * @param sourceID the source ID * @param transId the trans id * @param path the path + * @param apiVersion the apiVersion * @return the string * @throws Exception the exception */ - public static <T> String Post(T t, String sourceID, String transId, String path) throws Exception { + public <T> String Post(T t, String sourceID, String transId, String path, String apiVersion) throws Exception { String methodName = "Post"; String url=""; transId += ":" + UUID.randomUUID().toString(); @@ -365,8 +435,7 @@ public class RestController { try { initRestClient(); - - url = AAIConfig.get(AAIConstants.AAI_SERVER_URL) + path; + url = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE) + apiVersion + "/" + path; ClientResponse cres = client.resource(url) .accept("application/json") @@ -407,7 +476,7 @@ public class RestController { * @throws IllegalAccessException the illegal access exception * @throws InstantiationException the instantiation exception */ - public static <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException + public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException { return clazz.newInstance(); } @@ -440,7 +509,7 @@ public class RestController { * 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) { + public <T> T DoesResourceExist(String resourcePath, String resourceClassName, String fromAppId, String transId) { try { @@ -448,7 +517,7 @@ public class RestController { @SuppressWarnings("unchecked") T resourceObj = (T)getInstance(Class.forName(resourceClassName)); restObj.set(resourceObj); - RestController.<T>Get(resourceObj, fromAppId, transId, resourcePath, restObj, false, AAIConstants.AAI_RESOURCES_PORT); + Get(resourceObj, fromAppId, transId, resourcePath, restObj, false, AAIConstants.AAI_RESOURCES_PORT); resourceObj = restObj.get(); if (resourceObj != null) @@ -465,4 +534,73 @@ public class RestController { return null; } + /** + * Patch. + * + * @param <T> the generic type + * @param sourceID the source ID + * @param transId the trans id + * @param path the path + * @throws AAIException the AAI exception + */ + public <T> void Patch(T t, String sourceID, String transId, String path) throws AAIException { + String methodName = "Patch"; + String url=""; + transId += ":" + UUID.randomUUID().toString(); + + LoggingContext.save(); + LoggingContext.partnerName(sourceID); + LoggingContext.targetEntity(TARGET_NAME); + LoggingContext.requestId(transId); + LoggingContext.serviceName(methodName); + LoggingContext.targetServiceName(methodName); + + + int numRetries = 5; + ClientResponse cres = null; + int statusCode = -1; + + try { + url = String.format(AAIConstants.AAI_LOCAL_REST, AAIConstants.AAI_RESOURCES_PORT, AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP)) + path; + + initRestClient(); + do { + + cres = client.resource(url) + .accept("application/json") + .header("X-TransactionId", transId) + .header("X-FromAppId", sourceID) + .header("X-HTTP-Method-Override", "PATCH") + .type("application/merge-patch+json") + .entity(t) + .post(ClientResponse.class); + + statusCode = cres.getStatus(); + + if ( statusCode >= 200 && statusCode <= 299 ) { + LOGGER.debug(methodName + "REST api PATCH was successful!"); + return; + } else { + LOGGER.debug(methodName + "Unable to make the patch request to url " + url + " so retrying"); + } + + numRetries--; + + } while(numRetries >= 0); + + LOGGER.debug(methodName + "Unable to make the patch request to url " + url + " even after trying = " + numRetries + " times."); + 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()); + + } + finally { + LoggingContext.restore(); + } + + } } diff --git a/aai-core/src/main/java/org/onap/aai/util/RestControllerInterface.java b/aai-core/src/main/java/org/onap/aai/util/RestControllerInterface.java new file mode 100644 index 00000000..d7061188 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/util/RestControllerInterface.java @@ -0,0 +1,43 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.util; + +import org.onap.aai.util.RestObject; + +import org.onap.aai.exceptions.AAIException; + +public interface RestControllerInterface { + + public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, boolean oldserver) throws AAIException; + + public <T> void Get(T t, String sourceID, String transId, String path, RestObject<T> restObject, String apiVersion) throws AAIException; + + public <T> void Patch(T t, String sourceID, String transId, String path) throws AAIException; + + public <T> void Put(T t, String sourceID, String transId, String path) throws AAIException; + + public <T> void Put(T t, String sourceID, String transId, String path, boolean oldserver) throws AAIException; + + public void Delete(String sourceID, String transId, String path) throws AAIException; + + public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException; + + public <T> T DoesResourceExist(String resourcePath, String resourceClassName, String fromAppId, String transId) ; +}
\ No newline at end of file diff --git a/aai-core/src/main/java/org/onap/aai/util/StoreNotificationEvent.java b/aai-core/src/main/java/org/onap/aai/util/StoreNotificationEvent.java index 6bde4823..1f3341b8 100644 --- a/aai-core/src/main/java/org/onap/aai/util/StoreNotificationEvent.java +++ b/aai-core/src/main/java/org/onap/aai/util/StoreNotificationEvent.java @@ -19,6 +19,12 @@ */ package org.onap.aai.util; +import java.io.StringWriter; +import java.util.Iterator; +import java.util.UUID; + +import javax.xml.bind.Marshaller; + import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import org.eclipse.persistence.dynamic.DynamicEntity; @@ -35,14 +41,10 @@ import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; -import javax.xml.bind.Marshaller; -import java.io.StringWriter; -import java.util.Iterator; -import java.util.UUID; - public class StoreNotificationEvent { private static final EELFLogger logger = EELFManager.getInstance().getLogger(StoreNotificationEvent.class); + private MessageProducer messageProducer; private String fromAppId = ""; private String transId = ""; @@ -56,7 +58,7 @@ public class StoreNotificationEvent { * Instantiates a new store notification event. */ public StoreNotificationEvent(String transactionId, String sourceOfTruth) { - this.messageProducer = new AAIDmaapEventJMSProducer(); + this.messageProducer = new AAIDmaapEventJMSProducer(); this.transactionId = transactionId; this.sourceOfTruth = sourceOfTruth; } @@ -220,7 +222,6 @@ public class StoreNotificationEvent { marshaller.setProperty(org.eclipse.persistence.jaxb.MarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, false); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); marshaller.marshal(notificationEvent, result); - this.sendToDmaapJmsQueue(result.toString()); } catch (Exception e) { @@ -277,7 +278,7 @@ public class StoreNotificationEvent { } if (notificationEvent.getValue("cambria-partition") == null) { - notificationEvent.setValue("cambria-partition", AAIConstants.UEB_PUB_PARTITION_AAI); + notificationEvent.setValue("cambria-partition", AAIConfig.get("aai.notificationEvent.default.partition", AAIConstants.UEB_PUB_PARTITION_AAI)); } notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject()); @@ -294,6 +295,7 @@ public class StoreNotificationEvent { } private void sendToDmaapJmsQueue(String entityString) throws JSONException { + JSONObject entityJsonObject = new JSONObject(entityString); diff --git a/aai-core/src/main/java/org/onap/aai/util/UniquePropertyCheck.java b/aai-core/src/main/java/org/onap/aai/util/UniquePropertyCheck.java deleted file mode 100644 index bfa0f3a1..00000000 --- a/aai-core/src/main/java/org/onap/aai/util/UniquePropertyCheck.java +++ /dev/null @@ -1,290 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.util; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Properties; -import java.util.UUID; - -import org.apache.tinkerpop.gremlin.structure.Direction; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.apache.tinkerpop.gremlin.structure.Graph; -import org.apache.tinkerpop.gremlin.structure.Vertex; -import org.apache.tinkerpop.gremlin.structure.VertexProperty; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.logging.LoggingContext; -import org.onap.aai.logging.LoggingContext.StatusCode; -import org.slf4j.MDC; - -import com.att.eelf.configuration.Configuration; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.janusgraph.core.JanusGraphFactory; -import org.janusgraph.core.JanusGraph; -import org.onap.aai.dbmap.AAIGraphConfig; - -public class UniquePropertyCheck { - - - private static final String FROMAPPID = "AAI-UTILS"; - private static final String TRANSID = UUID.randomUUID().toString(); - private static final String COMPONENT = "UniquePropertyCheck"; - - /** - * The main method. - * - * @param args the arguments - */ - public static void main(String[] args) { - - - Properties props = System.getProperties(); - props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, "uniquePropertyCheck-logback.xml"); - props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_ETC_APP_PROPERTIES); - EELFLogger logger = EELFManager.getInstance().getLogger(UniquePropertyCheck.class.getSimpleName()); - - LoggingContext.init(); - LoggingContext.partnerName(FROMAPPID); - LoggingContext.serviceName(AAIConstants.AAI_RESOURCES_MS); - LoggingContext.component(COMPONENT); - LoggingContext.targetEntity(AAIConstants.AAI_RESOURCES_MS); - LoggingContext.targetServiceName("main"); - LoggingContext.requestId(TRANSID); - LoggingContext.statusCode(StatusCode.COMPLETE); - LoggingContext.responseCode(LoggingContext.SUCCESS); - - MDC.put("logFilenameAppender", UniquePropertyCheck.class.getSimpleName()); - - if( args == null || args.length != 1 ){ - String msg = "usage: UniquePropertyCheck propertyName \n"; - System.out.println(msg); - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.BUSINESS_PROCESS_ERROR); - logAndPrint(logger, msg ); - System.exit(1); - } - String propertyName = args[0]; - Graph graph = null; - - try { - AAIConfig.init(); - System.out.println(" ---- NOTE --- about to open graph (takes a little while)--------\n"); - JanusGraph tGraph = JanusGraphFactory.open(new AAIGraphConfig.Builder(AAIConstants.REALTIME_DB_CONFIG).forService(UniquePropertyCheck.class.getSimpleName()).withGraphType("realtime").buildConfiguration()); - - if( tGraph == null ) { - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR); - logAndPrint(logger, " Error: Could not get JanusGraph "); - System.exit(1); - } - - graph = tGraph.newTransaction(); - if( graph == null ){ - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.AVAILABILITY_TIMEOUT_ERROR); - logAndPrint(logger, "could not get graph object in UniquePropertyCheck() \n"); - System.exit(0); - } - } - catch (AAIException e1) { - String msg = "Threw Exception: [" + e1.toString() + "]"; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.UNKNOWN_ERROR); - logAndPrint(logger, msg); - System.exit(0); - } - catch (Exception e2) { - String msg = "Threw Exception: [" + e2.toString() + "]"; - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.UNKNOWN_ERROR); - logAndPrint(logger, msg); - System.exit(0); - } - - runTheCheckForUniqueness( TRANSID, FROMAPPID, graph, propertyName, logger ); - System.exit(0); - - }// End main() - - - /** - * Run the check for uniqueness. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param graph the graph - * @param propertyName the property name - * @param logger the logger - * @return the boolean - */ - public static Boolean runTheCheckForUniqueness( String transId, String fromAppId, Graph graph, - String propertyName, EELFLogger logger ){ - - // Note - property can be found in more than one nodetype - // our uniqueness constraints are always across the entire db - so this - // tool looks across all nodeTypes that the property is found in. - Boolean foundDupesFlag = false; - - HashMap <String,String> valuesAndVidHash = new HashMap <String, String> (); - HashMap <String,String> dupeHash = new HashMap <String, String> (); - - int propCount = 0; - int dupeCount = 0; - Iterator<Vertex> vertItor = graph.traversal().V().has(propertyName); - while( vertItor.hasNext() ){ - propCount++; - Vertex v = vertItor.next(); - String thisVid = v.id().toString(); - Object val = (v.<Object>property(propertyName)).orElse(null); - if( valuesAndVidHash.containsKey(val) ){ - // We've seen this one before- track it in our dupe hash - dupeCount++; - if( dupeHash.containsKey(val) ){ - // This is not the first one being added to the dupe hash for this value - String updatedDupeList = dupeHash.get(val) + "|" + thisVid; - dupeHash.put(val.toString(), updatedDupeList); - } - else { - // This is the first time we see this value repeating - String firstTwoVids = valuesAndVidHash.get(val) + "|" + thisVid; - dupeHash.put(val.toString(), firstTwoVids); - } - } - else { - valuesAndVidHash.put(val.toString(), thisVid); - } - } - - - String info = "\n Found this property [" + propertyName + "] " + propCount + " times in our db."; - logAndPrint(logger, info); - info = " Found " + dupeCount + " cases of duplicate values for this property.\n\n"; - logAndPrint(logger, info); - - try { - if( ! dupeHash.isEmpty() ){ - Iterator <?> dupeItr = dupeHash.entrySet().iterator(); - while( dupeItr.hasNext() ){ - Map.Entry pair = (Map.Entry) dupeItr.next(); - String dupeValue = pair.getKey().toString();; - String vidsStr = pair.getValue().toString(); - String[] vidArr = vidsStr.split("\\|"); - logAndPrint(logger, "\n\n -------------- Found " + vidArr.length - + " nodes with " + propertyName + " of this value: [" + dupeValue + "]. Node details: "); - - for( int i = 0; i < vidArr.length; i++ ){ - String vidString = vidArr[i]; - Long idLong = Long.valueOf(vidString); - Vertex tvx = graph.traversal().V(idLong).next(); - showPropertiesAndEdges( TRANSID, FROMAPPID, tvx, logger ); - } - } - } - } - catch( Exception e2 ){ - LoggingContext.statusCode(StatusCode.ERROR); - LoggingContext.responseCode(LoggingContext.DATA_ERROR); - logAndPrint(logger, "Threw Exception: [" + e2.toString() + "]"); - } - finally { - if( graph != null ){ - graph.tx().rollback(); - } - } - - return foundDupesFlag; - - }// end of runTheCheckForUniqueness() - - - /** - * Show properties and edges. - * - * @param transId the trans id - * @param fromAppId the from app id - * @param tVert the t vert - * @param logger the logger - */ - private static void showPropertiesAndEdges( String transId, String fromAppId, Vertex tVert, - EELFLogger logger ){ - - if( tVert == null ){ - logAndPrint(logger, "Null node passed to showPropertiesAndEdges."); - } - else { - String nodeType = ""; - Object ob = tVert.<String>property("aai-node-type").orElse(null); - if( ob == null ){ - nodeType = "null"; - } - else{ - nodeType = ob.toString(); - } - - logAndPrint(logger, " AAINodeType/VtxID for this Node = [" + nodeType + "/" + tVert.id() + "]"); - logAndPrint(logger, " Property Detail: "); - Iterator<VertexProperty<Object>> pI = tVert.properties(); - while( pI.hasNext() ){ - VertexProperty<Object> tp = pI.next(); - Object val = tp.value(); - logAndPrint(logger, "Prop: [" + tp.key() + "], val = [" + val + "] "); - } - - Iterator <Edge> eI = tVert.edges(Direction.BOTH); - if( ! eI.hasNext() ){ - logAndPrint(logger, "No edges were found for this vertex. "); - } - while( eI.hasNext() ){ - Edge ed = eI.next(); - String lab = ed.label(); - Vertex vtx; - if (tVert.equals(ed.inVertex())) { - vtx = ed.outVertex(); - } else { - vtx = ed.inVertex(); - } - if( vtx == null ){ - logAndPrint(logger, " >>> COULD NOT FIND VERTEX on the other side of this edge edgeId = " + ed.id() + " <<< "); - } - else { - String nType = vtx.<String>property("aai-node-type").orElse(null); - String vid = vtx.id().toString(); - logAndPrint(logger, "Found an edge (" + lab + ") from this vertex to a [" + nType + "] node with VtxId = " + vid); - } - } - } - } // End of showPropertiesAndEdges() - - - /** - * Log and print. - * - * @param logger the logger - * @param msg the msg - */ - protected static void logAndPrint(EELFLogger logger, String msg) { - System.out.println(msg); - logger.info(msg); - } - -} - - diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/ConfigTranslatorForDocs.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/ConfigTranslatorForDocs.java new file mode 100644 index 00000000..0ee9cc83 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/ConfigTranslatorForDocs.java @@ -0,0 +1,88 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.util.genxsd; + +import java.io.File; +import java.io.FilenameFilter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import org.onap.aai.setup.ConfigTranslator; +import org.onap.aai.setup.SchemaLocationsBean; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.springframework.context.annotation.Configuration; + +public class ConfigTranslatorForDocs extends ConfigTranslator { + + public ConfigTranslatorForDocs(SchemaLocationsBean bean, SchemaVersions schemaVersions) { + super(bean, schemaVersions); + } + + @Override + public Map<SchemaVersion, List<String>> getNodeFiles() { + List<SchemaVersion> versionsToGen = new ArrayList<>(); + versionsToGen = schemaVersions.getVersions(); + Collections.sort(versionsToGen); + Collections.reverse(versionsToGen); + Map<SchemaVersion, List<String>> mp = new TreeMap<>(); + for (SchemaVersion v : versionsToGen) { + File dir = new File(bean.getNodeDirectory() + File.separator + v.toString().toLowerCase()); + File [] fileSet = dir.listFiles(); + List<String> files = new ArrayList<>(); + for(File f: fileSet ) { + files.add(f.getAbsolutePath()); + } + + mp.put(v, files); + } + return mp; + } + + @Override + public Map<SchemaVersion, List<String>> getEdgeFiles() { + List<SchemaVersion> versionsToGen = new ArrayList<>(); + versionsToGen = schemaVersions.getVersions(); + Collections.sort(versionsToGen); + Collections.reverse(versionsToGen); + Map<SchemaVersion, List<String>> mp = new TreeMap<>(); + for (SchemaVersion v : versionsToGen) { + File dir = new File(bean.getEdgeDirectory()); + File [] fileSet = dir.listFiles(new FilenameFilter() { + @Override + public boolean accept(File dir, String name) { + return name.contains("_"+v.toString().toLowerCase()); + } + }); + List<String> files = new ArrayList<>(); + for(File f: fileSet ) { + files.add(f.getAbsolutePath()); + } + mp.put(v, files); + } + return mp; + } +} diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/DeleteOperation.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/DeleteOperation.java index 0651566e..6ee4194d 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/DeleteOperation.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/DeleteOperation.java @@ -19,12 +19,12 @@ */ package org.onap.aai.util.genxsd; -import org.apache.commons.lang3.StringUtils; -import org.onap.aai.util.GenerateXsd; - import java.util.HashMap; import java.util.StringTokenizer; +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.util.GenerateXsd; + public class DeleteOperation { private String useOpId; private String xmlRootElementName; diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/EdgeDescription.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/EdgeDescription.java index 6e1294ab..ae6b8803 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/EdgeDescription.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/EdgeDescription.java @@ -20,91 +20,82 @@ package org.onap.aai.util.genxsd; import org.apache.commons.lang3.StringUtils; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.edges.enums.DirectionNotation; +import org.onap.aai.edges.enums.EdgeField; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class EdgeDescription { - private static final Logger logger = LoggerFactory.getLogger("EdgeDescription.class"); + private static final Logger logger = LoggerFactory.getLogger("EdgeDescription.class"); + EdgeRule ed; public static enum LineageType { PARENT, CHILD, UNRELATED; } private String ruleKey; - private String to; - private String from; - private LineageType type = LineageType.UNRELATED; - private String direction; - private String multiplicity; - private String preventDelete; - private String deleteOtherV; - private String label; - private String description; +// private String to; +// private String from; + private LineageType lineageType = LineageType.UNRELATED; +// private String direction; +// private String multiplicity; +// private String preventDelete; +// private String deleteOtherV; +// private String label; +// private String description; + + public EdgeDescription(EdgeRule ed) { + super(); + if ( ed.getDirection().toString().equals(ed.getContains()) && + AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) { + this.lineageType=LineageType.PARENT; + } else if ( AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getContains())) && + ed.getDirection().toString().equals(ed.getContains())) { + this.lineageType=LineageType.CHILD; + } else if ( AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getContains())) && + AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getDirection()))) { + this.lineageType=LineageType.PARENT; + } else if ( AAIDirection.getValue("IN").equals(AAIDirection.getValue(ed.getContains())) && + AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) { + this.lineageType=LineageType.PARENT; + } else { + this.lineageType=LineageType.UNRELATED; + } + this.ruleKey = ed.getFrom()+"|"+ed.getTo(); + this.ed=ed; + } /** * @return the deleteOtherV */ public String getDeleteOtherV() { - return deleteOtherV; - } - /** - * @param deleteOtherV the deleteOtherV to set - */ - public void setDeleteOtherV(String deleteOtherV) { - logger.debug("Edge: "+this.getRuleKey()); - logger.debug("Truth: "+(("${direction}".equals(deleteOtherV)) ? "true" : "false")); - logger.debug("Truth: "+(("!${direction}".equals(deleteOtherV)) ? "true" : "false")); - - if("${direction}".equals(deleteOtherV) ) { - this.deleteOtherV = this.direction; - } else if("!${direction}".equals(deleteOtherV) ) { - this.deleteOtherV = this.direction.equals("IN") ? "OUT" : ((this.direction.equals("OUT")) ? "IN" : deleteOtherV); - } else { - this.deleteOtherV = deleteOtherV; - } - logger.debug("DeleteOtherV="+deleteOtherV+"/"+this.direction+"="+this.deleteOtherV); + return ed.getDeleteOtherV(); } /** * @return the preventDelete */ public String getPreventDelete() { - return preventDelete; - } - /** - * @param preventDelete the preventDelete to set - */ - public void setPreventDelete(String preventDelete) { - if(this.getTo().equals("flavor") || this.getFrom().equals("flavor") ){ - logger.debug("Edge: "+this.getRuleKey()); - logger.debug("Truth: "+(("${direction}".equals(preventDelete)) ? "true" : "false")); - logger.debug("Truth: "+(("!${direction}".equals(preventDelete)) ? "true" : "false")); - } - - if("${direction}".equals(preventDelete) ) { - this.preventDelete = this.direction; - } else if("!${direction}".equals(preventDelete) ) { - this.preventDelete = this.direction.equals("IN") ? "OUT" : ((this.direction.equals("OUT")) ? "IN" : preventDelete); - } else { - this.preventDelete = preventDelete; - } + return ed.getPreventDelete(); } public String getAlsoDeleteFootnote(String targetNode) { String returnVal = ""; - if(this.deleteOtherV.equals("IN") && this.to.equals(targetNode) ) { - logger.debug("Edge: "+this.getRuleKey()); + if(ed.getDeleteOtherV().equals("IN") && ed.getTo().equals(targetNode) ) { + logger.debug("Edge: "+this.ruleKey); logger.debug("IF this "+targetNode+" node is deleted, this FROM node is DELETED also"); returnVal = "(1)"; } - if(this.deleteOtherV.equals("OUT") && this.from.equals(targetNode) ) { - logger.debug("Edge: "+this.getRuleKey()); + if(ed.getDeleteOtherV().equals("OUT") && ed.getFrom().equals(targetNode) ) { + logger.debug("Edge: "+this.ruleKey); logger.debug("IF this "+targetNode+" is deleted, this TO node is DELETED also"); returnVal = "(2)"; } - if(this.deleteOtherV.equals("OUT") && this.to.equals(targetNode) ) { - logger.debug("Edge: "+this.getRuleKey()); + if(ed.getDeleteOtherV().equals("OUT") && ed.getTo().equals(targetNode) ) { + logger.debug("Edge: "+this.ruleKey); logger.debug("IF this FROM node is deleted, this "+targetNode+" is DELETED also"); returnVal = "(3)"; } - if(this.deleteOtherV.equals("IN") && this.from.equals(targetNode) ) { - logger.debug("Edge: "+this.getRuleKey()); + if(ed.getDeleteOtherV().equals("IN") && ed.getFrom().equals(targetNode) ) { + logger.debug("Edge: "+this.ruleKey); logger.debug("IF this TO node is deleted, this "+targetNode+" node is DELETED also"); returnVal = "(4)"; } @@ -114,100 +105,67 @@ public class EdgeDescription { * @return the to */ public String getTo() { - return to; - } - /** - * @param to the to to set - */ - public void setTo(String to) { - this.to = to; + return ed.getTo(); } /** * @return the from */ public String getFrom() { - return from; - } - /** - * @param from the from to set - */ - public void setFrom(String from) { - this.from = from; + return ed.getFrom(); } - - public String getRuleKey() { return ruleKey; } public String getMultiplicity() { - return multiplicity; + return ed.getMultiplicityRule().toString(); } - public String getDirection() { - return direction; + public AAIDirection getDirection() { + return AAIDirection.getValue(ed.getDirection()); } public String getDescription() { - return this.description; - } - public void setRuleKey(String val) { - this.ruleKey=val; - } - public void setType(LineageType val) { - this.type=val; + return ed.getDescription(); } - public void setDirection(String val) { - this.direction = val; - } - public void setMultiplicity(String val) { - this.multiplicity=val; - } - - public void setDescription(String val) { - this.description = val; - } - public String getRelationshipDescription(String fromTo, String otherNodeName) { String result = ""; if ("FROM".equals(fromTo)) { - if ("OUT".equals(direction)) { - if (LineageType.PARENT == type) { + if (AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) { + if (LineageType.PARENT == lineageType) { result = " (PARENT of "+otherNodeName; - result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to); + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); } } else { - if (LineageType.CHILD == type) { + if (LineageType.CHILD == lineageType) { result = " (CHILD of "+otherNodeName; - result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to); + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); } - else if (LineageType.PARENT == type) { + else if (LineageType.PARENT == lineageType) { result = " (PARENT of "+otherNodeName; - result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to); + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); } } - if (result.length() == 0) result = String.join(" ", "(", this.from, this.getShortLabel(), this.to+",", this.getMultiplicity()); + if (result.length() == 0) result = String.join(" ", "(", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); } else { //if ("TO".equals(fromTo) - if ("OUT".equals(direction)) { - if (LineageType.PARENT == type) { - result = " (CHILD of "+otherNodeName; - result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to+",", this.getMultiplicity()); + if (AAIDirection.getValue("OUT").equals(AAIDirection.getValue(ed.getDirection()))) { + if (LineageType.PARENT == lineageType) { + result = " (PARENT of "+otherNodeName; + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); } } else { - if (LineageType.PARENT == type) { + if (LineageType.PARENT == lineageType) { result = " (PARENT of "+otherNodeName; - result = String.join(" ", result+",", this.from, this.getShortLabel(), this.to+",", this.getMultiplicity()); + result = String.join(" ", result+",", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); } } - if (result.length() == 0) result = String.join(" ", "(", this.from, this.getShortLabel(), this.to+",", this.getMultiplicity()); + if (result.length() == 0) result = String.join(" ", "(", ed.getFrom(), this.getShortLabel(), ed.getTo()+",", this.getMultiplicity()); } -// Confusing... -// if (hasDelTarget()) result = result + ", will delete target node"; if (result.length() > 0) result = result + ")"; - if (description != null && description.length() > 0) result = result + "\n "+ description; // 6 spaces is important for yaml + if (ed.getDescription() != null && ed.getDescription().length() > 0) result = result + "\n "+ ed.getDescription(); // 6 spaces is important for yaml return result; } @@ -217,26 +175,24 @@ public class EdgeDescription { */ public boolean hasDelTarget() { - return StringUtils.isNotEmpty(this.deleteOtherV) && (! "NONE".equalsIgnoreCase(this.deleteOtherV)); + return StringUtils.isNotEmpty(ed.getDeleteOtherV()) && (! "NONE".equalsIgnoreCase(ed.getDeleteOtherV())); } /** * @return the type */ public LineageType getType() { - return type; + + return lineageType; } /** * @return the label */ public String getLabel() { - return label; + return ed.getLabel(); } public String getShortLabel() { String[] pieces = this.getLabel().split("[.]"); return pieces[pieces.length-1]; } - public void setLabel(String string) { - this.label=string; - } }
\ No newline at end of file diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/EdgeRuleSet.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/EdgeRuleSet.java deleted file mode 100644 index f3ef298a..00000000 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/EdgeRuleSet.java +++ /dev/null @@ -1,196 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.util.genxsd; - -import com.jayway.jsonpath.DocumentContext; -import com.jayway.jsonpath.JsonPath; -import org.onap.aai.serialization.db.EdgeProperty; - -import java.io.*; -import java.util.*; - -public class EdgeRuleSet { - private File edgeFile; - private DocumentContext jsonContext; - - public EdgeRuleSet(File edgeFile) throws IOException,FileNotFoundException { - this.edgeFile = edgeFile; - init(); - } - public EdgeRuleSet(DocumentContext jsonContext) { - this.jsonContext = jsonContext; - } - - public Collection<EdgeDescription> getEdgeRules( String nodeName ) - { - String fromRulesPath = "$['rules'][?(@['from']=='" + nodeName + "')]"; - String toRulesPath = "$['rules'][?(@['to']=='" + nodeName + "')]"; - Collection<EdgeDescription> fromEdges = getEdgeRulesFromJson( fromRulesPath, false ); - Collection<EdgeDescription> edges = getEdgeRulesFromJson( toRulesPath, true ); - edges.addAll(fromEdges); - return edges; - } - - public Collection<EdgeDescription> getEdgeRulesTO( String nodeName ) - { - String toRulesPath = "$['rules'][?(@['to']=='" + nodeName + "')]"; - Collection<EdgeDescription> edges = getEdgeRulesFromJson( toRulesPath, true ); - return edges; - } - - public Collection<EdgeDescription> getEdgeRulesFROM( String nodeName ) - { - String fromRulesPath = "$['rules'][?(@['from']=='" + nodeName + "')]"; - Collection<EdgeDescription> edges = getEdgeRulesFromJson( fromRulesPath, true ); - return edges; - } - - /** - * Guaranteed to at least return non null but empty collection of edge descriptions - * @param nodeName name of the vertex whose edge relationships to return - * @return collection of node neighbors based on DbEdgeRules - **/ - public Collection<EdgeDescription> getEdgeRulesFromJson( String path, boolean skipMatch ) - { - - ArrayList<EdgeDescription> result = new ArrayList<>(); - Iterator<Map<String, Object>> edgeRulesIterator; - try { - List<Map<String, Object>> inEdges = jsonContext.read(path); - - edgeRulesIterator = inEdges.iterator(); - Map<String, Object> edgeMap; - String fromNode; - String toNode; - String direction; - String multiplicity; - String isParent; - String deleteOtherV; - String preventDelete; - String description; - EdgeDescription edgeDes; - - while( edgeRulesIterator.hasNext() ){ - edgeMap = edgeRulesIterator.next(); - fromNode = (String)edgeMap.get("from"); - toNode = (String)edgeMap.get("to"); - if ( skipMatch ) { - if ( fromNode.equals(toNode)) { - continue; - } - } - edgeDes = new EdgeDescription(); - edgeDes.setRuleKey(fromNode + "|" + toNode); - edgeDes.setLabel((String)edgeMap.get("label")); - edgeDes.setTo((String)edgeMap.get("to")); - edgeDes.setFrom((String)edgeMap.get("from")); - direction = (String)edgeMap.get("direction"); - edgeDes.setDirection(direction); - multiplicity = (String)edgeMap.get("multiplicity"); - edgeDes.setMultiplicity(multiplicity); - isParent = (String)edgeMap.get(EdgeProperty.CONTAINS.toString()); - if ( "${direction}".equals(isParent)) { - edgeDes.setType(EdgeDescription.LineageType.PARENT); - } else { - edgeDes.setType(EdgeDescription.LineageType.UNRELATED); - } - deleteOtherV = (String)edgeMap.get(EdgeProperty.DELETE_OTHER_V.toString()); - edgeDes.setDeleteOtherV(deleteOtherV); - preventDelete = (String)edgeMap.get(EdgeProperty.PREVENT_DELETE.toString()); - edgeDes.setPreventDelete(preventDelete); - description = (String)edgeMap.get(EdgeProperty.DESCRIPTION.toString()); - edgeDes.setDescription(description); - - result.add(edgeDes); -// logger.debug("Edge: "+edgeDes.getRuleKey()); - } - } catch (Exception ex) { - ex.printStackTrace(); - } - return result; - - } - - private void init() throws FileNotFoundException, IOException { - InputStream is = null; - Scanner scanner = null; - String jsonEdges = null; - try { - is = new FileInputStream(edgeFile); - scanner = new Scanner(is); - jsonEdges = scanner.useDelimiter("\\Z").next(); - } catch (Exception e) { - throw e; - } finally { - scanner.close(); - if (is != null) { - try { - is.close(); - } catch (IOException e) { - throw e; - } - } - } - jsonContext = JsonPath.parse(jsonEdges); - } - - public String preventDeleteRules(String objectName) { - Collection<EdgeDescription> toEdges = getEdgeRulesTO(objectName); - toEdges.addAll(getEdgeRulesFROM(objectName)); -// logger.debug("TO Edges count: "+toEdges.size()+" Object: "+objectName); - String prevent=null; - LinkedHashSet<String> preventDelete = new LinkedHashSet<String>(); - for (EdgeDescription ed : toEdges) { -// logger.debug("{“comment”: From = "+ed.getFrom()+" To: "+ed.getTo()+" Object: "+objectName); -// logger.debug("{“comment”: Direction = "+ed.getDirection()+" PreventDelete: "+ed.getPreventDelete()+" DeleteOtherV: "+ed.getDeleteOtherV()+" Object: "+objectName); - if(ed.getPreventDelete().equals("IN") && ed.getTo().equals(objectName)) { - preventDelete.add(ed.getFrom().toUpperCase()); - } - if(ed.getPreventDelete().equals("OUT") && ed.getFrom().equals(objectName)) { - preventDelete.add(ed.getTo().toUpperCase()); - } - } - if(preventDelete.size() > 0) { - prevent = objectName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); -// logger.debug(prevent); - } - return String.join((prevent == null) ? "" : "\n", prevent == null ? "" : prevent)+((prevent == null) ? "" : "\n"); -// return String.join((prevent == null) ? "" : "\n", prevent == null ? "" : prevent, also == null ? "" : also)+((prevent == null) ? "" : "\n"); - } - - public String fromDeleteRules(String objectName) { - Collection<EdgeDescription> fromEdges = getEdgeRulesFROM(objectName); - LinkedHashSet<String> preventDelete = new LinkedHashSet <String>(); - String prevent=null; - String also=null; - for (EdgeDescription ed : fromEdges) { -// logger.debug("{“comment”: From = "+ed.getFrom()+" To: "+ed.getTo()+" Object: "+objectName); -// logger.debug("{“comment”: Direction = "+ed.getDirection()+" PreventDelete: "+ed.getPreventDelete()+" DeleteOtherV: "+ed.getDeleteOtherV()+" Object: "+objectName); - if(ed.getPreventDelete().equals("OUT") && ed.getFrom().equals(objectName)) { - preventDelete.add(ed.getTo().toUpperCase()); - } - } - if(preventDelete.size() > 0) { - prevent = objectName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); -// logger.debug(prevent); - } - return String.join((prevent == null || also == null) ? "" : "\n", prevent == null ? "" : prevent, also == null ? "" : also)+((prevent == null && also == null) ? "" : "\n"); - } -} diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/GetOperation.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/GetOperation.java index 16a2e682..ad1e2dc7 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/GetOperation.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/GetOperation.java @@ -19,14 +19,14 @@ */ package org.onap.aai.util.genxsd; -import org.apache.commons.lang3.StringUtils; -import org.onap.aai.util.GenerateXsd; - import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; import java.util.Vector; +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.util.GenerateXsd; + public class GetOperation { static Map<String, Vector<String>> containers = new HashMap<String, Vector<String>>(); public static void addContainerProps(String container, Vector<String> containerProps) { diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/HTMLfromOXM.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/HTMLfromOXM.java index 287b3b5c..cba65daa 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/HTMLfromOXM.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/HTMLfromOXM.java @@ -19,47 +19,64 @@ */ package org.onap.aai.util.genxsd; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import javax.xml.parsers.ParserConfigurationException; + + +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Version; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.w3c.dom.Attr; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -import javax.xml.parsers.ParserConfigurationException; -import java.io.File; -import java.io.IOException; - public class HTMLfromOXM extends OxmFileProcessor { + private static final Logger logger = LoggerFactory.getLogger("HTMLfromOXM.class"); + - Version v; - public HTMLfromOXM(File oxmFile, Version v) throws ParserConfigurationException, SAXException, IOException, AAIException { - super(oxmFile, v); - super.init(); + + public HTMLfromOXM(SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei ){ + super(schemaVersions, ni,ei); + } + public void setOxmVersion(File oxmFile, SchemaVersion v) { + super.setOxmVersion(oxmFile, v); this.v = v; } - public HTMLfromOXM(String xml, Version v) throws ParserConfigurationException, SAXException, IOException, AAIException { - super(xml, v); - super.init(); + public void setXmlVersion(String xml, SchemaVersion v) { + super.setXmlVersion(xml, v); + this.v = v; + } + public void setVersion(SchemaVersion v) { + super.setVersion(v); this.v = v; } + @Override public String getDocumentHeader() { StringBuffer sb = new StringBuffer(); logger.trace("processing starts"); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"); String namespace = "org.onap"; - if (v.compareTo(Version.v11) < 0 || v.compareTo(Version.v12) < 0) { + if (v.compareTo(getSchemaVersions().getNamespaceChangeVersion()) < 0 ) { namespace = "org.openecomp"; } - if ( versionUsesAnnotations(v.name()) ) { + if ( versionUsesAnnotations(v.toString()) ) { sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" - + v.name() + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + v.name() + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" + + v.toString() + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + v.toString() + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" + "\n" + "xmlns:jaxb=\"http://java.sun.com/xml/ns/jaxb\"\r\n" + " jaxb:version=\"2.1\"\r\n" + @@ -67,40 +84,53 @@ public class HTMLfromOXM extends OxmFileProcessor { " jaxb:extensionBindingPrefixes=\"annox\">\n\n"); } else { sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" - + v.name() + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + v.name() + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n\n"); + + v.toString() + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + v.toString() + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n\n"); } return sb.toString(); } @Override - public String process() throws AAIException { - StringBuilder sb = new StringBuilder(); - sb.append(getDocumentHeader()); - for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { - XSDElement javaTypeElement = new XSDElement((Element)javaTypeNodes.item(i)); - String javaTypeName = javaTypeElement.name(); - if ( javaTypeName == null ) { - String msg = "Invalid OXM file: <java-type> has no name attribute in " + oxmFile; - logger.error(msg); - throw new AAIException(msg); - } - if ("Nodes".equals(javaTypeName)) { - logger.debug("skipping Nodes entry (temporary feature)"); + public String process() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException { + StringBuilder sb = new StringBuilder(); + try { + init(); + } catch(Exception e) { + logger.error( "Error initializing " + this.getClass()); + throw e; + } + sb.append(getDocumentHeader()); + StringBuilder sbInventory = new StringBuilder(); + for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { + XSDElement javaTypeElement = new XSDElement((Element)javaTypeNodes.item(i)); + String javaTypeName = javaTypeElement.name(); + if ( javaTypeName == null ) { + String msg = "Invalid OXM file: <java-type> has no name attribute in " + oxmFile; + logger.error(msg); + throw new AAIException(msg); + } + if ("Nodes".equals(javaTypeName)) { + logger.debug("skipping Nodes entry (temporary feature)"); + continue; + } + //Skip any type that has already been processed(recursion could be the reason) + logger.debug(getXmlRootElementName(javaTypeName)+" vs "+ javaTypeName+":"+generatedJavaType.containsKey(getXmlRootElementName(javaTypeName))); + if ( generatedJavaType.containsKey(javaTypeName) ) { continue; - } - //Skip any type that has already been processed(recursion could be the reason) - logger.debug(getXmlRootElementName(javaTypeName)+" vs "+ javaTypeName+":"+generatedJavaType.containsKey(getXmlRootElementName(javaTypeName))); - if ( generatedJavaType.containsKey(javaTypeName) ) { - continue; - } + } + if ( !"Inventory".equals(javaTypeName)) { generatedJavaType.put(javaTypeName, null); - sb.append(processJavaTypeElement( javaTypeName, javaTypeElement )); } + sb.append(processJavaTypeElement( javaTypeName, javaTypeElement, sbInventory )); + } + sb.append(sbInventory); + sb.append(" </xs:sequence>\n"); + sb.append(" </xs:complexType>\n"); + sb.append(" </xs:element>\n"); sb.append("</xs:schema>\n"); return sb.toString(); } - public String processJavaTypeElement( String javaTypeName, Element javaType_Element) { + public String processJavaTypeElement( String javaTypeName, Element javaType_Element, StringBuilder sbInventory) { String xmlRootElementName = getXMLRootElementName(javaType_Element); @@ -113,39 +143,54 @@ public class HTMLfromOXM extends OxmFileProcessor { Element parentElement = (Element)parentNodes.item(0); NodeList xmlElementNodes = parentElement.getElementsByTagName("xml-element"); - + // support for multiple inventory elements across oxm files + boolean processingInventory = false; + boolean hasPreviousInventory = false; + if ( "inventory".equals(xmlRootElementName) && sbInventory != null ) { + processingInventory = true; + if ( sbInventory.toString().contains("xs:complexType") ) { + hasPreviousInventory = true; + } + } + StringBuffer sb1 = new StringBuffer(); if ( xmlElementNodes.getLength() > 0 ) { - sb1.append(" <xs:element name=\"" + xmlRootElementName + "\">\n"); - sb1.append(" <xs:complexType>\n"); - XSDElement javaTypeElement = new XSDElement(javaType_Element); - logger.debug("XSDElement name: "+javaTypeElement.name()); - if(versionUsesAnnotations(v.name())) { - sb1.append(javaTypeElement.getHTMLAnnotation("class", " ")); + + if ( !processingInventory || !hasPreviousInventory ) { + sb1.append(" <xs:element name=\"" + xmlRootElementName + "\">\n"); + sb1.append(" <xs:complexType>\n"); + + XSDElement javaTypeElement = new XSDElement(javaType_Element); + logger.debug("XSDElement name: "+javaTypeElement.name()); + if(versionUsesAnnotations(v.toString())) { + sb1.append(javaTypeElement.getHTMLAnnotation("class", " ")); + } + sb1.append(" <xs:sequence>\n"); } - sb1.append(" <xs:sequence>\n"); for ( int i = 0; i < xmlElementNodes.getLength(); ++i ) { XSDElement xmlElementElement = new XSDElement((Element)xmlElementNodes.item(i)); - String elementName = xmlElementElement.getAttribute("name"); +// String elementName = xmlElementElement.getAttribute("name"); String elementType = xmlElementElement.getAttribute("type"); //No simple types; only AAI custom types - String addType = elementType.contains("." + v.name() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; - if ( elementType.contains("." + v.name() + ".") && !generatedJavaType.containsKey(addType) ) { + String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; + if ( elementType.contains("." + v.toString() + ".") && !generatedJavaType.containsKey(addType) ) { generatedJavaType.put(addType, elementType); - sb.append(processJavaTypeElement( addType, getJavaTypeElement(addType) )); + sb.append(processJavaTypeElement( addType, getJavaTypeElement(addType), null )); } if ("Nodes".equals(addType)) { logger.trace("Skipping nodes, temporary testing"); continue; } //assembles the basic <element> - sb1.append(xmlElementElement.getHTMLElement(v, versionUsesAnnotations(v.name()), this)); + sb1.append(xmlElementElement.getHTMLElement(v, versionUsesAnnotations(v.toString()), this)); + } + if ( !processingInventory ) { + sb1.append(" </xs:sequence>\n"); + sb1.append(" </xs:complexType>\n"); + sb1.append(" </xs:element>\n"); } - sb1.append(" </xs:sequence>\n"); - sb1.append(" </xs:complexType>\n"); - sb1.append(" </xs:element>\n"); } if ( xmlElementNodes.getLength() < 1 ) { @@ -157,7 +202,11 @@ public class HTMLfromOXM extends OxmFileProcessor { generatedJavaType.put(javaTypeName, null); return sb.toString(); } - sb.append( sb1 ); + if ( processingInventory && sbInventory != null ) { + sbInventory.append(sb1); + } else { + sb.append( sb1 ); + } return sb.toString(); } @@ -182,7 +231,11 @@ public class HTMLfromOXM extends OxmFileProcessor { } private boolean versionUsesAnnotations( String version) { - if (new Integer(version.substring(1)).intValue() >= HTMLfromOXM.annotationsStartVersion ) { + int ver = new Integer(version.substring(1)).intValue(); + if ( ver >= HTMLfromOXM.annotationsStartVersion ) { + return true; + } + if ( ver <= HTMLfromOXM.annotationsMinVersion ) { return true; } return false; diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/NodeGetOperation.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/NodeGetOperation.java new file mode 100644 index 00000000..7ffdf407 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/NodeGetOperation.java @@ -0,0 +1,168 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.util.genxsd; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.StringTokenizer; +import java.util.Vector; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.util.GenerateXsd; + +public class NodeGetOperation { + static Map<String, Vector<String>> containers = new HashMap<String, Vector<String>>(); + static ArrayList<String> checklist = createChecklist(); + private static ArrayList<String> createChecklist() + { + ArrayList<String> list = new ArrayList<String>(); + return list; + } + public static void addContainerProps(String container, Vector<String> containerProps) { + containers.put(container, containerProps); + } + public static void resetContainers() { + containers = new HashMap<String, Vector<String>>(); + checklist = createChecklist(); + } + private String useOpId; + private String xmlRootElementName; + private String tag; + private String path; + private String CRUDpath; + private String pathParams; + private String queryParams; + + public NodeGetOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams) { + super(); + this.useOpId = useOpId; + this.xmlRootElementName = xmlRootElementName; + this.tag = tag; + this.CRUDpath = path; + this.path = nodePath(); + this.pathParams = pathParams; + StringBuilder p = new StringBuilder(); + + if(containers.get(xmlRootElementName) == null) { + this.queryParams = ""; + } else { + this.queryParams= String.join("", containers.get(xmlRootElementName)); + for(String param : containers.get(xmlRootElementName)) { + p.append(param); + } + this.queryParams = p.toString(); + } + } + String nodePath() { + String path = null; + int loc = CRUDpath.indexOf(xmlRootElementName); + if(loc > 0) { + path = "/nodes/"+CRUDpath.substring(loc); + } + return path; + } + @Override + public String toString() { + StringTokenizer st; + st = new StringTokenizer(CRUDpath, "/"); + //Path has to be longer than one element + /* + if ( st.countTokens() <= 1) { + return ""; + } + */ + //a valid tag is necessary + if ( StringUtils.isEmpty(tag) ) { + return ""; + } + if ( CRUDpath.endsWith("/relationship") ) { + return ""; + } + if ( CRUDpath.contains("/relationship/") ) { // filter paths with relationship-list + return ""; + } + if ( CRUDpath.endsWith("/relationship-list")) { + return ""; + } + if ( CRUDpath.startsWith("/search")) { + return ""; + } + if ( CRUDpath.startsWith("/actions")) { + return ""; + } + if ( CRUDpath.startsWith("/nodes")) { + return ""; + } + if (checklist.contains(xmlRootElementName)) { + return ""; + } + StringBuffer pathSb = new StringBuffer(); + //Drop out the operations with multiple path parameters + if(CRUDpath.lastIndexOf('{') > CRUDpath.indexOf('{') && StringUtils.isNotEmpty(pathParams)) { + return ""; + } + if(path.lastIndexOf('{') > path.indexOf('{') ) { + return ""; + } + //trim leading path elements before the current node type +// int loc = path.indexOf(xmlRootElementName); +// if(loc > 0) { +// path = "/nodes/"+path.substring(loc); +// } + //append generic parameter syntax to all plural queries + if(path.indexOf('{') == -1) { + path += "?parameter=value[¶meter2=value2]"; + } + pathSb.append(" " + path + ":\n" ); + pathSb.append(" get:\n"); + pathSb.append(" tags:\n"); + pathSb.append(" - Operations" + "\n"); + pathSb.append(" summary: returns " + xmlRootElementName + "\n"); + + pathSb.append(" description: returns " + xmlRootElementName + "\n"); + pathSb.append(" operationId: get" + useOpId + "\n"); + pathSb.append(" produces:\n"); + pathSb.append(" - application/json\n"); + pathSb.append(" - application/xml\n"); + + pathSb.append(" responses:\n"); + pathSb.append(" \"200\":\n"); + pathSb.append(" description: successful operation\n"); + pathSb.append(" schema:\n"); + pathSb.append(" $ref: \"#/definitions/" + xmlRootElementName + "\"\n"); + pathSb.append(" \"default\":\n"); + pathSb.append(" " + GenerateXsd.getResponsesUrl()); + if ( StringUtils.isNotEmpty(pathParams) || StringUtils.isNotEmpty(queryParams)) { + pathSb.append("\n parameters:\n"); + } + if ( StringUtils.isNotEmpty(pathParams)) { + pathSb.append(pathParams); + } + if ( StringUtils.isNotEmpty(pathParams) && StringUtils.isNotEmpty(queryParams)) { + pathSb.append("\n"); + } + if ( StringUtils.isNotEmpty(queryParams)) { + pathSb.append(queryParams); + } + checklist.add(xmlRootElementName); + return pathSb.toString(); + } + } diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/NodesYAMLfromOXM.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/NodesYAMLfromOXM.java new file mode 100644 index 00000000..712d82d5 --- /dev/null +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/NodesYAMLfromOXM.java @@ -0,0 +1,533 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-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.aai.util.genxsd; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.StringTokenizer; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.Vector; + +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.nodes.NodeIngestor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +import com.google.common.collect.Multimap; + +public class NodesYAMLfromOXM extends OxmFileProcessor { + private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class"); + private static final String root = "../aai-schema/src/main/resources"; + private static final String autoGenRoot = "aai-schema/src/main/resources"; + private static final String generateTypeYAML = "yaml"; + private static final String normalStartDir = "aai-core"; + private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/aai_swagger_yaml"; + private StringBuilder inventoryDefSb = null; + private Map<String,String> operationDefinitions = new HashMap<String, String>(); + + private String basePath; + + public NodesYAMLfromOXM(String basePath, SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){ + super(schemaVersions, ni,ei); + this.basePath = basePath; + } + public void setOxmVersion(File oxmFile, SchemaVersion v) { + super.setOxmVersion(oxmFile, v); + } + public void setXmlVersion(String xml, SchemaVersion v){ + super.setXmlVersion(xml, v); + } + + public void setVersion(SchemaVersion v) { + super.setVersion(v); + } + + @Override + public String getDocumentHeader() { + StringBuffer sb = new StringBuffer(); + sb.append("swagger: \"2.0\"\ninfo:\n "); + sb.append("description: |"); + if ( versionSupportsSwaggerDiff(v.toString())) { + sb.append("\n\n [Differences versus the previous schema version]("+"apidocs/aai_swagger_" + v.toString() + ".diff)"); + } + sb.append("\n\n Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.\n\n Licensed under the Creative Commons License, Attribution 4.0 Intl. (the "License"); you may not use this documentation except in compliance with the License.\n\n You may obtain a copy of the License at\n\n (https://creativecommons.org/licenses/by/4.0/)\n\n 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.\n\n This document is best viewed with Firefox or Chrome. Nodes can be found by appending /#/definitions/node-type-to-find to the path to this document. Edge definitions can be found with the node definitions.\n version: \"" + v.toString() +"\"\n"); + sb.append(" title: Active and Available Inventory REST API\n"); + sb.append(" license:\n name: Apache 2.0\n url: http://www.apache.org/licenses/LICENSE-2.0.html\n"); + sb.append(" contact:\n name:\n url:\n email:\n"); + sb.append("host:\nbasePath: " + basePath + "/" + v.toString() + "\n"); + sb.append("schemes:\n - https\npaths:\n"); + return sb.toString(); + } + + protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException { + super.init(); + } + + @Override + public String process() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException { + StringBuffer sb = new StringBuffer(); + StringBuffer pathSb = new StringBuffer(); + NodeGetOperation.resetContainers(); + try { + init(); + } catch(Exception e) { + logger.error( "Error initializing " + this.getClass()); + throw e; + } + pathSb.append(getDocumentHeader()); + StringBuffer definitionsSb = new StringBuffer(); + for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { + XSDElement javaTypeElement = new XSDElement((Element)javaTypeNodes.item(i)); + String javaTypeName = javaTypeElement.name(); + + logger.debug("External: "+javaTypeElement.getAttribute("name")+"/"+getXmlRootElementName(javaTypeName)); + if ( javaTypeName == null ) { + String msg = "Invalid OXM file: <java-type> has no name attribute in " + oxmFile; + logger.error(msg); + throw new AAIException(msg); + } + namespaceFilter.add(getXmlRootElementName(javaTypeName)); + //Skip any type that has already been processed(recursion could be the reason) + if ( generatedJavaType.containsKey(getXmlRootElementName(javaTypeName)) ) { + continue; + } + processJavaTypeElementSwagger( javaTypeName, javaTypeElement, pathSb, + definitionsSb, null, null, null, null, null, null); + } + sb.append(pathSb); +// sb.append(getDocumentHeader()); +// sb.append(totalPathSbAccumulator); + sb.append(appendOperations()); + sb.append(appendDefinitions()); + PutRelationPathSet prp = new PutRelationPathSet(v); + prp.generateRelations(ei); + return sb.toString(); + } + + public String appendDefinitions() { + return appendDefinitions(null); + } + + public String appendDefinitions(Set<String> namespaceFilter) { + if ( inventoryDefSb != null ) { + javaTypeDefinitions.put("inventory", inventoryDefSb.toString()); + } + StringBuffer sb = new StringBuffer("definitions:\n"); + Map<String, String> sortedJavaTypeDefinitions = new TreeMap<String, String>(javaTypeDefinitions); + + for (Map.Entry<String, String> entry : sortedJavaTypeDefinitions.entrySet()) { + if(namespaceFilter != null && (! namespaceFilter.contains(entry.getKey()))) { + continue; + } + sb.append(entry.getValue()); + } + return sb.toString(); + } + + private String processJavaTypeElementSwagger( String javaTypeName, Element javaTypeElement, + StringBuffer pathSb, StringBuffer definitionsSb, String path, String tag, String opId, + String getItemName, StringBuffer pathParams, String validEdges) { + + String xmlRootElementName = getXMLRootElementName(javaTypeElement); + StringBuilder definitionsLocalSb = new StringBuilder(256); + + String useTag = null; + String useOpId = null; + logger.debug("tag="+tag); + + if(tag != null && (! validTag(tag))) { + logger.debug("tag="+tag+"; javaTypeName="+javaTypeName); + return null; + } + if ( !javaTypeName.equals("Inventory") ) { + if ( javaTypeName.equals("AaiInternal")) + return null; + if ( opId == null ) + useOpId = javaTypeName; + else + useOpId = opId + javaTypeName; + if ( tag == null ) + useTag = javaTypeName; + } + + path = xmlRootElementName.equals("inventory") ? "" : (path == null) ? "/" + xmlRootElementName : path + "/" + xmlRootElementName; + XSDJavaType javaType = new XSDJavaType(javaTypeElement); + if ( getItemName != null) { + if ( getItemName.equals("array") ) + return javaType.getArrayType(); + else + return javaType.getItemName(); + } + + NodeList parentNodes = javaTypeElement.getElementsByTagName("java-attributes"); + if ( parentNodes.getLength() == 0 ) { + logger.debug( "no java-attributes for java-type " + javaTypeName); + return ""; + } + + String pathDescriptionProperty = javaType.getPathDescriptionProperty(); + String container = javaType.getContainerProperty(); + Vector<String> indexedProps = javaType.getIndexedProps(); + Vector<String> containerProps = new Vector<String>(); + if(container != null) { + logger.debug("javaTypeName " + javaTypeName + " container:" + container +" indexedProps:"+indexedProps); + } + + Element parentElement = (Element)parentNodes.item(0); + NodeList xmlElementNodes = parentElement.getElementsByTagName("xml-element"); + + StringBuffer sbParameters = new StringBuffer(); + StringBuffer sbRequired = new StringBuffer(); + int requiredCnt = 0; + int propertyCnt = 0; + StringBuffer sbProperties = new StringBuffer(); + + if ( appliedPaths.containsKey(path)) + return null; + + StringTokenizer st = new StringTokenizer(path, "/"); + logger.debug("path: " + path + " st? " + st.toString()); + if ( st.countTokens() > 1 && getItemName == null ) { + logger.debug("appliedPaths: " + appliedPaths + " containsKey? " + appliedPaths.containsKey(path)); + appliedPaths.put(path, xmlRootElementName); + } + Vector<String> addTypeV = null; + for ( int i = 0; i < xmlElementNodes.getLength(); ++i ) { + XSDElement xmlElementElement = new XSDElement((Element)xmlElementNodes.item(i)); + if ( !xmlElementElement.getParentNode().isSameNode(parentElement)) + continue; + String elementDescription=xmlElementElement.getPathDescriptionProperty(); + if(getItemName == null) { + addTypeV = xmlElementElement.getAddTypes(v.toString()); + } + if ( "true".equals(xmlElementElement.getAttribute("xml-key"))) { + path += "/{" + xmlElementElement.getAttribute("name") + "}"; + } + logger.debug("path: " + path); + logger.debug( "xmlElementElement.getAttribute(required):"+xmlElementElement.getAttribute("required") ); + + if ( ("true").equals(xmlElementElement.getAttribute("required"))) { + if ( requiredCnt == 0 ) + sbRequired.append(" required:\n"); + ++requiredCnt; + if ( addTypeV == null || addTypeV.isEmpty()) { + sbRequired.append(" - " + xmlElementElement.getAttribute("name") + "\n"); + } else { + for ( int k = 0; k < addTypeV.size(); ++k ) { + sbRequired.append(" - " + getXmlRootElementName(addTypeV.elementAt(k)) + ":\n"); + } + } + } + + if ( "true".equals(xmlElementElement.getAttribute("xml-key")) ) { + sbParameters.append(xmlElementElement.getPathParamYAML(elementDescription)); + } + if ( indexedProps != null + && indexedProps.contains(xmlElementElement.getAttribute("name") ) ) { + containerProps.add(xmlElementElement.getQueryParamYAML()); + NodeGetOperation.addContainerProps(container, containerProps); + } + if ( xmlElementElement.isStandardType()) { + sbProperties.append(xmlElementElement.getTypePropertyYAML()); + ++propertyCnt; + } + +// StringBuffer newPathParams = new StringBuffer((pathParams == null ? "" : pathParams.toString())+sbParameters.toString()); //cp8128 don't append the pathParams to sbParameters so that child nodes don't contain the parameters from parent + StringBuffer newPathParams = new StringBuffer(sbParameters.toString()); + for ( int k = 0; addTypeV != null && k < addTypeV.size(); ++k ) { + String addType = addTypeV.elementAt(k); + namespaceFilter.add(getXmlRootElementName(addType)); + if ( opId == null || !opId.contains(addType)) { + processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, null, + newPathParams, validEdges); + } + // need item name of array + String itemName = processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, + "array", null, null); + + if ( itemName != null ) { + if ( addType.equals("AaiInternal") ) { + logger.debug( "addType AaiInternal, skip properties"); + + } else if ( getItemName == null) { + ++propertyCnt; + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: array\n items:\n"); + sbProperties.append(" $ref: \"#/definitions/" + (itemName == "" ? "aai-internal" : itemName) + "\"\n"); + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + } + } else { + if ( ("java.util.ArrayList").equals(xmlElementElement.getAttribute("container-type"))) { + // need properties for getXmlRootElementName(addType) + namespaceFilter.add(getXmlRootElementName(addType)); + if(getXmlRootElementName(addType).equals("service-capabilities")) + { + logger.info("arrays: "+ getXmlRootElementName(addType)); + } +// newPathParams = new StringBuffer((pathParams == null ? "" : pathParams.toString())+sbParameters.toString()); //cp8128 - change this to not append pathParameters. Just use sbParameters + newPathParams = new StringBuffer(sbParameters.toString()); + processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), + pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, + null, newPathParams, validEdges); + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: array\n items: \n"); + sbProperties.append(" $ref: \"#/definitions/" + getXmlRootElementName(addType) + "\"\n"); + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + + } else { + //Make sure certain types added to the filter don't appear + if (nodeFilter.contains(getXmlRootElementName(addType))) { + ; + } else { + sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); + sbProperties.append(" type: object\n"); + sbProperties.append(" $ref: \"#/definitions/" + getXmlRootElementName(addType) + "\"\n"); + } + } + if ( StringUtils.isNotEmpty(elementDescription) ) + sbProperties.append(" description: " + elementDescription + "\n"); + ++propertyCnt; + } + } + } + + if ( sbParameters.toString().length() > 0 ) { + if ( pathParams == null ) + pathParams = new StringBuffer(); + pathParams.append(sbParameters); + } + if (indexedProps.isEmpty() && containerProps.isEmpty()){ + NodeGetOperation get = new NodeGetOperation(useOpId, xmlRootElementName, tag, path, null); + String operation = get.toString(); + if(StringUtils.isNotEmpty(operation)) { + operationDefinitions.put(xmlRootElementName, operation); + } + } else { + NodeGetOperation get = new NodeGetOperation(useOpId, xmlRootElementName, tag, path, pathParams == null ? "" : pathParams.toString()); + String operation = get.toString(); + if(StringUtils.isNotEmpty(operation)) { + operationDefinitions.put(xmlRootElementName, operation); + } + } + logger.debug("opId vs useOpId:"+opId+" vs "+useOpId+" PathParams="+pathParams); + // add PUT + if ( generatedJavaType.containsKey(xmlRootElementName) ) { + logger.debug("xmlRootElementName(1)="+xmlRootElementName); + return null; + } + boolean processingInventoryDef = false; + if ( xmlRootElementName.equals("inventory")) { + // inventory properties for each oxm to be concatenated + processingInventoryDef = true; + if ( inventoryDefSb == null ) { + inventoryDefSb = new StringBuilder(); + definitionsSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" properties:\n"); + } + + } else { + definitionsSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); + } + DeleteFootnoteSet footnotes = new DeleteFootnoteSet(xmlRootElementName); + StringBuffer sbEdge = new StringBuffer(); + LinkedHashSet<String> preventDelete = new LinkedHashSet<String>(); + String prevent=null; + String nodeCaption = new String(" ###### Related Nodes\n"); + try { + EdgeRuleQuery q = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).fromOnly().build(); + Multimap<String, EdgeRule> results = ei.getRules(q); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + sbEdge.append(nodeCaption); + nodeCaption=""; + for(String key : ss) { + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ logger.info(new String(new StringBuffer(" - TO ").append(i.getTo()).append(i.getDirection().toString()).append(i.getContains())));} ); + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ sbEdge.append(" - TO "+i.getTo()); EdgeDescription ed = new EdgeDescription(i); String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); sbEdge.append(ed.getRelationshipDescription("TO", xmlRootElementName)+footnote+"\n"); if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote);} ); + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("OUT")))).forEach((i) ->{ preventDelete.add(i.getTo().toUpperCase());} ); + } + } catch(Exception e) { + logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e); + } + try { + EdgeRuleQuery q1 = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).toOnly().build(); + Multimap<String, EdgeRule> results = ei.getRules(q1); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + sbEdge.append(nodeCaption); + for(String key : ss) { + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ sbEdge.append(" - FROM "+i.getFrom()); EdgeDescription ed = new EdgeDescription(i); String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); sbEdge.append(ed.getRelationshipDescription("FROM", xmlRootElementName)+footnote+"\n"); if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote);} ); + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ logger.info(new String(new StringBuffer(" - FROM ").append(i.getFrom()).append(i.getDirection().toString()).append(i.getContains())));} ); + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("IN")))).forEach((i) ->{ preventDelete.add(i.getFrom().toUpperCase());} ); + } + } catch(Exception e) { + logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e); + } + if(preventDelete.size() > 0) { + prevent = xmlRootElementName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); + logger.debug(prevent); + } + + if(StringUtils.isNotEmpty(prevent)) { + footnotes.add(prevent); + } + if(footnotes.footnotes.size() > 0) { + sbEdge.append(footnotes.toString()); + } + validEdges = sbEdge.toString(); + + // Handle description property. Might have a description OR valid edges OR both OR neither. + // Only put a description: tag if there is at least one. + if (StringUtils.isNotEmpty(pathDescriptionProperty) || StringUtils.isNotEmpty(validEdges) ) { + definitionsSb.append(" description: |\n"); + definitionsLocalSb.append(" description: |\n"); + + if ( pathDescriptionProperty != null ) { + definitionsSb.append(" " + pathDescriptionProperty + "\n" ); + definitionsLocalSb.append(" " + pathDescriptionProperty + "\n" ); + } + definitionsSb.append(validEdges); + definitionsLocalSb.append(validEdges); + } + + if ( requiredCnt > 0 ) { + definitionsSb.append(sbRequired); + definitionsLocalSb.append(sbRequired); + } + + if ( propertyCnt > 0 ) { + definitionsSb.append(" properties:\n"); + definitionsSb.append(sbProperties); + if ( !processingInventoryDef) { + definitionsLocalSb.append(" properties:\n"); + } + definitionsLocalSb.append(sbProperties); + } + try { + namespaceFilter.add(xmlRootElementName); + if ( xmlRootElementName.equals("inventory") ) { + //will add to javaTypeDefinitions at end + inventoryDefSb.append(definitionsLocalSb.toString()); + } else { + javaTypeDefinitions.put(xmlRootElementName, definitionsLocalSb.toString()); + } + } catch (Exception e) { + e.printStackTrace(); + } + if ( xmlRootElementName.equals("inventory") ) { + logger.trace("skip xmlRootElementName(2)="+xmlRootElementName); + return null; + } + generatedJavaType.put(xmlRootElementName, null); + //Write operations by Namespace(tagName) +/* + if( validTag(javaTypeName) && javaTypeName == useTag && tag == null) { + writeYAMLfile("nodes_"+javaTypeName, getDocumentHeader()+pathSb.toString()+appendDefinitions(namespaceFilter)); + totalPathSbAccumulator.append(pathSb); + pathSb.delete(0, pathSb.length()); + namespaceFilter.clear(); + } +*/ + logger.debug("xmlRootElementName(2)="+xmlRootElementName); + return null; + } + + private void writeYAMLfile(String outfileName, String fileContent) { + outfileName = (StringUtils.isEmpty(outfileName)) ? "aai_swagger" : outfileName; + outfileName = (outfileName.lastIndexOf(File.separator) == -1) ? yaml_dir + File.separator +outfileName+"_" + v.toString() + "." + generateTypeYAML : outfileName; + File outfile = new File(outfileName); + File parentDir = outfile.getParentFile(); + if(parentDir != null && ! parentDir.exists()) + parentDir.mkdirs(); + try { + outfile.createNewFile(); + } catch (IOException e) { + logger.error( "Exception creating output file " + outfileName); + e.printStackTrace(); + } + BufferedWriter bw = null; + try { + Charset charset = Charset.forName("UTF-8"); + Path path = Paths.get(outfileName); + bw = Files.newBufferedWriter(path, charset); + bw.write(fileContent); + if ( bw != null ) { + bw.close(); + } + } catch ( IOException e) { + logger.error( "Exception writing output file " + outfileName); + e.printStackTrace(); + } + } + + public boolean validTag(String tag) { + if(tag != null) { + switch ( tag ) { + case "Network": + case "Search": + case "Actions": + case "ServiceDesignAndCreation": + case "Business": + case "LicenseManagement": + case "CloudInfrastructure": + return true; + } + } + return false; + } + + public String appendOperations() { + //append definitions + StringBuffer sb = new StringBuffer(); + Map<String, String> sortedOperationDefinitions = new TreeMap<String, String>(operationDefinitions); + for (Map.Entry<String, String> entry : sortedOperationDefinitions.entrySet()) { + sb.append(entry.getValue()); + } + return sb.toString(); + } +} + diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/OxmFileProcessor.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/OxmFileProcessor.java index 04fdd3c0..391142cc 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/OxmFileProcessor.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/OxmFileProcessor.java @@ -19,37 +19,64 @@ */ package org.onap.aai.util.genxsd; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Version; -import org.w3c.dom.*; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import javax.xml.XMLConstants; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.StringReader; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Set; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.exceptions.AAIException; + +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.nodes.NodeIngestor; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; -public abstract class OxmFileProcessor { +import org.xml.sax.InputSource; + +public abstract class OxmFileProcessor { + EdgeIngestor ei; + NodeIngestor ni; + protected Set<String> namespaceFilter; protected File oxmFile; protected String xml; - protected Version v; + protected SchemaVersion v; protected Document doc = null; protected String apiVersion = null; + protected SchemaVersions schemaVersions; + + protected static int annotationsStartVersion = 9; // minimum version to support annotations in xsd - protected static int swaggerSupportStartsVersion = 7; // minimum version to support swagger documentation - + protected static int annotationsMinVersion = 6; // lower versions support annotations in xsd + protected static int swaggerSupportStartsVersion = 1; // minimum version to support swagger documentation + protected static int swaggerDiffStartVersion = 1; // minimum version to support difference + protected static int swaggerMinBasepath = 6; // minimum version to support difference + + protected String apiVersionFmt = null; protected HashMap<String, String> generatedJavaType = new HashMap<String, String>(); protected HashMap<String, String> appliedPaths = new HashMap<String, String>(); protected NodeList javaTypeNodes = null; - protected static Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions(); - private static Map<String, String> createJavaTypeDefinitions() + + protected Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions(); + private Map<String, String> createJavaTypeDefinitions() { StringBuffer aaiInternal = new StringBuffer(); StringBuffer nodes = new StringBuffer(); @@ -70,40 +97,61 @@ public abstract class OxmFileProcessor { javaTypeDefinitions.put("nodes", nodes.toString()); return javaTypeDefinitions; } + static List<String> nodeFilter = createNodeFilter(); + private static List<String> createNodeFilter() + { + List<String> list = Arrays.asList("search", "actions", "aai-internal", "nodes"); + return list; + } + public OxmFileProcessor(SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){ + this.schemaVersions = schemaVersions; + this.ni = ni; + this.ei = ei; + } + + - public OxmFileProcessor(File oxmFile, Version v) { - super(); + public void setOxmVersion(File oxmFile, SchemaVersion v) { this.oxmFile = oxmFile; this.v = v; } - public OxmFileProcessor(String xml, Version v) { + public void setXmlVersion(String xml, SchemaVersion v) { this.xml = xml; this.v = v; } - protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException { - DocumentBuilder dBuilder = null; - try { - DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); - dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); - dBuilder = dbFactory.newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw e; + + public void setVersion(SchemaVersion v) { + this.oxmFile = null; + this.v = v; + } + + public void setNodeIngestor(NodeIngestor ni) { + this.ni = ni; + } + + public void setEdgeIngestor(EdgeIngestor ei) { + this.ei = ei; + } + + public SchemaVersions getSchemaVersions() { + return schemaVersions; + } + + public void setSchemaVersions(SchemaVersions schemaVersions) { + this.schemaVersions = schemaVersions; + } + + protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException, EdgeRuleNotFoundException { + if(this.xml != null || this.oxmFile != null ) { + createDocument(); } - try { - if ( xml == null ) { - doc = dBuilder.parse(oxmFile); - } else { - InputSource isInput = new InputSource(new StringReader(xml)); - doc = dBuilder.parse(isInput); - } - } catch (SAXException e) { - throw e; - } catch (IOException e) { - throw e; + if(this.doc == null) { + this.doc = ni.getSchema(v); } - + namespaceFilter = new HashSet<>(); + NodeList bindingsNodes = doc.getElementsByTagName("xml-bindings"); Element bindingElement; NodeList javaTypesNodes; @@ -125,8 +173,32 @@ public abstract class OxmFileProcessor { throw new AAIException("OXM file error: missing <binding-nodes><java-types><java-type> in " + oxmFile ); } } + + private void createDocument() throws ParserConfigurationException, SAXException, IOException, AAIException { + DocumentBuilder dBuilder = null; + try { + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); + dBuilder = dbFactory.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw e; + } + try { + if ( xml == null ) { + doc = dBuilder.parse(oxmFile); + } else { + InputSource isInput = new InputSource(new StringReader(xml)); + doc = dBuilder.parse(isInput); + } + } catch (SAXException e) { + throw e; + } catch (IOException e) { + throw e; + } + return; + } public abstract String getDocumentHeader(); - public abstract String process() throws AAIException; + public abstract String process() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException ; public String getXMLRootElementName(Element javaTypeElement) { String xmlRootElementName=null; @@ -195,5 +267,21 @@ public abstract class OxmFileProcessor { } return (Element) null; } + + public boolean versionSupportsSwaggerDiff( String version) { + int ver = new Integer(version.substring(1)).intValue(); + if ( ver >= HTMLfromOXM.swaggerDiffStartVersion ) { + return true; + } + return false; + } + + public boolean versionSupportsBasePathProperty( String version) { + int ver = new Integer(version.substring(1)).intValue(); + if ( ver <= HTMLfromOXM.swaggerMinBasepath ) { + return true; + } + return false; + } } diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/PatchOperation.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/PatchOperation.java index cc90114f..37b2d1b1 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/PatchOperation.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/PatchOperation.java @@ -19,11 +19,11 @@ */ package org.onap.aai.util.genxsd; +import java.util.StringTokenizer; + import org.apache.commons.lang3.StringUtils; import org.onap.aai.util.GenerateXsd; -import java.util.StringTokenizer; - public class PatchOperation { private String useOpId; private String xmlRootElementName; @@ -76,7 +76,7 @@ public class PatchOperation { if ( path.endsWith("/relationship") ) { pathSb.append(" summary: see node definition for valid relationships\n"); - relationshipExamplesSb.append("[See Examples](apidocs/relations/"+ GenerateXsd.getAPIVersion()+"/"+useOpId+".json)"); + relationshipExamplesSb.append("[See Examples](apidocs/relations/"+GenerateXsd.getAPIVersion()+"/"+useOpId+".json)"); } else { pathSb.append(" summary: update an existing " + xmlRootElementName + "\n"); pathSb.append(" description: |\n"); @@ -95,10 +95,8 @@ public class PatchOperation { pathSb.append(" operationId: Update" + useOpId + "\n"); pathSb.append(" consumes:\n"); pathSb.append(" - application/json\n"); - pathSb.append(" - application/xml\n"); pathSb.append(" produces:\n"); pathSb.append(" - application/json\n"); - pathSb.append(" - application/xml\n"); pathSb.append(" responses:\n"); pathSb.append(" \"default\":\n"); pathSb.append(" " + GenerateXsd.getResponsesUrl()); diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/PutOperation.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/PutOperation.java index 58ccec37..16bbc2c7 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/PutOperation.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/PutOperation.java @@ -18,22 +18,21 @@ * ============LICENSE_END========================================================= */ package org.onap.aai.util.genxsd; +import java.util.StringTokenizer; import org.apache.commons.lang3.StringUtils; -import org.onap.aai.introspection.Version; +import org.onap.aai.setup.SchemaVersion; import org.onap.aai.util.GenerateXsd; -import java.util.StringTokenizer; - public class PutOperation { private String useOpId; private String xmlRootElementName; private String tag; private String path; private String pathParams; - private Version version; - - public PutOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams, Version v) { + private SchemaVersion version; + + public PutOperation(String useOpId, String xmlRootElementName, String tag, String path, String pathParams, SchemaVersion v) { super(); this.useOpId = useOpId; this.xmlRootElementName = xmlRootElementName; @@ -80,7 +79,7 @@ public class PutOperation { pathSb.append(" summary: create or update an existing " + xmlRootElementName + "\n"); pathSb.append(" description: |\n Create or update an existing " + xmlRootElementName + ".\n #\n Note! This PUT method has a corresponding PATCH method that can be used to update just a few of the fields of an existing object, rather than a full object replacement. An example can be found in the [PATCH section] below\n"); } - relationshipExamplesSb.append("[Valid relationship examples shown here](apidocs/relations/"+version.name()+"/"+useOpId.replace("RelationshipListRelationship", "")+".json)"); + relationshipExamplesSb.append("[Valid relationship examples shown here](apidocs/relations/"+version.toString()+"/"+useOpId.replace("RelationshipListRelationship", "")+".json)"); pathSb.append(" operationId: createOrUpdate" + useOpId + "\n"); pathSb.append(" consumes:\n"); pathSb.append(" - application/json\n"); diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java index ca5a6fa7..d6eaa5c9 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/PutRelationPathSet.java @@ -19,45 +19,53 @@ */ package org.onap.aai.util.genxsd; -import org.apache.commons.text.similarity.LevenshteinDistance; -import org.onap.aai.introspection.Version; -import org.onap.aai.util.GenerateXsd; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.SortedSet; +import java.util.TreeSet; + +import org.apache.commons.text.similarity.LevenshteinDistance; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.config.SpringContextAware; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.util.GenerateXsd; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.collect.Multimap; public class PutRelationPathSet { + EdgeIngestor ei; private static final Logger logger = LoggerFactory.getLogger("PutRelationPathSet.class"); protected static HashMap<String, String> putRelationPaths = new HashMap<String, String>(); - private static EdgeRuleSet edgeRuleSet = null; public static void add(String useOpId, String path) { putRelationPaths.put(useOpId, path); } String apiPath; String opId; - Version version; + SchemaVersion version; protected ArrayList<String> relations = new ArrayList<String>(); String objectName = ""; - public PutRelationPathSet(Version v) { + public PutRelationPathSet(SchemaVersion v) { this.version = v; } - public PutRelationPathSet(String opId, String path, Version v) { + public PutRelationPathSet(String opId, String path, SchemaVersion v) { this.apiPath = path.replace("/relationship-list/relationship", ""); this.opId = opId; this.version = v; - objectName = DeleteOperation.deletePaths.get(apiPath); + objectName = DeleteOperation.deletePaths.get(apiPath); logger.debug("II-apiPath: "+apiPath+"\nPath: "+path+"\nopId="+opId+"\nobjectName="+objectName); } - private void process() { + private void process(EdgeIngestor edgeIngestor) { + this.ei = edgeIngestor; this.toRelations(); this.fromRelations(); this.writeRelationsFile(); @@ -66,76 +74,75 @@ public class PutRelationPathSet { private void toRelations() { logger.debug("{“comment”: “Valid TO Relations that can be added”},"); logger.debug("apiPath: "+apiPath+"\nopId="+opId+"\nobjectName="+objectName); - Collection<EdgeDescription> toEdges = edgeRuleSet.getEdgeRulesTO(objectName); - - if(toEdges.size() > 0) { + try { + + EdgeRuleQuery q1 = new EdgeRuleQuery.Builder("ToOnly",objectName).version(version).build(); + Multimap<String, EdgeRule> results = ei.getRules(q1); relations.add("{\"comment\": \"Valid TO Relations that can be added\"}\n"); - } - for (EdgeDescription ed : toEdges) { - logger.debug(ed.getRuleKey()+"Type="+ed.getType()); - String obj = ed.getRuleKey().replace(objectName,"").replace("|",""); - String selectedRelation = ""; - if(ed.getType() == EdgeDescription.LineageType.UNRELATED) { - String selectObj = getUnrelatedObjectPaths(obj, apiPath); - logger.debug("SelectedObj:"+selectObj); - selectedRelation = formatObjectRelationSet(obj,selectObj); - logger.debug("ObjectRelationSet"+selectedRelation); - } else { - String selectObj = getKinObjectPath(obj, apiPath); - logger.debug("SelectedObj:"+selectObj); - selectedRelation = formatObjectRelation(obj,selectObj); - logger.debug("ObjectRelationSet"+selectedRelation); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + for(String key : ss) { + results.get(key).stream().filter((i) -> (! i.isPrivateEdge())).forEach((i) ->{ String rel = selectedRelation(i); relations.add(rel); logger.debug("Relation added: "+rel); } ); } - relations.add(selectedRelation); - logger.debug("Relation added: "+selectedRelation); + } catch(Exception e) { + logger.debug("objectName: "+objectName+"\n"+e); + } + } + private String selectedRelation(EdgeRule rule) { + String selectedRelation = ""; + EdgeDescription ed = new EdgeDescription(rule); + logger.debug(ed.getRuleKey()+"Type="+ed.getType()); + String obj = ed.getRuleKey().replace(objectName,"").replace("|",""); + + if(ed.getType() == EdgeDescription.LineageType.UNRELATED) { + String selectObj = getUnrelatedObjectPaths(obj, apiPath); + logger.debug("SelectedObj"+selectObj); + selectedRelation = formatObjectRelationSet(obj,selectObj); + logger.trace("ObjectRelationSet"+selectedRelation); + } else { + String selectObj = getKinObjectPath(obj, apiPath); + logger.debug("SelectedObj"+selectObj); + selectedRelation = formatObjectRelation(obj,selectObj); + logger.trace("ObjectRelationSet"+selectedRelation); } + return selectedRelation; } private void fromRelations() { logger.debug("“comment”: “Valid FROM Relations that can be added”"); - Collection<EdgeDescription> fromEdges = edgeRuleSet.getEdgeRulesFROM(objectName); - if(fromEdges.size() > 0) { + try { + + EdgeRuleQuery q1 = new EdgeRuleQuery.Builder(objectName,"FromOnly").version(version).build(); + Multimap<String, EdgeRule> results = ei.getRules(q1); relations.add("{\"comment\": \"Valid FROM Relations that can be added\"}\n"); - } - for (EdgeDescription ed : fromEdges) { - logger.debug(ed.getRuleKey()+"Type="+ed.getType()); - String obj = ed.getRuleKey().replace(objectName,"").replace("|",""); - String selectedRelation = ""; - if(ed.getType() == EdgeDescription.LineageType.UNRELATED) { - String selectObj = getUnrelatedObjectPaths(obj, apiPath); - logger.debug("SelectedObj"+selectObj); - selectedRelation = formatObjectRelationSet(obj,selectObj); - logger.trace("ObjectRelationSet"+selectedRelation); - } else { - String selectObj = getKinObjectPath(obj, apiPath); - logger.debug("SelectedObj"+selectObj); - selectedRelation = formatObjectRelation(obj,selectObj); - logger.trace("ObjectRelationSet"+selectedRelation); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + for(String key : ss) { + results.get(key).stream().filter((i) -> (! i.isPrivateEdge())).forEach((i) ->{ String rel = selectedRelation(i); relations.add(rel); logger.debug("Relation added: "+rel); } ); } - relations.add(selectedRelation); - logger.trace(selectedRelation); + } catch(Exception e) { + logger.debug("objectName: "+objectName+"\n"+e); } } private void writeRelationsFile() { + File examplefilePath = new File(GenerateXsd.getYamlDir() + "/relations/" + version.toString()+"/"+opId.replace("RelationshipListRelationship", "") + ".json"); - File exampleFilePath = new File(GenerateXsd.getYamlDir() + "/relations/" + version.name()+"/"+opId.replace("RelationshipListRelationship", "") + ".json"); - logger.debug(String.join("exampleFilePath: ", exampleFilePath.toString())); - + logger.debug(String.join("exampleFilePath: ", examplefilePath.toString())); + FileOutputStream fop = null; try { - if (!exampleFilePath.exists()) { - exampleFilePath.getParentFile().mkdirs(); - exampleFilePath.createNewFile(); + if (!examplefilePath.exists()) { + examplefilePath.getParentFile().mkdirs(); + examplefilePath.createNewFile(); } + fop = new FileOutputStream(examplefilePath); } catch(Exception e) { e.printStackTrace(); return; } - - try(FileOutputStream fop = new FileOutputStream(exampleFilePath)){ + try { if(relations.size() > 0) {fop.write("[\n".getBytes());} fop.write(String.join(",\n", relations).getBytes()); if(relations.size() > 0) {fop.write("\n]\n".getBytes());} fop.flush(); + fop.close(); } catch (Exception e) { e.printStackTrace(); return; @@ -194,22 +201,16 @@ public class PutRelationPathSet { return targetPath; } - public void generateRelations(EdgeRuleSet edgeRuleSet) { - - if(putRelationPaths == null) - return; - if(edgeRuleSet == null) - return; - else - PutRelationPathSet.edgeRuleSet = edgeRuleSet; + public void generateRelations(EdgeIngestor edgeIngestor) { putRelationPaths.forEach((k,v)->{ logger.trace("k="+k+"\n"+"v="+v+v.equals("/business/customers/customer/{global-customer-id}/service-subscriptions/service-subscription/{service-type}/service-instances/service-instance/{service-instance-id}/allotted-resources/allotted-resource/{id}/relationship-list/relationship")); logger.debug("apiPath(Operation): "+v); logger.debug("Target object: "+v.replace("/relationship-list/relationship", "")); logger.debug("Relations: "); PutRelationPathSet prp = new PutRelationPathSet(k, v, this.version); - prp.process(); + prp.process(edgeIngestor); }); } } + diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/XSDElement.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/XSDElement.java index 5008c764..91c9459d 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/XSDElement.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/XSDElement.java @@ -19,22 +19,32 @@ */ package org.onap.aai.util.genxsd; -import com.google.common.base.Joiner; -import org.apache.commons.lang3.StringUtils; -import org.onap.aai.introspection.Version; -import org.w3c.dom.*; - import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.util.Vector; +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.setup.SchemaVersion; +import org.w3c.dom.Attr; +import org.w3c.dom.DOMException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.TypeInfo; +import org.w3c.dom.UserDataHandler; + +import com.google.common.base.Joiner; + public class XSDElement implements Element { Element xmlElementElement; private static final int VALUE_NONE = 0; private static final int VALUE_DESCRIPTION = 1; private static final int VALUE_INDEXED_PROPS = 2; private static final int VALUE_CONTAINER = 3; + private static final int VALUE_REQUIRES = 4; public XSDElement(Element xmlElementElement) { super(); @@ -58,6 +68,8 @@ public class XSDElement implements Element { if ( attrName.equals("type")) { if ( attrValue.contains(apiVersionFmt) ) { addType = attrValue.substring(attrValue.lastIndexOf('.')+1); + if ( addTypeV == null ) + addTypeV = new Vector<String>(); addTypeV.add(addType); } @@ -66,6 +78,40 @@ public class XSDElement implements Element { return addTypeV; } + public String getRequiresProperty() { + String elementAlsoRequiresProperty = null; + NodeList xmlPropNodes = this.getElementsByTagName("xml-properties"); + + for ( int i = 0; i < xmlPropNodes.getLength(); ++i ) { + Element xmlPropElement = (Element)xmlPropNodes.item(i); + if (! xmlPropElement.getParentNode().getAttributes().getNamedItem("name").getNodeValue().equals(this.xmlElementElement.getAttribute("name"))){ + continue; + } + NodeList childNodes = xmlPropElement.getElementsByTagName("xml-property"); + + for ( int j = 0; j < childNodes.getLength(); ++j ) { + Element childElement = (Element)childNodes.item(j); + // get name + int useValue = VALUE_NONE; + NamedNodeMap attributes = childElement.getAttributes(); + for ( int k = 0; k < attributes.getLength(); ++k ) { + Attr attr = (Attr) attributes.item(k); + String attrName = attr.getNodeName(); + String attrValue = attr.getNodeValue(); + if ( attrName == null || attrValue == null ) + continue; + if ( attrName.equals("name") && attrValue.equals("requires")) { + useValue = VALUE_REQUIRES; + } + if ( useValue == VALUE_REQUIRES && attrName.equals("value")) { + elementAlsoRequiresProperty = attrValue; + } + } + } + } + return elementAlsoRequiresProperty; + } + public String getPathDescriptionProperty() { String pathDescriptionProperty = null; NodeList xmlPropNodes = this.getElementsByTagName("xml-properties"); @@ -230,13 +276,13 @@ public class XSDElement implements Element { return sbParameter.toString(); } - public String getHTMLElement(Version v, boolean useAnnotation, HTMLfromOXM driver) { + public String getHTMLElement(SchemaVersion v, boolean useAnnotation, HTMLfromOXM driver) { StringBuffer sbElement = new StringBuffer(); String elementName = this.getAttribute("name"); String elementType = this.getAttribute("type"); String elementContainerType = this.getAttribute("container-type"); String elementIsRequired = this.getAttribute("required"); - String addType = elementType.contains("." + v.name() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; + String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; if ( addType != null ) { sbElement.append(" <xs:element ref=\"tns:" + driver.getXmlRootElementName(addType)+"\""); @@ -255,7 +301,7 @@ public class XSDElement implements Element { sbElement.append(" minOccurs=\"0\""); } if ( elementContainerType != null && elementContainerType.equals("java.util.ArrayList")) { - sbElement.append(" maxOccurs=\"unbounded\""); + sbElement.append(" maxOccurs=\"5000\""); } if(useAnnotation) { String annotation = new XSDElement(xmlElementElement).getHTMLAnnotation("field", " "); @@ -269,7 +315,7 @@ public class XSDElement implements Element { // return sbElement.toString(); } - public String getHTMLElementWrapper(String unwrappedElement, Version v, boolean useAnnotation) { + public String getHTMLElementWrapper(String unwrappedElement, SchemaVersion v, boolean useAnnotation) { NodeList childNodes = this.getElementsByTagName("xml-element-wrapper"); @@ -286,7 +332,7 @@ public class XSDElement implements Element { sbElement.append(" <xs:element name=\"" + xmlElementWrapper +"\""); String elementType = xmlElementElement.getAttribute("type"); String elementIsRequired = this.getAttribute("required"); - String addType = elementType.contains("." + v.name() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; + String addType = elementType.contains("." + v.toString() + ".") ? elementType.substring(elementType.lastIndexOf('.')+1) : null; if ( elementIsRequired == null || !elementIsRequired.equals("true")||addType != null) { sbElement.append(" minOccurs=\"0\""); @@ -368,6 +414,9 @@ public class XSDElement implements Element { String attrDescription = this.getPathDescriptionProperty(); if ( attrDescription != null && attrDescription.length() > 0 ) sbProperties.append(" description: " + attrDescription + "\n"); + String elementAlsoRequiresProperty=this.getRequiresProperty(); + if ( StringUtils.isNotEmpty(elementAlsoRequiresProperty) ) + sbProperties.append(" also requires: " + elementAlsoRequiresProperty + "\n"); return sbProperties.toString(); } @@ -674,3 +723,4 @@ public class XSDElement implements Element { } + diff --git a/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java b/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java index da3855c3..930e2fb2 100644 --- a/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java +++ b/aai-core/src/main/java/org/onap/aai/util/genxsd/YAMLfromOXM.java @@ -19,16 +19,6 @@ */ package org.onap.aai.util.genxsd; -import org.apache.commons.lang3.StringUtils; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.Version; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - -import javax.xml.parsers.ParserConfigurationException; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; @@ -37,60 +27,93 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.*; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.StringTokenizer; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.Vector; + +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.commons.lang3.StringUtils; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.edges.EdgeRule; +import org.onap.aai.edges.EdgeRuleQuery; +import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.nodes.NodeIngestor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; +import com.google.common.collect.Multimap; + public class YAMLfromOXM extends OxmFileProcessor { - private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class"); + private static final Logger logger = LoggerFactory.getLogger("YAMLfromOXM.class"); +// private static StringBuffer totalPathSbAccumulator = new StringBuffer(); private static final String root = "../aai-schema/src/main/resources"; private static final String autoGenRoot = "aai-schema/src/main/resources"; private static final String generateTypeYAML = "yaml"; private static final String normalStartDir = "aai-core"; private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/aai_swagger_yaml"; + private StringBuilder inventoryDefSb = null; + - private File edgeFile; - private EdgeRuleSet edgeRuleSet = null; - public YAMLfromOXM(File oxmFile, Version v, File edgeFile) throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException { - super(oxmFile, v); - this.edgeFile = edgeFile; - init(); + private String basePath; + + public YAMLfromOXM(String basePath, SchemaVersions schemaVersions, NodeIngestor ni, EdgeIngestor ei){ + super(schemaVersions, ni,ei); + this.basePath = basePath; + } + public void setOxmVersion(File oxmFile, SchemaVersion v) { + super.setOxmVersion(oxmFile, v); } - public YAMLfromOXM(String xml, Version v, File edgeFile) throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException { - super(xml, v); - this.edgeFile = edgeFile; - init(); + public void setXmlVersion(String xml, SchemaVersion v){ + super.setXmlVersion(xml, v); } + public void setVersion(SchemaVersion v) { + super.setVersion(v); + } + @Override public String getDocumentHeader() { StringBuffer sb = new StringBuffer(); sb.append("swagger: \"2.0\"\ninfo:\n "); sb.append("description: |"); - sb.append("\n\n [Differences versus the previous schema version]("+"apidocs/aai_swagger_" + v.name() + ".diff)"); - sb.append("\n\n Copyright © 2017 AT&T Intellectual Property. All rights reserved.\n\n Licensed under the Creative Commons License, Attribution 4.0 Intl. (the "License"); you may not use this documentation except in compliance with the License.\n\n You may obtain a copy of the License at\n\n (https://creativecommons.org/licenses/by/4.0/)\n\n 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.\n\n ECOMP and OpenECOMP are trademarks and service marks of AT&T Intellectual Property.\n\n This document is best viewed with Firefox or Chrome. Nodes can be found by appending /#/definitions/node-type-to-find to the path to this document. Edge definitions can be found with the node definitions.\n version: \"" + v.name() +"\"\n"); + if ( versionSupportsSwaggerDiff(v.toString())) { + sb.append("\n\n [Differences versus the previous schema version](" + "apidocs/aai_swagger_" + v.toString() + ".diff)"); + } + sb.append("\n\n Copyright © 2017-18 AT&T Intellectual Property. All rights reserved.\n\n Licensed under the Creative Commons License, Attribution 4.0 Intl. (the "License"); you may not use this documentation except in compliance with the License.\n\n You may obtain a copy of the License at\n\n (https://creativecommons.org/licenses/by/4.0/)\n\n 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.\n\n This document is best viewed with Firefox or Chrome. Nodes can be found by appending /#/definitions/node-type-to-find to the path to this document. Edge definitions can be found with the node definitions.\n version: \"" + v.toString() +"\"\n"); sb.append(" title: Active and Available Inventory REST API\n"); sb.append(" license:\n name: Apache 2.0\n url: http://www.apache.org/licenses/LICENSE-2.0.html\n"); sb.append(" contact:\n name:\n url:\n email:\n"); - sb.append("host:\nbasePath: /aai/" + v.name() + "\n"); + sb.append("host:\nbasePath: " + basePath + "/" + v.toString() + "\n"); sb.append("schemes:\n - https\npaths:\n"); return sb.toString(); } - protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException { + protected void init() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException { super.init(); - edgeRuleSet = new EdgeRuleSet(edgeFile); } @Override - public String process() throws AAIException { + public String process() throws ParserConfigurationException, SAXException, IOException, AAIException, FileNotFoundException, EdgeRuleNotFoundException { StringBuffer sb = new StringBuffer(); - EdgeRuleSet edgeRuleSet = null; - try { - edgeRuleSet = new EdgeRuleSet(edgeFile); - } catch (Exception e) { - logger.warn("No valid Edge Rule Set available("+edgeFile+"): "+e.getMessage()); - } - StringBuffer pathSb = new StringBuffer(); + try { + init(); + } catch(Exception e) { + logger.error( "Error initializing " + this.getClass()); + throw e; + } pathSb.append(getDocumentHeader()); StringBuffer definitionsSb = new StringBuffer(); for ( int i = 0; i < javaTypeNodes.getLength(); ++ i ) { @@ -102,6 +125,7 @@ public class YAMLfromOXM extends OxmFileProcessor { logger.error(msg); throw new AAIException(msg); } + namespaceFilter.add(getXmlRootElementName(javaTypeName)); //Skip any type that has already been processed(recursion could be the reason) if ( generatedJavaType.containsKey(getXmlRootElementName(javaTypeName)) ) { continue; @@ -113,15 +137,29 @@ public class YAMLfromOXM extends OxmFileProcessor { sb.append(appendDefinitions()); PutRelationPathSet prp = new PutRelationPathSet(v); - prp.generateRelations(edgeRuleSet); + prp.generateRelations(ei); return sb.toString(); } public String appendDefinitions() { + return appendDefinitions(null); + } + + public String appendDefinitions(Set<String> namespaceFilter) { //append definitions + if ( inventoryDefSb != null ) { + javaTypeDefinitions.put("inventory", inventoryDefSb.toString()); + } StringBuffer sb = new StringBuffer("definitions:\n"); Map<String, String> sortedJavaTypeDefinitions = new TreeMap<String, String>(javaTypeDefinitions); for (Map.Entry<String, String> entry : sortedJavaTypeDefinitions.entrySet()) { +// logger.info("Key: "+entry.getKey()+"Value: "+ entry.getValue()); + if(namespaceFilter != null && entry.getKey().matches("service-capabilities")) { + for(String tally : namespaceFilter) { logger.debug("Marker: "+tally);} + } + if(namespaceFilter != null && (! namespaceFilter.contains(entry.getKey()))) { + continue; + } logger.debug("Key: "+entry.getKey()+"Test: "+ (entry.getKey() == "relationship")); if(entry.getKey().matches("relationship")) { String jb=entry.getValue(); @@ -140,6 +178,9 @@ public class YAMLfromOXM extends OxmFileProcessor { sb.append("patchDefinitions:\n"); for (Map.Entry<String, String> entry : sortedJavaTypeDefinitions.entrySet()) { + if(namespaceFilter != null && (! namespaceFilter.contains(entry.getKey()))) { + continue; + } String jb=entry.getValue().replaceAll("/definitions/", "/patchDefinitions/"); int ndx=jb.indexOf("relationship-list:"); if(ndx > 0) { @@ -159,6 +200,9 @@ public class YAMLfromOXM extends OxmFileProcessor { sb.append("getDefinitions:\n"); for (Map.Entry<String, String> entry : sortedJavaTypeDefinitions.entrySet()) { + if(namespaceFilter != null && (! namespaceFilter.contains(entry.getKey()))) { + continue; + } String jb=entry.getValue().replaceAll("/definitions/", "/getDefinitions/"); sb.append(jb); } @@ -182,6 +226,7 @@ public class YAMLfromOXM extends OxmFileProcessor { case "Business": case "LicenseManagement": case "CloudInfrastructure": + case "Common": break; default: logger.debug("javaTypeName="+javaTypeName); @@ -199,7 +244,6 @@ public class YAMLfromOXM extends OxmFileProcessor { if ( tag == null ) useTag = javaTypeName; } - path = xmlRootElementName.equals("inventory") ? "" : (path == null) ? "/" + xmlRootElementName : path + "/" + xmlRootElementName; XSDJavaType javaType = new XSDJavaType(javaTypeElement); if ( getItemName != null) { @@ -249,7 +293,7 @@ public class YAMLfromOXM extends OxmFileProcessor { continue; String elementDescription=xmlElementElement.getPathDescriptionProperty(); if(getItemName == null) { - addTypeV = xmlElementElement.getAddTypes(v.name()); + addTypeV = xmlElementElement.getAddTypes(v.toString()); } if ( "true".equals(xmlElementElement.getAttribute("xml-key"))) { path += "/{" + xmlElementElement.getAttribute("name") + "}"; @@ -286,7 +330,9 @@ public class YAMLfromOXM extends OxmFileProcessor { StringBuffer newPathParams = new StringBuffer((pathParams == null ? "" : pathParams.toString())+sbParameters.toString()); for ( int k = 0; addTypeV != null && k < addTypeV.size(); ++k ) { String addType = addTypeV.elementAt(k); + namespaceFilter.add(getXmlRootElementName(addType)); logger.debug("addType: "+ addType); + if ( opId == null || !opId.contains(addType)) { processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, null, @@ -305,13 +351,14 @@ public class YAMLfromOXM extends OxmFileProcessor { ++propertyCnt; sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); sbProperties.append(" type: array\n items:\n"); - sbProperties.append(" $ref: \"#/definitions/" + (itemName == "" ? "aai-internal" : itemName) + "\"\n"); + sbProperties.append(" $ref: \"#/definitions/" + (itemName == "" ? "inventory-item-data" : itemName) + "\"\n"); if ( StringUtils.isNotEmpty(elementDescription) ) sbProperties.append(" description: " + elementDescription + "\n"); } } else { if ( ("java.util.ArrayList").equals(xmlElementElement.getAttribute("container-type"))) { // need properties for getXmlRootElementName(addType) + namespaceFilter.add(getXmlRootElementName(addType)); newPathParams = new StringBuffer((pathParams == null ? "" : pathParams.toString())+sbParameters.toString()); processJavaTypeElementSwagger( addType, getJavaTypeElementSwagger(addType), pathSb, definitionsSb, path, tag == null ? useTag : tag, useOpId, @@ -323,9 +370,10 @@ public class YAMLfromOXM extends OxmFileProcessor { sbProperties.append(" description: " + elementDescription + "\n"); } else { - if(addType.equals("AaiInternal")) //Filter out references to AaiInternal - sbProperties.append(""); - else { + //Make sure certain types added to the filter don't appear + if(nodeFilter.contains(getXmlRootElementName(addType))) { + ; + } else { sbProperties.append(" " + getXmlRootElementName(addType) + ":\n"); sbProperties.append(" type: object\n"); sbProperties.append(" $ref: \"#/definitions/" + getXmlRootElementName(addType) + "\"\n"); @@ -355,51 +403,75 @@ public class YAMLfromOXM extends OxmFileProcessor { // add DELETE DeleteOperation del = new DeleteOperation(useOpId, xmlRootElementName, tag, path, pathParams == null ? "" : pathParams.toString()); pathSb.append(del.toString()); - //Write operations by Namespace(tagName) -// if(javaTypeName == useTag && tag == null) { -// pathSb.append(appendDefinitions()); -// writeYAMLfile(javaTypeName, pathSb.toString()); -// pathSb.delete(0, pathSb.length()); -// javaTypeDefinitions.clear(); -// generatedJavaType.clear(); -// } if ( generatedJavaType.containsKey(xmlRootElementName) ) { logger.debug("xmlRootElementName(1)="+xmlRootElementName); return null; } - definitionsSb.append(" " + xmlRootElementName + ":\n"); - definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); - Collection<EdgeDescription> edges = edgeRuleSet.getEdgeRules(xmlRootElementName ); + boolean processingInventoryDef = false; + if ( xmlRootElementName.equals("inventory")) { + // inventory properties for each oxm to be concatenated + processingInventoryDef = true; + if ( inventoryDefSb == null ) { + inventoryDefSb = new StringBuilder(); + definitionsSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" properties:\n"); + } + + } else { + definitionsSb.append(" " + xmlRootElementName + ":\n"); + definitionsLocalSb.append(" " + xmlRootElementName + ":\n"); + } +// Collection<EdgeDescription> edges = edgeRuleSet.getEdgeRules(xmlRootElementName ); DeleteFootnoteSet footnotes = new DeleteFootnoteSet(xmlRootElementName); - if ( edges.size() > 0 ) { - StringBuffer sbEdge = new StringBuffer(); - sbEdge.append(" ###### Related Nodes\n"); - - for (EdgeDescription ed : edges) { - if ( ed.getRuleKey().startsWith(xmlRootElementName)) { - sbEdge.append(" - TO ").append(ed.getRuleKey().substring(ed.getRuleKey().indexOf("|")+1)); - String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); - sbEdge.append(ed.getRelationshipDescription("TO", xmlRootElementName)+footnote+"\n"); - if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote); - } + StringBuffer sbEdge = new StringBuffer(); + LinkedHashSet<String> preventDelete = new LinkedHashSet<String>(); + String prevent=null; + String nodeCaption = new String(" ###### Related Nodes\n"); + try { + EdgeRuleQuery q = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).fromOnly().build(); + Multimap<String, EdgeRule> results = ei.getRules(q); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + sbEdge.append(nodeCaption); + nodeCaption=""; + for(String key : ss) { + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ logger.info(new String(new StringBuffer(" - TO ").append(i.getTo()).append(i.getDirection().toString()).append(i.getContains())));} ); + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ sbEdge.append(" - TO "+i.getTo()); EdgeDescription ed = new EdgeDescription(i); String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); sbEdge.append(ed.getRelationshipDescription("TO", xmlRootElementName)+footnote+"\n"); if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote);} ); + results.get(key).stream().filter((i) -> (i.getFrom().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("OUT")))).forEach((i) ->{ preventDelete.add(i.getTo().toUpperCase());} ); } - for (EdgeDescription ed : edges) { - if ( ed.getRuleKey().endsWith(xmlRootElementName)) { - sbEdge.append(" - FROM ").append(ed.getRuleKey().substring(0, ed.getRuleKey().indexOf("|"))); - String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); - sbEdge.append(ed.getRelationshipDescription("FROM", xmlRootElementName)+footnote+"\n"); - if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote); - } + } catch(Exception e) { + logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e); + } + try { + EdgeRuleQuery q1 = new EdgeRuleQuery.Builder(xmlRootElementName).version(v).toOnly().build(); + Multimap<String, EdgeRule> results = ei.getRules(q1); + SortedSet<String> ss=new TreeSet<String>(results.keySet()); + sbEdge.append(nodeCaption); + for(String key : ss) { + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ sbEdge.append(" - FROM "+i.getFrom()); EdgeDescription ed = new EdgeDescription(i); String footnote = ed.getAlsoDeleteFootnote(xmlRootElementName); sbEdge.append(ed.getRelationshipDescription("FROM", xmlRootElementName)+footnote+"\n"); if(StringUtils.isNotEmpty(footnote)) footnotes.add(footnote);} ); + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge()))).forEach((i) ->{ logger.info(new String(new StringBuffer(" - FROM ").append(i.getFrom()).append(i.getDirection().toString()).append(i.getContains())));} ); + results.get(key).stream().filter((i) -> (i.getTo().equals(xmlRootElementName) && (! i.isPrivateEdge() && i.getPreventDelete().equals("IN")))).forEach((i) ->{ preventDelete.add(i.getFrom().toUpperCase());} ); } - footnotes.add(edgeRuleSet.preventDeleteRules(xmlRootElementName)); - sbEdge.append(footnotes.toString()); - validEdges = sbEdge.toString(); + } catch(Exception e) { + logger.debug("xmlRootElementName: "+xmlRootElementName+"\n"+e); + } + if(preventDelete.size() > 0) { + prevent = xmlRootElementName.toUpperCase()+" cannot be deleted if related to "+String.join(",",preventDelete); + logger.debug(prevent); } + if(StringUtils.isNotEmpty(prevent)) { + footnotes.add(prevent); + } + if(footnotes.footnotes.size() > 0) { + sbEdge.append(footnotes.toString()); + } + validEdges = sbEdge.toString(); + // Handle description property. Might have a description OR valid edges OR both OR neither. // Only put a description: tag if there is at least one. - if (pathDescriptionProperty != null || validEdges != null) { + if (StringUtils.isNotEmpty(pathDescriptionProperty) || StringUtils.isNotEmpty(validEdges) ) { definitionsSb.append(" description: |\n"); definitionsLocalSb.append(" description: |\n"); @@ -407,10 +479,8 @@ public class YAMLfromOXM extends OxmFileProcessor { definitionsSb.append(" " + pathDescriptionProperty + "\n" ); definitionsLocalSb.append(" " + pathDescriptionProperty + "\n" ); } - if (validEdges != null) { - definitionsSb.append(validEdges); - definitionsLocalSb.append(validEdges); - } + definitionsSb.append(validEdges); + definitionsLocalSb.append(validEdges); } if ( requiredCnt > 0 ) { @@ -421,22 +491,43 @@ public class YAMLfromOXM extends OxmFileProcessor { if ( propertyCnt > 0 ) { definitionsSb.append(" properties:\n"); definitionsSb.append(sbProperties); - definitionsLocalSb.append(" properties:\n"); + if ( !processingInventoryDef) { + definitionsLocalSb.append(" properties:\n"); + } definitionsLocalSb.append(sbProperties); } try { - javaTypeDefinitions.put(xmlRootElementName, definitionsLocalSb.toString()); + namespaceFilter.add(xmlRootElementName); + if ( xmlRootElementName.equals("inventory") ) { + //will add to javaTypeDefinitions at end + inventoryDefSb.append(definitionsLocalSb.toString()); + } else { + javaTypeDefinitions.put(xmlRootElementName, definitionsLocalSb.toString()); + } } catch (Exception e) { e.printStackTrace(); } + if ( xmlRootElementName.equals("inventory") ) { + logger.trace("skip xmlRootElementName(2)="+xmlRootElementName); + return null; + } generatedJavaType.put(xmlRootElementName, null); +/* + if( validTag(javaTypeName) && javaTypeName == useTag && tag == null) { + String nameSpaceResult = getDocumentHeader()+pathSb.toString()+appendDefinitions(namespaceFilter); + writeYAMLfile(javaTypeName, nameSpaceResult); + totalPathSbAccumulator.append(pathSb); + pathSb.delete(0, pathSb.length()); + namespaceFilter.clear(); + } +*/ logger.trace("xmlRootElementName(2)="+xmlRootElementName); return null; } private void writeYAMLfile(String outfileName, String fileContent) { outfileName = (StringUtils.isEmpty(outfileName)) ? "aai_swagger" : outfileName; - outfileName = (outfileName.lastIndexOf(File.separator) == -1) ? yaml_dir + File.separator +outfileName+"_" + v.name() + "." + generateTypeYAML : outfileName; + outfileName = (outfileName.lastIndexOf(File.separator) == -1) ? yaml_dir + File.separator +outfileName+"_" + v.toString() + "." + generateTypeYAML : outfileName; File outfile = new File(outfileName); File parentDir = outfile.getParentFile(); if(parentDir != null && ! parentDir.exists()) @@ -447,12 +538,36 @@ public class YAMLfromOXM extends OxmFileProcessor { logger.error( "Exception creating output file " + outfileName); e.printStackTrace(); } - - try(BufferedWriter bw = Files.newBufferedWriter(Paths.get(outfileName), Charset.forName("UTF-8"))){ + BufferedWriter bw = null; + try { + Charset charset = Charset.forName("UTF-8"); + Path path = Paths.get(outfileName); + bw = Files.newBufferedWriter(path, charset); bw.write(fileContent); + if ( bw != null ) { + bw.close(); + } } catch ( IOException e) { logger.error( "Exception writing output file " + outfileName); e.printStackTrace(); } } + + public boolean validTag(String tag) { + if(tag != null) { + switch ( tag ) { + case "Network": +// case "Search": +// case "Actions": + case "ServiceDesignAndCreation": + case "Business": + case "LicenseManagement": + case "CloudInfrastructure": + case "Common": + return true; + } + } + return false; + } + } diff --git a/aai-core/src/main/java/org/onap/aai/util/swagger/Api.java b/aai-core/src/main/java/org/onap/aai/util/swagger/Api.java index 7d654489..cc422d95 100644 --- a/aai-core/src/main/java/org/onap/aai/util/swagger/Api.java +++ b/aai-core/src/main/java/org/onap/aai/util/swagger/Api.java @@ -220,6 +220,9 @@ public class Api { public boolean isBodyParametersEnabled() { return bodyParametersEnabled; } + public boolean isOpNotPatch() { + return type.equalsIgnoreCase("patch") ? false : true; + } public void setBodyParametersEnabled(boolean bodyParametersEnabled) { this.bodyParametersEnabled = bodyParametersEnabled; diff --git a/aai-core/src/main/java/org/onap/aai/util/swagger/GenerateSwagger.java b/aai-core/src/main/java/org/onap/aai/util/swagger/GenerateSwagger.java index 7fdc8059..21e264a1 100644 --- a/aai-core/src/main/java/org/onap/aai/util/swagger/GenerateSwagger.java +++ b/aai-core/src/main/java/org/onap/aai/util/swagger/GenerateSwagger.java @@ -24,7 +24,8 @@ import com.fasterxml.jackson.dataformat.yaml.snakeyaml.constructor.SafeConstruct import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; -import org.onap.aai.introspection.Version; + +import org.onap.aai.setup.SchemaVersions; import java.io.*; import java.util.*; @@ -36,18 +37,31 @@ public class GenerateSwagger { public static final String DEFAULT_WIKI = ""; public static final String DEFAULT_SCHEMA_DIR = "../aai-schema"; - public static final String CURRENT_VERSION = Version.getLatest().toString(); //if the program is run from aai-common, use this directory as default" public static final String ALT_SCHEMA_DIR = "aai-schema"; //used to check to see if program is run from aai-core public static final String DEFAULT_RUN_DIR = "aai-core"; - public static void main(String[] args) throws IOException, TemplateException { + public static SchemaVersions schemaVersions; + + public SchemaVersions getSchemaVersions() { + return schemaVersions; + } + + + + public static void main(String[] args) throws IOException, TemplateException { + + + + // SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); + String CURRENT_VERSION = schemaVersions.getDefaultVersion().toString(); String schemaDir = System.getProperty("aai.schema.dir"); String versionToGenerate = System.getProperty("aai.generate.version"); String wikiLink = System.getProperty("aai.wiki.link"); - + String release = System.getProperty("aai.release", "onap"); + if(schemaDir == null){ if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(DEFAULT_RUN_DIR)) { System.out.println("Warning: Schema directory is not set so using default schema dir: " + ALT_SCHEMA_DIR); @@ -69,7 +83,7 @@ public class GenerateSwagger { wikiLink = DEFAULT_WIKI; } - String yamlFile = schemaDir + "/src/main/resources/aai_swagger_yaml/aai_swagger_" + versionToGenerate + ".yaml"; + String yamlFile = schemaDir + "/src/main/resources/" + release + "/aai_swagger_yaml/aai_swagger_" + versionToGenerate + ".yaml"; File swaggerYamlFile = new File(yamlFile); if(!swaggerYamlFile.exists()){ @@ -82,7 +96,7 @@ public class GenerateSwagger { try (BufferedReader reader = new BufferedReader(new FileReader(swaggerYamlFile))){ swaggerMap = (Map<String, Object>) yaml.load(reader); - } catch(IOException ex){ + } catch(Exception ex){ ex.printStackTrace(); } @@ -158,7 +172,7 @@ public class GenerateSwagger { } Template template = configuration.getTemplate("swagger.html.ftl"); - String outputDirStr = schemaDir + "/src/main/resources/aai_swagger_html"; + String outputDirStr = schemaDir + "/src/main/resources/" + release + "/aai_swagger_html"; File outputDir = new File(outputDirStr); @@ -343,7 +357,7 @@ public class GenerateSwagger { .forEach((entry) -> { Definition definition = new Definition(); - String key = entry.getKey(); + String key = entry.getKey(); Map<String, Object> valueMap = (Map<String, Object>) entry.getValue(); definition.setDefinitionName(key); @@ -471,3 +485,4 @@ public class GenerateSwagger { } } + diff --git a/aai-core/src/main/java/org/onap/aai/config/DmaapConfig.java b/aai-core/src/main/java/org/onap/aai/web/DmaapConfig.java index b2b5bccd..657656c4 100644 --- a/aai-core/src/main/java/org/onap/aai/config/DmaapConfig.java +++ b/aai-core/src/main/java/org/onap/aai/web/DmaapConfig.java @@ -17,7 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.aai.config; +package org.onap.aai.web; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.broker.BrokerService; diff --git a/aai-core/src/main/java/org/onap/aai/config/EventClientPublisher.java b/aai-core/src/main/java/org/onap/aai/web/EventClientPublisher.java index 9567dd87..ac897273 100644 --- a/aai-core/src/main/java/org/onap/aai/config/EventClientPublisher.java +++ b/aai-core/src/main/java/org/onap/aai/web/EventClientPublisher.java @@ -17,7 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.aai.config; +package org.onap.aai.web; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; diff --git a/aai-core/src/main/java/org/onap/aai/workarounds/LegacyURITransformer.java b/aai-core/src/main/java/org/onap/aai/workarounds/LegacyURITransformer.java deleted file mode 100644 index b36d3e99..00000000 --- a/aai-core/src/main/java/org/onap/aai/workarounds/LegacyURITransformer.java +++ /dev/null @@ -1,92 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.workarounds; - -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class LegacyURITransformer { - - private final Pattern legacyUriPattern = Pattern.compile("/aai/servers/(?<version>v[23])/(?<tenantKey>.*?)/vservers/(?<vserverKey>[^/]*?$)"); - private final Pattern currentUriPattern = Pattern.compile("/aai/(?<version>v\\d+)/cloud-infrastructure/tenants/tenant/(?<tenantKey>.*?)/vservers/vserver/(?<vserverKey>[^/]*?$)"); - /** - * Instantiates a new legacy URL transformer. - */ - private LegacyURITransformer() { - - } - - private static class Helper { - private static final LegacyURITransformer INSTANCE = new LegacyURITransformer(); - } - - /** - * Gets the single instance of LegacyURLTransformer. - * - * @return single instance of LegacyURLTransformer - */ - public static LegacyURITransformer getInstance() { - return Helper.INSTANCE; - } - - /** - * Gets the legacy URL. - * - * @param url the url - * @return the legacy URL - * @throws URISyntaxException - * @throws MalformedURLException the malformed URL exception - */ - public URI getLegacyURI(URI url) throws URISyntaxException { - String replacement = "/aai/servers/${version}/${tenantKey}/vservers/${vserverKey}"; - String result = url.toString(); - Matcher m = currentUriPattern.matcher(result); - if (m.find()) { - result = m.replaceFirst(replacement); - - } - - return new URI(result); - - } - - /** - * Gets the current URL. - * - * @param url the url - * @return the current URL - * @throws URISyntaxException - * @throws MalformedURLException the malformed URL exception - */ - public URI getCurrentURI(URI url) throws URISyntaxException { - String replacement = "/aai/${version}/cloud-infrastructure/tenants/tenant/${tenantKey}/vservers/vserver/${vserverKey}"; - String result = url.toString(); - Matcher m = legacyUriPattern.matcher(result); - if (m.find()) { - result = m.replaceFirst(replacement); - - } - - return new URI(result); - } -} diff --git a/aai-core/src/main/java/org/onap/aai/workarounds/RemoveDME2QueryParams.java b/aai-core/src/main/java/org/onap/aai/workarounds/RemoveDME2QueryParams.java deleted file mode 100644 index 7d435b2c..00000000 --- a/aai-core/src/main/java/org/onap/aai/workarounds/RemoveDME2QueryParams.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-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.aai.workarounds; - -import javax.ws.rs.core.MultivaluedMap; - -public class RemoveDME2QueryParams { - - private final String[] blacklist = {"version", "envContext", "routeOffer"}; - - - /** - * Should remove query params. - * - * @param params the params - * @return true, if successful - */ - public boolean shouldRemoveQueryParams(MultivaluedMap<String, String> params) { - boolean remove = true; - - for (String param : blacklist) { - if (!params.containsKey(param)) { - remove = false; - break; - } - } - - return remove; - - } - - /** - * Removes the query params. - * - * @param params the params - */ - public void removeQueryParams(MultivaluedMap<String, String> params) { - - - for (String param : blacklist) { - params.remove(param); - } - - - } - -} |