blob: 58d772c27688c76ce0890a5876d0b38c63b8136a [file] [log] [blame]
Bharat saraswalf53b29a2016-09-27 15:35:15 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yms.app.ysr;
18
Bharat saraswalf53b29a2016-09-27 15:35:15 +053019import org.onosproject.yangutils.datamodel.YangInclude;
20import org.onosproject.yangutils.datamodel.YangModule;
21import org.onosproject.yangutils.datamodel.YangNode;
22import org.onosproject.yangutils.datamodel.YangSchemaNode;
23import org.onosproject.yangutils.datamodel.YangSubModule;
24import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053025import org.onosproject.yms.ysr.YangModuleIdentifier;
26import org.onosproject.yms.ysr.YangModuleInformation;
27import org.onosproject.yms.ysr.YangModuleLibrary;
sonu guptaeff184b2016-11-24 12:43:49 +053028import org.osgi.framework.Bundle;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053029import org.osgi.framework.BundleContext;
30import org.slf4j.Logger;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053031
32import java.io.File;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053033import java.io.IOException;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053034import java.text.SimpleDateFormat;
35import java.util.ArrayList;
36import java.util.Collections;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053037import java.util.List;
sonu guptaeff184b2016-11-24 12:43:49 +053038import java.util.Map;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053039import java.util.concurrent.ConcurrentHashMap;
40import java.util.concurrent.ConcurrentMap;
41import java.util.regex.Pattern;
42
sonu guptaeff184b2016-11-24 12:43:49 +053043import static java.util.Collections.sort;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053044import static org.apache.commons.io.FileUtils.deleteDirectory;
45import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.parseJarFile;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053046import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING;
sonu guptaeff184b2016-11-24 12:43:49 +053047import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053048import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM;
49import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
sonu guptaeff184b2016-11-24 12:43:49 +053050import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
51import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053052import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase;
53import static org.osgi.framework.FrameworkUtil.getBundle;
sonu guptaeff184b2016-11-24 12:43:49 +053054import static org.slf4j.LoggerFactory.getLogger;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053055
56
57/**
58 * Representation of default YANG schema registry. Yang schema registry
59 * provides interface to an application to register its YANG schema
60 * with YMS. It provides YANG schema nodes to YDT, YNB and YSB.
61 */
sonu guptaeff184b2016-11-24 12:43:49 +053062public class DefaultYangSchemaRegistry implements YangSchemaRegistry {
Bharat saraswalf53b29a2016-09-27 15:35:15 +053063
sonu guptaeff184b2016-11-24 12:43:49 +053064 private static final String SYSTEM = SLASH + "system" + SLASH;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053065 private static final String MAVEN = "mvn:";
Bharat saraswalf53b29a2016-09-27 15:35:15 +053066 private static final String JAR = ".jar";
67 private static final String USER_DIRECTORY = "user.dir";
Bharat saraswalf53b29a2016-09-27 15:35:15 +053068 private static final String AT = "@";
69 private static final String DATE_FORMAT = "yyyy-mm-dd";
sonu guptaeff184b2016-11-24 12:43:49 +053070 private static final String ONOS = "org.onosproject";
71 private static final Logger log = getLogger(DefaultYangSchemaRegistry.class);
72
73 /*
74 * Map for storing app objects.
75 */
76 private final ConcurrentMap<String, Object> appObjectStore;
77
78 /*
79 * Map for storing YANG schema nodes.
80 */
81 private final ConcurrentMap<String, ConcurrentMap<String, YangSchemaNode>>
82 yangSchemaStore;
83
84 /*
85 * Map for storing YANG schema nodes with respect to root's generated
86 * interface file name.
87 */
88 private final ConcurrentMap<String, YangSchemaNode> interfaceNameKeyStore;
89
90 /*
91 * Map for storing YANG schema nodes root's generated op param file name.
92 */
93 private final ConcurrentMap<String, YangSchemaNode> opParamNameKeyStore;
94
95 /*
96 * Map for storing YANG schema nodes with respect to notifications.
97 */
98 private final ConcurrentMap<String, YangSchemaNode> eventNameKeyStore;
99
100 /*
101 * Map for storing YANG schema nodes with respect to app name.
102 */
103 private final ConcurrentMap<String, YangSchemaNode> appNameKeyStore;
104
105 /*
106 * Map for storing registered classes.
107 */
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530108 private final ConcurrentMap<String, Class<?>> registerClassStore;
sonu guptaeff184b2016-11-24 12:43:49 +0530109
110 /*
111 * Map for storing YANG file details.
112 */
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530113 private final ConcurrentMap<YangModuleIdentifier, String> yangFileStore;
sonu guptaeff184b2016-11-24 12:43:49 +0530114
115 private final ConcurrentMap<Object, Boolean> ynhRegistrationStore;
116 private final ConcurrentMap<String, String> jarPathStore;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530117
118 /**
119 * Creates an instance of default YANG schema registry.
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530120 */
sonu guptaeff184b2016-11-24 12:43:49 +0530121 public DefaultYangSchemaRegistry() {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530122 appObjectStore = new ConcurrentHashMap<>();
123 yangSchemaStore = new ConcurrentHashMap<>();
sonu guptaeff184b2016-11-24 12:43:49 +0530124 interfaceNameKeyStore = new ConcurrentHashMap<>();
125 opParamNameKeyStore = new ConcurrentHashMap<>();
126 eventNameKeyStore = new ConcurrentHashMap<>();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530127 registerClassStore = new ConcurrentHashMap<>();
128 yangFileStore = new ConcurrentHashMap<>();
sonu guptaeff184b2016-11-24 12:43:49 +0530129 appNameKeyStore = new ConcurrentHashMap<>();
130 ynhRegistrationStore = new ConcurrentHashMap<>();
131 jarPathStore = new ConcurrentHashMap<>();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530132 }
133
134
135 @Override
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530136 public void registerApplication(Object appObject, Class<?> serviceClass) {
sonu guptaeff184b2016-11-24 12:43:49 +0530137 synchronized (serviceClass) {
138 doPreProcessing(serviceClass, appObject);
139 if (!verifyIfApplicationAlreadyRegistered(serviceClass)) {
140 BundleContext context = getBundle(serviceClass).getBundleContext();
141 Bundle[] bundles = context.getBundles();
142 Bundle bundle;
143 int len = bundles.length;
144 List<YangNode> curNodes;
145 String jarPath;
146 for (int i = len - 1; i >= 0; i--) {
147 bundle = bundles[i];
148 if (bundle.getSymbolicName().contains(ONOS)) {
149 jarPath = getJarPathFromBundleLocation(
150 bundle.getLocation(), context.getProperty(USER_DIRECTORY));
151 curNodes = processJarParsingOperations(jarPath);
152 // process application registration.
153 if (curNodes != null && !curNodes.isEmpty()) {
154 jarPathStore.put(serviceClass.getName(), jarPath);
155 processRegistration(serviceClass, jarPath,
156 curNodes, appObject, false);
157 }
158 }
159 }
160 }
161 }
162 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530163
sonu guptaeff184b2016-11-24 12:43:49 +0530164 @Override
165 public void unRegisterApplication(Object managerObject,
166 Class<?> serviceClass) {
167 synchronized (serviceClass) {
168 YangSchemaNode curNode;
169 String serviceName = serviceClass.getName();
170
171 //Check if service should be unregistered?
172 if (managerObject != null) {
173 verifyApplicationRegistration(managerObject, serviceClass);
174 }
175 //Remove registered class from store.
176 registerClassStore.remove(serviceName);
177 //check if service is in app store.
178 curNode = appNameKeyStore.get(serviceName);
179 if (curNode == null) {
180 curNode = interfaceNameKeyStore.get(serviceName);
181 }
182
183 if (curNode != null) {
184 removeSchemaNode(curNode);
185 eventNameKeyStore.remove(getEventClassName(curNode));
186 appObjectStore.remove(serviceName);
187 interfaceNameKeyStore.remove(getInterfaceClassName(curNode));
188 opParamNameKeyStore.remove(getOpParamClassName(curNode));
189 yangFileStore.remove(getModuleIdentifier(curNode));
190 appNameKeyStore.remove(serviceName);
191 removeYsrGeneratedTemporaryResources(jarPathStore.get(serviceName),
192 serviceName);
193 log.info(" service {} is unregistered.",
194 serviceClass.getSimpleName());
195 } else {
196 throw new RuntimeException(serviceClass.getSimpleName() +
197 " service was not registered.");
198 }
199 }
200 }
201
202 @Override
203 public Object getRegisteredApplication(YangSchemaNode schemaNode) {
204 Object obj = null;
205 if (schemaNode != null) {
206 String name = getServiceName(schemaNode);
207 obj = appObjectStore.get(name);
208 if (obj == null) {
209 log.error("{} not found.", name);
210 }
211 }
212 return obj;
213 }
214
215 @Override
216 public YangSchemaNode getYangSchemaNodeUsingSchemaName(String schemaName) {
217 return getSchemaNodeUsingSchemaNameWithRev(schemaName);
218 }
219
220 @Override
221 public YangSchemaNode getYangSchemaNodeUsingAppName(String appName) {
222 YangSchemaNode node = appNameKeyStore.get(appName);
223 if (node == null) {
224 log.error("{} not found.", appName);
225 }
226 return node;
227 }
228
229 @Override
230 public YangSchemaNode
231 getYangSchemaNodeUsingGeneratedRootNodeInterfaceFileName(String name) {
232 YangSchemaNode node = interfaceNameKeyStore.get(name);
233 if (node == null) {
234 log.error("{} not found.", name);
235 }
236 return node;
237 }
238
239 @Override
240 public YangSchemaNode getYangSchemaNodeUsingGeneratedRootNodeOpPramFileName(
241 String name) {
242 YangSchemaNode node = opParamNameKeyStore.get(name);
243 if (node == null) {
244 log.error("{} not found.", name);
245 }
246 return node;
247 }
248
249 @Override
250 public YangSchemaNode getRootYangSchemaNodeForNotification(String name) {
251 YangSchemaNode node = eventNameKeyStore.get(name);
252 if (node == null) {
253 log.error("{} not found.", name);
254 }
255 return node;
256 }
257
258 @Override
259 public Class<?> getRegisteredClass(YangSchemaNode schemaNode) {
260 String interfaceName = getInterfaceClassName(schemaNode);
261 String serviceName = getServiceName(schemaNode);
262 Class<?> regClass = registerClassStore.get(serviceName);
263 if (regClass == null) {
264 regClass = registerClassStore.get(interfaceName);
265 }
266 return regClass;
267 }
268
269 @Override
270 public String getYangFile(YangModuleIdentifier moduleIdentifier) {
271 String file = yangFileStore.get(moduleIdentifier);
272 if (file == null) {
273 log.error("YANG files for corresponding module identifier {} not " +
274 "found", moduleIdentifier);
275 }
276 return file;
277 }
278
279 @Override
280 public boolean verifyNotificationObject(Object appObj, Class<?> service) {
281 synchronized (service) {
282 YangSchemaNode node = appNameKeyStore.get(service.getName());
283 if (node == null) {
284 log.error("application is not registered with YMS {}",
285 service.getName());
286 return false;
287 }
288 try {
289 if (node.isNotificationPresent()) {
290 if (appObj != null) {
291 Boolean ifPresent = ynhRegistrationStore.get(appObj);
292 if (ifPresent == null) {
293 ynhRegistrationStore.put(appObj, true);
294 return true;
295 }
296 }
297 }
298 } catch (DataModelException e) {
299 log.error("notification registration error: {} {}", e
300 .getLocalizedMessage(), e);
301 }
302 return false;
303 }
304 }
305
306 @Override
307 public void flushYsrData() {
308 appObjectStore.clear();
309 yangSchemaStore.clear();
310 eventNameKeyStore.clear();
311 opParamNameKeyStore.clear();
312 interfaceNameKeyStore.clear();
313 registerClassStore.clear();
314 yangFileStore.clear();
315 }
316
317 @Override
318 public void processModuleLibrary(String serviceName,
319 YangModuleLibrary library) {
320 synchronized (serviceName) {
321 YangSchemaNode node = appNameKeyStore.get(serviceName);
322 if (node != null) {
323 YangModuleInformation moduleInformation =
324 new DefaultYangModuleInformation(getModuleIdentifier(node),
325 node.getNameSpace());
326 addSubModuleIdentifier(node, (
327 DefaultYangModuleInformation) moduleInformation);
328 //TODO: add feature list to module information.
329 ((DefaultYangModuleLibrary) library)
330 .addModuleInformation(moduleInformation);
331 }
332 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530333 }
334
335 /**
sonu guptaeff184b2016-11-24 12:43:49 +0530336 * Process service class.
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530337 *
338 * @param serviceClass service class
339 * @param appObject application object
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530340 */
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530341
sonu guptaeff184b2016-11-24 12:43:49 +0530342 void doPreProcessing(Class<?> serviceClass, Object appObject) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530343
344 //Check if service should be registered?
345 if (appObject != null) {
346 verifyApplicationRegistration(appObject, serviceClass);
347 }
sonu guptaeff184b2016-11-24 12:43:49 +0530348 String name = serviceClass.getName();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530349 //Add app class to registered service store.
sonu guptaeff184b2016-11-24 12:43:49 +0530350 if (!registerClassStore.containsKey(name)) {
351 registerClassStore.put(name, serviceClass);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530352 }
sonu guptaeff184b2016-11-24 12:43:49 +0530353 }
354
355 void updateServiceClass(Class<?> service) {
356 registerClassStore.put(service.getName(), service);
357 }
358
359 /**
360 * Process application registration.
361 *
362 * @param service service class
363 * @param jarPath jar path
364 * @param nodes YANG nodes
365 * @param appObj application object
366 * @param isFromUt if registration is being called form unit test
367 */
368 void processRegistration(Class<?> service, String jarPath,
369 List<YangNode> nodes,
370 Object appObj, boolean isFromUt) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530371
372 // process storing operations.
sonu guptaeff184b2016-11-24 12:43:49 +0530373 YangNode schemaNode = findNodeWhichShouldBeReg(service.getName(), nodes);
374 if (schemaNode != null) {
375 if (appObj != null) {
376 appObjectStore.put(service.getName(), appObj);
377 }
378 //Process application context for registrations.
379 processApplicationContext(schemaNode, service.getName(), isFromUt);
380 //Update YANG file store.
381 updateYangFileStore(schemaNode, jarPath);
382 }
383 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530384
sonu guptaeff184b2016-11-24 12:43:49 +0530385 /**
386 * Returns the node for which corresponding class is generated.
387 *
388 * @param name generated class name
389 * @param nodes list of yang nodes
390 * @return node for which corresponding class is generated
391 */
392 private YangNode findNodeWhichShouldBeReg(String name, List<YangNode> nodes) {
393 for (YangNode node : nodes) {
394 if (name.equals(getServiceName(node)) ||
395 name.equals(getInterfaceClassName(node))) {
396 return node;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530397 }
398 }
sonu guptaeff184b2016-11-24 12:43:49 +0530399 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530400 }
401
402 /**
403 * Verifies if service class should be registered or not.
404 *
405 * @param appObject application object
406 * @param appClass application class
407 */
408 private void verifyApplicationRegistration(Object appObject,
409 Class<?> appClass) {
410 Class<?> managerClass = appObject.getClass();
411 Class<?>[] services = managerClass.getInterfaces();
412 List<Class<?>> classes = new ArrayList<>();
413 Collections.addAll(classes, services);
414 if (!classes.contains(appClass)) {
415 throw new RuntimeException("service class " + appClass.getName() +
416 "is not being implemented by " +
417 managerClass.getName());
418 }
419 }
420
421 /**
422 * Verifies if application is already registered with YMS.
423 *
424 * @param appClass application class
425 * @return true if application already registered
426 */
427 private boolean verifyIfApplicationAlreadyRegistered(Class<?> appClass) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530428 String appName = appClass.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530429 return appObjectStore.containsKey(appName) ||
430 interfaceNameKeyStore.containsKey(appName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530431 }
432
433 /**
434 * Updates yang file store for YANG node.
435 *
436 * @param node YANG node
437 * @param jarPath jar file path
438 */
439 private void updateYangFileStore(YangNode node, String jarPath) {
sonu guptaeff184b2016-11-24 12:43:49 +0530440 yangFileStore.put(getModuleIdentifier(node),
441 getYangFilePath(jarPath, node.getFileName()));
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530442 }
443
444 /**
445 * Returns yang file path.
446 *
447 * @param jarPath jar path
448 * @param metaDataFileName name of yang file from metadata
449 * @return yang file path
450 */
451 private String getYangFilePath(String jarPath, String metaDataFileName) {
sonu guptaeff184b2016-11-24 12:43:49 +0530452 String[] metaData = metaDataFileName.split(SLASH);
453 return jarPath + SLASH + metaData[metaData.length - 1];
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530454 }
455
456 /**
457 * Process jar file for fetching YANG nodes.
458 *
459 * @param path jar file path
460 * @return YANG schema nodes
461 */
462 private List<YangNode> processJarParsingOperations(String path) {
463 //Deserialize data model and get the YANG node set.
sonu guptaeff184b2016-11-24 12:43:49 +0530464 String jar = path + JAR;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530465 try {
sonu guptaeff184b2016-11-24 12:43:49 +0530466 File file = new File(jar);
467 if (file.exists()) {
468 return parseJarFile(path + JAR, path);
469 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530470 } catch (IOException e) {
471 log.error(" failed to parse the jar file in path {} : {} ", path,
472 e.getMessage());
473 }
474 return null;
475 }
476
477 /**
478 * Process an application an updates the maps for YANG schema registry.
479 *
sonu guptaeff184b2016-11-24 12:43:49 +0530480 * @param appNode application YANG schema nodes
481 * @param name class name
482 * @param isFormUt if method is being called from unit tests
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530483 */
sonu guptaeff184b2016-11-24 12:43:49 +0530484 private void processApplicationContext(YangSchemaNode appNode, String name,
485 boolean isFormUt) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530486
sonu guptaeff184b2016-11-24 12:43:49 +0530487 //Update map for which registrations is being called.
488 appNameKeyStore.put(name, appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530489
490 // Updates schema store.
sonu guptaeff184b2016-11-24 12:43:49 +0530491 addToSchemaStore(appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530492 // update interface store.
sonu guptaeff184b2016-11-24 12:43:49 +0530493 interfaceNameKeyStore.put(getInterfaceClassName(appNode), appNode);
494
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530495 //update op param store.
sonu guptaeff184b2016-11-24 12:43:49 +0530496 opParamNameKeyStore.put(getOpParamClassName(appNode), appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530497 //Checks if notification is present then update notification store map.
498 String eventSubject = null;
499 try {
500 if (appNode.isNotificationPresent()) {
501 eventSubject = getEventClassName(appNode);
502 }
503 } catch (DataModelException e) {
504 log.error("failed to search notification from schema map : {}",
505 e.getLocalizedMessage());
506 }
507 if (eventSubject != null) {
sonu guptaeff184b2016-11-24 12:43:49 +0530508 eventNameKeyStore.put(eventSubject, appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530509 }
sonu guptaeff184b2016-11-24 12:43:49 +0530510 if (!isFormUt) {
511 log.info("successfully registered this application {}", name);
512 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530513 }
514
515 /**
516 * Returns jar path from bundle mvnLocationPath.
517 *
518 * @param mvnLocationPath mvnLocationPath of bundle
519 * @return path of jar
520 */
521 private String getJarPathFromBundleLocation(String mvnLocationPath,
522 String currentDirectory) {
523 String path = currentDirectory + SYSTEM;
sonu guptaeff184b2016-11-24 12:43:49 +0530524 if (mvnLocationPath.contains(MAVEN)) {
525 String[] strArray = mvnLocationPath.split(MAVEN);
526 if (strArray[1].contains(File.separator)) {
527 String[] split = strArray[1].split(File.separator);
528 if (split[0].contains(PERIOD)) {
529 String[] groupId = split[0].split(Pattern.quote(PERIOD));
530 return path + groupId[0] + SLASH + groupId[1] + SLASH + split[1] +
531 SLASH + split[2] + SLASH + split[1] + HYPHEN + split[2];
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530532 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530533 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530534 }
sonu guptaeff184b2016-11-24 12:43:49 +0530535 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530536 }
537
538 /**
539 * Returns schema node based on the revision.
540 *
541 * @param name name of the schema node
542 * @return schema node based on the revision
543 */
544 private YangSchemaNode getSchemaNodeUsingSchemaNameWithRev(String name) {
sonu guptaeff184b2016-11-24 12:43:49 +0530545 ConcurrentMap<String, YangSchemaNode> revMap;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530546 YangSchemaNode schemaNode;
547 if (name.contains(AT)) {
548 String[] revArray = name.split(AT);
sonu guptaeff184b2016-11-24 12:43:49 +0530549 revMap = yangSchemaStore.get(revArray[0]);
550 schemaNode = revMap.get(name);
551 if (schemaNode == null) {
552 log.error("{} not found.", name);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530553 }
sonu guptaeff184b2016-11-24 12:43:49 +0530554 return schemaNode;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530555 }
556 if (yangSchemaStore.containsKey(name)) {
sonu guptaeff184b2016-11-24 12:43:49 +0530557 revMap = yangSchemaStore.get(name);
558 if (revMap != null && !revMap.isEmpty()) {
559 YangSchemaNode node = revMap.get(name);
560 if (node != null) {
561 return node;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530562 }
sonu guptaeff184b2016-11-24 12:43:49 +0530563 String revName = getLatestVersion(revMap);
564 return revMap.get(revName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530565 }
566 }
567 log.error("{} not found.", name);
568 return null;
569 }
570
sonu guptaeff184b2016-11-24 12:43:49 +0530571 private String getLatestVersion(ConcurrentMap<String, YangSchemaNode> revMap) {
572 List<String> keys = new ArrayList<>();
573 for (Map.Entry<String, YangSchemaNode> entry : revMap.entrySet()) {
574 keys.add(entry.getKey());
575 }
576 sort(keys);
577 return keys.get(keys.size() - 1);
578 }
579
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530580 /**
581 * Adds schema node when different revision of node has received.
582 *
583 * @param schemaNode schema node
584 */
sonu guptaeff184b2016-11-24 12:43:49 +0530585 private void addToSchemaStore(YangSchemaNode schemaNode) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530586
587 String date = getDateInStringFormat(schemaNode);
588 String name = schemaNode.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530589 String revName = name;
590 if (date != null) {
591 revName = name + AT + date;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530592 }
593 //check if already present.
sonu guptaeff184b2016-11-24 12:43:49 +0530594 if (!yangSchemaStore.containsKey(name)) {
595 ConcurrentMap<String, YangSchemaNode> revStore =
596 new ConcurrentHashMap<>();
597 revStore.put(revName, schemaNode);
598 yangSchemaStore.put(name, revStore);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530599 } else {
sonu guptaeff184b2016-11-24 12:43:49 +0530600 yangSchemaStore.get(name).put(revName, schemaNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530601 }
602 }
603
604 /**
605 * Returns date in string format.
606 *
607 * @param schemaNode schema node
608 * @return date in string format
609 */
610 String getDateInStringFormat(YangSchemaNode schemaNode) {
611 if (schemaNode != null) {
612 if (((YangNode) schemaNode).getRevision() != null) {
613 return new SimpleDateFormat(DATE_FORMAT)
614 .format(((YangNode) schemaNode).getRevision()
615 .getRevDate());
616 }
617 }
sonu guptaeff184b2016-11-24 12:43:49 +0530618 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530619 }
620
621 /**
622 * Removes schema node from schema map.
623 *
624 * @param removableNode schema node which needs to be removed
625 */
626 private void removeSchemaNode(YangSchemaNode removableNode) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530627 String name = removableNode.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530628 String revName = name;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530629 String date = getDateInStringFormat(removableNode);
sonu guptaeff184b2016-11-24 12:43:49 +0530630 if (date != null) {
631 revName = name + AT + date;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530632 }
sonu guptaeff184b2016-11-24 12:43:49 +0530633 ConcurrentMap<String, YangSchemaNode> revMap = yangSchemaStore.get(name);
634 if (revMap != null && !revMap.isEmpty() && revMap.size() != 1) {
635 revMap.remove(revName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530636 } else {
637 yangSchemaStore.remove(removableNode.getName());
638 }
639 }
640
641 /**
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530642 * Adds sub module identifier.
643 *
644 * @param node schema node
645 * @param information module information
646 */
647 private void addSubModuleIdentifier(
648 YangSchemaNode node, DefaultYangModuleInformation information) {
649 List<YangInclude> includeList = new ArrayList<>();
650 if (node instanceof YangModule) {
651 includeList = ((YangModule) node).getIncludeList();
652 } else if (node instanceof YangSubModule) {
653 includeList = ((YangSubModule) node).getIncludeList();
654 }
655 for (YangInclude include : includeList) {
656 information.addSubModuleIdentifiers(getModuleIdentifier(
657 include.getIncludedNode()));
658 }
659 }
660
661 /**
662 * Returns module identifier for schema node.
663 *
664 * @param schemaNode schema node
665 * @return module identifier for schema node
666 */
667 private YangModuleIdentifier getModuleIdentifier(
668 YangSchemaNode schemaNode) {
669 return new DefaultYangModuleIdentifier(
670 schemaNode.getName(), getDateInStringFormat(schemaNode));
671 }
672
673 /**
sonu guptaeff184b2016-11-24 12:43:49 +0530674 * Returns schema node's generated interface class name.
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530675 *
sonu guptaeff184b2016-11-24 12:43:49 +0530676 * @param schemaNode schema node
677 * @return schema node's generated interface class name
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530678 */
sonu guptaeff184b2016-11-24 12:43:49 +0530679 String getInterfaceClassName(YangSchemaNode schemaNode) {
680 return schemaNode.getJavaPackage() + PERIOD +
681 getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType());
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530682 }
sonu guptaeff184b2016-11-24 12:43:49 +0530683
684 /**
685 * Returns schema node's generated op param class name.
686 *
687 * @param schemaNode schema node
688 * @return schema node's generated op param class name
689 */
690 private String getOpParamClassName(YangSchemaNode schemaNode) {
691 return getInterfaceClassName(schemaNode) + OP_PARAM;
692 }
693
694 /**
695 * Returns schema node's generated event class name.
696 *
697 * @param schemaNode schema node
698 * @return schema node's generated event class name
699 */
700 private String getEventClassName(YangSchemaNode schemaNode) {
701 return getInterfaceClassName(schemaNode).toLowerCase() + PERIOD +
702 getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType()) +
703 EVENT_STRING;
704 }
705
706 /**
707 * Returns schema node's generated service class name.
708 *
709 * @param schemaNode schema node
710 * @return schema node's generated service class name
711 */
712 String getServiceName(YangSchemaNode schemaNode) {
713 return getInterfaceClassName(schemaNode) + SERVICE;
714 }
715
716 /**
717 * Removes YSR generated temporary resources.
718 *
719 * @param rscPath resource path
720 * @param appName application name
721 */
722 private void removeYsrGeneratedTemporaryResources(String rscPath,
723 String appName) {
724 if (rscPath != null) {
725 File jarPath = new File(rscPath);
726 if (jarPath.exists()) {
727 try {
728 deleteDirectory(jarPath);
729 } catch (IOException e) {
730 log.error("failed to delete ysr resources for {} : {}",
731 appName, e.getLocalizedMessage());
732 }
733 }
734 }
735 }
736}