From 8e8ec2eb81e062010da230fae30626cb07c25bd1 Mon Sep 17 00:00:00 2001 From: Vidyashree Rama Date: Thu, 9 Aug 2018 12:21:34 +0530 Subject: RestconfDiscoveryNode Plugin implementation Initial code submit for supporting RestconfDiscoveryNode Plugin implementation Issue-ID: CCSDK-374 Change-Id: Ieb0b622b135ea78ef58bd36dfe171f4117bc3328 Signed-off-by: Vidyashree Rama --- .../plugins/restconfdiscovery/SseServerMock.java | 68 +++++++++++++ .../TestRestconfDiscoveryNode.java | 105 +++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SseServerMock.java create mode 100644 restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SseServerMock.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SseServerMock.java new file mode 100644 index 000000000..1b234a236 --- /dev/null +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SseServerMock.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.restconfdiscovery; + +import org.glassfish.jersey.media.sse.EventOutput; +import org.glassfish.jersey.media.sse.OutboundEvent; +import org.glassfish.jersey.media.sse.SseFeature; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import java.io.IOException; + +@Path("events") +public class SseServerMock { + + @GET + @Produces(SseFeature.SERVER_SENT_EVENTS) + public EventOutput getServerSentEvents() throws IOException { + String data = "{" + + "\"ietf-notification:notification\" : {" + + " \"eventTime\" : \"2017-10-25T08:22:33.44Z\"," + + " \"ietf-yang-push:push-change-update\": {" + + "\"subscription-id\":\"89\"," + + "\"datastore-changes\": {" + + "\"ietf-yang-patch:yang-patch\":{" + + "\"patch-id\":\"1\"," + + "\"edit\":[{" + + "\"edit-id\":\"edit1\"," + + "\"operation\":\"merge\"," + + "\"target\":\"/ietf-interfaces:interfaces-state\"," + + "\"value\": {" + + "\"ietf-interfaces:interfaces-state\":{"+ + "\"interface\": {" + + "\"name\":\"eth0\"," + + "\"oper-status\":\"down\"," + + "}" + + "}" + + "}" + + "}]"+ + "}" + + "}" + + "}" + + "}" + + "}"; + final EventOutput result = new EventOutput(); + result.write(new OutboundEvent.Builder().data(String.class, data).build()); + result.close(); + return result; + } +} diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java new file mode 100644 index 000000000..db878f0af --- /dev/null +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.restconfdiscovery; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.media.sse.SseFeature; +import org.glassfish.jersey.server.ResourceConfig; +import org.junit.Test; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class TestRestconfDiscoveryNode { + + private static final URI CONTEXT = URI.create("http://localhost:8080/"); + + @Test + public void testEstablishPersistentConnection() throws SvcLogicException, + InterruptedException { + final ResourceConfig resourceConfig = new ResourceConfig( + SseServerMock.class, SseFeature.class); + GrizzlyHttpServerFactory.createHttpServer(CONTEXT, resourceConfig); + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("prop.encoding-json", "encoding-json"); + ctx.setAttribute("restapi-result.response-code", "200"); + ctx.setAttribute("restapi-result.ietf-subscribed-notifications" + + ":output.identifier", "100"); + + Map p = new HashMap<>(); + p.put("sseConnectURL", "http://localhost:8080/events"); + p.put("subscriberId", "networkId"); + p.put("responsePrefix", "restapi-result"); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(); + rdn.establishPersistentConnection(p, ctx, "networkId"); + Thread.sleep(2000); + rdn.deleteSubscription(p, ctx); + } + + @Test(expected = SvcLogicException.class) + public void testSubGraphExecution() throws SvcLogicException{ + SvcLogicGraphInfo subDg = new SvcLogicGraphInfo(); + subDg.mode("sync"); + subDg.module("l3VpnService"); + subDg.rpc("createVpn"); + subDg.version("1.0"); + SvcLogicContext ctx = new SvcLogicContext(); + subDg.executeGraph(ctx); + } + + @Test(expected = SvcLogicException.class) + public void testEstablishSubscriptionWithoutSubscriberId() + throws SvcLogicException{ + SvcLogicContext ctx = new SvcLogicContext(); + Map p = new HashMap<>(); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(); + rdn.establishSubscription(p, ctx); + } + + @Test + public void testResponseCode() { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("restapi-result.response-code", "200"); + ctx.setAttribute("response-code", "404"); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(); + assertThat(rdn.getResponseCode("restapi-result", ctx), + is("200")); + assertThat(rdn.getResponseCode(null, ctx), + is("404")); + } + + @Test + public void testOutputIdentifier() { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" + + "output.identifier", "89"); + ctx.setAttribute("ietf-subscribed-notifications:output.identifier", + "89"); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(); + assertThat(rdn.getOutputIdentifier("restapi-result", ctx), + is("89")); + } +} -- cgit 1.2.3-korg From 224d394cda8a832f8d8c9db03795343ac82f7058 Mon Sep 17 00:00:00 2001 From: Vidyashree Rama Date: Fri, 17 Aug 2018 17:15:20 +0530 Subject: Unit test and decode implementation properties node serializer decode implementation and unit test cases Issue-ID: CCSDK-378 Change-Id: I586fd534f441eb361e8d75af18cc778f7ad4cc67 Signed-off-by: Vidyashree Rama --- restconf-client/provider/pom.xml | 18 + .../DefaultPropertiesNodeListener.java | 95 ++ .../yangserializers/pnserializer/InnerNode.java | 7 +- .../pnserializer/LeafListHolderNode.java | 1 + .../MdsalPropertiesNodeSerializer.java | 7 +- .../yangserializers/pnserializer/RootNode.java | 4 +- .../pnserializer/PropertiesSerializerTest.java | 1090 ++++++++++++++++++++ .../src/test/resources/yang/test-augment.yang | 85 ++ .../src/test/resources/yang/test-yang.yang | 231 +++++ 9 files changed, 1532 insertions(+), 6 deletions(-) create mode 100644 restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java create mode 100644 restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java create mode 100644 restconf-client/provider/src/test/resources/yang/test-augment.yang create mode 100644 restconf-client/provider/src/test/resources/yang/test-yang.yang (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/pom.xml b/restconf-client/provider/pom.xml index 8995f1dd3..2b5be877a 100755 --- a/restconf-client/provider/pom.xml +++ b/restconf-client/provider/pom.xml @@ -94,5 +94,23 @@ restapi-call-node-provider 0.3.0-SNAPSHOT + + org.hamcrest + hamcrest-library + 1.3 + test + + + org.opendaylight.yangtools + yang-test-util + 2.0.3 + test + + + org.opendaylight.netconf + restconf-nb-rfc8040 + 1.7.4-SNAPSHOT + test + diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java new file mode 100644 index 000000000..a8a9b4935 --- /dev/null +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java @@ -0,0 +1,95 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.yangserializers.pnserializer; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE; + +/** + * Represents implementation of PropertiesNodeListener. + */ +public class DefaultPropertiesNodeListener implements PropertiesNodeListener { + + private Map params = new HashMap<>(); + + @Override + public void start(PropertiesNode node) { + // do nothing + } + + @Override + public void end(PropertiesNode node) { + exitPropertiesNode(node); + } + + @Override + public void enterPropertiesNode(PropertiesNode node) { + /* + * Only if it is leaf node or leaf-list node, + * then create a property entry and add to map + */ + if (node.nodeType() == SINGLE_INSTANCE_LEAF_NODE + || node.nodeType() == MULTI_INSTANCE_LEAF_NODE) { + params.put(node.uri(), ((LeafNode) node).value()); + } + } + + @Override + public void exitPropertiesNode(PropertiesNode node) { + if (!node.augmentations().isEmpty()) { + for (Map.Entry> augmentationTochild + : node.augmentations().asMap().entrySet()) { + Collection childsFromAugmentations = augmentationTochild + .getValue(); + if (!childsFromAugmentations.isEmpty()) { + PropertiesNodeWalker walker = new DefaultPropertiesNodeWalker<>(); + for (PropertiesNode pNode : childsFromAugmentations) { + enterPropertiesNode(pNode); + walker.walk(this, pNode); + exitPropertiesNode(pNode); + } + } + } + } + } + + /** + * Returns properties. + * + * @return properties + */ + public Map params() { + return params; + } + + /** + * Sets properties. + * + * @param params properties + */ + public void params(Map params) { + this.params = params; + } +} diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java index 063c3d050..0711a7cff 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java @@ -104,7 +104,7 @@ public abstract class InnerNode extends PropertiesNode { @Override public PropertiesNode addChild(String name, Namespace namespace, NodeType type, String value, - Namespace valuens, + Namespace valueNs, Object appInfo) throws SvcLogicException { LeafNode node = ((LeafNode) children.get(name)); if (node != null) { @@ -120,6 +120,7 @@ public abstract class InnerNode extends PropertiesNode { String uri = getUri(this, name, namespace); node = new LeafNode(name, namespace, uri, this, appInfo, type, value); + node.valueNs(valueNs); if (augSchema != null && !isNamespaceAsParent(this, node)) { addToAugmentations(augSchema, this, node); @@ -196,12 +197,12 @@ public abstract class InnerNode extends PropertiesNode { children.put(localName, ((T) node)); } - node = node.addChild(index, localName, namespace, type, value, null, appInfo); + node = node.addChild(index, localName, namespace, type, value, valueNs, appInfo); } else if (node instanceof LeafListHolderNode) { LeafNode child = ((LeafNode) ((HolderNode) node).child(index)); node = (child != null ? child : node.addChild(index, localName, namespace, type, - value, null, + value, valueNs, appInfo)); } else { throw new SvcLogicException("Duplicate node exist with same node"); diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/LeafListHolderNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/LeafListHolderNode.java index 94892dcac..985bc1da1 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/LeafListHolderNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/LeafListHolderNode.java @@ -82,6 +82,7 @@ public class LeafListHolderNode extends HolderNode implemen String uri = this.uri() + "[" + index + "]"; node = (node != null) ? node : new LeafNode(name, namespace, uri, this, appInfo, type, value); + node.valueNs(valueNs); children().put(index, node); return node; } 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 405e24127..146e581fa 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 @@ -86,7 +86,10 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer decode(PropertiesNode propertiesNode) { - return null; + PropertiesNodeWalker walker = new DefaultPropertiesNodeWalker<>(); + DefaultPropertiesNodeListener listener = new DefaultPropertiesNodeListener(); + walker.walk(listener, propertiesNode); + return listener.params(); } private RootNode createRootNode(String lastNodeName, String rootUri) { @@ -128,6 +131,8 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer extends PropertiesNode { } else { children.put(localName, ((T) node)); } - node = node.addChild(index, localName, namespace, type, value, null, appInfo); + node = node.addChild(index, localName, namespace, type, value, valueNs, appInfo); } else if (node instanceof LeafListHolderNode) { LeafNode child = ((LeafNode) ((HolderNode) node).child(index)); node = (child != null ? child : node.addChild(index, localName, namespace, type, - value, null, + value, valueNs, appInfo)); } else { throw new SvcLogicException("Duplicate node exist with same node"); diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java new file mode 100644 index 000000000..fddfe2d40 --- /dev/null +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java @@ -0,0 +1,1090 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.yangserializers.pnserializer; + +import org.junit.Before; +import org.junit.Test; + +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; +import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertTrue; + +public final class PropertiesSerializerTest { + private SchemaContext context; + + @Before + public void initialization() throws FileNotFoundException { + context = compileYangFile(); + } + + @Test + public void testBasicConstructs() throws SvcLogicException { + String uri = "test-yang:cont1/cont2"; + Map params = new HashMap<>(); + params.put("test-yang:cont1.cont2.cont3.leaf10", "abc"); + params.put("test-yang:cont1.cont2.list1[0].leaf1", "abc"); + params.put("test-yang:cont1.cont2.list1[0].leaf2", "abc"); + params.put("test-yang:cont1.cont2.list1[0].leaf3", "abc"); + params.put("test-yang:cont1.cont2.list1[0].ll1[0]", "abc"); + params.put("test-yang:cont1.cont2.list1[0].ll1[1]", "abc"); + params.put("test-yang:cont1.cont2.list1[0].ll2[0]", "abc"); + params.put("test-yang:cont1.cont2.list1[0].ll2[1]", "abc"); + params.put("test-yang:cont1.cont2.list1[0].cont4.leaf11", "abc"); + params.put("test-yang:cont1.cont2.list1[0].list4[0].leaf8", "abc"); + params.put("test-yang:cont1.cont2.list1[0].list4[1].leaf8", "abc"); + params.put("test-yang:cont1.cont2.list1[0].list5[0].leaf9", "abc"); + params.put("test-yang:cont1.cont2.list1[0].list5[1].leaf9", "abc"); + params.put("test-yang:cont1.cont2.list1[1].leaf1", "abc"); + params.put("test-yang:cont1.cont2.list1[1].leaf2", "abc"); + params.put("test-yang:cont1.cont2.list1[1].leaf3", "abc"); + params.put("test-yang:cont1.cont2.list1[1].ll1[0]", "abc"); + params.put("test-yang:cont1.cont2.list1[1].ll1[1]", "abc"); + params.put("test-yang:cont1.cont2.list1[1].ll2[0]", "abc"); + params.put("test-yang:cont1.cont2.list1[1].ll2[1]", "abc"); + params.put("test-yang:cont1.cont2.list1[1].cont4.leaf11", "abc"); + params.put("test-yang:cont1.cont2.list1[1].list4[0].leaf8", "abc"); + params.put("test-yang:cont1.cont2.list1[1].list4[1].leaf8", "abc"); + params.put("test-yang:cont1.cont2.list1[1].list5[0].leaf9", "abc"); + params.put("test-yang:cont1.cont2.list1[1].list5[1].leaf9", "abc"); + params.put("test-yang:cont1.cont2.list2[0].leaf4", "abc"); + params.put("test-yang:cont1.cont2.list2[1].leaf4", "abc"); + params.put("test-yang:cont1.cont2.leaf5", "abc"); + params.put("test-yang:cont1.cont2.leaf6", "abc"); + params.put("test-yang:cont1.cont2.ll3[0]", "abc"); + params.put("test-yang:cont1.cont2.ll3[1]", "abc"); + params.put("test-yang:cont1.cont2.ll4[0]", "abc"); + params.put("test-yang:cont1.cont2.ll4[1]", "abc"); + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("cont3"), is(true)); + SingleInstanceNode cont3 = ((SingleInstanceNode) childNodes.get("cont3")); + assertThat(cont3.uri(), is("test-yang:cont1.cont2.cont3")); + assertThat(cont3.children().containsKey("leaf10"), is(true)); + + assertThat(childNodes.containsKey("list1"), is(true)); + HolderNode list1Holder = ((ListHolderNode) childNodes.get("list1")); + assertThat(list1Holder.uri(), is("test-yang:cont1.cont2.list1")); + MultiInstanceNode list10 = ((MultiInstanceNode) list1Holder.child("0")); + assertThat(list10.uri(), is("test-yang:cont1.cont2.list1[0]")); + Map list10Child = list10.children(); + assertThat(list10Child.containsKey("leaf1"), is(true)); + LeafNode l = ((LeafNode) list10Child.get("leaf1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].leaf1")); + assertThat(list10Child.containsKey("leaf2"), is(true)); + l = ((LeafNode) list10Child.get("leaf2")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].leaf2")); + assertThat(list10Child.containsKey("leaf2"), is(true)); + l = ((LeafNode) list10Child.get("leaf3")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].leaf3")); + + LeafListHolderNode ll1Holder = ((LeafListHolderNode) list10Child.get("ll1")); + assertThat(ll1Holder.uri(), is("test-yang:cont1.cont2.list1[0].ll1")); + assertThat(ll1Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll1Holder.child("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].ll1[0]")); + assertThat(ll1Holder.children().containsKey("1"), is(true)); + l = ((LeafNode) ll1Holder.child("1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].ll1[1]")); + + LeafListHolderNode ll2Holder = ((LeafListHolderNode) list10Child.get("ll2")); + assertThat(ll2Holder.uri(), is("test-yang:cont1.cont2.list1[0].ll2")); + assertThat(ll2Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll2Holder.child("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].ll2[0]")); + assertThat(ll2Holder.children().containsKey("1"), is(true)); + l = ((LeafNode) ll2Holder.child("1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].ll2[1]")); + + SingleInstanceNode cont4 = ((SingleInstanceNode) list10Child.get("cont4")); + assertThat(cont4.uri(), is("test-yang:cont1.cont2.list1[0].cont4")); + assertThat(cont4.children().containsKey("leaf11"), is(true)); + l = ((LeafNode) cont4.children().get("leaf11")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].cont4.leaf11")); + + HolderNode list4Holder = ((HolderNode) list10Child.get("list4")); + assertThat(list4Holder.uri(), is("test-yang:cont1.cont2.list1[0].list4")); + Map c = list4Holder.children(); + MultiInstanceNode list40 = ((MultiInstanceNode) c.get("0")); + assertThat(list40.uri(), is("test-yang:cont1.cont2.list1[0].list4[0]")); + assertThat(list40.children().containsKey("leaf8"), is(true)); + l = ((LeafNode) list40.children().get("leaf8")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].list4[0].leaf8")); + MultiInstanceNode list41 = ((MultiInstanceNode) c.get("1")); + assertThat(list41.uri(), is("test-yang:cont1.cont2.list1[0].list4[1]")); + assertThat(list41.children().containsKey("leaf8"), is(true)); + l = ((LeafNode) list41.children().get("leaf8")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].list4[1].leaf8")); + + HolderNode list5Holder = ((HolderNode) list10Child.get("list5")); + assertThat(list5Holder.uri(), is("test-yang:cont1.cont2.list1[0].list5")); + c = list5Holder.children(); + MultiInstanceNode list50 = ((MultiInstanceNode) c.get("0")); + assertThat(list50.uri(), is("test-yang:cont1.cont2.list1[0].list5[0]")); + assertThat(list50.children().containsKey("leaf9"), is(true)); + l = ((LeafNode) list50.children().get("leaf9")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].list5[0].leaf9")); + MultiInstanceNode list51 = ((MultiInstanceNode) c.get("1")); + assertThat(list51.uri(), is("test-yang:cont1.cont2.list1[0].list5[1]")); + assertThat(list51.children().containsKey("leaf9"), is(true)); + l = ((LeafNode) list51.children().get("leaf9")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[0].list5[1].leaf9")); + + MultiInstanceNode list11 = ((MultiInstanceNode) list1Holder.child("1")); + assertThat(list11.uri(), is("test-yang:cont1.cont2.list1[1]")); + Map list11Child = list11.children(); + assertThat(list11Child.containsKey("leaf1"), is(true)); + l = ((LeafNode) list11Child.get("leaf1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].leaf1")); + assertThat(list11Child.containsKey("leaf2"), is(true)); + l = ((LeafNode) list11Child.get("leaf2")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].leaf2")); + assertThat(list11Child.containsKey("leaf3"), is(true)); + l = ((LeafNode) list11Child.get("leaf3")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].leaf3")); + + ll1Holder = ((LeafListHolderNode) list11Child.get("ll1")); + assertThat(ll1Holder.uri(), is("test-yang:cont1.cont2.list1[1].ll1")); + assertThat(ll1Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll1Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].ll1[0]")); + assertThat(ll1Holder.children().containsKey("1"), is(true)); + l = ((LeafNode) ll1Holder.children().get("1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].ll1[1]")); + + ll2Holder = ((LeafListHolderNode) list11Child.get("ll2")); + assertThat(ll2Holder.uri(), is("test-yang:cont1.cont2.list1[1].ll2")); + assertThat(ll2Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll2Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].ll2[0]")); + assertThat(ll2Holder.children().containsKey("1"), is(true)); + l = ((LeafNode) ll2Holder.children().get("1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].ll2[1]")); + + cont4 = ((SingleInstanceNode) list11Child.get("cont4")); + assertThat(cont4.uri(), is("test-yang:cont1.cont2.list1[1].cont4")); + assertThat(cont4.children().containsKey("leaf11"), is(true)); + l = ((LeafNode) cont4.children().get("leaf11")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].cont4.leaf11")); + + list4Holder = ((HolderNode) list11Child.get("list4")); + assertThat(list4Holder.uri(), is("test-yang:cont1.cont2.list1[1].list4")); + c = list4Holder.children(); + list40 = ((MultiInstanceNode) c.get("0")); + assertThat(list40.uri(), is("test-yang:cont1.cont2.list1[1].list4[0]")); + assertThat(list40.children().containsKey("leaf8"), is(true)); + l = ((LeafNode) list40.children().get("leaf8")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].list4[0].leaf8")); + list41 = ((MultiInstanceNode) c.get("1")); + assertThat(list41.uri(), is("test-yang:cont1.cont2.list1[1].list4[1]")); + assertThat(list41.children().containsKey("leaf8"), is(true)); + l = ((LeafNode) list41.children().get("leaf8")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].list4[1].leaf8")); + + list5Holder = ((HolderNode) list11Child.get("list5")); + assertThat(list5Holder.uri(), is("test-yang:cont1.cont2.list1[1].list5")); + c = list5Holder.children(); + list50 = ((MultiInstanceNode) c.get("0")); + assertThat(list50.uri(), is("test-yang:cont1.cont2.list1[1].list5[0]")); + assertThat(list50.children().containsKey("leaf9"), is(true)); + l = ((LeafNode) list50.children().get("leaf9")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].list5[0].leaf9")); + list51 = ((MultiInstanceNode) c.get("1")); + assertThat(list51.uri(), is("test-yang:cont1.cont2.list1[1].list5[1]")); + assertThat(list51.children().containsKey("leaf9"), is(true)); + l = ((LeafNode) list51.children().get("leaf9")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list1[1].list5[1].leaf9")); + + assertThat(childNodes.containsKey("list2"), is(true)); + HolderNode list2Holder = ((HolderNode) childNodes.get("list2")); + assertThat(list2Holder.uri(), is("test-yang:cont1.cont2.list2")); + InnerNode list20 = ((InnerNode) list2Holder.children().get("0")); + assertThat(list20.uri(), is("test-yang:cont1.cont2.list2[0]")); + assertThat(list20.children().containsKey("leaf4"), is(true)); + l = ((LeafNode) list20.children().get("leaf4")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list2[0].leaf4")); + InnerNode list21 = ((InnerNode) list2Holder.children().get("1")); + assertThat(list21.uri(), is("test-yang:cont1.cont2.list2[1]")); + assertThat(list21.children().containsKey("leaf4"), is(true)); + l = ((LeafNode) list21.children().get("leaf4")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list2[1].leaf4")); + + assertThat(childNodes.containsKey("leaf5"), is(true)); + l = ((LeafNode) childNodes.get("leaf5")); + assertThat(l.uri(), is("test-yang:cont1.cont2.leaf5")); + assertThat(childNodes.containsKey("leaf6"), is(true)); + l = ((LeafNode) childNodes.get("leaf6")); + assertThat(l.uri(), is("test-yang:cont1.cont2.leaf6")); + + HolderNode ll3Holder = ((HolderNode) childNodes.get("ll3")); + assertThat(ll3Holder.uri(), is("test-yang:cont1.cont2.ll3")); + assertThat(((LeafNode) ll3Holder.children().get("0")).name(), is("ll3")); + l = ((LeafNode) ll3Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.ll3[0]")); + assertThat(((LeafNode) ll3Holder.children().get("1")).name(), is("ll3")); + l = ((LeafNode) ll3Holder.children().get("1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.ll3[1]")); + + HolderNode ll4Holder = ((HolderNode) childNodes.get("ll4")); + assertThat(ll4Holder.uri(), is("test-yang:cont1.cont2.ll4")); + assertThat(((LeafNode) ll4Holder.children().get("0")).name(), is("ll4")); + l = ((LeafNode) ll4Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.ll4[0]")); + assertThat(((LeafNode) ll4Holder.children().get("1")).name(), is("ll4")); + l = ((LeafNode) ll4Holder.children().get("1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.ll4[1]")); + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testAugment() throws SvcLogicException { + String uri = "test-yang:cont1/cont2"; + Map params = new HashMap<>(); + params.put("test-yang:cont1.cont2.cont4.leaf10", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:cont5.leaf13", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:list7[0].leaf14", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:list7[1].leaf14", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:leaf15", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:ll6[0]", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:ll6[1]", "abc"); + params.put("test-yang:cont1.cont2.list6[0].leaf11", "abc"); + params.put("test-yang:cont1.cont2.list6[1].leaf11", "abc"); + params.put("test-yang:cont1.cont2.leaf12", "abc"); + params.put("test-yang:cont1.cont2.ll5[0]", "abc"); + params.put("test-yang:cont1.cont2.ll5[1]", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("cont4"), is(true)); + SingleInstanceNode cont4 = ((SingleInstanceNode) childNodes.get("cont4")); + for (Map.Entry> augToChild + : cont4.augmentations().asMap().entrySet()) { + Collection child = augToChild.getValue(); + if (!child.isEmpty()) { + List expectedNodes = new LinkedList<>(); + expectedNodes.add("test-yang:cont1.cont2.cont4.test-augment:cont5"); + expectedNodes.add("test-yang:cont1.cont2.cont4.test-augment:list7"); + expectedNodes.add("test-yang:cont1.cont2.cont4.test-augment:leaf15"); + expectedNodes.add("test-yang:cont1.cont2.cont4.test-augment:ll6"); + assertThat(expectedNodes.size(), is(child.size())); + for (PropertiesNode pNode : child) { + assertThat(expectedNodes.contains(pNode.uri()), is(true)); + if (pNode.uri().equals("test-yang:cont1.cont2.cont4.test-augment:cont5")) { + assertThat(((SingleInstanceNode) pNode).children().containsKey("leaf13"), is(true)); + LeafNode l = ((LeafNode) ((SingleInstanceNode) pNode).children().get("leaf13")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont5.leaf13")); + } else if (pNode.uri().equals("test-yang:cont1.cont2.cont4.test-augment:list7")) { + ListHolderNode list7Holder = ((ListHolderNode) pNode); + MultiInstanceNode list7 = ((MultiInstanceNode) list7Holder.child("0")); + assertThat(list7.uri(), is("test-yang:cont1.cont2.cont4.test-augment:list7[0]")); + Map list7Child = list7.children(); + assertThat(list7Child.containsKey("leaf14"), is(true)); + LeafNode l = ((LeafNode) list7Child.get("leaf14")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.test-augment:list7[0].leaf14")); + list7 = ((MultiInstanceNode) list7Holder.child("1")); + assertThat(list7.uri(), is("test-yang:cont1.cont2.cont4.test-augment:list7[1]")); + list7Child = list7.children(); + assertThat(list7Child.containsKey("leaf14"), is(true)); + l = ((LeafNode) list7Child.get("leaf14")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.test-augment:list7[1].leaf14")); + } else if (pNode.uri().equals("test-yang:cont1.cont2.cont4.test-augment:leaf15")) { + LeafNode leaf15 = ((LeafNode) pNode); + assertThat(leaf15.name(), is("leaf15")); + assertThat(leaf15.uri(), is("test-yang:cont1.cont2.cont4.test-augment:leaf15")); + } else if (pNode.uri().equals("test-yang:cont1.cont2.cont4.test-augment:ll6")) { + LeafListHolderNode ll6Holder = ((LeafListHolderNode) pNode); + assertThat(ll6Holder.children().containsKey("0"), is(true)); + LeafNode l = ((LeafNode) ll6Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.test-augment:ll6[0]")); + assertThat(ll6Holder.children().containsKey("1"), is(true)); + l = ((LeafNode) ll6Holder.children().get("1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.test-augment:ll6[1]")); + } + } + } + } + assertThat(cont4.uri(), is("test-yang:cont1.cont2.cont4")); + assertThat(cont4.children().containsKey("leaf10"), is(true)); + LeafNode l = ((LeafNode) cont4.children().get("leaf10")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.leaf10")); + + assertThat(childNodes.containsKey("list6"), is(true)); + HolderNode list6Holder = ((ListHolderNode) childNodes.get("list6")); + assertThat(list6Holder.uri(), is("test-yang:cont1.cont2.list6")); + MultiInstanceNode list6 = ((MultiInstanceNode) list6Holder.child("0")); + assertThat(list6.uri(), is("test-yang:cont1.cont2.list6[0]")); + Map list6Child = list6.children(); + assertThat(list6Child.containsKey("leaf11"), is(true)); + l = ((LeafNode) list6Child.get("leaf11")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list6[0].leaf11")); + list6 = ((MultiInstanceNode) list6Holder.child("1")); + assertThat(list6.uri(), is("test-yang:cont1.cont2.list6[1]")); + list6Child = list6.children(); + assertThat(list6Child.containsKey("leaf11"), is(true)); + l = ((LeafNode) list6Child.get("leaf11")); + assertThat(l.uri(), is("test-yang:cont1.cont2.list6[1].leaf11")); + + assertThat(childNodes.containsKey("leaf12"), is(true)); + LeafNode leaf12 = ((LeafNode) childNodes.get("leaf12")); + assertThat(leaf12.name(), is("leaf12")); + assertThat(leaf12.uri(), is("test-yang:cont1.cont2.leaf12")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) childNodes.get("ll5")); + assertThat(ll5Holder.uri(), is("test-yang:cont1.cont2.ll5")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll5Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.ll5[0]")); + assertThat(ll5Holder.children().containsKey("1"), is(true)); + l = ((LeafNode) ll5Holder.children().get("1")); + assertThat(l.uri(), is("test-yang:cont1.cont2.ll5[1]")); + + Map output = ser.decode(node); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testChoiceCase1() throws SvcLogicException { + String uri = "test-yang:cont8"; + Map params = new HashMap<>(); + params.put("test-yang:cont8.cont6.leaf16", "abc"); + params.put("test-yang:cont8.list8[0].leaf18", "abc"); + params.put("test-yang:cont8.list8[1].leaf18", "abc"); + params.put("test-yang:cont8.leaf19", "abc"); + params.put("test-yang:cont8.ll7[0]", "abc"); + params.put("test-yang:cont8.ll7[1]", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("cont6"), is(true)); + SingleInstanceNode cont6 = ((SingleInstanceNode) childNodes.get("cont6")); + assertThat(cont6.uri(), is("test-yang:cont8.cont6")); + assertThat(cont6.children().containsKey("leaf16"), is(true)); + LeafNode l = ((LeafNode) cont6.children().get("leaf16")); + assertThat(l.uri(), is("test-yang:cont8.cont6.leaf16")); + + assertThat(childNodes.containsKey("list8"), is(true)); + HolderNode list6Holder = ((ListHolderNode) childNodes.get("list8")); + assertThat(list6Holder.uri(), is("test-yang:cont8.list8")); + MultiInstanceNode list6 = ((MultiInstanceNode) list6Holder.child("0")); + assertThat(list6.uri(), is("test-yang:cont8.list8[0]")); + Map list6Child = list6.children(); + assertThat(list6Child.containsKey("leaf18"), is(true)); + l = ((LeafNode) list6Child.get("leaf18")); + assertThat(l.uri(), is("test-yang:cont8.list8[0].leaf18")); + list6 = ((MultiInstanceNode) list6Holder.child("1")); + list6Child = list6.children(); + assertThat(list6Child.containsKey("leaf18"), is(true)); + l = ((LeafNode) list6Child.get("leaf18")); + assertThat(l.uri(), is("test-yang:cont8.list8[1].leaf18")); + + assertThat(childNodes.containsKey("leaf19"), is(true)); + LeafNode leaf12 = ((LeafNode) childNodes.get("leaf19")); + assertThat(leaf12.name(), is("leaf19")); + assertThat(leaf12.uri(), is("test-yang:cont8.leaf19")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) childNodes.get("ll7")); + assertThat(ll5Holder.uri(), is("test-yang:cont8.ll7")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll5Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont8.ll7[0]")); + assertThat(ll5Holder.children().containsKey("1"), is(true)); + l = ((LeafNode) ll5Holder.children().get("1")); + assertThat(l.uri(), is("test-yang:cont8.ll7[1]")); + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testChoiceCase2() throws SvcLogicException { + String uri = "test-yang:cont9"; + Map params = new HashMap<>(); + params.put("test-yang:cont9.leaf20", "abc"); + params.put("test-yang:cont9.ll8[0]", "abc"); + params.put("test-yang:cont9.cont11.leaf25", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("cont11"), is(true)); + SingleInstanceNode cont4 = ((SingleInstanceNode) childNodes.get("cont11")); + assertThat(cont4.uri(), is("test-yang:cont9.cont11")); + assertThat(cont4.children().containsKey("leaf25"), is(true)); + LeafNode l = ((LeafNode) cont4.children().get("leaf25")); + assertThat(l.uri(), is("test-yang:cont9.cont11.leaf25")); + + assertThat(childNodes.containsKey("leaf20"), is(true)); + l = ((LeafNode) childNodes.get("leaf20")); + assertThat(l.uri(), is("test-yang:cont9.leaf20")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) childNodes.get("ll8")); + assertThat(ll5Holder.uri(), is("test-yang:cont9.ll8")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll5Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont9.ll8[0]")); + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testChoiceCase3() throws SvcLogicException { + String uri = "test-yang:cont8/cont6"; + Map params = new HashMap<>(); + params.put("test-yang:cont8.cont6.test-augment:leaf21", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + for (Map.Entry> augToChild + : node.augmentations().asMap().entrySet()) { + Collection child = augToChild.getValue(); + if (!child.isEmpty()) { + List expectedNodes = new LinkedList<>(); + expectedNodes.add("test-yang:cont8.cont6.test-augment:leaf21"); + assertThat(expectedNodes.size(), is(child.size())); + for (PropertiesNode pNode : child) { + assertThat(expectedNodes.contains(pNode.uri()), is(true)); + } + } + } + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("leaf21"), is(true)); + LeafNode leaf12 = ((LeafNode) childNodes.get("leaf21")); + assertThat(leaf12.name(), is("leaf21")); + assertThat(leaf12.uri(), is("test-yang:cont8.cont6.test-augment:leaf21")); + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testGrouping() throws SvcLogicException { + String uri = "test-yang:cont13"; + Map params = new HashMap<>(); + params.put("test-yang:cont13.cont12.leaf26", "abc"); + params.put("test-yang:cont13.list9[0].leaf27", "abc"); + params.put("test-yang:cont13.leaf28", "abc"); + params.put("test-yang:cont13.ll9[0]", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("cont12"), is(true)); + SingleInstanceNode cont4 = ((SingleInstanceNode) childNodes.get("cont12")); + assertThat(cont4.uri(), is("test-yang:cont13.cont12")); + assertThat(cont4.children().containsKey("leaf26"), is(true)); + LeafNode l = ((LeafNode) cont4.children().get("leaf26")); + assertThat(l.uri(), is("test-yang:cont13.cont12.leaf26")); + + assertThat(childNodes.containsKey("list9"), is(true)); + HolderNode list6Holder = ((ListHolderNode) childNodes.get("list9")); + assertThat(list6Holder.uri(), is("test-yang:cont13.list9")); + MultiInstanceNode list6 = ((MultiInstanceNode) list6Holder.child("0")); + assertThat(list6.uri(), is("test-yang:cont13.list9[0]")); + Map list6Child = list6.children(); + assertThat(list6Child.containsKey("leaf27"), is(true)); + l = ((LeafNode) list6Child.get("leaf27")); + assertThat(l.uri(), is("test-yang:cont13.list9[0].leaf27")); + + assertThat(childNodes.containsKey("leaf28"), is(true)); + LeafNode leaf12 = ((LeafNode) childNodes.get("leaf28")); + assertThat(leaf12.name(), is("leaf28")); + assertThat(leaf12.uri(), is("test-yang:cont13.leaf28")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) childNodes.get("ll9")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testGrouping2() throws SvcLogicException { + String uri = "test-yang:cont9/cont11"; + Map params = new HashMap<>(); + params.put("test-yang:cont9.cont11.leaf25", "abc"); + params.put("test-yang:cont9.cont11.cont13.cont12.leaf26", "abc"); + params.put("test-yang:cont9.cont11.cont13.list9[0].leaf27", "abc"); + params.put("test-yang:cont9.cont11.cont13.leaf28", "abc"); + params.put("test-yang:cont9.cont11.cont13.ll9[0]", "abc"); + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("cont13"), is(true)); + SingleInstanceNode cont13 = ((SingleInstanceNode) childNodes.get("cont13")); + assertThat(cont13.uri(), is("test-yang:cont9.cont11.cont13")); + SingleInstanceNode cont12 = ((SingleInstanceNode) cont13.children().get("cont12")); + assertThat(cont12.children().containsKey("leaf26"), is(true)); + assertThat(cont12.uri(), is("test-yang:cont9.cont11.cont13.cont12")); + assertThat(cont12.children().containsKey("leaf26"), is(true)); + LeafNode l = ((LeafNode) cont12.children().get("leaf26")); + assertThat(l.uri(), is("test-yang:cont9.cont11.cont13.cont12.leaf26")); + + assertThat(cont13.children().containsKey("list9"), is(true)); + HolderNode list6Holder = ((ListHolderNode) cont13.children().get("list9")); + assertThat(list6Holder.uri(), is("test-yang:cont9.cont11.cont13.list9")); + MultiInstanceNode list6 = ((MultiInstanceNode) list6Holder.child("0")); + assertThat(list6.uri(), is("test-yang:cont9.cont11.cont13.list9[0]")); + Map list6Child = list6.children(); + assertThat(list6Child.containsKey("leaf27"), is(true)); + l = ((LeafNode) list6Child.get("leaf27")); + assertThat(l.uri(), is("test-yang:cont9.cont11.cont13.list9[0].leaf27")); + + assertThat(cont13.children().containsKey("leaf28"), is(true)); + LeafNode leaf12 = ((LeafNode) cont13.children().get("leaf28")); + assertThat(leaf12.name(), is("leaf28")); + assertThat(leaf12.uri(), is("test-yang:cont9.cont11.cont13.leaf28")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) cont13.children().get("ll9")); + assertThat(ll5Holder.uri(), is("test-yang:cont9.cont11.cont13.ll9")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll5Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont9.cont11.cont13.ll9[0]")); + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testGrouping3() throws SvcLogicException { + String uri = "test-augment:cont13"; + Map params = new HashMap<>(); + params.put("test-augment:cont13.cont12.leaf26", "abc"); + params.put("test-augment:cont13.list9[0].leaf27", "abc"); + params.put("test-augment:cont13.leaf28", "abc"); + params.put("test-augment:cont13.ll9[0]", "abc"); + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("cont12"), is(true)); + SingleInstanceNode cont12 = ((SingleInstanceNode) childNodes.get("cont12")); + assertThat(cont12.uri(), is("test-augment:cont13.cont12")); + assertThat(cont12.children().containsKey("leaf26"), is(true)); + LeafNode l = ((LeafNode) cont12.children().get("leaf26")); + assertThat(l.uri(), is("test-augment:cont13.cont12.leaf26")); + + assertThat(childNodes.containsKey("list9"), is(true)); + HolderNode list6Holder = ((ListHolderNode) childNodes.get("list9")); + assertThat(list6Holder.uri(), is("test-augment:cont13.list9")); + MultiInstanceNode list6 = ((MultiInstanceNode) list6Holder.child("0")); + assertThat(list6.uri(), is("test-augment:cont13.list9[0]")); + Map list6Child = list6.children(); + assertThat(list6Child.containsKey("leaf27"), is(true)); + l = ((LeafNode) list6Child.get("leaf27")); + assertThat(l.uri(), is("test-augment:cont13.list9[0].leaf27")); + + assertThat(childNodes.containsKey("leaf28"), is(true)); + LeafNode leaf12 = ((LeafNode) childNodes.get("leaf28")); + assertThat(leaf12.name(), is("leaf28")); + assertThat(leaf12.uri(), is("test-augment:cont13.leaf28")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) childNodes.get("ll9")); + assertThat(ll5Holder.uri(), is("test-augment:cont13.ll9")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll5Holder.children().get("0")); + assertThat(l.uri(), is("test-augment:cont13.ll9[0]")); + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testGrouping4() throws SvcLogicException { + String uri = "test-yang:cont1/cont2/cont4"; + Map params = new HashMap<>(); + params.put("test-yang:cont1.cont2.cont4.test-augment:cont13.cont12.leaf26", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:cont13.list9[0].leaf27", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:cont13.leaf28", "abc"); + params.put("test-yang:cont1.cont2.cont4.test-augment:cont13.ll9[0]", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + for (Map.Entry> augToChild + : node.augmentations().asMap().entrySet()) { + Collection child = augToChild.getValue(); + if (!child.isEmpty()) { + List expectedNodes = new LinkedList<>(); + expectedNodes.add("test-yang:cont1.cont2.cont4.test-augment:cont13"); + assertThat(expectedNodes.size(), is(child.size())); + for (PropertiesNode pNode : child) { + assertThat(expectedNodes.contains(pNode.uri()), is(true)); + SingleInstanceNode cont13 = ((SingleInstanceNode) pNode); + assertThat(cont13.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont13")); + SingleInstanceNode cont12 = ((SingleInstanceNode) cont13.children().get("cont12")); + assertThat(cont12.children().containsKey("leaf26"), is(true)); + LeafNode l = ((LeafNode) cont12.children().get("leaf26")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont13.cont12.leaf26")); + + assertThat(cont13.children().containsKey("list9"), is(true)); + HolderNode list6Holder = ((ListHolderNode) cont13.children().get("list9")); + assertThat(list6Holder.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont13.list9")); + MultiInstanceNode list6 = ((MultiInstanceNode) list6Holder.child("0")); + assertThat(list6.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont13.list9[0]")); + Map list6Child = list6.children(); + assertThat(list6Child.containsKey("leaf27"), is(true)); + l = ((LeafNode) list6Child.get("leaf27")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont13.list9[0].leaf27")); + + assertThat(cont13.children().containsKey("leaf28"), is(true)); + LeafNode leaf12 = ((LeafNode) cont13.children().get("leaf28")); + assertThat(leaf12.name(), is("leaf28")); + assertThat(leaf12.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont13.leaf28")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) cont13.children().get("ll9")); + assertThat(ll5Holder.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont13.ll9")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll5Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:cont1.cont2.cont4.test-augment:cont13.ll9[0]")); + } + } + } + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testRpcInput() throws SvcLogicException { + String uri = "test-yang:create-sfc"; + Map params = new HashMap<>(); + params.put("test-yang:create-sfc.input.cont14.leaf28", "abc"); + params.put("test-yang:create-sfc.input.list10[0].leaf29", "abc"); + params.put("test-yang:create-sfc.input.leaf30", "abc"); + params.put("test-yang:create-sfc.input.ll10[0]", "abc"); + params.put("test-yang:create-sfc.input.cont15.leaf31", "abc"); + params.put("test-yang:create-sfc.input.cont13.cont12.leaf26", "abc"); + params.put("test-yang:create-sfc.input.cont13.list9[0].leaf27", "abc"); + params.put("test-yang:create-sfc.input.cont13.leaf28", "abc"); + params.put("test-yang:create-sfc.input.cont13.ll9[0]", "abc"); + params.put("test-yang:create-sfc.input.test-augment:leaf36", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + PropertiesNode input = childNodes.get("input"); + assertThat(input.uri(), is("test-yang:create-sfc.input")); + for (Map.Entry> augToChild + : node.augmentations().asMap().entrySet()) { + Collection child = augToChild.getValue(); + if (!child.isEmpty()) { + List expectedNodes = new LinkedList<>(); + expectedNodes.add("test-yang:create-sfc.input.test-augment:leaf36"); + assertThat(expectedNodes.size(), is(child.size())); + for (PropertiesNode pNode : child) { + assertThat(expectedNodes.contains(pNode.uri()), is(true)); + LeafNode leaf37 = ((LeafNode) pNode); + assertThat(leaf37.name(), is("leaf36")); + assertThat(leaf37.uri(), is("test-yang:create-sfc.input.test-augment:leaf36")); + } + } + } + childNodes = ((InnerNode) input).children(); + + assertThat(childNodes.containsKey("cont14"), is(true)); + SingleInstanceNode cont14 = ((SingleInstanceNode) childNodes.get("cont14")); + assertThat(cont14.uri(), is("test-yang:create-sfc.input.cont14")); + assertThat(cont14.children().containsKey("leaf28"), is(true)); + LeafNode l = ((LeafNode) cont14.children().get("leaf28")); + assertThat(l.uri(), is("test-yang:create-sfc.input.cont14.leaf28")); + + assertThat(childNodes.containsKey("list10"), is(true)); + HolderNode list10Holder = ((ListHolderNode) childNodes.get("list10")); + assertThat(list10Holder.uri(), is("test-yang:create-sfc.input.list10")); + MultiInstanceNode list10 = ((MultiInstanceNode) list10Holder.child("0")); + assertThat(list10.uri(), is("test-yang:create-sfc.input.list10[0]")); + Map list10Child = list10.children(); + assertThat(list10Child.containsKey("leaf29"), is(true)); + l = ((LeafNode) list10Child.get("leaf29")); + assertThat(l.uri(), is("test-yang:create-sfc.input.list10[0].leaf29")); + + assertThat(childNodes.containsKey("leaf30"), is(true)); + LeafNode leaf30 = ((LeafNode) childNodes.get("leaf30")); + assertThat(leaf30.name(), is("leaf30")); + assertThat(leaf30.uri(), is("test-yang:create-sfc.input.leaf30")); + + LeafListHolderNode ll10Holder = ((LeafListHolderNode) childNodes.get("ll10")); + assertThat(ll10Holder.uri(), is("test-yang:create-sfc.input.ll10")); + assertThat(ll10Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll10Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:create-sfc.input.ll10[0]")); + + assertThat(childNodes.containsKey("cont15"), is(true)); + SingleInstanceNode cont15 = ((SingleInstanceNode) childNodes.get("cont15")); + assertThat(cont15.uri(), is("test-yang:create-sfc.input.cont15")); + assertThat(cont15.children().containsKey("leaf31"), is(true)); + l = ((LeafNode) cont15.children().get("leaf31")); + assertThat(l.uri(), is("test-yang:create-sfc.input.cont15.leaf31")); + + assertThat(childNodes.containsKey("cont13"), is(true)); + SingleInstanceNode cont13 = ((SingleInstanceNode) childNodes.get("cont13")); + assertThat(cont13.uri(), is("test-yang:create-sfc.input.cont13")); + SingleInstanceNode cont12 = ((SingleInstanceNode) cont13.children().get("cont12")); + assertThat(cont12.uri(), is("test-yang:create-sfc.input.cont13.cont12")); + assertThat(cont12.children().containsKey("leaf26"), is(true)); + l = ((LeafNode) cont12.children().get("leaf26")); + assertThat(l.uri(), is("test-yang:create-sfc.input.cont13.cont12.leaf26")); + + assertThat(cont13.children().containsKey("list9"), is(true)); + HolderNode list9Holder = ((ListHolderNode) cont13.children().get("list9")); + assertThat(list9Holder.uri(), is("test-yang:create-sfc.input.cont13.list9")); + MultiInstanceNode list9 = ((MultiInstanceNode) list9Holder.child("0")); + assertThat(list9.uri(), is("test-yang:create-sfc.input.cont13.list9[0]")); + Map list6Child = list9.children(); + assertThat(list6Child.containsKey("leaf27"), is(true)); + l = ((LeafNode) list6Child.get("leaf27")); + assertThat(l.uri(), is("test-yang:create-sfc.input.cont13.list9[0].leaf27")); + + assertThat(cont13.children().containsKey("leaf28"), is(true)); + LeafNode leaf12 = ((LeafNode) cont13.children().get("leaf28")); + assertThat(leaf12.name(), is("leaf28")); + assertThat(leaf12.uri(), is("test-yang:create-sfc.input.cont13.leaf28")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) cont13.children().get("ll9")); + assertThat(ll5Holder.uri(), is("test-yang:create-sfc.input.cont13.ll9")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll5Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:create-sfc.input.cont13.ll9[0]")); + + Map output = ser.decode(node); + assertThat(output.size(), is(params.size())); + for (Map.Entry entry : output.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testRpcOutput() throws SvcLogicException { + String uri = "test-yang:create-sfc"; + Map params = new HashMap<>(); + params.put("test-yang:create-sfc.output.cont16.leaf32", "abc"); + params.put("test-yang:create-sfc.output.list11[0].leaf33", "abc"); + params.put("test-yang:create-sfc.output.leaf34", "abc"); + params.put("test-yang:create-sfc.output.ll11[0]", "abc"); + params.put("test-yang:create-sfc.output.cont17.leaf35", "abc"); + params.put("test-yang:create-sfc.output.cont13.cont12.leaf26", "abc"); + params.put("test-yang:create-sfc.output.cont13.list9[0].leaf27", "abc"); + params.put("test-yang:create-sfc.output.cont13.leaf28", "abc"); + params.put("test-yang:create-sfc.output.cont13.ll9[0]", "abc"); + params.put("test-yang:create-sfc.output.test-augment:leaf37", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + PropertiesNode output = childNodes.get("output"); + assertThat(output.uri(), is("test-yang:create-sfc.output")); + for (Map.Entry> augmentationToChild : + node.augmentations().asMap().entrySet()) { + Collection c = augmentationToChild.getValue(); + if(!c.isEmpty()) { + List expectedNodes = new LinkedList<>(); + expectedNodes.add("test-yang:create-sfc.output.test-augment:leaf37"); + assertThat(expectedNodes.size(), is(expectedNodes)); + for (PropertiesNode pNode : c) { + assertThat(expectedNodes.contains(pNode.uri()), is(true)); + LeafNode leaf37 = ((LeafNode) pNode); + assertThat(leaf37.name(), is("leaf37")); + assertThat(leaf37.uri(), is("test-yang:create-sfc.output.test-augment:leaf37")); + } + } + } + childNodes = ((InnerNode) output).children(); + + assertThat(childNodes.containsKey("cont16"), is(true)); + SingleInstanceNode cont16 = ((SingleInstanceNode) childNodes.get("cont16")); + assertThat(cont16.uri(), is("test-yang:create-sfc.output.cont16")); + assertThat(cont16.children().containsKey("leaf32"), is(true)); + LeafNode l = ((LeafNode) cont16.children().get("leaf32")); + assertThat(l.uri(), is("test-yang:create-sfc.output.cont16.leaf32")); + + assertThat(childNodes.containsKey("list11"), is(true)); + HolderNode list11Holder = ((ListHolderNode) childNodes.get("list11")); + assertThat(list11Holder.uri(), is("test-yang:create-sfc.output.list11")); + MultiInstanceNode list11 = ((MultiInstanceNode) list11Holder.child("0")); + assertThat(list11.uri(), is("test-yang:create-sfc.output.list11[0]")); + Map list11Child = list11.children(); + assertThat(list11Child.containsKey("leaf33"), is(true)); + l = ((LeafNode) list11Child.get("leaf33")); + assertThat(l.uri(), is("test-yang:create-sfc.output.list11[0].leaf33")); + + assertThat(childNodes.containsKey("leaf34"), is(true)); + LeafNode leaf34 = ((LeafNode) childNodes.get("leaf34")); + assertThat(leaf34.name(), is("leaf34")); + assertThat(leaf34.uri(), is("test-yang:create-sfc.output.leaf34")); + + LeafListHolderNode ll10Holder = ((LeafListHolderNode) childNodes.get("ll11")); + assertThat(ll10Holder.uri(), is("test-yang:create-sfc.output.ll11")); + assertThat(ll10Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll10Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:create-sfc.output.ll11[0]")); + + assertThat(childNodes.containsKey("cont17"), is(true)); + SingleInstanceNode cont17 = ((SingleInstanceNode) childNodes.get("cont17")); + assertThat(cont17.uri(), is("test-yang:create-sfc.output.cont17")); + assertThat(cont17.children().containsKey("leaf35"), is(true)); + l = ((LeafNode) cont17.children().get("leaf35")); + assertThat(l.uri(), is("test-yang:create-sfc.output.cont17.leaf35")); + + assertThat(childNodes.containsKey("cont13"), is(true)); + SingleInstanceNode cont13 = ((SingleInstanceNode) childNodes.get("cont13")); + assertThat(cont13.uri(), is("test-yang:create-sfc.output.cont13")); + SingleInstanceNode cont12 = ((SingleInstanceNode) cont13.children().get("cont12")); + assertThat(cont12.uri(), is("test-yang:create-sfc.output.cont13.cont12")); + assertThat(cont12.children().containsKey("leaf26"), is(true)); + l = ((LeafNode) cont12.children().get("leaf26")); + assertThat(l.uri(), is("test-yang:create-sfc.output.cont13.cont12.leaf26")); + + assertThat(cont13.children().containsKey("list9"), is(true)); + HolderNode list9Holder = ((ListHolderNode) cont13.children().get("list9")); + assertThat(list9Holder.uri(), is("test-yang:create-sfc.output.cont13.list9")); + MultiInstanceNode list9 = ((MultiInstanceNode) list9Holder.child("0")); + assertThat(list9.uri(), is("test-yang:create-sfc.output.cont13.list9[0]")); + Map list6Child = list9.children(); + assertThat(list6Child.containsKey("leaf27"), is(true)); + l = ((LeafNode) list6Child.get("leaf27")); + assertThat(l.uri(), is("test-yang:create-sfc.output.cont13.list9[0].leaf27")); + + assertThat(cont13.children().containsKey("leaf28"), is(true)); + LeafNode leaf12 = ((LeafNode) cont13.children().get("leaf28")); + assertThat(leaf12.name(), is("leaf28")); + assertThat(leaf12.uri(), is("test-yang:create-sfc.output.cont13.leaf28")); + + LeafListHolderNode ll5Holder = ((LeafListHolderNode) cont13.children().get("ll9")); + assertThat(ll5Holder.uri(), is("test-yang:create-sfc.output.cont13.ll9")); + assertThat(ll5Holder.children().containsKey("0"), is(true)); + l = ((LeafNode) ll5Holder.children().get("0")); + assertThat(l.uri(), is("test-yang:create-sfc.output.cont13.ll9[0]")); + + Map output1 = ser.decode(node); + assertThat(output1.size(), is(params.size())); + for (Map.Entry entry : output1.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testContainerSameName() throws SvcLogicException { + String uri = "test-yang:cont18"; + Map params = new HashMap<>(); + params.put("test-yang:cont18.cont18.list12[0].list12[0].leaf36", "abc"); + params.put("test-yang:cont18.cont18.list12[0].leaf36", "hi"); + params.put("test-yang:cont18.cont18.list12[1].list12[0].leaf36", "xyz"); + params.put("test-yang:cont18.cont18.list12[1].list12[1].leaf36", "hey!"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + + assertThat(childNodes.containsKey("cont18"), is(true)); + node = childNodes.get("cont18"); + assertThat(node.uri(), is("test-yang:cont18.cont18")); + childNodes = ((InnerNode) node).children(); + + assertThat(childNodes.containsKey("list12"), is(true)); + HolderNode holder = ((ListHolderNode) childNodes.get("list12")); + assertThat(holder.uri(), is("test-yang:cont18.cont18.list12")); + MultiInstanceNode node1 = ((MultiInstanceNode) holder.child("0")); + assertThat(node1.uri(), is("test-yang:cont18.cont18.list12[0]")); + Map list12Child = node1.children(); + + assertThat(list12Child.containsKey("leaf36"), is(true)); + LeafNode leaf = ((LeafNode) list12Child.get("leaf36")); + assertThat(leaf.value(), is("hi")); + assertThat(leaf.uri(), is("test-yang:cont18.cont18.list12[0].leaf36")); + + assertThat(list12Child.containsKey("list12"), is(true)); + HolderNode holder1 = ((ListHolderNode) list12Child.get("list12")); + assertThat(holder1.uri(), is("test-yang:cont18.cont18.list12[0].list12")); + node1 = ((MultiInstanceNode) holder1.child("0")); + assertThat(node1.uri(), is("test-yang:cont18.cont18.list12[0].list12[0]")); + list12Child = node1.children(); + assertThat(list12Child.containsKey("leaf36"), is(true)); + leaf = ((LeafNode) list12Child.get("leaf36")); + assertThat(leaf.value(), is("abc")); + assertThat(leaf.uri(), is("test-yang:cont18.cont18.list12[0].list12[0].leaf36")); + + node1 = ((MultiInstanceNode) holder.child("1")); + assertThat(node1.uri(), is("test-yang:cont18.cont18.list12[1]")); + list12Child = node1.children(); + assertThat(list12Child.containsKey("list12"), is(true)); + holder = ((ListHolderNode) list12Child.get("list12")); + assertThat(holder.uri(), is("test-yang:cont18.cont18.list12[1].list12")); + node1 = ((MultiInstanceNode) holder.child("0")); + assertThat(node1.uri(), is("test-yang:cont18.cont18.list12[1].list12[0]")); + assertThat(node1.children().containsKey("leaf36"), is(true)); + leaf = ((LeafNode) node1.children().get("leaf36")); + assertThat(leaf.value(), is("xyz")); + assertThat(leaf.uri(), is("test-yang:cont18.cont18.list12[1].list12[0].leaf36")); + + node1 = ((MultiInstanceNode) holder.child("1")); + assertThat(node1.uri(), is("test-yang:cont18.cont18.list12[1].list12[1]")); + assertThat(node1.children().containsKey("leaf36"), is(true)); + leaf = ((LeafNode) node1.children().get("leaf36")); + assertThat(leaf.value(), is("hey!")); + assertThat(leaf.uri(), is("test-yang:cont18.cont18.list12[1].list12[1].leaf36")); + + Map output1 = ser.decode(node); + assertThat(output1.size(), is(params.size())); + for (Map.Entry entry : output1.entrySet()) { + assertTrue(params.containsKey(entry.getKey())); + } + } + + @Test + public void testPropertiesWithoutSchema() throws SvcLogicException { + String uri = "test-yang:cont18"; + Map params = new HashMap<>(); + params.put("test-yang:cont18.leaf40", "abc"); + params.put("leaf41", "hi"); + params.put("test-yang:cont18.leaf41", "abc"); + + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + + Map childNodes = ((RootNode) node).children(); + assertThat(childNodes.containsKey("leaf40"), is(true)); + node = childNodes.get("leaf40"); + assertThat(node.uri(), is("test-yang:cont18.leaf40")); + } + + public static SchemaContext compileYangFile() throws FileNotFoundException { + String path = PropertiesSerializerTest.class.getResource("/yang").getPath(); + File dir = new File(path); + String[] fileList = dir.list(); + List yangFiles = new ArrayList(); + if (fileList == null) { + throw new FileNotFoundException("/yang"); + } + for (int i = 0; i < fileList.length; i++) { + final String fileName = fileList[i]; + if (new File(dir, fileName).isDirectory() == false) { + yangFiles.add(new File(dir, fileName)); + } + } + return YangParserTestUtils.parseYangFiles(yangFiles); + } +} \ No newline at end of file diff --git a/restconf-client/provider/src/test/resources/yang/test-augment.yang b/restconf-client/provider/src/test/resources/yang/test-augment.yang new file mode 100644 index 000000000..bac19171b --- /dev/null +++ b/restconf-client/provider/src/test/resources/yang/test-augment.yang @@ -0,0 +1,85 @@ +module test-augment { + yang-version 1; + namespace "urn:opendaylight:params:xml:ns:yang:augment"; + prefix "hello"; + + import test-yang { + prefix t; + } + + revision "2015-01-05" { + description "Initial revision of hello model"; + } + + augment "/t:cont1/t:cont2/t:cont4" { + container cont5 { + leaf leaf13 { + type string; + } + } + list list7 { + leaf leaf14 { + type string; + } + } + leaf leaf15 { + type string; + } + leaf-list ll6 { + type string; + } + uses "t:g1"; + } + + uses "t:g1"; + augment "/t:ch1/t:c1/t:cont8/t:cont6" { + choice ch2 { + case c3 { + leaf leaf21 { + type string; + } + } + case c4 { + leaf leaf22 { + type string; + } + } + } + } + + augment "/t:ch1" { + case c5 { + container cont10 { + leaf leaf23 { + type string; + } + } + } + } + + augment "/t:ch1/t:c1" { + container cont7 { + leaf leaf24 { + type string; + } + } + } + + augment "/t:cont13/t:cont12" { + leaf leaf29 { + type string; + } + } + + augment "/t:create-sfc/t:input" { + leaf leaf36 { + type string; + } + } + + augment "/t:create-sfc/t:output" { + leaf leaf37 { + type string; + } + } +} \ No newline at end of file diff --git a/restconf-client/provider/src/test/resources/yang/test-yang.yang b/restconf-client/provider/src/test/resources/yang/test-yang.yang new file mode 100644 index 000000000..e99d87bf8 --- /dev/null +++ b/restconf-client/provider/src/test/resources/yang/test-yang.yang @@ -0,0 +1,231 @@ +module test-yang { + yang-version 1; + namespace "urn:opendaylight:params:xml:ns:yang:test"; + prefix "hello"; + + revision "2015-01-05" { + description "Initial revision of hello model"; + } + + container cont1 { + container cont2 { + container cont3 { + leaf leaf10 { + type string; + } + } + list list1 { + key "leaf1 leaf2"; + leaf leaf1 { + type string; + } + leaf leaf2 { + type string; + } + leaf leaf3 { + type string; + } + leaf-list ll1 { + type string; + } + leaf-list ll2 { + type string; + } + container cont4 { + leaf leaf11 { + type string; + } + } + list list4 { + leaf leaf8 { + type string; + } + } + list list5 { + leaf leaf9 { + type string; + } + } + } + list list2 { + leaf leaf4 { + type string; + } + } + leaf leaf5 { + type string; + } + leaf leaf6 { + type string; + } + leaf-list ll3 { + type string; + } + leaf-list ll4 { + type string; + } + } + } + + augment "/cont1/cont2" { + container cont4 { + leaf leaf10 { + type string; + } + } + list list6 { + leaf leaf11 { + type string; + } + } + leaf leaf12 { + type string; + } + leaf-list ll5 { + type string; + } + } + + choice ch1 { + case c1 { + container cont8 { + container cont6 { + leaf leaf16 { + type string; + } + } + list list8 { + leaf leaf18 { + type string; + } + } + leaf leaf19 { + type string; + } + leaf-list ll7 { + type string; + } + } + } + case c2 { + container cont9 { + leaf leaf20 { + type string; + } + leaf-list ll8 { + type string; + } + container cont11 { + choice ch3 { + case c1 { + leaf leaf25 { + type string; + } + uses g1; + } + } + } + } + } + } + + grouping g1 { + container cont13 { + container cont12 { + leaf leaf26 { + type string; + } + } + list list9 { + leaf leaf27 { + type string; + } + } + leaf leaf28 { + type string; + } + leaf-list ll9 { + type string; + } + } + } + + uses g1; + + rpc create-sfc { + input { + container cont14 { + leaf leaf28 { + type string; + } + } + list list10 { + leaf leaf29 { + type string; + } + } + leaf leaf30 { + type string; + } + leaf-list ll10 { + type string; + } + choice ch3 { + case c1 { + container cont15 { + leaf leaf31 { + type string; + } + } + } + } + uses g1; + } + output { + container cont16 { + leaf leaf32 { + type string; + } + } + list list11 { + leaf leaf33 { + type string; + } + } + leaf leaf34 { + type string; + } + leaf-list ll11 { + type string; + } + choice ch4 { + case c1 { + container cont17 { + leaf leaf35 { + type string; + } + } + } + } + uses g1; + } + } + + container cont18 { + container cont18 { + list list12 { + list list12 { + leaf leaf36 { + type string; + } + } + leaf leaf36 { + type string; + } + } + } + leaf leaf40 { + type string; + } + } +} \ No newline at end of file -- cgit 1.2.3-korg From 4633416831f2d98ca50668d4514cbd1905749201 Mon Sep 17 00:00:00 2001 From: Vidyashree Rama Date: Tue, 21 Aug 2018 13:47:16 +0530 Subject: Handling identity ref type Handling identity ref type in properties node serializer Issue-ID: CCSDK-378 Change-Id: Ie74088a5e2449c09f9712048f1856990b81d05e7 Signed-off-by: Vidyashree Rama --- .../MdsalPropertiesNodeSerializer.java | 8 ++- .../pnserializer/MdsalPropertiesNodeUtils.java | 56 ++++++++++++++++ .../pnserializer/PropertiesSerializerTest.java | 54 +++++++++++++++ .../src/test/resources/yang/identity-test.yang | 77 ++++++++++++++++++++++ .../test/resources/yang/identity-types-second.yang | 25 +++++++ .../src/test/resources/yang/identity-types.yang | 17 +++++ 6 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 restconf-client/provider/src/test/resources/yang/identity-test.yang create mode 100644 restconf-client/provider/src/test/resources/yang/identity-types-second.yang create mode 100644 restconf-client/provider/src/test/resources/yang/identity-types.yang (limited to 'restconf-client/provider/src/test/java') 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 146e581fa..20b06d0bc 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 @@ -34,6 +34,7 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPrope import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNamespace; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNodeType; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE; @@ -121,13 +122,16 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer r) { return (r.isPresent()) ? r.get().toString() : null; } + + /** + * Returns value namespace for leaf value. + * + * @param value value of the leaf + * @param ctx schema context + * @return value namespace + * @throws SvcLogicException if identity/module could not be found + */ + static Namespace getValueNamespace(String value, + SchemaContext ctx) + throws SvcLogicException { + String prefix = getPrefixFromValue(value); + if (prefix == null) { + return null; + } + + IdentitySchemaNode id = IdentityCodecUtil.parseIdentity(value, + ctx, + prefixToModule -> { + final Iterator modules = ctx.findModules(prefix).iterator(); + checkArgument(modules.hasNext(), "Could not find " + + "module %s", prefix); + return modules.next().getQNameModule(); + }); + + if (id == null) { + throw new SvcLogicException("Could not find identity"); + } + + return getModuleNamespace(id.getQName(), ctx); + } + + private static String getPrefixFromValue(String value) { + int lastIndexOfColon = value.lastIndexOf(":"); + if (lastIndexOfColon != -1) { + return value.substring(0, lastIndexOfColon); + } + return null; + } + + private static Namespace getModuleNamespace(QName qName, SchemaContext ctx) + throws SvcLogicException { + Optional module = ctx.findModule(qName.getModule()); + if (!module.isPresent()) { + throw new SvcLogicException("Could not find module node"); + } + Module m = module.get(); + return new Namespace(m.getName(), m.getQNameModule().getNamespace(), + getRevision(m.getRevision())); + } } diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java index fddfe2d40..dc0da6f7c 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java @@ -1071,6 +1071,60 @@ public final class PropertiesSerializerTest { assertThat(node.uri(), is("test-yang:cont18.leaf40")); } + @Test + public void testIdentityRef() throws SvcLogicException { + String uri = "identity-test:test"; + Map params = new HashMap<>(); + params.put("identity-test:test.con1.interface", "identity-types:physical"); + params.put("identity-test:test.con1.interfaces.int-list[0].iden", "identity-test:Giga"); + params.put("identity-test:test.con1.interfaces.int-list[0].available.ll[0]", "identity-types:Loopback"); + params.put("identity-test:test.con1.interfaces.int-list[0].available.leaf1", "identity-types-second:Ethernet"); + params.put("identity-test:test.con1.interfaces.int-list[0].available.leaf2", "identity-types-second:iden2"); + InstanceIdentifierContext iCtx = ParserIdentifier + .toInstanceIdentifier(uri, context, null); + + PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( + iCtx.getSchemaNode(), context, uri); + PropertiesNode node = ser.encode(params); + Map childNodes = ((RootNode) node).children(); + assertThat(childNodes.containsKey("con1"), is(true)); + node = childNodes.get("con1"); + assertThat(node.uri(), is("identity-test:test.con1")); + LeafNode l = ((LeafNode) ((SingleInstanceNode) node).children().get("interface")); + assertThat(l.uri(), is("identity-test:test.con1.interface")); + assertThat(l.valueNs().moduleName(), is("identity-types")); + assertThat(l.valueNs().moduleNs().toString(), is("identity:list:ns:test:json:ser")); + + // identity type inside union + node = ((SingleInstanceNode) ((SingleInstanceNode) node).children().get("interfaces")); + node = ((ListHolderNode) ((SingleInstanceNode) node).children().get("int-list")); + node = ((MultiInstanceNode) ((ListHolderNode) node).children().get("0")); + l = ((LeafNode) ((MultiInstanceNode) node).children().get("iden")); + assertThat(l.uri(), is("identity-test:test.con1.interfaces.int-list[0].iden")); + assertThat(l.valueNs().moduleName(), is("identity-test")); + assertThat(l.valueNs().moduleNs().toString(), is("identity:ns:test:json:ser")); + + // leaf-list test + node = (SingleInstanceNode) ((MultiInstanceNode) node).children().get("available"); + LeafListHolderNode holder = (LeafListHolderNode) ((SingleInstanceNode) node).children().get("ll"); + l = ((LeafNode) holder.children().get("0")); + assertThat(l.uri(), is("identity-test:test.con1.interfaces.int-list[0].available.ll[0]")); + assertThat(l.valueNs().moduleName(), is("identity-types")); + assertThat(l.valueNs().moduleNs().toString(), is("identity:list:ns:test:json:ser")); + + // leaf-ref test + l = ((LeafNode) ((SingleInstanceNode) node).children().get("leaf1")); + assertThat(l.uri(), is("identity-test:test.con1.interfaces.int-list[0].available.leaf1")); + assertThat(l.valueNs().moduleName(), is("identity-types-second")); + assertThat(l.valueNs().moduleNs().toString(), is("identity:list:second:ns:test:json:ser")); + + // list of base identity test + l = ((LeafNode) ((SingleInstanceNode) node).children().get("leaf2")); + assertThat(l.uri(), is("identity-test:test.con1.interfaces.int-list[0].available.leaf2")); + assertThat(l.valueNs().moduleName(), is("identity-types-second")); + assertThat(l.valueNs().moduleNs().toString(), is("identity:list:second:ns:test:json:ser")); + } + public static SchemaContext compileYangFile() throws FileNotFoundException { String path = PropertiesSerializerTest.class.getResource("/yang").getPath(); File dir = new File(path); diff --git a/restconf-client/provider/src/test/resources/yang/identity-test.yang b/restconf-client/provider/src/test/resources/yang/identity-test.yang new file mode 100644 index 000000000..81d3f0a0b --- /dev/null +++ b/restconf-client/provider/src/test/resources/yang/identity-test.yang @@ -0,0 +1,77 @@ +module identity-test { + yang-version 1.1; + namespace "identity:ns:test:json:ser"; + prefix "id"; + + import identity-types { + prefix "type"; + } + + import identity-types-second { + prefix "sec"; + } + + revision "2013-07-15"; + + identity optical { + base type:int-type; + } + + identity Giga { + base type:physical; + } + + typedef available { + type identityref { + base "type:physical"; + } + } + + typedef typed{ + type union { + type int32; + type int8; + type identityref { + base type:int-type; + } + } + } + + container test { + leaf l { + type string; + } + container con1 { + leaf interface { + type identityref { + base "type:int-type"; + } + } + container interfaces { + list int-list { + key "iden"; + leaf iden { + type "id:typed"; + } + container available { + leaf-list ll { + type available; + } + leaf leaf1 { + type leafref { + path "../../iden"; + } + } + + leaf leaf2 { + type identityref { + base type:int-type; + base sec:iden1; + } + } + } + } + } + } + } +} diff --git a/restconf-client/provider/src/test/resources/yang/identity-types-second.yang b/restconf-client/provider/src/test/resources/yang/identity-types-second.yang new file mode 100644 index 000000000..ad752de31 --- /dev/null +++ b/restconf-client/provider/src/test/resources/yang/identity-types-second.yang @@ -0,0 +1,25 @@ +module identity-types-second{ + yang-version 1; + namespace "identity:list:second:ns:test:json:ser"; + prefix "sec"; + + import identity-types { + prefix "type"; + } + + revision "2013-07-15"; + + identity virtual { + base type:int-type; + } + + identity Ethernet { + base type:physical; + } + + identity iden1; + + identity iden2 { + base iden1; + } +} diff --git a/restconf-client/provider/src/test/resources/yang/identity-types.yang b/restconf-client/provider/src/test/resources/yang/identity-types.yang new file mode 100644 index 000000000..25c8fa54f --- /dev/null +++ b/restconf-client/provider/src/test/resources/yang/identity-types.yang @@ -0,0 +1,17 @@ +module identity-types { + yang-version 1; + namespace "identity:list:ns:test:json:ser"; + prefix "type"; + revision "2013-07-15"; + + identity int-type { + } + + identity physical { + base int-type; + } + + identity Loopback { + base physical; + } +} -- cgit 1.2.3-korg From 259eece936114ccacf9acc8f412eeed033fd10c4 Mon Sep 17 00:00:00 2001 From: janani b Date: Wed, 12 Sep 2018 18:30:28 +0530 Subject: UT and defect fixes for DF serializer UT and defect fixes Issue-ID: CCSDK-376 Change-Id: Ia5f04a0d7e07caad0e3b380fdfa6cc109ec43b4e Signed-off-by: janani b --- restconf-client/provider/pom.xml | 20 +- .../restconfapicall/RestconfApiCallNode.java | 39 +- .../dfserializer/DefaultJsonListener.java | 43 +- .../dfserializer/DefaultXmlListener.java | 41 ++ .../dfserializer/DfSerializerUtil.java | 46 +- .../dfserializer/MdsalSerializerHelper.java | 59 +- .../dfserializer/PropertiesNodeXmlListener.java | 8 +- .../DefaultPropertiesNodeListener.java | 6 +- .../MdsalPropertiesNodeSerializer.java | 15 +- .../pnserializer/MdsalPropertiesNodeUtils.java | 8 + .../blueprint/restconf-client-blueprint.xml | 10 +- .../dfserializer/DataFormatSerializerTest.java | 612 +++++++++++++++++++++ .../dfserializer/DataFormatUtilsTest.java | 504 +++++++++++++++++ .../src/test/resources/yang/identity-test.yang | 10 +- .../test/resources/yang/identity-types-second.yang | 2 +- .../src/test/resources/yang/test-augment.yang | 59 +- .../src/test/resources/yang/test-yang.yang | 114 ++-- 17 files changed, 1438 insertions(+), 158 deletions(-) create mode 100644 restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java create mode 100644 restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/pom.xml b/restconf-client/provider/pom.xml index c063c9bd1..db0bee061 100755 --- a/restconf-client/provider/pom.xml +++ b/restconf-client/provider/pom.xml @@ -73,6 +73,12 @@ junit test + + org.mockito + mockito-core + ${mockito.version} + test + org.glassfish.jersey.inject jersey-hk2 @@ -106,23 +112,13 @@ org.opendaylight.netconf restconf-nb-rfc8040 - 1.7.3 - test + ${odl.mdsal.version} org.opendaylight.yangtools yang-parser-impl 2.0.6.1 - - org.opendaylight.netconf - restconf-common - 1.7.3 - - - org.opendaylight.netconf - restconf-nb-bierman02 - 1.7.3 - + diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java index 42caf3685..f9a1ecbf3 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java @@ -36,8 +36,8 @@ import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.SerializerHelper; import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.YangParameters; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeSerializer; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeSerializer; -import org.opendaylight.netconf.sal.restconf.impl.ControllerContext; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; +import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; @@ -90,6 +90,27 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { private static final Logger log = LoggerFactory.getLogger( RestconfApiCallNode.class); + /** + * Rest api call node service instance + */ + private RestapiCallNode restapiCallNode; + + /** + * Creates an instance of restconf api call node with restapi call node. + * + * @param r restapi call node + */ + public RestconfApiCallNode(RestapiCallNode r) { + this.restapiCallNode = r; + } + + /** + * Returns the restapi call node instance. + * @return + */ + public RestapiCallNode getRestapiCallNode() { + return restapiCallNode; + } /** * Sends the restconf request using the parameters map and the memory @@ -116,7 +137,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { */ public void sendRequest(Map paramMap, SvcLogicContext ctx, Integer retryCount) throws SvcLogicException { - RestapiCallNode rest = new RestapiCallNode(); + RestapiCallNode rest = getRestapiCallNode(); RetryPolicy retryPolicy = null; HttpResponse r = new HttpResponse(); try { @@ -203,7 +224,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { * @return JSON or XML message to be sent * @throws SvcLogicException when serializing the request fails */ - protected String serializeRequest(Map properties, + public String serializeRequest(Map properties, YangParameters params, String uri, InstanceIdentifierContext insIdCtx) throws SvcLogicException { @@ -227,9 +248,9 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { * @return response message as properties * @throws SvcLogicException when serializing the response fails */ - protected Map serializeResponse(YangParameters params, - String uri, String response, - InstanceIdentifierContext insIdCtx) + public Map serializeResponse(YangParameters params, + String uri, String response, + InstanceIdentifierContext insIdCtx) throws SvcLogicException { PropertiesNodeSerializer propSer = new MdsalPropertiesNodeSerializer( insIdCtx.getSchemaNode(), insIdCtx.getSchemaContext(), uri); @@ -255,9 +276,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { String uri) throws SvcLogicException { SchemaContext context = getSchemaContext(params); - ControllerContext contCtx = ControllerContext.getInstance(); - contCtx.onGlobalContextUpdated(context); - return contCtx.toInstanceIdentifier(uri); + return ParserIdentifier.toInstanceIdentifier(uri, context, null); } /** @@ -294,7 +313,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { * @param res http response * @return response message body */ - private String getResponse(SvcLogicContext ctx, YangParameters params, + public String getResponse(SvcLogicContext ctx, YangParameters params, String pre, HttpResponse res) { ctx.setAttribute(pre + RES_CODE, String.valueOf(res.code)); ctx.setAttribute(pre + RES_MSG, res.message); diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DefaultJsonListener.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DefaultJsonListener.java index 107585a72..45317522a 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DefaultJsonListener.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DefaultJsonListener.java @@ -52,6 +52,16 @@ public class DefaultJsonListener implements JsonListener { */ private String modName; + /** + * Value of the current JSON node. + */ + private String value; + + /** + * Value namespace of the current JSON node. + */ + private String valueNs; + /** * Creates an instance of default json listener with its serializer helper. * @@ -64,16 +74,18 @@ public class DefaultJsonListener implements JsonListener { @Override public void enterJsonNode(String nodeName, JsonNode node, NodeType nodeType) throws SvcLogicException { - getNodeName(nodeName); + getNodeName(nodeName, false); switch (nodeType) { case SINGLE_INSTANCE_LEAF_NODE: - serializerHelper.addNode(name, modName, node.asText(), null, + getNodeName(node.asText(), true); + serializerHelper.addNode(name, modName, value, valueNs, SINGLE_INSTANCE_LEAF_NODE); break; case MULTI_INSTANCE_LEAF_NODE: - serializerHelper.addNode(name, modName, node.asText(), null, + getNodeName(node.asText(), true); + serializerHelper.addNode(name, modName, value, valueNs, MULTI_INSTANCE_LEAF_NODE); break; @@ -105,18 +117,29 @@ public class DefaultJsonListener implements JsonListener { /** * Parses the abstract JSON name and fills the node name and node - * namespace of the current JSON node. + * namespace or value and value namespace of the current JSON node . * - * @param abstractName abstract JSON name + * @param abstractName full name value + * @param isVal if it is for value parsing */ - private void getNodeName(String abstractName) { + private void getNodeName(String abstractName, boolean isVal) { String[] val = abstractName.split(":"); if (val.length == 2) { - modName = val[0]; - name = val[1]; + if (isVal) { + valueNs = val[0]; + value = val[1]; + } else { + modName = val[0]; + name = val[1]; + } } else { - name = val[0]; + if (isVal) { + value = val[0]; + valueNs = null; + } else { + name = val[0]; + modName = null; + } } } - } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DefaultXmlListener.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DefaultXmlListener.java index 57969146d..03abf44fd 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DefaultXmlListener.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DefaultXmlListener.java @@ -21,8 +21,11 @@ package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer; import org.dom4j.Element; +import org.dom4j.Namespace; import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import java.util.List; + import static java.lang.String.format; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.NODE_TYPE_ERR; @@ -56,6 +59,11 @@ public class DefaultXmlListener implements XmlListener { break; case OBJECT_NODE: + List cont = element.content(); + if (cont != null && cont.size() == 2 && + isValueNsForLeaf(cont, element)) { + return; + } serializerHelper.addNode(element.getName(), element.getNamespace().getURI(), null, null, null); @@ -67,6 +75,39 @@ public class DefaultXmlListener implements XmlListener { } } + /** + * Returns true if element has value namespace and adds the node to + * property tree; false otherwise. + * + * @param cont content of the element + * @param element element + * @return true if element has value namespace; false otherwise + * @throws SvcLogicException + */ + private boolean isValueNsForLeaf(List cont, Element element) + throws SvcLogicException { + for (Object c : cont) { + if (c instanceof Namespace) { + String value = element.getText(); + if (value != null) { + String[] val = value.split(":"); + String valPrefix = val[0]; + String actVal = val[1]; + if (valPrefix != null && actVal != null && + valPrefix.equals(((Namespace) c).getPrefix())) { + serializerHelper.addNode( + element.getName(), + element.getNamespace().getURI(), + actVal, + ((Namespace) c).getURI(), null); + return true; + } + } + } + } + return false; + } + @Override public void exitXmlElement(Element element) throws SvcLogicException { serializerHelper.exitNode(); diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java index 6acb04a81..598b08c1b 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java @@ -26,7 +26,6 @@ import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode; 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.api.TypeDefinition; import org.w3c.dom.Document; import org.xml.sax.InputSource; @@ -46,6 +45,7 @@ import java.io.StringWriter; import java.io.Writer; import java.net.URI; import java.net.URISyntaxException; +import java.util.Iterator; import static javax.xml.transform.OutputKeys.INDENT; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.XmlNodeType.OBJECT_NODE; @@ -150,40 +150,44 @@ public final class DfSerializerUtil { * Returns the resolved namespace object from the input received from the * abstract data format. * - * @param mName module name - * @param curSchema current schema - * @param ctx schema context - * @param mUri module URI - * @param pNode properties node + * @param mName module name + * @param mUri module URI + * @param ctx schema context + * @param parent parent properties node * @return namespace * @throws SvcLogicException when resolving namespace fails */ - static Namespace getResolvedNamespace(String mName, SchemaNode curSchema, - SchemaContext ctx, String mUri, - PropertiesNode pNode) + static Namespace getResolvedNamespace(String mName, String mUri, + SchemaContext ctx, + PropertiesNode parent) throws SvcLogicException { - Module m = null; - URI namespace = curSchema.getQName().getNamespace(); + if (mName == null && mUri == null) { + Namespace parentNs = parent.namespace(); + return new Namespace(parentNs.moduleName(), parentNs.moduleNs(), + parentNs.revision()); + } + Iterator it; + Module mod; if (mName != null) { - m = ctx.findModule(mName).get(); - namespace = m == null ? null : m.getNamespace(); - } - if (mUri != null) { + it = ctx.findModules(mName).iterator(); + } else { + URI modUri = null; try { - m = ctx.findModule(new URI(mUri)).get(); + modUri = new URI(mUri); } catch (URISyntaxException e) { throw new SvcLogicException(URI_ERR, e); } - namespace = m == null ? null : m.getNamespace(); - mName = m.getName(); + it = ctx.findModules(modUri).iterator(); } - if (mName == null && mUri == null) { - return pNode.namespace(); + if (!it.hasNext()) { + return null; } + mod = it.next(); - return new Namespace(mName, namespace, getRevision(m.getRevision())); + return new Namespace(mod.getName(), mod.getQNameModule().getNamespace(), + getRevision(mod.getRevision())); } /** diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java index 5a898dfc5..6f9f9070a 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java @@ -25,6 +25,9 @@ import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.RootNode; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.Revision; +import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; @@ -49,6 +52,8 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.M import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_NODE; +import static org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils.findDataChildSchemaByQName; +import static org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils.findSchemaForChild; import static org.opendaylight.yangtools.yang.data.util.ParserStreamUtils.findSchemaNodeByNameAndNamespace; /** @@ -108,14 +113,17 @@ public class MdsalSerializerHelper extends SerializerHelper baseType = resolveBaseTypeFrom(type); if (baseType instanceof IdentityrefTypeDefinition) { if (nodeType == null) { - ns = getResolvedNamespace(null,schemaNode, getSchemaCtx(), - valNs, propNode); + ns = getResolvedNamespace(null, valNs, getSchemaCtx(), + propNode); } else { - ns = getResolvedNamespace(valNs, schemaNode, getSchemaCtx(), - null, propNode); + ns = getResolvedNamespace(valNs, null, getSchemaCtx(), + propNode); } } } @@ -255,14 +268,28 @@ public class MdsalSerializerHelper extends SerializerHelper dataSchema = findSchemaNodeByNameAndNamespace( - (DataSchemaNode) curSchemaNode, name, namespace.moduleNs()); - if (dataSchema != null) { - DataSchemaNode node = dataSchema.pop(); - if (node != null) { - curSchemaNode = node; - return true; + QName qname = QName.create(namespace.moduleNs(), + Revision.of(namespace.revision()), name); + SchemaNode childNode = null; + if (curSchemaNode instanceof DataSchemaNode) { + Deque dataSchema = findSchemaNodeByNameAndNamespace( + (DataSchemaNode) curSchemaNode, name, namespace.moduleNs()); + if (dataSchema != null && !dataSchema.isEmpty()) { + childNode = dataSchema.pop(); + } + + if (!dataSchema.isEmpty()) { + childNode = findSchemaForChild(((ChoiceSchemaNode) childNode), + qname); } + + } else { + childNode = findDataChildSchemaByQName(curSchemaNode, qname); + } + + if (childNode != null) { + curSchemaNode = childNode; + return true; } return false; } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeXmlListener.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeXmlListener.java index cf59b7794..39a08e387 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeXmlListener.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeXmlListener.java @@ -42,6 +42,8 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializ import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.UTF_HEADER; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_PREFIX; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.getXmlWriter; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_HOLDER_NODE; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_HOLDER_NODE; /** * Representation of XML implementation of properties node listener. @@ -193,7 +195,11 @@ public class PropertiesNodeXmlListener implements PropertiesNodeListener { */ private String getNodeNamespace(PropertiesNode node) { PropertiesNode parent = node.parent(); - if (parent instanceof RootNode || !parent.namespace().moduleName() + if (parent.nodeType() == MULTI_INSTANCE_HOLDER_NODE || + parent.nodeType() == MULTI_INSTANCE_LEAF_HOLDER_NODE) { + parent = parent.parent(); + } + if (parent instanceof RootNode || ! parent.namespace().moduleName() .equals(node.namespace().moduleName())) { return node.namespace().moduleNs().toString(); } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java index d91209121..491dcb099 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java @@ -54,7 +54,11 @@ public class DefaultPropertiesNodeListener implements PropertiesNodeListener { */ if (node.nodeType() == SINGLE_INSTANCE_LEAF_NODE || node.nodeType() == MULTI_INSTANCE_LEAF_NODE) { - params.put(node.uri(), ((LeafNode) node).value()); + String val = ((LeafNode) node).value(); + if (((LeafNode) node).valueNs() != null) { + val = ((LeafNode) node).valueNs().moduleName() + ":" + val; + } + params.put(node.uri(), val); } } 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 c24146267..13a5c5a01 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 @@ -33,6 +33,7 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPrope import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getListName; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNamespace; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNodeType; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getParsedValue; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName; @@ -116,26 +117,32 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer - + + + + + + diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java new file mode 100644 index 000000000..0f46d625b --- /dev/null +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java @@ -0,0 +1,612 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.yangserializers.dfserializer; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.plugins.restapicall.HttpResponse; +import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode; +import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_JSON_RPC; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_XML_RPC; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_RPC; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_RPC; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG; + + +/** + * Unit test cases for data format serialization and restconf api call node. + */ +public class DataFormatSerializerTest { + + private Map p; + + private RestconfApiCallNode restconf; + + private RestapiCallNode restApi; + + private DfCaptor dfCaptor; + + /** + * Sets up the pre-requisite for each test case. + * + * @throws SvcLogicException when test case fails + */ + @Before + public void setUp() throws SvcLogicException { + p = new HashMap<>(); + p.put("restapiUser", "user1"); + p.put("restapiPassword", "abc123"); + p.put("responsePrefix", "response"); + p.put("skipSending", "true"); + restApi = new RestapiCallNode(); + restconf = mock(RestconfApiCallNode.class); + dfCaptor = new DfCaptor(); + createMethodMocks(); + } + + /** + * Creates method mocks using mockito for RestconfApiCallNode class. + * + * @throws SvcLogicException when test case fails + */ + private void createMethodMocks() throws SvcLogicException { + doReturn(restApi).when(restconf).getRestapiCallNode(); + doCallRealMethod().when(restconf).sendRequest( + any(Map.class), any(SvcLogicContext.class)); + doCallRealMethod().when(restconf).sendRequest( + any(Map.class), any(SvcLogicContext.class), any(Integer.class)); + doAnswer(dfCaptor).when(restconf).serializeRequest( + any(Map.class), any(YangParameters.class), any(String.class), + any(InstanceIdentifierContext.class)); + } + + /** + * Creates mock using mockito with input data for decoding. + * + * @param decodeData input data + * @throws SvcLogicException when test case fails + */ + private void createMockForDecode(String decodeData) + throws SvcLogicException { + doReturn(decodeData).when(restconf).getResponse( + any(SvcLogicContext.class), any(YangParameters.class), + any(String.class), any(HttpResponse.class)); + doCallRealMethod().when(restconf).serializeResponse( + any(YangParameters.class), any(String.class), any(String.class), + any(InstanceIdentifierContext.class)); + } + + /** + * Verifies encoding of parameters to JSON data format with identity-ref + * and inter-file linking. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonId() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + ctx.setAttribute(pre + "l", "abc"); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID)); + } + + /** + * Verifies encoding of parameters to XML data format with identity-ref + * and inter-file linking. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlId() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID)); + } + + /** + * Verifies decoding of parameters from JSON data format with identity-ref + * and inter-file linking. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void decodeToJsonId() throws SvcLogicException { + createMockForDecode(ENCODE_TO_JSON_ID); + SvcLogicContext ctx = new SvcLogicContext(); + String pre = "identity-test:test."; + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "get"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(ctx.getAttribute(pre + "l"), is("abc")); + verifyAttList(ctx, pre); + } + + /** + * Verifies decoding of parameters from XML data format with identity-ref + * and inter-file linking. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void decodeToXmlId() throws SvcLogicException { + createMockForDecode(ENCODE_TO_XML_ID); + SvcLogicContext ctx = new SvcLogicContext(); + String pre = "identity-test:test."; + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "get"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + verifyAttList(ctx, pre); + } + + /** + * Verifies encoding of parameters to JSON data format with containers, + * grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYang() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG)); + } + + /** + * Verifies decoding of parameters from JSON data format with containers, + * grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void decodeToJsonYang() throws SvcLogicException { + createMockForDecode(ENCODE_TO_JSON_YANG); + SvcLogicContext ctx = new SvcLogicContext(); + String pre = "test-yang:cont1.cont2."; + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "get"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1"); + restconf.sendRequest(p, ctx); + verifyAttListYang(ctx, pre); + } + + /** + * Verifies encoding of parameters to XML data format with containers, + * grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlYang() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG)); + } + + /** + * Verifies decoding of parameters from XML data format with containers, + * grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void decodeToXmlYang() throws SvcLogicException { + createMockForDecode(ENCODE_TO_XML_YANG); + SvcLogicContext ctx = new SvcLogicContext(); + String pre = "test-yang:cont1.cont2."; + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "get"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1"); + restconf.sendRequest(p, ctx); + verifyAttListYang(ctx, pre); + } + + /** + * Verifies encoding of and decoding from, JSON respectively for data + * format with containers, grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void codecToJsonRpc() throws SvcLogicException { + createMockForDecode(DECODE_FROM_JSON_RPC); + String inPre = "test-yang:create-sfc.input."; + String outPre = "test-yang:create-sfc.output."; + SvcLogicContext ctx = createAttListRpc(inPre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:create-sfc"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_RPC)); + verifyAttListRpc(ctx, outPre); + } + + /** + * Verifies encoding of and decoding from, XML respectively for data + * format with containers, grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void codecToXmlRpc() throws SvcLogicException { + createMockForDecode(DECODE_FROM_XML_RPC); + String inPre = "test-yang:create-sfc.input."; + String outPre = "test-yang:create-sfc.output."; + SvcLogicContext ctx = createAttListRpc(inPre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:create-sfc"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_RPC)); + verifyAttListRpc(ctx, outPre); + } + + /** + * Creates attribute list for encoding JSON or XML with identity-ref YANG + * file. + * + * @param pre prefix + * @return service logic context + */ + private SvcLogicContext createAttList(String pre) { + SvcLogicContext ctx = new SvcLogicContext(); + String pre1 = pre + "con1.interfaces."; + ctx.setAttribute(pre + "con1.interface", "identity-types:physical"); + ctx.setAttribute(pre1 + "int-list[0].iden", "optical"); + ctx.setAttribute(pre1 + "int-list[0].available.ll[0]", "Giga"); + ctx.setAttribute(pre1 + "int-list[0].available.ll[1]", + "identity-types:Loopback"); + ctx.setAttribute(pre1 + "int-list[0].available.ll[2]", + "identity-types-second:Ethernet"); + ctx.setAttribute(pre1 + "int-list[0].available.leaf1", "58"); + ctx.setAttribute(pre1 + "int-list[0].available.leaf2", + "identity-types-second:iden2"); + + ctx.setAttribute(pre1 + "int-list[1].iden", "214748364"); + ctx.setAttribute(pre1 + "int-list[1].available.ll[0]", "Giga"); + ctx.setAttribute(pre1 + "int-list[1].available.ll[1]", + "identity-types:Loopback"); + ctx.setAttribute(pre1 + "int-list[1].available.ll[2]", + "identity-types-second:Ethernet"); + ctx.setAttribute(pre1 + "int-list[1].available.leaf1", + "8888"); + ctx.setAttribute(pre1 + "int-list[1].available.leaf2", + "identity-types-second:iden2"); + return ctx; + } + + /** + * Creates attribute list for encoding JSON or XML with container, + * grouping and augmented YANG file. + * + * @param pre prefix + * @return service logic context + */ + private SvcLogicContext createAttListYang(String pre) { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(pre + "cont3.leaf10", "abc"); + ctx.setAttribute(pre + "list1[0].leaf1", "true"); + ctx.setAttribute(pre + "list1[0].leaf2", "abc"); + ctx.setAttribute(pre + "list1[0].leaf3", "abc"); + ctx.setAttribute(pre + "list1[0].ll1[0]", "abc"); + ctx.setAttribute(pre + "list1[0].ll1[1]", "abc"); + ctx.setAttribute(pre + "list1[0].ll2[0]", "abc"); + ctx.setAttribute(pre + "list1[0].ll2[1]", "abc"); + ctx.setAttribute(pre + "list1[0].cont4.leaf11", "abc"); + ctx.setAttribute(pre + "list1[0].list4[0].leaf8", "abc"); + ctx.setAttribute(pre + "list1[0].list4[1].leaf8", "abc"); + ctx.setAttribute(pre + "list1[0].list5[0].leaf9", "abc"); + ctx.setAttribute(pre + "list1[0].list5[1].leaf9", "abc"); + ctx.setAttribute(pre + "list1[1].leaf1", "true"); + ctx.setAttribute(pre + "list1[1].leaf2", "abc"); + ctx.setAttribute(pre + "list1[1].leaf3", "abc"); + ctx.setAttribute(pre + "list1[1].ll1[0]", "abc"); + ctx.setAttribute(pre + "list1[1].ll1[1]", "abc"); + ctx.setAttribute(pre + "list1[1].ll2[0]", "abc"); + ctx.setAttribute(pre + "list1[1].ll2[1]", "abc"); + ctx.setAttribute(pre + "list1[1].cont4.leaf11", "abc"); + ctx.setAttribute(pre + "list1[1].list4[0].leaf8", "abc"); + ctx.setAttribute(pre + "list1[1].list4[1].leaf8", "abc"); + ctx.setAttribute(pre + "list1[1].list5[0].leaf9", "abc"); + ctx.setAttribute(pre + "list1[1].list5[1].leaf9", "abc"); + ctx.setAttribute(pre + "list2[0].leaf4", "abc"); + ctx.setAttribute(pre + "list2[1].leaf4", "abc"); + ctx.setAttribute(pre + "leaf5", "abc"); + ctx.setAttribute(pre + "leaf6", "abc"); + ctx.setAttribute(pre + "ll3[0]", "abc"); + ctx.setAttribute(pre + "ll3[1]", "abc"); + ctx.setAttribute(pre + "ll4[0]", "abc"); + ctx.setAttribute(pre + "ll4[1]", "abc"); + ctx.setAttribute(pre + "cont4.leaf10", "abc"); + ctx.setAttribute(pre + "list6[0].leaf11", "abc"); + ctx.setAttribute(pre + "list6[1].leaf11", "abc"); + ctx.setAttribute(pre + "leaf12", "abc"); + ctx.setAttribute(pre + "ll5[0]", "abc"); + ctx.setAttribute(pre + "ll5[1]", "abc"); + ctx.setAttribute(pre + "cont4.test-augment:cont5.leaf13", "true"); + ctx.setAttribute(pre + "cont4.test-augment:list7[0].leaf14", "test"); + ctx.setAttribute(pre + "cont4.test-augment:list7[1].leaf14", "create"); + ctx.setAttribute(pre + "cont4.test-augment:leaf15", "abc"); + ctx.setAttribute(pre + "cont4.test-augment:ll6[0]", "unbounded"); + ctx.setAttribute(pre + "cont4.test-augment:ll6[1]", "8"); + ctx.setAttribute(pre + "cont4.test-augment:cont13.cont12.leaf26", + "abc"); + ctx.setAttribute(pre + "cont4.test-augment:cont13.list9[0].leaf27", + "abc"); + ctx.setAttribute(pre + "cont4.test-augment:cont13.list9[1].leaf27", + "abc"); + ctx.setAttribute(pre + "cont4.test-augment:cont13.leaf28", "abc"); + ctx.setAttribute(pre + "cont4.test-augment:cont13.ll9[0]", "abc"); + ctx.setAttribute(pre + "cont4.test-augment:cont13.ll9[1]", "abc"); + return ctx; + } + + /** + * Creates attribute list for encoding JSON or XML with RPC YANG file. + * + * @param pre prefix + * @return service logic context + */ + private SvcLogicContext createAttListRpc(String pre) { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(pre + "cont14.leaf28", "abc"); + ctx.setAttribute(pre + "list10[0].leaf29", "abc"); + ctx.setAttribute(pre + "list10[1].leaf29", "abc"); + ctx.setAttribute(pre + "leaf30", "abc"); + ctx.setAttribute(pre + "ll10[0]", "abc"); + ctx.setAttribute(pre + "ll10[1]", "abc"); + ctx.setAttribute(pre + "cont15.leaf31", "abc"); + ctx.setAttribute(pre + "cont13.list9[0].leaf27", "abc"); + ctx.setAttribute(pre + "cont13.list9[1].leaf27", "abc"); + ctx.setAttribute(pre + "cont13.leaf28", "abc"); + ctx.setAttribute(pre + "cont13.ll9[0]", "abc"); + ctx.setAttribute(pre + "cont13.ll9[1]", "abc"); + return ctx; + } + + /** + * Verifies the attribute list for decoding from JSON or XML with + * identity-ref YANG file. + * + * @param ctx service logic context + * @param pre prefix + */ + private void verifyAttList(SvcLogicContext ctx, String pre) { + String pre1 = pre + "con1.interfaces."; + assertThat(ctx.getAttribute(pre + "con1.interface"), is( + "identity-types:physical")); + assertThat(ctx.getAttribute(pre + "con1.interface"), is( + "identity-types:physical")); + assertThat(ctx.getAttribute(pre1 + "int-list[0].iden"), is("optical")); + assertThat(ctx.getAttribute(pre1 + "int-list[0].available.ll[0]"), is( + "Giga")); + assertThat(ctx.getAttribute(pre1 + "int-list[0].available.ll[1]"), is( + "identity-types:Loopback")); + assertThat(ctx.getAttribute(pre1 + "int-list[0].available.ll[2]"), is( + "identity-types-second:Ethernet")); + assertThat(ctx.getAttribute(pre1 + "int-list[0].available.leaf1"), is( + "58")); + assertThat(ctx.getAttribute(pre1 + "int-list[0].available.leaf2"), is( + "identity-types-second:iden2")); + + assertThat(ctx.getAttribute(pre1 + "int-list[1].iden"), is( + "214748364")); + assertThat(ctx.getAttribute(pre1 + "int-list[1].available.ll[0]"), is( + "Giga")); + assertThat(ctx.getAttribute(pre1 + "int-list[1].available.ll[1]"), is( + "identity-types:Loopback")); + assertThat(ctx.getAttribute(pre1 + "int-list[1].available.ll[2]"), is( + "identity-types-second:Ethernet")); + assertThat(ctx.getAttribute(pre1 + "int-list[1].available.leaf1"), is( + "8888")); + assertThat(ctx.getAttribute(pre1 + "int-list[1].available.leaf2"), is( + "identity-types-second:iden2")); + } + + /** + * Verifies the attribute list for decoding from JSON or XML with + * container, grouping and augmented file. + * + * @param ctx service logic context + * @param pre prefix + */ + private void verifyAttListYang(SvcLogicContext ctx, String pre) { + assertThat(ctx.getAttribute(pre + "cont3.leaf10"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].leaf1"), is("true")); + assertThat(ctx.getAttribute(pre + "list1[0].leaf2"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].leaf3"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].ll1[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].ll1[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].ll2[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].ll2[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].cont4.leaf11"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].list4[0].leaf8"), + is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].list4[1].leaf8"), + is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].list5[0].leaf9"), + is("abc")); + assertThat(ctx.getAttribute(pre + "list1[0].list5[1].leaf9"), + is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].leaf1"), is("true")); + assertThat(ctx.getAttribute(pre + "list1[1].leaf2"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].leaf3"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].ll1[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].ll1[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].ll2[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].ll2[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].cont4.leaf11"), is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].list4[0].leaf8"), + is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].list4[1].leaf8"), + is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].list5[0].leaf9"), + is("abc")); + assertThat(ctx.getAttribute(pre + "list1[1].list5[1].leaf9"), + is("abc")); + assertThat(ctx.getAttribute(pre + "list2[0].leaf4"), is("abc")); + assertThat(ctx.getAttribute(pre + "list2[1].leaf4"), is("abc")); + assertThat(ctx.getAttribute(pre + "leaf5"), is("abc")); + assertThat(ctx.getAttribute(pre + "leaf6"), is("abc")); + assertThat(ctx.getAttribute(pre + "ll3[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "ll3[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "ll4[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "ll4[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont4.leaf10"), is( "abc")); + assertThat(ctx.getAttribute(pre + "list6[0].leaf11"), is("abc")); + assertThat(ctx.getAttribute(pre + "list6[1].leaf11"), is("abc")); + assertThat(ctx.getAttribute(pre + "leaf12"), is("abc")); + assertThat(ctx.getAttribute(pre + "ll5[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "ll5[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont5.leaf13"), + is("true")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:list7[0].leaf14"), + is("test")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:list7[1].leaf14"), + is("create")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:leaf15"), + is("abc")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:ll6[0]"), + is("unbounded")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:ll6[1]"), + is("8")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13" + + ".cont12.leaf26"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.list9[0]" + + ".leaf27"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.list9[1]" + + ".leaf27"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.leaf28"), + is("abc")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.ll9[0]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.ll9[1]"), + is("abc")); + } + + /** + * Verifies the attribute list for decoding from JSON or XML with + * RPC YANG file. + * + * @param ctx service logic context + * @param pre prefix + */ + private void verifyAttListRpc(SvcLogicContext ctx, String pre) { + assertThat(ctx.getAttribute(pre + "cont16.leaf32"), is("abc")); + assertThat(ctx.getAttribute(pre + "list11[0].leaf33"), is("abc")); + assertThat(ctx.getAttribute(pre + "list11[1].leaf33"), is("abc")); + assertThat(ctx.getAttribute(pre + "leaf34"), is("abc")); + assertThat(ctx.getAttribute(pre + "ll11[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "ll11[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont17.leaf35"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont13.cont12.leaf26"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont13.list9[0].leaf27"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont13.list9[1].leaf27"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont13.ll9[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont13.ll9[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "cont13.leaf28"), is("abc")); + } + + /** + * Captures the data format messages by mocking it, which can be used in + * testing the value. + * + * @param capturing data format + */ + public class DfCaptor implements Answer { + + private String result; + + /** + * Returns the captured data format message. + * + * @return data format message. + */ + public String getResult() { + return result; + } + + @Override + public String answer(InvocationOnMock invocationOnMock) + throws Throwable { + result = (String) invocationOnMock.callRealMethod(); + return result; + } + } + +} diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java new file mode 100644 index 000000000..c7efdeb37 --- /dev/null +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java @@ -0,0 +1,504 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.yangserializers.dfserializer; + +/** + * Unit test case utilities for data format serializer and restconf api call + * node. + */ +public final class DataFormatUtilsTest { + + static final String ENCODE_TO_JSON_ID = "{\n" + + " \"identity-test:con1\": {\n" + + " \"interfaces\": {\n" + + " \"int-list\": [\n" + + " {\n" + + " \"iden\": \"optical\",\n" + + " \"available\": {\n" + + " \"ll\": [\n" + + " \"Giga\",\n" + + " \"identity-types:Loopback\",\n" + + " \"identity-types-second:Ethernet" + + "\"\n" + + " ],\n" + + " \"leaf1\": \"58\",\n" + + " \"leaf2\": \"identity-types-second:iden" + + "2\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"iden\": \"214748364\",\n" + + " \"available\": {\n" + + " \"ll\": [\n" + + " \"Giga\",\n" + + " \"identity-types:Loopback\",\n" + + " \"identity-types-second:Ethernet" + + "\"\n" + + " ],\n" + + " \"leaf1\": \"8888\",\n" + + " \"leaf2\": \"identity-types-second:ide" + + "n2\"\n" + + " }\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"interface\": \"identity-types:physical\"\n" + + " },\n" + + " \"identity-test:l\": \"abc\"\n" + + "}"; + + static final String ENCODE_TO_XML_ID = "\n" + + "\n" + + " \n" + + " \n" + + " optical\n" + + " \n" + + " Giga\n" + + " yangid:Loopback\n" + + " yangid:Ethernet\n" + + " 58\n" + + " yangid:iden2\n" + + " \n" + + " \n" + + " \n" + + " 214748364\n" + + " \n" + + " Giga\n" + + " yangid:Loopback\n" + + " yangid:Ethernet\n" + + " 8888\n" + + " yangid:iden2\n" + + " \n" + + " \n" + + " \n" + + " " + + "yangid:physical\n" + + "\n"; + + static final String ENCODE_TO_JSON_YANG = "{\n" + + " \"test-yang:cont2\": {\n" + + " \"list1\": [\n" + + " {\n" + + " \"ll1\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"leaf1\": \"true\",\n" + + " \"ll2\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"list5\": [\n" + + " {\n" + + " \"leaf9\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf9\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"leaf3\": \"abc\",\n" + + " \"leaf2\": \"abc\",\n" + + " \"list4\": [\n" + + " {\n" + + " \"leaf8\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf8\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"cont4\": {\n" + + " \"leaf11\": \"abc\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"ll1\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"leaf1\": \"true\",\n" + + " \"ll2\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"leaf3\": \"abc\",\n" + + " \"list5\": [\n" + + " {\n" + + " \"leaf9\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf9\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"list4\": [\n" + + " {\n" + + " \"leaf8\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf8\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"leaf2\": \"abc\",\n" + + " \"cont4\": {\n" + + " \"leaf11\": \"abc\"\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"ll3\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"ll5\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"cont4\": {\n" + + " \"leaf10\": \"abc\",\n" + + " \"test-augment:cont13\": {\n" + + " \"ll9\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"list9\": [\n" + + " {\n" + + " \"leaf27\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf27\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"leaf28\": \"abc\",\n" + + " \"cont12\": {\n" + + " \"leaf26\": \"abc\"\n" + + " }\n" + + " },\n" + + " \"test-augment:list7\": [\n" + + " {\n" + + " \"leaf14\": \"test\"\n" + + " },\n" + + " {\n" + + " \"leaf14\": \"create\"\n" + + " }\n" + + " ],\n" + + " \"test-augment:leaf15\": \"abc\",\n" + + " \"test-augment:ll6\": [\n" + + " \"unbounded\",\n" + + " \"8\"\n" + + " ],\n" + + " \"test-augment:cont5\": {\n" + + " \"leaf13\": \"true\"\n" + + " }\n" + + " },\n" + + " \"ll4\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"cont3\": {\n" + + " \"leaf10\": \"abc\"\n" + + " },\n" + + " \"leaf5\": \"abc\",\n" + + " \"list2\": [\n" + + " {\n" + + " \"leaf4\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf4\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"leaf12\": \"abc\",\n" + + " \"leaf6\": \"abc\",\n" + + " \"list6\": [\n" + + " {\n" + + " \"leaf11\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf11\": \"abc\"\n" + + " }\n" + + " ]\n" + + " }\n" + + "}"; + + static final String ENCODE_TO_XML_YANG = "\n" + + "\n" + + " \n" + + " abc\n" + + " abc\n" + + " true\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " \n" + + " abc\n" + + " abc\n" + + " true\n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " \n" + + " test\n" + + " \n" + + " \n" + + " create\n" + + " \n" + + " abc\n" + + " unbounded\n" + + " 8\n" + + " \n" + + " true\n" + + " \n" + + " \n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + "\n"; + + + static final String ENCODE_TO_JSON_RPC = "{\n" + + " \"test-yang:input\": {\n" + + " \"leaf30\": \"abc\",\n" + + " \"list10\": [\n" + + " {\n" + + " \"leaf29\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf29\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"cont15\": {\n" + + " \"leaf31\": \"abc\"\n" + + " },\n" + + " \"cont14\": {\n" + + " \"leaf28\": \"abc\"\n" + + " },\n" + + " \"cont13\": {\n" + + " \"list9\": [\n" + + " {\n" + + " \"leaf27\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf27\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"ll9\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"leaf28\": \"abc\"\n" + + " },\n" + + " \"ll10\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ]\n" + + " }\n" + + "}"; + + static final String DECODE_FROM_JSON_RPC = "{\n" + + " \"test-yang:output\": {\n" + + " \"cont16\": {\n" + + " \"leaf32\": \"abc\"\n" + + " },\n" + + " \"list11\": [\n" + + " {\n" + + " \"leaf33\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf33\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"leaf34\": \"abc\",\n" + + " \"ll11\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"cont17\": {\n" + + " \"leaf35\": \"abc\"\n" + + " },\n" + + " \"cont13\": {\n" + + " \"cont12\": {\n" + + " \"leaf26\": \"abc\"\n" + + " },\n" + + " \"list9\": [\n" + + " {\n" + + " \"leaf27\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf27\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"ll9\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"leaf28\": \"abc\"\n" + + " }\n" + + " }\n" + + "}"; + + static final String ENCODE_TO_XML_RPC = "\n" + + "\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + "\n"; + + static final String DECODE_FROM_XML_RPC = "\n" + + "\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + ""; +} diff --git a/restconf-client/provider/src/test/resources/yang/identity-test.yang b/restconf-client/provider/src/test/resources/yang/identity-test.yang index 81d3f0a0b..12ef717f6 100644 --- a/restconf-client/provider/src/test/resources/yang/identity-test.yang +++ b/restconf-client/provider/src/test/resources/yang/identity-test.yang @@ -58,16 +58,16 @@ module identity-test { type available; } leaf leaf1 { - type leafref { - path "../../iden"; - } + type leafref { + path "../../iden"; + } } leaf leaf2 { - type identityref { + type identityref { base type:int-type; base sec:iden1; - } + } } } } diff --git a/restconf-client/provider/src/test/resources/yang/identity-types-second.yang b/restconf-client/provider/src/test/resources/yang/identity-types-second.yang index ad752de31..98d6a6e60 100644 --- a/restconf-client/provider/src/test/resources/yang/identity-types-second.yang +++ b/restconf-client/provider/src/test/resources/yang/identity-types-second.yang @@ -1,4 +1,4 @@ -module identity-types-second{ +module identity-types-second { yang-version 1; namespace "identity:list:second:ns:test:json:ser"; prefix "sec"; diff --git a/restconf-client/provider/src/test/resources/yang/test-augment.yang b/restconf-client/provider/src/test/resources/yang/test-augment.yang index bac19171b..795000d39 100644 --- a/restconf-client/provider/src/test/resources/yang/test-augment.yang +++ b/restconf-client/provider/src/test/resources/yang/test-augment.yang @@ -14,19 +14,24 @@ module test-augment { augment "/t:cont1/t:cont2/t:cont4" { container cont5 { leaf leaf13 { - type string; + type empty; } } list list7 { - leaf leaf14 { - type string; - } + leaf leaf14 { + type instance-identifier; + } } leaf leaf15 { type string; } leaf-list ll6 { - type string; + type union { + type int32; + type enumeration { + enum "unbounded"; + } + } } uses "t:g1"; } @@ -34,16 +39,22 @@ module test-augment { uses "t:g1"; augment "/t:ch1/t:c1/t:cont8/t:cont6" { choice ch2 { - case c3 { - leaf leaf21 { - type string; - } - } - case c4 { - leaf leaf22 { - type string; - } - } + case c3 { + leaf leaf21 { + type string; + } + } + case c4 { + leaf leaf22 { + type enumeration { + enum zero; + enum one; + enum seven { + value 7; + } + } + } + } } } @@ -72,14 +83,24 @@ module test-augment { } augment "/t:create-sfc/t:input" { - leaf leaf36 { - type string; - } + leaf leaf36 { + type bits { + bit angle { + position 0; + } + bit degree { + position 1; + } + bit movement { + position 2; + } + } + } } augment "/t:create-sfc/t:output" { leaf leaf37 { - type string; + type boolean; } } } \ No newline at end of file diff --git a/restconf-client/provider/src/test/resources/yang/test-yang.yang b/restconf-client/provider/src/test/resources/yang/test-yang.yang index e99d87bf8..b2bf06003 100644 --- a/restconf-client/provider/src/test/resources/yang/test-yang.yang +++ b/restconf-client/provider/src/test/resources/yang/test-yang.yang @@ -17,7 +17,7 @@ module test-yang { list list1 { key "leaf1 leaf2"; leaf leaf1 { - type string; + type empty; } leaf leaf2 { type string; @@ -87,66 +87,66 @@ module test-yang { } choice ch1 { - case c1 { - container cont8 { - container cont6 { - leaf leaf16 { - type string; - } - } - list list8 { - leaf leaf18 { - type string; - } - } - leaf leaf19 { - type string; - } - leaf-list ll7 { - type string; - } - } - } - case c2 { - container cont9 { - leaf leaf20 { - type string; - } - leaf-list ll8 { - type string; - } - container cont11 { - choice ch3 { - case c1 { - leaf leaf25 { + case c1 { + container cont8 { + container cont6 { + leaf leaf16 { type string; - } - uses g1; - } - } - } - } - } + } + } + list list8 { + leaf leaf18 { + type string; + } + } + leaf leaf19 { + type string; + } + leaf-list ll7 { + type string; + } + } + } + case c2 { + container cont9 { + leaf leaf20 { + type string; + } + leaf-list ll8 { + type string; + } + container cont11 { + choice ch3 { + case c1 { + leaf leaf25 { + type string; + } + uses g1; + } + } + } + } + } } grouping g1 { - container cont13 { - container cont12 { - leaf leaf26 { - type string; - } - } - list list9 { - leaf leaf27 { - type string; - } - } - leaf leaf28 { - type string; - } - leaf-list ll9 { - type string; - } + container cont13 { + container cont12 { + leaf leaf26 { + type string; + } + } + list list9 { + leaf leaf27 { + type string; + } + } + leaf leaf28 { + type string; + } + leaf-list ll9 { + type string; + } } } -- cgit 1.2.3-korg From 2092b7c383f1a3280da05b71517fae6521b73327 Mon Sep 17 00:00:00 2001 From: janani b Date: Wed, 19 Sep 2018 21:10:17 +0530 Subject: Issue fix for RestconfApiCallNode Sonar and Issue fix Issue-ID: CCSDK-325 Change-Id: I469fcc5c4da2ec26c3a39b48d52736d45c1718d4 Signed-off-by: janani b --- .../restconfapicall/RestconfApiCallNode.java | 18 ++++---- .../plugins/restconfapicall/RestconfApiUtils.java | 29 ++++++++++--- .../dfserializer/MdsalSerializerHelper.java | 3 +- .../dfserializer/DataFormatSerializerTest.java | 49 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 16 deletions(-) (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java index f9a1ecbf3..91ce3338a 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java @@ -54,6 +54,7 @@ import java.util.Map; import static java.lang.String.format; import static org.apache.commons.lang3.StringUtils.join; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PATCH; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.POST; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.parseParam; @@ -153,7 +154,8 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { InstanceIdentifierContext insIdCtx = getInsIdCtx(p, uri); String req = null; - if (p.httpMethod == POST || p.httpMethod == PUT) { + if (p.httpMethod == POST || p.httpMethod == PUT + || p.httpMethod == PATCH) { req = serializeRequest(props, p, uri, insIdCtx); } if (req == null && p.requestBody != null) { @@ -181,8 +183,8 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { log.error(REQ_ERR + e.getMessage(), e); String prefix = parseParam(paramMap, RES_PRE, false, null); - if (retryPolicy == null || shouldRetry == false) { - setFailureResponseStatus(ctx, prefix, e.getMessage(), r); + if (retryPolicy == null || !shouldRetry) { + setFailureResponseStatus(ctx, prefix, e.getMessage()); } else { if (retryCount == null) { retryCount = 0; @@ -198,12 +200,11 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { sendRequest(paramMap, ctx, retryCount); } else { log.debug(MAX_RETRY_ERR); - setFailureResponseStatus(ctx, prefix, - e.getMessage(), r); + setFailureResponseStatus(ctx, prefix, e.getMessage()); } } catch (Exception ex) { log.error(NO_MORE_RETRY, ex); - setFailureResponseStatus(ctx, prefix, RETRY_FAIL, r); + setFailureResponseStatus(ctx, prefix, RETRY_FAIL); } } } @@ -338,11 +339,10 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { * @param ctx service logic context * @param prefix prefix to be added * @param errMsg error message - * @param res http response */ private void setFailureResponseStatus(SvcLogicContext ctx, String prefix, - String errMsg, HttpResponse res) { - res = new HttpResponse(); + String errMsg) { + HttpResponse res = new HttpResponse(); res.code = 500; res.message = errMsg; ctx.setAttribute(prefix + RES_CODE, String.valueOf(res.code)); diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java index 0f9c9401c..b51272f7e 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java @@ -97,8 +97,6 @@ public final class RestconfApiUtils { private static final String URL_SYNTAX = "The following URL cannot be " + "parsed into URI : "; - private static final String RESTCONF_PATH = "/restconf/operations/"; - private static final String PUT_NODE_ERR = "The following URL does not " + "contain minimum two nodes for PUT operation."; @@ -136,7 +134,7 @@ public final class RestconfApiUtils { * @return YANG path pointing to parent * @throws SvcLogicException when parsing the URL fails */ - static String parseUrl(String url, HttpMethod method) + public static String parseUrl(String url, HttpMethod method) throws SvcLogicException { URI uri; try { @@ -146,9 +144,7 @@ public final class RestconfApiUtils { } String path = uri.getPath(); - if (path.contains(RESTCONF_PATH)) { - path = path.replaceFirst(RESTCONF_PATH, ""); - } + path = getParsedPath(path); if (method == PUT) { if (!path.contains(SLASH)) { throw new SvcLogicException(PUT_NODE_ERR + url); @@ -158,6 +154,27 @@ public final class RestconfApiUtils { return path; } + /** + * Returns the path which contains only the schema nodes. + * + * @param path path + * @return path representing schema + */ + private static String getParsedPath(String path) { + String firstHalf; + if (path.contains(":")) { + String[] p = path.split(":"); + if (p[0].contains(SLASH)) { + int slash = p[0].lastIndexOf(SLASH); + firstHalf = p[0].substring(slash + 1); + } else { + firstHalf = p[0]; + } + return firstHalf + ":" + p[1]; + } + return path; + } + /** * Returns the schema context of the YANG files present in a directory. * diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java index 6f9f9070a..1fd0d2dee 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/MdsalSerializerHelper.java @@ -274,11 +274,12 @@ public class MdsalSerializerHelper extends SerializerHelper dataSchema = findSchemaNodeByNameAndNamespace( (DataSchemaNode) curSchemaNode, name, namespace.moduleNs()); + if (dataSchema != null && !dataSchema.isEmpty()) { childNode = dataSchema.pop(); } - if (!dataSchema.isEmpty()) { + if (dataSchema != null && !dataSchema.isEmpty()) { childNode = findSchemaForChild(((ChoiceSchemaNode) childNode), qname); } diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java index 0f46d625b..6c11206c9 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java @@ -41,6 +41,10 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.GET; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.POST; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; +import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_JSON_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_XML_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID; @@ -316,6 +320,51 @@ public class DataFormatSerializerTest { verifyAttListRpc(ctx, outPre); } + /** + * Verifies URL parser returning path with only schema information for all + * kind of URL. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void validateUrlParser() throws SvcLogicException { + String actVal = "identity-test:test"; + String url1 = "http://echo.getpostman.com/restconf/operations/" + + actVal; + String url2 = "http://echo.getpostman.com/restconf/data/" + actVal; + String url3 = "https://echo.getpostman.com/restconf/operations/" + + actVal; + String url4 = "https://echo.getpostman.com/restconf/data/" + actVal + + "/for-put"; + String url5 = "http://localhost:8282/restconf/operations/" + actVal; + String url6 = "https://localhost:8282/restconf/operations/" + actVal; + String url7 = "http://localhost:8282/restconf/data/" + actVal + + "/for-put"; + String url8 = "https://localhost:8282/restconf/data/" + actVal; + String url9 = "http://182.2.61.24:2250/restconf/data/" + actVal; + String url10 = "https://182.2.61.24:2250/restconf/operations/" + actVal; + String val1 = parseUrl(url1, POST); + String val2 = parseUrl(url2, GET); + String val3 = parseUrl(url3, POST); + String val4 = parseUrl(url4, PUT); + String val5 = parseUrl(url5, GET); + String val6 = parseUrl(url6, POST); + String val7 = parseUrl(url7, PUT); + String val8 = parseUrl(url8, POST); + String val9 = parseUrl(url9, GET); + String val10 = parseUrl(url10, POST); + assertThat(val1, is(actVal)); + assertThat(val2, is(actVal)); + assertThat(val3, is(actVal)); + assertThat(val4, is(actVal)); + assertThat(val5, is(actVal)); + assertThat(val6, is(actVal)); + assertThat(val7, is(actVal)); + assertThat(val8, is(actVal)); + assertThat(val9, is(actVal)); + assertThat(val10, is(actVal)); + } + /** * Creates attribute list for encoding JSON or XML with identity-ref YANG * file. -- cgit 1.2.3-korg From c8e5aa9f493ec5f80e4444285816ceed27b1752b Mon Sep 17 00:00:00 2001 From: janani b Date: Thu, 27 Sep 2018 15:26:39 +0530 Subject: Issue fixes for Restconf discovery node i) Adding constructor param ii) execute dg param order fix iii) Constants value fix. Issue-ID: CCSDK-325 Change-Id: I5aaf4b66b3982e4a1d3736362be3a5e8d7ab6d00 Signed-off-by: janani b --- .../plugins/restconfdiscovery/EventProcessor.java | 9 ++++--- .../restconfdiscovery/RestconfDiscoveryNode.java | 18 ++++++++----- .../restconfdiscovery/SvcLogicGraphInfo.java | 2 +- .../blueprint/restconf-client-blueprint.xml | 4 ++- .../TestRestconfDiscoveryNode.java | 31 ++++++++++++++-------- 5 files changed, 41 insertions(+), 23 deletions(-) (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/EventProcessor.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/EventProcessor.java index aae808155..e46e47a9b 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/EventProcessor.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/EventProcessor.java @@ -28,17 +28,18 @@ import org.slf4j.LoggerFactory; import java.util.Map; import static org.onap.ccsdk.sli.plugins.prop.JsonParser.convertToProperties; +import static org.slf4j.LoggerFactory.getLogger; /** * Processes the events from event queue and executes callback DG. */ class EventProcessor implements Runnable { - private static final Logger log = LoggerFactory.getLogger(EventProcessor.class); + + private static final Logger log = getLogger(EventProcessor.class); private RestconfDiscoveryNode node; - private static final String EVENT_SUBSCRIPTION_ID = "ietf-notification:notification" + - ".ietf-yang-push:push-change-update" + - ".subscription-id"; + private static final String EVENT_SUBSCRIPTION_ID = "ietf-restconf:" + + "notification.ietf-yang-push:push-change-update.subscription-id"; public EventProcessor(RestconfDiscoveryNode node) { this.node = node; diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java index f5366a544..111d62847 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java @@ -26,7 +26,6 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; @@ -37,12 +36,15 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; +import static org.slf4j.LoggerFactory.getLogger; + /** * Representation of a plugin to subscribe for notification and then * to handle the received notifications. */ public class RestconfDiscoveryNode implements SvcLogicDiscoveryPlugin { - private static final Logger log = LoggerFactory.getLogger(RestconfDiscoveryNode.class); + + private static final Logger log = getLogger(RestconfDiscoveryNode.class); private ExecutorService executor = Executors.newCachedThreadPool(); private Map runnableInfo = new ConcurrentHashMap<>(); @@ -54,15 +56,19 @@ public class RestconfDiscoveryNode implements SvcLogicDiscoveryPlugin { private static final String SUBSCRIBER_ID = "subscriberId"; private static final String RESPONSE_CODE = "response-code"; private static final String RESPONSE_PREFIX = "responsePrefix"; - private static final String OUTPUT_IDENTIFIER = "ietf-subscribed-notifications:output.identifier"; + private static final String OUTPUT_IDENTIFIER = "ietf-subscribed-notif" + + "ications:establish-subscription.output.identifier"; private static final String RESPONSE_CODE_200 = "200"; private static final String SSE_URL = "sseConnectURL"; /** - * Creates an instance of RestconfDiscoveryNode and - * starts processing of event. + * Creates an instance of RestconfDiscoveryNode and starts processing of + * event. + * + * @param r restconf api call node */ - public RestconfDiscoveryNode() { + public RestconfDiscoveryNode(RestconfApiCallNode r) { + this.restconfApiCallNode = r; ExecutorService e = Executors.newFixedThreadPool(20); EventProcessor p = new EventProcessor(this); for (int i = 0; i < 20; ++i) { diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SvcLogicGraphInfo.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SvcLogicGraphInfo.java index 725826bda..1cc6261ab 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SvcLogicGraphInfo.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SvcLogicGraphInfo.java @@ -142,7 +142,7 @@ class SvcLogicGraphInfo { SvcLogicStore store = service.getStore(); if (store != null) { - SvcLogicGraph subGraph = store.fetch(module, rpc, version, mode); + SvcLogicGraph subGraph = store.fetch(module, rpc, mode, version); if (subGraph != null) { ctx.setAttribute("subGraph", subGraph.toString()); service.execute(subGraph, ctx); diff --git a/restconf-client/provider/src/main/resources/org/opendaylight/blueprint/restconf-client-blueprint.xml b/restconf-client/provider/src/main/resources/org/opendaylight/blueprint/restconf-client-blueprint.xml index c8f2dc4c9..45c335b13 100755 --- a/restconf-client/provider/src/main/resources/org/opendaylight/blueprint/restconf-client-blueprint.xml +++ b/restconf-client/provider/src/main/resources/org/opendaylight/blueprint/restconf-client-blueprint.xml @@ -32,7 +32,9 @@ - + + + diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java index db878f0af..b2094ca73 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java @@ -20,16 +20,19 @@ package org.onap.ccsdk.sli.plugins.restconfdiscovery; -import java.net.URI; -import java.util.HashMap; -import java.util.Map; - import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; import org.glassfish.jersey.media.sse.SseFeature; import org.glassfish.jersey.server.ResourceConfig; import org.junit.Test; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode; +import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; @@ -47,13 +50,15 @@ public class TestRestconfDiscoveryNode { ctx.setAttribute("prop.encoding-json", "encoding-json"); ctx.setAttribute("restapi-result.response-code", "200"); ctx.setAttribute("restapi-result.ietf-subscribed-notifications" + - ":output.identifier", "100"); + ":establish-subscription.output.identifier", + "100"); Map p = new HashMap<>(); p.put("sseConnectURL", "http://localhost:8080/events"); p.put("subscriberId", "networkId"); p.put("responsePrefix", "restapi-result"); - RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode( + new RestconfApiCallNode(new RestapiCallNode())); rdn.establishPersistentConnection(p, ctx, "networkId"); Thread.sleep(2000); rdn.deleteSubscription(p, ctx); @@ -75,7 +80,8 @@ public class TestRestconfDiscoveryNode { throws SvcLogicException{ SvcLogicContext ctx = new SvcLogicContext(); Map p = new HashMap<>(); - RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode( + new RestconfApiCallNode(new RestapiCallNode())); rdn.establishSubscription(p, ctx); } @@ -84,7 +90,8 @@ public class TestRestconfDiscoveryNode { SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute("restapi-result.response-code", "200"); ctx.setAttribute("response-code", "404"); - RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode( + new RestconfApiCallNode(new RestapiCallNode())); assertThat(rdn.getResponseCode("restapi-result", ctx), is("200")); assertThat(rdn.getResponseCode(null, ctx), @@ -95,10 +102,12 @@ public class TestRestconfDiscoveryNode { public void testOutputIdentifier() { SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute("restapi-result.ietf-subscribed-notifications:" + - "output.identifier", "89"); - ctx.setAttribute("ietf-subscribed-notifications:output.identifier", + "establish-subscription.output.identifier", "89"); - RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(); + ctx.setAttribute("ietf-subscribed-notifications:establish-subscripti" + + "on.output.identifier", "89"); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode( + new RestconfApiCallNode(new RestapiCallNode())); assertThat(rdn.getOutputIdentifier("restapi-result", ctx), is("89")); } -- cgit 1.2.3-korg From 769622040a1fa819d2708c50d4904ac24b867a38 Mon Sep 17 00:00:00 2001 From: janani b Date: Mon, 8 Oct 2018 14:17:51 +0530 Subject: Resolving root level augment nodes Issue fix for root level augment nodes being added to the data format Issue-ID: CCSDK-324 Change-Id: I1c69f97739c6cb56c74b5f22bc4283f2f927719b Signed-off-by: janani b --- .../pnserializer/DefaultPropertiesNodeWalker.java | 27 +++++++ .../dfserializer/DataFormatSerializerTest.java | 20 +++++ .../dfserializer/DataFormatUtilsTest.java | 92 ++++++++++++++-------- 3 files changed, 104 insertions(+), 35 deletions(-) (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java index 8497805c2..f23452653 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java @@ -22,6 +22,7 @@ package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer; import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import java.util.Collection; import java.util.Map; /** @@ -58,6 +59,32 @@ public class DefaultPropertiesNodeWalker implements Propert listener.exitPropertiesNode(node); } } + if (propertiesNode instanceof RootNode) { + processAugments(propertiesNode, listener); + } + } + + /** + * Processes the augments present in the root node. + * + * @param node root node + * @param listener properties node listener + * @throws SvcLogicException when augment node walking fails + */ + private void processAugments(PropertiesNode node, + PropertiesNodeListener listener) + throws SvcLogicException { + for (Map.Entry> + augToChild : node.augmentations().asMap().entrySet()) { + Collection child = augToChild.getValue(); + if (!child.isEmpty()) { + for (PropertiesNode p : child) { + listener.enterPropertiesNode(p); + walkChildNode(listener, p); + listener.exitPropertiesNode(p); + } + } + } } /** diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java index 6c11206c9..40f4c4b1b 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java @@ -50,6 +50,7 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG_AUG_POST; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG; @@ -217,6 +218,25 @@ public class DataFormatSerializerTest { assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG)); } + /** + * Verifies encoding of parameters to JSON data format with augment as + * root child. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonWithAugAsRootChild() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_AUG_POST)); + } + /** * Verifies decoding of parameters from JSON data format with containers, * grouping and augment. diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java index c7efdeb37..7e6445fcd 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java @@ -100,6 +100,46 @@ public final class DataFormatUtilsTest { "yangid:physical\n" + "\n"; + static final String ENCODE_TO_JSON_YANG_COMMON = "\n " + + "\"test-augment:cont13\": {\n" + + " \"ll9\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"list9\": [\n" + + " {\n" + + " \"leaf27\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"leaf27\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"leaf28\": \"abc\",\n" + + " \"cont12\": {\n" + + " \"leaf26\": \"abc\"\n" + + " }\n" + + " },\n" + + " \"test-augment:list7\": [\n" + + " {\n" + + " \"leaf14\": \"test\"\n" + + " },\n" + + " {\n" + + " \"leaf14\": \"create\"\n" + + " }\n" + + " ],\n" + + " \"test-augment:leaf15\": \"abc\",\n" + + " \"test-augment:ll6\": [\n" + + " \"unbounded\",\n" + + " \"8\"\n" + + " ],\n" + + " \"test-augment:cont5\": {\n" + + " \"leaf13\": \"true\"\n" + + " }"; + + static final String ENCODE_TO_JSON_YANG_AUG_POST = "{\n" + + " \"test-yang:leaf10\": \"abc\"," + + ENCODE_TO_JSON_YANG_COMMON + "\n}"; + static final String ENCODE_TO_JSON_YANG = "{\n" + " \"test-yang:cont2\": {\n" + " \"list1\": [\n" + @@ -177,41 +217,8 @@ public final class DataFormatUtilsTest { " \"abc\"\n" + " ],\n" + " \"cont4\": {\n" + - " \"leaf10\": \"abc\",\n" + - " \"test-augment:cont13\": {\n" + - " \"ll9\": [\n" + - " \"abc\",\n" + - " \"abc\"\n" + - " ],\n" + - " \"list9\": [\n" + - " {\n" + - " \"leaf27\": \"abc\"\n" + - " },\n" + - " {\n" + - " \"leaf27\": \"abc\"\n" + - " }\n" + - " ],\n" + - " \"leaf28\": \"abc\",\n" + - " \"cont12\": {\n" + - " \"leaf26\": \"abc\"\n" + - " }\n" + - " },\n" + - " \"test-augment:list7\": [\n" + - " {\n" + - " \"leaf14\": \"test\"\n" + - " },\n" + - " {\n" + - " \"leaf14\": \"create\"\n" + - " }\n" + - " ],\n" + - " \"test-augment:leaf15\": \"abc\",\n" + - " \"test-augment:ll6\": [\n" + - " \"unbounded\",\n" + - " \"8\"\n" + - " ],\n" + - " \"test-augment:cont5\": {\n" + - " \"leaf13\": \"true\"\n" + - " }\n" + + " \"leaf10\": \"abc\"," + + addSpace(ENCODE_TO_JSON_YANG_COMMON, 8) + "\n" + " },\n" + " \"ll4\": [\n" + " \"abc\",\n" + @@ -501,4 +508,19 @@ public final class DataFormatUtilsTest { " abc\n" + " \n" + ""; + + /** + * Adds the specified number of space required for a req in each line. + * + * @param req request message + * @param i number of space + * @return space appended string + */ + private static String addSpace(String req, int i) { + StringBuilder space = new StringBuilder(""); + for (int sp = 0; sp < i; sp++) { + space = space.append(" "); + } + return req.replaceAll("\n", "\n" + space.toString()); + } } -- cgit 1.2.3-korg From da54fc49d3f16d7028a42cd90ad1500973814790 Mon Sep 17 00:00:00 2001 From: janani b Date: Mon, 8 Oct 2018 16:13:36 +0530 Subject: Appending dummy root node to XML data format To hold all the children in XML message a dummy root node is added Issue-ID: CCSDK-324 Change-Id: I7cf0266d15e6bfaa2b9207e77d8bd5372a164b4f Signed-off-by: janani b --- .../dfserializer/DfSerializerUtil.java | 7 +- .../dfserializer/PropertiesNodeXmlListener.java | 12 ++-- .../dfserializer/XmlSerializer.java | 18 ++++- .../dfserializer/DataFormatSerializerTest.java | 20 ++++++ .../dfserializer/DataFormatUtilsTest.java | 78 ++++++++++++---------- 5 files changed, 87 insertions(+), 48 deletions(-) (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java index 598b08c1b..fbebd2b2d 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java @@ -210,11 +210,10 @@ public final class DfSerializerUtil { * @return base type definition */ static TypeDefinition resolveBaseTypeFrom(TypeDefinition type) { - TypeDefinition superType; - for(superType = type; superType.getBaseType() != null; - superType = superType.getBaseType()) { + TypeDefinition superType = type; + while (superType.getBaseType() != null) { + superType = superType.getBaseType(); } return superType; } - } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeXmlListener.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeXmlListener.java index 39a08e387..f098195e4 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeXmlListener.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeXmlListener.java @@ -78,11 +78,13 @@ public class PropertiesNodeXmlListener implements PropertiesNodeListener { @Override public void start(PropertiesNode node) { - //Do Nothing. + rootElement = addElement(null, node); + elementStack.push(rootElement); } @Override public void end(PropertiesNode node) throws SvcLogicException { + xmlData = rootElement.asXML(); xmlData = UTF_HEADER + xmlData; writer = getXmlWriter(xmlData, "4"); } @@ -113,9 +115,6 @@ public class PropertiesNodeXmlListener implements PropertiesNodeListener { NODE_TYPE_ERR, node.nodeType().toString())); } if (element != null) { - if (elementStack.isEmpty()) { - rootElement = element; - } elementStack.push(element); } } @@ -133,10 +132,7 @@ public class PropertiesNodeXmlListener implements PropertiesNodeListener { case MULTI_INSTANCE_NODE: case MULTI_INSTANCE_LEAF_NODE: case SINGLE_INSTANCE_LEAF_NODE: - if (!elementStack.isEmpty() && - elementStack.peek().equals(rootElement)) { - xmlData = rootElement.asXML(); - } else { + if (!elementStack.isEmpty()) { elementStack.pop(); } break; diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/XmlSerializer.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/XmlSerializer.java index 6eeb4b82c..f3c6723e1 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/XmlSerializer.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/XmlSerializer.java @@ -61,7 +61,7 @@ public class XmlSerializer extends DataFormatSerializer { PropertiesNodeXmlListener xmlListener = new PropertiesNodeXmlListener(); nodeWalker.walk(xmlListener, propNode); Writer writer = xmlListener.getWriter(); - return writer.toString(); + return removeRootNode(writer.toString(), propNode.name()); } @Override @@ -85,4 +85,20 @@ public class XmlSerializer extends DataFormatSerializer { return serializerContext().getPropNodeSerializer().decode( listener.serializerHelper().getPropertiesNode()); } + + /** + * Removes root node from the XML data format message and makes the + * string to be pretty print. + * + * @param xml XML data format message + * @param rootName root node name + * @return pretty print format XML message + */ + private static String removeRootNode(String xml, String rootName) { + xml = xml.replace("\n<" + rootName + ">", "\n"); + xml = xml.replace("" + "\n", ""); + xml = xml.replaceAll("\n" + " ", "\n"); + xml = xml.replaceFirst("\n", ""); + return xml; + } } \ No newline at end of file diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java index 40f4c4b1b..1185eea1c 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java @@ -54,6 +54,7 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG_AUG_POST; /** @@ -276,6 +277,25 @@ public class DataFormatSerializerTest { assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG)); } + /** + * Verifies encoding of parameters to XML data format with augment as + * root child. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlWithAugAsRootChild() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_AUG_POST)); + } + /** * Verifies decoding of parameters from XML data format with containers, * grouping and augment. diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java index 7e6445fcd..a0a154bf3 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java @@ -249,6 +249,47 @@ public final class DataFormatUtilsTest { " }\n" + "}"; + static final String ENCODE_TO_XML_YANG_COMMON = "\n" + + "\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + "\n" + + "\n" + + " test\n" + + "\n" + + "\n" + + " create\n" + + "\n" + + "abc\n" + + "unbounded\n" + + "8\n" + + "\n" + + " true\n" + + ""; + + static final String ENCODE_TO_XML_YANG_AUG_POST = "\n" + + "abc" + + "" + + ENCODE_TO_XML_YANG_COMMON + "\n"; + static final String ENCODE_TO_XML_YANG = "\n" + "\n" + @@ -305,40 +346,8 @@ public final class DataFormatUtilsTest { " abc\n" + " abc\n" + " \n" + - " abc\n" + - " \n" + - " abc\n" + - " abc\n" + - " \n" + - " abc\n" + - " \n" + - " \n" + - " abc\n" + - " \n" + - " abc\n" + - " \n" + - " abc\n" + - " \n" + - " \n" + - " \n" + - " test\n" + - " \n" + - " \n" + - " create\n" + - " \n" + - " abc\n" + - " unbounded\n" + - " 8\n" + - " \n" + - " true\n" + - " \n" + + " abc"+ + addSpace(ENCODE_TO_XML_YANG_COMMON, 8) + "\n" + " \n" + " abc\n" + " abc\n" + @@ -362,7 +371,6 @@ public final class DataFormatUtilsTest { " \n" + "\n"; - static final String ENCODE_TO_JSON_RPC = "{\n" + " \"test-yang:input\": {\n" + " \"leaf30\": \"abc\",\n" + -- cgit 1.2.3-korg From bc2c9899ae7d71d0e4a970c146a5121822913ff9 Mon Sep 17 00:00:00 2001 From: janani b Date: Mon, 8 Oct 2018 19:11:02 +0530 Subject: PUT and PATCH operation support Changes to add a additional root node to the data format req to support PUT and PATCH operations in RestconfApiCallNode Issue-ID: CCSDK-614 Change-Id: If094810861f8152a2a6d6ee86e9f81c8812b8ad6 Signed-off-by: janani b --- .../restconfapicall/RestconfApiCallNode.java | 149 +++++++++++++++++- .../plugins/restconfapicall/RestconfApiUtils.java | 41 +++-- .../dfserializer/DfSerializerUtil.java | 38 +++-- .../pnserializer/MdsalPropertiesNodeUtils.java | 10 +- .../dfserializer/DataFormatSerializerTest.java | 173 ++++++++++++++++++++- .../dfserializer/DataFormatUtilsTest.java | 53 +++++-- 6 files changed, 415 insertions(+), 49 deletions(-) (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java index 91ce3338a..a76b27d0b 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java @@ -20,6 +20,14 @@ package org.onap.ccsdk.sli.plugins.restconfapicall; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.stream.JsonWriter; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; @@ -35,30 +43,39 @@ import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.MdsalSerializerHe import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.SerializerHelper; import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.YangParameters; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeSerializer; +import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeSerializer; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.ws.rs.core.UriBuilder; +import java.io.StringWriter; +import java.io.Writer; import java.net.SocketException; import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; +import static com.google.common.base.Strings.repeat; import static java.lang.String.format; +import static java.lang.String.valueOf; import static org.apache.commons.lang3.StringUtils.join; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.DELETE; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.GET; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PATCH; -import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.POST; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.parseParam; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.ATTEMPTS_MSG; +import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COLON; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COMMA; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COMM_FAIL; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.HEADER; @@ -74,10 +91,16 @@ import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RES_PR import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RETRY_COUNT; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RETRY_FAIL; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.UPDATED_URL; +import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getUpdatedXmlReq; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getSchemaCtxFromDir; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getYangParameters; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfListenerFactory.instance; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.FORMAT_ERR; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.UTF_HEADER; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_TREE_ERR; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.getXmlWriter; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getModuleNamespace; import static org.osgi.framework.FrameworkUtil.getBundle; /** @@ -154,9 +177,11 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { InstanceIdentifierContext insIdCtx = getInsIdCtx(p, uri); String req = null; - if (p.httpMethod == POST || p.httpMethod == PUT - || p.httpMethod == PATCH) { + if (p.httpMethod != GET && p.httpMethod != DELETE) { req = serializeRequest(props, p, uri, insIdCtx); + if (p.httpMethod == PUT || p.httpMethod == PATCH) { + updateReq(req, p, insIdCtx); + } } if (req == null && p.requestBody != null) { req = p.requestBody; @@ -210,8 +235,8 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { } if (r != null && r.code >= 300) { - throw new SvcLogicException( - String.valueOf(r.code) + ": " + r.message); + throw new SvcLogicException(valueOf(r.code) + + COLON + " " + r.message); } } @@ -316,7 +341,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { */ public String getResponse(SvcLogicContext ctx, YangParameters params, String pre, HttpResponse res) { - ctx.setAttribute(pre + RES_CODE, String.valueOf(res.code)); + ctx.setAttribute(pre + RES_CODE, valueOf(res.code)); ctx.setAttribute(pre + RES_MSG, res.message); if (params.dumpHeaders && res.headers != null) { @@ -345,7 +370,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { HttpResponse res = new HttpResponse(); res.code = 500; res.message = errMsg; - ctx.setAttribute(prefix + RES_CODE, String.valueOf(res.code)); + ctx.setAttribute(prefix + RES_CODE, valueOf(res.code)); ctx.setAttribute(prefix + RES_MSG, res.message); } @@ -373,4 +398,114 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { log.debug(UPDATED_URL + retryUri.toString()); log.debug(format(COMM_FAIL, hostName, retryString)); } + + /** + * Updates request message for JSON and XML data format, when the HTTP + * method points it as PUT or PATCH. + * + * @param req current request message + * @param p YANG parameters + * @param insIdCtx instance identifier context + * @return update request message + * @throws SvcLogicException when the data format type is wrong + */ + public String updateReq(String req, YangParameters p, + InstanceIdentifierContext insIdCtx) + throws SvcLogicException { + + SchemaNode schemaNode = insIdCtx.getSchemaNode(); + Namespace modNs = getModuleNamespace(schemaNode.getQName(), + insIdCtx.getSchemaContext()); + String nodeName = schemaNode.getQName().getLocalName(); + + switch (p.format) { + case JSON: + return getUpdatedJsonReq(req, nodeName, modNs.moduleName()); + + case XML: + return getXmlReqForPutOp(req, nodeName, modNs.moduleNs()); + + default: + throw new SvcLogicException(format(FORMAT_ERR, p.format)); + } + } + + /** + * Returns the updated JSON request message, when the HTTP method + * points to PUT or PATCH. + * + * @param req current JSON request message + * @param nodeName root node name + * @param modName module name of the root node + * @return update JSON request message + */ + private String getUpdatedJsonReq(String req, String nodeName, + String modName) { + Writer writer = new StringWriter(); + JsonWriter jsonWriter = new JsonWriter(writer); + jsonWriter.setIndent(repeat(" ", 4)); + + JsonParser jsonParser = new JsonParser(); + JsonObject oldJson = (JsonObject)jsonParser.parse(req); + oldJson = remChildModName(oldJson, modName); + JsonObject newJson = new JsonObject(); + newJson.add(modName + COLON + nodeName, oldJson.deepCopy()); + + Gson gson= new Gson(); + gson.toJson(newJson, jsonWriter); + return writer.toString(); + } + + /** + * Removes module name from all the updated first level child node, if it + * is same as the root node added. + * + * @param oldJson JSON object for old request + * @param modName module name of root node + * @return JSON object for old request with updated child module name + */ + private JsonObject remChildModName(JsonObject oldJson, String modName) { + Iterator> it = oldJson.entrySet().iterator(); + Map m = new HashMap<>(); + while (it.hasNext()) { + Map.Entry jNode = it.next(); + if (jNode.getKey().contains(COLON)) { + String[] modArr = jNode.getKey().split(COLON); + if (modArr[0].equals(modName)) { + it.remove(); + m.put(modArr[1], jNode.getValue()); + } + } + } + if (!m.isEmpty()) { + for (Map.Entry element : m.entrySet()) { + oldJson.add(element.getKey(), element.getValue()); + } + } + return oldJson; + } + + /** + * Returns the updated XML request message, when the HTTP method points + * to PUT or PATCH. + * + * @param req current JSON request message + * @param nodeName root node name + * @param modNs module namespace of the root node + * @return update JSON request message + * @throws SvcLogicException when XML parsing fails + */ + private String getXmlReqForPutOp(String req, String nodeName, + URI modNs) throws SvcLogicException { + req = getUpdatedXmlReq(req, nodeName, modNs.toString()); + Document oldDoc; + try { + oldDoc = DocumentHelper.parseText(req); + } catch (DocumentException e) { + throw new SvcLogicException(XML_TREE_ERR, e); + } + Writer writer = getXmlWriter( + UTF_HEADER + oldDoc.getRootElement().asXML(), "4"); + return writer.toString(); + } } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java index b51272f7e..3a0b5fb35 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java @@ -41,7 +41,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.getParameters; import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.parseParam; import static org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode.DEFAULT_MODE; @@ -66,6 +65,8 @@ public final class RestconfApiUtils { static final String COMMA = ","; + static final String COLON = ":"; + static final String HTTP_RES = "httpResponse"; static final String REST_API_URL = "restapiUrl"; @@ -97,9 +98,6 @@ public final class RestconfApiUtils { private static final String URL_SYNTAX = "The following URL cannot be " + "parsed into URI : "; - private static final String PUT_NODE_ERR = "The following URL does not " + - "contain minimum two nodes for PUT operation."; - private static final String YANG = ".yang"; private static final String YANG_FILE_ERR = "Unable to parse the YANG " + @@ -145,12 +143,6 @@ public final class RestconfApiUtils { String path = uri.getPath(); path = getParsedPath(path); - if (method == PUT) { - if (!path.contains(SLASH)) { - throw new SvcLogicException(PUT_NODE_ERR + url); - } - path = path.substring(0, path.lastIndexOf(SLASH)); - } return path; } @@ -162,15 +154,15 @@ public final class RestconfApiUtils { */ private static String getParsedPath(String path) { String firstHalf; - if (path.contains(":")) { - String[] p = path.split(":"); + if (path.contains(COLON)) { + String[] p = path.split(COLON); if (p[0].contains(SLASH)) { int slash = p[0].lastIndexOf(SLASH); firstHalf = p[0].substring(slash + 1); } else { firstHalf = p[0]; } - return firstHalf + ":" + p[1]; + return firstHalf + COLON + p[1]; } return path; } @@ -222,6 +214,13 @@ public final class RestconfApiUtils { } } + /** + * Processes all the obtained files by isolating all the YANG files from + * all the directory of the given path recursively. + * + * @param files files in the given path + * @param yangFiles YANG files list + */ private static void processFiles(File[] files, List yangFiles) { for (File file : files) { if (file.isFile() && file.getName().endsWith(YANG)) { @@ -231,4 +230,20 @@ public final class RestconfApiUtils { } } } + + /** + * Returns the updated XML request message by adding root node to it. + * + * @param req XML request + * @param nodeName root node name + * @param modNs module namespace of the root node + * @return updated XML request message + */ + static String getUpdatedXmlReq(String req, String nodeName, String modNs) { + String rootNode = "\n<" + nodeName + " xmlns=\"" + modNs.toString() + + "\">\n"; + req = req.replaceFirst("\n", rootNode); + req = req + ""; + return req.replaceAll(">\\s+<", "><"); + } } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java index fbebd2b2d..707c29444 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java @@ -68,24 +68,12 @@ public final class DfSerializerUtil { static final String XML_LIS_ERR = "The XML serializer doesn't have XML " + "listener"; - static final String JSON_TREE_ERR = "Unable to form JSON tree object from" + - " the JSON body provided."; - - static final String XML_TREE_ERR = "Unable to form XML tree object from " + - "the XML body provided."; - - static final String FORMAT_ERR = "Only JSON and XML formats are supported" + - ". %s is not supported"; - static final String PROP_NODE_ERR = "The property node doesn't have " + "schema node bound to it."; static final String DF_ERR = "Type mismatch for the node %s. The schema " + "node does not match with the data format node type %s."; - static final String UTF_HEADER = ""; - static final String XML_PREFIX = "yangid"; private static final String YES = "yes"; @@ -98,6 +86,30 @@ public final class DfSerializerUtil { private static final String URI_ERR = "Unable to parse the URI"; + /** + * Data format error message for unsupported types. + */ + public static final String FORMAT_ERR = "Only JSON and XML formats are " + + "supported. %s is not supported"; + + /** + * UTF header message for XML data format message. + */ + public static final String UTF_HEADER = ""; + + /** + * Error message when a JSON tree creation fails. + */ + public static final String JSON_TREE_ERR = "Unable to form JSON tree " + + "object from the JSON body provided."; + + /** + * Error message when a XML tree creation fails. + */ + public static final String XML_TREE_ERR = "Unable to form XML tree object" + + " from the XML body provided."; + //No instantiation. private DfSerializerUtil() { } @@ -110,7 +122,7 @@ public final class DfSerializerUtil { * @return writer with XML * @throws SvcLogicException when transformation of source fails */ - static Writer getXmlWriter(String input, String indent) + public static Writer getXmlWriter(String input, String indent) throws SvcLogicException { try { Transformer transformer = TransformerFactory.newInstance() diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java index d7b4818b4..ccc4f2bdd 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java @@ -340,7 +340,15 @@ public final class MdsalPropertiesNodeUtils { return null; } - private static Namespace getModuleNamespace(QName qName, SchemaContext ctx) + /** + * Returns module namespace from a given qName. + * + * @param qName qName of a node + * @param ctx schema context + * @return module namespace of the node + * @throws SvcLogicException when the module is not available + */ + public static Namespace getModuleNamespace(QName qName, SchemaContext ctx) throws SvcLogicException { Optional module = ctx.findModule(qName.getModule()); if (!module.isPresent()) { diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java index 1185eea1c..08fa33ee7 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java @@ -42,19 +42,24 @@ import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.GET; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PATCH; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.POST; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_JSON_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_XML_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID_PUT; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG_AUG_POST; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG_PUT; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID_PUT; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG_AUG_POST; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG_PUT; /** @@ -102,6 +107,9 @@ public class DataFormatSerializerTest { doAnswer(dfCaptor).when(restconf).serializeRequest( any(Map.class), any(YangParameters.class), any(String.class), any(InstanceIdentifierContext.class)); + doAnswer(dfCaptor).when(restconf).updateReq( + any(String.class), any(YangParameters.class), + any(InstanceIdentifierContext.class)); } /** @@ -140,6 +148,46 @@ public class DataFormatSerializerTest { assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID)); } + /** + * Verifies encoding of parameters to JSON data format with identity-ref + * and inter-file linking for put operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonIdWithPut() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + ctx.setAttribute(pre + "l", "abc"); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID_PUT)); + } + + /** + * Verifies encoding of parameters to JSON data format with identity-ref + * and inter-file linking for patch operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonIdWithPatch() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + ctx.setAttribute(pre + "l", "abc"); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "patch"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID_PUT)); + } + /** * Verifies encoding of parameters to XML data format with identity-ref * and inter-file linking. @@ -159,6 +207,44 @@ public class DataFormatSerializerTest { assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID)); } + /** + * Verifies encoding of parameters to XML data format with identity-ref + * and inter-file linking for put operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlIdWithPut() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID_PUT)); + } + + /** + * Verifies encoding of parameters to XML data format with identity-ref + * and inter-file linking for patch operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlIdWithPatch() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "patch"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID_PUT)); + } + /** * Verifies decoding of parameters from JSON data format with identity-ref * and inter-file linking. @@ -219,6 +305,44 @@ public class DataFormatSerializerTest { assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG)); } + /** + * Verifies encoding of parameters to JSON data format with containers, + * grouping and augment for put operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYangWithPut() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_PUT)); + } + + /** + * Verifies encoding of parameters to JSON data format with containers, + * grouping and augment for patch operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYangWithPatch() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "patch"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_PUT)); + } + /** * Verifies encoding of parameters to JSON data format with augment as * root child. @@ -277,6 +401,44 @@ public class DataFormatSerializerTest { assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG)); } + /** + * Verifies encoding of parameters to XML data format with containers, + * grouping and augment for put operation-type + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlYangWithPut() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_PUT)); + } + + /** + * Verifies encoding of parameters to XML data format with containers, + * grouping and augment for patch operation-type + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlYangWithPatch() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_PUT)); + } + /** * Verifies encoding of parameters to XML data format with augment as * root child. @@ -369,23 +531,24 @@ public class DataFormatSerializerTest { @Test public void validateUrlParser() throws SvcLogicException { String actVal = "identity-test:test"; + String putId = "/for-put"; String url1 = "http://echo.getpostman.com/restconf/operations/" + actVal; String url2 = "http://echo.getpostman.com/restconf/data/" + actVal; String url3 = "https://echo.getpostman.com/restconf/operations/" + actVal; String url4 = "https://echo.getpostman.com/restconf/data/" + actVal + - "/for-put"; + putId; String url5 = "http://localhost:8282/restconf/operations/" + actVal; String url6 = "https://localhost:8282/restconf/operations/" + actVal; String url7 = "http://localhost:8282/restconf/data/" + actVal + - "/for-put"; + putId; String url8 = "https://localhost:8282/restconf/data/" + actVal; String url9 = "http://182.2.61.24:2250/restconf/data/" + actVal; String url10 = "https://182.2.61.24:2250/restconf/operations/" + actVal; String val1 = parseUrl(url1, POST); String val2 = parseUrl(url2, GET); - String val3 = parseUrl(url3, POST); + String val3 = parseUrl(url3, PATCH); String val4 = parseUrl(url4, PUT); String val5 = parseUrl(url5, GET); String val6 = parseUrl(url6, POST); @@ -396,10 +559,10 @@ public class DataFormatSerializerTest { assertThat(val1, is(actVal)); assertThat(val2, is(actVal)); assertThat(val3, is(actVal)); - assertThat(val4, is(actVal)); + assertThat(val4, is(actVal + putId)); assertThat(val5, is(actVal)); assertThat(val6, is(actVal)); - assertThat(val7, is(actVal)); + assertThat(val7, is(actVal + putId)); assertThat(val8, is(actVal)); assertThat(val9, is(actVal)); assertThat(val10, is(actVal)); diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java index a0a154bf3..027604480 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java @@ -26,9 +26,9 @@ package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer; */ public final class DataFormatUtilsTest { - static final String ENCODE_TO_JSON_ID = "{\n" + - " \"identity-test:con1\": {\n" + - " \"interfaces\": {\n" + + static final String ENCODE_TO_JSON_ID_COMMON = "\n \"interfaces\"" + + ": " + + "{\n" + " \"int-list\": [\n" + " {\n" + " \"iden\": \"optical\",\n" + @@ -61,14 +61,23 @@ public final class DataFormatUtilsTest { " ]\n" + " },\n" + " \"interface\": \"identity-types:physical\"\n" + - " },\n" + + " }"; + + static final String ENCODE_TO_JSON_ID = "{\n" + + " \"identity-test:con1\": {" + ENCODE_TO_JSON_ID_COMMON + + ",\n" + " \"identity-test:l\": \"abc\"\n" + "}"; - static final String ENCODE_TO_XML_ID = "\n" + - "\n" + - " \n" + + static final String ENCODE_TO_JSON_ID_PUT = "{\n" + + " \"identity-test:test\": {\n" + + " \"con1\": {" + addSpace(ENCODE_TO_JSON_ID_COMMON, 4) + + ",\n" + + " \"l\": \"abc\"\n" + + " }\n" + + "}"; + + static final String ENCODE_TO_XML_ID_COMMON = "\n \n" + " \n" + " optical\n" + " \n" + @@ -97,8 +106,19 @@ public final class DataFormatUtilsTest { " \n" + " \n" + " " + - "yangid:physical\n" + - "\n"; + "yangid:physical"; + + static final String ENCODE_TO_XML_ID = "\n" + + "" + + ENCODE_TO_XML_ID_COMMON + "\n\n"; + + static final String ENCODE_TO_XML_ID_PUT = "\n" + + "\n" + + " " + addSpace(ENCODE_TO_XML_ID_COMMON, 4) + + "\n \n" + + "\n"; static final String ENCODE_TO_JSON_YANG_COMMON = "\n " + "\"test-augment:cont13\": {\n" + @@ -249,6 +269,13 @@ public final class DataFormatUtilsTest { " }\n" + "}"; + static final String ENCODE_TO_JSON_YANG_PUT = "{\n" + + " \"test-yang:cont4\": {" + addSpace( + ENCODE_TO_JSON_YANG_COMMON, 4) + ",\n" + + " \"leaf10\": \"abc\"\n" + + " }\n" + + "}"; + static final String ENCODE_TO_XML_YANG_COMMON = "\n" + "\n" + @@ -290,6 +317,12 @@ public final class DataFormatUtilsTest { "" + ENCODE_TO_XML_YANG_COMMON + "\n"; + static final String ENCODE_TO_XML_YANG_PUT = "\n" + + "\n" + + " abc" + + addSpace(ENCODE_TO_XML_YANG_COMMON, 4) + "\n\n"; + static final String ENCODE_TO_XML_YANG = "\n" + "\n" + -- cgit 1.2.3-korg From 69d612621915cf9793b5e8c5a4e9ec42187a557d Mon Sep 17 00:00:00 2001 From: Vidyashree Rama Date: Thu, 25 Oct 2018 14:50:31 +0530 Subject: Add authorization header in SSE request Add authorization header in SSE request to support https Issue-ID: CCSDK-628 Change-Id: I031cbed94e21e1f6d90bc0b00b2d70905d47bd69 Signed-off-by: Vidyashree Rama --- .../sli/plugins/restapicall/RestapiCallNode.java | 2 +- .../AdditionalHeaderWebTarget.java | 162 +++++++++++++++++++++ .../restconfdiscovery/RestconfDiscoveryNode.java | 75 +++++++++- .../plugins/restconfdiscovery/SseServerMock.java | 2 +- .../TestRestconfDiscoveryNode.java | 21 +++ 5 files changed, 255 insertions(+), 7 deletions(-) create mode 100644 restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/AdditionalHeaderWebTarget.java (limited to 'restconf-client/provider/src/test/java') diff --git a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java index 9a89af6b5..39399a25a 100644 --- a/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java +++ b/restapi-call-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/restapicall/RestapiCallNode.java @@ -543,7 +543,7 @@ public class RestapiCallNode implements SvcLogicJavaPlugin { return addAuthType(c, p); } - protected Client addAuthType(Client client, Parameters p) throws SvcLogicException { + public Client addAuthType(Client client, Parameters p) throws SvcLogicException { if (p.authtype == AuthType.Unspecified) { if (p.restapiUser != null && p.restapiPassword != null) { client.register(HttpAuthenticationFeature.basic(p.restapiUser, p.restapiPassword)); diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/AdditionalHeaderWebTarget.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/AdditionalHeaderWebTarget.java new file mode 100644 index 000000000..1a0ddbc5f --- /dev/null +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/AdditionalHeaderWebTarget.java @@ -0,0 +1,162 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2018 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.restconfdiscovery; + +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Configuration; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriBuilder; +import java.net.URI; +import java.util.Map; + +/** + * Adds additional headers for SSE request. + */ +class AdditionalHeaderWebTarget implements WebTarget { + private WebTarget base; + private String token; + + public AdditionalHeaderWebTarget(WebTarget target, String token) { + base = target; + this.token = token; + } + + @Override + public Invocation.Builder request() { + return base.request().header("X-ACCESS-TOKEN", token); + } + + @Override + public Invocation.Builder request(String... acceptedResponseTypes) { + return base.request().header("X-ACCESS-TOKEN", token); + } + + @Override + public Invocation.Builder request(MediaType... acceptedResponseTypes) { + return base.request().header("X-ACCESS-TOKEN", token); + } + + @Override + public Configuration getConfiguration() { + return base.getConfiguration(); + } + + @Override + public URI getUri() { + return base.getUri(); + } + + @Override + public UriBuilder getUriBuilder() { + return base.getUriBuilder(); + } + + @Override + public WebTarget path(String path) { + return base.path(path); + } + + @Override + public WebTarget resolveTemplate(String name, Object value) { + return base.resolveTemplate(name, value); + } + + @Override + public WebTarget resolveTemplate(String name, Object value, boolean encodeSlashInPath) { + return base.resolveTemplate(name, value, encodeSlashInPath); + } + + @Override + public WebTarget resolveTemplateFromEncoded(String name, Object value) { + return base.resolveTemplateFromEncoded(name, value); + } + + @Override + public WebTarget resolveTemplates(Map templateValues) { + return base.resolveTemplates(templateValues); + } + + @Override + public WebTarget resolveTemplates(Map templateValues, boolean encodeSlashInPath) { + return base.resolveTemplates(templateValues, encodeSlashInPath); + } + + @Override + public WebTarget resolveTemplatesFromEncoded(Map templateValues) { + return base.resolveTemplatesFromEncoded(templateValues); + } + + @Override + public WebTarget matrixParam(String name, Object... values) { + return base.matrixParam(name, values); + } + + @Override + public WebTarget queryParam(String name, Object... values) { + return base.queryParam(name, values); + } + + @Override + public WebTarget property(String name, Object value) { + return base.property(name, value); + } + + @Override + public WebTarget register(Class componentClass) { + return base.register(componentClass); + } + + @Override + public WebTarget register(Class componentClass, int priority) { + return base.register(componentClass, priority); + } + + @Override + public WebTarget register(Class componentClass, Class... contracts) { + return base.register(componentClass, contracts); + } + + @Override + public WebTarget register(Class componentClass, Map, Integer> contracts) { + return base.register(componentClass, contracts); + } + + @Override + public WebTarget register(Object component) { + return base.register(component); + } + + @Override + public WebTarget register(Object component, int priority) { + return base.register(component, priority); + } + + @Override + public WebTarget register(Object component, Class... contracts) { + return base.register(component, contracts); + } + + @Override + public WebTarget register(Object component, Map, Integer> contracts) { + return base.register(component, contracts); + } +} diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java index 111d62847..5c7d695d9 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java @@ -24,12 +24,21 @@ import org.glassfish.jersey.media.sse.EventSource; import org.glassfish.jersey.media.sse.SseFeature; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.plugins.restapicall.Parameters; +import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode; import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode; import org.slf4j.Logger; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; @@ -115,9 +124,11 @@ public class RestconfDiscoveryNode implements SvcLogicDiscoveryPlugin { class PersistentConnection implements Runnable { private String url; private volatile boolean running = true; + private Map paramMap; - PersistentConnection(String url) { + PersistentConnection(String url, Map paramMap) { this.url = url; + this.paramMap = paramMap; } private void terminate() { @@ -126,15 +137,26 @@ public class RestconfDiscoveryNode implements SvcLogicDiscoveryPlugin { @Override public void run() { - Client client = ClientBuilder.newBuilder() - .register(SseFeature.class).build(); - WebTarget target = client.target(url); + Parameters p; + WebTarget target = null; + try { + RestapiCallNode restapi = restconfApiCallNode.getRestapiCallNode(); + p = restapi.getParameters(paramMap, new Parameters()); + Client client = ignoreSslClient().register(SseFeature.class); + target = restapi.addAuthType(client, p).target(url); + } catch (SvcLogicException e) { + log.error("Exception occured!", e); + Thread.currentThread().interrupt(); + } + + target = addToken(target, paramMap.get("customHttpHeaders")); EventSource eventSource = EventSource.target(target).build(); eventSource.register(new EventHandler(RestconfDiscoveryNode.this)); eventSource.open(); log.info("Connected to SSE source"); while (running) { try { + log.info("SSE state " + eventSource.isOpen()); Thread.sleep(5000); } catch (InterruptedException e) { log.error("Interrupted!", e); @@ -146,6 +168,49 @@ public class RestconfDiscoveryNode implements SvcLogicDiscoveryPlugin { } } + private Client ignoreSslClient() { + SSLContext sslcontext = null; + + try { + sslcontext = SSLContext.getInstance("TLS"); + sslcontext.init(null, new TrustManager[]{new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + } }, new java.security.SecureRandom()); + } catch (NoSuchAlgorithmException | KeyManagementException e) { + throw new IllegalStateException(e); + } + + return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build(); + } + + protected String getTokenId(String customHttpHeaders) { + if (customHttpHeaders.contains("=")) { + String s[] = customHttpHeaders.split("="); + return s[1]; + } + return customHttpHeaders; + } + + protected WebTarget addToken(WebTarget target, String customHttpHeaders) { + if (customHttpHeaders == null) { + return target; + } + + return new AdditionalHeaderWebTarget( + target, getTokenId(customHttpHeaders)); + } + /** * Establishes a persistent between the client and server. * @@ -167,7 +232,7 @@ public class RestconfDiscoveryNode implements SvcLogicDiscoveryPlugin { subscriptionInfoMap.put(id, info); String url = paramMap.get(SSE_URL); - PersistentConnection connection = new PersistentConnection(url); + PersistentConnection connection = new PersistentConnection(url, paramMap); runnableInfo.put(id, connection); executor.execute(connection); } diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SseServerMock.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SseServerMock.java index 1b234a236..35ac221fb 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SseServerMock.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/SseServerMock.java @@ -35,7 +35,7 @@ public class SseServerMock { @Produces(SseFeature.SERVER_SENT_EVENTS) public EventOutput getServerSentEvents() throws IOException { String data = "{" + - "\"ietf-notification:notification\" : {" + + "\"ietf-restconf:notification\" : {" + " \"eventTime\" : \"2017-10-25T08:22:33.44Z\"," + " \"ietf-yang-push:push-change-update\": {" + "\"subscription-id\":\"89\"," + diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java index b2094ca73..af354e9bf 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java @@ -57,6 +57,13 @@ public class TestRestconfDiscoveryNode { p.put("sseConnectURL", "http://localhost:8080/events"); p.put("subscriberId", "networkId"); p.put("responsePrefix", "restapi-result"); + p.put("restapiUser", "access"); + p.put("restapiPassword", "abc@123"); + p.put("customHttpHeaders", "X-ACCESS-TOKEN=x-ik2ps4ikvzupbx0486ft" + + "1ebzs7rt85futh9ho6eofy3wjsap7wqktemlqm4bbsmnar3vrtbyrzuk" + + "bv5itd6m1cftldpjarnyle3sdcqq9hftc4lebz464b5ffxmlbvg9"); + p.put("restapiUrl", "https://localhost:8080/restconf/operations/" + + "ietf-subscribed-notifications:establish-subscription"); RestconfDiscoveryNode rdn = new RestconfDiscoveryNode( new RestconfApiCallNode(new RestapiCallNode())); rdn.establishPersistentConnection(p, ctx, "networkId"); @@ -111,4 +118,18 @@ public class TestRestconfDiscoveryNode { assertThat(rdn.getOutputIdentifier("restapi-result", ctx), is("89")); } + + @Test + public void testGetTokenId() { + String customHttpHeaders = "X-ACCESS-TOKEN=x-ik2ps4ikvzupbx0486ft1ebzs7rt85" + + "futh9ho6eofy3wjsap7wqktemlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarny" + + "le3sdcqq9hftc4lebz464b5ffxmlbvg9"; + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode( + new RestconfApiCallNode(new RestapiCallNode())); + + assertThat(rdn.getTokenId(customHttpHeaders), + is("x-ik2ps4ikvzupbx0486ft1ebzs7rt85futh9ho6eofy3wjsap7wqkt" + + "emlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarnyle3sdcqq9h" + + "ftc4lebz464b5ffxmlbvg9")); + } } -- cgit 1.2.3-korg From 614bd0b6897b1ece3ee7deb581089c301bb3f2e8 Mon Sep 17 00:00:00 2001 From: janani b Date: Fri, 5 Apr 2019 18:23:44 +0530 Subject: Replacing ":" by "_" for parameters Supporting "_" to be present for module name seperator while giving parameters in DG node. Issue-ID: CCSDK-1045 Change-Id: I008fa8503f1314e593f96393121a02f429bac636 Signed-off-by: janani b --- .../restconfapicall/RestconfApiCallNode.java | 25 +- .../plugins/restconfapicall/RestconfApiUtils.java | 22 +- .../DefaultPropertiesNodeListener.java | 11 +- .../MdsalPropertiesNodeSerializer.java | 41 +- .../pnserializer/MdsalPropertiesNodeUtils.java | 243 ++++++- .../pnserializer/SchemaPathHolder.java | 77 +++ .../dfserializer/DataFormatSerializerTest.java | 98 +-- .../dfserializer/DataFormatUtilsTest.java | 22 +- .../dfserializer/IdentifierValidationTest.java | 762 +++++++++++++++++++++ .../IdentifierValidationUtilsTest.java | 469 +++++++++++++ .../pnserializer/PropertiesSerializerTest.java | 224 +++--- .../resources/yang/test_augment_1_for_module.yang | 108 +++ .../resources/yang/test_name_of_the_module.yang | 231 +++++++ 13 files changed, 2107 insertions(+), 226 deletions(-) create mode 100644 restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/SchemaPathHolder.java create mode 100644 restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/IdentifierValidationTest.java create mode 100644 restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/IdentifierValidationUtilsTest.java create mode 100644 restconf-client/provider/src/test/resources/yang/test_augment_1_for_module.yang create mode 100644 restconf-client/provider/src/test/resources/yang/test_name_of_the_module.yang (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java index a76b27d0b..3007b5a1b 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java @@ -25,6 +25,18 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.stream.JsonWriter; + +import javax.ws.rs.core.UriBuilder; +import java.io.StringWriter; +import java.io.Writer; +import java.net.SocketException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; @@ -54,17 +66,6 @@ import org.osgi.framework.ServiceReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.ws.rs.core.UriBuilder; -import java.io.StringWriter; -import java.io.Writer; -import java.net.SocketException; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - import static com.google.common.base.Strings.repeat; import static java.lang.String.format; import static java.lang.String.valueOf; @@ -91,8 +92,8 @@ import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RES_PR import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RETRY_COUNT; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RETRY_FAIL; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.UPDATED_URL; -import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getUpdatedXmlReq; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getSchemaCtxFromDir; +import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getUpdatedXmlReq; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getYangParameters; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfListenerFactory.instance; diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java index c5c7a919b..505089c44 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java @@ -22,15 +22,6 @@ package org.onap.ccsdk.sli.plugins.restconfapicall; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import org.onap.ccsdk.sli.plugins.restapicall.HttpMethod; -import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.YangParameters; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; -import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; -import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; -import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; - import java.io.File; import java.io.IOException; import java.net.URI; @@ -43,6 +34,15 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.plugins.restapicall.HttpMethod; +import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.YangParameters; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; +import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource; +import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException; +import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; + import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.getParameters; import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.parseParam; import static org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode.DEFAULT_MODE; @@ -156,6 +156,7 @@ public final class RestconfApiUtils { */ private static String getParsedPath(String path) { String firstHalf; + String secondHalf; if (path.contains(COLON)) { String[] p = path.split(COLON); if (p[0].contains(SLASH)) { @@ -164,7 +165,8 @@ public final class RestconfApiUtils { } else { firstHalf = p[0]; } - return firstHalf + COLON + p[1]; + secondHalf = path.substring(p[0].length() + 1); + return firstHalf + COLON + secondHalf; } return path; } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java index 491dcb099..2fa00bfb7 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeListener.java @@ -20,12 +20,14 @@ package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; - import java.util.Collection; import java.util.HashMap; import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.COLON; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.UNDERSCORE; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE; @@ -56,9 +58,10 @@ public class DefaultPropertiesNodeListener implements PropertiesNodeListener { || node.nodeType() == MULTI_INSTANCE_LEAF_NODE) { String val = ((LeafNode) node).value(); if (((LeafNode) node).valueNs() != null) { - val = ((LeafNode) node).valueNs().moduleName() + ":" + val; + val = ((LeafNode) node).valueNs().moduleName() + COLON + val; } - params.put(node.uri(), val); + String uri = node.uri().replaceAll(COLON, UNDERSCORE); + params.put(uri, val); } } 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 13a5c5a01..759fe802e 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 @@ -20,20 +20,25 @@ package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer; +import java.util.HashMap; +import java.util.Map; + import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.opendaylight.restconf.common.errors.RestconfDocumentedException; 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 java.util.Map; - +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.DOT_REGEX; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.SLASH; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getChildSchemaNode; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getIndex; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getListName; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNamespace; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getNodeType; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getParsedValue; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getProcessedPath; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName; @@ -70,6 +75,8 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer entry : paramMap.entrySet()) { String[] names = entry.getKey().split("\\."); for (int i = 0; i < names.length; i++) { @@ -86,6 +93,30 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer convertToValidParam(Map paramMap) { + Map fixedParams = new HashMap<>(); + for(Map.Entry entry : paramMap.entrySet()) { + String key = entry.getKey().replaceAll(DOT_REGEX, SLASH); + try { + SchemaPathHolder fixedUrl = getProcessedPath(key, schemaCtx()); + String fixedUri = fixedUrl.getUri().replaceAll( + SLASH, DOT_REGEX); + fixedParams.put(fixedUri, entry.getValue()); + } catch (IllegalArgumentException | RestconfDocumentedException + | NullPointerException e) { + fixedParams.put(entry.getKey(), entry.getValue()); + } + } + return fixedParams; + } + @Override public Map decode(PropertiesNode propertiesNode) throws SvcLogicException { @@ -104,8 +135,10 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer id = toInstanceIdentifier( + uri1, context, null); + return new SchemaPathHolder(id, uri1); + } catch (IllegalArgumentException | RestconfDocumentedException + | NullPointerException e) { + return processNodesAndAppendPath(uri, context); + } + } + + /** + * Processes the nodes in the given uri and finds instance identifier + * context till it reaches the last node in uri. If its not able to find + * schema for the path, it appends the suffix part and puts it back in + * the param list. + * + * @param uri uri with underscore + * @param context schema context + * @return schema and path holder + */ + private static SchemaPathHolder processNodesAndAppendPath(String uri, + SchemaContext context) { + + String actPath = ""; + SchemaPathHolder id = new SchemaPathHolder(null, ""); + String[] uriParts = uri.split(SLASH); + String sec = ""; + if (uri.contains(UNDERSCORE)) { + sec = uri.substring(uriParts[0].length()+1); + } + for (int i = 0; i id; + for (int i = 0; i< values.length-1; i++) { + val = values[i]; + val = firstHalf + val + COLON + secondHalf; + try { + id = toInstanceIdentifier(prefix + val, context, null); + return new SchemaPathHolder(id, val); + } catch (IllegalArgumentException | RestconfDocumentedException | + NullPointerException e) { + log.info(format(INFO_MSG, val)); + } + firstHalf.append(values[i]).append(UNDERSCORE); + secondHalf = secondHalf.replaceFirst( + values[i + 1] + UNDERSCORE,""); + } + val = val.replace(COLON,UNDERSCORE); + try { + id = toInstanceIdentifier(prefix + val, context, null); + return new SchemaPathHolder(id, val); + } catch (IllegalArgumentException | RestconfDocumentedException | + NullPointerException e1) { + throw new IllegalArgumentException(EXC_MSG, e1); + } + } + + /** + * Returns the namespace of the given node name. If the node name is + * separated by colon, the it splits with colon and forms the namespace. + * If the node name is formed with underscore, then it splits the node + * name consecutively to figure out the proper module name. + * + * @param childName node name + * @param ctx schema context + * @param parent parent properties node + * @param curSchema current schema + * @return namespace of the given node + */ + static Namespace getNamespace(String childName, SchemaContext ctx, + PropertiesNode parent, SchemaNode curSchema) { + + Namespace parentNs = parent.namespace(); + Namespace ns = new Namespace(parentNs.moduleName(), + parentNs.moduleNs(), parentNs.revision()); + int lastIndexOfColon = childName.lastIndexOf(COLON); if (lastIndexOfColon != -1) { String moduleName = childName.substring(0, lastIndexOfColon); - Iterator it = ctx.findModules(moduleName).iterator(); - if (!it.hasNext()) { - // module is not present in context - return null; + childName = childName.substring(lastIndexOfColon+1); + Namespace ns1 = getNs(moduleName, ctx); + if (ns1 != null) { + ns = ns1; } + } + + SchemaNode child = getChildSchemaNode(curSchema, childName, ns); + + if (child == null && childName.contains(UNDERSCORE)) { + String[] children = childName.split(UNDERSCORE); + String second = childName.substring(children[0].length() + 1); + StringBuilder first = new StringBuilder(); + + for (int i =0; i< children.length; i++) { + String moduleName = first + children[i]; + Namespace newNs = getNs(moduleName, ctx); + if (newNs != null) { + return newNs; + } + first.append(children[i]).append(UNDERSCORE); + if (i + 1 < children.length) { + second = second.replaceFirst( + children[i + 1] + UNDERSCORE, ""); + } + } + return ns; + } + return ns; + } + + /** + * Returns the namespace by finding the given module in the schema context. + * + * @param modName module name + * @param ctx schema context + * @return namespace of the given node name + */ + private static Namespace getNs(String modName, SchemaContext ctx) { + Iterator it = ctx.findModules(modName).iterator(); + if (it.hasNext()) { Module m = it.next(); - return new Namespace(moduleName, m.getQNameModule().getNamespace(), + return new Namespace(modName, m.getQNameModule().getNamespace(), getRevision(m.getRevision())); } - Namespace parentNs = parent.namespace(); - return new Namespace(parentNs.moduleName(), parentNs.moduleNs(), - parentNs.revision()); + return null; } /** @@ -239,7 +434,7 @@ public final class MdsalPropertiesNodeUtils { * @return child schema node */ public static SchemaNode getChildSchemaNode(SchemaNode curSchema, - String name, + String name, Namespace namespace) { if (namespace == null) { return null; diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/SchemaPathHolder.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/SchemaPathHolder.java new file mode 100644 index 000000000..4ef1bc664 --- /dev/null +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/SchemaPathHolder.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2019 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.yangserializers.pnserializer; + +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; + +/** + * Representation of a holder for a proper path and its corresponding schema. + */ +public class SchemaPathHolder { + + /** + * Schema context for the path. + */ + private InstanceIdentifierContext insId; + + /** + * Formatted path. + */ + private String uri; + + /** + * Constructs schema path holder with path and its schema. + * + * @param insId instance identifier context + * @param uri path + */ + public SchemaPathHolder(InstanceIdentifierContext insId, String uri) { + this.insId = insId; + this.uri = uri; + } + + /** + * Returns the instance identifier context of the path. + * + * @return schema of the path + */ + public InstanceIdentifierContext getInsId() { + return insId; + } + + /** + * Returns the formatted path. + * + * @return formatted path + */ + public String getUri() { + return uri; + } + + /** + * Sets the formatted path. + * + * @param uri formatted path + */ + public void setUri(String uri) { + this.uri = uri; + } +} diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java index 08fa33ee7..45e8eb3f1 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java @@ -20,6 +20,9 @@ package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer; +import java.util.HashMap; +import java.util.Map; + import org.junit.Before; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; @@ -31,9 +34,6 @@ import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode; import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; -import java.util.HashMap; -import java.util.Map; - import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.mockito.Matchers.any; @@ -136,7 +136,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToJsonId() throws SvcLogicException { - String pre = "identity-test:test."; + String pre = "identity-test_test."; SvcLogicContext ctx = createAttList(pre); ctx.setAttribute(pre + "l", "abc"); p.put("dirPath", "src/test/resources"); @@ -156,7 +156,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToJsonIdWithPut() throws SvcLogicException { - String pre = "identity-test:test."; + String pre = "identity-test_test."; SvcLogicContext ctx = createAttList(pre); ctx.setAttribute(pre + "l", "abc"); p.put("dirPath", "src/test/resources"); @@ -176,7 +176,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToJsonIdWithPatch() throws SvcLogicException { - String pre = "identity-test:test."; + String pre = "identity-test_test."; SvcLogicContext ctx = createAttList(pre); ctx.setAttribute(pre + "l", "abc"); p.put("dirPath", "src/test/resources"); @@ -196,7 +196,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToXmlId() throws SvcLogicException { - String pre = "identity-test:test."; + String pre = "identity-test_test."; SvcLogicContext ctx = createAttList(pre); p.put("dirPath", "src/test/resources"); p.put("format", "xml"); @@ -215,7 +215,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToXmlIdWithPut() throws SvcLogicException { - String pre = "identity-test:test."; + String pre = "identity-test_test."; SvcLogicContext ctx = createAttList(pre); p.put("dirPath", "src/test/resources"); p.put("format", "xml"); @@ -234,7 +234,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToXmlIdWithPatch() throws SvcLogicException { - String pre = "identity-test:test."; + String pre = "identity-test_test."; SvcLogicContext ctx = createAttList(pre); p.put("dirPath", "src/test/resources"); p.put("format", "xml"); @@ -255,7 +255,7 @@ public class DataFormatSerializerTest { public void decodeToJsonId() throws SvcLogicException { createMockForDecode(ENCODE_TO_JSON_ID); SvcLogicContext ctx = new SvcLogicContext(); - String pre = "identity-test:test."; + String pre = "identity-test_test."; p.put("dirPath", "src/test/resources"); p.put("format", "json"); p.put("httpMethod", "get"); @@ -276,7 +276,7 @@ public class DataFormatSerializerTest { public void decodeToXmlId() throws SvcLogicException { createMockForDecode(ENCODE_TO_XML_ID); SvcLogicContext ctx = new SvcLogicContext(); - String pre = "identity-test:test."; + String pre = "identity-test_test."; p.put("dirPath", "src/test/resources"); p.put("format", "xml"); p.put("httpMethod", "get"); @@ -294,7 +294,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToJsonYang() throws SvcLogicException { - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; SvcLogicContext ctx = createAttListYang(pre); p.put("dirPath", "src/test/resources"); p.put("format", "json"); @@ -313,7 +313,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToJsonYangWithPut() throws SvcLogicException { - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; SvcLogicContext ctx = createAttListYang(pre); p.put("dirPath", "src/test/resources"); p.put("format", "json"); @@ -332,7 +332,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToJsonYangWithPatch() throws SvcLogicException { - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; SvcLogicContext ctx = createAttListYang(pre); p.put("dirPath", "src/test/resources"); p.put("format", "json"); @@ -351,7 +351,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToJsonWithAugAsRootChild() throws SvcLogicException { - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; SvcLogicContext ctx = createAttListYang(pre); p.put("dirPath", "src/test/resources"); p.put("format", "json"); @@ -372,7 +372,7 @@ public class DataFormatSerializerTest { public void decodeToJsonYang() throws SvcLogicException { createMockForDecode(ENCODE_TO_JSON_YANG); SvcLogicContext ctx = new SvcLogicContext(); - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; p.put("dirPath", "src/test/resources"); p.put("format", "json"); p.put("httpMethod", "get"); @@ -390,7 +390,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToXmlYang() throws SvcLogicException { - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; SvcLogicContext ctx = createAttListYang(pre); p.put("dirPath", "src/test/resources"); p.put("format", "xml"); @@ -409,7 +409,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToXmlYangWithPut() throws SvcLogicException { - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; SvcLogicContext ctx = createAttListYang(pre); p.put("dirPath", "src/test/resources"); p.put("format", "xml"); @@ -428,7 +428,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToXmlYangWithPatch() throws SvcLogicException { - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; SvcLogicContext ctx = createAttListYang(pre); p.put("dirPath", "src/test/resources"); p.put("format", "xml"); @@ -447,7 +447,7 @@ public class DataFormatSerializerTest { */ @Test public void encodeToXmlWithAugAsRootChild() throws SvcLogicException { - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; SvcLogicContext ctx = createAttListYang(pre); p.put("dirPath", "src/test/resources"); p.put("format", "xml"); @@ -468,7 +468,7 @@ public class DataFormatSerializerTest { public void decodeToXmlYang() throws SvcLogicException { createMockForDecode(ENCODE_TO_XML_YANG); SvcLogicContext ctx = new SvcLogicContext(); - String pre = "test-yang:cont1.cont2."; + String pre = "test-yang_cont1.cont2."; p.put("dirPath", "src/test/resources"); p.put("format", "xml"); p.put("httpMethod", "get"); @@ -487,8 +487,8 @@ public class DataFormatSerializerTest { @Test public void codecToJsonRpc() throws SvcLogicException { createMockForDecode(DECODE_FROM_JSON_RPC); - String inPre = "test-yang:create-sfc.input."; - String outPre = "test-yang:create-sfc.output."; + String inPre = "test-yang_create-sfc.input."; + String outPre = "test-yang_create-sfc.output."; SvcLogicContext ctx = createAttListRpc(inPre); p.put("dirPath", "src/test/resources"); p.put("format", "json"); @@ -509,8 +509,8 @@ public class DataFormatSerializerTest { @Test public void codecToXmlRpc() throws SvcLogicException { createMockForDecode(DECODE_FROM_XML_RPC); - String inPre = "test-yang:create-sfc.input."; - String outPre = "test-yang:create-sfc.output."; + String inPre = "test-yang_create-sfc.input."; + String outPre = "test-yang_create-sfc.output."; SvcLogicContext ctx = createAttListRpc(inPre); p.put("dirPath", "src/test/resources"); p.put("format", "xml"); @@ -650,21 +650,21 @@ public class DataFormatSerializerTest { ctx.setAttribute(pre + "leaf12", "abc"); ctx.setAttribute(pre + "ll5[0]", "abc"); ctx.setAttribute(pre + "ll5[1]", "abc"); - ctx.setAttribute(pre + "cont4.test-augment:cont5.leaf13", "true"); - ctx.setAttribute(pre + "cont4.test-augment:list7[0].leaf14", "test"); - ctx.setAttribute(pre + "cont4.test-augment:list7[1].leaf14", "create"); - ctx.setAttribute(pre + "cont4.test-augment:leaf15", "abc"); - ctx.setAttribute(pre + "cont4.test-augment:ll6[0]", "unbounded"); - ctx.setAttribute(pre + "cont4.test-augment:ll6[1]", "8"); - ctx.setAttribute(pre + "cont4.test-augment:cont13.cont12.leaf26", + ctx.setAttribute(pre + "cont4.test-augment_cont5.leaf13", "true"); + ctx.setAttribute(pre + "cont4.test-augment_list7[0].leaf14", "test"); + ctx.setAttribute(pre + "cont4.test-augment_list7[1].leaf14", "create"); + ctx.setAttribute(pre + "cont4.test-augment_leaf15", "abc"); + ctx.setAttribute(pre + "cont4.test-augment_ll6[0]", "unbounded"); + ctx.setAttribute(pre + "cont4.test-augment_ll6[1]", "8"); + ctx.setAttribute(pre + "cont4.test-augment_cont13.cont12.leaf26", "abc"); - ctx.setAttribute(pre + "cont4.test-augment:cont13.list9[0].leaf27", + ctx.setAttribute(pre + "cont4.test-augment_cont13.list9[0].leaf27", "abc"); - ctx.setAttribute(pre + "cont4.test-augment:cont13.list9[1].leaf27", + ctx.setAttribute(pre + "cont4.test-augment_cont13.list9[1].leaf27", "abc"); - ctx.setAttribute(pre + "cont4.test-augment:cont13.leaf28", "abc"); - ctx.setAttribute(pre + "cont4.test-augment:cont13.ll9[0]", "abc"); - ctx.setAttribute(pre + "cont4.test-augment:cont13.ll9[1]", "abc"); + ctx.setAttribute(pre + "cont4.test-augment_cont13.leaf28", "abc"); + ctx.setAttribute(pre + "cont4.test-augment_cont13.ll9[0]", "abc"); + ctx.setAttribute(pre + "cont4.test-augment_cont13.ll9[1]", "abc"); return ctx; } @@ -785,29 +785,29 @@ public class DataFormatSerializerTest { assertThat(ctx.getAttribute(pre + "leaf12"), is("abc")); assertThat(ctx.getAttribute(pre + "ll5[0]"), is("abc")); assertThat(ctx.getAttribute(pre + "ll5[1]"), is("abc")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont5.leaf13"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont5.leaf13"), is("true")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:list7[0].leaf14"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_list7[0].leaf14"), is("test")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:list7[1].leaf14"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_list7[1].leaf14"), is("create")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:leaf15"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_leaf15"), is("abc")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:ll6[0]"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_ll6[0]"), is("unbounded")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:ll6[1]"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_ll6[1]"), is("8")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13" + + assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13" + ".cont12.leaf26"), is("abc")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.list9[0]" + + assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.list9[0]" + ".leaf27"), is("abc")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.list9[1]" + + assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.list9[1]" + ".leaf27"), is("abc")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.leaf28"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.leaf28"), is("abc")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.ll9[0]"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.ll9[0]"), is("abc")); - assertThat(ctx.getAttribute(pre + "cont4.test-augment:cont13.ll9[1]"), + assertThat(ctx.getAttribute(pre + "cont4.test-augment_cont13.ll9[1]"), is("abc")); } diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java index 027604480..a7814e10e 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java @@ -121,7 +121,11 @@ public final class DataFormatUtilsTest { "\n"; static final String ENCODE_TO_JSON_YANG_COMMON = "\n " + - "\"test-augment:cont13\": {\n" + + "\"test-augment:ll6\": [\n" + + " \"unbounded\",\n" + + " \"8\"\n" + + " ],\n" + + " \"test-augment:cont13\": {\n" + " \"ll9\": [\n" + " \"abc\",\n" + " \"abc\"\n" + @@ -148,10 +152,6 @@ public final class DataFormatUtilsTest { " }\n" + " ],\n" + " \"test-augment:leaf15\": \"abc\",\n" + - " \"test-augment:ll6\": [\n" + - " \"unbounded\",\n" + - " \"8\"\n" + - " ],\n" + " \"test-augment:cont5\": {\n" + " \"leaf13\": \"true\"\n" + " }"; @@ -277,6 +277,10 @@ public final class DataFormatUtilsTest { "}"; static final String ENCODE_TO_XML_YANG_COMMON = "\n" + + "unbounded\n" + + "8\n" + "\n" + " abc\n" + @@ -302,10 +306,6 @@ public final class DataFormatUtilsTest { "\n" + "abc\n" + - "unbounded\n" + - "8\n" + "\n" + " true\n" + @@ -557,8 +557,8 @@ public final class DataFormatUtilsTest { * @param i number of space * @return space appended string */ - private static String addSpace(String req, int i) { - StringBuilder space = new StringBuilder(""); + public static String addSpace(String req, int i) { + StringBuilder space = new StringBuilder(); for (int sp = 0; sp < i; sp++) { space = space.append(" "); } diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/IdentifierValidationTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/IdentifierValidationTest.java new file mode 100644 index 000000000..1109d426c --- /dev/null +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/IdentifierValidationTest.java @@ -0,0 +1,762 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2019 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.yangserializers.dfserializer; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.plugins.restapicall.HttpResponse; +import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode; +import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.DECODE_FROM_JSON_RPC_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.DECODE_FROM_XML_RPC_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_JSON_RPC_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_JSON_WITH_AUG_PATH; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_JSON_YANG_AUG_POST_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_JSON_YANG_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_JSON_YANG_PUT_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_XML_RPC_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_XML_YANG_AUG_POST_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_XML_YANG_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.IdentifierValidationUtilsTest.ENCODE_TO_XML_YANG_PUT_ID; + +/** + * Unit test cases for identifier validation test. + */ +public class IdentifierValidationTest { + + private Map p; + + private RestconfApiCallNode restconf; + + private RestapiCallNode restApi; + + private DfCaptor dfCaptor; + + /** + * Sets up the pre-requisite for each test case. + * + * @throws SvcLogicException when test case fails + */ + @Before + public void setUp() throws SvcLogicException { + p = new HashMap<>(); + p.put("restapiUser", "user1"); + p.put("restapiPassword", "abc123"); + p.put("responsePrefix", "response"); + p.put("skipSending", "true"); + restApi = new RestapiCallNode(); + restconf = mock(RestconfApiCallNode.class); + dfCaptor = new DfCaptor(); + createMethodMocks(); + } + + /** + * Creates method mocks using mockito for RestconfApiCallNode class. + * + * @throws SvcLogicException when test case fails + */ + private void createMethodMocks() throws SvcLogicException { + doReturn(restApi).when(restconf).getRestapiCallNode(); + doCallRealMethod().when(restconf).sendRequest( + any(Map.class), any(SvcLogicContext.class)); + doCallRealMethod().when(restconf).sendRequest( + any(Map.class), any(SvcLogicContext.class), any(Integer.class)); + doAnswer(dfCaptor).when(restconf).serializeRequest( + any(Map.class), any(YangParameters.class), any(String.class), + any(InstanceIdentifierContext.class)); + doAnswer(dfCaptor).when(restconf).updateReq( + any(String.class), any(YangParameters.class), + any(InstanceIdentifierContext.class)); + } + + /** + * Creates mock using mockito with input data for decoding. + * + * @param decodeData input data + * @throws SvcLogicException when test case fails + */ + private void createMockForDecode(String decodeData) + throws SvcLogicException { + doReturn(decodeData).when(restconf).getResponse( + any(SvcLogicContext.class), any(YangParameters.class), + any(String.class), any(HttpResponse.class)); + doCallRealMethod().when(restconf).serializeResponse( + any(YangParameters.class), any(String.class), any(String.class), + any(InstanceIdentifierContext.class)); + } + + /** + * Verifies encoding of parameters to JSON data format with containers, + * grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYang() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_" + + "of_the_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman.com/restconf/operati" + + "ons/test_name_of_the_module:name_of_the_cont1"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_ID)); + } + + /** + * Verifies encoding of parameters with augment in the URL. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYangWithAugUrl() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_" + + "of_the_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman.com/restconf/operati" + + "ons/test_name_of_the_module:name_of_the_cont1/name_of_t" + + "he_cont2/name_of_the_cont4/test_augment_1_for_module:na" + + "me_of_the_cont5"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_WITH_AUG_PATH)); + } + + /** + * Verifies encoding of parameters to JSON data format with containers, + * grouping and augment for put operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYangWithPut() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_of_the_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test_name_of_the_module:name_of" + + "_the_cont1/name_of_the_cont2/name_of_the_cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_PUT_ID)); + } + + /** + * Verifies encoding of parameters to JSON data format with containers, + * grouping and augment for patch operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYangWithPatch() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_o" + + "f_the_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "patch"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test_name_of_the_module:name_of_" + + "the_cont1/name_of_the_cont2/name_of_the_cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_PUT_ID)); + } + + /** + * Verifies encoding of parameters to JSON data format with augment as + * root child. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonWithAugAsRootChild() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_of_" + + "the_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test_name_of_the_module:name_of_" + + "the_cont1/name_of_the_cont2/name_of_the_cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_AUG_POST_ID)); + } + + /** + * Verifies decoding of parameters from JSON data format with containers, + * grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void decodeToJsonYang() throws SvcLogicException { + createMockForDecode(ENCODE_TO_JSON_YANG_ID); + SvcLogicContext ctx = new SvcLogicContext(); + String pre = "test_name_of_the_module_name_of_the_cont1.name_" + + "of_the_cont2."; + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "get"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test_name_of_the_module:name" + + "_of_the_cont1"); + restconf.sendRequest(p, ctx); + verifyAttListYang(ctx, pre); + } + + /** + * Verifies encoding of parameters to XML data format with containers, + * grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlYang() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_of" + + "_the_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman.com/restconf/operations/" + + "test_name_of_the_module:name_of_the_cont1"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_ID)); + } + + /** + * Verifies encoding of parameters to XML data format with containers, + * grouping and augment for put operation-type + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlYangWithPut() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_" + + "of_the_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman.com/restconf/operations/" + + "test_name_of_the_module:name_of_the_cont1/name_of_the_cont2" + + "/name_of_the_cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_PUT_ID)); + } + + /** + * Verifies encoding of parameters to XML data format with containers, + * grouping and augment for patch operation-type + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlYangWithPatch() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_of" + + "_the_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman.com/restconf/operation" + + "s/test_name_of_the_module:name_of_the_cont1/name_of_the_c" + + "ont2/name_of_the_cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_PUT_ID)); + } + + /** + * Verifies encoding of parameters to XML data format with augment as + * root child. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlWithAugAsRootChild() throws SvcLogicException { + String pre = "test_name_of_the_module_name_of_the_cont1.name_of_the" + + "_cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman.com/restconf/operations/" + + "test_name_of_the_module:name_of_the_cont1/name_of_the_cont2" + + "/name_of_the_cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_AUG_POST_ID)); + } + + /** + * Verifies decoding of parameters from XML data format with containers, + * grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void decodeToXmlYang() throws SvcLogicException { + createMockForDecode(ENCODE_TO_XML_YANG_ID); + SvcLogicContext ctx = new SvcLogicContext(); + String pre = "test_name_of_the_module_name_of_the_cont1.name_of_" + + "the_cont2."; + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "get"); + p.put("restapiUrl", "http://echo.getpostman.com/restconf/operation" + + "s/test_name_of_the_module:name_of_the_cont1"); + restconf.sendRequest(p, ctx); + verifyAttListYang(ctx, pre); + } + + /** + * Verifies encoding of and decoding from, JSON respectively for data + * format with containers, grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void codecToJsonRpc() throws SvcLogicException { + createMockForDecode(DECODE_FROM_JSON_RPC_ID); + String inPre = "test_name_of_the_module_name_of_the_create-sfc.input."; + String outPre = "test_name_of_the_module_name_of_the_create-sfc" + + ".output."; + SvcLogicContext ctx = createAttListRpc(inPre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman.com/restconf/operations" + + "/test_name_of_the_module:name_of_the_create-sfc"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_RPC_ID)); + verifyAttListRpc(ctx, outPre); + } + + /** + * Verifies encoding of and decoding from, XML respectively for data + * format with containers, grouping and augment. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void codecToXmlRpc() throws SvcLogicException { + createMockForDecode(DECODE_FROM_XML_RPC_ID); + String inPre = "test_name_of_the_module_name_of_the_create-sfc.input."; + String outPre = "test_name_of_the_module_name_of_the_create-sfc.output."; + SvcLogicContext ctx = createAttListRpc(inPre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test_name_of_the_module" + + ":name_of_the_create-sfc"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_RPC_ID)); + verifyAttListRpc(ctx, outPre); + } + + /** + * Creates attribute list for encoding JSON or XML with container, + * grouping and augmented YANG file. + * + * @param pre prefix + * @return service logic context + */ + private SvcLogicContext createAttListYang(String pre) { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(pre + "name_of_the_cont3.name_of_the_leaf" + + "10", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_leaf1" + + "", "true"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_leaf2" + + "", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_leaf3" + + "", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "ll1[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "ll1[1]", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "ll2[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "ll2[1]", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "cont4.name_of_the_leaf11", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "list4[0].name_of_the_leaf8", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "list4[1].name_of_the_leaf8", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "list5[0].name_of_the_leaf9", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[0].name_of_the_" + + "list5[1].name_of_the_leaf9", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "leaf1", "true"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "leaf2", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "leaf3", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "ll1[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "ll1[1]", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "ll2[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "ll2[1]", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "cont4.name_of_the_leaf11", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "list4[0].name_of_the_leaf8", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "list4[1].name_of_the_leaf8", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "list5[0].name_of_the_leaf9", "abc"); + ctx.setAttribute(pre + "name_of_the_list1[1].name_of_the_" + + "list5[1].name_of_the_leaf9", "abc"); + ctx.setAttribute(pre + "name_of_the_list2[0].name_of_the_" + + "leaf4", "abc"); + ctx.setAttribute(pre + "name_of_the_list2[1].name_of_the_" + + "leaf4", "abc"); + ctx.setAttribute(pre + "name_of_the_leaf5", "abc"); + ctx.setAttribute(pre + "name_of_the_leaf6", "abc"); + ctx.setAttribute(pre + "name_of_the_ll3[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_ll3[1]", "abc"); + ctx.setAttribute(pre + "name_of_the_ll4[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_ll4[1]", "abc"); + ctx.setAttribute(pre + "name_of_the_cont4.name_of_the_leaf10", + "abc"); + ctx.setAttribute(pre + "name_of_the_list6[0].name_of_the_leaf11", + "abc"); + ctx.setAttribute(pre + "name_of_the_list6[1].name_of_the_leaf11", + "abc"); + ctx.setAttribute(pre + "name_of_the_leaf12", "abc"); + ctx.setAttribute(pre + "name_of_the_ll5[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_ll5[1]", "abc"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for_" + + "module_name_of_the_cont5.name_of_the_leaf13", + "true"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for_" + + "module_name_of_the_list7[0].name_of_the" + + "_leaf14", "test"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for_" + + "module_name_of_the_list7[1].name_of_the" + + "_leaf14", "create"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for_" + + "module_name_of_the_leaf15", "abc"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for_" + + "module_name_of_the_ll6[0]", + "unbounded"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for" + + "_module_name_of_the_ll6[1]", "8"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for" + + "_module_name_of_the_cont13.name_of_the_" + + "cont12.name_of_the_leaf26", + "abc"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for_" + + "module_name_of_the_cont13.name_of_the_" + + "list9[0].name_of_the_leaf27", + "abc"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for" + + "_module_name_of_the_cont13.name_of_the_" + + "list9[1].name_of_the_leaf27", + "abc"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for" + + "_module_name_of_the_cont13.name_of_the_" + + "leaf28", "abc"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for" + + "_module_name_of_the_cont13.name_of_the_" + + "ll9[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_cont4.test_augment_1_for" + + "_module_name_of_the_cont13.name_of_the_" + + "ll9[1]", "abc"); + return ctx; + } + + /** + * Creates attribute list for encoding JSON or XML with RPC YANG file. + * + * @param pre prefix + * @return service logic context + */ + private SvcLogicContext createAttListRpc(String pre) { + SvcLogicContext ctx = new SvcLogicContext(); + ctx.setAttribute(pre + "name_of_the_cont14.name_of_the_leaf28", + "abc"); + ctx.setAttribute(pre + "name_of_the_list10[0].name_of_the_leaf29", + "abc"); + ctx.setAttribute(pre + "name_of_the_list10[1].name_of_the_leaf29", + "abc"); + ctx.setAttribute(pre + "name_of_the_leaf30", "abc"); + ctx.setAttribute(pre + "name_of_the_ll10[0]", "abc"); + ctx.setAttribute(pre + "name_of_the_ll10[1]", "abc"); + ctx.setAttribute(pre + "name_of_the_cont15.name_of_the_leaf31", + "abc"); + ctx.setAttribute(pre + "name_of_the_cont13.name_of_the_list9[0]" + + ".name_of_the_leaf27", "abc"); + ctx.setAttribute(pre + "name_of_the_cont13.name_of_the_list9[1]" + + ".name_of_the_leaf27", "abc"); + ctx.setAttribute(pre + "name_of_the_cont13.name_of_the_leaf28", + "abc"); + ctx.setAttribute(pre + "name_of_the_cont13.name_of_the_ll9[0]", + "abc"); + ctx.setAttribute(pre + "name_of_the_cont13.name_of_the_ll9[1]", + "abc"); + return ctx; + } + + /** + * Verifies the attribute list for decoding from JSON or XML with + * container, grouping and augmented file. + * + * @param ctx service logic context + * @param pre prefix + */ + private void verifyAttListYang(SvcLogicContext ctx, String pre) { + assertThat(ctx.getAttribute(pre + "name_of_the_cont3.name_of" + + "_the_leaf10"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name" + + "_of_the_leaf1"), is("true")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name" + + "_of_the_leaf2"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name" + + "_of_the_leaf3"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name" + + "_of_the_ll1[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name" + + "_of_the_ll1[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name" + + "_of_the_ll2[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name" + + "_of_the_ll2[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name" + + "_of_the_cont4.name_of_the_leaf11"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name_of" + + "_the_list4[0].name_of_the_leaf8"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name_of" + + "_the_list4[1].name_of_the_leaf8"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name_of" + + "_the_list5[0].name_of_the_leaf9"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[0].name_of" + + "_the_list5[1].name_of_the_leaf9"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_leaf1"), is("true")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_leaf2"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_leaf3"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_ll1[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_ll1[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_ll2[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_ll2[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_cont4.name_of_the_leaf11"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_list4[0].name_of_the_leaf8"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_list4[1].name_of_the_leaf8"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_list5[0].name_of_the_leaf9"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list1[1].name_of" + + "_the_list5[1].name_of_the_leaf9"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list2[0].name_of" + + "_the_leaf4"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list2[1].name_of" + + "_the_leaf4"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_leaf5"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_leaf6"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_ll3[0]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_ll3[1]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_ll4[0]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_ll4[1]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.name_of" + + "_the_leaf10"), is( "abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list6[0].name_of" + + "_the_leaf11"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list6[1].name_of" + + "_the_leaf11"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_leaf12"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_ll5[0]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_ll5[1]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_cont5.name_of_the_leaf13"), + is("true")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_list7[0].name_of_the_leaf14"), + is("test")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_list7[1].name_of_the_leaf14"), + is("create")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_leaf15"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_ll6[0]"), + is("unbounded")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_ll6[1]"), + is("8")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_cont13" + + ".name_of_the_cont12.name_of_" + + "the_leaf26"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_cont13.name_of_the_list9[0]" + + ".name_of_the_leaf27"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_cont13.name_of_the_list9[1]" + + ".name_of_the_leaf27"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_cont13.name_of_the_leaf28"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_cont13.name_of_the_ll9[0]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont4.test_" + + "augment_1_for_module_name_of_" + + "the_cont13.name_of_the_ll9[1]"), + is("abc")); + } + + /** + * Verifies the attribute list for decoding from JSON or XML with + * RPC YANG file. + * + * @param ctx service logic context + * @param pre prefix + */ + private void verifyAttListRpc(SvcLogicContext ctx, String pre) { + assertThat(ctx.getAttribute(pre + "name_of_the_cont16.name_of_" + + "the_leaf32"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list11[0].name" + + "_of_the_leaf33"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_list11[1].name" + + "_of_the_leaf33"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_leaf34"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_ll11[0]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_ll11[1]"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont17.name_of_" + + "the_leaf35"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont13.name_of_" + + "the_cont12.name_of_the_leaf26"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont13.name_of_" + + "the_list9[0].name_of_the_leaf27"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont13.name_of_" + + "the_list9[1].name_of_the_leaf27"), + is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont13.name_of_" + + "the_ll9[0]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont13.name_of_" + + "the_ll9[1]"), is("abc")); + assertThat(ctx.getAttribute(pre + "name_of_the_cont13.name_of_" + + "the_leaf28"), is("abc")); + } + + /** + * Captures the data format messages by mocking it, which can be used in + * testing the value. + * + * @param capturing data format + */ + public class DfCaptor implements Answer { + + private String result; + + /** + * Returns the captured data format message. + * + * @return data format message. + */ + public String getResult() { + return result; + } + + @Override + public String answer(InvocationOnMock invocationOnMock) + throws Throwable { + result = (String) invocationOnMock.callRealMethod(); + return result; + } + } + +} diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/IdentifierValidationUtilsTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/IdentifierValidationUtilsTest.java new file mode 100644 index 000000000..a866f1c4a --- /dev/null +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/IdentifierValidationUtilsTest.java @@ -0,0 +1,469 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - CCSDK + * ================================================================================ + * Copyright (C) 2019 Huawei Technologies Co., Ltd. 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.onap.ccsdk.sli.plugins.yangserializers.dfserializer; + +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.addSpace; + +/** + * Unit test case utilities for identifier validation and restconf api + * call node. + */ +public final class IdentifierValidationUtilsTest { + + static final String ENCODE_TO_JSON_YANG_COMMON_ID = "\n " + + "\"test_augment_1_for_module:name_of_the_ll6\": [\n" + + " \"unbounded\",\n" + + " \"8\"\n" + + " ],\n" + + " \"test_augment_1_for_module:name_of_the_cont13\": {\n" + + " \"name_of_the_cont12\": {\n" + + " \"name_of_the_leaf26\": \"abc\"\n" + + " },\n" + + " \"name_of_the_ll9\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_leaf28\": \"abc\",\n" + + " \"name_of_the_list9\": [\n" + + " {\n" + + " \"name_of_the_leaf27\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf27\": \"abc\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"test_augment_1_for_module:name_of_the_list7\": [\n" + + " {\n" + + " \"name_of_the_leaf14\": \"test\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf14\": \"create\"\n" + + " }\n" + + " ],\n" + + " \"test_augment_1_for_module:name_of_the_leaf15\": \"abc\",\n" + + " \"test_augment_1_for_module:name_of_the_cont5\": {\n" + + " \"name_of_the_leaf13\": \"true\"\n" + + " }"; + + static final String ENCODE_TO_JSON_YANG_AUG_POST_ID = "{\n" + + " \"test_name_of_the_module:name_of_the_leaf10\": \"abc\"," + + ENCODE_TO_JSON_YANG_COMMON_ID + "\n}"; + + static final String ENCODE_TO_JSON_YANG_ID = "{\n" + + " \"test_name_of_the_module:name_of_the_cont2\": {\n" + + " \"name_of_the_ll4\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_leaf5\": \"abc\",\n" + + " \"name_of_the_list6\": [\n" + + " {\n" + + " \"name_of_the_leaf11\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf11\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_ll5\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_ll3\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_leaf6\": \"abc\",\n" + + " \"name_of_the_cont3\": {\n" + + " \"name_of_the_leaf10\": \"abc\"\n" + + " },\n" + + " \"name_of_the_list2\": [\n" + + " {\n" + + " \"name_of_the_leaf4\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf4\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_list1\": [\n" + + " {\n" + + " \"name_of_the_ll2\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_list5\": [\n" + + " {\n" + + " \"name_of_the_leaf9\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf9\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_list4\": [\n" + + " {\n" + + " \"name_of_the_leaf8\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf8\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_leaf1\": \"true\",\n" + + " \"name_of_the_leaf3\": \"abc\",\n" + + " \"name_of_the_leaf2\": \"abc\",\n" + + " \"name_of_the_cont4\": {\n" + + " \"name_of_the_leaf11\": \"abc\"\n" + + " },\n" + + " \"name_of_the_ll1\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"name_of_the_ll2\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_list5\": [\n" + + " {\n" + + " \"name_of_the_leaf9\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf9\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_list4\": [\n" + + " {\n" + + " \"name_of_the_leaf8\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf8\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_leaf1\": \"true\",\n" + + " \"name_of_the_leaf3\": \"abc\",\n" + + " \"name_of_the_leaf2\": \"abc\",\n" + + " \"name_of_the_cont4\": {\n" + + " \"name_of_the_leaf11\": \"abc\"\n" + + " },\n" + + " \"name_of_the_ll1\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ]\n" + + " }\n" + + " ],\n" + + " \"name_of_the_cont4\": {\n" + + " \"name_of_the_leaf10\": \"abc\"," + + addSpace(ENCODE_TO_JSON_YANG_COMMON_ID,8) + "\n" + + " },\n" + + " \"name_of_the_leaf12\": \"abc\"\n" + + " }\n" + + "}"; + + static final String ENCODE_TO_JSON_WITH_AUG_PATH = "{\n" + + " \"test_augment_1_for_module:name_of_the_leaf13\": \"true\"\n" + + "}"; + + static final String ENCODE_TO_JSON_YANG_PUT_ID = "{\n" + + " \"test_name_of_the_module:name_of_the_cont4\": {" + addSpace( + ENCODE_TO_JSON_YANG_COMMON_ID, 4) + ",\n" + + " \"name_of_the_leaf10\": \"abc\"\n" + + " }\n" + + "}"; + + static final String ENCODE_TO_XML_YANG_COMMON_ID = "\n" + + "unbounded\n" + + "8\n" + + "\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + "\n" + + "\n" + + " test\n" + + "\n" + + "\n" + + " create\n" + + "\n" + + "abc\n" + + "\n" + + " true\n" + + ""; + + static final String ENCODE_TO_XML_YANG_AUG_POST_ID = "\n" + + "abc" + + ENCODE_TO_XML_YANG_COMMON_ID + "\n"; + + static final String ENCODE_TO_XML_YANG_PUT_ID = "\n" + + "\n" + + " abc" + + addSpace(ENCODE_TO_XML_YANG_COMMON_ID, 4) + "\n\n"; + + static final String ENCODE_TO_XML_YANG_ID= "\n" + + "\n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " true\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " true\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " \n" + + " \n" + + " abc" + + addSpace(ENCODE_TO_XML_YANG_COMMON_ID, 8) + "\n" + + " \n" + + " abc\n" + + "\n"; + + static final String ENCODE_TO_JSON_RPC_ID = "{\n" + + " \"test_name_of_the_module:input\": {\n" + + " \"name_of_the_cont14\": {\n" + + " \"name_of_the_leaf28\": \"abc\"\n" + + " },\n" + + " \"name_of_the_cont13\": {\n" + + " \"name_of_the_ll9\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_leaf28\": \"abc\",\n" + + " \"name_of_the_list9\": [\n" + + " {\n" + + " \"name_of_the_leaf27\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf27\": \"abc\"\n" + + " }\n" + + " ]\n" + + " },\n" + + " \"name_of_the_leaf30\": \"abc\",\n" + + " \"name_of_the_ll10\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_list10\": [\n" + + " {\n" + + " \"name_of_the_leaf29\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf29\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_cont15\": {\n" + + " \"name_of_the_leaf31\": \"abc\"\n" + + " }\n" + + " }\n" + + "}"; + + static final String DECODE_FROM_JSON_RPC_ID = "{\n" + + " \"test_name_of_the_module:output\": {\n" + + " \"name_of_the_cont16\": {\n" + + " \"name_of_the_leaf32\": \"abc\"\n" + + " },\n" + + " \"name_of_the_list11\": [\n" + + " {\n" + + " \"name_of_the_leaf33\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf33\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_leaf34\": \"abc\",\n" + + " \"name_of_the_ll11\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_cont17\": {\n" + + " \"name_of_the_leaf35\": \"abc\"\n" + + " },\n" + + " \"name_of_the_cont13\": {\n" + + " \"name_of_the_cont12\": {\n" + + " \"name_of_the_leaf26\": \"abc\"\n" + + " },\n" + + " \"name_of_the_list9\": [\n" + + " {\n" + + " \"name_of_the_leaf27\": \"abc\"\n" + + " },\n" + + " {\n" + + " \"name_of_the_leaf27\": \"abc\"\n" + + " }\n" + + " ],\n" + + " \"name_of_the_ll9\": [\n" + + " \"abc\",\n" + + " \"abc\"\n" + + " ],\n" + + " \"name_of_the_leaf28\": \"abc\"\n" + + " }\n" + + " }\n" + + "}"; + + static final String ENCODE_TO_XML_RPC_ID = "\n" + + "\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + "\n"; + + static final String DECODE_FROM_XML_RPC_ID = "\n" + + "\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " \n" + + " abc\n" + + " \n" + + " abc\n" + + " abc\n" + + " abc\n" + + " \n" + + ""; +} diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java index dc0da6f7c..c3a6b4ea8 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/PropertiesSerializerTest.java @@ -19,15 +19,6 @@ */ package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer; -import org.junit.Before; -import org.junit.Test; - -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import org.opendaylight.restconf.common.context.InstanceIdentifierContext; -import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; - import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; @@ -37,6 +28,14 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import org.junit.Before; +import org.junit.Test; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.opendaylight.restconf.common.context.InstanceIdentifierContext; +import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertTrue; @@ -53,39 +52,39 @@ public final class PropertiesSerializerTest { public void testBasicConstructs() throws SvcLogicException { String uri = "test-yang:cont1/cont2"; Map params = new HashMap<>(); - params.put("test-yang:cont1.cont2.cont3.leaf10", "abc"); - params.put("test-yang:cont1.cont2.list1[0].leaf1", "abc"); - params.put("test-yang:cont1.cont2.list1[0].leaf2", "abc"); - params.put("test-yang:cont1.cont2.list1[0].leaf3", "abc"); - params.put("test-yang:cont1.cont2.list1[0].ll1[0]", "abc"); - params.put("test-yang:cont1.cont2.list1[0].ll1[1]", "abc"); - params.put("test-yang:cont1.cont2.list1[0].ll2[0]", "abc"); - params.put("test-yang:cont1.cont2.list1[0].ll2[1]", "abc"); - params.put("test-yang:cont1.cont2.list1[0].cont4.leaf11", "abc"); - params.put("test-yang:cont1.cont2.list1[0].list4[0].leaf8", "abc"); - params.put("test-yang:cont1.cont2.list1[0].list4[1].leaf8", "abc"); - params.put("test-yang:cont1.cont2.list1[0].list5[0].leaf9", "abc"); - params.put("test-yang:cont1.cont2.list1[0].list5[1].leaf9", "abc"); - params.put("test-yang:cont1.cont2.list1[1].leaf1", "abc"); - params.put("test-yang:cont1.cont2.list1[1].leaf2", "abc"); - params.put("test-yang:cont1.cont2.list1[1].leaf3", "abc"); - params.put("test-yang:cont1.cont2.list1[1].ll1[0]", "abc"); - params.put("test-yang:cont1.cont2.list1[1].ll1[1]", "abc"); - params.put("test-yang:cont1.cont2.list1[1].ll2[0]", "abc"); - params.put("test-yang:cont1.cont2.list1[1].ll2[1]", "abc"); - params.put("test-yang:cont1.cont2.list1[1].cont4.leaf11", "abc"); - params.put("test-yang:cont1.cont2.list1[1].list4[0].leaf8", "abc"); - params.put("test-yang:cont1.cont2.list1[1].list4[1].leaf8", "abc"); - params.put("test-yang:cont1.cont2.list1[1].list5[0].leaf9", "abc"); - params.put("test-yang:cont1.cont2.list1[1].list5[1].leaf9", "abc"); - params.put("test-yang:cont1.cont2.list2[0].leaf4", "abc"); - params.put("test-yang:cont1.cont2.list2[1].leaf4", "abc"); - params.put("test-yang:cont1.cont2.leaf5", "abc"); - params.put("test-yang:cont1.cont2.leaf6", "abc"); - params.put("test-yang:cont1.cont2.ll3[0]", "abc"); - params.put("test-yang:cont1.cont2.ll3[1]", "abc"); - params.put("test-yang:cont1.cont2.ll4[0]", "abc"); - params.put("test-yang:cont1.cont2.ll4[1]", "abc"); + params.put("test-yang_cont1.cont2.cont3.leaf10", "abc"); + params.put("test-yang_cont1.cont2.list1[0].leaf1", "abc"); + params.put("test-yang_cont1.cont2.list1[0].leaf2", "abc"); + params.put("test-yang_cont1.cont2.list1[0].leaf3", "abc"); + params.put("test-yang_cont1.cont2.list1[0].ll1[0]", "abc"); + params.put("test-yang_cont1.cont2.list1[0].ll1[1]", "abc"); + params.put("test-yang_cont1.cont2.list1[0].ll2[0]", "abc"); + params.put("test-yang_cont1.cont2.list1[0].ll2[1]", "abc"); + params.put("test-yang_cont1.cont2.list1[0].cont4.leaf11", "abc"); + params.put("test-yang_cont1.cont2.list1[0].list4[0].leaf8", "abc"); + params.put("test-yang_cont1.cont2.list1[0].list4[1].leaf8", "abc"); + params.put("test-yang_cont1.cont2.list1[0].list5[0].leaf9", "abc"); + params.put("test-yang_cont1.cont2.list1[0].list5[1].leaf9", "abc"); + params.put("test-yang_cont1.cont2.list1[1].leaf1", "abc"); + params.put("test-yang_cont1.cont2.list1[1].leaf2", "abc"); + params.put("test-yang_cont1.cont2.list1[1].leaf3", "abc"); + params.put("test-yang_cont1.cont2.list1[1].ll1[0]", "abc"); + params.put("test-yang_cont1.cont2.list1[1].ll1[1]", "abc"); + params.put("test-yang_cont1.cont2.list1[1].ll2[0]", "abc"); + params.put("test-yang_cont1.cont2.list1[1].ll2[1]", "abc"); + params.put("test-yang_cont1.cont2.list1[1].cont4.leaf11", "abc"); + params.put("test-yang_cont1.cont2.list1[1].list4[0].leaf8", "abc"); + params.put("test-yang_cont1.cont2.list1[1].list4[1].leaf8", "abc"); + params.put("test-yang_cont1.cont2.list1[1].list5[0].leaf9", "abc"); + params.put("test-yang_cont1.cont2.list1[1].list5[1].leaf9", "abc"); + params.put("test-yang_cont1.cont2.list2[0].leaf4", "abc"); + params.put("test-yang_cont1.cont2.list2[1].leaf4", "abc"); + params.put("test-yang_cont1.cont2.leaf5", "abc"); + params.put("test-yang_cont1.cont2.leaf6", "abc"); + params.put("test-yang_cont1.cont2.ll3[0]", "abc"); + params.put("test-yang_cont1.cont2.ll3[1]", "abc"); + params.put("test-yang_cont1.cont2.ll4[0]", "abc"); + params.put("test-yang_cont1.cont2.ll4[1]", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -283,18 +282,18 @@ public final class PropertiesSerializerTest { public void testAugment() throws SvcLogicException { String uri = "test-yang:cont1/cont2"; Map params = new HashMap<>(); - params.put("test-yang:cont1.cont2.cont4.leaf10", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:cont5.leaf13", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:list7[0].leaf14", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:list7[1].leaf14", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:leaf15", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:ll6[0]", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:ll6[1]", "abc"); - params.put("test-yang:cont1.cont2.list6[0].leaf11", "abc"); - params.put("test-yang:cont1.cont2.list6[1].leaf11", "abc"); - params.put("test-yang:cont1.cont2.leaf12", "abc"); - params.put("test-yang:cont1.cont2.ll5[0]", "abc"); - params.put("test-yang:cont1.cont2.ll5[1]", "abc"); + params.put("test-yang_cont1.cont2.cont4.leaf10", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_cont5.leaf13", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_list7[0].leaf14", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_list7[1].leaf14", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_leaf15", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_ll6[0]", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_ll6[1]", "abc"); + params.put("test-yang_cont1.cont2.list6[0].leaf11", "abc"); + params.put("test-yang_cont1.cont2.list6[1].leaf11", "abc"); + params.put("test-yang_cont1.cont2.leaf12", "abc"); + params.put("test-yang_cont1.cont2.ll5[0]", "abc"); + params.put("test-yang_cont1.cont2.ll5[1]", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -397,12 +396,12 @@ public final class PropertiesSerializerTest { public void testChoiceCase1() throws SvcLogicException { String uri = "test-yang:cont8"; Map params = new HashMap<>(); - params.put("test-yang:cont8.cont6.leaf16", "abc"); - params.put("test-yang:cont8.list8[0].leaf18", "abc"); - params.put("test-yang:cont8.list8[1].leaf18", "abc"); - params.put("test-yang:cont8.leaf19", "abc"); - params.put("test-yang:cont8.ll7[0]", "abc"); - params.put("test-yang:cont8.ll7[1]", "abc"); + params.put("test-yang_cont8.cont6.leaf16", "abc"); + params.put("test-yang_cont8.list8[0].leaf18", "abc"); + params.put("test-yang_cont8.list8[1].leaf18", "abc"); + params.put("test-yang_cont8.leaf19", "abc"); + params.put("test-yang_cont8.ll7[0]", "abc"); + params.put("test-yang_cont8.ll7[1]", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -459,9 +458,9 @@ public final class PropertiesSerializerTest { public void testChoiceCase2() throws SvcLogicException { String uri = "test-yang:cont9"; Map params = new HashMap<>(); - params.put("test-yang:cont9.leaf20", "abc"); - params.put("test-yang:cont9.ll8[0]", "abc"); - params.put("test-yang:cont9.cont11.leaf25", "abc"); + params.put("test-yang_cont9.leaf20", "abc"); + params.put("test-yang_cont9.ll8[0]", "abc"); + params.put("test-yang_cont9.cont11.leaf25", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -499,7 +498,7 @@ public final class PropertiesSerializerTest { public void testChoiceCase3() throws SvcLogicException { String uri = "test-yang:cont8/cont6"; Map params = new HashMap<>(); - params.put("test-yang:cont8.cont6.test-augment:leaf21", "abc"); + params.put("test-yang_cont8.cont6.test-augment_leaf21", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -538,10 +537,10 @@ public final class PropertiesSerializerTest { public void testGrouping() throws SvcLogicException { String uri = "test-yang:cont13"; Map params = new HashMap<>(); - params.put("test-yang:cont13.cont12.leaf26", "abc"); - params.put("test-yang:cont13.list9[0].leaf27", "abc"); - params.put("test-yang:cont13.leaf28", "abc"); - params.put("test-yang:cont13.ll9[0]", "abc"); + params.put("test-yang_cont13.cont12.leaf26", "abc"); + params.put("test-yang_cont13.list9[0].leaf27", "abc"); + params.put("test-yang_cont13.leaf28", "abc"); + params.put("test-yang_cont13.ll9[0]", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -587,11 +586,11 @@ public final class PropertiesSerializerTest { public void testGrouping2() throws SvcLogicException { String uri = "test-yang:cont9/cont11"; Map params = new HashMap<>(); - params.put("test-yang:cont9.cont11.leaf25", "abc"); - params.put("test-yang:cont9.cont11.cont13.cont12.leaf26", "abc"); - params.put("test-yang:cont9.cont11.cont13.list9[0].leaf27", "abc"); - params.put("test-yang:cont9.cont11.cont13.leaf28", "abc"); - params.put("test-yang:cont9.cont11.cont13.ll9[0]", "abc"); + params.put("test-yang_cont9.cont11.leaf25", "abc"); + params.put("test-yang_cont9.cont11.cont13.cont12.leaf26", "abc"); + params.put("test-yang_cont9.cont11.cont13.list9[0].leaf27", "abc"); + params.put("test-yang_cont9.cont11.cont13.leaf28", "abc"); + params.put("test-yang_cont9.cont11.cont13.ll9[0]", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( @@ -642,10 +641,10 @@ public final class PropertiesSerializerTest { public void testGrouping3() throws SvcLogicException { String uri = "test-augment:cont13"; Map params = new HashMap<>(); - params.put("test-augment:cont13.cont12.leaf26", "abc"); - params.put("test-augment:cont13.list9[0].leaf27", "abc"); - params.put("test-augment:cont13.leaf28", "abc"); - params.put("test-augment:cont13.ll9[0]", "abc"); + params.put("test-augment_cont13.cont12.leaf26", "abc"); + params.put("test-augment_cont13.list9[0].leaf27", "abc"); + params.put("test-augment_cont13.leaf28", "abc"); + params.put("test-augment_cont13.ll9[0]", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); PropertiesNodeSerializer ser = new MdsalPropertiesNodeSerializer( @@ -693,10 +692,10 @@ public final class PropertiesSerializerTest { public void testGrouping4() throws SvcLogicException { String uri = "test-yang:cont1/cont2/cont4"; Map params = new HashMap<>(); - params.put("test-yang:cont1.cont2.cont4.test-augment:cont13.cont12.leaf26", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:cont13.list9[0].leaf27", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:cont13.leaf28", "abc"); - params.put("test-yang:cont1.cont2.cont4.test-augment:cont13.ll9[0]", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_cont13.cont12.leaf26", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_cont13.list9[0].leaf27", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_cont13.leaf28", "abc"); + params.put("test-yang_cont1.cont2.cont4.test-augment_cont13.ll9[0]", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -755,16 +754,16 @@ public final class PropertiesSerializerTest { public void testRpcInput() throws SvcLogicException { String uri = "test-yang:create-sfc"; Map params = new HashMap<>(); - params.put("test-yang:create-sfc.input.cont14.leaf28", "abc"); - params.put("test-yang:create-sfc.input.list10[0].leaf29", "abc"); - params.put("test-yang:create-sfc.input.leaf30", "abc"); - params.put("test-yang:create-sfc.input.ll10[0]", "abc"); - params.put("test-yang:create-sfc.input.cont15.leaf31", "abc"); - params.put("test-yang:create-sfc.input.cont13.cont12.leaf26", "abc"); - params.put("test-yang:create-sfc.input.cont13.list9[0].leaf27", "abc"); - params.put("test-yang:create-sfc.input.cont13.leaf28", "abc"); - params.put("test-yang:create-sfc.input.cont13.ll9[0]", "abc"); - params.put("test-yang:create-sfc.input.test-augment:leaf36", "abc"); + params.put("test-yang_create-sfc.input.cont14.leaf28", "abc"); + params.put("test-yang_create-sfc.input.list10[0].leaf29", "abc"); + params.put("test-yang_create-sfc.input.leaf30", "abc"); + params.put("test-yang_create-sfc.input.ll10[0]", "abc"); + params.put("test-yang_create-sfc.input.cont15.leaf31", "abc"); + params.put("test-yang_create-sfc.input.cont13.cont12.leaf26", "abc"); + params.put("test-yang_create-sfc.input.cont13.list9[0].leaf27", "abc"); + params.put("test-yang_create-sfc.input.cont13.leaf28", "abc"); + params.put("test-yang_create-sfc.input.cont13.ll9[0]", "abc"); + params.put("test-yang_create-sfc.input.test-augment_leaf36", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -869,16 +868,16 @@ public final class PropertiesSerializerTest { public void testRpcOutput() throws SvcLogicException { String uri = "test-yang:create-sfc"; Map params = new HashMap<>(); - params.put("test-yang:create-sfc.output.cont16.leaf32", "abc"); - params.put("test-yang:create-sfc.output.list11[0].leaf33", "abc"); - params.put("test-yang:create-sfc.output.leaf34", "abc"); - params.put("test-yang:create-sfc.output.ll11[0]", "abc"); - params.put("test-yang:create-sfc.output.cont17.leaf35", "abc"); - params.put("test-yang:create-sfc.output.cont13.cont12.leaf26", "abc"); - params.put("test-yang:create-sfc.output.cont13.list9[0].leaf27", "abc"); - params.put("test-yang:create-sfc.output.cont13.leaf28", "abc"); - params.put("test-yang:create-sfc.output.cont13.ll9[0]", "abc"); - params.put("test-yang:create-sfc.output.test-augment:leaf37", "abc"); + params.put("test-yang_create-sfc.output.cont16.leaf32", "abc"); + params.put("test-yang_create-sfc.output.list11[0].leaf33", "abc"); + params.put("test-yang_create-sfc.output.leaf34", "abc"); + params.put("test-yang_create-sfc.output.ll11[0]", "abc"); + params.put("test-yang_create-sfc.output.cont17.leaf35", "abc"); + params.put("test-yang_create-sfc.output.cont13.cont12.leaf26", "abc"); + params.put("test-yang_create-sfc.output.cont13.list9[0].leaf27", "abc"); + params.put("test-yang_create-sfc.output.cont13.leaf28", "abc"); + params.put("test-yang_create-sfc.output.cont13.ll9[0]", "abc"); + params.put("test-yang_create-sfc.output.test-augment_leaf37", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -983,10 +982,10 @@ public final class PropertiesSerializerTest { public void testContainerSameName() throws SvcLogicException { String uri = "test-yang:cont18"; Map params = new HashMap<>(); - params.put("test-yang:cont18.cont18.list12[0].list12[0].leaf36", "abc"); - params.put("test-yang:cont18.cont18.list12[0].leaf36", "hi"); - params.put("test-yang:cont18.cont18.list12[1].list12[0].leaf36", "xyz"); - params.put("test-yang:cont18.cont18.list12[1].list12[1].leaf36", "hey!"); + params.put("test-yang_cont18.cont18.list12[0].list12[0].leaf36", "abc"); + params.put("test-yang_cont18.cont18.list12[0].leaf36", "hi"); + params.put("test-yang_cont18.cont18.list12[1].list12[0].leaf36", "xyz"); + params.put("test-yang_cont18.cont18.list12[1].list12[1].leaf36", "hey!"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -1055,9 +1054,9 @@ public final class PropertiesSerializerTest { public void testPropertiesWithoutSchema() throws SvcLogicException { String uri = "test-yang:cont18"; Map params = new HashMap<>(); - params.put("test-yang:cont18.leaf40", "abc"); + params.put("test-yang_cont18.leaf40", "abc"); params.put("leaf41", "hi"); - params.put("test-yang:cont18.leaf41", "abc"); + params.put("test-yang_cont18.leaf41", "abc"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); @@ -1075,11 +1074,12 @@ public final class PropertiesSerializerTest { public void testIdentityRef() throws SvcLogicException { String uri = "identity-test:test"; Map params = new HashMap<>(); - params.put("identity-test:test.con1.interface", "identity-types:physical"); - params.put("identity-test:test.con1.interfaces.int-list[0].iden", "identity-test:Giga"); - params.put("identity-test:test.con1.interfaces.int-list[0].available.ll[0]", "identity-types:Loopback"); - params.put("identity-test:test.con1.interfaces.int-list[0].available.leaf1", "identity-types-second:Ethernet"); - params.put("identity-test:test.con1.interfaces.int-list[0].available.leaf2", "identity-types-second:iden2"); + params.put("identity-test_test.con1.interface", "identity-types" + + ":physical"); + params.put("identity-test_test.con1.interfaces.int-list[0].iden", "identity-test:Giga"); + params.put("identity-test_test.con1.interfaces.int-list[0].available.ll[0]", "identity-types:Loopback"); + params.put("identity-test_test.con1.interfaces.int-list[0].available.leaf1", "identity-types-second:Ethernet"); + params.put("identity-test_test.con1.interfaces.int-list[0].available.leaf2", "identity-types-second:iden2"); InstanceIdentifierContext iCtx = ParserIdentifier .toInstanceIdentifier(uri, context, null); diff --git a/restconf-client/provider/src/test/resources/yang/test_augment_1_for_module.yang b/restconf-client/provider/src/test/resources/yang/test_augment_1_for_module.yang new file mode 100644 index 000000000..d2eeea7d1 --- /dev/null +++ b/restconf-client/provider/src/test/resources/yang/test_augment_1_for_module.yang @@ -0,0 +1,108 @@ +module test_augment_1_for_module { + yang-version 1; + namespace "urn:opendaylight:params:xml:ns:yang:test:augment:name"; + prefix "augment-name"; + + import test_name_of_the_module { + prefix aug; + } + + revision "2015-01-05" { + description "Initial revision of hello model"; + } + + augment "/aug:name_of_the_cont1/aug:name_of_the_cont2/aug:name_of_the_cont4" { + container name_of_the_cont5 { + leaf name_of_the_leaf13 { + type empty; + } + } + list name_of_the_list7 { + leaf name_of_the_leaf14 { + type instance-identifier; + } + } + leaf name_of_the_leaf15 { + type string; + } + leaf-list name_of_the_ll6 { + type union { + type int32; + type enumeration { + enum "unbounded"; + } + } + } + uses "aug:name_of_the_g1"; + } + + uses "aug:name_of_the_g1"; + + augment "/aug:name_of_the_cont1/aug:name_of_the_cont2/aug:name_of_the_cont3" { + choice name_of_the_ch2 { + case name_of_the_c3 { + leaf name_of_the_leaf21 { + type string; + } + } + case name_of_the_c4 { + leaf name_of_the_leaf22 { + type enumeration { + enum zero; + enum one; + enum seven { + value 7; + } + } + } + } + } + } + + augment "/aug:name_of_the_ch1" { + case name_of_the_c5 { + container name_of_the_cont10 { + leaf name_of_the_leaf23 { + type string; + } + } + } + } + + augment "/aug:name_of_the_ch1/aug:name_of_the_c1" { + container name_of_the_cont7 { + leaf name_of_the_leaf24 { + type string; + } + } + } + + augment "/aug:name_of_the_cont13/aug:name_of_the_cont12" { + leaf name_of_the_leaf29 { + type string; + } + } + + augment "/aug:name_of_the_create-sfc/aug:input" { + leaf name_of_the_leaf36 { + type bits { + bit angle { + position 0; + } + bit degree { + position 1; + } + bit movement { + position 2; + } + } + } + } + + augment "/aug:name_of_the_create-sfc/aug:output" { + leaf leaf37 { + type boolean; + } + } + +} \ No newline at end of file diff --git a/restconf-client/provider/src/test/resources/yang/test_name_of_the_module.yang b/restconf-client/provider/src/test/resources/yang/test_name_of_the_module.yang new file mode 100644 index 000000000..973475ee7 --- /dev/null +++ b/restconf-client/provider/src/test/resources/yang/test_name_of_the_module.yang @@ -0,0 +1,231 @@ +module test_name_of_the_module { + yang-version 1; + namespace "urn:opendaylight:params:xml:ns:yang:test:name"; + prefix "name"; + + revision "2015-01-05" { + description "Initial revision of hello model"; + } + + container name_of_the_cont1 { + container name_of_the_cont2 { + container name_of_the_cont3 { + leaf name_of_the_leaf10 { + type string; + } + } + list name_of_the_list1 { + key "name_of_the_leaf1 name_of_the_leaf2"; + leaf name_of_the_leaf1 { + type empty; + } + leaf name_of_the_leaf2 { + type string; + } + leaf name_of_the_leaf3 { + type string; + } + leaf-list name_of_the_ll1 { + type string; + } + leaf-list name_of_the_ll2 { + type string; + } + container name_of_the_cont4 { + leaf name_of_the_leaf11 { + type string; + } + } + list name_of_the_list4 { + leaf name_of_the_leaf8 { + type string; + } + } + list name_of_the_list5 { + leaf name_of_the_leaf9 { + type string; + } + } + } + list name_of_the_list2 { + leaf name_of_the_leaf4 { + type string; + } + } + leaf name_of_the_leaf5 { + type string; + } + leaf name_of_the_leaf6 { + type string; + } + leaf-list name_of_the_ll3 { + type string; + } + leaf-list name_of_the_ll4 { + type string; + } + } + } + + augment "/name_of_the_cont1/name_of_the_cont2" { + container name_of_the_cont4 { + leaf name_of_the_leaf10 { + type string; + } + } + list name_of_the_list6 { + leaf name_of_the_leaf11 { + type string; + } + } + leaf name_of_the_leaf12 { + type string; + } + leaf-list name_of_the_ll5 { + type string; + } + } + + choice name_of_the_ch1 { + case name_of_the_c1 { + container name_of_the_cont8 { + container name_of_the_cont6 { + leaf name_of_the_leaf16 { + type string; + } + } + list name_of_the_list8 { + leaf name_of_the_leaf18 { + type string; + } + } + leaf name_of_the_leaf19 { + type string; + } + leaf-list name_of_the_ll7 { + type string; + } + } + } + case name_of_the_c2 { + container name_of_the_cont9 { + leaf name_of_the_leaf20 { + type string; + } + leaf-list name_of_the_ll8 { + type string; + } + container name_of_the_cont11 { + choice name_of_the_ch3 { + case name_of_the_c1 { + leaf name_of_the_leaf25 { + type string; + } + uses name_of_the_g1; + } + } + } + } + } + } + + grouping name_of_the_g1 { + container name_of_the_cont13 { + container name_of_the_cont12 { + leaf name_of_the_leaf26 { + type string; + } + } + list name_of_the_list9 { + leaf name_of_the_leaf27 { + type string; + } + } + leaf name_of_the_leaf28 { + type string; + } + leaf-list name_of_the_ll9 { + type string; + } + } + } + + uses name_of_the_g1; + + rpc name_of_the_create-sfc { + input { + container name_of_the_cont14 { + leaf name_of_the_leaf28 { + type string; + } + } + list name_of_the_list10 { + leaf name_of_the_leaf29 { + type string; + } + } + leaf name_of_the_leaf30 { + type string; + } + leaf-list name_of_the_ll10 { + type string; + } + choice name_of_the_ch3 { + case name_of_the_c1 { + container name_of_the_cont15 { + leaf name_of_the_leaf31 { + type string; + } + } + } + } + uses name_of_the_g1; + } + output { + container name_of_the_cont16 { + leaf name_of_the_leaf32 { + type string; + } + } + list name_of_the_list11 { + leaf name_of_the_leaf33 { + type string; + } + } + leaf name_of_the_leaf34 { + type string; + } + leaf-list name_of_the_ll11 { + type string; + } + choice name_of_the_ch4 { + case name_of_the_c1 { + container name_of_the_cont17 { + leaf name_of_the_leaf35 { + type string; + } + } + } + } + uses name_of_the_g1; + } + } + + container name_of_the_cont18 { + container name_of_the_cont18 { + list name_of_the_list12 { + list name_of_the_list12 { + leaf name_of_the_leaf36 { + type string; + } + } + leaf name_of_the_leaf36 { + type string; + } + } + } + leaf name_of_the_leaf40 { + type string; + } + } +} \ No newline at end of file -- cgit 1.2.3-korg From 93c91c2fc80a31585c8f902c86bc4e1faf5b966d Mon Sep 17 00:00:00 2001 From: Vidyashree Rama Date: Tue, 23 Apr 2019 09:29:36 +0530 Subject: Test coverage imporvement and code refactoring of restconf discovery node. Test coverage imporvement and code refactoring of restconf discovery node. Issue-ID: CCSDK-1236 Change-Id: Ic44899d71b92be3d7b88be3abb0feb311e6ad8c0 Signed-off-by: Vidyashree Rama --- .../restconfdiscovery/RestconfDiscoveryNode.java | 48 +++++++++---------- .../TestRestconfDiscoveryNode.java | 56 ++++++++++++++++++---- 2 files changed, 72 insertions(+), 32 deletions(-) (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java index 5c7d695d9..cf69d7a3c 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/RestconfDiscoveryNode.java @@ -141,7 +141,7 @@ public class RestconfDiscoveryNode implements SvcLogicDiscoveryPlugin { WebTarget target = null; try { RestapiCallNode restapi = restconfApiCallNode.getRestapiCallNode(); - p = restapi.getParameters(paramMap, new Parameters()); + p = RestapiCallNode.getParameters(paramMap, new Parameters()); Client client = ignoreSslClient().register(SseFeature.class); target = restapi.addAuthType(client, p).target(url); } catch (SvcLogicException e) { @@ -166,37 +166,37 @@ public class RestconfDiscoveryNode implements SvcLogicDiscoveryPlugin { eventSource.close(); log.info("Closed connection to SSE source"); } - } - - private Client ignoreSslClient() { - SSLContext sslcontext = null; - try { - sslcontext = SSLContext.getInstance("TLS"); - sslcontext.init(null, new TrustManager[]{new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { - } + private Client ignoreSslClient() { + SSLContext sslcontext = null; - @Override - public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { - } + try { + sslcontext = SSLContext.getInstance("TLS"); + sslcontext.init(null, new TrustManager[]{new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + } }, new java.security.SecureRandom()); + } catch (NoSuchAlgorithmException | KeyManagementException e) { + throw new IllegalStateException(e); + } - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - } }, new java.security.SecureRandom()); - } catch (NoSuchAlgorithmException | KeyManagementException e) { - throw new IllegalStateException(e); + return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build(); } - - return ClientBuilder.newBuilder().sslContext(sslcontext).hostnameVerifier((s1, s2) -> true).build(); } protected String getTokenId(String customHttpHeaders) { if (customHttpHeaders.contains("=")) { - String s[] = customHttpHeaders.split("="); + String[] s = customHttpHeaders.split("="); return s[1]; } return customHttpHeaders; diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java index af354e9bf..aa89d67d7 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/restconfdiscovery/TestRestconfDiscoveryNode.java @@ -20,6 +20,7 @@ package org.onap.ccsdk.sli.plugins.restconfdiscovery; +import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; import org.glassfish.jersey.media.sse.SseFeature; import org.glassfish.jersey.server.ResourceConfig; @@ -29,29 +30,41 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode; import org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiCallNode; +import java.io.IOException; import java.net.URI; import java.util.HashMap; import java.util.Map; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; public class TestRestconfDiscoveryNode { private static final URI CONTEXT = URI.create("http://localhost:8080/"); @Test - public void testEstablishPersistentConnection() throws SvcLogicException, - InterruptedException { + public void sendRequest() throws SvcLogicException, InterruptedException, IOException { final ResourceConfig resourceConfig = new ResourceConfig( SseServerMock.class, SseFeature.class); - GrizzlyHttpServerFactory.createHttpServer(CONTEXT, resourceConfig); + HttpServer server = GrizzlyHttpServerFactory.createHttpServer(CONTEXT, + resourceConfig); + server.start(); + RestconfApiCallNode restconf = mock(RestconfApiCallNode.class); + doNothing().when(restconf) + .sendRequest(any(Map.class), any(SvcLogicContext.class)); + RestapiCallNode restApi = new RestapiCallNode(); + doReturn(restApi).when(restconf).getRestapiCallNode(); + SvcLogicContext ctx = new SvcLogicContext(); ctx.setAttribute("prop.encoding-json", "encoding-json"); ctx.setAttribute("restapi-result.response-code", "200"); ctx.setAttribute("restapi-result.ietf-subscribed-notifications" + ":establish-subscription.output.identifier", - "100"); + "89"); Map p = new HashMap<>(); p.put("sseConnectURL", "http://localhost:8080/events"); @@ -64,11 +77,15 @@ public class TestRestconfDiscoveryNode { "bv5itd6m1cftldpjarnyle3sdcqq9hftc4lebz464b5ffxmlbvg9"); p.put("restapiUrl", "https://localhost:8080/restconf/operations/" + "ietf-subscribed-notifications:establish-subscription"); - RestconfDiscoveryNode rdn = new RestconfDiscoveryNode( - new RestconfApiCallNode(new RestapiCallNode())); - rdn.establishPersistentConnection(p, ctx, "networkId"); - Thread.sleep(2000); + p.put("module", "testmodule"); + p.put("rpc", "testrpc"); + p.put("version", "1.0"); + p.put("mode", "sync"); + RestconfDiscoveryNode rdn = new RestconfDiscoveryNode(restconf); + rdn.establishSubscription(p, ctx); + Thread.sleep(1000); rdn.deleteSubscription(p, ctx); + server.shutdown(); } @Test(expected = SvcLogicException.class) @@ -132,4 +149,27 @@ public class TestRestconfDiscoveryNode { "emlqm4bbsmnar3vrtbyrzukbv5itd6m1cftldpjarnyle3sdcqq9h" + "ftc4lebz464b5ffxmlbvg9")); } + + @Test + public void testSubscriptionInfo() throws SvcLogicException { + SubscriptionInfo info = new SubscriptionInfo(); + info.subscriberId("network-id"); + info.subscriptionId("8"); + info.filterUrl("/ietf-interfaces:interfaces"); + info.yangFilePath("/opt/yang"); + SvcLogicGraphInfo svcLogicGraphInfo = new SvcLogicGraphInfo(); + svcLogicGraphInfo.mode("sync"); + svcLogicGraphInfo.module("testModule"); + svcLogicGraphInfo.rpc("testRpc"); + svcLogicGraphInfo.version("1.0"); + info.callBackDG(svcLogicGraphInfo); + assertThat(info.subscriberId(), is("network-id")); + assertThat(info.subscriptionId(), is("8")); + assertThat(info.filterUrl(), is("/ietf-interfaces:interfaces")); + assertThat(info.yangFilePath(), is("/opt/yang")); + assertThat(info.callBackDG().module(), is("testModule")); + assertThat(info.callBackDG().mode(), is("sync")); + assertThat(info.callBackDG().rpc(), is("testRpc")); + assertThat(info.callBackDG().version(), is("1.0")); + } } -- cgit 1.2.3-korg From bea0f101d0a4c7d1fbe8cb9d4a27491ba7d66dbf Mon Sep 17 00:00:00 2001 From: janani Date: Thu, 23 May 2019 15:45:14 +0530 Subject: Bug fix to add anyxml node. Anyxml node fix Change-Id: Ice40fde63ac8f589fa9358ebaf12c32247b0ae96 Issue-ID: CCSDK-1344 Signed-off-by: janani --- .../restconfapicall/RestconfApiCallNode.java | 35 +++++- .../plugins/restconfapicall/RestconfApiUtils.java | 12 ++ .../dfserializer/JsonSerializer.java | 12 +- .../dfserializer/PropertiesNodeJsonListener.java | 28 +++-- .../pnserializer/DefaultPropertiesNodeWalker.java | 5 +- .../yangserializers/pnserializer/InnerNode.java | 21 ++-- .../MdsalPropertiesNodeSerializer.java | 21 +++- .../pnserializer/MdsalPropertiesNodeUtils.java | 7 +- .../yangserializers/pnserializer/NodeType.java | 3 +- .../pnserializer/PropertiesNode.java | 20 ++++ .../dfserializer/DataFormatSerializerTest.java | 132 +++++++++++++++++++++ .../dfserializer/DataFormatUtilsTest.java | 64 ++++++++++ .../src/test/resources/yang/execution-service.yang | 43 +++++++ 13 files changed, 373 insertions(+), 30 deletions(-) create mode 100644 restconf-client/provider/src/test/resources/yang/execution-service.yang (limited to 'restconf-client/provider/src/test/java') diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java index 3007b5a1b..ac0897e8f 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java @@ -43,10 +43,12 @@ import org.dom4j.DocumentHelper; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; +import org.onap.ccsdk.sli.plugins.restapicall.Format; import org.onap.ccsdk.sli.plugins.restapicall.HttpResponse; import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode; import org.onap.ccsdk.sli.plugins.restapicall.RetryException; import org.onap.ccsdk.sli.plugins.restapicall.RetryPolicy; +import org.onap.ccsdk.sli.plugins.restapicall.XmlParser; import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatSerializer; import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatSerializerContext; import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerFactory; @@ -195,10 +197,14 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { String response = getResponse(ctx, p, pp, r); if (response != null) { - Map resProp = serializeResponse( - p, uri, response, insIdCtx); - for (Map.Entry pro : resProp.entrySet()) { - ctx.setAttribute(pro.getKey(), pro.getValue()); + try { + Map resProp = serializeResponse( + p, uri, response, insIdCtx); + for (Map.Entry pro : resProp.entrySet()) { + ctx.setAttribute(pro.getKey(), pro.getValue()); + } + } catch (SvcLogicException e) { + convertToNormalRes(ctx, p, pp, response); } } } catch (SvcLogicException e) { @@ -241,6 +247,27 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { } } + private void convertToNormalRes(SvcLogicContext ctx , + YangParameters p, String pp, String body) + throws SvcLogicException { + if (p.convertResponse) { + Map mm = null; + if (p.format == Format.XML) { + mm = XmlParser.convertToProperties(body, p.listNameList); + } else if (p.format == Format.JSON) { + mm = org.onap.ccsdk.sli.plugins.restapicall.JsonParser + .convertToProperties(body); + } + + if (mm != null) { + for (Map.Entry entry : mm.entrySet()) { + ctx.setAttribute(pp + entry.getKey(), + entry.getValue()); + } + } + } + } + /** * Serializes the request message to JSON or XML from the properties. * diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java index 505089c44..1309102b3 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java @@ -167,6 +167,18 @@ public final class RestconfApiUtils { } secondHalf = path.substring(p[0].length() + 1); return firstHalf + COLON + secondHalf; + } else if (path.contains(SLASH)) { + String[] p = path.split(SLASH); + if (p.length > 4) { + String actual = p[3] + COLON + p[4]; + if (p.length > 5) { + secondHalf = path.substring( + p[1].length() + p[2].length() + actual.length() + 3); + path = actual + secondHalf; + } else { + path = actual; + } + } } return path; } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/JsonSerializer.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/JsonSerializer.java index 1be1309bb..91adb8126 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/JsonSerializer.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/JsonSerializer.java @@ -22,18 +22,20 @@ package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; -import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker; -import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode; -import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeWalker; import java.io.IOException; import java.io.Writer; import java.util.List; import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker; +import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode; +import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeWalker; + import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormat.JSON; -import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.*; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.JSON_LIS_ERR; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.JSON_TREE_ERR; /** * Representation of JSON serializer which encodes properties to JSON and diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeJsonListener.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeJsonListener.java index e51ccf7a6..0f03039e5 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeJsonListener.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/PropertiesNodeJsonListener.java @@ -21,6 +21,13 @@ package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer; import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Collection; +import java.util.Map; + import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.LeafNode; @@ -29,12 +36,6 @@ import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeListener; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.RootNode; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; -import java.util.Collection; -import java.util.Map; - import static com.google.common.base.Strings.repeat; import static java.lang.String.format; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.JSON_WRITE_ERR; @@ -116,6 +117,16 @@ public class PropertiesNodeJsonListener implements PropertiesNodeListener{ jsonWriter.value(val); break; + case ANY_XML_NODE: + jsonWriter.name(nodeName); + val = ((LeafNode) node).value(); + try { + jsonWriter.jsonValue(val); + } catch (IOException e) { + throw new SvcLogicException(JSON_WRITE_ERR, e); + } + break; + default: throw new SvcLogicException(format( NODE_TYPE_ERR, node.nodeType().toString())); @@ -143,6 +154,7 @@ public class PropertiesNodeJsonListener implements PropertiesNodeListener{ case SINGLE_INSTANCE_LEAF_NODE: case MULTI_INSTANCE_LEAF_NODE: + case ANY_XML_NODE: break; default: @@ -174,7 +186,9 @@ public class PropertiesNodeJsonListener implements PropertiesNodeListener{ PropertiesNode parent = node.parent(); if (parent instanceof RootNode || !parent.namespace().moduleName() .equals(node.namespace().moduleName())) { - return node.namespace().moduleName() + ":" + node.name(); + if (!parent.nonAppend()) { + return node.namespace().moduleName() + ":" + node.name(); + } } return node.name(); } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java index f23452653..5034530d5 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/DefaultPropertiesNodeWalker.java @@ -20,11 +20,11 @@ package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer; -import org.onap.ccsdk.sli.core.sli.SvcLogicException; - import java.util.Collection; import java.util.Map; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + /** * Implementation of properties node walker which helps in forming a new tree from properties node. * @@ -110,6 +110,7 @@ public class DefaultPropertiesNodeWalker implements Propert .children()); case SINGLE_INSTANCE_LEAF_NODE: case MULTI_INSTANCE_LEAF_NODE: + case ANY_XML_NODE: return null; default: throw new IllegalArgumentException("No more types allowed"); diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java index 0711a7cff..1cf99b2d4 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/InnerNode.java @@ -20,13 +20,13 @@ package org.onap.ccsdk.sli.plugins.yangserializers.pnserializer; +import java.util.HashMap; +import java.util.Map; + import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import java.util.HashMap; -import java.util.Map; - import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.addToAugmentations; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.createNode; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getAugmentationNode; @@ -112,15 +112,18 @@ public abstract class InnerNode extends PropertiesNode { } AugmentationSchemaNode augSchema = null; - if (((DataSchemaNode) appInfo).isAugmenting()) { - augSchema = findCorrespondingAugment(((DataSchemaNode) this.appInfo()), - ((DataSchemaNode) appInfo)); - } - String uri = getUri(this, name, namespace); node = new LeafNode(name, namespace, uri, this, appInfo, type, value); - node.valueNs(valueNs); + + if (type != NodeType.ANY_XML_NODE) { + if (((DataSchemaNode) appInfo).isAugmenting()) { + augSchema = findCorrespondingAugment( + ((DataSchemaNode) this.appInfo()), + ((DataSchemaNode) appInfo)); + } + node.valueNs(valueNs); + } if (augSchema != null && !isNamespaceAsParent(this, node)) { addToAugmentations(augSchema, this, node); 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 0eca40d06..8a6e75668 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 @@ -44,6 +44,7 @@ import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPrope import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getRevision; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getValueNamespace; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.resolveName; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.ANY_XML_NODE; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE; import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE; @@ -81,6 +82,8 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer entry : paramMap.entrySet()) { String[] names = entry.getKey().split("\\."); for (int i = 0; i < names.length; i++) { @@ -97,6 +100,15 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer paramMap, + String rootUri) { + String isReqStr = rootUri + "." + "isNonAppend"; + String val = paramMap.get(isReqStr); + if (val != null && val.equals("true")) { + node.nonAppend(true); + } + } + /** * Converts all the params in the svc logic context into a valid param by * replacing the underscore in module name to colon at necessary places. @@ -150,7 +162,7 @@ public class MdsalPropertiesNodeSerializer extends PropertiesNodeSerializer augmentations = ArrayListMultimap.create(); /** @@ -106,6 +107,16 @@ public abstract class PropertiesNode { this.appInfo = appInfo; } + /** + * Sets to true if module name is required in forming a request; false + * otherwise. + * + * @param isNotReq true if required; false otherwise + */ + public void nonAppend(boolean isNotReq) { + this.nonAppend = isNotReq; + } + /** * Returns parent. * @@ -160,6 +171,15 @@ public abstract class PropertiesNode { return nodeType; } + /** + * Returns if module name is required. + * + * @return status of module name if required + */ + public boolean nonAppend() { + return nonAppend; + } + /** * Returns augmentations. * diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java index 45e8eb3f1..aa1e50d6a 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java @@ -46,8 +46,10 @@ import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PATCH; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.POST; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_ANYXML_RESPONSE; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_JSON_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_XML_RPC; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_ANYXML; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID_PUT; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_RPC; @@ -148,6 +150,24 @@ public class DataFormatSerializerTest { assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID)); } + /** + * Verifies encoding of parameters to JSON data format any xml in it. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeForAnyXml() throws SvcLogicException { + String pre = "execution-service_process."; + SvcLogicContext ctx = createAnyXmlAttList(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/api/v1/execution-service/process"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_ANYXML)); + } + /** * Verifies encoding of parameters to JSON data format with identity-ref * and inter-file linking for put operation-type. @@ -500,6 +520,27 @@ public class DataFormatSerializerTest { verifyAttListRpc(ctx, outPre); } + /** + * Verifies encoding of and decoding from, JSON for ANYXML. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void codecForNormalAnyXml() throws SvcLogicException { + createMockForDecode(DECODE_ANYXML_RESPONSE); + String inPre = "execution-service_process."; + SvcLogicContext ctx = createAnyXmlAttList(inPre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "post"); + p.put("responsePrefix", "pp"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/api/v1/execution-service/process"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_ANYXML)); + verifyOutputOfAnyXml(ctx); + } + /** * Verifies encoding of and decoding from, XML respectively for data * format with containers, grouping and augment. @@ -546,6 +587,12 @@ public class DataFormatSerializerTest { String url8 = "https://localhost:8282/restconf/data/" + actVal; String url9 = "http://182.2.61.24:2250/restconf/data/" + actVal; String url10 = "https://182.2.61.24:2250/restconf/operations/" + actVal; + String url11 = "https://182.2.61.24:2250/api/v1/execution-service" + + "/process"; + String url12 = "https://182.2.61.24:2250/api/v1/execution-service" + + "/process/payload"; + String url13 = "https://182.2.61.24:2250/api/v1/execution-service" + + "/process/payload/"; String val1 = parseUrl(url1, POST); String val2 = parseUrl(url2, GET); String val3 = parseUrl(url3, PATCH); @@ -556,6 +603,9 @@ public class DataFormatSerializerTest { String val8 = parseUrl(url8, POST); String val9 = parseUrl(url9, GET); String val10 = parseUrl(url10, POST); + String val11 = parseUrl(url11, POST); + String val12 = parseUrl(url12, POST); + String val13 = parseUrl(url13, POST); assertThat(val1, is(actVal)); assertThat(val2, is(actVal)); assertThat(val3, is(actVal)); @@ -566,6 +616,43 @@ public class DataFormatSerializerTest { assertThat(val8, is(actVal)); assertThat(val9, is(actVal)); assertThat(val10, is(actVal)); + assertThat(val11, is("execution-service:process")); + assertThat(val12, is("execution-service:process/payload")); + assertThat(val13, is("execution-service:process/payload/")); + } + + /** + * Creates attribute list for encoding JSON or XML with ANYXML YANG + * file. + * + * @param pre prefix + * @return service logic context + */ + private SvcLogicContext createAnyXmlAttList(String pre) { + SvcLogicContext ctx = new SvcLogicContext(); + String pre1 = pre + "commonHeader."; + String pre2 = pre + "actionIdentifiers."; + ctx.setAttribute(pre + "isNonAppend", "true"); + ctx.setAttribute(pre1 + "originatorId", "SDNC_DG"); + ctx.setAttribute(pre1 + "requestId", "123456-1000"); + ctx.setAttribute(pre1 + "subRequestId", "sub-123456-1000"); + ctx.setAttribute(pre2 + "blueprintName", + "baseconfiguration"); + ctx.setAttribute(pre2 + "blueprintVersion", "1.0.0"); + ctx.setAttribute(pre2 + "actionName", "assign-activate"); + ctx.setAttribute(pre2 + "mode", "sync"); + ctx.setAttribute(pre + "payload." + + "template-prefix", "vDNS-test"); + ctx.setAttribute(pre + "payload.resource-assignment-request" + + ".resource-assignment-properties", + "{\n" + + " \"service-instance-id\": " + + "\"1234\",\n" + + " \"vnf-id\": \"3526\",\n" + + " \"customer-name\": \"htipl\",\n" + + " \"subscriber-name\": \"huawei\"\n" + + " }"); + return ctx; } /** @@ -834,6 +921,51 @@ public class DataFormatSerializerTest { assertThat(ctx.getAttribute(pre + "cont13.leaf28"), is("abc")); } + /** + * Verifies the attribute list for decoding from JSON or XML with + * ANYXML YANG file. + * + * @param ctx service logic context + */ + private void verifyOutputOfAnyXml(SvcLogicContext ctx) { + System.out.println(ctx.getAttribute("pp.status.eventType")); + assertThat(ctx.getAttribute("pp.status.eventType"), is( + "EVENT_COMPONENT_EXECUTED")); + assertThat(ctx.getAttribute("pp.actionIdentifiers.blueprintName"), + is("golden")); + assertThat(ctx.getAttribute("pp.actionIdentifiers.mode"), + is("sync")); + assertThat(ctx.getAttribute("pp.stepData.name"), + is("resource-assignment")); + assertThat(ctx.getAttribute("pp.status.message"), + is("success")); + assertThat(ctx.getAttribute("pp.commonHeader.originatorId"), + is("System")); + assertThat(ctx.getAttribute("pp.status.code"), + is("200")); + assertThat(ctx.getAttribute("pp.commonHeader.requestId"), + is("1234")); + assertThat(ctx.getAttribute("pp.commonHeader.subRequestId"), + is("1234-12234")); + assertThat(ctx.getAttribute("pp.commonHeader.timestamp"), + is("2019-05-18T23:42:41.658Z")); + assertThat(ctx.getAttribute("pp.status.timestamp"), + is("2019-05-18T23:42:41.950Z")); + assertThat(ctx.getAttribute("pp.actionIdentifiers.blueprintV" + + "ersion"), is("1.0.0")); + assertThat(ctx.getAttribute("pp.actionIdentifiers.actionName"), + is("resource-assignment")); + assertThat(ctx.getAttribute("pp.payload.resource-assignment-resp" + + "onse.meshed-template.vf-module-1"), + is("\n This i" + + "s the Virtual Firewall entity\n" + + " 10.0.101.20/24\n" + + "")); + assertThat(ctx.getAttribute("pp.actionIdentifiers.actionName"), + is("resource-assignment")); + } + + /** * Captures the data format messages by mocking it, which can be used in * testing the value. diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java index a7814e10e..c1bb71985 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java @@ -69,6 +69,31 @@ public final class DataFormatUtilsTest { " \"identity-test:l\": \"abc\"\n" + "}"; + static final String ENCODE_TO_ANYXML = "{\n" + + " \"actionIdentifiers\": {\n" + + " \"mode\": \"sync\",\n" + + " \"blueprintName\": \"baseconfiguration\",\n" + + " \"blueprintVersion\": \"1.0.0\",\n" + + " \"actionName\": \"assign-activate\"\n" + + " },\n" + + " \"payload\": {\n" + + " \"template-prefix\": \"vDNS-test\",\n" + + " \"resource-assignment-request\": {\n" + + " \"resource-assignment-properties\": {\n" + + " \"service-instance-id\": \"1234\",\n" + + " \"vnf-id\": \"3526\",\n" + + " \"customer-name\": \"htipl\",\n" + + " \"subscriber-name\": \"huawei\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"commonHeader\": {\n" + + " \"subRequestId\": \"sub-123456-1000\",\n" + + " \"requestId\": \"123456-1000\",\n" + + " \"originatorId\": \"SDNC_DG\"\n" + + " }\n" + + "}"; + static final String ENCODE_TO_JSON_ID_PUT = "{\n" + " \"identity-test:test\": {\n" + " \"con1\": {" + addSpace(ENCODE_TO_JSON_ID_COMMON, 4) + @@ -485,6 +510,45 @@ public final class DataFormatUtilsTest { " }\n" + "}"; + static final String DECODE_ANYXML_RESPONSE = "{\n" + + " \"commonHeader\": {\n" + + " \"timestamp\": \"2019-05-18T23:42:41.658Z\",\n" + + " \"originatorId\": \"System\",\n" + + " \"requestId\": \"1234\",\n" + + " \"subRequestId\": \"1234-12234\",\n" + + " \"flags\": null\n" + + " },\n" + + " \"actionIdentifiers\": {\n" + + " \"blueprintName\": \"golden\",\n" + + " \"blueprintVersion\": \"1.0.0\",\n" + + " \"actionName\": \"resource-assignment\",\n" + + " \"mode\": \"sync\"\n" + + " },\n" + + " \"status\": {\n" + + " \"code\": 200,\n" + + " \"eventType\": \"EVENT_COMPONENT_EXECUTED\",\n" + + " \"timestamp\": \"2019-05-18T23:42:41.950Z\",\n" + + " \"errorMessage\": null,\n" + + " \"message\": \"success\"\n" + + " },\n" + + " \"payload\": {\n" + + " \"resource-assignment-response\": {\n" + + " \"meshed-template\": {\n" + + " \"vf-module-1\": \"\\n " + + " This is the Virtual Firewall entity\\n 10.0.101.20/24\\n\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"stepData\": {\n" + + " \"name\": \"resource-assignment\",\n" + + " \"properties\": {\n" + + " \"resource-assignment-params\": null,\n" + + " \"status\": null\n" + + " }\n" + + " }\n" + + "}"; + static final String ENCODE_TO_XML_RPC = "\n" + "\n" + diff --git a/restconf-client/provider/src/test/resources/yang/execution-service.yang b/restconf-client/provider/src/test/resources/yang/execution-service.yang new file mode 100644 index 000000000..d7cf68f12 --- /dev/null +++ b/restconf-client/provider/src/test/resources/yang/execution-service.yang @@ -0,0 +1,43 @@ +module execution-service { + yang-version 1.1; + namespace "cds:workflow:rest"; + prefix "cds"; + + revision "2019-05-21"; + + container process { + container commonHeader { + leaf originatorId { + type string; + } + leaf requestId { + type string; + } + leaf subRequestId { + type string; + } + } + container actionIdentifiers { + leaf blueprintName { + type string; + } + leaf blueprintVersion { + type string; + } + leaf actionName { + type string; + } + leaf mode { + type string; + } + } + container payload { + leaf-list template-prefix { + type string; + } + container resource-assignment-request { + anyxml resource-assignment-properties; + } + } + } +} -- cgit 1.2.3-korg