2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
 
   6  * ================================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
 
  23 import com.google.gson.stream.JsonWriter;
 
  24 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  25 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.DefaultPropertiesNodeWalker;
 
  26 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.LeafNode;
 
  27 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace;
 
  28 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNode;
 
  29 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeListener;
 
  30 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.RootNode;
 
  32 import java.io.IOException;
 
  33 import java.io.StringWriter;
 
  34 import java.io.Writer;
 
  35 import java.util.Collection;
 
  38 import static com.google.common.base.Strings.repeat;
 
  39 import static java.lang.String.format;
 
  40 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.JSON_WRITE_ERR;
 
  41 import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.NODE_TYPE_ERR;
 
  44  * Representation of JSON implementation of properties node listener.
 
  46 public class PropertiesNodeJsonListener implements PropertiesNodeListener{
 
  49      * JSON writer to write the JSON data format.
 
  51     private JsonWriter jsonWriter;
 
  54      * Writer to write the JSON.
 
  56     private Writer writer;
 
  59      * Creates the properties node JSON listener by instantiating and
 
  60      * indenting the writer.
 
  62     public PropertiesNodeJsonListener() {
 
  63         writer = new StringWriter();
 
  64         jsonWriter = new JsonWriter(writer);
 
  65         jsonWriter.setIndent(repeat(" ", 4));
 
  69     public void start(PropertiesNode node) throws SvcLogicException {
 
  71             jsonWriter.beginObject();
 
  72         } catch (IOException e) {
 
  73             throw new SvcLogicException(JSON_WRITE_ERR, e);
 
  78     public void end(PropertiesNode node) throws SvcLogicException {
 
  80             jsonWriter.endObject();
 
  82         } catch (IOException e) {
 
  83             throw new SvcLogicException(JSON_WRITE_ERR, e);
 
  88     public void enterPropertiesNode(PropertiesNode node)
 
  89             throws SvcLogicException {
 
  91         String nodeName = getNodeName(node);
 
  93             switch (node.nodeType()) {
 
  94                 case SINGLE_INSTANCE_NODE:
 
  95                     jsonWriter.name(nodeName);
 
  96                     jsonWriter.beginObject();
 
  99                 case MULTI_INSTANCE_NODE:
 
 100                     jsonWriter.beginObject();
 
 103                 case SINGLE_INSTANCE_LEAF_NODE:
 
 104                     val = getValueWithNs((LeafNode) node);
 
 105                     jsonWriter.name(nodeName).value(val);
 
 108                 case MULTI_INSTANCE_HOLDER_NODE:
 
 109                 case MULTI_INSTANCE_LEAF_HOLDER_NODE:
 
 110                     jsonWriter.name(nodeName);
 
 111                     jsonWriter.beginArray();
 
 114                 case MULTI_INSTANCE_LEAF_NODE:
 
 115                     val = getValueWithNs((LeafNode) node);
 
 116                     jsonWriter.value(val);
 
 120                     throw new SvcLogicException(format(
 
 121                             NODE_TYPE_ERR, node.nodeType().toString()));
 
 124         } catch (IOException e) {
 
 125             throw new SvcLogicException(JSON_WRITE_ERR, e);
 
 130     public void exitPropertiesNode(PropertiesNode node) throws SvcLogicException {
 
 131         walkAugmentationNode(node);
 
 133             switch (node.nodeType()) {
 
 134                 case SINGLE_INSTANCE_NODE:
 
 135                 case MULTI_INSTANCE_NODE:
 
 136                     jsonWriter.endObject();
 
 139                 case MULTI_INSTANCE_HOLDER_NODE:
 
 140                 case MULTI_INSTANCE_LEAF_HOLDER_NODE:
 
 141                     jsonWriter.endArray();
 
 144                 case  SINGLE_INSTANCE_LEAF_NODE:
 
 145                 case MULTI_INSTANCE_LEAF_NODE:
 
 149                     throw new SvcLogicException(format(
 
 150                             NODE_TYPE_ERR, node.nodeType().toString()));
 
 152         } catch (IOException e) {
 
 153             throw new SvcLogicException(JSON_WRITE_ERR, e);
 
 158      * Returns the writer.
 
 162     public Writer getWriter() {
 
 167      * Returns the abstract JSON node name to be used in JSON data format
 
 168      * from the properties node.
 
 170      * @param node properties node
 
 171      * @return abstract JSON node
 
 173     private String getNodeName(PropertiesNode node) {
 
 174         PropertiesNode parent = node.parent();
 
 175         if (parent instanceof RootNode || !parent.namespace().moduleName()
 
 176                 .equals(node.namespace().moduleName())) {
 
 177             return node.namespace().moduleName() + ":" + node.name();
 
 183      * Returns the value of JSON leaf node with module name if required.
 
 185      * @param node properties node
 
 186      * @return value with namespace
 
 188     private String getValueWithNs(LeafNode node) {
 
 189         Namespace valNs = node.valueNs();
 
 190         String modName = (valNs == null) ? null : valNs.moduleName();
 
 191         if (modName != null) {
 
 192             return modName + ":" + node.value();
 
 198      * Gets all the augmentation of the given node and walks through it.
 
 200      * @param node properties node
 
 201      * @throws SvcLogicException when walking the properties node fails
 
 203     private void walkAugmentationNode(PropertiesNode node)
 
 204             throws SvcLogicException {
 
 205         for (Map.Entry<Object, Collection<PropertiesNode>>
 
 206                 augToChild : node.augmentations().asMap().entrySet()) {
 
 207             Collection<PropertiesNode> augChild = augToChild.getValue();
 
 208             if (!augChild.isEmpty()) {
 
 209                 DefaultPropertiesNodeWalker walker = new
 
 210                         DefaultPropertiesNodeWalker();
 
 211                 for (PropertiesNode p : augChild) {
 
 212                     enterPropertiesNode(p);
 
 213                     walker.walkChildNode(this, p);
 
 214                     exitPropertiesNode(p);