sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
sonu gupta | eff184b | 2016-11-24 12:43:49 +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 | |
| 19 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network1.rev20151208.IetfNetwork1Service; |
| 20 | import org.onosproject.yangutils.datamodel.YangNode; |
| 21 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.io.IOException; |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.List; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.deSerializeDataModel; |
| 30 | import static org.onosproject.yangutils.utils.UtilConstants.TEMP; |
| 31 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory; |
| 32 | |
| 33 | /** |
| 34 | * Represents mock bundle context. provides bundle context for YSR to do unit |
| 35 | * testing. |
| 36 | */ |
| 37 | public class TestYangSchemaNodeProvider { |
| 38 | |
| 39 | private static final String FS = File.separator; |
| 40 | private static final String PATH = System.getProperty("user.dir") + |
| 41 | FS + "target" + FS + "classes" + FS; |
| 42 | private static final String SER_FILE_PATH = "yang" + FS + "resources" + |
| 43 | FS + "YangMetaData.ser"; |
| 44 | private static final String TEMP_FOLDER_PATH = PATH + TEMP; |
| 45 | private final DefaultYangSchemaRegistry registry = |
| 46 | new DefaultYangSchemaRegistry(); |
| 47 | private static final String RESOURCE = "src/test/resources"; |
| 48 | private List<YangNode> nodes = new ArrayList<>(); |
| 49 | |
| 50 | /** |
| 51 | * Creates an instance of mock bundle context. |
| 52 | */ |
| 53 | public TestYangSchemaNodeProvider() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Process YANG schema node for a application. |
| 58 | * |
| 59 | * @param appObject application object |
| 60 | */ |
| 61 | public void processSchemaRegistry(Object appObject) { |
| 62 | try { |
| 63 | Set<YangNode> appNode = deSerializeDataModel(PATH + SER_FILE_PATH); |
| 64 | nodes.addAll(appNode); |
| 65 | String appName; |
| 66 | ClassLoader classLoader = TestYangSchemaNodeProvider.class.getClassLoader(); |
| 67 | for (YangSchemaNode node : nodes) { |
| 68 | appName = registry.getServiceName(node); |
| 69 | Class<?> cls; |
| 70 | try { |
| 71 | cls = classLoader.loadClass(appName); |
| 72 | } catch (ClassNotFoundException e) { |
| 73 | continue; |
| 74 | } |
| 75 | registry.processRegistration(cls, RESOURCE, nodes, appObject, true); |
| 76 | registry.updateServiceClass(cls); |
| 77 | //interface generation. |
| 78 | appName = registry.getInterfaceClassName(node); |
| 79 | try { |
| 80 | cls = classLoader.loadClass(appName); |
| 81 | } catch (ClassNotFoundException e) { |
| 82 | continue; |
| 83 | } |
| 84 | registry.processRegistration(cls, RESOURCE, |
| 85 | nodes, appObject, true); |
| 86 | registry.updateServiceClass(cls); |
| 87 | } |
| 88 | deleteDirectory(TEMP_FOLDER_PATH); |
| 89 | } catch (IOException e) { |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Unregisters services. |
| 95 | * |
| 96 | * @param appName application name |
| 97 | */ |
| 98 | void unregisterService(String appName) { |
| 99 | ClassLoader classLoader = TestYangSchemaNodeProvider.class.getClassLoader(); |
| 100 | try { |
| 101 | Class<?> cls = classLoader.loadClass(appName); |
| 102 | registry.unRegisterApplication(null, cls); |
| 103 | } catch (ClassNotFoundException e) { |
| 104 | } |
| 105 | |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns schema registry. |
| 110 | * |
| 111 | * @return schema registry |
| 112 | */ |
| 113 | public DefaultYangSchemaRegistry getDefaultYangSchemaRegistry() { |
| 114 | return registry; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Process registration of a service. |
| 119 | */ |
| 120 | void processRegistrationOfApp() { |
| 121 | getDefaultYangSchemaRegistry().doPreProcessing(IetfNetwork1Service.class, |
| 122 | new MockIetfManager()); |
| 123 | } |
| 124 | |
| 125 | } |