From 0570deb66e8815ad78a486516ebe5273b3238e78 Mon Sep 17 00:00:00 2001 From: Vidyashree Rama Date: Wed, 17 Apr 2019 09:10:56 +0530 Subject: Code refactoring in restconf client yang serialisers. 1. logging the exception when handling underscore in yang serialisers 2. Refactor code to not nest more than 3 if/for statements 3. Throw SvcLogicException instead of generic 4. Reduce switch case number of lines from 8 to at most 5 Issue-ID: CCSDK-1236 Change-Id: Ic02ba424c2d1748d071e38d25ae159f5d8bd6451 Signed-off-by: Vidyashree Rama --- .../MdsalPropertiesNodeSerializer.java | 51 ++++++++++++++++------ .../pnserializer/MdsalPropertiesNodeUtils.java | 31 ++++++++----- 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeSerializer.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeSerializer.java index 759fe802..0eca40d0 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeSerializer.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeSerializer.java @@ -29,6 +29,8 @@ import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.DOT_REGEX; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.SLASH; @@ -52,6 +54,8 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.S */ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer { + private static final Logger log = LoggerFactory.getLogger( + MdsalPropertiesNodeSerializer.class); private SchemaNode curSchema; private PropertiesNode node; @@ -111,6 +115,8 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer childsFromAugmentation = parent - .augmentations().get(augSchema); - if (!childsFromAugmentation.isEmpty()) { - for (PropertiesNode pNode : childsFromAugmentation) { - if (pNode.name().equals(name)) { - return pNode; - } + if (augSchema == null) { + return null; + } + + Collection childsFromAugmentation = parent + .augmentations().get(augSchema); + if (!childsFromAugmentation.isEmpty()) { + for (PropertiesNode pNode : childsFromAugmentation) { + if (pNode.name().equals(name)) { + return pNode; } } } + return null; } @@ -218,10 +221,12 @@ public final class MdsalPropertiesNodeUtils { * @param appInfo application info * @param type node type * @return new properties node + * @throws SvcLogicException exception while creating properties node */ public static PropertiesNode createNode(String name, Namespace namespace, String uri, PropertiesNode parent, - Object appInfo, NodeType type) { + Object appInfo, NodeType type) + throws SvcLogicException { switch (type) { case SINGLE_INSTANCE_NODE: return new SingleInstanceNode(name, namespace, uri, parent, appInfo, type); @@ -230,7 +235,7 @@ public final class MdsalPropertiesNodeUtils { case MULTI_INSTANCE_LEAF_HOLDER_NODE: return new LeafListHolderNode(name, namespace, uri, parent, appInfo, type); default: - throw new RuntimeException("Invalid node type"); + throw new SvcLogicException("Invalid node type " + type); } } @@ -265,6 +270,9 @@ public final class MdsalPropertiesNodeUtils { return new SchemaPathHolder(id, uri1); } catch (IllegalArgumentException | RestconfDocumentedException | NullPointerException e) { + log.info("Exception while converting uri to instance identifier" + + " context. Process each node in uri to get instance identifier" + + " context " + e); return processNodesAndAppendPath(uri, context); } } @@ -294,6 +302,7 @@ public final class MdsalPropertiesNodeUtils { try { id = processIdentifier(uriParts[i], context, actPath); } catch (IllegalArgumentException e) { + log.info(format(EXC_MSG, e)); id.setUri(actPath+ uriParts[i] + sec); return id; } @@ -340,7 +349,7 @@ public final class MdsalPropertiesNodeUtils { return new SchemaPathHolder(id, val); } catch (IllegalArgumentException | RestconfDocumentedException | NullPointerException e) { - log.info(format(INFO_MSG, val)); + log.info(format(INFO_MSG, val, e)); } firstHalf.append(values[i]).append(UNDERSCORE); secondHalf = secondHalf.replaceFirst( -- cgit 1.2.3-korg