blob: f78d903672ea325b0fdfd3b317a927a65163c3bf [file] [log] [blame]
sonu guptaeff184b2016-11-24 12:43:49 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sonu guptaeff184b2016-11-24 12:43:49 +05303 *
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
17package org.onosproject.yms.app.ysr;
18
19import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network1.rev20151208.IetfNetwork1Service;
20import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.datamodel.YangSchemaNode;
22
23import java.io.File;
24import java.io.IOException;
25import java.util.ArrayList;
26import java.util.List;
27import java.util.Set;
28
29import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.deSerializeDataModel;
30import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
31import 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 */
37public 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}