Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 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) { |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 143 | synchronized (DefaultYangSchemaRegistry.class) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 144 | doPreProcessing(serviceClass, appObject); |
| 145 | if (!verifyIfApplicationAlreadyRegistered(serviceClass)) { |
| 146 | BundleContext context = getBundle(serviceClass).getBundleContext(); |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 147 | 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 171 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 172 | @Override |
| 173 | public void unRegisterApplication(Object managerObject, |
| 174 | Class<?> serviceClass) { |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 175 | synchronized (DefaultYangSchemaRegistry.class) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 176 | 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 saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame] | 199 | nameSpaceSchemaStore.remove(curNode.getNameSpace() |
| 200 | .getModuleNamespace()); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 201 | 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 saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame] | 280 | 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 290 | 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 saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 301 | synchronized (DefaultYangSchemaRegistry.class) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 302 | 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 saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame] | 335 | nameSpaceSchemaStore.clear(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void processModuleLibrary(String serviceName, |
| 340 | YangModuleLibrary library) { |
Bharat saraswal | a899a21 | 2017-02-28 13:19:57 +0530 | [diff] [blame] | 341 | synchronized (DefaultYangSchemaRegistry.class) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 342 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 357 | * Process service class. |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 358 | * |
| 359 | * @param serviceClass service class |
| 360 | * @param appObject application object |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 361 | */ |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 362 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 363 | void doPreProcessing(Class<?> serviceClass, Object appObject) { |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 364 | |
| 365 | //Check if service should be registered? |
| 366 | if (appObject != null) { |
| 367 | verifyApplicationRegistration(appObject, serviceClass); |
| 368 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 369 | String name = serviceClass.getName(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 370 | //Add app class to registered service store. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 371 | if (!registerClassStore.containsKey(name)) { |
| 372 | registerClassStore.put(name, serviceClass); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 373 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 374 | } |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 392 | |
| 393 | // process storing operations. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 394 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 405 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 406 | /** |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 418 | } |
| 419 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 420 | return null; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 421 | } |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 449 | String appName = appClass.getName(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 450 | return appObjectStore.containsKey(appName) || |
| 451 | interfaceNameKeyStore.containsKey(appName); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 452 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 461 | yangFileStore.put(getModuleIdentifier(node), |
| 462 | getYangFilePath(jarPath, node.getFileName())); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 463 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 473 | String[] metaData = metaDataFileName.split(SLASH); |
| 474 | return jarPath + SLASH + metaData[metaData.length - 1]; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 475 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 485 | String jar = path + JAR; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 486 | try { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 487 | File file = new File(jar); |
| 488 | if (file.exists()) { |
| 489 | return parseJarFile(path + JAR, path); |
| 490 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 491 | } 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 501 | * @param appNode application YANG schema nodes |
| 502 | * @param name class name |
| 503 | * @param isFormUt if method is being called from unit tests |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 504 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 505 | private void processApplicationContext(YangSchemaNode appNode, String name, |
| 506 | boolean isFormUt) { |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 507 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 508 | //Update map for which registrations is being called. |
| 509 | appNameKeyStore.put(name, appNode); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 510 | |
| 511 | // Updates schema store. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 512 | addToSchemaStore(appNode); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 513 | // update interface store. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 514 | interfaceNameKeyStore.put(getInterfaceClassName(appNode), appNode); |
| 515 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 516 | //update op param store. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 517 | opParamNameKeyStore.put(getOpParamClassName(appNode), appNode); |
Bharat saraswal | 1b0a39a | 2016-12-16 16:29:02 +0530 | [diff] [blame] | 518 | |
| 519 | //update namespaceSchema store. |
| 520 | nameSpaceSchemaStore.put(appNode.getNameSpace().getModuleNamespace(), appNode); |
| 521 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 522 | //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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 533 | eventNameKeyStore.put(eventSubject, appNode); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 534 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 535 | if (!isFormUt) { |
| 536 | log.info("successfully registered this application {}", name); |
| 537 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 538 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 549 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 557 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 558 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 559 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 560 | return null; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 561 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 570 | ConcurrentMap<String, YangSchemaNode> revMap; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 571 | YangSchemaNode schemaNode; |
| 572 | if (name.contains(AT)) { |
| 573 | String[] revArray = name.split(AT); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 574 | revMap = yangSchemaStore.get(revArray[0]); |
| 575 | schemaNode = revMap.get(name); |
| 576 | if (schemaNode == null) { |
| 577 | log.error("{} not found.", name); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 578 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 579 | return schemaNode; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 580 | } |
| 581 | if (yangSchemaStore.containsKey(name)) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 582 | revMap = yangSchemaStore.get(name); |
| 583 | if (revMap != null && !revMap.isEmpty()) { |
| 584 | YangSchemaNode node = revMap.get(name); |
| 585 | if (node != null) { |
| 586 | return node; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 587 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 588 | String revName = getLatestVersion(revMap); |
| 589 | return revMap.get(revName); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | log.error("{} not found.", name); |
| 593 | return null; |
| 594 | } |
| 595 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 596 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 605 | /** |
| 606 | * Adds schema node when different revision of node has received. |
| 607 | * |
| 608 | * @param schemaNode schema node |
| 609 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 610 | private void addToSchemaStore(YangSchemaNode schemaNode) { |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 611 | |
| 612 | String date = getDateInStringFormat(schemaNode); |
| 613 | String name = schemaNode.getName(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 614 | String revName = name; |
| 615 | if (date != null) { |
| 616 | revName = name + AT + date; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 617 | } |
| 618 | //check if already present. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 619 | if (!yangSchemaStore.containsKey(name)) { |
| 620 | ConcurrentMap<String, YangSchemaNode> revStore = |
| 621 | new ConcurrentHashMap<>(); |
| 622 | revStore.put(revName, schemaNode); |
| 623 | yangSchemaStore.put(name, revStore); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 624 | } else { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 625 | yangSchemaStore.get(name).put(revName, schemaNode); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 626 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 643 | return null; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 644 | } |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 652 | String name = removableNode.getName(); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 653 | String revName = name; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 654 | String date = getDateInStringFormat(removableNode); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 655 | if (date != null) { |
| 656 | revName = name + AT + date; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 657 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 658 | ConcurrentMap<String, YangSchemaNode> revMap = yangSchemaStore.get(name); |
| 659 | if (revMap != null && !revMap.isEmpty() && revMap.size() != 1) { |
| 660 | revMap.remove(revName); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 661 | } else { |
| 662 | yangSchemaStore.remove(removableNode.getName()); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /** |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 667 | * 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 699 | * Returns schema node's generated interface class name. |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 700 | * |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 701 | * @param schemaNode schema node |
| 702 | * @return schema node's generated interface class name |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 703 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 704 | String getInterfaceClassName(YangSchemaNode schemaNode) { |
| 705 | return schemaNode.getJavaPackage() + PERIOD + |
| 706 | getCapitalCase(schemaNode.getJavaClassNameOrBuiltInType()); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 707 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 708 | |
| 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 | } |