blob: fbd6c8692ae50c3e3a85a99e7b8e9204996e1a6b [file] [log] [blame]
Bharat saraswalf53b29a2016-09-27 15:35:15 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Bharat saraswalf53b29a2016-09-27 15:35:15 +05303 *
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) {
Bharat saraswala899a212017-02-28 13:19:57 +0530143 synchronized (DefaultYangSchemaRegistry.class) {
sonu guptaeff184b2016-11-24 12:43:49 +0530144 doPreProcessing(serviceClass, appObject);
145 if (!verifyIfApplicationAlreadyRegistered(serviceClass)) {
146 BundleContext context = getBundle(serviceClass).getBundleContext();
Bharat saraswala899a212017-02-28 13:19:57 +0530147 if (context != null) {
148 Bundle[] bundles = context.getBundles();
149 Bundle bundle;
150 int len = bundles.length;
151 List<YangNode> curNodes;
152 String jarPath;
153 for (int i = len - 1; i >= 0; i--) {
154 bundle = bundles[i];
155 if (bundle.getSymbolicName().contains(ONOS)) {
156 jarPath = getJarPathFromBundleLocation(
157 bundle.getLocation(), context.getProperty(USER_DIRECTORY));
158 curNodes = processJarParsingOperations(jarPath);
159 // process application registration.
160 if (curNodes != null && !curNodes.isEmpty()) {
161 jarPathStore.put(serviceClass.getName(), jarPath);
162 processRegistration(serviceClass, jarPath,
163 curNodes, appObject, false);
164 }
sonu guptaeff184b2016-11-24 12:43:49 +0530165 }
166 }
167 }
168 }
169 }
170 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530171
sonu guptaeff184b2016-11-24 12:43:49 +0530172 @Override
173 public void unRegisterApplication(Object managerObject,
174 Class<?> serviceClass) {
Bharat saraswala899a212017-02-28 13:19:57 +0530175 synchronized (DefaultYangSchemaRegistry.class) {
sonu guptaeff184b2016-11-24 12:43:49 +0530176 YangSchemaNode curNode;
177 String serviceName = serviceClass.getName();
178
179 //Check if service should be unregistered?
180 if (managerObject != null) {
181 verifyApplicationRegistration(managerObject, serviceClass);
182 }
183 //Remove registered class from store.
184 registerClassStore.remove(serviceName);
185 //check if service is in app store.
186 curNode = appNameKeyStore.get(serviceName);
187 if (curNode == null) {
188 curNode = interfaceNameKeyStore.get(serviceName);
189 }
190
191 if (curNode != null) {
192 removeSchemaNode(curNode);
193 eventNameKeyStore.remove(getEventClassName(curNode));
194 appObjectStore.remove(serviceName);
195 interfaceNameKeyStore.remove(getInterfaceClassName(curNode));
196 opParamNameKeyStore.remove(getOpParamClassName(curNode));
197 yangFileStore.remove(getModuleIdentifier(curNode));
198 appNameKeyStore.remove(serviceName);
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530199 nameSpaceSchemaStore.remove(curNode.getNameSpace()
200 .getModuleNamespace());
sonu guptaeff184b2016-11-24 12:43:49 +0530201 removeYsrGeneratedTemporaryResources(jarPathStore.get(serviceName),
202 serviceName);
203 log.info(" service {} is unregistered.",
204 serviceClass.getSimpleName());
205 } else {
206 throw new RuntimeException(serviceClass.getSimpleName() +
207 " service was not registered.");
208 }
209 }
210 }
211
212 @Override
213 public Object getRegisteredApplication(YangSchemaNode schemaNode) {
214 Object obj = null;
215 if (schemaNode != null) {
216 String name = getServiceName(schemaNode);
217 obj = appObjectStore.get(name);
218 if (obj == null) {
219 log.error("{} not found.", name);
220 }
221 }
222 return obj;
223 }
224
225 @Override
226 public YangSchemaNode getYangSchemaNodeUsingSchemaName(String schemaName) {
227 return getSchemaNodeUsingSchemaNameWithRev(schemaName);
228 }
229
230 @Override
231 public YangSchemaNode getYangSchemaNodeUsingAppName(String appName) {
232 YangSchemaNode node = appNameKeyStore.get(appName);
233 if (node == null) {
234 log.error("{} not found.", appName);
235 }
236 return node;
237 }
238
239 @Override
240 public YangSchemaNode
241 getYangSchemaNodeUsingGeneratedRootNodeInterfaceFileName(String name) {
242 YangSchemaNode node = interfaceNameKeyStore.get(name);
243 if (node == null) {
244 log.error("{} not found.", name);
245 }
246 return node;
247 }
248
249 @Override
250 public YangSchemaNode getYangSchemaNodeUsingGeneratedRootNodeOpPramFileName(
251 String name) {
252 YangSchemaNode node = opParamNameKeyStore.get(name);
253 if (node == null) {
254 log.error("{} not found.", name);
255 }
256 return node;
257 }
258
259 @Override
260 public YangSchemaNode getRootYangSchemaNodeForNotification(String name) {
261 YangSchemaNode node = eventNameKeyStore.get(name);
262 if (node == null) {
263 log.error("{} not found.", name);
264 }
265 return node;
266 }
267
268 @Override
269 public Class<?> getRegisteredClass(YangSchemaNode schemaNode) {
270 String interfaceName = getInterfaceClassName(schemaNode);
271 String serviceName = getServiceName(schemaNode);
272 Class<?> regClass = registerClassStore.get(serviceName);
273 if (regClass == null) {
274 regClass = registerClassStore.get(interfaceName);
275 }
276 return regClass;
277 }
278
279 @Override
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530280 public YangSchemaNode getSchemaWrtNameSpace(String nameSpace) {
281
282 YangSchemaNode node = nameSpaceSchemaStore.get(nameSpace);
283 if (node == null) {
284 log.error("node with {} namespace not found.", nameSpace);
285 }
286 return node;
287 }
288
289 @Override
sonu guptaeff184b2016-11-24 12:43:49 +0530290 public String getYangFile(YangModuleIdentifier moduleIdentifier) {
291 String file = yangFileStore.get(moduleIdentifier);
292 if (file == null) {
293 log.error("YANG files for corresponding module identifier {} not " +
294 "found", moduleIdentifier);
295 }
296 return file;
297 }
298
299 @Override
300 public boolean verifyNotificationObject(Object appObj, Class<?> service) {
Bharat saraswala899a212017-02-28 13:19:57 +0530301 synchronized (DefaultYangSchemaRegistry.class) {
sonu guptaeff184b2016-11-24 12:43:49 +0530302 YangSchemaNode node = appNameKeyStore.get(service.getName());
303 if (node == null) {
304 log.error("application is not registered with YMS {}",
305 service.getName());
306 return false;
307 }
308 try {
309 if (node.isNotificationPresent()) {
310 if (appObj != null) {
311 Boolean ifPresent = ynhRegistrationStore.get(appObj);
312 if (ifPresent == null) {
313 ynhRegistrationStore.put(appObj, true);
314 return true;
315 }
316 }
317 }
318 } catch (DataModelException e) {
319 log.error("notification registration error: {} {}", e
320 .getLocalizedMessage(), e);
321 }
322 return false;
323 }
324 }
325
326 @Override
327 public void flushYsrData() {
328 appObjectStore.clear();
329 yangSchemaStore.clear();
330 eventNameKeyStore.clear();
331 opParamNameKeyStore.clear();
332 interfaceNameKeyStore.clear();
333 registerClassStore.clear();
334 yangFileStore.clear();
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530335 nameSpaceSchemaStore.clear();
sonu guptaeff184b2016-11-24 12:43:49 +0530336 }
337
338 @Override
339 public void processModuleLibrary(String serviceName,
340 YangModuleLibrary library) {
Bharat saraswala899a212017-02-28 13:19:57 +0530341 synchronized (DefaultYangSchemaRegistry.class) {
sonu guptaeff184b2016-11-24 12:43:49 +0530342 YangSchemaNode node = appNameKeyStore.get(serviceName);
343 if (node != null) {
344 YangModuleInformation moduleInformation =
345 new DefaultYangModuleInformation(getModuleIdentifier(node),
346 node.getNameSpace());
347 addSubModuleIdentifier(node, (
348 DefaultYangModuleInformation) moduleInformation);
349 //TODO: add feature list to module information.
350 ((DefaultYangModuleLibrary) library)
351 .addModuleInformation(moduleInformation);
352 }
353 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530354 }
355
356 /**
sonu guptaeff184b2016-11-24 12:43:49 +0530357 * Process service class.
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530358 *
359 * @param serviceClass service class
360 * @param appObject application object
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530361 */
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530362
sonu guptaeff184b2016-11-24 12:43:49 +0530363 void doPreProcessing(Class<?> serviceClass, Object appObject) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530364
365 //Check if service should be registered?
366 if (appObject != null) {
367 verifyApplicationRegistration(appObject, serviceClass);
368 }
sonu guptaeff184b2016-11-24 12:43:49 +0530369 String name = serviceClass.getName();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530370 //Add app class to registered service store.
sonu guptaeff184b2016-11-24 12:43:49 +0530371 if (!registerClassStore.containsKey(name)) {
372 registerClassStore.put(name, serviceClass);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530373 }
sonu guptaeff184b2016-11-24 12:43:49 +0530374 }
375
376 void updateServiceClass(Class<?> service) {
377 registerClassStore.put(service.getName(), service);
378 }
379
380 /**
381 * Process application registration.
382 *
383 * @param service service class
384 * @param jarPath jar path
385 * @param nodes YANG nodes
386 * @param appObj application object
387 * @param isFromUt if registration is being called form unit test
388 */
389 void processRegistration(Class<?> service, String jarPath,
390 List<YangNode> nodes,
391 Object appObj, boolean isFromUt) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530392
393 // process storing operations.
sonu guptaeff184b2016-11-24 12:43:49 +0530394 YangNode schemaNode = findNodeWhichShouldBeReg(service.getName(), nodes);
395 if (schemaNode != null) {
396 if (appObj != null) {
397 appObjectStore.put(service.getName(), appObj);
398 }
399 //Process application context for registrations.
400 processApplicationContext(schemaNode, service.getName(), isFromUt);
401 //Update YANG file store.
402 updateYangFileStore(schemaNode, jarPath);
403 }
404 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530405
sonu guptaeff184b2016-11-24 12:43:49 +0530406 /**
407 * Returns the node for which corresponding class is generated.
408 *
409 * @param name generated class name
410 * @param nodes list of yang nodes
411 * @return node for which corresponding class is generated
412 */
413 private YangNode findNodeWhichShouldBeReg(String name, List<YangNode> nodes) {
414 for (YangNode node : nodes) {
415 if (name.equals(getServiceName(node)) ||
416 name.equals(getInterfaceClassName(node))) {
417 return node;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530418 }
419 }
sonu guptaeff184b2016-11-24 12:43:49 +0530420 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530421 }
422
423 /**
424 * Verifies if service class should be registered or not.
425 *
426 * @param appObject application object
427 * @param appClass application class
428 */
429 private void verifyApplicationRegistration(Object appObject,
430 Class<?> appClass) {
431 Class<?> managerClass = appObject.getClass();
432 Class<?>[] services = managerClass.getInterfaces();
433 List<Class<?>> classes = new ArrayList<>();
434 Collections.addAll(classes, services);
435 if (!classes.contains(appClass)) {
436 throw new RuntimeException("service class " + appClass.getName() +
437 "is not being implemented by " +
438 managerClass.getName());
439 }
440 }
441
442 /**
443 * Verifies if application is already registered with YMS.
444 *
445 * @param appClass application class
446 * @return true if application already registered
447 */
448 private boolean verifyIfApplicationAlreadyRegistered(Class<?> appClass) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530449 String appName = appClass.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530450 return appObjectStore.containsKey(appName) ||
451 interfaceNameKeyStore.containsKey(appName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530452 }
453
454 /**
455 * Updates yang file store for YANG node.
456 *
457 * @param node YANG node
458 * @param jarPath jar file path
459 */
460 private void updateYangFileStore(YangNode node, String jarPath) {
sonu guptaeff184b2016-11-24 12:43:49 +0530461 yangFileStore.put(getModuleIdentifier(node),
462 getYangFilePath(jarPath, node.getFileName()));
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530463 }
464
465 /**
466 * Returns yang file path.
467 *
468 * @param jarPath jar path
469 * @param metaDataFileName name of yang file from metadata
470 * @return yang file path
471 */
472 private String getYangFilePath(String jarPath, String metaDataFileName) {
sonu guptaeff184b2016-11-24 12:43:49 +0530473 String[] metaData = metaDataFileName.split(SLASH);
474 return jarPath + SLASH + metaData[metaData.length - 1];
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530475 }
476
477 /**
478 * Process jar file for fetching YANG nodes.
479 *
480 * @param path jar file path
481 * @return YANG schema nodes
482 */
483 private List<YangNode> processJarParsingOperations(String path) {
484 //Deserialize data model and get the YANG node set.
sonu guptaeff184b2016-11-24 12:43:49 +0530485 String jar = path + JAR;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530486 try {
sonu guptaeff184b2016-11-24 12:43:49 +0530487 File file = new File(jar);
488 if (file.exists()) {
489 return parseJarFile(path + JAR, path);
490 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530491 } catch (IOException e) {
492 log.error(" failed to parse the jar file in path {} : {} ", path,
493 e.getMessage());
494 }
495 return null;
496 }
497
498 /**
499 * Process an application an updates the maps for YANG schema registry.
500 *
sonu guptaeff184b2016-11-24 12:43:49 +0530501 * @param appNode application YANG schema nodes
502 * @param name class name
503 * @param isFormUt if method is being called from unit tests
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530504 */
sonu guptaeff184b2016-11-24 12:43:49 +0530505 private void processApplicationContext(YangSchemaNode appNode, String name,
506 boolean isFormUt) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530507
sonu guptaeff184b2016-11-24 12:43:49 +0530508 //Update map for which registrations is being called.
509 appNameKeyStore.put(name, appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530510
511 // Updates schema store.
sonu guptaeff184b2016-11-24 12:43:49 +0530512 addToSchemaStore(appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530513 // update interface store.
sonu guptaeff184b2016-11-24 12:43:49 +0530514 interfaceNameKeyStore.put(getInterfaceClassName(appNode), appNode);
515
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530516 //update op param store.
sonu guptaeff184b2016-11-24 12:43:49 +0530517 opParamNameKeyStore.put(getOpParamClassName(appNode), appNode);
Bharat saraswal1b0a39a2016-12-16 16:29:02 +0530518
519 //update namespaceSchema store.
520 nameSpaceSchemaStore.put(appNode.getNameSpace().getModuleNamespace(), appNode);
521
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530522 //Checks if notification is present then update notification store map.
523 String eventSubject = null;
524 try {
525 if (appNode.isNotificationPresent()) {
526 eventSubject = getEventClassName(appNode);
527 }
528 } catch (DataModelException e) {
529 log.error("failed to search notification from schema map : {}",
530 e.getLocalizedMessage());
531 }
532 if (eventSubject != null) {
sonu guptaeff184b2016-11-24 12:43:49 +0530533 eventNameKeyStore.put(eventSubject, appNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530534 }
sonu guptaeff184b2016-11-24 12:43:49 +0530535 if (!isFormUt) {
536 log.info("successfully registered this application {}", name);
537 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530538 }
539
540 /**
541 * Returns jar path from bundle mvnLocationPath.
542 *
543 * @param mvnLocationPath mvnLocationPath of bundle
544 * @return path of jar
545 */
546 private String getJarPathFromBundleLocation(String mvnLocationPath,
547 String currentDirectory) {
548 String path = currentDirectory + SYSTEM;
sonu guptaeff184b2016-11-24 12:43:49 +0530549 if (mvnLocationPath.contains(MAVEN)) {
550 String[] strArray = mvnLocationPath.split(MAVEN);
551 if (strArray[1].contains(File.separator)) {
552 String[] split = strArray[1].split(File.separator);
553 if (split[0].contains(PERIOD)) {
554 String[] groupId = split[0].split(Pattern.quote(PERIOD));
555 return path + groupId[0] + SLASH + groupId[1] + SLASH + split[1] +
556 SLASH + split[2] + SLASH + split[1] + HYPHEN + split[2];
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530557 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530558 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530559 }
sonu guptaeff184b2016-11-24 12:43:49 +0530560 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530561 }
562
563 /**
564 * Returns schema node based on the revision.
565 *
566 * @param name name of the schema node
567 * @return schema node based on the revision
568 */
569 private YangSchemaNode getSchemaNodeUsingSchemaNameWithRev(String name) {
sonu guptaeff184b2016-11-24 12:43:49 +0530570 ConcurrentMap<String, YangSchemaNode> revMap;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530571 YangSchemaNode schemaNode;
572 if (name.contains(AT)) {
573 String[] revArray = name.split(AT);
sonu guptaeff184b2016-11-24 12:43:49 +0530574 revMap = yangSchemaStore.get(revArray[0]);
575 schemaNode = revMap.get(name);
576 if (schemaNode == null) {
577 log.error("{} not found.", name);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530578 }
sonu guptaeff184b2016-11-24 12:43:49 +0530579 return schemaNode;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530580 }
581 if (yangSchemaStore.containsKey(name)) {
sonu guptaeff184b2016-11-24 12:43:49 +0530582 revMap = yangSchemaStore.get(name);
583 if (revMap != null && !revMap.isEmpty()) {
584 YangSchemaNode node = revMap.get(name);
585 if (node != null) {
586 return node;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530587 }
sonu guptaeff184b2016-11-24 12:43:49 +0530588 String revName = getLatestVersion(revMap);
589 return revMap.get(revName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530590 }
591 }
592 log.error("{} not found.", name);
593 return null;
594 }
595
sonu guptaeff184b2016-11-24 12:43:49 +0530596 private String getLatestVersion(ConcurrentMap<String, YangSchemaNode> revMap) {
597 List<String> keys = new ArrayList<>();
598 for (Map.Entry<String, YangSchemaNode> entry : revMap.entrySet()) {
599 keys.add(entry.getKey());
600 }
601 sort(keys);
602 return keys.get(keys.size() - 1);
603 }
604
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530605 /**
606 * Adds schema node when different revision of node has received.
607 *
608 * @param schemaNode schema node
609 */
sonu guptaeff184b2016-11-24 12:43:49 +0530610 private void addToSchemaStore(YangSchemaNode schemaNode) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530611
612 String date = getDateInStringFormat(schemaNode);
613 String name = schemaNode.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530614 String revName = name;
615 if (date != null) {
616 revName = name + AT + date;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530617 }
618 //check if already present.
sonu guptaeff184b2016-11-24 12:43:49 +0530619 if (!yangSchemaStore.containsKey(name)) {
620 ConcurrentMap<String, YangSchemaNode> revStore =
621 new ConcurrentHashMap<>();
622 revStore.put(revName, schemaNode);
623 yangSchemaStore.put(name, revStore);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530624 } else {
sonu guptaeff184b2016-11-24 12:43:49 +0530625 yangSchemaStore.get(name).put(revName, schemaNode);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530626 }
627 }
628
629 /**
630 * Returns date in string format.
631 *
632 * @param schemaNode schema node
633 * @return date in string format
634 */
635 String getDateInStringFormat(YangSchemaNode schemaNode) {
636 if (schemaNode != null) {
637 if (((YangNode) schemaNode).getRevision() != null) {
638 return new SimpleDateFormat(DATE_FORMAT)
639 .format(((YangNode) schemaNode).getRevision()
640 .getRevDate());
641 }
642 }
sonu guptaeff184b2016-11-24 12:43:49 +0530643 return null;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530644 }
645
646 /**
647 * Removes schema node from schema map.
648 *
649 * @param removableNode schema node which needs to be removed
650 */
651 private void removeSchemaNode(YangSchemaNode removableNode) {
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530652 String name = removableNode.getName();
sonu guptaeff184b2016-11-24 12:43:49 +0530653 String revName = name;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530654 String date = getDateInStringFormat(removableNode);
sonu guptaeff184b2016-11-24 12:43:49 +0530655 if (date != null) {
656 revName = name + AT + date;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530657 }
sonu guptaeff184b2016-11-24 12:43:49 +0530658 ConcurrentMap<String, YangSchemaNode> revMap = yangSchemaStore.get(name);
659 if (revMap != null && !revMap.isEmpty() && revMap.size() != 1) {
660 revMap.remove(revName);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530661 } else {
662 yangSchemaStore.remove(removableNode.getName());
663 }
664 }
665
666 /**
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530667 * Adds sub module identifier.
668 *
669 * @param node schema node
670 * @param information module information
671 */
672 private void addSubModuleIdentifier(
673 YangSchemaNode node, DefaultYangModuleInformation information) {
674 List<YangInclude> includeList = new ArrayList<>();
675 if (node instanceof YangModule) {
676 includeList = ((YangModule) node).getIncludeList();
677 } else if (node instanceof YangSubModule) {
678 includeList = ((YangSubModule) node).getIncludeList();
679 }
680 for (YangInclude include : includeList) {
681 information.addSubModuleIdentifiers(getModuleIdentifier(
682 include.getIncludedNode()));
683 }
684 }
685
686 /**
687 * Returns module identifier for schema node.
688 *
689 * @param schemaNode schema node
690 * @return module identifier for schema node
691 */
692 private YangModuleIdentifier getModuleIdentifier(
693 YangSchemaNode schemaNode) {
694 return new DefaultYangModuleIdentifier(
695 schemaNode.getName(), getDateInStringFormat(schemaNode));
696 }
697
698 /**
sonu guptaeff184b2016-11-24 12:43:49 +0530699 * Returns schema node's generated interface class name.
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530700 *
sonu guptaeff184b2016-11-24 12:43:49 +0530701 * @param schemaNode schema node
702 * @return schema node's generated interface class name
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530703 */
sonu guptaeff184b2016-11-24 12:43:49 +0530704 String getInterfaceClassName(YangSchemaNode schemaNode) {
705 return schemaNode.getJavaPackage() + PERIOD +
706 getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType());
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530707 }
sonu guptaeff184b2016-11-24 12:43:49 +0530708
709 /**
710 * Returns schema node's generated op param class name.
711 *
712 * @param schemaNode schema node
713 * @return schema node's generated op param class name
714 */
715 private String getOpParamClassName(YangSchemaNode schemaNode) {
716 return getInterfaceClassName(schemaNode) + OP_PARAM;
717 }
718
719 /**
720 * Returns schema node's generated event class name.
721 *
722 * @param schemaNode schema node
723 * @return schema node's generated event class name
724 */
725 private String getEventClassName(YangSchemaNode schemaNode) {
726 return getInterfaceClassName(schemaNode).toLowerCase() + PERIOD +
727 getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType()) +
728 EVENT_STRING;
729 }
730
731 /**
732 * Returns schema node's generated service class name.
733 *
734 * @param schemaNode schema node
735 * @return schema node's generated service class name
736 */
737 String getServiceName(YangSchemaNode schemaNode) {
738 return getInterfaceClassName(schemaNode) + SERVICE;
739 }
740
741 /**
742 * Removes YSR generated temporary resources.
743 *
744 * @param rscPath resource path
745 * @param appName application name
746 */
747 private void removeYsrGeneratedTemporaryResources(String rscPath,
748 String appName) {
749 if (rscPath != null) {
750 File jarPath = new File(rscPath);
751 if (jarPath.exists()) {
752 try {
753 deleteDirectory(jarPath);
754 } catch (IOException e) {
755 log.error("failed to delete ysr resources for {} : {}",
756 appName, e.getLocalizedMessage());
757 }
758 }
759 }
760 }
761}