Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [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 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 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; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 25 | import org.onosproject.yms.ysr.YangModuleIdentifier; |
| 26 | import org.onosproject.yms.ysr.YangModuleInformation; |
| 27 | import org.onosproject.yms.ysr.YangModuleLibrary; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 28 | import org.osgi.framework.Bundle; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 29 | import org.osgi.framework.BundleContext; |
| 30 | import org.slf4j.Logger; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 31 | |
| 32 | import java.io.File; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 33 | import java.io.IOException; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 34 | import java.text.SimpleDateFormat; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.Collections; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 37 | import java.util.List; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 38 | import java.util.Map; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 39 | import java.util.concurrent.ConcurrentHashMap; |
| 40 | import java.util.concurrent.ConcurrentMap; |
| 41 | import java.util.regex.Pattern; |
| 42 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 43 | import static java.util.Collections.sort; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 44 | import static org.apache.commons.io.FileUtils.deleteDirectory; |
| 45 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.parseJarFile; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.utils.UtilConstants.EVENT_STRING; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 47 | import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 48 | import static org.onosproject.yangutils.utils.UtilConstants.OP_PARAM; |
| 49 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 50 | import static org.onosproject.yangutils.utils.UtilConstants.SERVICE; |
| 51 | import static org.onosproject.yangutils.utils.UtilConstants.SLASH; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 52 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 53 | import static org.osgi.framework.FrameworkUtil.getBundle; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 54 | import static org.slf4j.LoggerFactory.getLogger; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 55 | |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 62 | public class DefaultYangSchemaRegistry implements YangSchemaRegistry { |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 63 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 64 | private static final String SYSTEM = SLASH + "system" + SLASH; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 65 | private static final String MAVEN = "mvn:"; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 66 | private static final String JAR = ".jar"; |
| 67 | private static final String USER_DIRECTORY = "user.dir"; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 68 | private static final String AT = "@"; |
| 69 | private static final String DATE_FORMAT = "yyyy-mm-dd"; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 70 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 108 | private final ConcurrentMap<String, Class<?>> registerClassStore; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 109 | |
| 110 | /* |
| 111 | * Map for storing YANG file details. |
| 112 | */ |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 113 | private final ConcurrentMap<YangModuleIdentifier, String> yangFileStore; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 114 | |
Bharat saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame^] | 115 | /** |
| 116 | * Map for storing schema nodes with respect to namespace. |
| 117 | */ |
| 118 | private final ConcurrentMap<String, YangSchemaNode> nameSpaceSchemaStore; |
| 119 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 120 | private final ConcurrentMap<Object, Boolean> ynhRegistrationStore; |
| 121 | private final ConcurrentMap<String, String> jarPathStore; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * Creates an instance of default YANG schema registry. |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 125 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 126 | public DefaultYangSchemaRegistry() { |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 127 | appObjectStore = new ConcurrentHashMap<>(); |
| 128 | yangSchemaStore = new ConcurrentHashMap<>(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 129 | interfaceNameKeyStore = new ConcurrentHashMap<>(); |
| 130 | opParamNameKeyStore = new ConcurrentHashMap<>(); |
| 131 | eventNameKeyStore = new ConcurrentHashMap<>(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 132 | registerClassStore = new ConcurrentHashMap<>(); |
| 133 | yangFileStore = new ConcurrentHashMap<>(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 134 | appNameKeyStore = new ConcurrentHashMap<>(); |
| 135 | ynhRegistrationStore = new ConcurrentHashMap<>(); |
| 136 | jarPathStore = new ConcurrentHashMap<>(); |
Bharat saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame^] | 137 | nameSpaceSchemaStore = new ConcurrentHashMap<>(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | @Override |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 142 | public void registerApplication(Object appObject, Class<?> serviceClass) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 143 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 169 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 170 | @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 saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame^] | 197 | nameSpaceSchemaStore.remove(curNode.getNameSpace() |
| 198 | .getModuleNamespace()); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 199 | 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 saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame^] | 278 | 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 288 | 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 saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame^] | 333 | nameSpaceSchemaStore.clear(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 334 | } |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /** |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 355 | * Process service class. |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 356 | * |
| 357 | * @param serviceClass service class |
| 358 | * @param appObject application object |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 359 | */ |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 360 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 361 | void doPreProcessing(Class<?> serviceClass, Object appObject) { |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 362 | |
| 363 | //Check if service should be registered? |
| 364 | if (appObject != null) { |
| 365 | verifyApplicationRegistration(appObject, serviceClass); |
| 366 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 367 | String name = serviceClass.getName(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 368 | //Add app class to registered service store. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 369 | if (!registerClassStore.containsKey(name)) { |
| 370 | registerClassStore.put(name, serviceClass); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 371 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 372 | } |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 390 | |
| 391 | // process storing operations. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 392 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 403 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 404 | /** |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 416 | } |
| 417 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 418 | return null; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 419 | } |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 447 | String appName = appClass.getName(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 448 | return appObjectStore.containsKey(appName) || |
| 449 | interfaceNameKeyStore.containsKey(appName); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 450 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 459 | yangFileStore.put(getModuleIdentifier(node), |
| 460 | getYangFilePath(jarPath, node.getFileName())); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 461 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 471 | String[] metaData = metaDataFileName.split(SLASH); |
| 472 | return jarPath + SLASH + metaData[metaData.length - 1]; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 473 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 483 | String jar = path + JAR; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 484 | try { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 485 | File file = new File(jar); |
| 486 | if (file.exists()) { |
| 487 | return parseJarFile(path + JAR, path); |
| 488 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 489 | } 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 499 | * @param appNode application YANG schema nodes |
| 500 | * @param name class name |
| 501 | * @param isFormUt if method is being called from unit tests |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 502 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 503 | private void processApplicationContext(YangSchemaNode appNode, String name, |
| 504 | boolean isFormUt) { |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 505 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 506 | //Update map for which registrations is being called. |
| 507 | appNameKeyStore.put(name, appNode); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 508 | |
| 509 | // Updates schema store. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 510 | addToSchemaStore(appNode); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 511 | // update interface store. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 512 | interfaceNameKeyStore.put(getInterfaceClassName(appNode), appNode); |
| 513 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 514 | //update op param store. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 515 | opParamNameKeyStore.put(getOpParamClassName(appNode), appNode); |
Bharat saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame^] | 516 | |
| 517 | //update namespaceSchema store. |
| 518 | nameSpaceSchemaStore.put(appNode.getNameSpace().getModuleNamespace(), appNode); |
| 519 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 520 | //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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 531 | eventNameKeyStore.put(eventSubject, appNode); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 532 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 533 | if (!isFormUt) { |
| 534 | log.info("successfully registered this application {}", name); |
| 535 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 536 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 547 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 555 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 556 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 557 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 558 | return null; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 559 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 568 | ConcurrentMap<String, YangSchemaNode> revMap; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 569 | YangSchemaNode schemaNode; |
| 570 | if (name.contains(AT)) { |
| 571 | String[] revArray = name.split(AT); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 572 | revMap = yangSchemaStore.get(revArray[0]); |
| 573 | schemaNode = revMap.get(name); |
| 574 | if (schemaNode == null) { |
| 575 | log.error("{} not found.", name); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 576 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 577 | return schemaNode; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 578 | } |
| 579 | if (yangSchemaStore.containsKey(name)) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 580 | revMap = yangSchemaStore.get(name); |
| 581 | if (revMap != null && !revMap.isEmpty()) { |
| 582 | YangSchemaNode node = revMap.get(name); |
| 583 | if (node != null) { |
| 584 | return node; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 585 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 586 | String revName = getLatestVersion(revMap); |
| 587 | return revMap.get(revName); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | log.error("{} not found.", name); |
| 591 | return null; |
| 592 | } |
| 593 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 594 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 603 | /** |
| 604 | * Adds schema node when different revision of node has received. |
| 605 | * |
| 606 | * @param schemaNode schema node |
| 607 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 608 | private void addToSchemaStore(YangSchemaNode schemaNode) { |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 609 | |
| 610 | String date = getDateInStringFormat(schemaNode); |
| 611 | String name = schemaNode.getName(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 612 | String revName = name; |
| 613 | if (date != null) { |
| 614 | revName = name + AT + date; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 615 | } |
| 616 | //check if already present. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 617 | if (!yangSchemaStore.containsKey(name)) { |
| 618 | ConcurrentMap<String, YangSchemaNode> revStore = |
| 619 | new ConcurrentHashMap<>(); |
| 620 | revStore.put(revName, schemaNode); |
| 621 | yangSchemaStore.put(name, revStore); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 622 | } else { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 623 | yangSchemaStore.get(name).put(revName, schemaNode); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 624 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 641 | return null; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 642 | } |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 650 | String name = removableNode.getName(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 651 | String revName = name; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 652 | String date = getDateInStringFormat(removableNode); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 653 | if (date != null) { |
| 654 | revName = name + AT + date; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 655 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 656 | ConcurrentMap<String, YangSchemaNode> revMap = yangSchemaStore.get(name); |
| 657 | if (revMap != null && !revMap.isEmpty() && revMap.size() != 1) { |
| 658 | revMap.remove(revName); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 659 | } else { |
| 660 | yangSchemaStore.remove(removableNode.getName()); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /** |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 665 | * 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 697 | * Returns schema node's generated interface class name. |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 698 | * |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 699 | * @param schemaNode schema node |
| 700 | * @return schema node's generated interface class name |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 701 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 702 | String getInterfaceClassName(YangSchemaNode schemaNode) { |
| 703 | return schemaNode.getJavaPackage() + PERIOD + |
| 704 | getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType()); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 705 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 706 | |
| 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 | } |