blob: 785ba8dc361923a484c2b77c0bb2eae28c996825 [file] [log] [blame]
Sean Condond911af92017-07-19 18:05:27 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Sean Condond911af92017-07-19 18:05:27 +01003 *
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 */
16package org.onosproject.yang;
17
Sean Condoncf2751f2017-11-28 17:29:30 +000018import com.google.common.collect.ImmutableMap;
Sean Condon1dbcd712017-10-19 12:09:21 +010019import org.onosproject.models.microsemi.MicrosemiModelRegistrator;
Sean Condond911af92017-07-19 18:05:27 +010020import org.onosproject.yang.compiler.datamodel.YangNode;
Ray Milkey8e2c3392017-10-16 14:57:02 -070021import org.onosproject.yang.compiler.tool.YangNodeInfo;
Sean Condoncf2751f2017-11-28 17:29:30 +000022import org.onosproject.yang.model.YangModuleId;
23import org.onosproject.yang.runtime.AppModuleInfo;
Sean Condond911af92017-07-19 18:05:27 +010024import org.onosproject.yang.runtime.DefaultModelRegistrationParam;
25import org.onosproject.yang.runtime.ModelRegistrationParam;
26import org.onosproject.yang.runtime.YangModelRegistry;
Sean Condond911af92017-07-19 18:05:27 +010027import org.onosproject.yang.runtime.impl.DefaultYangModelRegistry;
28
29import java.io.File;
30import java.io.IOException;
31import java.util.ArrayList;
Sean Condoncf2751f2017-11-28 17:29:30 +000032import java.util.HashMap;
Sean Condond911af92017-07-19 18:05:27 +010033import java.util.List;
Sean Condoncf2751f2017-11-28 17:29:30 +000034import java.util.Map;
Sean Condond911af92017-07-19 18:05:27 +010035
Yuta HIGUCHI2e4a6d42017-08-25 10:51:40 -070036import static org.junit.Assert.fail;
Thomas Vachuska89534452017-07-28 10:20:12 -070037import static org.onosproject.yang.compiler.tool.YangCompilerManager.deSerializeDataModel;
38import static org.onosproject.yang.compiler.tool.YangCompilerManager.getYangNodes;
39import static org.onosproject.yang.compiler.tool.YangCompilerManager.processYangModel;
40
Sean Condond911af92017-07-19 18:05:27 +010041public class MockMicrosemiRegistrator extends MicrosemiModelRegistrator {
42 private static final String FS = File.separator;
43 private static final String PATH = System.getProperty("user.dir") +
44 FS + "buck-out" + FS + "gen" +
45 FS + "models" + FS + "microsemi" + FS + "onos-models-microsemi-schema" + FS;
46 private static final String SER_FILE_PATH = "yang" + FS + "resources" +
47 FS + "YangMetaData.ser";
48 private static final String META_PATH =
49 PATH.replace("drivers/microsemi", "")
50 + SER_FILE_PATH;
51
52 @Override
53 public void activate() {
54 modelRegistry = new DefaultYangModelRegistry();
Ray Milkey8e2c3392017-10-16 14:57:02 -070055 List<YangNodeInfo> nodes = new ArrayList<>();
Sean Condond911af92017-07-19 18:05:27 +010056 try {
Ray Milkey8e2c3392017-10-16 14:57:02 -070057 for (YangNode node : getYangNodes(deSerializeDataModel(META_PATH))) {
58 nodes.add(new YangNodeInfo(node, false));
59 }
Sean Condond911af92017-07-19 18:05:27 +010060
Thomas Vachuska89534452017-07-28 10:20:12 -070061 model = processYangModel(META_PATH, nodes, "test", false);
Sean Condond911af92017-07-19 18:05:27 +010062 ModelRegistrationParam.Builder b =
63 DefaultModelRegistrationParam.builder().setYangModel(model);
64 b.setYangModel(model);
65
66 ModelRegistrationParam registrationParam = getAppInfo(b).setYangModel(model).build();
67 modelRegistry.registerModel(registrationParam);
68 } catch (IOException ioe) {
69 ioe.printStackTrace();
Yuta HIGUCHI2e4a6d42017-08-25 10:51:40 -070070 fail();
Sean Condond911af92017-07-19 18:05:27 +010071 }
72 }
73
Sean Condoncf2751f2017-11-28 17:29:30 +000074
75 public void addAppInfo(Map<YangModuleId, AppModuleInfo> map) {
76 Map<YangModuleId, AppModuleInfo> appInfoCopy = new HashMap<>();
77 appInfoCopy.putAll(appInfo);
78 appInfoCopy.putAll(map);
79 appInfo = ImmutableMap.copyOf(appInfoCopy);
80 }
81
Sean Condond911af92017-07-19 18:05:27 +010082 public YangModelRegistry registry() {
83 return modelRegistry;
84 }
85}