Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yms.app.ysr; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.YangInclude; |
| 20 | import org.onosproject.yangutils.datamodel.YangModule; |
| 21 | import org.onosproject.yangutils.datamodel.YangNode; |
| 22 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 23 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 24 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 25 | import org.onosproject.yms.ysr.YangModuleIdentifier; |
| 26 | import org.onosproject.yms.ysr.YangModuleInformation; |
| 27 | import org.onosproject.yms.ysr.YangModuleLibrary; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | import java.io.File; |
| 31 | import java.io.IOException; |
| 32 | import java.io.UncheckedIOException; |
| 33 | import java.nio.file.Files; |
| 34 | import java.nio.file.Path; |
| 35 | import java.nio.file.Paths; |
| 36 | import java.text.SimpleDateFormat; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.Collections; |
| 39 | import java.util.List; |
| 40 | import java.util.Map; |
| 41 | import java.util.concurrent.ConcurrentHashMap; |
| 42 | import java.util.concurrent.ConcurrentMap; |
| 43 | import java.util.regex.Pattern; |
| 44 | |
| 45 | import static java.util.Collections.sort; |
| 46 | import static org.apache.commons.io.FileUtils.deleteDirectory; |
| 47 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.deSerializeDataModel; |
| 48 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING; |
| 49 | import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN; |
| 50 | import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM; |
| 51 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 52 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE; |
| 53 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
| 54 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 55 | import static org.slf4j.LoggerFactory.getLogger; |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * Representation of default YANG schema registry. Yang schema registry |
| 60 | * provides interface to an application to register its YANG schema |
| 61 | * with YMS. It provides YANG schema nodes to YDT, YNB and YSB. |
| 62 | */ |
| 63 | public class MockYangSchemaRegistry implements YangSchemaRegistry { |
| 64 | |
| 65 | private static final String SYSTEM = SLASH + "system" + SLASH; |
| 66 | private static final String MAVEN = "mvn:"; |
| 67 | private static final String JAR = ".jar"; |
| 68 | private static final String USER_DIRECTORY = "user.dir"; |
| 69 | private static final String AT = "@"; |
| 70 | private static final String DATE_FORMAT = "yyyy-mm-dd"; |
| 71 | private static final String ONOS = "org.onosproject"; |
| 72 | private static final Logger log = getLogger(MockYangSchemaRegistry.class); |
| 73 | |
| 74 | private static final String FS = File.separator; |
| 75 | private static final String USER_DIR = System.getProperty("user.dir").replaceAll("ea1000driver", "ea1000yang"); |
| 76 | // private static final String BUCK_OUT_DIR = "/buck-out/gen/drivers/microsemi/ea1000yang/"; |
| 77 | private static final String BUCK_OUT_BIN_LOC = |
| 78 | "/buck-out/bin/drivers/microsemi/ea1000yang/" |
| 79 | + "lib__onos-drivers-microsemi-ea1000yang__classes/YangMetaData.ser"; |
| 80 | private static final String PATH = FS + "target" + FS + "classes" + FS; |
| 81 | private static final String SER_FILE_PATH = "yang" + FS + "resources" + |
| 82 | FS + "YangMetaData.ser"; |
| 83 | private static final String RESOURCE = "src/test/resources"; |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | * Map for storing app objects. |
| 88 | */ |
| 89 | private final ConcurrentMap<String, Object> appObjectStore; |
| 90 | |
| 91 | /* |
| 92 | * Map for storing YANG schema nodes. |
| 93 | */ |
| 94 | private final ConcurrentMap<String, ConcurrentMap<String, YangSchemaNode>> |
| 95 | yangSchemaStore; |
| 96 | |
| 97 | /* |
| 98 | * Map for storing YANG schema nodes with respect to root's generated |
| 99 | * interface file name. |
| 100 | */ |
| 101 | private final ConcurrentMap<String, YangSchemaNode> interfaceNameKeyStore; |
| 102 | |
| 103 | /* |
| 104 | * Map for storing YANG schema nodes root's generated op param file name. |
| 105 | */ |
| 106 | private final ConcurrentMap<String, YangSchemaNode> opParamNameKeyStore; |
| 107 | |
| 108 | /* |
| 109 | * Map for storing YANG schema nodes with respect to notifications. |
| 110 | */ |
| 111 | private final ConcurrentMap<String, YangSchemaNode> eventNameKeyStore; |
| 112 | |
| 113 | /* |
| 114 | * Map for storing YANG schema nodes with respect to app name. |
| 115 | */ |
| 116 | private final ConcurrentMap<String, YangSchemaNode> appNameKeyStore; |
| 117 | |
| 118 | /* |
| 119 | * Map for storing registered classes. |
| 120 | */ |
| 121 | private final ConcurrentMap<String, Class<?>> registerClassStore; |
| 122 | |
| 123 | /* |
| 124 | * Map for storing YANG file details. |
| 125 | */ |
| 126 | private final ConcurrentMap<YangModuleIdentifier, String> yangFileStore; |
| 127 | |
| 128 | /** |
| 129 | * Map for storing schema nodes with respect to namespace. |
| 130 | */ |
| 131 | private final ConcurrentMap<String, YangSchemaNode> nameSpaceSchemaStore; |
| 132 | |
| 133 | private final ConcurrentMap<Object, Boolean> ynhRegistrationStore; |
| 134 | private final ConcurrentMap<String, String> jarPathStore; |
| 135 | |
| 136 | /** |
| 137 | * Creates an instance of default YANG schema registry. |
| 138 | */ |
| 139 | public MockYangSchemaRegistry() { |
| 140 | appObjectStore = new ConcurrentHashMap<>(); |
| 141 | yangSchemaStore = new ConcurrentHashMap<>(); |
| 142 | interfaceNameKeyStore = new ConcurrentHashMap<>(); |
| 143 | opParamNameKeyStore = new ConcurrentHashMap<>(); |
| 144 | eventNameKeyStore = new ConcurrentHashMap<>(); |
| 145 | registerClassStore = new ConcurrentHashMap<>(); |
| 146 | yangFileStore = new ConcurrentHashMap<>(); |
| 147 | appNameKeyStore = new ConcurrentHashMap<>(); |
| 148 | ynhRegistrationStore = new ConcurrentHashMap<>(); |
| 149 | jarPathStore = new ConcurrentHashMap<>(); |
| 150 | nameSpaceSchemaStore = new ConcurrentHashMap<>(); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * This is overridden for Maven and Buck. |
| 156 | * |
| 157 | * Because they don't have a Bundle Context, and the JAR file doesn't exist |
| 158 | * when this is called, we have to work with the .ser file |
| 159 | */ |
| 160 | @Override |
| 161 | public void registerApplication(Object appObject, Class<?> serviceClass) { |
| 162 | synchronized (MockYangSchemaRegistry.class) { |
| 163 | doPreProcessing(serviceClass, appObject); |
| 164 | if (!verifyIfApplicationAlreadyRegistered(serviceClass)) { |
| 165 | |
| 166 | List<YangNode> curNodes = new ArrayList<>(); |
| 167 | Path serFile = Paths.get(USER_DIR, PATH, SER_FILE_PATH); |
| 168 | if (USER_DIR.contains("ea1000yang")) { |
| 169 | serFile = Paths.get(USER_DIR, PATH, SER_FILE_PATH); |
| 170 | } else { |
| 171 | serFile = Paths.get(USER_DIR, BUCK_OUT_BIN_LOC); |
| 172 | } |
| 173 | |
| 174 | if (Files.notExists(serFile)) { |
| 175 | throw new UncheckedIOException( |
| 176 | new IOException("File " + serFile.toString() + " does not exist!")); |
| 177 | } |
| 178 | |
| 179 | try { |
| 180 | curNodes.addAll(deSerializeDataModel(serFile.toString())); |
| 181 | } catch (IOException e) { |
| 182 | throw new UncheckedIOException(e); |
| 183 | } |
| 184 | |
| 185 | // process application registration. |
| 186 | if (curNodes != null && !curNodes.isEmpty()) { |
| 187 | jarPathStore.put(serviceClass.getName(), serFile.toString()); |
| 188 | processRegistration(serviceClass, serFile.toString(), |
| 189 | curNodes, appObject, false); |
| 190 | } else { |
| 191 | throw new UncheckedIOException( |
| 192 | new IOException("Unable to find Yang Nodes in serFile: " + serFile.toString())); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void unRegisterApplication(Object managerObject, |
| 200 | Class<?> serviceClass) { |
| 201 | synchronized (MockYangSchemaRegistry.class) { |
| 202 | YangSchemaNode curNode; |
| 203 | String serviceName = serviceClass.getName(); |
| 204 | |
| 205 | //Check if service should be unregistered? |
| 206 | if (managerObject != null) { |
| 207 | verifyApplicationRegistration(managerObject, serviceClass); |
| 208 | } |
| 209 | //Remove registered class from store. |
| 210 | registerClassStore.remove(serviceName); |
| 211 | //check if service is in app store. |
| 212 | curNode = appNameKeyStore.get(serviceName); |
| 213 | if (curNode == null) { |
| 214 | curNode = interfaceNameKeyStore.get(serviceName); |
| 215 | } |
| 216 | |
| 217 | if (curNode != null) { |
| 218 | removeSchemaNode(curNode); |
| 219 | eventNameKeyStore.remove(getEventClassName(curNode)); |
| 220 | appObjectStore.remove(serviceName); |
| 221 | interfaceNameKeyStore.remove(getInterfaceClassName(curNode)); |
| 222 | opParamNameKeyStore.remove(getOpParamClassName(curNode)); |
| 223 | yangFileStore.remove(getModuleIdentifier(curNode)); |
| 224 | appNameKeyStore.remove(serviceName); |
| 225 | nameSpaceSchemaStore.remove(curNode.getNameSpace() |
| 226 | .getModuleNamespace()); |
| 227 | removeYsrGeneratedTemporaryResources(jarPathStore.get(serviceName), |
| 228 | serviceName); |
| 229 | log.info(" service {} is unregistered.", |
| 230 | serviceClass.getSimpleName()); |
| 231 | } else { |
| 232 | throw new RuntimeException(serviceClass.getSimpleName() + |
| 233 | " service was not registered."); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public Object getRegisteredApplication(YangSchemaNode schemaNode) { |
| 240 | Object obj = null; |
| 241 | if (schemaNode != null) { |
| 242 | String name = getServiceName(schemaNode); |
| 243 | obj = appObjectStore.get(name); |
| 244 | if (obj == null) { |
| 245 | log.error("{} not found.", name); |
| 246 | } |
| 247 | } |
| 248 | return obj; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public YangSchemaNode getYangSchemaNodeUsingSchemaName(String schemaName) { |
| 253 | return getSchemaNodeUsingSchemaNameWithRev(schemaName); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public YangSchemaNode getYangSchemaNodeUsingAppName(String appName) { |
| 258 | YangSchemaNode node = appNameKeyStore.get(appName); |
| 259 | if (node == null) { |
| 260 | log.error("{} not found.", appName); |
| 261 | } |
| 262 | return node; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public YangSchemaNode |
| 267 | getYangSchemaNodeUsingGeneratedRootNodeInterfaceFileName(String name) { |
| 268 | YangSchemaNode node = interfaceNameKeyStore.get(name); |
| 269 | if (node == null) { |
| 270 | log.error("{} not found.", name); |
| 271 | } |
| 272 | return node; |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public YangSchemaNode getYangSchemaNodeUsingGeneratedRootNodeOpPramFileName( |
| 277 | String name) { |
| 278 | YangSchemaNode node = opParamNameKeyStore.get(name); |
| 279 | if (node == null) { |
| 280 | log.error("{} not found.", name); |
| 281 | } |
| 282 | return node; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public YangSchemaNode getRootYangSchemaNodeForNotification(String name) { |
| 287 | YangSchemaNode node = eventNameKeyStore.get(name); |
| 288 | if (node == null) { |
| 289 | log.error("{} not found.", name); |
| 290 | } |
| 291 | return node; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public Class<?> getRegisteredClass(YangSchemaNode schemaNode) { |
| 296 | String interfaceName = getInterfaceClassName(schemaNode); |
| 297 | String serviceName = getServiceName(schemaNode); |
| 298 | Class<?> regClass = registerClassStore.get(serviceName); |
| 299 | if (regClass == null) { |
| 300 | regClass = registerClassStore.get(interfaceName); |
| 301 | } |
| 302 | return regClass; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public YangSchemaNode getSchemaWrtNameSpace(String nameSpace) { |
| 307 | |
| 308 | YangSchemaNode node = nameSpaceSchemaStore.get(nameSpace); |
| 309 | if (node == null) { |
| 310 | log.error("node with {} namespace not found.", nameSpace); |
| 311 | } |
| 312 | return node; |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public String getYangFile(YangModuleIdentifier moduleIdentifier) { |
| 317 | String file = yangFileStore.get(moduleIdentifier); |
| 318 | if (file == null) { |
| 319 | log.error("YANG files for corresponding module identifier {} not " + |
| 320 | "found", moduleIdentifier); |
| 321 | } |
| 322 | return file; |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public boolean verifyNotificationObject(Object appObj, Class<?> service) { |
| 327 | synchronized (MockYangSchemaRegistry.class) { |
| 328 | YangSchemaNode node = appNameKeyStore.get(service.getName()); |
| 329 | if (node == null) { |
| 330 | log.error("application is not registered with YMS {}", |
| 331 | service.getName()); |
| 332 | return false; |
| 333 | } |
| 334 | try { |
| 335 | if (node.isNotificationPresent()) { |
| 336 | if (appObj != null) { |
| 337 | Boolean ifPresent = ynhRegistrationStore.get(appObj); |
| 338 | if (ifPresent == null) { |
| 339 | ynhRegistrationStore.put(appObj, true); |
| 340 | return true; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } catch (DataModelException e) { |
| 345 | log.error("notification registration error: {} {}", e |
| 346 | .getLocalizedMessage(), e); |
| 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public void flushYsrData() { |
| 354 | appObjectStore.clear(); |
| 355 | yangSchemaStore.clear(); |
| 356 | eventNameKeyStore.clear(); |
| 357 | opParamNameKeyStore.clear(); |
| 358 | interfaceNameKeyStore.clear(); |
| 359 | registerClassStore.clear(); |
| 360 | yangFileStore.clear(); |
| 361 | nameSpaceSchemaStore.clear(); |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public void processModuleLibrary(String serviceName, |
| 366 | YangModuleLibrary library) { |
| 367 | synchronized (MockYangSchemaRegistry.class) { |
| 368 | YangSchemaNode node = appNameKeyStore.get(serviceName); |
| 369 | if (node != null) { |
| 370 | YangModuleInformation moduleInformation = |
| 371 | new DefaultYangModuleInformation(getModuleIdentifier(node), |
| 372 | node.getNameSpace()); |
| 373 | addSubModuleIdentifier(node, ( |
| 374 | DefaultYangModuleInformation) moduleInformation); |
| 375 | //TODO: add feature list to module information. |
| 376 | ((DefaultYangModuleLibrary) library) |
| 377 | .addModuleInformation(moduleInformation); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Process service class. |
| 384 | * |
| 385 | * @param serviceClass service class |
| 386 | * @param appObject application object |
| 387 | */ |
| 388 | |
| 389 | void doPreProcessing(Class<?> serviceClass, Object appObject) { |
| 390 | |
| 391 | //Check if service should be registered? |
| 392 | if (appObject != null) { |
| 393 | verifyApplicationRegistration(appObject, serviceClass); |
| 394 | } |
| 395 | String name = serviceClass.getName(); |
| 396 | //Add app class to registered service store. |
| 397 | if (!registerClassStore.containsKey(name)) { |
| 398 | registerClassStore.put(name, serviceClass); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void updateServiceClass(Class<?> service) { |
| 403 | registerClassStore.put(service.getName(), service); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Process application registration. |
| 408 | * |
| 409 | * @param service service class |
| 410 | * @param jarPath jar path |
| 411 | * @param nodes YANG nodes |
| 412 | * @param appObj application object |
| 413 | * @param isFromUt if registration is being called form unit test |
| 414 | */ |
| 415 | void processRegistration(Class<?> service, String jarPath, |
| 416 | List<YangNode> nodes, |
| 417 | Object appObj, boolean isFromUt) { |
| 418 | |
| 419 | // process storing operations. |
| 420 | YangNode schemaNode = findNodeWhichShouldBeReg(service.getName(), nodes); |
| 421 | if (schemaNode != null) { |
| 422 | if (appObj != null) { |
| 423 | appObjectStore.put(service.getName(), appObj); |
| 424 | } |
| 425 | //Process application context for registrations. |
| 426 | processApplicationContext(schemaNode, service.getName(), isFromUt); |
| 427 | //Update YANG file store. |
| 428 | updateYangFileStore(schemaNode, jarPath); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Returns the node for which corresponding class is generated. |
| 434 | * |
| 435 | * @param name generated class name |
| 436 | * @param nodes list of yang nodes |
| 437 | * @return node for which corresponding class is generated |
| 438 | */ |
| 439 | private YangNode findNodeWhichShouldBeReg(String name, List<YangNode> nodes) { |
| 440 | for (YangNode node : nodes) { |
| 441 | if (name.equals(getServiceName(node)) || |
| 442 | name.equals(getInterfaceClassName(node))) { |
| 443 | return node; |
| 444 | } |
| 445 | } |
| 446 | return null; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Verifies if service class should be registered or not. |
| 451 | * |
| 452 | * @param appObject application object |
| 453 | * @param appClass application class |
| 454 | */ |
| 455 | private void verifyApplicationRegistration(Object appObject, |
| 456 | Class<?> appClass) { |
| 457 | Class<?> managerClass = appObject.getClass(); |
| 458 | Class<?>[] services = managerClass.getInterfaces(); |
| 459 | List<Class<?>> classes = new ArrayList<>(); |
| 460 | Collections.addAll(classes, services); |
| 461 | if (!classes.contains(appClass)) { |
| 462 | throw new RuntimeException("service class " + appClass.getName() + |
| 463 | "is not being implemented by " + |
| 464 | managerClass.getName()); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Verifies if application is already registered with YMS. |
| 470 | * |
| 471 | * @param appClass application class |
| 472 | * @return true if application already registered |
| 473 | */ |
| 474 | private boolean verifyIfApplicationAlreadyRegistered(Class<?> appClass) { |
| 475 | String appName = appClass.getName(); |
| 476 | return appObjectStore.containsKey(appName) || |
| 477 | interfaceNameKeyStore.containsKey(appName); |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Updates yang file store for YANG node. |
| 482 | * |
| 483 | * @param node YANG node |
| 484 | * @param jarPath jar file path |
| 485 | */ |
| 486 | private void updateYangFileStore(YangNode node, String jarPath) { |
| 487 | yangFileStore.put(getModuleIdentifier(node), |
| 488 | getYangFilePath(jarPath, node.getFileName())); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Returns yang file path. |
| 493 | * |
| 494 | * @param jarPath jar path |
| 495 | * @param metaDataFileName name of yang file from metadata |
| 496 | * @return yang file path |
| 497 | */ |
| 498 | private String getYangFilePath(String jarPath, String metaDataFileName) { |
| 499 | String[] metaData = metaDataFileName.split(SLASH); |
| 500 | return jarPath + SLASH + metaData[metaData.length - 1]; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /** |
| 505 | * Process an application an updates the maps for YANG schema registry. |
| 506 | * |
| 507 | * @param appNode application YANG schema nodes |
| 508 | * @param name class name |
| 509 | * @param isFormUt if method is being called from unit tests |
| 510 | */ |
| 511 | private void processApplicationContext(YangSchemaNode appNode, String name, |
| 512 | boolean isFormUt) { |
| 513 | |
| 514 | //Update map for which registrations is being called. |
| 515 | appNameKeyStore.put(name, appNode); |
| 516 | |
| 517 | // Updates schema store. |
| 518 | addToSchemaStore(appNode); |
| 519 | // update interface store. |
| 520 | interfaceNameKeyStore.put(getInterfaceClassName(appNode), appNode); |
| 521 | |
| 522 | //update op param store. |
| 523 | opParamNameKeyStore.put(getOpParamClassName(appNode), appNode); |
| 524 | |
| 525 | //update namespaceSchema store. |
| 526 | nameSpaceSchemaStore.put(appNode.getNameSpace().getModuleNamespace(), appNode); |
| 527 | |
| 528 | //Checks if notification is present then update notification store map. |
| 529 | String eventSubject = null; |
| 530 | try { |
| 531 | if (appNode.isNotificationPresent()) { |
| 532 | eventSubject = getEventClassName(appNode); |
| 533 | } |
| 534 | } catch (DataModelException e) { |
| 535 | log.error("failed to search notification from schema map : {}", |
| 536 | e.getLocalizedMessage()); |
| 537 | } |
| 538 | if (eventSubject != null) { |
| 539 | eventNameKeyStore.put(eventSubject, appNode); |
| 540 | } |
| 541 | if (!isFormUt) { |
| 542 | log.info("successfully registered this application {}", name); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Returns jar path from bundle mvnLocationPath. |
| 548 | * |
| 549 | * @param mvnLocationPath mvnLocationPath of bundle |
| 550 | * @return path of jar |
| 551 | */ |
| 552 | private String getJarPathFromBundleLocation(String mvnLocationPath, |
| 553 | String currentDirectory) { |
| 554 | String path = currentDirectory + SYSTEM; |
| 555 | if (mvnLocationPath.contains(MAVEN)) { |
| 556 | String[] strArray = mvnLocationPath.split(MAVEN); |
| 557 | if (strArray[1].contains(File.separator)) { |
| 558 | String[] split = strArray[1].split(File.separator); |
| 559 | if (split[0].contains(PERIOD)) { |
| 560 | String[] groupId = split[0].split(Pattern.quote(PERIOD)); |
| 561 | return path + groupId[0] + SLASH + groupId[1] + SLASH + split[1] + |
| 562 | SLASH + split[2] + SLASH + split[1] + HYPHEN + split[2]; |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | return null; |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Returns schema node based on the revision. |
| 571 | * |
| 572 | * @param name name of the schema node |
| 573 | * @return schema node based on the revision |
| 574 | */ |
| 575 | private YangSchemaNode getSchemaNodeUsingSchemaNameWithRev(String name) { |
| 576 | ConcurrentMap<String, YangSchemaNode> revMap; |
| 577 | YangSchemaNode schemaNode; |
| 578 | if (name.contains(AT)) { |
| 579 | String[] revArray = name.split(AT); |
| 580 | revMap = yangSchemaStore.get(revArray[0]); |
| 581 | schemaNode = revMap.get(name); |
| 582 | if (schemaNode == null) { |
| 583 | log.error("{} not found.", name); |
| 584 | } |
| 585 | return schemaNode; |
| 586 | } |
| 587 | if (yangSchemaStore.containsKey(name)) { |
| 588 | revMap = yangSchemaStore.get(name); |
| 589 | if (revMap != null && !revMap.isEmpty()) { |
| 590 | YangSchemaNode node = revMap.get(name); |
| 591 | if (node != null) { |
| 592 | return node; |
| 593 | } |
| 594 | String revName = getLatestVersion(revMap); |
| 595 | return revMap.get(revName); |
| 596 | } |
| 597 | } |
| 598 | log.error("{} not found.", name); |
| 599 | return null; |
| 600 | } |
| 601 | |
| 602 | private String getLatestVersion(ConcurrentMap<String, YangSchemaNode> revMap) { |
| 603 | List<String> keys = new ArrayList<>(); |
| 604 | for (Map.Entry<String, YangSchemaNode> entry : revMap.entrySet()) { |
| 605 | keys.add(entry.getKey()); |
| 606 | } |
| 607 | sort(keys); |
| 608 | return keys.get(keys.size() - 1); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Adds schema node when different revision of node has received. |
| 613 | * |
| 614 | * @param schemaNode schema node |
| 615 | */ |
| 616 | private void addToSchemaStore(YangSchemaNode schemaNode) { |
| 617 | |
| 618 | String date = getDateInStringFormat(schemaNode); |
| 619 | String name = schemaNode.getName(); |
| 620 | String revName = name; |
| 621 | if (date != null) { |
| 622 | revName = name + AT + date; |
| 623 | } |
| 624 | //check if already present. |
| 625 | if (!yangSchemaStore.containsKey(name)) { |
| 626 | ConcurrentMap<String, YangSchemaNode> revStore = |
| 627 | new ConcurrentHashMap<>(); |
| 628 | revStore.put(revName, schemaNode); |
| 629 | yangSchemaStore.put(name, revStore); |
| 630 | } else { |
| 631 | yangSchemaStore.get(name).put(revName, schemaNode); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * Returns date in string format. |
| 637 | * |
| 638 | * @param schemaNode schema node |
| 639 | * @return date in string format |
| 640 | */ |
| 641 | String getDateInStringFormat(YangSchemaNode schemaNode) { |
| 642 | if (schemaNode != null) { |
| 643 | if (((YangNode) schemaNode).getRevision() != null) { |
| 644 | return new SimpleDateFormat(DATE_FORMAT) |
| 645 | .format(((YangNode) schemaNode).getRevision() |
| 646 | .getRevDate()); |
| 647 | } |
| 648 | } |
| 649 | return null; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Removes schema node from schema map. |
| 654 | * |
| 655 | * @param removableNode schema node which needs to be removed |
| 656 | */ |
| 657 | private void removeSchemaNode(YangSchemaNode removableNode) { |
| 658 | String name = removableNode.getName(); |
| 659 | String revName = name; |
| 660 | String date = getDateInStringFormat(removableNode); |
| 661 | if (date != null) { |
| 662 | revName = name + AT + date; |
| 663 | } |
| 664 | ConcurrentMap<String, YangSchemaNode> revMap = yangSchemaStore.get(name); |
| 665 | if (revMap != null && !revMap.isEmpty() && revMap.size() != 1) { |
| 666 | revMap.remove(revName); |
| 667 | } else { |
| 668 | yangSchemaStore.remove(removableNode.getName()); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Adds sub module identifier. |
| 674 | * |
| 675 | * @param node schema node |
| 676 | * @param information module information |
| 677 | */ |
| 678 | private void addSubModuleIdentifier( |
| 679 | YangSchemaNode node, DefaultYangModuleInformation information) { |
| 680 | List<YangInclude> includeList = new ArrayList<>(); |
| 681 | if (node instanceof YangModule) { |
| 682 | includeList = ((YangModule) node).getIncludeList(); |
| 683 | } else if (node instanceof YangSubModule) { |
| 684 | includeList = ((YangSubModule) node).getIncludeList(); |
| 685 | } |
| 686 | for (YangInclude include : includeList) { |
| 687 | information.addSubModuleIdentifiers(getModuleIdentifier( |
| 688 | include.getIncludedNode())); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Returns module identifier for schema node. |
| 694 | * |
| 695 | * @param schemaNode schema node |
| 696 | * @return module identifier for schema node |
| 697 | */ |
| 698 | private YangModuleIdentifier getModuleIdentifier( |
| 699 | YangSchemaNode schemaNode) { |
| 700 | return new DefaultYangModuleIdentifier( |
| 701 | schemaNode.getName(), getDateInStringFormat(schemaNode)); |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Returns schema node's generated interface class name. |
| 706 | * |
| 707 | * @param schemaNode schema node |
| 708 | * @return schema node's generated interface class name |
| 709 | */ |
| 710 | String getInterfaceClassName(YangSchemaNode schemaNode) { |
| 711 | return schemaNode.getJavaPackage() + PERIOD + |
| 712 | getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType()); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * Returns schema node's generated op param class name. |
| 717 | * |
| 718 | * @param schemaNode schema node |
| 719 | * @return schema node's generated op param class name |
| 720 | */ |
| 721 | private String getOpParamClassName(YangSchemaNode schemaNode) { |
| 722 | return getInterfaceClassName(schemaNode) + OP_PARAM; |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * Returns schema node's generated event class name. |
| 727 | * |
| 728 | * @param schemaNode schema node |
| 729 | * @return schema node's generated event class name |
| 730 | */ |
| 731 | private String getEventClassName(YangSchemaNode schemaNode) { |
| 732 | return getInterfaceClassName(schemaNode).toLowerCase() + PERIOD + |
| 733 | getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType()) + |
| 734 | EVENT_STRING; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * Returns schema node's generated service class name. |
| 739 | * |
| 740 | * @param schemaNode schema node |
| 741 | * @return schema node's generated service class name |
| 742 | */ |
| 743 | String getServiceName(YangSchemaNode schemaNode) { |
| 744 | return getInterfaceClassName(schemaNode) + SERVICE; |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Removes YSR generated temporary resources. |
| 749 | * |
| 750 | * @param rscPath resource path |
| 751 | * @param appName application name |
| 752 | */ |
| 753 | private void removeYsrGeneratedTemporaryResources(String rscPath, |
| 754 | String appName) { |
| 755 | if (rscPath != null) { |
| 756 | File jarPath = new File(rscPath); |
| 757 | if (jarPath.exists()) { |
| 758 | try { |
| 759 | deleteDirectory(jarPath); |
| 760 | } catch (IOException e) { |
| 761 | log.error("failed to delete ysr resources for {} : {}", |
| 762 | appName, e.getLocalizedMessage()); |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |