Adjust to yangtools-2.0.0 changes
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / ftl / model / Annotation.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 package org.opendaylight.controller.config.yangjmxgenerator.plugin.ftl.model;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import org.opendaylight.controller.config.api.annotations.Description;
19 import org.opendaylight.controller.config.api.annotations.RequireInterface;
20 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
21 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
22 import org.opendaylight.yangtools.yang.binding.annotations.ModuleQName;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.Revision;
25
26 public class Annotation {
27     final String name;
28     final List<Parameter> params;
29
30     public Annotation(final String name, final List<Parameter> params) {
31         this.name = name;
32         this.params = params;
33     }
34
35     public String getName() {
36         return name;
37     }
38
39     public List<Parameter> getParams() {
40         return params;
41     }
42
43     public static Annotation createFromMap(final Class<?> annotationClass, final Map<String, String> parameters) {
44         List<Parameter> parameterList = new ArrayList<>();
45         for(Entry<String, String> entry: parameters.entrySet()) {
46             parameterList.add(new Parameter(entry.getKey(), entry.getValue()));
47         }
48         return new Annotation(annotationClass.getCanonicalName(), parameterList);
49     }
50
51     public static Annotation createDescriptionAnnotation(final String description) {
52         Preconditions.checkNotNull(description,
53                 "Cannot create annotation from null description");
54         return new Annotation(Description.class.getCanonicalName(),
55                 Lists.newArrayList(new Parameter("value", q(description))));
56     }
57
58     public static Annotation createModuleQNameANnotation(final QName qName) {
59         Map<String, String> parameters = new HashMap<>();
60         parameters.put("namespace", q(qName.getNamespace().toString()));
61         parameters.put("revision", q(qName.getRevision().map(Revision::toString).orElse(null)));
62         parameters.put("name", q(qName.getLocalName()));
63         return Annotation.createFromMap(ModuleQName.class, parameters);
64     }
65
66     public static Collection<Annotation> createSieAnnotations(final ServiceInterfaceEntry sie){
67
68         String exportedClassName = sie.getExportedOsgiClassName();
69         Preconditions.checkNotNull(sie.getQName(),
70                 "Cannot create annotation from null qname");
71         Preconditions.checkNotNull(exportedClassName,
72                 "Cannot create annotation from null exportedClassName");
73         List<Annotation> result = new ArrayList<>();
74         {
75             List<Parameter> params = Lists.newArrayList(new Parameter("value", q(sie.getQName().toString())));
76             params.add(new Parameter("osgiRegistrationType", exportedClassName + ".class"));
77             params.add(new Parameter("registerToOsgi", Boolean.toString(sie.isRegisterToOsgi())));
78             params.add(new Parameter("namespace", q(sie.getQName().getNamespace().toString())));
79             params.add(new Parameter("revision", q(sie.getQName().getRevision().map(Revision::toString).orElse(null))));
80             params.add(new Parameter("localName", q(sie.getQName().getLocalName())));
81
82             Annotation sieAnnotation = new Annotation(ServiceInterfaceAnnotation.class.getCanonicalName(), params);
83             result.add(sieAnnotation);
84
85         }
86         {
87             List<Parameter> params = new ArrayList<>();
88             params.add(new Parameter("namespace", q(sie.getYangModuleQName().getNamespace().toString())));
89             params.add(new Parameter("revision", q(sie.getYangModuleQName().getRevision()
90                 .map(Revision::toString).orElse(null))));
91             params.add(new Parameter("name", q(sie.getYangModuleQName().getLocalName())));
92
93             Annotation moduleQNameAnnotation = new Annotation(ModuleQName.class.getCanonicalName(), params);
94             result.add(moduleQNameAnnotation);
95         }
96         return result;
97     }
98
99     public static Annotation createRequireIfcAnnotation(
100             final ServiceInterfaceEntry sie) {
101         String reqIfc = sie.getFullyQualifiedName() + ".class";
102         return new Annotation(RequireInterface.class.getCanonicalName(),
103                 Lists.newArrayList(new Parameter("value", reqIfc)));
104     }
105
106     private static final String quote = "\"";
107
108     public static String q(final String nullableDescription) {
109         return nullableDescription == null ? null : quote + nullableDescription + quote;
110     }
111
112     public static class Parameter {
113         private final String key, value;
114
115         public Parameter(final String key, final String value) {
116             this.key = key;
117             this.value = value;
118         }
119
120         public String getKey() {
121             return key;
122         }
123
124         public String getValue() {
125             return value;
126         }
127     }
128
129     @Override
130     public String toString() {
131         return AnnotationSerializer.toString(this);
132     }
133 }

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