3eb6fcebf1652441e6c5318097188a458ba102b8
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / toxml / ObjectXmlWriter.java
1 /*
2  * Copyright (c) 2015 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.config.facade.xml.mapping.attributes.toxml;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import javax.management.openmbean.ArrayType;
16 import javax.management.openmbean.CompositeType;
17 import javax.management.openmbean.OpenType;
18 import javax.management.openmbean.SimpleType;
19 import org.opendaylight.controller.config.facade.xml.mapping.attributes.AttributeIfcSwitchStatement;
20 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
21 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
22 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
23 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
24 import org.w3c.dom.Document;
25
26 public class ObjectXmlWriter extends AttributeIfcSwitchStatement<AttributeWritingStrategy> {
27
28     private Document document;
29     private String key;
30
31     public Map<String, AttributeWritingStrategy> prepareWriting(Map<String, AttributeIfc> yangToAttrConfig,
32             Document document) {
33
34         Map<String, AttributeWritingStrategy> preparedWriting = Maps.newHashMap();
35
36         for (Entry<String, AttributeIfc> mappedAttributeEntry : yangToAttrConfig.entrySet()) {
37             String key = mappedAttributeEntry.getKey();
38             AttributeIfc value = mappedAttributeEntry.getValue();
39             AttributeWritingStrategy strat = prepareWritingStrategy(key, value, document);
40             preparedWriting.put(key, strat);
41         }
42
43         return preparedWriting;
44     }
45
46     public AttributeWritingStrategy prepareWritingStrategy(String key, AttributeIfc expectedAttr, Document document) {
47         Preconditions.checkNotNull(expectedAttr, "Mbean attributes mismatch, unable to find expected attribute for %s",
48                 key);
49         this.document = document;
50         this.key = key;
51         return switchAttribute(expectedAttr);
52     }
53
54     @Override
55     protected AttributeWritingStrategy caseJavaBinaryAttribute(OpenType<?> openType) {
56         return new SimpleBinaryAttributeWritingStrategy(document, key);
57     }
58
59     @Override
60     protected AttributeWritingStrategy caseJavaEnumAttribute(final OpenType<?> openType) {
61         return new SimpleAttributeWritingStrategy(document, key);
62     }
63
64     @Override
65     protected AttributeWritingStrategy caseJavaSimpleAttribute(SimpleType<?> openType) {
66         return new SimpleAttributeWritingStrategy(document, key);
67     }
68
69     @Override
70     protected AttributeWritingStrategy caseJavaArrayAttribute(ArrayType<?> openType) {
71         AttributeWritingStrategy innerStrategy = new SimpleAttributeWritingStrategy(document, key);
72         return new ArrayAttributeWritingStrategy(innerStrategy);
73     }
74
75     @Override
76     protected AttributeWritingStrategy caseJavaIdentityRefAttribute(OpenType<?> openType) {
77         return new SimpleIdentityRefAttributeWritingStrategy(document, key);
78     }
79
80     @Override
81     protected AttributeWritingStrategy caseJavaCompositeAttribute(CompositeType openType) {
82         return new SimpleCompositeAttributeWritingStrategy(document, key);
83     }
84
85     @Override
86     protected AttributeWritingStrategy caseJavaUnionAttribute(OpenType<?> openType) {
87         return new SimpleUnionAttributeWritingStrategy(document, key);
88     }
89
90     @Override
91     protected AttributeWritingStrategy caseDependencyAttribute(SimpleType<?> openType) {
92         return new ObjectNameAttributeWritingStrategy(document, key);
93     }
94
95     @Override
96     protected AttributeWritingStrategy caseTOAttribute(CompositeType openType) {
97         Preconditions.checkState(getLastAttribute() instanceof TOAttribute);
98
99         Map<String, AttributeWritingStrategy> innerStrats = Maps.newHashMap();
100         String currentKey = key;
101         for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute) getLastAttribute()).getYangPropertiesToTypesMap().entrySet()) {
102
103             AttributeWritingStrategy innerStrategy = prepareWritingStrategy(innerAttrEntry.getKey(),
104                     innerAttrEntry.getValue(), document);
105             innerStrats.put(innerAttrEntry.getKey(), innerStrategy);
106         }
107
108         return new CompositeAttributeWritingStrategy(document, currentKey, innerStrats);
109     }
110
111     @Override
112     protected AttributeWritingStrategy caseListAttribute(ArrayType<?> openType) {
113         Preconditions.checkState(getLastAttribute() instanceof ListAttribute);
114         AttributeIfc innerAttribute = ((ListAttribute) getLastAttribute()).getInnerAttribute();
115
116         AttributeWritingStrategy innerStrategy = prepareWritingStrategy(key, innerAttribute, document);
117         return new ArrayAttributeWritingStrategy(innerStrategy);
118     }
119
120     @Override
121     protected AttributeWritingStrategy caseListDependeciesAttribute(ArrayType<?> openType) {
122         Preconditions.checkState(getLastAttribute() instanceof ListDependenciesAttribute);
123         AttributeWritingStrategy innerStrategy = caseDependencyAttribute(SimpleType.OBJECTNAME);
124         return new ArrayAttributeWritingStrategy(innerStrategy);
125     }
126
127 }

©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.