4cd0feb0868ef0d00e146c81bbd7c2259523254a
[controller.git] / opendaylight / config / yang-jmx-generator / src / main / java / org / opendaylight / controller / config / yangjmxgenerator / ServiceInterfaceEntry.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;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import static java.lang.String.format;
12 import static org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.SERVICE_TYPE_Q_NAME;
13 import com.google.common.base.Optional;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Represents identity derived from {@link ConfigConstants#SERVICE_TYPE_Q_NAME}.
30  * Example:
31  * <p>
32  * <blockquote>
33  *
34  * <pre>
35  *  identity eventbus {
36  *  description
37  *  "Service representing an event bus. The service acts as message
38  *  router between event producers and event consumers";
39  *
40  *  base "config:service-type";
41  *  config:java-class "com.google.common.eventbus.EventBus";
42  *  }
43  * </pre>
44  *
45  * </blockquote>
46  */
47 public class ServiceInterfaceEntry extends AbstractEntry {
48     private static final Logger LOG = LoggerFactory
49             .getLogger(ServiceInterfaceEntry.class);
50
51     private static final String CLASS_NAME_SUFFIX = "ServiceInterface";
52     private final Optional<ServiceInterfaceEntry> maybeBaseCache;
53     private final String exportedOsgiClassName;
54     private final QName qName;
55     private final String nullableDescription, packageName, typeName;
56     private final QName yangModuleQName;
57     private final boolean registerToOsgi;
58
59     private ServiceInterfaceEntry(IdentitySchemaNode id, String packageName, QName yangModuleQName) {
60         this(Optional.<ServiceInterfaceEntry> absent(), id, packageName, yangModuleQName);
61     }
62
63     private ServiceInterfaceEntry(Optional<ServiceInterfaceEntry> base,
64             IdentitySchemaNode id, String packageName, QName yangModuleQName) {
65         checkNotNull(base);
66         this.maybeBaseCache = base;
67         List<UnknownSchemaNode> unknownSchemaNodes = id.getUnknownSchemaNodes();
68         List<String> exportedOsgiClassNames = new ArrayList<>(
69                 unknownSchemaNodes.size());
70
71         boolean disableOsgiServiceRegistration = false;
72         for (UnknownSchemaNode usn : unknownSchemaNodes) {
73             if (ConfigConstants.JAVA_CLASS_EXTENSION_QNAME.equals(usn.getNodeType())) {
74                 String localName = usn.getNodeParameter();
75                 exportedOsgiClassNames.add(localName);
76             } else if (ConfigConstants.DISABLE_OSGI_SERVICE_REG_QNAME.equals(usn.getNodeType())) {
77                 disableOsgiServiceRegistration = true;
78             } else {
79                 throw new IllegalStateException(format(
80                         "Unexpected unknown schema node. Expected %s, got %s",
81                         ConfigConstants.JAVA_CLASS_EXTENSION_QNAME,
82                         usn.getNodeType()));
83             }
84         }
85         if (exportedOsgiClassNames.size() != 1) {
86             throw new IllegalArgumentException(
87                     format("Cannot find one to one mapping from %s to "
88                             + "java class defined by %s language extension in %s",
89                             getClass(),
90                             ConfigConstants.JAVA_CLASS_EXTENSION_QNAME, id));
91         }
92
93         this.registerToOsgi = !disableOsgiServiceRegistration;
94         this.exportedOsgiClassName = exportedOsgiClassNames.get(0);
95         qName = id.getQName();
96         nullableDescription = id.getDescription();
97         typeName = getSimpleName(exportedOsgiClassName) + CLASS_NAME_SUFFIX;
98         this.packageName = packageName;
99         this.yangModuleQName = yangModuleQName;
100     }
101
102     private static final String getSimpleName(String fullyQualifiedName) {
103         int lastDotPosition = fullyQualifiedName.lastIndexOf(".");
104         return fullyQualifiedName.substring(lastDotPosition + 1);
105     }
106
107     public String getNullableDescription() {
108         return nullableDescription;
109     }
110
111     public Optional<ServiceInterfaceEntry> getBase() {
112         return maybeBaseCache;
113     }
114
115     public String getExportedOsgiClassName() {
116         return exportedOsgiClassName;
117     }
118
119     public QName getQName() {
120         return qName;
121     }
122
123     public boolean isRegisterToOsgi() {
124         return registerToOsgi;
125     }
126
127     /**
128      * @return Map of QNames as keys and ServiceInterfaceEntry instances as
129      *         values
130      */
131     public static Map<QName, ServiceInterfaceEntry> create(Module currentModule,
132             String packageName,Map<IdentitySchemaNode, ServiceInterfaceEntry> definedSEItracker) {
133         LOG.debug("Generating ServiceInterfaces from {} to package {}",
134                 currentModule.getNamespace(), packageName);
135
136         Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
137         Set<IdentitySchemaNode> notVisited = new HashSet<>(
138                 currentModule.getIdentities());
139         int lastSize = notVisited.size() + 1;
140         while (!notVisited.isEmpty()) {
141             if (notVisited.size() == lastSize) {
142                 LOG.debug(
143                         "Following identities will be ignored while generating ServiceInterfaces, as they are not derived from {} : {}",
144                         SERVICE_TYPE_Q_NAME, notVisited);
145                 break;
146             }
147             lastSize = notVisited.size();
148             for (Iterator<IdentitySchemaNode> iterator = notVisited.iterator(); iterator
149                     .hasNext();) {
150                 IdentitySchemaNode identity = iterator.next();
151                 ServiceInterfaceEntry created = null;
152                 if (identity.getBaseIdentity() == null) {
153                     // this can happen while loading config module, just skip
154                     // the identity
155                     continue;
156                 } else if (identity.getBaseIdentity().getQName()
157                         .equals(SERVICE_TYPE_Q_NAME)) {
158                     // this is a base type
159                     created = new ServiceInterfaceEntry(identity, packageName, ModuleUtil.getQName(currentModule));
160                 } else {
161                     ServiceInterfaceEntry foundBase = definedSEItracker
162                             .get(identity.getBaseIdentity());
163                     // derived type, did we convert the parent?
164                     if (foundBase != null) {
165                         created = new ServiceInterfaceEntry(
166                                 Optional.of(foundBase), identity, packageName, ModuleUtil.getQName(currentModule));
167                     }
168                 }
169
170
171                 if (created != null) {
172                     created.setYangModuleName(currentModule.getName());
173                     // TODO how to get local name
174                     created.setYangModuleLocalname(identity.getQName()
175                             .getLocalName());
176                     identitiesToSIs.put(identity, created);
177                     definedSEItracker.put(identity, created);
178                     iterator.remove();
179                 }
180             }
181         }
182         // create result map
183         Map<QName, ServiceInterfaceEntry> resultMap = new HashMap<>();
184         for (ServiceInterfaceEntry sie : identitiesToSIs.values()) {
185             resultMap.put(sie.getQName(), sie);
186         }
187         LOG.debug("Number of ServiceInterfaces to be generated: {}",
188                 resultMap.size());
189         return resultMap;
190     }
191
192     public String getFullyQualifiedName() {
193         return packageName + "." + typeName;
194     }
195
196     public String getPackageName() {
197         return packageName;
198     }
199
200     public String getTypeName() {
201         return typeName;
202     }
203
204     public QName getYangModuleQName() {
205         return yangModuleQName;
206     }
207
208     @Override
209     public boolean equals(Object o) {
210         if (this == o) {
211             return true;
212         }
213         if (o == null || getClass() != o.getClass()) {
214             return false;
215         }
216
217         ServiceInterfaceEntry that = (ServiceInterfaceEntry) o;
218
219         if (!maybeBaseCache.equals(that.maybeBaseCache)) {
220             return false;
221         }
222         if (!nullableDescription.equals(that.nullableDescription)) {
223             return false;
224         }
225         if (!exportedOsgiClassName.equals(that.exportedOsgiClassName)) {
226             return false;
227         }
228         if (!qName.equals(that.qName)) {
229             return false;
230         }
231         if (!packageName.equals(that.packageName)) {
232             return false;
233         }
234         if (!typeName.equals(that.typeName)) {
235             return false;
236         }
237
238         return true;
239     }
240
241     @Override
242     public int hashCode() {
243         int result = maybeBaseCache.hashCode();
244         result = 31 * result + exportedOsgiClassName.hashCode();
245         result = 31 * result + nullableDescription.hashCode();
246         result = 31 * result + typeName.hashCode();
247         result = 31 * result + packageName.hashCode();
248         result = 31 * result + qName.hashCode();
249         return result;
250     }
251
252     @Override
253     public String toString() {
254         return "ServiceInterfaceEntry{" + "maybeBaseCache=" + maybeBaseCache
255                 + ", qName='" + qName + '\'' + ", fullyQualifiedName='"
256                 + getFullyQualifiedName() + '\'' + ", exportedOsgiClassName="
257                 + exportedOsgiClassName + ", nullableDescription='"
258                 + nullableDescription + '\'' + '}';
259     }
260 }

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