diff options
Diffstat (limited to 'ECOMP-PAP-REST')
16 files changed, 667 insertions, 882 deletions
diff --git a/ECOMP-PAP-REST/Decision_GuardPolicyTemplate.xml b/ECOMP-PAP-REST/Decision_GuardPolicyTemplate.xml new file mode 100644 index 000000000..3a7f25008 --- /dev/null +++ b/ECOMP-PAP-REST/Decision_GuardPolicyTemplate.xml @@ -0,0 +1,99 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<Policy xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17" PolicyId="urn:com:xacml:policy:id:d56af069-6cf1-430c-ba07-e26602e06a52" Version="1" RuleCombiningAlgId="urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides"> + <Description>${description}</Description> + <Target> + <AnyOf> + <AllOf> + <Match MatchId="org.openecomp.function.regex-match"> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">${PolicyName}</AttributeValue> + <AttributeDesignator Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" AttributeId="PolicyName" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> + </Match> + </AllOf> + <AllOf> + <Match MatchId="org.openecomp.function.regex-match"> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">${ECOMPName}</AttributeValue> + <AttributeDesignator Category="urn:oasis:names:tc:xacml:1.0:subject-category:access-subject" AttributeId="ECOMPName" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> + </Match> + <Match MatchId="org.openecomp.function.regex-match"> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">${actor}</AttributeValue> + <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" AttributeId="actor" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> + </Match> + <Match MatchId="org.openecomp.function.regex-match"> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">${recipe}</AttributeValue> + <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" AttributeId="recipe" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> + </Match> + </AllOf> + </AnyOf> + </Target> + <Rule RuleId="urn:com:xacml:rule:id:284d9393-f861-4250-b62d-fc36640a363a" Effect="Permit"> + <Target> + <AnyOf> + <AllOf> + <Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case"> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">DECIDE</AttributeValue> + <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:action" AttributeId="urn:oasis:names:tc:xacml:1.0:action:action-id" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> + </Match> + </AllOf> + </AnyOf> + </Target> + <Condition> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:or"> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:not"> + <Apply FunctionId="urn:oasis:names:tc:xacml:2.0:function:time-in-range"> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:time-one-and-only"> + <AttributeDesignator AttributeId="urn:oasis:names:tc:xacml:1.0:environment:current-time" DataType="http://www.w3.org/2001/XMLSchema#time" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:environment" MustBePresent="false"/> + </Apply> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#time">${guardActiveStart}</AttributeValue> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#time">${guardActiveEnd}</AttributeValue> + </Apply> + </Apply> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:integer-less-than-or-equal"> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:integer-one-and-only"> + <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" AttributeId="count" DataType="http://www.w3.org/2001/XMLSchema#integer" Issuer="org:openecomp:xacml:sql:${timeWindow}" MustBePresent="false"/> + </Apply> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#integer">${limit}</AttributeValue> + </Apply> + </Apply> + </Condition> + </Rule> + <Rule RuleId="urn:com:xacml:rule:id:284d9393-f861-4250-b62d-fc36640a363a" Effect="Deny"> + <Target> + <AnyOf> + <AllOf> + <Match MatchId="urn:oasis:names:tc:xacml:3.0:function:string-equal-ignore-case"> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">DECIDE</AttributeValue> + <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:action" AttributeId="urn:oasis:names:tc:xacml:1.0:action:action-id" DataType="http://www.w3.org/2001/XMLSchema#string" MustBePresent="false"/> + </Match> + </AllOf> + </AnyOf> + </Target> + <Condition> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:not"> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:or"> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:not"> + <Apply FunctionId="urn:oasis:names:tc:xacml:2.0:function:time-in-range"> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:time-one-and-only"> + <AttributeDesignator AttributeId="urn:oasis:names:tc:xacml:1.0:environment:current-time" DataType="http://www.w3.org/2001/XMLSchema#time" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:environment" MustBePresent="false"/> + </Apply> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#time">${guardActiveStart}</AttributeValue> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#time">${guardActiveEnd}</AttributeValue> + </Apply> + </Apply> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:integer-less-than-or-equal"> + <Apply FunctionId="urn:oasis:names:tc:xacml:1.0:function:integer-one-and-only"> + <AttributeDesignator Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource" AttributeId="count" DataType="http://www.w3.org/2001/XMLSchema#integer" Issuer="org:openecomp:xacml:sql:${timeWindow}" MustBePresent="false"/> + </Apply> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#integer">${limit}</AttributeValue> + </Apply> + </Apply> + </Apply> + </Condition> + <AdviceExpressions> + <AdviceExpression AdviceId="GUARD_YAML" AppliesTo="Deny"> + <AttributeAssignmentExpression AttributeId="guard.response" Category="urn:oasis:names:tc:xacml:3.0:attribute-category:resource"> + <AttributeValue DataType="http://www.w3.org/2001/XMLSchema#string">Denied!</AttributeValue> + </AttributeAssignmentExpression> + </AdviceExpression> + </AdviceExpressions> + </Rule> +</Policy>
\ No newline at end of file diff --git a/ECOMP-PAP-REST/pom.xml b/ECOMP-PAP-REST/pom.xml index 5d35edaf6..f05729ebb 100644 --- a/ECOMP-PAP-REST/pom.xml +++ b/ECOMP-PAP-REST/pom.xml @@ -75,6 +75,18 @@ <groupId>org.json</groupId> <artifactId>json</artifactId> </exclusion> + <exclusion> + <groupId>com.att.nsa</groupId> + <artifactId>dmaapClient</artifactId> + </exclusion> + <exclusion> + <groupId>com.att.nsa</groupId> + <artifactId>cambriaClient</artifactId> + </exclusion> + <exclusion> + <groupId>com.att.cadi</groupId> + <artifactId>cadi-aaf</artifactId> + </exclusion> </exclusions> </dependency> <dependency> @@ -125,6 +137,11 @@ <version>1.3.1</version> </dependency> <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <version>2.8.2</version> + </dependency> + <dependency> <groupId>log4j</groupId> <artifactId>apache-log4j-extras</artifactId> <version>1.2.17</version> @@ -192,6 +209,11 @@ <version>2.0.8</version> </dependency> <dependency> + <groupId>org.openecomp.policy.engine</groupId> + <artifactId>ControlloopPolicy</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> <groupId>com.mockrunner</groupId> <artifactId>mockrunner</artifactId> <version>0.3.1</version> diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/DecisionPolicy.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/DecisionPolicy.java index b7b47cf28..4c67d798a 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/DecisionPolicy.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/DecisionPolicy.java @@ -20,18 +20,44 @@ package org.openecomp.policy.pap.xacml.rest.components; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.UUID; import javax.persistence.EntityManager; import javax.persistence.Query; +import org.openecomp.policy.common.logging.eelf.MessageCodes; +import org.openecomp.policy.common.logging.eelf.PolicyLogger; +import org.openecomp.policy.controlloop.policy.builder.BuilderException; +import org.openecomp.policy.controlloop.policy.builder.Results; +import org.openecomp.policy.controlloop.policy.guard.Constraint; +import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard; +import org.openecomp.policy.controlloop.policy.guard.Guard; +import org.openecomp.policy.controlloop.policy.guard.GuardPolicy; +import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder; +import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet; +import org.openecomp.policy.pap.xacml.rest.util.JPAUtils; +import org.openecomp.policy.rest.adapter.PolicyRestAdapter; +import org.openecomp.policy.rest.jpa.Datatype; +import org.openecomp.policy.rest.jpa.DecisionSettings; +import org.openecomp.policy.rest.jpa.FunctionDefinition; +import org.openecomp.policy.xacml.api.XACMLErrorConstants; +import org.openecomp.policy.xacml.std.pip.engines.aaf.AAFEngine; +import org.openecomp.policy.xacml.util.XACMLPolicyScanner; + +import com.att.research.xacml.std.IdentifierImpl; + import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType; @@ -50,22 +76,13 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableDefinitionType; import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType; -import org.openecomp.policy.common.logging.eelf.MessageCodes; -import org.openecomp.policy.common.logging.eelf.PolicyLogger; -import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet; -import org.openecomp.policy.pap.xacml.rest.util.JPAUtils; -import org.openecomp.policy.rest.adapter.PolicyRestAdapter; -import org.openecomp.policy.rest.jpa.Datatype; -import org.openecomp.policy.rest.jpa.DecisionSettings; -import org.openecomp.policy.rest.jpa.FunctionDefinition; -import org.openecomp.policy.xacml.std.pip.engines.aaf.AAFEngine; - -import com.att.research.xacml.std.IdentifierImpl; - public class DecisionPolicy extends Policy { public static final String FUNCTION_NOT = "urn:oasis:names:tc:xacml:1.0:function:not"; private static final String AAFProvider = "AAF"; + public static final String GUARD_YAML = "GUARD_YAML"; + private static final String XACMLTEMPLATE = "Decision_GuardPolicyTemplate.xml"; + List<String> dynamicLabelRuleAlgorithms = new LinkedList<String>(); List<String> dynamicFieldComboRuleAlgorithms = new LinkedList<String>(); @@ -131,7 +148,25 @@ public class DecisionPolicy extends Policy { } policyName = policyAdapter.getNewFileName(); - if (policyAdapter.getData() != null) { + if(policyAdapter.getRuleProvider().equals(GUARD_YAML)){ + Map<String, String> yamlParams = new HashMap<String, String>(); + yamlParams.put("description", (policyAdapter.getPolicyDescription()!=null)? policyAdapter.getPolicyDescription(): "YAML Guard Policy"); + String fileName = policyAdapter.getNewFileName(); + String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length()); + if ((name == null) || (name.equals(""))) { + name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length()); + } + yamlParams.put("PolicyName", name); + yamlParams.put("ECOMPName", policyAdapter.getEcompName()); + Map<String, String> params = policyAdapter.getDynamicFieldConfigAttributes(); + yamlParams.putAll(params); + // Call YAML to XACML + PolicyType decisionPolicy = getGuardPolicy(yamlParams); + decisionPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId()); + decisionPolicy.setVersion(Integer.toString(version)); + policyAdapter.setPolicyData(decisionPolicy); + policyAdapter.setData(decisionPolicy); + }else if (policyAdapter.getData() != null) { PolicyType decisionPolicy = (PolicyType) policyAdapter.getData(); decisionPolicy.setDescription(policyAdapter.getPolicyDescription()); @@ -174,7 +209,7 @@ public class DecisionPolicy extends Policy { Map<String, String> dynamicFieldDecisionSettings = policyAdapter.getDynamicSettingsMap(); //dynamicVariableList = policyAdapter.getDynamicVariableList(); - if(policyAdapter.getProviderComboBox()!=null && policyAdapter.getProviderComboBox().equals(AAFProvider)){ + if(policyAdapter.getRuleProvider()!=null && policyAdapter.getRuleProvider().equals(AAFProvider)){ dynamicFieldDecisionSettings = new HashMap<String,String>(); } @@ -196,6 +231,47 @@ public class DecisionPolicy extends Policy { return true; } + public PolicyType getGuardPolicy(Map<String, String> yamlParams) { + try { + ControlLoopGuardBuilder builder = ControlLoopGuardBuilder.Factory.buildControlLoopGuard(new Guard()); + GuardPolicy policy1 = new GuardPolicy((policyAdapter.getUuid()!=null? policyAdapter.getUuid(): UUID.randomUUID().toString()) ,yamlParams.get("PolicyName"), yamlParams.get("description"), yamlParams.get("actor"), yamlParams.get("recipe")); + builder = builder.addGuardPolicy(policy1); + Map<String, String> time_in_range = new HashMap<String, String>(); + time_in_range.put("arg2", yamlParams.get("guardActiveStart")); + time_in_range.put("arg3", yamlParams.get("guardActiveEnd")); + Constraint cons = new Constraint(Integer.parseInt(yamlParams.get("limit")), yamlParams.get("timeWindow"), time_in_range); + builder = builder.addLimitConstraint(policy1.id, cons); + // Build the specification + Results results = builder.buildSpecification(); + // YAML TO XACML + ControlLoopGuard yamlGuardObject = SafePolicyBuilder.loadYamlGuard(results.getSpecification()); + Path xacmlTemplatePath = Paths.get(XACMLTEMPLATE); + String xacmlTemplateContent; + try { + xacmlTemplateContent = new String(Files.readAllBytes(xacmlTemplatePath)); + HashMap<String, String> yamlSpecs = new HashMap<String, String>(); + yamlSpecs.put("PolicyName", yamlParams.get("PolicyName")); + yamlSpecs.put("description", yamlParams.get("description")); + yamlSpecs.put("ECOMPName", yamlParams.get("ECOMPName")); + yamlSpecs.put("actor", yamlGuardObject.guards.getFirst().actor); + yamlSpecs.put("recipe", yamlGuardObject.guards.getFirst().recipe); + yamlSpecs.put("limit", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().num.toString()); + yamlSpecs.put("timeWindow", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().duration); + yamlSpecs.put("guardActiveStart", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg2")); + yamlSpecs.put("guardActiveEnd", yamlGuardObject.guards.getFirst().limit_constraints.getFirst().time_in_range.get("arg3")); + String xacmlPolicyContent = SafePolicyBuilder.generateXacmlGuard(xacmlTemplateContent,yamlSpecs); + // Convert the Policy into Stream input to Policy Adapter. + Object policy = XACMLPolicyScanner.readPolicy(new ByteArrayInputStream(xacmlPolicyContent.getBytes(StandardCharsets.UTF_8))); + return (PolicyType) policy; + } catch (IOException e) { + PolicyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage()); + } + } catch (BuilderException e) { + PolicyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while creating the policy " + e.getMessage()); + } + return null; + } + private DecisionSettings findDecisionSettingsBySettingId(String settingId) { DecisionSettings decisionSetting = null; diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/SafePolicyBuilder.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/SafePolicyBuilder.java new file mode 100644 index 000000000..7637719b1 --- /dev/null +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/components/SafePolicyBuilder.java @@ -0,0 +1,57 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-PAP-REST + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.openecomp.policy.pap.xacml.rest.components; + +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.openecomp.policy.common.logging.eelf.PolicyLogger; +import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard; +import org.yaml.snakeyaml.Yaml; +import org.yaml.snakeyaml.constructor.Constructor; + +public class SafePolicyBuilder { + + public static ControlLoopGuard loadYamlGuard(String specification) { + // + // Read the yaml into our Java Object + // + PolicyLogger.info("Requested YAML to convert : " + specification); + Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class)); + Object obj = yaml.load(specification); + return (ControlLoopGuard) obj; + } + + public static String generateXacmlGuard(String xacmlFileContent,Map<String, String> generateMap) { + for(String key: generateMap.keySet()){ + Pattern p = Pattern.compile("\\$\\{" +key +"\\}"); + Matcher m = p.matcher(xacmlFileContent); + String finalInput = generateMap.get(key); + if(finalInput.contains("$")){ + finalInput = finalInput.replace("$", "\\$"); + } + xacmlFileContent=m.replaceAll(finalInput); + } + PolicyLogger.info("Generated XACML from the YAML Spec: \n" + xacmlFileContent); + + return xacmlFileContent; + } +} diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/MicroServiceDictionaryController.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/MicroServiceDictionaryController.java index 75a3c7fa0..876f380d4 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/MicroServiceDictionaryController.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/controller/MicroServiceDictionaryController.java @@ -55,6 +55,7 @@ import org.springframework.web.servlet.ModelAndView; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; @Controller public class MicroServiceDictionaryController { @@ -100,7 +101,7 @@ public class MicroServiceDictionaryController { response.getWriter().write(j.toString()); } catch (Exception e){ - e.printStackTrace(); + LOGGER.error(e); } } @@ -225,7 +226,7 @@ public class MicroServiceDictionaryController { return null; } catch (Exception e){ - System.out.println(e); + LOGGER.error(e); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); @@ -246,7 +247,7 @@ public class MicroServiceDictionaryController { response.getWriter().write(j.toString()); } catch (Exception e){ - e.printStackTrace(); + LOGGER.error(e); } } @@ -374,7 +375,7 @@ public class MicroServiceDictionaryController { return null; } catch (Exception e){ - System.out.println(e); + LOGGER.error(e); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); @@ -394,7 +395,7 @@ public class MicroServiceDictionaryController { response.getWriter().write(j.toString()); } catch (Exception e){ - e.printStackTrace(); + LOGGER.error(e); } } @@ -520,7 +521,7 @@ public class MicroServiceDictionaryController { return null; } catch (Exception e){ - System.out.println(e); + LOGGER.error(e); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); @@ -757,7 +758,7 @@ public class MicroServiceDictionaryController { catch (Exception e){ response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.addHeader("error", "dictionaryDBQuery"); - e.printStackTrace(); + LOGGER.error(e); } } @@ -785,74 +786,128 @@ public class MicroServiceDictionaryController { public ModelAndView saveMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{ try { boolean duplicateflag = false; - boolean fromAPI = false; - this.newModel = new MicroServiceModels(); - if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) { - fromAPI = true; - } + boolean fromAPI = false; + this.newModel = new MicroServiceModels(); + if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) { + fromAPI = true; + } ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); - MicroServiceModels microServiceModels = new MicroServiceModels(); - String userId = null; - if (fromAPI) { - microServiceModels = (MicroServiceModels)mapper.readValue(root.get("dictionaryFields").toString(), MicroServiceModels.class); - userId = "API"; - - //check if update operation or create, get id for data to be updated and update attributeData - if (request.getParameter(OPERATION).equals("update")) { - String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion(); - List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class); - int id = 0; - for (int i=0; i< duplicateData.size(); i++){ - MicroServiceModels data = (MicroServiceModels) duplicateData.get(0); - id = data.getId(); - } - microServiceModels.setId(id); - microServiceModels.setUserCreatedBy(this.getUserInfo(userId)); - - } - } else { - //microServiceModels = (MicroServiceModels)mapper.readValue(root.get("microServiceModelsDictionaryData").toString(), MicroServiceModels.class); - if (root.has("microServiceModelsDictionaryData")){ - if (root.get("microServiceModelsDictionaryData").has("description")){ - microServiceModels.setDescription(root.get("microServiceModelsDictionaryData").get("description").asText().replace("\"", "")); - } - if (root.get("microServiceModelsDictionaryData").has("modelName")){ - microServiceModels.setModelName(root.get("microServiceModelsDictionaryData").get("modelName").asText().replace("\"", "")); - this.newModel.setModelName(microServiceModels.getModelName()); - } - if (root.get("microServiceModelsDictionaryData").has("version")){ - microServiceModels.setVersion(root.get("microServiceModelsDictionaryData").get("version").asText().replace("\"", "")); - this.newModel.setVersion(microServiceModels.getVersion()); - } - } - if(root.has("classMap")){ - classMap = new HashMap<String, MSAttributeObject>(); - JsonNode data = root.get("classMap"); - ObjectMapper mapper1 = new ObjectMapper(); - String data1 = data.toString().substring(1, data.toString().length()-1); - data1 = data1.replace("\\", ""); - JSONObject jsonObject = new JSONObject(data1); - Set<String> keys = jsonObject.keySet(); - for(String key : keys){ - String value = jsonObject.get(key).toString(); - MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class); - classMap.put(key, msAttributeObject); - } - } - userId = root.get("userid").textValue(); - addValuesToNewModel(classMap); - } + MicroServiceModels microServiceModels = new MicroServiceModels(); + String userId = null; + + if(root.has("modelType")){ + JsonNode dataType = root.get("modelType"); + String modelType= dataType.toString(); + if(modelType.contains("yml")){ + if (root.has("microServiceModelsDictionaryData")){ + if (root.get("microServiceModelsDictionaryData").has("description")){ + microServiceModels.setDescription(root.get("microServiceModelsDictionaryData").get("description").asText().replace("\"", "")); + } + if (root.get("microServiceModelsDictionaryData").has("modelName")){ + microServiceModels.setModelName(root.get("microServiceModelsDictionaryData").get("modelName").asText().replace("\"", "")); + this.newModel.setModelName(microServiceModels.getModelName()); + } + if (root.get("microServiceModelsDictionaryData").has("version")){ + microServiceModels.setVersion(root.get("microServiceModelsDictionaryData").get("version").asText().replace("\"", "")); + this.newModel.setVersion(microServiceModels.getVersion()); + } + } + + MSAttributeObject mainClass = null; + classMap = new HashMap<String, MSAttributeObject>(); + JsonNode data = root.get("classMap"); + ObjectMapper mapper1 = new ObjectMapper(); + String data1 = data.toString().substring(1, data.toString().length()-1); + data1 = data1.replace("\\", ""); + data1=data1.replace("\"{","{"); + data1=data1.replace("}\"","}"); + JSONObject jsonObject = new JSONObject(data1); + Set<String> keys = jsonObject.keySet(); + for(String key : keys){ + String value = jsonObject.get(key).toString(); + MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class); + classMap.put(key, msAttributeObject); + } + + userId = root.get("userid").textValue(); + mainClass = classMap.get(this.newModel.getModelName()); + this.newModel.setDependency("[]"); + String value = new Gson().toJson(mainClass.getSubClass()); + this.newModel.setSub_attributes(value); + String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", ""); + int equalsIndexForAttributes= attributes.indexOf("="); + String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1); + this.newModel.setAttributes(atttributesAfterFirstEquals); + String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""); + int equalsIndex= refAttributes.indexOf("="); + String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1); + this.newModel.setRef_attributes(refAttributesAfterFirstEquals); + this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", "")); + this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", "")); + + }else{ + if (fromAPI) { + microServiceModels = (MicroServiceModels)mapper.readValue(root.get("dictionaryFields").toString(), MicroServiceModels.class); + userId = "API"; + + //check if update operation or create, get id for data to be updated and update attributeData + if (request.getParameter(OPERATION).equals("update")) { + String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion(); + List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class); + int id = 0; + for (int i=0; i< duplicateData.size(); i++){ + MicroServiceModels data = (MicroServiceModels) duplicateData.get(0); + id = data.getId(); + } + microServiceModels.setId(id); + microServiceModels.setUserCreatedBy(this.getUserInfo(userId)); + + } + } else { + if (root.has("microServiceModelsDictionaryData")){ + if (root.get("microServiceModelsDictionaryData").has("description")){ + microServiceModels.setDescription(root.get("microServiceModelsDictionaryData").get("description").asText().replace("\"", "")); + } + if (root.get("microServiceModelsDictionaryData").has("modelName")){ + microServiceModels.setModelName(root.get("microServiceModelsDictionaryData").get("modelName").asText().replace("\"", "")); + this.newModel.setModelName(microServiceModels.getModelName()); + } + if (root.get("microServiceModelsDictionaryData").has("version")){ + microServiceModels.setVersion(root.get("microServiceModelsDictionaryData").get("version").asText().replace("\"", "")); + this.newModel.setVersion(microServiceModels.getVersion()); + } + } + if(root.has("classMap")){ + classMap = new HashMap<String, MSAttributeObject>(); + JsonNode data = root.get("classMap"); + ObjectMapper mapper1 = new ObjectMapper(); + String data1 = data.toString().substring(1, data.toString().length()-1); + data1 = data1.replace("\\", ""); + JSONObject jsonObject = new JSONObject(data1); + Set<String> keys = jsonObject.keySet(); + for(String key : keys){ + String value = jsonObject.get(key).toString(); + MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class); + classMap.put(key, msAttributeObject); + } + } + userId = root.get("userid").textValue(); + addValuesToNewModel(classMap); + } + } + + } microServiceModels.setAttributes(this.newModel.getAttributes()); microServiceModels.setRef_attributes(this.newModel.getRef_attributes()); microServiceModels.setDependency(this.newModel.getDependency()); microServiceModels.setModelName(this.newModel.getModelName()); microServiceModels.setSub_attributes(this.newModel.getSub_attributes()); microServiceModels.setVersion(this.newModel.getVersion()); - microServiceModels.setEnumValues(this.newModel.getEnumValues()); - microServiceModels.setAnnotation(this.newModel.getAnnotation()); - + microServiceModels.setEnumValues(this.newModel.getEnumValues()); + microServiceModels.setAnnotation(this.newModel.getAnnotation()); + if(microServiceModels.getId() == 0){ String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion(); List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class); @@ -865,31 +920,31 @@ public class MicroServiceDictionaryController { }else{ commonClassDao.update(microServiceModels); } - String responseString = ""; - if(duplicateflag){ - responseString = "Duplicate"; - }else{ - responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class)); - } - - if (fromAPI) { - if (responseString!=null && !responseString.equals("Duplicate")) { - responseString = "Success"; - } - ModelAndView result = new ModelAndView(); - result.setViewName(responseString); - return result; - } else { - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); - - PrintWriter out = response.getWriter(); - JSONObject j = new JSONObject("{microServiceModelsDictionaryDatas: " + responseString + "}"); - out.write(j.toString()); - return null; - } - }catch (Exception e){ + String responseString = ""; + if(duplicateflag){ + responseString = "Duplicate"; + }else{ + responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class)); + } + + if (fromAPI) { + if (responseString!=null && !responseString.equals("Duplicate")) { + responseString = "Success"; + } + ModelAndView result = new ModelAndView(); + result.setViewName(responseString); + return result; + } else { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application / json"); + request.setCharacterEncoding("UTF-8"); + + PrintWriter out = response.getWriter(); + JSONObject j = new JSONObject("{microServiceModelsDictionaryDatas: " + responseString + "}"); + out.write(j.toString()); + return null; + } + }catch (Exception e){ response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); @@ -919,7 +974,7 @@ public class MicroServiceDictionaryController { return null; } catch (Exception e){ - System.out.println(e); + LOGGER.error(e); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); @@ -927,11 +982,7 @@ public class MicroServiceDictionaryController { } return null; } - - - - - + private void addValuesToNewModel(HashMap<String,MSAttributeObject > classMap) { new MicroServiceModels(); //Loop through the classmap and pull out the required info for the new file. diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/ElkConnector.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/ElkConnector.java index 1fd95abd4..2b94e44c7 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/ElkConnector.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/ElkConnector.java @@ -20,7 +20,7 @@ package org.openecomp.policy.pap.xacml.rest.elk.client; -import java.util.ArrayList; +import java.util.Map; import org.openecomp.policy.rest.adapter.PolicyRestAdapter; @@ -58,106 +58,43 @@ public interface ElkConnector { none, } - public JestResult policy(String policyId) - throws IllegalStateException, IllegalArgumentException; - public boolean delete(PolicyRestAdapter policyData) throws IllegalStateException; - public ArrayList<PolicyLocator> policyLocators(PolicyIndexType type, String text,int connector) - throws IllegalStateException, IllegalArgumentException; - - public ArrayList<PolicyLocator> policyLocators(PolicyIndexType type, String text, - ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s,int connector) - throws IllegalStateException, IllegalArgumentException; - public JestResult search(PolicyIndexType type, String text) throws IllegalStateException, IllegalArgumentException; public JestResult search(PolicyIndexType type, String text, - ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s) + Map<String, String> searchKeyValue) throws IllegalStateException, IllegalArgumentException; public boolean update(PolicyRestAdapter policyData) throws IllegalStateException; public ElkConnector singleton = new ElkConnectorImpl(); - public static PolicyIndexType toPolicyIndexType(PolicyType type) - throws IllegalArgumentException { - if (type == null) - throw new IllegalArgumentException("Unsupported NULL type conversion"); - - switch(type) { - case Config: - return PolicyIndexType.config; - case Action: - return PolicyIndexType.action; - case Decision: - return PolicyIndexType.decision; - case Config_Fault: - return PolicyIndexType.closedloop; - case Config_PM: - return PolicyIndexType.closedloop; - case Config_FW: - return PolicyIndexType.config; - case Config_MS: - return PolicyIndexType.config; - case none: - return PolicyIndexType.all; - default: - throw new IllegalArgumentException("Unsupported type conversion to index: " + type.name()); - } - } - public static PolicyIndexType toPolicyIndexType(String policyName) - throws IllegalArgumentException { - if (policyName == null) - throw new IllegalArgumentException("Unsupported NULL policy name conversion"); - - if (policyName.startsWith("Config_Fault")) { - return PolicyIndexType.closedloop; - } else if (policyName.startsWith("Config_PM")) { - return PolicyIndexType.closedloop; - } else if (policyName.startsWith("Config_FW")) { - return PolicyIndexType.config; - } else if (policyName.startsWith("Config_MS")) { - return PolicyIndexType.config; - }else if (policyName.startsWith("Action")) { - return PolicyIndexType.action; - } else if (policyName.startsWith("Decision")) { - return PolicyIndexType.decision; - } else if (policyName.startsWith("Config")) { - return PolicyIndexType.config; - } else { - throw new IllegalArgumentException - ("Unsupported policy name conversion to index: " + - policyName); - } - } - - public static PolicyType toPolicyType(String policyName) - throws IllegalArgumentException { + throws IllegalArgumentException { if (policyName == null) - throw new IllegalArgumentException("Unsupported NULL policy name conversion to Policy Type"); - + throw new IllegalArgumentException("Unsupported NULL policy name conversion"); + if (policyName.startsWith("Config_Fault")) { - return PolicyType.Config_Fault; + return PolicyIndexType.closedloop; } else if (policyName.startsWith("Config_PM")) { - return PolicyType.Config_PM; + return PolicyIndexType.closedloop; } else if (policyName.startsWith("Config_FW")) { - return PolicyType.Config_FW; + return PolicyIndexType.config; } else if (policyName.startsWith("Config_MS")) { - return PolicyType.Config_MS; + return PolicyIndexType.config; }else if (policyName.startsWith("Action")) { - return PolicyType.Action; + return PolicyIndexType.action; } else if (policyName.startsWith("Decision")) { - return PolicyType.Decision; + return PolicyIndexType.decision; } else if (policyName.startsWith("Config")) { - return PolicyType.Config; + return PolicyIndexType.config; } else { throw new IllegalArgumentException - ("Unsupported policy name conversion to index: " + - policyName); + ("Unsupported policy name conversion to index: " + + policyName); } } diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/ElkConnectorImpl.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/ElkConnectorImpl.java index 5a64ec2b7..5508ddc69 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/ElkConnectorImpl.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/ElkConnectorImpl.java @@ -19,32 +19,25 @@ */ package org.openecomp.policy.pap.xacml.rest.elk.client; -import java.io.File; import java.io.IOException; -import java.util.ArrayList; +import java.util.Map; import java.util.Map.Entry; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryStringQueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.json.JSONObject; -import org.kohsuke.args4j.Option; import org.openecomp.policy.common.logging.flexlogger.FlexLogger; import org.openecomp.policy.common.logging.flexlogger.Logger; import org.openecomp.policy.rest.adapter.PolicyRestAdapter; import org.openecomp.policy.xacml.api.XACMLErrorConstants; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; - import io.searchbox.action.Action; import io.searchbox.client.JestClient; import io.searchbox.client.JestClientFactory; import io.searchbox.client.JestResult; import io.searchbox.client.config.HttpClientConfig; import io.searchbox.core.Delete; -import io.searchbox.core.Get; import io.searchbox.core.Index; import io.searchbox.core.Search; import io.searchbox.core.Search.Builder; @@ -54,26 +47,6 @@ import io.searchbox.params.Parameters; public class ElkConnectorImpl implements ElkConnector{ - protected static class CLIOptions { - @Option(name="-s", usage="search", aliases={"-search", "--search"}, required=false, metaVar="<search text>") - protected String searchText; - - @Option(name="-e", usage="test and update policy if not exists", aliases={"-exist", "--exists"}, required=false, metaVar="<policy file>") - protected File testFile; - - @Option(name = "-h", aliases = {"-help", "--help"}, usage = "print this message") - private boolean help = false; - }; - - private static final String POLICY_RESULT_FIELDS = "[ \"Policy.PolicyType\", " + - "\"Policy.PolicyName\", " + - "\"Policy.Owner\", " + - "\"Policy.Scope\", " + - "\"Policy.PolicyId\", " + - "\"Policy.Version\" ]"; - - private static final String SOURCE_RESULT_FIELDS = "\"_source\": " + POLICY_RESULT_FIELDS; - private static final Logger LOGGER = FlexLogger.getLogger(ElkConnector.class); protected final JestClientFactory jestFactory = new JestClientFactory(); @@ -139,17 +112,10 @@ public class ElkConnectorImpl implements ElkConnector{ throw new IllegalArgumentException("No search string provided"); } - // MatchQueryBuilder mQ = QueryBuilders.matchQuery("_all", text); - QueryStringQueryBuilder mQ = QueryBuilders.queryStringQuery(text); + QueryStringQueryBuilder mQ = QueryBuilders.queryStringQuery("*"+text+"*"); SearchSourceBuilder searchSourceBuilder = - new SearchSourceBuilder().query(mQ). - fetchSource(new String[]{"Policy.PolicyType", - "Policy.PolicyName", - "Policy.Owner", - "Policy.Scope", - "Policy.PolicyId", - "Policy.Version"}, - null); + new SearchSourceBuilder().query(mQ); + Builder searchBuilder = new Search.Builder(searchSourceBuilder.toString()). addIndex(ELK_INDEX_POLICY). setParameter(Parameters.SIZE, ElkConnectorImpl.QUERY_MAXRECORDS); @@ -219,191 +185,10 @@ public class ElkConnectorImpl implements ElkConnector{ return result; } - public JestResult searchKey(PolicyIndexType type, String text, - ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s,int connector) - throws IllegalStateException, IllegalArgumentException { - if (LOGGER.isTraceEnabled()){ - LOGGER.trace("ENTER: " + text); - } - if (filter_s == null || filter_s.size() <= 0) { - return search(type, text); - } - - String matches_s = ""; - - if(connector==0)// AND CONNECTOR - { - matches_s = "{\n" + - " " + SOURCE_RESULT_FIELDS + ",\n" + - " \"size\" : "+ ElkConnectorImpl.QUERY_MAXRECORDS + ",\n" + - " \"query\": {\n" + - " \"bool\" : {\n" + - " \"must\" : ["; - } - else if (connector ==1)//OR CONNECTOR - { - matches_s = "{\n" + - " " + SOURCE_RESULT_FIELDS + ",\n" + - " \"size\" : "+ ElkConnectorImpl.QUERY_MAXRECORDS + ",\n" + - " \"query\": {\n" + - " \"bool\" : {\n" + - " \"should\" : ["; - } - - for (Pair<ArrayList<String>,ArrayList<String>> p : filter_s) { - ArrayList<String> name_s = p.left(); - ArrayList<String> value_s = p.right(); - - if (name_s == null || name_s.size() <= 0) { - if (LOGGER.isWarnEnabled()){ - LOGGER.warn("Defaulting to text search: Empty field name array passed in"); - } - return search(type, text); - } - - if (LOGGER.isDebugEnabled()) { - for (String n: name_s) { - LOGGER.debug("Filter Name: " + n); - } - } - - if (value_s == null || value_s.size() <= 0) { - if (LOGGER.isWarnEnabled()){ - LOGGER.warn("Defaulting to text search: Empty field value array passed in"); - } - return search(type, text); - } - - if (LOGGER.isDebugEnabled()) { - for (String v: value_s) { - LOGGER.debug("Filter Value: " + v); - } - } - - /* common case: # filter names == # filter values */ - if (name_s.size() == value_s.size()) { - String match = ""; - for (int i=0; i<name_s.size(); i++) { - if (name_s.get(i).contains("*")) { - match = - "{ \"query_string\": { \"fields\": [ \"" + - name_s.get(i) + "\" ], " + - "\"query\" : \"" + - value_s.get(i) + "\" } },"; - } else { - match = - "{ \"match_phrase\": { \"" + - name_s.get(i) + "\" : \"" + - value_s.get(i) + "\" } },"; - } - if (LOGGER.isDebugEnabled()){ - LOGGER.debug("Adding Match Line: " + match); - } - matches_s = matches_s + "\n " + match; - } - } - else if (name_s.size() > value_s.size() && (value_s.size() == 1)) { - String match = - "{ \"multi_match\": { \"query\": \"" + value_s.get(0) + "\", \"type\": \"phrase\", \"fields\": ["; - for (String n: name_s) { - match += " \"" + n + "\","; - } - match = match.substring(0, match.length()-1); - match += " ] } },";//debug - if (LOGGER.isDebugEnabled()){ - LOGGER.debug("Adding Match Line: " + match); - } - matches_s = matches_s + "\n " + match; - } else { - if (LOGGER.isWarnEnabled()) - LOGGER.warn("Defaulting to text search: different number of filter names and values"); - return search(type, text); - } - } - - matches_s = matches_s.substring(0, matches_s.length()-1); // remove last comma - - matches_s = matches_s + - " ]\n" + - " }\n" + - " }\n" + - "}"; - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(matches_s); - } - - Builder searchBuilder = new Search.Builder(matches_s). - addIndex(ELK_INDEX_POLICY); - - if (type == null || type == PolicyIndexType.all) { - for (PolicyIndexType pT: PolicyIndexType.values()) { - if (pT != PolicyIndexType.all) { - searchBuilder.addType(pT.toString()); - } - } - } else { - searchBuilder.addType(type.toString()); - } - - Search search = searchBuilder.build(); - - JestResult result; - try { - result = jestClient.execute(search); - } catch (IOException ioe) { - LOGGER.warn(XACMLErrorConstants.ERROR_SYSTEM_ERROR + ":" + - search + ": " + ioe.getMessage(), ioe); - throw new IllegalStateException(ioe); - } - - if (result.isSucceeded()) { - if (LOGGER.isInfoEnabled()){ - LOGGER.info("OK:" + result.getResponseCode() + ":" + search + ": " + - result.getPathToResult() + ":" + System.lineSeparator() + - result.getJsonString()); - } - } else { - /* Unsuccessful search */ - if (LOGGER.isWarnEnabled()){ - LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW + ":" + - result.getResponseCode() + ": " + - search.getURI() + ":" + - result.getPathToResult() + ":" + - result.getJsonString() + ":" + - result.getErrorMessage()); - } - - String errorMessage = result.getErrorMessage(); - if (errorMessage != null && !errorMessage.isEmpty()) { - String xMessage = errorMessage; - if (errorMessage.contains("TokenMgrError")) { - int indexError = errorMessage.lastIndexOf("TokenMgrError"); - xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); - } else if (errorMessage.contains("QueryParsingException")) { - int indexError = errorMessage.lastIndexOf("QueryParsingException"); - xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); - } else if (errorMessage.contains("JsonParseException")) { - int indexError = errorMessage.lastIndexOf("JsonParseException"); - xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); - } else if (errorMessage.contains("Parse Failure")) { - int indexError = errorMessage.lastIndexOf("Parse Failure"); - xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); - } else if (errorMessage.contains("SearchParseException")) { - int indexError = errorMessage.lastIndexOf("SearchParseException"); - xMessage = "Invalid Search Expression. Details: " + errorMessage.substring(indexError); - } else { - xMessage = result.getErrorMessage(); - } - throw new IllegalStateException(xMessage); - } - } - return result; - } @Override public JestResult search(PolicyIndexType type, String text, - ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s) + Map<String, String> filter_s) throws IllegalStateException, IllegalArgumentException { if (LOGGER.isTraceEnabled()){ LOGGER.trace("ENTER: " + text); @@ -415,106 +200,41 @@ public class ElkConnectorImpl implements ElkConnector{ String matches_s = ""; matches_s = "{\n" + - " " + SOURCE_RESULT_FIELDS + ",\n" + " \"size\" : "+ ElkConnectorImpl.QUERY_MAXRECORDS + ",\n" + " \"query\": {\n" + " \"bool\" : {\n" + " \"must\" : ["; - for (Pair<ArrayList<String>,ArrayList<String>> p : filter_s) { - ArrayList<String> name_s = p.left(); - ArrayList<String> value_s = p.right(); - - if (name_s == null || name_s.size() <= 0) { - if (LOGGER.isWarnEnabled()){ - LOGGER.warn("Defaulting to text search: Empty field name array passed in"); - } - return search(type, text); - } - - if (LOGGER.isDebugEnabled()) { - for (String n: name_s) { - LOGGER.debug("Filter Name: " + n); - } - } - - if (value_s == null || value_s.size() <= 0) { - if (LOGGER.isWarnEnabled()) - LOGGER.warn("Defaulting to text search: Empty field value array passed in"); - return search(type, text); - } - - if (LOGGER.isDebugEnabled()) { - for (String v: value_s) { - LOGGER.debug("Filter Value: " + v); - } - } - - /* common case: # filter names == # filter values */ - if (name_s.size() == value_s.size()) { - String match = ""; - for (int i=0; i<name_s.size(); i++) { - if (name_s.get(i).contains("*")) { - match = - "{ \"query_string\": { \"fields\": [ \"" + - name_s.get(i) + "\" ], " + - "\"query\" : \"" + - value_s.get(i) + "\" } },"; - } else { - match = - "{ \"match_phrase\": { \"" + - name_s.get(i) + "\" : \"" + - value_s.get(i) + "\" } },"; - } - if (LOGGER.isDebugEnabled()){ - LOGGER.debug("Adding Match Line: " + match); - } - matches_s = matches_s + "\n " + match; - } - } else if (name_s.size() > value_s.size() && (value_s.size() == 1)) { - String match = - "{ \"multi_match\": { \"query\": \"" + value_s.get(0) + "\", \"type\": \"phrase\", \"fields\": ["; - for (String n: name_s) { - match += " \"" + n + "\","; - } - match = match.substring(0, match.length()-1); - match += " ] } },"; - if (LOGGER.isDebugEnabled()){ - LOGGER.debug("Adding Match Line: " + match); - } - matches_s = matches_s + "\n " + match; - } else { - if (LOGGER.isWarnEnabled()){ - LOGGER.warn("Defaulting to text search: different number of filter names and values"); - } - return search(type, text); + + String match_params = ""; + for(Entry<String, String> entry : filter_s.entrySet()){ + String key = entry.getKey(); + String value = entry.getValue(); + if(filter_s.size() == 1){ + match_params = "\"match\" : {\""+key+"\" : \""+value+"\" }"; + }else{ + match_params = match_params + "match\" : { \""+key+"\" : \""+value+"\" } ,"; } } - if (text != null && !text.isEmpty()) { - if (LOGGER.isDebugEnabled()){ - LOGGER.debug("Adding Match Line for search text: " + text); - } - - final JsonObject jsonText = new JsonObject(); - jsonText.addProperty("_all", text); - String escapedText = jsonText.toString(); + if(match_params.endsWith(",")){ + match_params = match_params.substring(0, match_params.length()-1); + } - matches_s = matches_s + "\n " + - "{ \"match\": " + - escapedText + " },"; + matches_s = matches_s + "{\n" + match_params + "\n}" ; + + boolean query = false; + String query_String = ""; + if(text != null){ + query = true; + query_String = "{\n \"query_string\" : {\n \"query\" : \"*"+text+"*\"\n} \n}"; } - matches_s = matches_s.substring(0, matches_s.length()-1); // remove last comma - matches_s = matches_s + "\n" + - " ]\n" + - " }\n" + - " }\n" + - "}"; - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug(matches_s); + + if(query){ + matches_s = matches_s + "," + query_String + "]\n}\n}\n}"; + }else{ + matches_s = matches_s + "]\n}\n}\n}"; } - - Builder searchBuilder = new Search.Builder(matches_s). - addIndex(ELK_INDEX_POLICY); + + Builder searchBuilder = new Search.Builder(matches_s).addIndex(ELK_INDEX_POLICY); if (type == null || type == PolicyIndexType.all) { for (PolicyIndexType pT: PolicyIndexType.values()) { @@ -582,155 +302,6 @@ public class ElkConnectorImpl implements ElkConnector{ return result; } - @Override - public JestResult policy(String policyId) - throws IllegalStateException, IllegalArgumentException { - if (LOGGER.isTraceEnabled()){ - LOGGER.trace("ENTER: " + policyId); - } - - if (policyId == null || policyId.isEmpty()) { - throw new IllegalArgumentException("No policy id string provided"); - } - - Get policyRequest = new Get.Builder(ELK_INDEX_POLICY, policyId).build(); - - if (LOGGER.isInfoEnabled()){ - LOGGER.info("ELK Search body request: " + policyRequest.toString()); - } - - JestResult result; - try { - result = jestClient.execute(policyRequest); - } catch (IOException ioe) { - LOGGER.warn(XACMLErrorConstants.ERROR_SYSTEM_ERROR + ":" + - policyId + ": " + ioe.getMessage(), ioe); - throw new IllegalStateException(ioe); - } - - if (result.isSucceeded()) { - if (LOGGER.isInfoEnabled()){ - LOGGER.info("OK:" + result.getResponseCode() + ":" + policyId + ":" + - result.getPathToResult() + ":" + System.lineSeparator() + - result.getJsonString()); - } - - return result; - } - - /* Unsuccessful search */ - if (LOGGER.isWarnEnabled()) - LOGGER.warn(XACMLErrorConstants.ERROR_PROCESS_FLOW + ":" + - result.getResponseCode() + ": " + policyId + ":" + - result.getPathToResult() + ":" + - result.getErrorMessage()); - - return result; - } - - protected JsonObject getJsonObject(JsonObject jsonObject, String member) throws IllegalArgumentException { - if (jsonObject == null) { - if (LOGGER.isWarnEnabled()) - LOGGER.warn("No JSON object provided to get " + member); - - throw new IllegalArgumentException("No JSON Object provided"); - } - - if (LOGGER.isTraceEnabled()) { - LOGGER.trace("ENTER: " + member); - for (Entry<String, JsonElement> entry: jsonObject.entrySet()) { - LOGGER.trace("JSONOBJECT: " + entry.getKey() + "->" + entry.getValue()); - } - } - - if (jsonObject.has(member)) { - JsonElement element = jsonObject.getAsJsonObject(member); - if (element.isJsonObject()) { - return (JsonObject) element; - } - } - - throw new IllegalArgumentException(member + " is not a JSON Object"); - } - - protected JsonArray getJsonArray(JsonObject jsonObject, String member) throws IllegalArgumentException { - if (jsonObject == null) { - throw new IllegalArgumentException("No JSON Object provided"); - } - - if (jsonObject.has(member)) { - if (jsonObject.get(member).isJsonArray()) { - return (JsonArray) jsonObject.get(member); - } - } - - throw new IllegalArgumentException(member + " is not a JSON Array"); - } - - protected String getJsonPolicyMember(JsonObject aHit, String member) throws IllegalArgumentException { - if (aHit == null) { - throw new IllegalArgumentException("No JSON Object provided"); - } - - JsonObject jSource = getJsonObject(aHit, "_source"); - JsonObject jPolicy = getJsonObject(jSource, "Policy"); - JsonElement jMember = jPolicy.get(member); - if (jMember == null) { - throw new IllegalArgumentException(member + " is not a JSON Object"); - } - return jMember.getAsString(); - } - - @Override - public ArrayList<PolicyLocator> policyLocators(PolicyIndexType indexType, String text, int connector) - throws IllegalStateException, IllegalArgumentException { - return policyLocators(indexType, text, new ArrayList<Pair<ArrayList<String>,ArrayList<String>>>(),connector); - } - - @Override - public ArrayList<PolicyLocator> policyLocators(PolicyIndexType indexType, - String text, - ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s, int connector) - throws IllegalStateException, IllegalArgumentException { - final ArrayList<PolicyLocator> policyLocators = new ArrayList<PolicyLocator>(); - - JestResult results = searchKey(indexType, text, filter_s,connector); - if (!results.isSucceeded()) { - return policyLocators; - } - - JsonArray jsonHit_s = null; - try { - JsonObject jsonHits = getJsonObject(results.getJsonObject(), "hits"); - jsonHit_s = getJsonArray(jsonHits, "hits"); - } catch (IllegalArgumentException e) { - LOGGER.warn("SEARCH:" + text + " no valid element provided", e); - return policyLocators; - } - - for (JsonElement e : jsonHit_s) { - JsonObject elkSource = (JsonObject) e; - try { - String policyType = getJsonPolicyMember(elkSource,"PolicyType"); - String policyName = getJsonPolicyMember(elkSource,"PolicyName"); - String owner = getJsonPolicyMember(elkSource,"Owner"); - String scope = getJsonPolicyMember(elkSource,"Scope"); - String policyId = getJsonPolicyMember(elkSource,"PolicyId"); - String version = getJsonPolicyMember(elkSource,"Version"); - PolicyLocator policyLocator = - new PolicyLocator(policyType, policyName, owner, - scope, policyId, version); - policyLocators.add(policyLocator); - if (LOGGER.isInfoEnabled()) { - LOGGER.info("SEARCH:" + text + "|FOUND:" + policyLocator); - } - } catch (IllegalArgumentException ex) { - LOGGER.warn("SEARCH:" + text + " missing locator information.", ex); - } - } - return policyLocators; - } - public boolean put(PolicyRestAdapter policyData) throws IOException, IllegalStateException { if (LOGGER.isTraceEnabled()) LOGGER.trace("ENTER"); diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicyElasticData.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicyElasticData.java index f0783529a..d4cf812a9 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicyElasticData.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicyElasticData.java @@ -25,18 +25,26 @@ import java.util.List; import java.util.Map; import org.openecomp.policy.rest.adapter.PolicyRestAdapter; +import org.openecomp.policy.rest.adapter.YAMLParams; public class PolicyElasticData { private String scope; private String policyType; private String configPolicyType; + private String configBodyData; private String policyName; private String policyDescription; private String ecompName; private String configName; private String configType; private String jsonBody; + private Object jsonBodyData; + + private LinkedHashMap<?, ?> serviceTypePolicyName; + private LinkedHashMap<?, ?> verticaMetrics; + private LinkedHashMap<?, ?> description; + private LinkedHashMap<?, ?> attributeFields; //Safe Policy private String policyScope; @@ -102,17 +110,25 @@ public class PolicyElasticData { private String actionDictType; private String actionDictUrl; private String actionDictMethod; + private YAMLParams yamlparams; public PolicyElasticData(PolicyRestAdapter policyData) { this.scope = policyData.getDomain(); this.policyType = policyData.getPolicyType(); this.configPolicyType = policyData.getConfigPolicyType(); + this.configBodyData = policyData.getConfigBodyData(); this.policyName = policyData.getNewFileName(); this.policyDescription = policyData.getPolicyDescription(); this.ecompName = policyData.getEcompName(); this.configName = policyData.getConfigName(); this.configType = policyData.getConfigType(); this.jsonBody = policyData.getJsonBody(); + this.jsonBodyData = policyData.getJsonBodyData(); + + this.serviceTypePolicyName = policyData.getServiceTypePolicyName(); + this.verticaMetrics = policyData.getVerticaMetrics(); + this.description = policyData.getDescription(); + this.attributeFields = policyData.getAttributeFields(); //Safe Policy this.policyScope = policyData.getPolicyScope(); @@ -178,6 +194,7 @@ public class PolicyElasticData { this.actionDictType = policyData.getActionDictType(); this.actionDictUrl = policyData.getActionDictUrl(); this.actionDictMethod = policyData.getActionDictMethod(); + this.yamlparams = policyData.getYamlparams(); } public String getScope() { @@ -198,6 +215,13 @@ public class PolicyElasticData { public void setConfigPolicyType(String configPolicyType) { this.configPolicyType = configPolicyType; } + public String getConfigBodyData() { + return configBodyData; + } + + public void setConfigBodyData(String configBodyData) { + this.configBodyData = configBodyData; + } public String getPolicyName() { return policyName; } @@ -234,6 +258,37 @@ public class PolicyElasticData { public void setJsonBody(String jsonBody) { this.jsonBody = jsonBody; } + public LinkedHashMap<?, ?> getServiceTypePolicyName() { + return serviceTypePolicyName; + } + + public void setServiceTypePolicyName(LinkedHashMap<?, ?> serviceTypePolicyName) { + this.serviceTypePolicyName = serviceTypePolicyName; + } + + public LinkedHashMap<?, ?> getVerticaMetrics() { + return verticaMetrics; + } + + public void setVerticaMetrics(LinkedHashMap<?, ?> verticaMetrics) { + this.verticaMetrics = verticaMetrics; + } + + public LinkedHashMap<?, ?> getDescription() { + return description; + } + + public void setDescription(LinkedHashMap<?, ?> description) { + this.description = description; + } + + public LinkedHashMap<?, ?> getAttributeFields() { + return attributeFields; + } + + public void setAttributeFields(LinkedHashMap<?, ?> attributeFields) { + this.attributeFields = attributeFields; + } public String getPolicyScope() { return policyScope; } @@ -546,5 +601,19 @@ public class PolicyElasticData { public void setActionDictMethod(String actionDictMethod) { this.actionDictMethod = actionDictMethod; } + public YAMLParams getYamlparams() { + return yamlparams; + } + + public void setYamlparams(YAMLParams yamlparams) { + this.yamlparams = yamlparams; + } + public Object getJsonBodyData() { + return jsonBodyData; + } + + public void setJsonBodyData(Object jsonBodyData) { + this.jsonBodyData = jsonBodyData; + } } diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicyElasticSearchController.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicyElasticSearchController.java index 9e512dab2..a067e9038 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicyElasticSearchController.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicyElasticSearchController.java @@ -20,15 +20,14 @@ package org.openecomp.policy.pap.xacml.rest.elk.client; -import java.io.File; import java.io.PrintWriter; -import java.nio.file.Path; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; @@ -52,7 +51,6 @@ import org.openecomp.policy.rest.jpa.ClosedLoopD2Services; import org.openecomp.policy.rest.jpa.ClosedLoopSite; import org.openecomp.policy.rest.jpa.DCAEuuid; import org.openecomp.policy.rest.jpa.DecisionSettings; -import org.openecomp.policy.rest.jpa.DescriptiveScope; import org.openecomp.policy.rest.jpa.EcompName; import org.openecomp.policy.rest.jpa.EnforcingType; import org.openecomp.policy.rest.jpa.GroupPolicyScopeList; @@ -74,12 +72,13 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.searchbox.client.JestResult; + @Controller @RequestMapping({"/"}) public class PolicyElasticSearchController{ private static final Logger LOGGER = FlexLogger.getLogger(PolicyElasticSearchController.class); - private volatile HashMap<Path, String> filteredPolicies = new HashMap<Path, String>(); private List<JSONObject> policyNames = null; enum Mode{ @@ -90,8 +89,6 @@ public class PolicyElasticSearchController{ public static final HashMap<String, String> name2jsonPath = new HashMap<String, String>() { private static final long serialVersionUID = 1L; }; - //For AND and OR logical connector AND=0 and OR=1 - private int connectorSelected; public static CommonClassDao commonClassDao; @@ -350,27 +347,17 @@ public class PolicyElasticSearchController{ public String searchElkDatabase(String value){ String policyType = ""; String searchText = value; - ArrayList<PolicyLocator> locators; - ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s = new ArrayList<Pair<ArrayList<String>,ArrayList<String>>>(); + JestResult locators; + Map<String, String> filter_s = new HashMap<String, String>(); try { - locators = ElkConnector.singleton.policyLocators(toPolicyIndexType(policyType), searchText, filter_s,0); + locators = ElkConnector.singleton.search(toPolicyIndexType(policyType), searchText, filter_s); } catch (Exception ise) { LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Search is unavailable: " + ise.getMessage()); value = "$notSuccess%"; return value; } policyNames = new ArrayList<JSONObject>(); - for (PolicyLocator p: locators) { - String dbPolicyName = p.scope + "/" + p.policyType + "_" + p.policyName + "." +p.version + ".xml"; - LOGGER.debug(dbPolicyName); - JSONObject el = new JSONObject(); - el.put("name", dbPolicyName); - policyNames.add(el); - } - if(!locators.isEmpty()){ - value = "$success%"; - return value; - } + System.out.println(locators); return value; } @@ -378,8 +365,8 @@ public class PolicyElasticSearchController{ public String searchElkDatabase(String key, String value){ String policyType = ""; String searchText = key+":"+value; - ArrayList<PolicyLocator> locators; - ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s = new ArrayList<Pair<ArrayList<String>,ArrayList<String>>>(); + JestResult locators; + Map<String, String> filter_s = new HashMap<String, String>(); LOGGER.debug("Parameter value is"+value); String clSearchKey=null; @@ -387,13 +374,7 @@ public class PolicyElasticSearchController{ LOGGER.debug("Filter value is"+clSearchKey); - ArrayList<String> clSearchBoxFilterField_s = new ArrayList<String>(); - clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_Fault.name() + "_Body." + clSearchKey); - clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_PM.name() + "_Body." + clSearchKey); - clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_FW.name() + "_Body." + clSearchKey); - clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_MS.name() + "_Body." + clSearchKey); - //clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_PM.name() + "_Body." + clSearchKey); String clSearchValue=null; clSearchValue=value; @@ -403,15 +384,10 @@ public class PolicyElasticSearchController{ ArrayList<String> clSearchBoxFilterValue_s = new ArrayList<String>(); clSearchBoxFilterValue_s.add(clSearchValue); - filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clSearchBoxFilterField_s, clSearchBoxFilterValue_s)); try { - locators = ElkConnector.singleton.policyLocators(toPolicyIndexType(policyType), searchText, filter_s,0); - LOGGER.debug("No Exceptions"); - for (PolicyLocator l: locators) { - LOGGER.debug(l.policyName); - } - LOGGER.debug("After for"); + locators = ElkConnector.singleton.search(toPolicyIndexType(policyType), searchText, filter_s); + System.out.println(locators); } catch (Exception ise) { LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Search is unavailable: " + ise.getMessage()); //PolicyLogger.error(MessageCodes.ERROR_SYSTEM_ERROR, ise, "AttributeDictionary", " Exception while searching Elk database "); @@ -419,107 +395,14 @@ public class PolicyElasticSearchController{ value = "$notSuccess%"; return value; } - policyNames = new ArrayList<JSONObject>(); - for (PolicyLocator p: locators) { - String dbPolicyName = p.scope + File.separator + p.policyType + "_" + p.policyName + ".xml"; - LOGGER.debug(dbPolicyName); - JSONObject el = new JSONObject(); - el.put("name", dbPolicyName); - policyNames.add(el); - } - if(!locators.isEmpty()){ - value = "$success%"; - LOGGER.debug("Success"); - return value; - } return value; } - - //For AutoPush of policy using descriptive Scope. - //Returns string either "UnMatched" or "Matched" or "Search Unavailable". - public String searchDescriptiveScope(String scopeName, String policyNameToCheck) { - String searchText=null; - String status="UnMatched"; - ArrayList<Pair<ArrayList<String>,ArrayList<String>>> filter_s = - new ArrayList<Pair<ArrayList<String>,ArrayList<String>>>(); - //Finding the descriptive scope search tag. - LOGGER.warn("Entry into DS"); - DescriptiveScope dsSearch = (DescriptiveScope) commonClassDao.getEntityItem(DescriptiveScope.class, "descriptiveScopeName", scopeName); - - searchText=dsSearch.getSearch(); - LOGGER.warn("Search text is " + searchText); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("DescriptiveScope Search String is " +searchText ); - } - - - if(searchText.contains(":")) - { - String connector="AND"; - - for (String retval: searchText.split(connector)){ - - int index= retval.indexOf(':'); - String filterKey=null; - String filterValue=null; - - filterKey=retval.substring(0,index).trim(); - filterValue= retval.substring(index+1).trim(); - - LOGGER.debug("Key is "+filterKey+" and value is "+filterValue); - String clSearchBoxFilter=filterKey; - - ArrayList<String> clSearchBoxFilterField_s = new ArrayList<String>(); - - clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_Fault.name() + "_Body." + clSearchBoxFilter); - clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_PM.name() + "_Body." + clSearchBoxFilter); - clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_FW.name() + "_Body." + clSearchBoxFilter); - clSearchBoxFilterField_s.add("Policy.Body." + ElkConnector.PolicyType.Config_MS.name() + "_Body." + clSearchBoxFilter); - - - ArrayList<String> clSearchBoxFilterValue_s = new ArrayList<String>(); - clSearchBoxFilterValue_s.add(filterValue); - - filter_s.add(new Pair<ArrayList<String>,ArrayList<String>>(clSearchBoxFilterField_s, clSearchBoxFilterValue_s)); - } - } - - ArrayList<PolicyLocator> locators=null; - try { - LOGGER.warn("Before calling search"); - locators = ElkConnector.singleton.policyLocators(ElkConnector.PolicyIndexType.all, - searchText, filter_s,connectorSelected); - LOGGER.warn("After calling search"); - } catch (Exception ise) { - //AdminNotification.warn("Search is unavailable: " + ise.getMessage()); - status= "Search Unavailable"; - LOGGER.warn("Search is unavailable"); - } - synchronized(filteredPolicies) { - if (locators.isEmpty()) { - LOGGER.debug("No match has been found"); - //AdminNotification.warn("No match has been found"); - status="UnMatched"; - } - - for (PolicyLocator p: locators) { - LOGGER.debug("Second String "+policyNameToCheck); - if(p.policyName.contains(policyNameToCheck)) - { - status="Matched"; - LOGGER.warn("Policies matched"); - break; - } - else - { - LOGGER.warn("Policies Unmatched"); - status="UnMatched"; - } - } - } - return status; - + + public JestResult search(PolicyIndexType type, String text, + Map<String, String> searchKeyValue) { + return ElkConnector.singleton.search(type, text, searchKeyValue); } + } class SearchData{ diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicySearchController.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicySearchController.java index 91f97cae3..fc70e9a84 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicySearchController.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/elk/client/PolicySearchController.java @@ -19,20 +19,33 @@ */ package org.openecomp.policy.pap.xacml.rest.elk.client; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONObject; import org.openecomp.policy.common.logging.flexlogger.FlexLogger; import org.openecomp.policy.common.logging.flexlogger.Logger; +import org.openecomp.policy.pap.xacml.rest.elk.client.ElkConnector.PolicyIndexType; import org.openecomp.policy.pap.xacml.rest.util.JsonMessage; import org.openecomp.policy.rest.adapter.PolicyRestAdapter; +import org.openecomp.policy.rest.dao.CommonClassDao; +import org.openecomp.policy.rest.jpa.DescriptiveScope; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.JsonArray; + +import io.searchbox.client.JestResult; @Controller @RequestMapping("/") @@ -40,23 +53,109 @@ public class PolicySearchController { private static final Logger LOGGER = FlexLogger.getLogger(PolicySearchController.class); + @Autowired + CommonClassDao commonClassDao; + @RequestMapping(value="/searchPolicy", method= RequestMethod.POST) public void elkTransaction(HttpServletRequest request, HttpServletResponse response) { try{ boolean result = false; + boolean policyResult = false; ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); PolicyRestAdapter policyData = new PolicyRestAdapter(); + PolicyElasticSearchController controller = new PolicyElasticSearchController(); + Map<String, String> searchKeyValue = new HashMap<String, String>(); + List<String> policyList = new ArrayList<String>(); if(request.getParameter("policyName") != null){ String policyName = request.getParameter("policyName"); policyData.setNewFileName(policyName); - PolicyElasticSearchController controller = new PolicyElasticSearchController(); if("delete".equalsIgnoreCase(request.getParameter("action"))){ result = controller.deleteElk(policyData); }else{ result = controller.updateElk(policyData); } } + if("search".equalsIgnoreCase(request.getParameter("action"))){ + try { + JsonNode root = mapper.readTree(request.getReader()); + SearchData searchData = (SearchData)mapper.readValue(root.get("searchdata").toString(), SearchData.class); + + String policyType = searchData.getPolicyType(); + + String searchText = searchData.getQuery(); + String descriptivevalue = searchData.getDescriptiveScope(); + if(descriptivevalue != null){ + DescriptiveScope dsSearch = (DescriptiveScope) commonClassDao.getEntityItem(DescriptiveScope.class, "descriptiveScopeName", searchData.getDescriptiveScope()); + if(dsSearch != null){ + String[] descriptiveList = dsSearch.getSearch().split("AND"); + for(String keyValue : descriptiveList){ + String[] entry = keyValue.split(":"); + searchKeyValue.put(entry[0], entry[1]); + } + } + } + + if(searchData.getClosedLooppolicyType() != null){ + String closedLoopType; + if("Config_Fault".equalsIgnoreCase(searchData.getClosedLooppolicyType())){ + closedLoopType = "ClosedLoop_Fault"; + }else{ + closedLoopType = "ClosedLoop_PM"; + } + searchKeyValue.put("configPolicyType", closedLoopType); + } + if(searchData.getEcompName() != null){ + searchKeyValue.put("ecompName", searchData.getEcompName()); + } + if(searchData.getD2Service() != null){ + searchKeyValue.put("_all", searchData.getD2Service()); + } + if(searchData.getVnfType() != null){ + searchKeyValue.put("_all", searchData.getVnfType()); + } + if(searchData.getPolicyStatus() != null){ + searchKeyValue.put("_all", searchData.getPolicyStatus()); + } + if(searchData.getVproAction() != null){ + searchKeyValue.put("_all", searchData.getVproAction()); + } + if(searchData.getServiceType() != null){ + searchKeyValue.put("_all", searchData.getServiceType()); + } + if(searchData.getBindTextSearch() != null){ + searchKeyValue.put("_all", searchData.getBindTextSearch()); + } + PolicyIndexType type = null; + if(policyType != null){ + if(policyType.equalsIgnoreCase("action")){ + type = ElkConnector.PolicyIndexType.action; + }else if(policyType.equalsIgnoreCase("decision")){ + type = ElkConnector.PolicyIndexType.decision; + }else if(policyType.equalsIgnoreCase("config")){ + type = ElkConnector.PolicyIndexType.config; + }else { + type = ElkConnector.PolicyIndexType.closedloop; + } + }else{ + type = ElkConnector.PolicyIndexType.all; + } + JestResult policyResultList = controller.search(type, searchText, searchKeyValue); + if(policyResultList.isSucceeded()){ + result = true; + policyResult = true; + JsonArray resultObject = policyResultList.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray(); + for(int i =0; i < resultObject.size(); i++){ + String policyName = resultObject.get(i).getAsJsonObject().get("_id").toString(); + policyList.add(policyName); + } + }else{ + LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server, Check the Logs"); + } + }catch(Exception e){ + LOGGER.error("Exception Occured While Searching for Data in Elastic Search Server" + e); + } + } String message=""; if(result){ message = "Elastic Server Transaction is success"; @@ -67,10 +166,15 @@ public class PolicySearchController { JSONObject j = new JSONObject(msg); response.setStatus(HttpServletResponse.SC_OK); response.addHeader("success", "success"); - response.getWriter().write(j.toString()); + if(policyResult){ + JSONObject k = new JSONObject("{policyresult: " + policyList + "}"); + response.getWriter().write(k.toString()); + }else{ + response.getWriter().write(j.toString()); + } }catch(Exception e){ - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); - response.addHeader("error", "Exception Occured While Performing Elastic Transaction"); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + response.addHeader("error", "Exception Occured While Performing Elastic Transaction"); LOGGER.error("Exception Occured While Performing Elastic Transaction"+e.getMessage()); e.printStackTrace(); } diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/APIRequestHandler.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/APIRequestHandler.java index efaed9ffc..c1de11548 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/APIRequestHandler.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/APIRequestHandler.java @@ -40,12 +40,6 @@ public class APIRequestHandler { dictionaryHandler.doDictionaryAPIGet(request, response); return; } - // Request from the API to get the gitPath - if ("gitPath".equalsIgnoreCase(apiflag)) { - PushPolicyHandler pushHandler = new PushPolicyHandler(); - pushHandler.getGitPath(request, response); - return; - } // Request from the API to get the ActiveVersion from the PolicyVersion table if ("version".equalsIgnoreCase(apiflag)){ PushPolicyHandler pushHandler = new PushPolicyHandler(); diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/PushPolicyHandler.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/PushPolicyHandler.java index 59fa60aec..009a2d601 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/PushPolicyHandler.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/PushPolicyHandler.java @@ -21,9 +21,6 @@ package org.openecomp.policy.pap.xacml.rest.handler; import java.io.File; import java.net.URI; -import java.net.URL; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.List; import javax.persistence.EntityManager; @@ -34,17 +31,12 @@ import javax.servlet.http.HttpServletResponse; import org.openecomp.policy.common.logging.eelf.MessageCodes; import org.openecomp.policy.common.logging.eelf.PolicyLogger; import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet; -import org.openecomp.policy.rest.XACMLRestProperties; import org.openecomp.policy.rest.jpa.PolicyVersion; import org.openecomp.policy.xacml.api.pap.EcompPDPGroup; import org.openecomp.policy.xacml.std.pap.StdPDPPolicy; -import org.openecomp.policy.xacml.util.XACMLPolicyScanner; import com.att.research.xacml.util.XACMLProperties; -import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType; -import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType; - public class PushPolicyHandler { /* @@ -94,85 +86,6 @@ public class PushPolicyHandler { } /* - * Get Git Path - */ - public void getGitPath(HttpServletRequest request, HttpServletResponse response) { - String policyScope = request.getParameter("policyScope"); - String filePrefix = request.getParameter("filePrefix"); - String policyName = request.getParameter("policyName"); - String activeVersion = request.getParameter("activeVersion"); - - Path workspacePath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_WORKSPACE), "admin"); - Path repositoryPath = Paths.get(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_REPOSITORY)); - Path gitPath = Paths.get(workspacePath.toString(), repositoryPath.getFileName().toString()); - - //getting the fullpath of the gitPath and convert to string - String fullGitPath = gitPath.toAbsolutePath().toString(); - String finalGitPath = null; - - //creating the parentPath directory for the Admin Console use - if(fullGitPath.contains("\\")){ - if(fullGitPath.contains("ECOMP-PAP-REST")){ - finalGitPath = fullGitPath.replace("ECOMP-PAP-REST", "ecomp-sdk-app"); - }else{ - finalGitPath = fullGitPath.replace("ATT-PAP-REST", "ATT-ecomp-sdk-app"); - } - }else{ - finalGitPath = fullGitPath.replace("pap", "console"); - } - - finalGitPath += File.separator + policyScope + File.separator + filePrefix + policyName + "." + activeVersion + ".xml"; - File file = new File(finalGitPath); - URI uri = file.toURI(); - - // - // Extract XACML policy information - // - Boolean isValid = false; - String policyId = null; - String description = null; - String version = null; - - URL url; - try { - url = uri.toURL(); - Object rootElement = XACMLPolicyScanner.readPolicy(url.openStream()); - if (rootElement == null || - ( - ! (rootElement instanceof PolicySetType) && - ! (rootElement instanceof PolicyType) - ) ) { - PolicyLogger.warn("No root policy element in URI: " + uri.toString() + " : " + rootElement); - isValid = false; - } else { - if (rootElement instanceof PolicySetType) { - policyId = ((PolicySetType)rootElement).getPolicySetId(); - description = ((PolicySetType)rootElement).getDescription(); - isValid = true; - version = ((PolicySetType)rootElement).getVersion(); - } else if (rootElement instanceof PolicyType) { - policyId = ((PolicyType)rootElement).getPolicyId(); - description = ((PolicyType)rootElement).getDescription(); - version = ((PolicyType)rootElement).getVersion(); - isValid = true; - } else { - PolicyLogger.error("Unknown root element: " + rootElement.getClass().getCanonicalName()); - } - } - } catch (Exception e) { - PolicyLogger.error("Exception Occured While Extracting Policy Information"); - } - if (!finalGitPath.equalsIgnoreCase("") || policyId!=null || description!=null || version!=null || isValid!=null) { - response.setStatus(HttpServletResponse.SC_OK); - response.addHeader("gitPath", finalGitPath); - response.addHeader("policyId", policyId); - response.addHeader("description", description); - response.addHeader("version", version); - response.addHeader("isValid", isValid.toString()); - } - } - - /* * Get Selected URI path. */ public void getSelectedURI(HttpServletRequest request, HttpServletResponse response) { diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/SavePolicyHandler.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/SavePolicyHandler.java index 65402d5c2..ae75ad24c 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/SavePolicyHandler.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/handler/SavePolicyHandler.java @@ -127,7 +127,7 @@ public class SavePolicyHandler { policyAdapter.setDynamicRuleAlgorithmField2(policy.getDynamicRuleAlgorithmField2()); //Decision Policy Specific policyAdapter.setDynamicSettingsMap(policy.getDynamicSettingsMap()); - policyAdapter.setProviderComboBox(policy.getProviderComboBox()); + policyAdapter.setRuleProvider(policy.getProviderComboBox()); policyAdapter.setDomainDir(policyAdapter.getPolicyScope()); policyAdapter.setDomain(policyAdapter.getPolicyScope()); return policyAdapter; diff --git a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/policycontroller/PolicyCreation.java b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/policycontroller/PolicyCreation.java index a987e9abf..b9d6def5a 100644 --- a/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/policycontroller/PolicyCreation.java +++ b/ECOMP-PAP-REST/src/main/java/org/openecomp/policy/pap/xacml/rest/policycontroller/PolicyCreation.java @@ -349,7 +349,7 @@ public class PolicyCreation extends AbstractPolicyCreation{ } } } - if(policyData.getRuleAlgorithmschoices().size() > 0){ + if(policyData.getRuleAlgorithmschoices()!=null && policyData.getRuleAlgorithmschoices().size() > 0){ for(Object attribute : policyData.getRuleAlgorithmschoices()){ if(attribute instanceof LinkedHashMap<?, ?>){ String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString(); @@ -363,7 +363,14 @@ public class PolicyCreation extends AbstractPolicyCreation{ } } } - + if(policyData.getRuleProvider()!=null && policyData.getRuleProvider().equals(DecisionPolicy.GUARD_YAML) && policyData.getYamlparams()!=null){ + attributeMap.put("actor", policyData.getYamlparams().getActor()); + attributeMap.put("recipe", policyData.getYamlparams().getRecipe()); + attributeMap.put("limit", policyData.getYamlparams().getLimit()); + attributeMap.put("timeWindow", policyData.getYamlparams().getTimeWindow()); + attributeMap.put("guardActiveStart", policyData.getYamlparams().getGuardActiveStart()); + attributeMap.put("guardActiveEnd", policyData.getYamlparams().getGuardActiveEnd()); + } policyData.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels); policyData.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo); policyData.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1); diff --git a/ECOMP-PAP-REST/xacml.pap.properties b/ECOMP-PAP-REST/xacml.pap.properties index 7ec9447b0..c25a81207 100644 --- a/ECOMP-PAP-REST/xacml.pap.properties +++ b/ECOMP-PAP-REST/xacml.pap.properties @@ -27,7 +27,7 @@ xacml.PAP.papEngineFactory=org.openecomp.policy.xacml.std.pap.StdEngineFactory # Dictionary and Policy Implementation Classes Properties dictionary.impl.className=org.openecomp.policy.pap.xacml.rest.handler.DictionaryHandlerImpl savePolicy.impl.className=org.openecomp.policy.pap.xacml.rest.handler.SavePolicyHandler -pushPolicy.impl.className=org.openecomp.policy.pap.xacml.rest.handler.PushPolicyHander +pushPolicy.impl.className=org.openecomp.policy.pap.xacml.rest.handler.PushPolicyHandler deletePolicy.impl.className=org.openecomp.policy.pap.xacml.rest.handler.DeleteHandler #AutoPush Policy xacml.rest.pap.autopush.file=autopush.properties diff --git a/ECOMP-PAP-REST/xacml.pap.test.properties b/ECOMP-PAP-REST/xacml.pap.test.properties index 0f6f253e5..5b77fb18c 100644 --- a/ECOMP-PAP-REST/xacml.pap.test.properties +++ b/ECOMP-PAP-REST/xacml.pap.test.properties @@ -27,6 +27,8 @@ xacml.PAP.papEngineFactory=org.openecomp.policy.xacml.std.pap.StdEngineFactory # Dictionary and Policy Implementation Classes Properties dictionary.impl.className=org.openecomp.policy.pap.xacml.rest.handler.DictionaryHandlerImpl savePolicy.impl.className=org.openecomp.policy.pap.xacml.rest.handler.SavePolicyHandler +pushPolicy.impl.className=org.openecomp.policy.pap.xacml.rest.handler.PushPolicyHandler +deletePolicy.impl.className=org.openecomp.policy.pap.xacml.rest.handler.DeleteHandler #AutoPush Policy xacml.rest.pap.autopush.file=autopush.properties #=====================================================================================# |