blob: 0c4e22f96a42e0d338dad14030f4a366e2d401a3 [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
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530115 /**
116 * Map for storing schema nodes with respect to namespace.
117 */
118 private final ConcurrentMap<String, YangSchemaNode> nameSpaceSchemaStore;
119
sonu guptaeff184b2016-11-24 12:43:49 +0530120 private final ConcurrentMap<Object, Boolean> ynhRegistrationStore;
121 private final ConcurrentMap<String, String> jarPathStore;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530122
123 /**
124 * Creates an instance of default YANG schema registry.
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530125 */
sonu guptaeff184b2016-11-24 12:43:49 +0530126 public DefaultYangSchemaRegistry() {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530127 appObjectStore = new ConcurrentHashMap<>();
128 yangSchemaStore = new ConcurrentHashMap<>();
sonu guptaeff184b2016-11-24 12:43:49 +0530129 interfaceNameKeyStore = new ConcurrentHashMap<>();
130 opParamNameKeyStore = new ConcurrentHashMap<>();
131 eventNameKeyStore = new ConcurrentHashMap<>();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530132 registerClassStore = new ConcurrentHashMap<>();
133 yangFileStore = new ConcurrentHashMap<>();
sonu guptaeff184b2016-11-24 12:43:49 +0530134 appNameKeyStore = new ConcurrentHashMap<>();
135 ynhRegistrationStore = new ConcurrentHashMap<>();
136 jarPathStore = new ConcurrentHashMap<>();
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530137 nameSpaceSchemaStore = new ConcurrentHashMap<>();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530138 }
139
140
141 @Override
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530142 public void registerApplication(Object appObject, Class<?> serviceClass) {
sonu guptaeff184b2016-11-24 12:43:49 +0530143 synchronized (serviceClass) {
144 doPreProcessing(serviceClass, appObject);
145 if (!verifyIfApplicationAlreadyRegistered(serviceClass)) {
146 BundleContext context = getBundle(serviceClass).getBundleContext();
147 Bundle[] bundles = context.getBundles();
148 Bundle bundle;
149 int len = bundles.length;
150 List<YangNode> curNodes;
151 String jarPath;
152 for (int i = len - 1; i >= 0; i--) {
153 bundle = bundles[i];
154 if (bundle.getSymbolicName().contains(ONOS)) {
155 jarPath = getJarPathFromBundleLocation(
156 bundle.getLocation(), context.getProperty(USER_DIRECTORY));
157 curNodes = processJarParsingOperations(jarPath);
158 // process application registration.
159 if (curNodes != null && !curNodes.isEmpty()) {
160 jarPathStore.put(serviceClass.getName(), jarPath);
161 processRegistration(serviceClass, jarPath,
162 curNodes, appObject, false);
163 }
164 }
165 }
166 }
167 }
168 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530169
sonu guptaeff184b2016-11-24 12:43:49 +0530170 @Override
171 public void unRegisterApplication(Object managerObject,
172 Class<?> serviceClass) {
173 synchronized (serviceClass) {
174 YangSchemaNode curNode;
175 String serviceName = serviceClass.getName();
176
177 //Check if service should be unregistered?
178 if (managerObject != null) {
179 verifyApplicationRegistration(managerObject, serviceClass);
180 }
181 //Remove registered class from store.
182 registerClassStore.remove(serviceName);
183 //check if service is in app store.
184 curNode = appNameKeyStore.get(serviceName);
185 if (curNode == null) {
186 curNode = interfaceNameKeyStore.get(serviceName);
187 }
188
189 if (curNode != null) {
190 removeSchemaNode(curNode);
191 eventNameKeyStore.remove(getEventClassName(curNode));
192 appObjectStore.remove(serviceName);
193 interfaceNameKeyStore.remove(getInterfaceClassName(curNode));
194 opParamNameKeyStore.remove(getOpParamClassName(curNode));
195 yangFileStore.remove(getModuleIdentifier(curNode));
196 appNameKeyStore.remove(serviceName);
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530197 nameSpaceSchemaStore.remove(curNode.getNameSpace()
198 .getModuleNamespace());
sonu guptaeff184b2016-11-24 12:43:49 +0530199 removeYsrGeneratedTemporaryResources(jarPathStore.get(serviceName),
200 serviceName);
201 log.info(" service {} is unregistered.",
202 serviceClass.getSimpleName());
203 } else {
204 throw new RuntimeException(serviceClass.getSimpleName() +
205 " service was not registered.");
206 }
207 }
208 }
209
210 @Override
211 public Object getRegisteredApplication(YangSchemaNode schemaNode) {
212 Object obj = null;
213 if (schemaNode != null) {
214 String name = getServiceName(schemaNode);
215 obj = appObjectStore.get(name);
216 if (obj == null) {
217 log.error("{} not found.", name);
218 }
219 }
220 return obj;
221 }
222
223 @Override
224 public YangSchemaNode getYangSchemaNodeUsingSchemaName(String schemaName) {
225 return getSchemaNodeUsingSchemaNameWithRev(schemaName);
226 }
227
228 @Override
229 public YangSchemaNode getYangSchemaNodeUsingAppName(String appName) {
230 YangSchemaNode node = appNameKeyStore.get(appName);
231 if (node == null) {
232 log.error("{} not found.", appName);
233 }
234 return node;
235 }
236
237 @Override
238 public YangSchemaNode
239 getYangSchemaNodeUsingGeneratedRootNodeInterfaceFileName(String name) {
240 YangSchemaNode node = interfaceNameKeyStore.get(name);
241 if (node == null) {
242 log.error("{} not found.", name);
243 }
244 return node;
245 }
246
247 @Override
248 public YangSchemaNode getYangSchemaNodeUsingGeneratedRootNodeOpPramFileName(
249 String name) {
250 YangSchemaNode node = opParamNameKeyStore.get(name);
251 if (node == null) {
252 log.error("{} not found.", name);
253 }
254 return node;
255 }
256
257 @Override
258 public YangSchemaNode getRootYangSchemaNodeForNotification(String name) {
259 YangSchemaNode node = eventNameKeyStore.get(name);
260 if (node == null) {
261 log.error("{} not found.", name);
262 }
263 return node;
264 }
265
266 @Override
267 public Class<?> getRegisteredClass(YangSchemaNode schemaNode) {
268 String interfaceName = getInterfaceClassName(schemaNode);
269 String serviceName = getServiceName(schemaNode);
270 Class<?> regClass = registerClassStore.get(serviceName);
271 if (regClass == null) {
272 regClass = registerClassStore.get(interfaceName);
273 }
274 return regClass;
275 }
276
277 @Override
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530278 public YangSchemaNode getSchemaWrtNameSpace(String nameSpace) {
279
280 YangSchemaNode node = nameSpaceSchemaStore.get(nameSpace);
281 if (node == null) {
282 log.error("node with {} namespace not found.", nameSpace);
283 }
284 return node;
285 }
286
287 @Override
sonu guptaeff184b2016-11-24 12:43:49 +0530288 public String getYangFile(YangModuleIdentifier moduleIdentifier) {
289 String file = yangFileStore.get(moduleIdentifier);
290 if (file == null) {
291 log.error("YANG files for corresponding module identifier {} not " +
292 "found", moduleIdentifier);
293 }
294 return file;
295 }
296
297 @Override
298 public boolean verifyNotificationObject(Object appObj, Class<?> service) {
299 synchronized (service) {
300 YangSchemaNode node = appNameKeyStore.get(service.getName());
301 if (node == null) {
302 log.error("application is not registered with YMS {}",
303 service.getName());
304 return false;
305 }
306 try {
307 if (node.isNotificationPresent()) {
308 if (appObj != null) {
309 Boolean ifPresent = ynhRegistrationStore.get(appObj);
310 if (ifPresent == null) {
311 ynhRegistrationStore.put(appObj, true);
312 return true;
313 }
314 }
315 }
316 } catch (DataModelException e) {
317 log.error("notification registration error: {} {}", e
318 .getLocalizedMessage(), e);
319 }
320 return false;
321 }
322 }
323
324 @Override
325 public void flushYsrData() {
326 appObjectStore.clear();
327 yangSchemaStore.clear();
328 eventNameKeyStore.clear();
329 opParamNameKeyStore.clear();
330 interfaceNameKeyStore.clear();
331 registerClassStore.clear();
332 yangFileStore.clear();
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530333 nameSpaceSchemaStore.clear();
sonu guptaeff184b2016-11-24 12:43:49 +0530334 }
335
336 @Override
337 public void processModuleLibrary(String serviceName,
338 YangModuleLibrary library) {
339 synchronized (serviceName) {
340 YangSchemaNode node = appNameKeyStore.get(serviceName);
341 if (node != null) {
342 YangModuleInformation moduleInformation =
343 new DefaultYangModuleInformation(getModuleIdentifier(node),
344 node.getNameSpace());
345 addSubModuleIdentifier(node, (
346 DefaultYangModuleInformation) moduleInformation);
347 //TODO: add feature list to module information.
348 ((DefaultYangModuleLibrary) library)
349 .addModuleInformation(moduleInformation);
350 }
351 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530352 }
353
354 /**
sonu guptaeff184b2016-11-24 12:43:49 +0530355 * Process service class.
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530356 *
357 * @param serviceClass service class
358 * @param appObject application object
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530359 */
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530360
sonu guptaeff184b2016-11-24 12:43:49 +0530361 void doPreProcessing(Class<?> serviceClass, Object appObject) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530362
363 //Check if service should be registered?
364 if (appObject != null) {
365 verifyApplicationRegistration(appObject, serviceClass);
366 }
sonu guptaeff184b2016-11-24 12:43:49 +0530367 String name = serviceClass.getName();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530368 //Add app class to registered service store.
sonu guptaeff184b2016-11-24 12:43:49 +0530369 if (!registerClassStore.containsKey(name)) {
370 registerClassStore.put(name, serviceClass);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530371 }
sonu guptaeff184b2016-11-24 12:43:49 +0530372 }
373
374 void updateServiceClass(Class<?> service) {
375 registerClassStore.put(service.getName(), service);
376 }
377
378 /**
379 * Process application registration.
380 *
381 * @param service service class
382 * @param jarPath jar path
383 * @param nodes YANG nodes
384 * @param appObj application object
385 * @param isFromUt if registration is being called form unit test
386 */
387 void processRegistration(Class<?> service, String jarPath,
388 List<YangNode> nodes,
389 Object appObj, boolean isFromUt) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530390
391 // process storing operations.
sonu guptaeff184b2016-11-24 12:43:49 +0530392 YangNode schemaNode = findNodeWhichShouldBeReg(service.getName(), nodes);
393 if (schemaNode != null) {
394 if (appObj != null) {
395 appObjectStore.put(service.getName(), appObj);
396 }
397 //Process application context for registrations.
398 processApplicationContext(schemaNode, service.getName(), isFromUt);
399 //Update YANG file store.
400 updateYangFileStore(schemaNode, jarPath);
401 }
402 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530403
sonu guptaeff184b2016-11-24 12:43:49 +0530404 /**
405 * Returns the node for which corresponding class is generated.
406 *
407 * @param name generated class name
408 * @param nodes list of yang nodes
409 * @return node for which corresponding class is generated
410 */
411 private YangNode findNodeWhichShouldBeReg(String name, List<YangNode> nodes) {
412 for (YangNode node : nodes) {
413 if (name.equals(getServiceName(node)) ||
414 name.equals(getInterfaceClassName(node))) {
415 return node;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530416 }
417 }
sonu guptaeff184b2016-11-24 12:43:49 +0530418 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530419 }
420
421 /**
422 * Verifies if service class should be registered or not.
423 *
424 * @param appObject application object
425 * @param appClass application class
426 */
427 private void verifyApplicationRegistration(Object appObject,
428 Class<?> appClass) {
429 Class<?> managerClass = appObject.getClass();
430 Class<?>[] services = managerClass.getInterfaces();
431 List<Class<?>> classes = new ArrayList<>();
432 Collections.addAll(classes, services);
433 if (!classes.contains(appClass)) {
434 throw new RuntimeException("service class " + appClass.getName() +
435 "is not being implemented by " +
436 managerClass.getName());
437 }
438 }
439
440 /**
441 * Verifies if application is already registered with YMS.
442 *
443 * @param appClass application class
444 * @return true if application already registered
445 */
446 private boolean verifyIfApplicationAlreadyRegistered(Class<?> appClass) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530447 String appName = appClass.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530448 return appObjectStore.containsKey(appName) ||
449 interfaceNameKeyStore.containsKey(appName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530450 }
451
452 /**
453 * Updates yang file store for YANG node.
454 *
455 * @param node YANG node
456 * @param jarPath jar file path
457 */
458 private void updateYangFileStore(YangNode node, String jarPath) {
sonu guptaeff184b2016-11-24 12:43:49 +0530459 yangFileStore.put(getModuleIdentifier(node),
460 getYangFilePath(jarPath, node.getFileName()));
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530461 }
462
463 /**
464 * Returns yang file path.
465 *
466 * @param jarPath jar path
467 * @param metaDataFileName name of yang file from metadata
468 * @return yang file path
469 */
470 private String getYangFilePath(String jarPath, String metaDataFileName) {
sonu guptaeff184b2016-11-24 12:43:49 +0530471 String[] metaData = metaDataFileName.split(SLASH);
472 return jarPath + SLASH + metaData[metaData.length - 1];
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530473 }
474
475 /**
476 * Process jar file for fetching YANG nodes.
477 *
478 * @param path jar file path
479 * @return YANG schema nodes
480 */
481 private List<YangNode> processJarParsingOperations(String path) {
482 //Deserialize data model and get the YANG node set.
sonu guptaeff184b2016-11-24 12:43:49 +0530483 String jar = path + JAR;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530484 try {
sonu guptaeff184b2016-11-24 12:43:49 +0530485 File file = new File(jar);
486 if (file.exists()) {
487 return parseJarFile(path + JAR, path);
488 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530489 } catch (IOException e) {
490 log.error(" failed to parse the jar file in path {} : {} ", path,
491 e.getMessage());
492 }
493 return null;
494 }
495
496 /**
497 * Process an application an updates the maps for YANG schema registry.
498 *
sonu guptaeff184b2016-11-24 12:43:49 +0530499 * @param appNode application YANG schema nodes
500 * @param name class name
501 * @param isFormUt if method is being called from unit tests
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530502 */
sonu guptaeff184b2016-11-24 12:43:49 +0530503 private void processApplicationContext(YangSchemaNode appNode, String name,
504 boolean isFormUt) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530505
sonu guptaeff184b2016-11-24 12:43:49 +0530506 //Update map for which registrations is being called.
507 appNameKeyStore.put(name, appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530508
509 // Updates schema store.
sonu guptaeff184b2016-11-24 12:43:49 +0530510 addToSchemaStore(appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530511 // update interface store.
sonu guptaeff184b2016-11-24 12:43:49 +0530512 interfaceNameKeyStore.put(getInterfaceClassName(appNode), appNode);
513
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530514 //update op param store.
sonu guptaeff184b2016-11-24 12:43:49 +0530515 opParamNameKeyStore.put(getOpParamClassName(appNode), appNode);
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530516
517 //update namespaceSchema store.
518 nameSpaceSchemaStore.put(appNode.getNameSpace().getModuleNamespace(), appNode);
519
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530520 //Checks if notification is present then update notification store map.
521 String eventSubject = null;
522 try {
523 if (appNode.isNotificationPresent()) {
524 eventSubject = getEventClassName(appNode);
525 }
526 } catch (DataModelException e) {
527 log.error("failed to search notification from schema map : {}",
528 e.getLocalizedMessage());
529 }
530 if (eventSubject != null) {
sonu guptaeff184b2016-11-24 12:43:49 +0530531 eventNameKeyStore.put(eventSubject, appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530532 }
sonu guptaeff184b2016-11-24 12:43:49 +0530533 if (!isFormUt) {
534 log.info("successfully registered this application {}", name);
535 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530536 }
537
538 /**
539 * Returns jar path from bundle mvnLocationPath.
540 *
541 * @param mvnLocationPath mvnLocationPath of bundle
542 * @return path of jar
543 */
544 private String getJarPathFromBundleLocation(String mvnLocationPath,
545 String currentDirectory) {
546 String path = currentDirectory + SYSTEM;
sonu guptaeff184b2016-11-24 12:43:49 +0530547 if (mvnLocationPath.contains(MAVEN)) {
548 String[] strArray = mvnLocationPath.split(MAVEN);
549 if (strArray[1].contains(File.separator)) {
550 String[] split = strArray[1].split(File.separator);
551 if (split[0].contains(PERIOD)) {
552 String[] groupId = split[0].split(Pattern.quote(PERIOD));
553 return path + groupId[0] + SLASH + groupId[1] + SLASH + split[1] +
554 SLASH + split[2] + SLASH + split[1] + HYPHEN + split[2];
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530555 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530556 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530557 }
sonu guptaeff184b2016-11-24 12:43:49 +0530558 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530559 }
560
561 /**
562 * Returns schema node based on the revision.
563 *
564 * @param name name of the schema node
565 * @return schema node based on the revision
566 */
567 private YangSchemaNode getSchemaNodeUsingSchemaNameWithRev(String name) {
sonu guptaeff184b2016-11-24 12:43:49 +0530568 ConcurrentMap<String, YangSchemaNode> revMap;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530569 YangSchemaNode schemaNode;
570 if (name.contains(AT)) {
571 String[] revArray = name.split(AT);
sonu guptaeff184b2016-11-24 12:43:49 +0530572 revMap = yangSchemaStore.get(revArray[0]);
573 schemaNode = revMap.get(name);
574 if (schemaNode == null) {
575 log.error("{} not found.", name);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530576 }
sonu guptaeff184b2016-11-24 12:43:49 +0530577 return schemaNode;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530578 }
579 if (yangSchemaStore.containsKey(name)) {
sonu guptaeff184b2016-11-24 12:43:49 +0530580 revMap = yangSchemaStore.get(name);
581 if (revMap != null && !revMap.isEmpty()) {
582 YangSchemaNode node = revMap.get(name);
583 if (node != null) {
584 return node;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530585 }
sonu guptaeff184b2016-11-24 12:43:49 +0530586 String revName = getLatestVersion(revMap);
587 return revMap.get(revName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530588 }
589 }
590 log.error("{} not found.", name);
591 return null;
592 }
593
sonu guptaeff184b2016-11-24 12:43:49 +0530594 private String getLatestVersion(ConcurrentMap<String, YangSchemaNode> revMap) {
595 List<String> keys = new ArrayList<>();
596 for (Map.Entry<String, YangSchemaNode> entry : revMap.entrySet()) {
597 keys.add(entry.getKey());
598 }
599 sort(keys);
600 return keys.get(keys.size() - 1);
601 }
602
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530603 /**
604 * Adds schema node when different revision of node has received.
605 *
606 * @param schemaNode schema node
607 */
sonu guptaeff184b2016-11-24 12:43:49 +0530608 private void addToSchemaStore(YangSchemaNode schemaNode) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530609
610 String date = getDateInStringFormat(schemaNode);
611 String name = schemaNode.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530612 String revName = name;
613 if (date != null) {
614 revName = name + AT + date;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530615 }
616 //check if already present.
sonu guptaeff184b2016-11-24 12:43:49 +0530617 if (!yangSchemaStore.containsKey(name)) {
618 ConcurrentMap<String, YangSchemaNode> revStore =
619 new ConcurrentHashMap<>();
620 revStore.put(revName, schemaNode);
621 yangSchemaStore.put(name, revStore);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530622 } else {
sonu guptaeff184b2016-11-24 12:43:49 +0530623 yangSchemaStore.get(name).put(revName, schemaNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530624 }
625 }
626
627 /**
628 * Returns date in string format.
629 *
630 * @param schemaNode schema node
631 * @return date in string format
632 */
633 String getDateInStringFormat(YangSchemaNode schemaNode) {
634 if (schemaNode != null) {
635 if (((YangNode) schemaNode).getRevision() != null) {
636 return new SimpleDateFormat(DATE_FORMAT)
637 .format(((YangNode) schemaNode).getRevision()
638 .getRevDate());
639 }
640 }
sonu guptaeff184b2016-11-24 12:43:49 +0530641 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530642 }
643
644 /**
645 * Removes schema node from schema map.
646 *
647 * @param removableNode schema node which needs to be removed
648 */
649 private void removeSchemaNode(YangSchemaNode removableNode) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530650 String name = removableNode.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530651 String revName = name;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530652 String date = getDateInStringFormat(removableNode);
sonu guptaeff184b2016-11-24 12:43:49 +0530653 if (date != null) {
654 revName = name + AT + date;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530655 }
sonu guptaeff184b2016-11-24 12:43:49 +0530656 ConcurrentMap<String, YangSchemaNode> revMap = yangSchemaStore.get(name);
657 if (revMap != null && !revMap.isEmpty() && revMap.size() != 1) {
658 revMap.remove(revName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530659 } else {
660 yangSchemaStore.remove(removableNode.getName());
661 }
662 }
663
664 /**
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530665 * Adds sub module identifier.
666 *
667 * @param node schema node
668 * @param information module information
669 */
670 private void addSubModuleIdentifier(
671 YangSchemaNode node, DefaultYangModuleInformation information) {
672 List<YangInclude> includeList = new ArrayList<>();
673 if (node instanceof YangModule) {
674 includeList = ((YangModule) node).getIncludeList();
675 } else if (node instanceof YangSubModule) {
676 includeList = ((YangSubModule) node).getIncludeList();
677 }
678 for (YangInclude include : includeList) {
679 information.addSubModuleIdentifiers(getModuleIdentifier(
680 include.getIncludedNode()));
681 }
682 }
683
684 /**
685 * Returns module identifier for schema node.
686 *
687 * @param schemaNode schema node
688 * @return module identifier for schema node
689 */
690 private YangModuleIdentifier getModuleIdentifier(
691 YangSchemaNode schemaNode) {
692 return new DefaultYangModuleIdentifier(
693 schemaNode.getName(), getDateInStringFormat(schemaNode));
694 }
695
696 /**
sonu guptaeff184b2016-11-24 12:43:49 +0530697 * Returns schema node's generated interface class name.
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530698 *
sonu guptaeff184b2016-11-24 12:43:49 +0530699 * @param schemaNode schema node
700 * @return schema node's generated interface class name
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530701 */
sonu guptaeff184b2016-11-24 12:43:49 +0530702 String getInterfaceClassName(YangSchemaNode schemaNode) {
703 return schemaNode.getJavaPackage() + PERIOD +
704 getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType());
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530705 }
sonu guptaeff184b2016-11-24 12:43:49 +0530706
707 /**
708 * Returns schema node's generated op param class name.
709 *
710 * @param schemaNode schema node
711 * @return schema node's generated op param class name
712 */
713 private String getOpParamClassName(YangSchemaNode schemaNode) {
714 return getInterfaceClassName(schemaNode) + OP_PARAM;
715 }
716
717 /**
718 * Returns schema node's generated event class name.
719 *
720 * @param schemaNode schema node
721 * @return schema node's generated event class name
722 */
723 private String getEventClassName(YangSchemaNode schemaNode) {
724 return getInterfaceClassName(schemaNode).toLowerCase() + PERIOD +
725 getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType()) +
726 EVENT_STRING;
727 }
728
729 /**
730 * Returns schema node's generated service class name.
731 *
732 * @param schemaNode schema node
733 * @return schema node's generated service class name
734 */
735 String getServiceName(YangSchemaNode schemaNode) {
736 return getInterfaceClassName(schemaNode) + SERVICE;
737 }
738
739 /**
740 * Removes YSR generated temporary resources.
741 *
742 * @param rscPath resource path
743 * @param appName application name
744 */
745 private void removeYsrGeneratedTemporaryResources(String rscPath,
746 String appName) {
747 if (rscPath != null) {
748 File jarPath = new File(rscPath);
749 if (jarPath.exists()) {
750 try {
751 deleteDirectory(jarPath);
752 } catch (IOException e) {
753 log.error("failed to delete ysr resources for {} : {}",
754 appName, e.getLocalizedMessage());
755 }
756 }
757 }
758 }
759}