X-Git-Url: https://gerrit.onap.org/r/gitweb?p=policy%2Fengine.git;a=blobdiff_plain;f=ONAP-PAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fpap%2Fxacml%2Frest%2Fcontroller%2FDictionaryImportController.java;h=ef475039d782ebbe1f8cc60619c4a02355cda982;hp=9423017c31b0e604b01298692ecc2cfaa3a877d2;hb=2f3ca9fc5edc5a52659b3486605e0db508143318;hpb=b6d9063e06ab8cdf2d97fc75810792659344e4a8 diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryImportController.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryImportController.java index 9423017c3..ef475039d 100644 --- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryImportController.java +++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/controller/DictionaryImportController.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * ONAP-PAP-REST * ================================================================================ - * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,6 +20,11 @@ package org.onap.policy.pap.xacml.rest.controller; +import au.com.bytecode.opencsv.CSVReader; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; + import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; @@ -39,9 +44,9 @@ import org.onap.policy.rest.jpa.ActionList; import org.onap.policy.rest.jpa.ActionPolicyDict; import org.onap.policy.rest.jpa.AddressGroup; import org.onap.policy.rest.jpa.Attribute; -import org.onap.policy.rest.jpa.BRMSController; -import org.onap.policy.rest.jpa.BRMSDependency; -import org.onap.policy.rest.jpa.BRMSParamTemplate; +import org.onap.policy.rest.jpa.BrmsController; +import org.onap.policy.rest.jpa.BrmsDependency; +import org.onap.policy.rest.jpa.BrmsParamTemplate; import org.onap.policy.rest.jpa.Category; import org.onap.policy.rest.jpa.ClosedLoopD2Services; import org.onap.policy.rest.jpa.ClosedLoopSite; @@ -51,7 +56,7 @@ import org.onap.policy.rest.jpa.DescriptiveScope; import org.onap.policy.rest.jpa.GroupServiceList; import org.onap.policy.rest.jpa.MicroServiceModels; import org.onap.policy.rest.jpa.OnapName; -import org.onap.policy.rest.jpa.PEPOptions; +import org.onap.policy.rest.jpa.PepOptions; import org.onap.policy.rest.jpa.PrefixList; import org.onap.policy.rest.jpa.ProtocolList; import org.onap.policy.rest.jpa.SecurityZone; @@ -67,712 +72,758 @@ 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.ObjectMapper; +@Controller +public class DictionaryImportController { + private static final Logger LOGGER = FlexLogger.getLogger(DictionaryImportController.class); -import au.com.bytecode.opencsv.CSVReader; + private static CommonClassDao commonClassDao; + private static final String DESCRIPTION = "description"; + private static final String ERROR = "Error"; + private static final String DEPENDENCY = "dependency"; + @Autowired + public DictionaryImportController(CommonClassDao commonClassDao) { + setCommonClassDao(commonClassDao); + } -@Controller -public class DictionaryImportController { - private static final Logger LOGGER = FlexLogger.getLogger(DictionaryImportController.class); - - private static CommonClassDao commonClassDao; - private static final String DESCRIPTION= "description"; - private static final String ERROR= "Error"; - private static final String DEPENDENCY= "dependency"; - - @Autowired - public DictionaryImportController(CommonClassDao commonClassDao){ - setCommonClassDao(commonClassDao); - } - - public static void setCommonClassDao(CommonClassDao commonClassDao) { - DictionaryImportController.commonClassDao = commonClassDao; - } - - public DictionaryImportController(){ - super(); - } + public static void setCommonClassDao(CommonClassDao commonClassDao) { + DictionaryImportController.commonClassDao = commonClassDao; + } + + public DictionaryImportController() { + super(); + } + + @RequestMapping(value = {"/dictionary/import_dictionary"}, method = {RequestMethod.POST}) + public void importDictionaryData(HttpServletRequest request, HttpServletResponse response) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + String userId = request.getParameter("userId"); + String dictionaryName = request.getParameter("dictionaryName"); + + if (dictionaryName == null || dictionaryName.isEmpty()) { + LOGGER.error("dictionaryName is null/empty"); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + response.getWriter().write(ERROR); + return; + } + + // fix Fortify Path Manipulation issue + if (!isValidDictionaryName(dictionaryName)) { + LOGGER.error("dictionaryName is invalid"); + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + response.getWriter().write(ERROR); + return; + } + File file = new File(dictionaryName); + try (OutputStream outputStream = new FileOutputStream(file); + FileReader fileReader = new FileReader(file.toString())) { + IOUtils.copy(request.getInputStream(), outputStream); + CSVReader csvReader = new CSVReader(fileReader); + List dictSheet = csvReader.readAll(); + if (dictionaryName.startsWith("Attribute")) { + for (int i = 1; i < dictSheet.size(); i++) { + Attribute attribute = new Attribute(""); + UserInfo userinfo = new UserInfo(); + userinfo.setUserLoginId(userId); + attribute.setUserCreatedBy(userinfo); + attribute.setUserModifiedBy(userinfo); + String[] rows = dictSheet.get(i); + for (int j = 0; j < rows.length; j++) { + if ("xacml_id".equalsIgnoreCase(dictSheet.get(0)[j]) + || "Attribute ID".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setXacmlId(rows[j]); + } + if (DESCRIPTION.equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setDescription(rows[j]); + } + if ("priority".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setPriority(rows[j]); + } + if ("datatype".equalsIgnoreCase(dictSheet.get(0)[j]) + || "Data Type".equalsIgnoreCase(dictSheet.get(0)[j])) { + Datatype dataType = new Datatype(); + if ("string".equalsIgnoreCase(rows[j])) { + dataType.setId(26); + } else if ("integer".equalsIgnoreCase(rows[j])) { + dataType.setId(12); + } else if ("double".equalsIgnoreCase(rows[j])) { + dataType.setId(25); + } else if ("boolean".equalsIgnoreCase(rows[j])) { + dataType.setId(18); + } else if ("user".equalsIgnoreCase(rows[j])) { + dataType.setId(29); + } + attribute.setDatatypeBean(dataType); + Category category = new Category(); + category.setId(5); + attribute.setCategoryBean(category); + } + if ("attribute_value".equalsIgnoreCase(dictSheet.get(0)[j]) + || "Attribute Value".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setAttributeValue(rows[j]); + } + } + commonClassDao.save(attribute); + } + } + if (dictionaryName.startsWith("ActionPolicyDictionary")) { + for (int i = 1; i < dictSheet.size(); i++) { + ActionPolicyDict attribute = new ActionPolicyDict(); + UserInfo userinfo = new UserInfo(); + userinfo.setUserLoginId(userId); + attribute.setUserCreatedBy(userinfo); + attribute.setUserModifiedBy(userinfo); + String[] rows = dictSheet.get(i); + for (int j = 0; j < rows.length; j++) { + if ("attribute_name".equalsIgnoreCase(dictSheet.get(0)[j]) + || "Attribute Name".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setAttributeName(rows[j]); + } + if ("body".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setBody(rows[j]); + } + if (DESCRIPTION.equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setDescription(rows[j]); + } + if ("headers".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setHeader(rows[j]); + } + if ("method".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setMethod(rows[j]); + } + if ("type".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setType(rows[j]); + } + if ("url".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setUrl(rows[j]); + } + } + commonClassDao.save(attribute); + } + } + if (dictionaryName.startsWith("OnapName")) { + for (int i = 1; i < dictSheet.size(); i++) { + OnapName attribute = new OnapName(); + UserInfo userinfo = new UserInfo(); + userinfo.setUserLoginId(userId); + attribute.setUserCreatedBy(userinfo); + attribute.setUserModifiedBy(userinfo); + String[] rows = dictSheet.get(i); + for (int j = 0; j < rows.length; j++) { + if ("onap_name".equalsIgnoreCase(dictSheet.get(0)[j]) + || "Onap Name".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setName(rows[j]); + } + if (DESCRIPTION.equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setDescription(rows[j]); + } + } + commonClassDao.save(attribute); + } + } + + if (dictionaryName.startsWith("MSPolicyDictionary")) { + for (int i = 1; i < dictSheet.size(); i++) { + MicroServiceModels attribute = new MicroServiceModels(); + UserInfo userinfo = new UserInfo(); + userinfo.setUserLoginId(userId); + attribute.setUserCreatedBy(userinfo); + String[] rows = dictSheet.get(i); + for (int j = 0; j < rows.length; j++) { + if ("modelName".equalsIgnoreCase(dictSheet.get(0)[j]) + || "Micro Service Model".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setModelName(rows[j]); + } + if ("version".equalsIgnoreCase(dictSheet.get(0)[j]) + || "Model Version".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setVersion(rows[j]); + } + if (DESCRIPTION.equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setDescription(rows[j]); + } + if (DEPENDENCY.equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setDependency(rows[j]); + } + if ("attributes".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setAttributes(rows[j]); + } + if ("enumValues".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setEnumValues(rows[j]); + } + if ("Ref Attributes".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setRefAttributes(rows[j]); + } + if ("Sub Attributes".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setSubAttributes(rows[j]); + } + if ("annotations".equalsIgnoreCase(dictSheet.get(0)[j])) { + attribute.setAnnotation(rows[j]); + } + } - @RequestMapping(value={"/dictionary/import_dictionary"}, method={RequestMethod.POST}) - public void importDictionaryData(HttpServletRequest request, HttpServletResponse response) throws IOException{ - ObjectMapper mapper = new ObjectMapper(); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - String userId = request.getParameter("userId"); - String dictionaryName = request.getParameter("dictionaryName"); - - if(dictionaryName == null || dictionaryName.isEmpty()){ - LOGGER.error("dictionaryName is null/empty"); - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); - response.getWriter().write(ERROR); - return; - } - - // fix Fortify Path Manipulation issue - if(!isValidDictionaryName(dictionaryName)){ - LOGGER.error("dictionaryName is invalid"); - response.setStatus(HttpServletResponse.SC_BAD_REQUEST); - response.getWriter().write(ERROR); - return; - } - File file = new File(dictionaryName); - try(OutputStream outputStream = new FileOutputStream(file); FileReader fileReader = new FileReader(file.toString())){ - IOUtils.copy(request.getInputStream(), outputStream); - CSVReader csvReader = new CSVReader(fileReader); - List dictSheet = csvReader.readAll(); - if(dictionaryName.startsWith("Attribute")){ - for(int i = 1; i< dictSheet.size(); i++){ - Attribute attribute = new Attribute(""); - UserInfo userinfo = new UserInfo(); - userinfo.setUserLoginId(userId); - attribute.setUserCreatedBy(userinfo); - attribute.setUserModifiedBy(userinfo); - String[] rows = dictSheet.get(i); - for (int j=0 ; j