2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.mapping;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
14 import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
15 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
16 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
17 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.AttributeIfcSwitchStatement;
19 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.config.ServiceRegistryWrapper;
21 import javax.management.openmbean.ArrayType;
22 import javax.management.openmbean.CompositeType;
23 import javax.management.openmbean.OpenType;
24 import javax.management.openmbean.SimpleType;
26 import java.util.Map.Entry;
28 public class ObjectMapper extends AttributeIfcSwitchStatement<AttributeMappingStrategy<?, ? extends OpenType<?>>> {
30 private final ServiceRegistryWrapper dependencyTracker;
32 public ObjectMapper(ServiceRegistryWrapper depTracker) {
33 this.dependencyTracker = depTracker;
36 public Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> prepareMapping(
37 Map<String, AttributeIfc> configDefinition) {
38 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> strategies = Maps.newHashMap();
40 for (Entry<String, AttributeIfc> attrEntry : configDefinition.entrySet()) {
41 strategies.put(attrEntry.getKey(), prepareStrategy(attrEntry.getValue()));
47 public AttributeMappingStrategy<?, ? extends OpenType<?>> prepareStrategy(AttributeIfc attributeIfc) {
49 if(attributeIfc instanceof DependencyAttribute) {
50 serviceNameOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getLocalName();
51 namespaceOfDepAttr = ((DependencyAttribute)attributeIfc).getDependency().getSie().getQName().getNamespace().toString();
54 return switchAttribute(attributeIfc);
57 private Map<String, String> createJmxToYangMapping(TOAttribute attributeIfc) {
58 Map<String, String> retVal = Maps.newHashMap();
59 for (Entry<String, AttributeIfc> entry : attributeIfc.getJmxPropertiesToTypesMap().entrySet()) {
60 retVal.put(entry.getKey(), (entry.getValue()).getAttributeYangName());
66 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaSimpleAttribute(SimpleType<?> openType) {
67 return new SimpleAttributeMappingStrategy(openType);
71 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaArrayAttribute(ArrayType<?> openType) {
73 AttributeMappingStrategy<?, ? extends OpenType<?>> innerStrategy = new SimpleAttributeMappingStrategy(
74 (SimpleType<?>) openType.getElementOpenType());
75 return new ArrayAttributeMappingStrategy(openType, innerStrategy);
79 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaCompositeAttribute(CompositeType openType) {
80 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
82 Map<String, String> attributeMapping = Maps.newHashMap();
84 for (String innerAttributeKey : openType.keySet()) {
86 innerStrategies.put(innerAttributeKey, caseJavaAttribute(openType.getType(innerAttributeKey)));
87 attributeMapping.put(innerAttributeKey, innerAttributeKey);
90 return new CompositeAttributeMappingStrategy(openType, innerStrategies, attributeMapping);
94 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseJavaUnionAttribute(OpenType<?> openType) {
95 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
97 Map<String, String> attributeMapping = Maps.newHashMap();
99 CompositeType compositeType = (CompositeType) openType;
100 for (String innerAttributeKey : compositeType.keySet()) {
102 innerStrategies.put(innerAttributeKey, caseJavaAttribute(compositeType.getType(innerAttributeKey)));
103 attributeMapping.put(innerAttributeKey, innerAttributeKey);
106 return new UnionCompositeAttributeMappingStrategy(compositeType, innerStrategies, attributeMapping);
109 private String serviceNameOfDepAttr;
110 private String namespaceOfDepAttr;
113 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseDependencyAttribute(
114 SimpleType<?> openType) {
115 return new ObjectNameAttributeMappingStrategy(openType, dependencyTracker,
116 serviceNameOfDepAttr, namespaceOfDepAttr);
120 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseTOAttribute(CompositeType openType) {
121 Map<String, AttributeMappingStrategy<?, ? extends OpenType<?>>> innerStrategies = Maps.newHashMap();
123 Preconditions.checkState(lastAttribute instanceof TOAttribute);
124 TOAttribute lastTO = (TOAttribute) lastAttribute;
126 for (Entry<String, AttributeIfc> innerAttrEntry : ((TOAttribute)lastAttribute).getJmxPropertiesToTypesMap().entrySet()) {
127 innerStrategies.put(innerAttrEntry.getKey(), prepareStrategy(innerAttrEntry.getValue()));
130 return new CompositeAttributeMappingStrategy(openType, innerStrategies,
131 createJmxToYangMapping(lastTO));
135 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListAttribute(ArrayType<?> openType) {
136 Preconditions.checkState(lastAttribute instanceof ListAttribute);
137 return new ArrayAttributeMappingStrategy(openType,
138 prepareStrategy(((ListAttribute) lastAttribute).getInnerAttribute()));
142 protected AttributeMappingStrategy<?, ? extends OpenType<?>> caseListDependeciesAttribute(ArrayType<?> openType) {
143 Preconditions.checkState(lastAttribute instanceof ListDependenciesAttribute);
144 return new ArrayAttributeMappingStrategy(openType, caseDependencyAttribute(SimpleType.OBJECTNAME));