From 5397f3a9420c685745a4f7057a21271ef44fda22 Mon Sep 17 00:00:00 2001 From: rama-huawei Date: Thu, 28 Sep 2017 17:01:41 +0530 Subject: [PATCH] Replaced with Map return type instead HashMap Replaced with diamond symbol on RHS side Removed useless assignment to variables Moved string literal on the left side. Issue-id: AAI-409 Change-Id: I699efafc15e1c08652f1a60ddbf524d07983885d Signed-off-by: rama-huawei --- .../util/SearchSuggestionPermutation.java | 126 ++--- .../datarouter/util/VersionedOxmEntities.java | 547 ++++++++++----------- .../datarouter/util/client/NoAuthRestClient.java | 368 +++++++------- 3 files changed, 518 insertions(+), 523 deletions(-) diff --git a/src/main/java/org/openecomp/datarouter/util/SearchSuggestionPermutation.java b/src/main/java/org/openecomp/datarouter/util/SearchSuggestionPermutation.java index fbf7c5b..0a72ce5 100644 --- a/src/main/java/org/openecomp/datarouter/util/SearchSuggestionPermutation.java +++ b/src/main/java/org/openecomp/datarouter/util/SearchSuggestionPermutation.java @@ -8,16 +8,16 @@ * 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.openecomp.datarouter.util; @@ -26,73 +26,73 @@ import java.util.ArrayList; import java.util.List; public class SearchSuggestionPermutation { - /* - * Will return all the unique combinations of the suggestions provided. - * The order of the permutation is not taken into account when computing - * the uniqueness. - * e.g.: A list of A,B,C,D will return - * [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]] - * - * @param list The list of statuses to create permutations of - * @return A list which contains a array list of all possible combinations - */ - public static ArrayList> getUniqueListForSuggestions( - List originalList) { - ArrayList> lists = new ArrayList>(); - if (originalList.isEmpty()) { - lists.add(new ArrayList()); - return lists; - } - List list = new ArrayList(originalList); - String head = list.get(0); - ArrayList rest = new ArrayList(list.subList(1, list.size())); - - for (ArrayList activeList : getUniqueListForSuggestions(rest)) { - ArrayList newList = new ArrayList(); - newList.add(head); - newList.addAll(activeList); - lists.add(newList); - lists.add(activeList); - } - return lists; - } - - public static ArrayList> getNonEmptyUniqueLists(List list){ - ArrayList> lists = getUniqueListForSuggestions(list); - // remove empty list from the power set - for (ArrayList emptyList : lists ){ - if ( emptyList.isEmpty() ) { - lists.remove(emptyList); - break; - } + /* + * Will return all the unique combinations of the suggestions provided. + * The order of the permutation is not taken into account when computing + * the uniqueness. + * e.g.: A list of A,B,C,D will return + * [[A], [A, B, C, D], [A, C, D], [A, D], [B], [B, C, D], [B, D], [C], [C, D], [D]] + * + * @param list The list of statuses to create permutations of + * @return A list which contains a array list of all possible combinations + */ + public static ArrayList> getUniqueListForSuggestions( + List originalList) { + ArrayList> lists = new ArrayList<>(); + if (originalList.isEmpty()) { + lists.add(new ArrayList()); + return lists; + } + List list = new ArrayList<>(originalList); + String head = list.get(0); + ArrayList rest = new ArrayList<>(list.subList(1, list.size())); + + for (ArrayList activeList : getUniqueListForSuggestions(rest)) { + ArrayList newList = new ArrayList<>(); + newList.add(head); + newList.addAll(activeList); + lists.add(newList); + lists.add(activeList); + } + return lists; } - return lists; - } - public static List> getListPermutations(List list) { - List inputList = new ArrayList(); - inputList.addAll(list); - if (inputList.size() == 0) { - List> result = new ArrayList>(); - result.add(new ArrayList()); - return result; + public static ArrayList> getNonEmptyUniqueLists(List list) { + ArrayList> lists = getUniqueListForSuggestions(list); + // remove empty list from the power set + for (ArrayList emptyList : lists) { + if (emptyList.isEmpty()) { + lists.remove(emptyList); + break; + } + } + return lists; } - List> listOfLists = new ArrayList>(); + public static List> getListPermutations(List list) { + List inputList = new ArrayList<>(); + inputList.addAll(list); + if (inputList.isEmpty()) { + List> result = new ArrayList<>(); + result.add(new ArrayList()); + return result; + } + + List> listOfLists = new ArrayList<>(); - String firstElement = inputList.remove(0); + String firstElement = inputList.remove(0); - List> recursiveReturn = getListPermutations(inputList); - for (List li : recursiveReturn) { + List> recursiveReturn = getListPermutations(inputList); + for (List li : recursiveReturn) { - for (int index = 0; index <= li.size(); index++) { - List temp = new ArrayList(li); - temp.add(index, firstElement); - listOfLists.add(temp); - } + for (int index = 0; index <= li.size(); index++) { + List temp = new ArrayList<>(li); + temp.add(index, firstElement); + listOfLists.add(temp); + } + } + return listOfLists; } - return listOfLists; - } } diff --git a/src/main/java/org/openecomp/datarouter/util/VersionedOxmEntities.java b/src/main/java/org/openecomp/datarouter/util/VersionedOxmEntities.java index 3c3150d..1c993bb 100644 --- a/src/main/java/org/openecomp/datarouter/util/VersionedOxmEntities.java +++ b/src/main/java/org/openecomp/datarouter/util/VersionedOxmEntities.java @@ -8,20 +8,29 @@ * 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.openecomp.datarouter.util; +import org.eclipse.persistence.dynamic.DynamicType; +import org.eclipse.persistence.internal.helper.DatabaseField; +import org.eclipse.persistence.internal.oxm.XPathFragment; +import org.eclipse.persistence.internal.oxm.mappings.Descriptor; +import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; +import org.eclipse.persistence.mappings.DatabaseMapping; +import org.eclipse.persistence.oxm.XMLField; +import org.onap.aai.datarouter.entity.OxmEntityDescriptor; + import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -33,97 +42,85 @@ import java.util.Map; import java.util.Set; import java.util.Vector; -import org.eclipse.persistence.dynamic.DynamicType; -import org.eclipse.persistence.internal.helper.DatabaseField; -import org.eclipse.persistence.internal.oxm.XPathFragment; -import org.eclipse.persistence.internal.oxm.mappings.Descriptor; -import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext; -import org.eclipse.persistence.mappings.DatabaseMapping; -import org.eclipse.persistence.oxm.XMLField; -import org.onap.aai.datarouter.entity.OxmEntityDescriptor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Builds up a representation of the versioned entities in a way that they can be cross referenced * in a data-driven way + * * @author DAVEA */ public class VersionedOxmEntities { - private static final Logger logger = LoggerFactory.getLogger(VersionedOxmEntities.class); - private static final String REST_ROOT_ENTITY = "inventory"; - - private HashMap crossEntityReferenceContainerLookup = new HashMap(); - private HashMap crossEntityReferenceLookup = new HashMap(); - private Map entityTypeLookup = new LinkedHashMap(); - private Map searchableEntityDescriptors = new HashMap(); - private Map suggestableEntityDescriptors = new HashMap(); - private Map entityAliasDescriptors = new HashMap(); - - - public void initialize(DynamicJAXBContext context) { - parseOxmContext(context); - buildCrossEntityReferenceCollections(REST_ROOT_ENTITY, new HashSet()); - populateSearchableDescriptors(context); - } - - /** - * The big goal for these methods is to make the processing as generic and model driven as possible. There are only two - * exceptions to this rule, at the moment. I needed to hard-coded the top level REST data model entity type, which is - * "inventory" for now. And as this class is heavily focused and coupled towards building a version specific set of - * lookup structures for the "crossEntityReference" model attribute, it possesses knowledge of that attribute whether it - * exists or not in the DynamicJAXBContext we are currently analyzing. - * - * This method will build two collections: - * - * 1) A list of entity types that can have nested entities containing cross entity reference definitions. The purpose - * of this collection is a fail-fast test when processing UEB events so we can quickly determine if it is necessary - * to deeply parse the event looking for cross entity reference attributes which not exist. - * - * For example, looking at a service-instance <=> inventory path: - * - * inventory (true) - * -> business (true) - * -> customers (true) - * -> customer (true) - * -> service-subscriptions (true) - * -> service-subscription (CER defined here in the model) (true) - * -> service-instances (false) - * -> service-instance (false) - * - * Because service-subscription contains a model definition of CER, in the first collection all the types in the tree will - * indicate that it possesses one or more contained entity types with a cross-entity-reference definition. - * - * 2) A lookup for { entityType => CrossEntityReference } so we can quickly access the model definition of a CER for - * a specific entity type when we begin extracting parent attributes for transposition into nested child entity types. - * - * - * @param entityType - * @param checked - * @return - */ - protected boolean buildCrossEntityReferenceCollections(String entityType, HashSet checked) { + private static final String REST_ROOT_ENTITY = "inventory"; + + private HashMap crossEntityReferenceContainerLookup = new HashMap<>(); + private HashMap crossEntityReferenceLookup = new HashMap<>(); + private Map entityTypeLookup = new LinkedHashMap<>(); + private Map searchableEntityDescriptors = new HashMap<>(); + private Map suggestableEntityDescriptors = new HashMap<>(); + private Map entityAliasDescriptors = new HashMap<>(); + + + public void initialize(DynamicJAXBContext context) { + parseOxmContext(context); + buildCrossEntityReferenceCollections(REST_ROOT_ENTITY, new HashSet()); + populateSearchableDescriptors(context); + } + + /** + * The big goal for these methods is to make the processing as generic and model driven as possible. + * There are only two exceptions to this rule, at the moment. I needed to hard-coded the top level REST data + * model entity type, which is "inventory" for now. And as this class is heavily focused and coupled towards + * building a version specific set of lookup structures for the "crossEntityReference" model attribute, it possesses + * knowledge of that attribute whether it exists or not in the DynamicJAXBContext we are currently analyzing. + *

+ * This method will build two collections: + *

+ * 1) A list of entity types that can have nested entities containing cross entity reference definitions. + * The purpose of this collection is a fail-fast test when processing UEB events so we can quickly determine if + * it is necessary to deeply parse the event looking for cross entity reference attributes which not exist. + *

+ * For example, looking at a service-instance <=> inventory path: + *

+ * inventory (true) + * -> business (true) + * -> customers (true) + * -> customer (true) + * -> service-subscriptions (true) + * -> service-subscription (CER defined here in the model) (true) + * -> service-instances (false) + * -> service-instance (false) + *

+ * Because service-subscription contains a model definition of CER, in the first collection all the types in the + * tree will indicate that it possesses one or more contained entity types with a cross-entity-reference definition. + *

+ * 2) A lookup for { entityType => CrossEntityReference } so we can quickly access the model definition of a CER + * for a specific entity type when we begin extracting parent attributes for transposition into nested child entity + * types. + * + * @param entityType + * @param checked + * @return + */ + protected boolean buildCrossEntityReferenceCollections(String entityType, HashSet checked) { /* * To short-circuit infinite loops, make sure this entityType hasn't * already been checked */ - if(checked.contains(entityType)) { - return false; - } - else { - checked.add(entityType); - } + if (checked.contains(entityType)) { + return false; + } else { + checked.add(entityType); + } - DynamicType parentType = entityTypeLookup.get(entityType); - DynamicType childType = null; - boolean returnValue = false; + DynamicType parentType = entityTypeLookup.get(entityType); + DynamicType childType; + boolean returnValue = false; - if(parentType == null) { - return returnValue; - } + if (parentType == null) { + return returnValue; + } /* * Check if current descriptor contains the cross-entity-reference @@ -131,220 +128,220 @@ public class VersionedOxmEntities { * entity types that may contain the reference. */ - Map properties = parentType.getDescriptor().getProperties(); - if(properties != null) { - for(Map.Entry entry : properties.entrySet()) { - if(entry.getKey().equalsIgnoreCase("crossEntityReference")) { - returnValue = true; - CrossEntityReference cer = new CrossEntityReference(); - cer.initialize(entry.getValue()); - crossEntityReferenceLookup.put( entityType, cer); - //System.out.println("entityType = " + entityType + " contains a CER instance = " + returnValue); - // return true; + Map properties = parentType.getDescriptor().getProperties(); + if (properties != null) { + for (Map.Entry entry : properties.entrySet()) { + if ("crossEntityReference".equalsIgnoreCase(entry.getKey())) { + returnValue = true; + CrossEntityReference cer = new CrossEntityReference(); + cer.initialize(entry.getValue()); + crossEntityReferenceLookup.put(entityType, cer); + //System.out.println("entityType = " + entityType + " contains a CER instance = " + returnValue); + // return true; + } } - } - } + } - Vector fields = parentType.getDescriptor().getAllFields(); + Vector fields = parentType.getDescriptor().getAllFields(); - if(fields != null) { + if (fields != null) { - XMLField xmlField = null; - for(DatabaseField f : fields) { + XMLField xmlField; + for (DatabaseField f : fields) { - if(f instanceof XMLField) { - xmlField = (XMLField)f; - XPathFragment xpathFragment = xmlField.getXPathFragment(); - String entityShortName = xpathFragment.getLocalName(); + if (f instanceof XMLField) { + xmlField = (XMLField) f; + XPathFragment xpathFragment = xmlField.getXPathFragment(); + String entityShortName = xpathFragment.getLocalName(); - childType = entityTypeLookup.get(entityShortName); + childType = entityTypeLookup.get(entityShortName); - if(childType != null) { + if (childType != null) { - if(!checked.contains(entityShortName)) { + if (!checked.contains(entityShortName)) { - if(buildCrossEntityReferenceCollections(entityShortName,checked)) { - returnValue = true; - } + if (buildCrossEntityReferenceCollections(entityShortName, checked)) { + returnValue = true; + } - } + } - checked.add(entityShortName); + checked.add(entityShortName); - } + } + + } } - } - - } - - crossEntityReferenceContainerLookup.put(entityType, Boolean.valueOf(returnValue)); - return returnValue; - } - - private void populateSearchableDescriptors(DynamicJAXBContext oxmContext) { - List descriptorsList = oxmContext.getXMLContext().getDescriptors(); - OxmEntityDescriptor newOxmEntity = null; - - for (Descriptor desc: descriptorsList) { - - DynamicType entity = (DynamicType) oxmContext.getDynamicType(desc.getAlias()); - - //LinkedHashMap oxmProperties = new LinkedHashMap(); - String primaryKeyAttributeNames = null; - - //Not all fields have key attributes - if (desc.getPrimaryKeyFields() != null) { - primaryKeyAttributeNames = desc.getPrimaryKeyFields().toString().replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""); - } - - String entityName = desc.getDefaultRootElement(); - - Map properties = entity.getDescriptor().getProperties(); - if (properties != null) { - for (Map.Entry entry : properties.entrySet()) { - if (entry.getKey().equalsIgnoreCase("searchable")) { + } + + crossEntityReferenceContainerLookup.put(entityType, Boolean.valueOf(returnValue)); + return returnValue; + } + + private void populateSearchableDescriptors(DynamicJAXBContext oxmContext) { + List descriptorsList = oxmContext.getXMLContext().getDescriptors(); + OxmEntityDescriptor newOxmEntity; + + for (Descriptor desc : descriptorsList) { + + DynamicType entity = (DynamicType) oxmContext.getDynamicType(desc.getAlias()); + + //LinkedHashMap oxmProperties = new LinkedHashMap(); + String primaryKeyAttributeNames = null; + + //Not all fields have key attributes + if (desc.getPrimaryKeyFields() != null) { + primaryKeyAttributeNames = desc.getPrimaryKeyFields() + .toString().replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""); + } + + String entityName = desc.getDefaultRootElement(); + + Map properties = entity.getDescriptor().getProperties(); + if (properties != null) { + for (Map.Entry entry : properties.entrySet()) { + if ("searchable".equalsIgnoreCase(entry.getKey())) { /* * we can do all the work here, we don't have a create additional collections for * subsequent passes */ - newOxmEntity = new OxmEntityDescriptor(); - newOxmEntity.setEntityName(entityName); - newOxmEntity.setPrimaryKeyAttributeName(Arrays.asList(primaryKeyAttributeNames.split(","))); - newOxmEntity.setSearchableAttributes(Arrays.asList(entry.getValue().split(","))); - searchableEntityDescriptors.put(entityName, newOxmEntity); - } else if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) { - newOxmEntity = new OxmEntityDescriptor(); - newOxmEntity.setEntityName(entityName); - newOxmEntity.setSuggestableEntity(true); - Vector descriptorMaps = entity.getDescriptor().getMappings(); - List listOfSuggestableAttributes = new ArrayList(); - - for (DatabaseMapping descMap : descriptorMaps) { - if (descMap.isAbstractDirectMapping()) { - - if (descMap.getProperties().get("suggestibleOnSearch") != null) { - String suggestableOnSearchString = String.valueOf( - descMap.getProperties().get("suggestibleOnSearch")); - - boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString); - - if (isSuggestibleOnSearch) { + newOxmEntity = new OxmEntityDescriptor(); + newOxmEntity.setEntityName(entityName); + newOxmEntity + .setPrimaryKeyAttributeName(Arrays.asList(primaryKeyAttributeNames.split(","))); + newOxmEntity.setSearchableAttributes(Arrays.asList(entry.getValue().split(","))); + searchableEntityDescriptors.put(entityName, newOxmEntity); + } else if ("containsSuggestibleProps".equalsIgnoreCase(entry.getKey())) { + newOxmEntity = new OxmEntityDescriptor(); + newOxmEntity.setEntityName(entityName); + newOxmEntity.setSuggestableEntity(true); + Vector descriptorMaps = entity.getDescriptor().getMappings(); + List listOfSuggestableAttributes = new ArrayList<>(); + + for (DatabaseMapping descMap : descriptorMaps) { + if (descMap.isAbstractDirectMapping()) { + + if (descMap.getProperties().get("suggestibleOnSearch") != null) { + String suggestableOnSearchString = String.valueOf( + descMap.getProperties().get("suggestibleOnSearch")); + + boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString); + + if (isSuggestibleOnSearch) { /* Grab attribute types for suggestion */ - String attributeName = descMap.getField().getName() - .replaceAll("/text\\(\\)", ""); - listOfSuggestableAttributes.add(attributeName); - } - } - } - } - newOxmEntity.setSuggestableAttributes(listOfSuggestableAttributes); - suggestableEntityDescriptors.put(entityName, newOxmEntity); - } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) { - newOxmEntity = new OxmEntityDescriptor(); - newOxmEntity.setEntityName(entityName); - newOxmEntity.setAlias(Arrays.asList(entry.getValue().split(","))); - entityAliasDescriptors.put(entityName, newOxmEntity); - } + String attributeName = descMap.getField().getName() + .replaceAll("/text\\(\\)", ""); + listOfSuggestableAttributes.add(attributeName); + } + } + } + } + newOxmEntity.setSuggestableAttributes(listOfSuggestableAttributes); + suggestableEntityDescriptors.put(entityName, newOxmEntity); + } else if ("suggestionAliases".equalsIgnoreCase(entry.getKey())) { + newOxmEntity = new OxmEntityDescriptor(); + newOxmEntity.setEntityName(entityName); + newOxmEntity.setAlias(Arrays.asList(entry.getValue().split(","))); + entityAliasDescriptors.put(entityName, newOxmEntity); + } + } + } + + } + + } + + public Map getSearchableEntityDescriptors() { + return searchableEntityDescriptors; + } + + public OxmEntityDescriptor getSearchableEntityDescriptor(String entityType) { + return searchableEntityDescriptors.get(entityType); + } + + + public HashMap getCrossEntityReferenceContainers() { + return crossEntityReferenceContainerLookup; + } + + public HashMap getCrossEntityReferences() { + return crossEntityReferenceLookup; + } + + + private void parseOxmContext(DynamicJAXBContext oxmContext) { + List descriptorsList = oxmContext.getXMLContext().getDescriptors(); + + for (Descriptor desc : descriptorsList) { + + DynamicType entity = (DynamicType) oxmContext.getDynamicType(desc.getAlias()); + + String entityName = desc.getDefaultRootElement(); + + entityTypeLookup.put(entityName, entity); + + } + + } + + public boolean entityModelContainsCrossEntityReference(String containerEntityType) { + Boolean v = crossEntityReferenceContainerLookup.get(containerEntityType); + + if (v == null) { + return false; + } + + return v; + } + + public boolean entityContainsCrossEntityReference(String entityType) { + return crossEntityReferenceLookup.get(entityType) != null; + } + + public CrossEntityReference getCrossEntityReference(String entityType) { + return crossEntityReferenceLookup.get(entityType); + } + + public Map getSuggestableEntityDescriptors() { + return suggestableEntityDescriptors; + } + + public void setSuggestableEntityDescriptors( + Map suggestableEntityDescriptors) { + this.suggestableEntityDescriptors = suggestableEntityDescriptors; + } + + public Map getEntityAliasDescriptors() { + return entityAliasDescriptors; + } + + public void setEntityAliasDescriptors(Map entityAliasDescriptors) { + this.entityAliasDescriptors = entityAliasDescriptors; + } + + public void extractEntities(String entityType, DynamicJAXBContext context, Collection entities) { + + + } + + public String dumpCrossEntityReferenceContainers() { + + Set keys = crossEntityReferenceContainerLookup.keySet(); + StringBuilder sb = new StringBuilder(128); + + for (String key : keys) { + + if (crossEntityReferenceContainerLookup.get(key)) { + sb.append("\n").append("Entity-Type = '" + key + "' contains a Cross-Entity-Reference."); } - } - - } - - } - - public Map getSearchableEntityDescriptors() { - return searchableEntityDescriptors; - } - - public OxmEntityDescriptor getSearchableEntityDescriptor(String entityType) { - return searchableEntityDescriptors.get(entityType); - } - - - public HashMap getCrossEntityReferenceContainers() { - return crossEntityReferenceContainerLookup; - } - - public HashMap getCrossEntityReferences() { - return crossEntityReferenceLookup; - } - - - private void parseOxmContext(DynamicJAXBContext oxmContext) { - List descriptorsList = oxmContext.getXMLContext().getDescriptors(); - - for(Descriptor desc : descriptorsList) { - - DynamicType entity = (DynamicType)oxmContext.getDynamicType(desc.getAlias()); - - String entityName = desc.getDefaultRootElement(); - - entityTypeLookup.put(entityName, entity); - - } - - } - - public boolean entityModelContainsCrossEntityReference(String containerEntityType) { - Boolean v = crossEntityReferenceContainerLookup.get(containerEntityType); - - if(v == null) { - return false; - } - - return v; - } - - public boolean entityContainsCrossEntityReference(String entityType) { - return (crossEntityReferenceLookup.get(entityType) != null); - } - - public CrossEntityReference getCrossEntityReference(String entityType) { - return crossEntityReferenceLookup.get(entityType); - } - - public Map getSuggestableEntityDescriptors() { - return suggestableEntityDescriptors; - } - - public void setSuggestableEntityDescriptors( - Map suggestableEntityDescriptors) { - this.suggestableEntityDescriptors = suggestableEntityDescriptors; - } - - public Map getEntityAliasDescriptors() { - return entityAliasDescriptors; - } - - public void setEntityAliasDescriptors(Map entityAliasDescriptors) { - this.entityAliasDescriptors = entityAliasDescriptors; - } - - public void extractEntities(String entityType, DynamicJAXBContext context, Collection entities) { - - - - - } - - public String dumpCrossEntityReferenceContainers() { - - Set keys = crossEntityReferenceContainerLookup.keySet(); - StringBuilder sb = new StringBuilder(128); - - for ( String key : keys ) { - - if ( crossEntityReferenceContainerLookup.get(key) ) { - sb.append("\n").append("Entity-Type = '" + key + "' contains a Cross-Entity-Reference."); - } - } - - - return sb.toString(); - - } - -} \ No newline at end of file + } + + + return sb.toString(); + + } + +} diff --git a/src/main/java/org/openecomp/datarouter/util/client/NoAuthRestClient.java b/src/main/java/org/openecomp/datarouter/util/client/NoAuthRestClient.java index da985fd..b334226 100644 --- a/src/main/java/org/openecomp/datarouter/util/client/NoAuthRestClient.java +++ b/src/main/java/org/openecomp/datarouter/util/client/NoAuthRestClient.java @@ -8,217 +8,215 @@ * 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.openecomp.datarouter.util.client; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import javax.ws.rs.core.MediaType; - +import com.fasterxml.jackson.databind.JsonNode; import org.apache.camel.Exchange; -import org.apache.camel.component.http.HttpMessage; import org.apache.camel.Message; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.cl.mdc.MdcContext; -import org.openecomp.datarouter.logging.DataRouterMsgs; -import org.openecomp.datarouter.util.AaiUiSvcPolicyUtil; -import org.openecomp.datarouter.util.NodeUtils; -import org.openecomp.datarouter.util.RouterServiceUtil; import org.onap.aai.restclient.client.Headers; import org.onap.aai.restclient.client.OperationResult; import org.onap.aai.restclient.client.RestClient; import org.onap.aai.restclient.enums.RestAuthenticationMode; +import org.onap.aai.restclient.rest.HttpUtil; +import org.openecomp.datarouter.logging.DataRouterMsgs; +import org.openecomp.datarouter.util.AaiUiSvcPolicyUtil; +import org.openecomp.datarouter.util.NodeUtils; +import org.openecomp.datarouter.util.RouterServiceUtil; import org.slf4j.MDC; - import org.springframework.http.HttpStatus; -import org.onap.aai.restclient.rest.HttpUtil; -import com.fasterxml.jackson.databind.JsonNode; +import javax.ws.rs.core.MediaType; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class NoAuthRestClient implements SvcRoutingRestClient { - private RestClient restClient; - - private String host; - private String port; - private String originUrl; - private String targetUri; - private JsonNode targetPayload; - private Logger logger; - private Logger auditLogger; - - public NoAuthRestClient(int connectTimeOut, int readTimeOut) { - LoggerFactory loggerFactoryInstance = LoggerFactory.getInstance(); - logger = loggerFactoryInstance.getLogger(NoAuthRestClient.class.getName()); - auditLogger = loggerFactoryInstance.getAuditLogger(NoAuthRestClient.class.getName()); - restClient = new RestClient().authenticationMode(RestAuthenticationMode.HTTP_NOAUTH) - .connectTimeoutMs(connectTimeOut).readTimeoutMs(readTimeOut); - } - - - private OperationResult getResults(String url, JsonNode payload){ - Map> headers = new HashMap<>(); - headers.put(Headers.FROM_APP_ID, Arrays.asList("Synapse")); - headers.put(Headers.TRANSACTION_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID))); - return this.getRestClient().post(url, payload.asText(), headers, MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); - } - - public final void handleRequest (String host, String port, Exchange exchange) throws Exception { - RouterServiceUtil.setMdcContext(exchange); - Message message = exchange.getIn(); - String body = message.getBody(String.class); - OperationResult result = new OperationResult(); - - this.setHost(host); - this.setPort(port); - - this.setOriginUrl(message.getHeader(Exchange.HTTP_URL).toString()); - if (body != null && body.length() != 0) { - JsonNode node = NodeUtils.convertJsonStrToJsonNode(body); - this.setTargetPayload(AaiUiSvcPolicyUtil.getOriginPayload(node)); - this.setTargetUri(AaiUiSvcPolicyUtil.getTargetUri(node)); - } - - if ( this.getTargetPayload() == null || this.getTargetUri() == null){ - logger.error(DataRouterMsgs.INVALID_ORIGIN_PAYLOAD, body); - result.setResultCode(HttpStatus.BAD_REQUEST.value()); - result.setFailureCause("Invalid payload"); + private RestClient restClient; + + private String host; + private String port; + private String originUrl; + private String targetUri; + private JsonNode targetPayload; + private Logger logger; + private Logger auditLogger; + + public NoAuthRestClient(int connectTimeOut, int readTimeOut) { + LoggerFactory loggerFactoryInstance = LoggerFactory.getInstance(); + logger = loggerFactoryInstance.getLogger(NoAuthRestClient.class.getName()); + auditLogger = loggerFactoryInstance.getAuditLogger(NoAuthRestClient.class.getName()); + restClient = new RestClient().authenticationMode(RestAuthenticationMode.HTTP_NOAUTH) + .connectTimeoutMs(connectTimeOut).readTimeoutMs(readTimeOut); + } + + + private OperationResult getResults(String url, JsonNode payload) { + Map> headers = new HashMap<>(); + headers.put(Headers.FROM_APP_ID, Arrays.asList("Synapse")); + headers.put(Headers.TRANSACTION_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID))); + return this.getRestClient().post(url, payload.asText(), + headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); } - - String targetUrl = "http://" + host + ":" + port + "/" + this.targetUri; - auditLogger.info(DataRouterMsgs.ROUTING_FROM_TO, this.getOriginUrl(), targetUrl); - long startTimeInMs = System.currentTimeMillis(); - - result = this.getResults(targetUrl, targetPayload); - - long targetMsOpTime = (System.currentTimeMillis() - startTimeInMs); - auditLogger.info(DataRouterMsgs.OP_TIME, "Target service at "+ targetUrl, String.valueOf(targetMsOpTime)); - - int rc = result.getResultCode(); - String resultStr = ""; - if (HttpUtil.isHttpResponseClassSuccess(rc)) { - resultStr = result.getResult(); - } else { - resultStr = result.getFailureCause(); + + public final void handleRequest(String host, String port, Exchange exchange) throws Exception { + RouterServiceUtil.setMdcContext(exchange); + Message message = exchange.getIn(); + String body = message.getBody(String.class); + OperationResult result = new OperationResult(); + + this.setHost(host); + this.setPort(port); + + this.setOriginUrl(message.getHeader(Exchange.HTTP_URL).toString()); + if (body != null && body.length() != 0) { + JsonNode node = NodeUtils.convertJsonStrToJsonNode(body); + this.setTargetPayload(AaiUiSvcPolicyUtil.getOriginPayload(node)); + this.setTargetUri(AaiUiSvcPolicyUtil.getTargetUri(node)); + } + + if (this.getTargetPayload() == null || this.getTargetUri() == null) { + logger.error(DataRouterMsgs.INVALID_ORIGIN_PAYLOAD, body); + result.setResultCode(HttpStatus.BAD_REQUEST.value()); + result.setFailureCause("Invalid payload"); + } + + String targetUrl = "http://" + host + ":" + port + "/" + this.targetUri; + auditLogger.info(DataRouterMsgs.ROUTING_FROM_TO, this.getOriginUrl(), targetUrl); + long startTimeInMs = System.currentTimeMillis(); + + result = this.getResults(targetUrl, targetPayload); + + long targetMsOpTime = System.currentTimeMillis() - startTimeInMs; + auditLogger.info(DataRouterMsgs.OP_TIME, "Target service at " + targetUrl, String.valueOf(targetMsOpTime)); + + int rc = result.getResultCode(); + String resultStr; + if (HttpUtil.isHttpResponseClassSuccess(rc)) { + resultStr = result.getResult(); + } else { + resultStr = result.getFailureCause(); + } + + logger.debug(DataRouterMsgs.ROUTING_RESPONSE, targetUrl, result.toString()); + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, rc); + exchange.getOut().setBody(resultStr); + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getHost() + */ + @Override + public String getHost() { + return host; + } + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setHost(java.lang.String) + */ + @Override + public void setHost(String host) { + this.host = host; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getPort() + */ + @Override + public String getPort() { + return port; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setPort(java.lang.String) + */ + @Override + public void setPort(String port) { + this.port = port; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getTargetUri() + */ + @Override + public String getTargetUri() { + return targetUri; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setTargetUri(java.lang.String) + */ + @Override + public void setTargetUri(String targetUri) { + this.targetUri = targetUri; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getTargetPayload() + */ + @Override + public JsonNode getTargetPayload() { + return targetPayload; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client + * .SvcRoutingRestClient#setTargetPayload(com.fasterxml.jackson.databind.JsonNode) + */ + @Override + public void setTargetPayload(JsonNode targetPayload) { + this.targetPayload = targetPayload; + } + + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getRestClient() + */ + @Override + public RestClient getRestClient() { + return restClient; + } + + /* (non-Javadoc) + * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setRestClient() + */ + @Override + public void setRestClient(RestClient client) { + this.restClient = client; + } + + + public String getOriginUrl() { + return originUrl; + } + + + public void setOriginUrl(String originUrl) { + this.originUrl = originUrl; } - - logger.debug(DataRouterMsgs.ROUTING_RESPONSE, targetUrl, result.toString()); - exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, rc); - exchange.getOut().setBody(resultStr); - } - - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getHost() - */ - @Override - public String getHost() { - return host; - } - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setHost(java.lang.String) - */ - @Override - public void setHost(String host) { - this.host = host; - } - - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getPort() - */ - @Override - public String getPort() { - return port; - } - - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setPort(java.lang.String) - */ - @Override - public void setPort(String port) { - this.port = port; - } - - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getTargetUri() - */ - @Override - public String getTargetUri() { - return targetUri; - } - - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setTargetUri(java.lang.String) - */ - @Override - public void setTargetUri(String targetUri) { - this.targetUri = targetUri; - } - - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getTargetPayload() - */ - @Override - public JsonNode getTargetPayload() { - return targetPayload; - } - - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setTargetPayload(com.fasterxml.jackson.databind.JsonNode) - */ - @Override - public void setTargetPayload(JsonNode targetPayload) { - this.targetPayload = targetPayload; - } - - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#getRestClient() - */ - @Override - public RestClient getRestClient() { - return restClient; - } - - /* (non-Javadoc) - * @see org.openecomp.datarouter.util.client.SvcRoutingRestClient#setRestClient() - */ - @Override - public void setRestClient(RestClient client) { - this.restClient = client; - } - - - public String getOriginUrl() { - return originUrl; - } - - - public void setOriginUrl(String originUrl) { - this.originUrl = originUrl; - } - } -- 2.16.6