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 | |
| 19 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network1.rev20151208.IetfNetwork1Service; |
| 20 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 21 | |
| 22 | import java.io.File; |
| 23 | import java.io.IOException; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; |
| 27 | import static org.onosproject.yangutils.utils.UtilConstants.TEMP; |
| 28 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory; |
| 29 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCapitalCase; |
| 30 | |
| 31 | /** |
| 32 | * Represents mock bundle context. provides bundle context for YSR to do unit |
| 33 | * testing. |
| 34 | */ |
| 35 | public class TestYangSchemaNodeProvider { |
| 36 | |
| 37 | private static final String FS = File.separator; |
| 38 | private static final String PATH = System.getProperty("user.dir") + |
| 39 | FS + "target" + FS + "classes" + FS; |
| 40 | private static final String SER_FILE_PATH = "yang" + FS + "resources" + |
| 41 | FS + "YangMetaData.ser"; |
| 42 | private static final String TEMP_FOLDER_PATH = PATH + TEMP; |
| 43 | private final DefaultYangSchemaRegistry defaultYangSchemaRegistry = |
| 44 | new DefaultYangSchemaRegistry("module-id"); |
| 45 | |
| 46 | /** |
| 47 | * Creates an instance of mock bundle context. |
| 48 | */ |
| 49 | public TestYangSchemaNodeProvider() { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Process YANG schema node for a application. |
| 54 | * |
| 55 | * @param appObject application object |
| 56 | */ |
| 57 | public void processSchemaRegistry(Object appObject) { |
| 58 | |
| 59 | Set<YangSchemaNode> appNode = defaultYangSchemaRegistry |
| 60 | .deSerializeDataModel(PATH + SER_FILE_PATH); |
| 61 | YsrAppContext appContext = new YsrAppContext(); |
| 62 | defaultYangSchemaRegistry.ysrContextForSchemaStore(appContext); |
| 63 | defaultYangSchemaRegistry |
| 64 | .setClassLoader(this.getClass().getClassLoader()); |
| 65 | String appName; |
| 66 | for (YangSchemaNode node : appNode) { |
| 67 | defaultYangSchemaRegistry.processApplicationContext(node); |
| 68 | defaultYangSchemaRegistry.ysrAppContext().appObject(appObject); |
| 69 | defaultYangSchemaRegistry.ysrContextForAppStore() |
| 70 | .appObject(appObject); |
| 71 | defaultYangSchemaRegistry.ysrContextForSchemaStore() |
| 72 | .appObject(appObject); |
| 73 | appName = node.getJavaPackage() + PERIOD + |
| 74 | getCapitalCase(node.getJavaClassNameOrBuiltInType()); |
| 75 | storeClasses(appName); |
| 76 | } |
| 77 | |
| 78 | try { |
| 79 | deleteDirectory(TEMP_FOLDER_PATH); |
| 80 | } catch (IOException e) { |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Stores test generated class to YSR store. |
| 86 | * |
| 87 | * @param name name of class |
| 88 | */ |
| 89 | private void storeClasses(String name) { |
| 90 | ClassLoader classLoader = this.getClass().getClassLoader(); |
| 91 | if (getDefaultYangSchemaRegistry().verifyClassExistence(name)) { |
| 92 | try { |
| 93 | Class<?> nodeClass = classLoader.loadClass(name); |
| 94 | getDefaultYangSchemaRegistry().updateServiceClass(nodeClass); |
| 95 | } catch (ClassNotFoundException e) { |
| 96 | e.printStackTrace(); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Unregisters services. |
| 103 | * |
| 104 | * @param appName application name |
| 105 | */ |
| 106 | public void unregisterService(String appName) { |
| 107 | |
| 108 | if (getDefaultYangSchemaRegistry().verifyClassExistence(appName)) { |
| 109 | try { |
| 110 | Class<?> curClass = Class.forName(appName); |
| 111 | getDefaultYangSchemaRegistry() |
| 112 | .unRegisterApplication(null, curClass); |
| 113 | } catch (ClassNotFoundException e) { |
| 114 | e.printStackTrace(); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns schema registry. |
| 121 | * |
| 122 | * @return schema registry |
| 123 | */ |
| 124 | public DefaultYangSchemaRegistry getDefaultYangSchemaRegistry() { |
| 125 | return defaultYangSchemaRegistry; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Process registration of a service. |
| 130 | */ |
| 131 | public void processRegistrationOfApp() { |
| 132 | getDefaultYangSchemaRegistry() |
| 133 | .processRegistration(IetfNetwork1Service.class, |
| 134 | new MockIetfManager(), "target"); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | } |