Added support for list, leaf-list and container in output under rpc node in yang...
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / attributes / toxml / ObjectXmlWriter.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.toxml;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import org.opendaylight.controller.config.yangjmxgenerator.attribute.*;
14 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
15 import org.w3c.dom.Document;
16
17 import javax.management.openmbean.ArrayType;
18 import javax.management.openmbean.SimpleType;
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 public class ObjectXmlWriter extends AttributeIfcSwitchStatement<AttributeWritingStrategy> {
23
24     private Document document;
25     private String key;
26
27     public Map<String, AttributeWritingStrategy> prepareWriting(Map<String, AttributeIfc> yangToAttrConfig,
28             Document document) {
29
30         Map<String, AttributeWritingStrategy> preparedWriting = Maps.newHashMap();
31
32         for (Entry<String, AttributeIfc> mappedAttributeEntry : yangToAttrConfig.entrySet()) {
33             String key = mappedAttributeEntry.getKey();
34             AttributeIfc value = mappedAttributeEntry.getValue();
35             AttributeWritingStrategy strat = prepareWritingStrategy(key, value, document);
36             preparedWriting.put(key, strat);
37         }
38
39         return preparedWriting;
40     }
41
42     public AttributeWritingStrategy prepareWritingStrategy(String key, AttributeIfc expectedAttr, Document document) {
43         Preconditions.checkNotNull(expectedAttr, "Mbean attributes mismatch, unable to find expected attribute for %s",
44                 key);
45         this.document = document;
46         this.key = key;
47         return switchAttribute(expectedAttr);
48     }
49
50     @Override
51     protected AttributeWritingStrategy caseJavaAttribute(JavaAttribute attributeIfc) {
52
53         if (attributeIfc.getOpenType() instanceof SimpleType<?>)
54             return new SimpleAttributeWritingStrategy(document, key);
55         else if (attributeIfc.getOpenType() instanceof ArrayType<?>) {
56             AttributeWritingStrategy innerStrategy = new SimpleAttributeWritingStrategy(document, key);
57             return new ArrayAttributeWritingStrategy(innerStrategy);
58         }
59         throw new IllegalStateException(JavaAttribute.class + " can only provide open type " + SimpleType.class
60                 + " or " + ArrayType.class);
61     }
62
63     @Override
64     protected AttributeWritingStrategy caseDependencyAttribute(DependencyAttribute attributeIfc) {
65         return new ObjectNameAttributeWritingStrategy(document, key);
66     }
67
68     @Override
69     protected AttributeWritingStrategy caseTOAttribute(TOAttribute attributeIfc) {
70         Map<String, AttributeWritingStrategy> innerStrats = Maps.newHashMap();
71         String currentKey = key;
72         for (Entry<String, AttributeIfc> innerAttrEntry : attributeIfc.getYangPropertiesToTypesMap().entrySet()) {
73
74             AttributeWritingStrategy innerStrategy = prepareWritingStrategy(innerAttrEntry.getKey(),
75                     innerAttrEntry.getValue(), document);
76             innerStrats.put(innerAttrEntry.getKey(), innerStrategy);
77         }
78
79         return new CompositeAttributeWritingStrategy(document, currentKey, innerStrats);
80     }
81
82     @Override
83     protected AttributeWritingStrategy caseListAttribute(ListAttribute attributeIfc) {
84         AttributeIfc inner = attributeIfc.getInnerAttribute();
85         AttributeWritingStrategy innerStrategy = prepareWritingStrategy(key, inner, document);
86         return new ArrayAttributeWritingStrategy(innerStrategy);
87     }
88
89 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.