diff options
author | BT2983 <BT2983@att.com> | 2018-10-07 22:39:46 -0600 |
---|---|---|
committer | BT2983 <BT2983@att.com> | 2018-10-07 22:39:46 -0600 |
commit | 07a976d05e824365778dac013df88b24ca5d055a (patch) | |
tree | cb3d85cd703a3ad2742390ac466a3df7f3e466f7 /ms/neng/src/main/java | |
parent | 0c283ec5b010e3af949efa032d9faea7c4383f5f (diff) |
Naming micro-service code.
Changes for bypassing normal flow when client passes an already resolved name. Plus, unit test updates, and code formatting updates to follow standards.
Change-Id: I7e432531b6f344f6aeb358e2b28b8a6fd74c5070
Issue-ID: CCSDK-608
Signed-off-by: BT2983 <BT2983@att.com>
Diffstat (limited to 'ms/neng/src/main/java')
11 files changed, 280 insertions, 100 deletions
diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGenerator.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGenerator.java index ec04960f..0e1cbc1f 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGenerator.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/gen/NameGenerator.java @@ -42,7 +42,6 @@ import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException; import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; -import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyPropertyMethodUtils; import org.onap.ccsdk.apps.ms.neng.core.policy.PolicySequence; import org.onap.ccsdk.apps.ms.neng.core.policy.PropertyOperator; import org.onap.ccsdk.apps.ms.neng.core.policy.RecipeParser; @@ -70,6 +69,7 @@ public class NameGenerator { private final List<Map<String, String>> allElements; private final Map<String, Map<String, String>> earlierNames; private final Map<String, Map<String, ?>> policyCache; + private final List<String> earlierNamingTypes; /** * Constructor. @@ -89,11 +89,13 @@ public class NameGenerator { * to names (which is a map with keys "resource-name", "resource-value" and "external-key") * @param policyCache cache containing policies retrieved in this transaction, to avoid repeated * calls to policy manager within the same transaction + * @param earlierNamingTypes naming-types used earlier in the same transaction */ public NameGenerator(PolicyFinder policyFinder, PolicyParameters policyParams, SequenceGenerator seqGenerator, DbNameValidator dbValidator, AaiNameValidator aaiValidator, NamePersister namePersister, Map<String, String> requestElement, List<Map<String, String>> allElements, - Map<String, Map<String, String>> earlierNames, Map<String, Map<String, ?>> policyCache) { + Map<String, Map<String, String>> earlierNames, Map<String, Map<String, ?>> policyCache, + List<String> earlierNamingTypes) { this.policyFinder = policyFinder; this.policyParams = policyParams; this.seqGenerator = seqGenerator; @@ -104,6 +106,7 @@ public class NameGenerator { this.allElements = allElements; this.earlierNames = earlierNames; this.policyCache = policyCache; + this.earlierNamingTypes = earlierNamingTypes; } /** @@ -117,17 +120,66 @@ public class NameGenerator { throw new NengException("Could not find policy name in the request"); } String namingType = findElementNamingType(); + String relaxedNamingType = relaxedNamingType(namingType); + Map<String,String> generated = null; if (namingType != null) { - Map<String, String> generated = this.earlierNames.get(namingType); + if (!earlierNamingTypes.contains(namingType)) { + generated = this.earlierNames.get(namingType); + if (generated == null) { + generated = this.earlierNames.get(relaxedNamingType); + } + } if (generated != null) { return generated; } + earlierNamingTypes.add(namingType); return generateNew(policyName, namingType); + } else { throw new NengException("Could not find naming type in the request for policy " + policyName); } } + /** + * Updates a generated name. + * + * @return the map (with keys "resource-name", "resource-value" and "external-key") containing the name. + */ + public Map<String, String> updateGenerateName() throws Exception { + String externalKey = findElementExternalKey(); + String resourceValue = value(this.requestElement, RESOURCE_VALUE_ELEMENT_ITEM); + String reqNamingType = findElementNamingType(); + String reqResourceName = findElementResourceName(); + String namingType = (reqNamingType == null) ? reqResourceName : reqNamingType; + String relaxedNamingType = relaxedNamingType(namingType); + + if (!aaiValidator.validate(namingType, resourceValue)) { + throw new NengException("Name already exists in AAI"); + } + GeneratedName generatedName = null; + if (relaxedNamingType != null) { + generatedName = namePersister.findByExternalIdAndElementType(externalKey, relaxedNamingType); + } else { + throw new NengException("Resource Name or naming type must be provided"); + } + if (generatedName == null) { + generatedName = new GeneratedName(); + } + generatedName.setName(resourceValue); + generatedName.setExternalId(externalKey); + generatedName.setElementType(namingType); + generatedName.setSequenceNumber(null); + generatedName.setSequenceNumberEnc(null); + generatedName.setPrefix(null); + generatedName.setSuffix(null); + generatedName.setIsReleased(null); + namePersister.persist(generatedName); + Map<String, String> respMap = buildResponse(externalKey, reqResourceName, resourceValue); + respMap.put(externalKey, "Resource value updated successfully"); + + return respMap; + } + String applyNameOperation(Map<String, ?> namingModel, String name) throws Exception { String nameOperation = namingOperation(namingModel); if (nameOperation != null && !"".equals(nameOperation)) { @@ -136,8 +188,8 @@ public class NameGenerator { return name; } - String applyPropertyOperation(String value, Map<String, ?> propertyMap) throws Exception { - return new PropertyOperator().apply(value, propertyMap, this.policyParams); + String applyPropertyOperation(String value, Map<String, ?> propertyMap, String recipeItem) throws Exception { + return new PropertyOperator().apply(value, propertyMap, this.policyParams, recipeItem); } static Map<String, String> buildResponse(String key, String name, String value) { @@ -210,14 +262,13 @@ public class NameGenerator { for (String recipeItem : recipe) { Map<String, ?> propMap = namingProperty(namingModel, recipeItem); if ("SEQUENCE".equals(recipeItem)) { - PolicySequence seq = seq(propMap); - recipeValues.put(recipeItem, seq); - } else if ("UUID".equals(recipeItem)) { - String uuid = PolicyPropertyMethodUtils.genUuid(); - recipeValues.put(recipeItem, uuid); - } else if ("TIMESTAMP".equals(recipeItem)) { - String ts = PolicyPropertyMethodUtils.getIsoDateString(); - recipeValues.put(recipeItem, ts); + String propValue = value(this.requestElement,recipeItem); + if (propValue != null) { + recipeValues.put(recipeItem, propValue); + } else { + PolicySequence seq = seq(propMap); + recipeValues.put(recipeItem, seq); + } } else { String val = generateNonSequenceValue(namingModels, policyName, namingType, namingModel, propMap, recipeItem); @@ -361,7 +412,13 @@ public class NameGenerator { val = generateValueRecursively(namingModels, policyName, recipeItem); } if (val != null) { - val = applyPropertyOperation(val, propMap); + val = applyPropertyOperation(val, propMap, null); + } + if (val == null) { + val = applyPropertyOperation(val, propMap, recipeItem); + } + if (val == null) { + val = value(namingModel, recipeItem); } return val; } @@ -389,7 +446,8 @@ public class NameGenerator { } if (relaxedElement != null) { NameGenerator recursive = new NameGenerator(policyFinder, policyParams, seqGenerator, dbValidator, - aaiValidator, namePersister, relaxedElement, allElements, earlierNames, policyCache); + aaiValidator, namePersister, relaxedElement, allElements, earlierNames, policyCache, + earlierNamingTypes); Map<String, String> gen = recursive.generateNew(policyName, relaxedNamingType, namingModels, relaxedModel); if (gen != null) { @@ -455,7 +513,7 @@ public class NameGenerator { seqEncoded = seqData.getSeqEncoded(); } GeneratedName record = new GeneratedName(); - GeneratedName releasedName = namePersister.findBy(namingType, name, "Y"); + GeneratedName releasedName = namePersister.findByElementTypeAndNameAndReleased(namingType, name, "Y"); if (releasedName != null) { record = releasedName; record.setLastUpdatedTime(new Timestamp(System.currentTimeMillis())); diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/persistence/NamePersister.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/persistence/NamePersister.java index 4875052b..cd07a46e 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/persistence/NamePersister.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/persistence/NamePersister.java @@ -47,7 +47,17 @@ public class NamePersister { * @param name A name * @param isReleased An Y/N flag indicating if the name is released or not */ - public GeneratedName findBy(String elementType, String name, String isReleased) { + public GeneratedName findByElementTypeAndNameAndReleased(String elementType, String name, String isReleased) { return repository.findByElementTypeAndNameAndIsReleased(elementType, name, isReleased); } + + /** + * Finds a name stored in the DB of the given external ID and type. + * + * @param externalId The external ID + * @param elementType The type of the name + */ + public GeneratedName findByExternalIdAndElementType(String externalId, String elementType) { + return repository.findByExternalIdAndRelaxedElementType(externalId, elementType); + } } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PolicyPropertyMethodUtils.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PolicyPropertyMethodUtils.java index 67805246..79ad2a7a 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PolicyPropertyMethodUtils.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PolicyPropertyMethodUtils.java @@ -77,7 +77,9 @@ public class PolicyPropertyMethodUtils { */ public static String genUuid() { UUID uuid = UUID.randomUUID(); - return uuid.toString(); + String str = uuid.toString(); + str = str.replaceAll("-", ""); + return str; } /** diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PolicyReader.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PolicyReader.java index 778c4781..73f46c95 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PolicyReader.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PolicyReader.java @@ -25,6 +25,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; /** * Converts policy data to the structure expected by this micro-service. @@ -163,7 +164,11 @@ public abstract class PolicyReader implements PolicyFinder { * Finds the property-value from the given property map. */ public static String propertyValue(Map<String, ?> properties) { - return value(properties, "property-value"); + String value = value(properties, "property-value"); + if (value != null && !Pattern.matches("\\$\\{.*\\}.*", value)) { + return value; + } + return null; } /** @@ -175,6 +180,9 @@ public abstract class PolicyReader implements PolicyFinder { * @param type the naming-type */ public static String relaxedNamingType(String type) { + if (type == null) { + return type; + } type = type.toUpperCase(); if (type.endsWith("NAME")) { type = type.substring(0, type.length() - 4); @@ -221,6 +229,11 @@ public abstract class PolicyReader implements PolicyFinder { if (!(value instanceof String)) { value = null; } + if (value != null && !Pattern.matches("\\$\\{.*\\}.*", value)) { + return value; + } else { + value = null; + } } return value; } @@ -259,6 +272,11 @@ public abstract class PolicyReader implements PolicyFinder { if (!(value instanceof String)) { value = null; } + if (value != null && !Pattern.matches("\\$\\{.*\\}.*", value)) { + return value; + } else { + value = null; + } } return value; } else { diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PropertyOperator.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PropertyOperator.java index 7eb63244..448ee075 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PropertyOperator.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/policy/PropertyOperator.java @@ -38,10 +38,12 @@ public class PropertyOperator { * @param propertyMap a map representing the property, the key of which is "property-operation" * and the value is the actual property * @param policyParams parameters configuring policy + * @param recipeItem a special recipe item (such as TIMESTAMP, UUID) * @return the result of applying the property * @throws Exception all exceptions are propagated */ - public String apply(String value, Map<String, ?> propertyMap, PolicyParameters policyParams) throws Exception { + public String apply(String value, Map<String, ?> propertyMap, PolicyParameters policyParams, + String recipeItem) throws Exception { String op = propertyOperation(propertyMap); String mapped = null; if (op != null) { @@ -56,6 +58,8 @@ public class PropertyOperator { } } value = applyJavaOperation(value, op, mapped); + } else if (recipeItem != null) { + value = applyOperationByRecipeName(recipeItem, policyParams); } return value; } @@ -99,8 +103,11 @@ public class PropertyOperator { argPart = op.substring(funcStartIndex + 1, funcEndIndex); } - argPart = inputString + "," + argPart; - String[] args = argPart.split(","); + String[] args = new String[0]; + if (inputString != null) { + argPart = inputString + "," + argPart; + args = argPart.split(","); + } PolicyPropertyMethodUtils utils = new PolicyPropertyMethodUtils(); for (Method m : PolicyPropertyMethodUtils.class.getDeclaredMethods()) { @@ -115,6 +122,15 @@ public class PropertyOperator { } return postOp; } + + private String applyOperationByRecipeName(String recipeItem, PolicyParameters policyParams) throws Exception { + String mapped = policyParams.mapFunction(recipeItem); + if (mapped == null) { + mapped = camelConverted(recipeItem); + } + String value = applyJavaOperation(null, recipeItem, mapped); + return value; + } static String operationFunction(String operation) throws Exception { operation = operation.trim(); @@ -149,4 +165,6 @@ public class PropertyOperator { } return buf.toString(); } + + } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringService.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringService.java index 1cdd61e1..982ffd38 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringService.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringService.java @@ -20,6 +20,8 @@ package org.onap.ccsdk.apps.ms.neng.core.service; +import java.util.List; +import java.util.Map; import org.onap.ccsdk.apps.ms.neng.core.resource.model.HelloWorld; import org.onap.ccsdk.apps.ms.neng.core.resource.model.NameGenRequest; import org.onap.ccsdk.apps.ms.neng.core.resource.model.NameGenResponse; @@ -30,33 +32,43 @@ import org.onap.ccsdk.apps.ms.neng.persistence.entity.PolicyDetails; */ public interface SpringService { /** - * Name generation API. + * API to add a naming policy to the database cache in this micro-service. + * + * <p/>This is not used by clients -- it is here to help with diagnostics. */ - public NameGenResponse genNetworkElementName(NameGenRequest request) throws Exception; + public void addPolicy(Object request) throws Exception; /** - * Name removal API. + * Name generation API. */ - public NameGenResponse releaseNetworkElementName(NameGenRequest request) throws Exception; + public NameGenResponse genNetworkElementName(NameGenRequest request) throws Exception; /** - * API to return naming policy cached in this micro-service. - * - * <p/>This is not used by clients -- it is here to help with diagnostics. + * Name generation or update API. */ - public PolicyDetails getPolicyDetails(String policyName); + public NameGenResponse generateOrUpdateName(NameGenRequest request) throws Exception; /** - * API to add a naming policy to the database cache in this micro-service. + * Heart-beat/ping API. * * <p/>This is not used by clients -- it is here to help with diagnostics. */ - public void addPolicy(Object request) throws Exception; + public HelloWorld getQuickHello(String name); /** - * Heart-beat/ping API. + * API to return naming policy cached in this micro-service. * * <p/>This is not used by clients -- it is here to help with diagnostics. */ - public HelloWorld getQuickHello(String name); + public PolicyDetails getPolicyDetails(String policyName); + + /** + * Name removal API. + */ + public NameGenResponse releaseNetworkElementName(NameGenRequest request) throws Exception; + + /** + * Name update API. + */ + public List<Map<String, String>> updateNetworkElementName(NameGenRequest request) throws Exception; } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java index a8ca4bb6..01c8324d 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/SpringServiceImpl.java @@ -28,15 +28,15 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.logging.Logger; -import java.util.stream.Collectors; +import java.util.regex.Pattern; import javax.transaction.Transactional; import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException; import org.onap.ccsdk.apps.ms.neng.core.gen.NameGenerator; import org.onap.ccsdk.apps.ms.neng.core.persistence.NamePersister; import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyFinder; import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyParameters; +import org.onap.ccsdk.apps.ms.neng.core.policy.PolicyReader; import org.onap.ccsdk.apps.ms.neng.core.resource.model.HelloWorld; import org.onap.ccsdk.apps.ms.neng.core.resource.model.NameGenRequest; import org.onap.ccsdk.apps.ms.neng.core.resource.model.NameGenResponse; @@ -76,18 +76,33 @@ public class SpringServiceImpl implements SpringService { @Autowired GeneratedNameRespository generatedNameRepository; /** - * Heart-beat/ping API. + * API to add a naming policy to the database cache in this micro-service. */ @Override - public HelloWorld getQuickHello(String name) { - if (name == null || name.isEmpty()) { - name = "world"; + public void addPolicy(Object request) throws Exception { + @SuppressWarnings("unchecked") + Map<String, Object> policyData = (Map<String, Object>)request; + PolicyDetails pd = new PolicyDetails(); + String name = (String) policyData.get("policyName"); + pd.setPolicyName(name); + ObjectMapper objectmapper = new ObjectMapper(); + log.info(objectmapper.writeValueAsString(policyData.get("policyValue"))); + pd.setPolicyResponse((String)policyData.get("policyValue")); + policyDetailsRepository.save(pd); + } + + /** + * Name generation API -- generates or updates the name. + */ + @Transactional(rollbackOn = Exception.class) + public NameGenResponse generateOrUpdateName(NameGenRequest request) throws Exception { + if (isUpdateRequest(request)) { + NameGenResponse resp = new NameGenResponse(); + resp.setElements(updateNetworkElementName(request)); + return resp; + } else { + return genNetworkElementName(request); } - String message = "Hello " + name + "!"; - log.info(message); - HelloWorld hello = new HelloWorld(message); - log.info(hello.toString()); - return hello; } /** @@ -100,8 +115,9 @@ public class SpringServiceImpl implements SpringService { List<Map<String, String>> allElements = new ArrayList<>(); Map<String, Map<String, ?>> policyCache = new HashMap<>(); List<Map<String, String>> generatedNames = new ArrayList<>(); + List<String> earlierNamingTypes = new ArrayList<String>(); validateRequest(request); - if (!request.getElements().isEmpty()) { + if (request.getElements() != null && !request.getElements().isEmpty()) { allElements.addAll(request.getElements()); } PolicyFinder policyFinderImpl = findPolicyFinderImpl(request); @@ -109,7 +125,7 @@ public class SpringServiceImpl implements SpringService { log.info("Processing " + requestElement.toString()); NameGenerator nameGen = new NameGenerator(policyFinderImpl, policyParameters, sequenceGenerator, dbNameValidator, aaiNameValidator, namePersister, requestElement, allElements, - earlierNames, policyCache); + earlierNames, policyCache, earlierNamingTypes); generatedNames.add(nameGen.generate()); } NameGenResponse resp = new NameGenResponse(); @@ -126,6 +142,33 @@ public class SpringServiceImpl implements SpringService { } /** + * API to return naming policy cached in this micro-service. + */ + @Override + public PolicyDetails getPolicyDetails(String policyName) { + try { + return policyDetailsRepository.findPolicyResponseByName(policyName); + } catch (Exception e) { + return new PolicyDetails(); + } + } + + /** + * Heart-beat/ping API. + */ + @Override + public HelloWorld getQuickHello(String name) { + if (name == null || name.isEmpty()) { + name = "world"; + } + String message = "Hello " + name + "!"; + log.info(message); + HelloWorld hello = new HelloWorld(message); + log.info(hello.toString()); + return hello; + } + + /** * Name removal API. */ @Transactional(rollbackOn = Exception.class) @@ -154,31 +197,19 @@ public class SpringServiceImpl implements SpringService { } /** - * API to return naming policy cached in this micro-service. + * Name update API. */ @Override - public PolicyDetails getPolicyDetails(String policyName) { - try { - return policyDetailsRepository.findPolicyResponseByName(policyName); - } catch (Exception e) { - return new PolicyDetails(); + @Transactional(rollbackOn = Exception.class) + public List<Map<String, String>> updateNetworkElementName(NameGenRequest request) throws Exception { + List<Map<String, String>> responseList = new ArrayList<>(); + for (Map<String, String> reqElement :request.getElements()) { + NameGenerator nameGen = new NameGenerator(null, null,null, null, aaiNameValidator, namePersister, + reqElement, request.getElements(),null, null, null); + Map<String, String> resp = nameGen.updateGenerateName(); + responseList.add(resp); } - } - - /** - * API to add a naming policy to the database cache in this micro-service. - */ - @Override - public void addPolicy(Object request) throws Exception { - @SuppressWarnings("unchecked") - Map<String, Object> policyData = (Map<String, Object>)request; - PolicyDetails pd = new PolicyDetails(); - String name = (String) policyData.get("policyName"); - pd.setPolicyName(name); - ObjectMapper objectmapper = new ObjectMapper(); - log.info(objectmapper.writeValueAsString(policyData.get("policyValue"))); - pd.setPolicyResponse((String)policyData.get("policyValue")); - policyDetailsRepository.save(pd); + return responseList; } void buildUnAssignResponse(List<GeneratedName> generatedNames, NameGenResponse response) { @@ -195,22 +226,6 @@ public class SpringServiceImpl implements SpringService { } void validateRequest(NameGenRequest request) throws Exception { - List<Map<String, String>> elems = request.getElements(); - if (elems != null && !elems.isEmpty()) { - boolean error = false; - Set<String> externalKeySet = elems.stream().map(s -> s.get("external-key")).collect(Collectors.toSet()); - if (externalKeySet.size() != request.getElements().size()) { - error = true; - } - for (String externalKey : externalKeySet) { - if (externalKey == null || externalKeyValidator.isPresent(externalKey)) { - error = true; - } - } - if (error) { - throw new NengException("External Key is required and must be unique"); - } - } } private PolicyFinder findPolicyFinderImpl(NameGenRequest request) { @@ -224,4 +239,14 @@ public class SpringServiceImpl implements SpringService { } return this.policyFinder; } + + private boolean isUpdateRequest(NameGenRequest request) throws Exception { + for (Map<String, String> reqElement : request.getElements()) { + String resourceValue = PolicyReader.value(reqElement, "resource-value"); + if (resourceValue != null && !Pattern.matches("\\$\\{.*\\}.*", resourceValue)) { + return true; + } + } + return false; + } } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestService.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestService.java index 8319a545..8d6db91b 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestService.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestService.java @@ -24,7 +24,6 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; -import java.util.Map; import javax.validation.Valid; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -87,7 +86,7 @@ public interface RestService { @POST @Path("/v1/addPolicy") @Produces({MediaType.APPLICATION_JSON}) - public Map<String, Object> addPolicyToDb(Object request) throws Exception; + public Response addPolicyToDb(Object request) throws Exception; /** * Heart-beat/ping API. diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestServiceImpl.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestServiceImpl.java index bc8cab42..e52e0016 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestServiceImpl.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/core/service/rs/RestServiceImpl.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.logging.Logger; import javax.validation.Valid; import javax.ws.rs.core.Response; +import org.onap.ccsdk.apps.ms.neng.core.exceptions.NengException; import org.onap.ccsdk.apps.ms.neng.core.resource.model.HelloWorld; import org.onap.ccsdk.apps.ms.neng.core.resource.model.NameGenRequest; import org.onap.ccsdk.apps.ms.neng.core.resource.model.NameGenResponse; @@ -40,6 +41,7 @@ import org.springframework.web.bind.annotation.RequestBody; @Component public class RestServiceImpl implements RestService { private static Logger log = Logger.getLogger(RestServiceImpl.class.getName()); + private static final String INTERNAL_ERROR_MSG = "Internal error occured while processing the request."; @Autowired SpringService service; @@ -59,12 +61,18 @@ public class RestServiceImpl implements RestService { @Override public Response generateNetworkElementName(@RequestBody @Valid NameGenRequest request) { log.info("Received request: " + request.toString()); + Map<String, Object> response = new HashMap<>(); try { - NameGenResponse resp = service.genNetworkElementName(request); - return Response.ok().entity(resp).build(); + NameGenResponse resp = service.generateOrUpdateName(request); + return buildResponse(resp); + } catch (NengException e) { + log.warning(e.getMessage()); + response.put("error", buildErrorResponse("NELGEN-0003", e.getMessage())); + return buildErrorResponse(response); } catch (Exception e) { log.warning(e.getMessage()); - return Response.status(500).entity("{ \"error\": \"" + e.getMessage() + "\" }").build(); + response.put("error", buildErrorResponse("err-0500", INTERNAL_ERROR_MSG)); + return buildErrorResponse(response); } } @@ -74,12 +82,18 @@ public class RestServiceImpl implements RestService { @Override public Response releaseNetworkElementName(NameGenRequest request) { NameGenResponse resp; + Map<String, Object> response = new HashMap<>(); try { resp = service.releaseNetworkElementName(request); - return Response.ok().entity(resp).build(); + return buildResponse(resp); + } catch (NengException e) { + log.warning(e.getMessage()); + response.put("error", buildErrorResponse("NELGEN-0002", e.getMessage())); + return buildErrorResponse(response); } catch (Exception e) { log.warning(e.getMessage()); - return Response.status(500).entity("{ \"error\": \"" + e.getMessage() + "\" }").build(); + response.put("error", buildErrorResponse("err-0500", INTERNAL_ERROR_MSG)); + return buildErrorResponse(response); } } @@ -98,15 +112,31 @@ public class RestServiceImpl implements RestService { * API to add a naming policy to the database cache in this micro-service. */ @Override - public Map<String, Object> addPolicyToDb(Object request) throws Exception { - Map<String, Object> respMap = new HashMap<>(); + public Response addPolicyToDb(Object request) throws Exception { + Map<String, Object> response = new HashMap<>(); try { service.addPolicy(request); - respMap.put("status", "Policy added successfully"); + response.put("status", "Policy added successfully"); + return buildResponse(response); } catch (Exception e) { log.warning(e.getMessage()); - respMap.put("status", "Failed"); + response.put("error", buildErrorResponse("err-0500", e.getMessage())); + return buildErrorResponse(response); } - return respMap; + } + + Response buildResponse(Object response) { + return Response.ok().entity(response).build(); + } + + Response buildErrorResponse(Map<String, Object> response) { + return Response.status(500).entity(response).build(); + } + + Map<String,Object> buildErrorResponse(String errorCode, String message) { + Map<String,Object> error = new HashMap<>(); + error.put("errorId", errorCode); + error.put("message", message); + return error; } } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/persistence/repository/GeneratedNameRespository.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/persistence/repository/GeneratedNameRespository.java index 4f5f65d5..ed2ac531 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/persistence/repository/GeneratedNameRespository.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/persistence/repository/GeneratedNameRespository.java @@ -42,6 +42,15 @@ public interface GeneratedNameRespository extends CrudRepository<GeneratedName, public List<GeneratedName> findByExternalId(String externalId); /* + * Finds entities for a given external system ID and element type, ignoring any special characters in names. + */ + @Query(value = "select * from Generated_Name g where g.external_Id=:externalId and " + + "REPLACE(REPLACE(REPLACE(element_type,'NAME',''),'-',''),'_','')=:elementType", + nativeQuery = true) + public GeneratedName findByExternalIdAndRelaxedElementType(@Param("externalId")String externalId, + @Param("elementType")String elementType); + + /* * Finds the maximum sequence number used for a given prefix and suffix. */ @Query("select max(sequenceNumber) from GeneratedName where prefix=:prefix " @@ -64,4 +73,6 @@ public interface GeneratedNameRespository extends CrudRepository<GeneratedName, @Query("select g from GeneratedName g where g.elementType=:elementType " + "and g.name=:name and (g.isReleased is null or g.isReleased ='N')") public GeneratedName findUnReleased(@Param("elementType") String elementType, @Param("name") String name); + + } diff --git a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/AaiServiceImpl.java b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/AaiServiceImpl.java index 4dd45725..6c4ae889 100644 --- a/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/AaiServiceImpl.java +++ b/ms/neng/src/main/java/org/onap/ccsdk/apps/ms/neng/service/extinf/impl/AaiServiceImpl.java @@ -23,9 +23,6 @@ package org.onap.ccsdk.apps.ms.neng.service.extinf.impl; import com.fasterxml.jackson.databind.ObjectMapper; import java.net.URI; import java.util.logging.Logger; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLSession; import org.apache.http.client.HttpClient; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.HttpClientBuilder; |