Merge "Bug 615: Removed xtend from Topology Manager"
[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 org.opendaylight.controller.config.api.annotations.Description;
13 import org.opendaylight.controller.config.api.annotations.RequireInterface;
14 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
15 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntry;
16 import org.opendaylight.yangtools.yang.binding.annotations.ModuleQName;
17 import org.opendaylight.yangtools.yang.common.QName;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 public class Annotation {
27     final String name;
28     final List<Parameter> params;
29
30     public Annotation(String name, 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(Class<?> annotationClass, 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(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(QName qName) {
59         Map<String, String> parameters = new HashMap<>();
60         parameters.put("namespace", q(qName.getNamespace().toString()));
61         parameters.put("revision", q(qName.getFormattedRevision()));
62         parameters.put("name", q(qName.getLocalName()));
63         return Annotation.createFromMap(ModuleQName.class, parameters);
64     }
65
66     public static Collection<Annotation> createSieAnnotations(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
78             params.add(new Parameter("namespace", q(sie.getQName().getNamespace().toString())));
79             params.add(new Parameter("revision", q(sie.getQName().getFormattedRevision())));
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().getFormattedRevision())));
90             params.add(new Parameter("name", q(sie.getYangModuleQName().getLocalName())));
91
92             Annotation moduleQNameAnnotation = new Annotation(ModuleQName.class.getCanonicalName(), params);
93             result.add(moduleQNameAnnotation);
94         }
95         return result;
96     }
97
98     public static Annotation createRequireIfcAnnotation(
99             ServiceInterfaceEntry sie) {
100         String reqIfc = sie.getFullyQualifiedName() + ".class";
101         return new Annotation(RequireInterface.class.getCanonicalName(),
102                 Lists.newArrayList(new Parameter("value", reqIfc)));
103     }
104
105     private static final String quote = "\"";
106
107     public static String q(String nullableDescription) {
108         return nullableDescription == null ? null : quote + nullableDescription
109                 + quote;
110     }
111
112     public static class Parameter {
113         private final String key, value;
114
115         public Parameter(String key, 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 }