8f00f908ef259b2df561d6949c649b51b61ee6d6
[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
25 public class Annotation {
26     final String name;
27     final List<Parameter> params;
28
29     public Annotation(String name, List<Parameter> params) {
30         this.name = name;
31         this.params = params;
32     }
33
34     public String getName() {
35         return name;
36     }
37
38     public List<Parameter> getParams() {
39         return params;
40     }
41
42     public static Annotation createFromMap(Class<?> annotationClass, Map<String, String> parameters) {
43         List<Parameter> parameterList = new ArrayList<>();
44         for(Entry<String, String> entry: parameters.entrySet()) {
45             parameterList.add(new Parameter(entry.getKey(), entry.getValue()));
46         }
47         return new Annotation(annotationClass.getCanonicalName(), parameterList);
48     }
49
50     public static Annotation createDescriptionAnnotation(String description) {
51         Preconditions.checkNotNull(description,
52                 "Cannot create annotation from null description");
53         return new Annotation(Description.class.getCanonicalName(),
54                 Lists.newArrayList(new Parameter("value", q(description))));
55     }
56
57     public static Annotation createModuleQNameANnotation(QName qName) {
58         Map<String, String> parameters = new HashMap<>();
59         parameters.put("namespace", q(qName.getNamespace().toString()));
60         parameters.put("revision", q(qName.getFormattedRevision()));
61         parameters.put("name", q(qName.getLocalName()));
62         return Annotation.createFromMap(ModuleQName.class, parameters);
63     }
64
65     public static Collection<Annotation> createSieAnnotations(ServiceInterfaceEntry sie){
66
67         String exportedClassName = sie.getExportedOsgiClassName();
68         Preconditions.checkNotNull(sie.getQName(),
69                 "Cannot create annotation from null qname");
70         Preconditions.checkNotNull(exportedClassName,
71                 "Cannot create annotation from null exportedClassName");
72         List<Annotation> result = new ArrayList<>();
73         {
74             List<Parameter> params = Lists.newArrayList(new Parameter("value", q(sie.getQName().toString())));
75             params.add(new Parameter("osgiRegistrationType", exportedClassName + ".class"));
76             params.add(new Parameter("registerToOsgi", Boolean.toString(sie.isRegisterToOsgi())));
77             params.add(new Parameter("namespace", q(sie.getQName().getNamespace().toString())));
78             params.add(new Parameter("revision", q(sie.getQName().getFormattedRevision())));
79             params.add(new Parameter("localName", q(sie.getQName().getLocalName())));
80
81             Annotation sieAnnotation = new Annotation(ServiceInterfaceAnnotation.class.getCanonicalName(), params);
82             result.add(sieAnnotation);
83
84         }
85         {
86             List<Parameter> params = new ArrayList<>();
87             params.add(new Parameter("namespace", q(sie.getYangModuleQName().getNamespace().toString())));
88             params.add(new Parameter("revision", q(sie.getYangModuleQName().getFormattedRevision())));
89             params.add(new Parameter("name", q(sie.getYangModuleQName().getLocalName())));
90
91             Annotation moduleQNameAnnotation = new Annotation(ModuleQName.class.getCanonicalName(), params);
92             result.add(moduleQNameAnnotation);
93         }
94         return result;
95     }
96
97     public static Annotation createRequireIfcAnnotation(
98             ServiceInterfaceEntry sie) {
99         String reqIfc = sie.getFullyQualifiedName() + ".class";
100         return new Annotation(RequireInterface.class.getCanonicalName(),
101                 Lists.newArrayList(new Parameter("value", reqIfc)));
102     }
103
104     private static final String quote = "\"";
105
106     public static String q(String nullableDescription) {
107         return nullableDescription == null ? null : quote + nullableDescription
108                 + quote;
109     }
110
111     public static class Parameter {
112         private final String key, value;
113
114         public Parameter(String key, String value) {
115             this.key = key;
116             this.value = value;
117         }
118
119         public String getKey() {
120             return key;
121         }
122
123         public String getValue() {
124             return value;
125         }
126     }
127
128     @Override
129     public String toString() {
130         return AnnotationSerializer.toString(this);
131     }
132 }

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