Sean Condon | 16df3b1 | 2017-06-09 15:14:01 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | package org.onosproject.yang.serializers.xml; |
| 17 | |
| 18 | |
| 19 | import org.onosproject.yang.compiler.datamodel.YangNode; |
| 20 | import org.onosproject.yang.compiler.datamodel.YangSchemaNode; |
| 21 | import org.onosproject.yang.model.YangModel; |
| 22 | import org.onosproject.yang.runtime.AppModuleInfo; |
| 23 | import org.onosproject.yang.runtime.DefaultAppModuleInfo; |
| 24 | import org.onosproject.yang.runtime.DefaultModelRegistrationParam; |
| 25 | import org.onosproject.yang.runtime.ModelRegistrationParam; |
| 26 | import org.onosproject.yang.runtime.YangModelRegistry; |
| 27 | import org.onosproject.yang.runtime.impl.DefaultYangModelRegistry; |
| 28 | |
| 29 | import java.io.File; |
| 30 | import java.io.IOException; |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.Iterator; |
| 33 | import java.util.List; |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import org.onosproject.yang.compiler.datamodel.utils.DataModelUtils; |
| 37 | import org.onosproject.yang.compiler.utils.UtilConstants; |
| 38 | import org.onosproject.yang.compiler.utils.io.impl.YangIoUtils; |
| 39 | import org.onosproject.yang.runtime.helperutils.YangApacheUtils; |
| 40 | import org.onosproject.yang.runtime.RuntimeHelper; |
| 41 | |
| 42 | public class MockYangSchemaNodeProvider { |
| 43 | |
| 44 | private static final String FS = File.separator; |
| 45 | private static final String PATH = System.getProperty("user.dir") + |
| 46 | FS + "buck-out" + FS + "gen" + |
| 47 | FS + "models" + FS + "microsemi" + FS + "onos-models-microsemi-schema" + FS; |
| 48 | private static final String SER_FILE_PATH = "yang" + FS + "resources" + |
| 49 | FS + "YangMetaData.ser"; |
| 50 | private static final String META_PATH = |
| 51 | PATH.replace("drivers/microsemi", "") |
| 52 | + SER_FILE_PATH; |
| 53 | private static final String TEMP_FOLDER_PATH = PATH + UtilConstants.TEMP; |
| 54 | private YangModelRegistry reg = new DefaultYangModelRegistry(); |
| 55 | private List<YangNode> nodes = new ArrayList<>(); |
| 56 | |
| 57 | /** |
| 58 | * Creates an instance of mock bundle context. |
| 59 | */ |
| 60 | public MockYangSchemaNodeProvider() { |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Process YANG schema node for a application. |
| 65 | */ |
| 66 | public void processSchemaRegistry() { |
| 67 | try { |
| 68 | //Need to deserialize generated meta data file for unit tests. |
| 69 | Set<YangNode> appNode = DataModelUtils.deSerializeDataModel(META_PATH); |
| 70 | RuntimeHelper.addLinkerAndJavaInfo(appNode); |
| 71 | nodes.addAll(appNode); |
| 72 | reg.registerModel(prepareParam(nodes)); |
| 73 | YangIoUtils.deleteDirectory(TEMP_FOLDER_PATH); |
| 74 | } catch (IOException e) { |
| 75 | throw new IllegalArgumentException("YangMetaData.ser could not " + |
| 76 | "be loaded from " + META_PATH, e); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Unregister given nodes from runtime service. |
| 82 | * |
| 83 | * @param nodes list of nodes |
| 84 | */ |
| 85 | public void unRegister(List<YangNode> nodes) { |
| 86 | reg.unregisterModel(prepareParam(nodes)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Prepares model registration parameter. |
| 91 | * |
| 92 | * @param nodes list of nodes |
| 93 | * @return model registration parameter |
| 94 | */ |
| 95 | private ModelRegistrationParam prepareParam(List<YangNode> nodes) { |
| 96 | //Process loading class file. |
| 97 | String appName; |
| 98 | ClassLoader classLoader = getClass().getClassLoader(); |
| 99 | |
| 100 | //Create model registration param. |
| 101 | ModelRegistrationParam.Builder b = |
| 102 | DefaultModelRegistrationParam.builder(); |
| 103 | |
| 104 | //create a new YANG model |
| 105 | YangModel model = YangApacheUtils.processYangModel(META_PATH, nodes); |
| 106 | //set YANG model |
| 107 | b.setYangModel(model); |
| 108 | |
| 109 | Iterator<YangNode> it = nodes.iterator(); |
| 110 | while (it.hasNext()) { |
| 111 | YangSchemaNode node = it.next(); |
| 112 | |
| 113 | //If service class is not generated then use |
| 114 | // interface file to load this class. |
| 115 | appName = RuntimeHelper.getInterfaceClassName(node); |
| 116 | Class<?> cls; |
| 117 | try { |
| 118 | cls = classLoader.loadClass(appName); |
| 119 | } catch (ClassNotFoundException e) { |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | //generate app info. |
| 124 | AppModuleInfo info = new DefaultAppModuleInfo(cls, null); |
| 125 | b.addAppModuleInfo(YangApacheUtils.processModuleId((YangNode) node), info); |
| 126 | } |
| 127 | return b.build(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns schema registry. |
| 132 | * |
| 133 | * @return schema registry |
| 134 | */ |
| 135 | public DefaultYangModelRegistry registry() { |
| 136 | return (DefaultYangModelRegistry) reg; |
| 137 | } |
| 138 | } |