Making YangModel immutable.
Change-Id: I5b00b1e298e2907f8461cac253122c214281b5fa
diff --git a/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/impl/YangCompilerManager.java b/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/impl/YangCompilerManager.java
index 357abfe..a0f7b98 100644
--- a/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/impl/YangCompilerManager.java
+++ b/compiler/base/tool/src/main/java/org/onosproject/yang/compiler/tool/impl/YangCompilerManager.java
@@ -16,12 +16,6 @@
package org.onosproject.yang.compiler.tool.impl;
-import org.onosproject.yang.model.DefaultYangModel;
-import org.onosproject.yang.model.DefaultYangModule;
-import org.onosproject.yang.model.DefaultYangModuleId;
-import org.onosproject.yang.model.YangModel;
-import org.onosproject.yang.model.YangModule;
-import org.onosproject.yang.model.YangModuleId;
import org.onosproject.yang.compiler.api.YangCompilationParam;
import org.onosproject.yang.compiler.api.YangCompiledOutput;
import org.onosproject.yang.compiler.api.YangCompilerException;
@@ -38,6 +32,12 @@
import org.onosproject.yang.compiler.parser.impl.YangUtilsParserManager;
import org.onosproject.yang.compiler.tool.YangFileInfo;
import org.onosproject.yang.compiler.utils.io.YangPluginConfig;
+import org.onosproject.yang.model.DefaultYangModel;
+import org.onosproject.yang.model.DefaultYangModule;
+import org.onosproject.yang.model.DefaultYangModuleId;
+import org.onosproject.yang.model.YangModel;
+import org.onosproject.yang.model.YangModule;
+import org.onosproject.yang.model.YangModuleId;
import org.slf4j.Logger;
import java.io.File;
@@ -112,15 +112,15 @@
* @return YANG model
*/
private YangModel processYangModel(String path) {
- DefaultYangModel model = new DefaultYangModel();
+ YangModel.Builder b = DefaultYangModel.builder();
YangModuleId id;
for (YangNode node : yangNodeSet) {
id = processModuleId(node);
YangModule module =
new DefaultYangModule(id, get(node.getFileName()), get(path));
- model.addModule(id, module);
+ b.addModule(id, module);
}
- return model;
+ return b.build();
}
/**
@@ -160,22 +160,6 @@
}
/**
- * Provides a list of files from list of strings.
- *
- * @param yangFileInfo set of yang file information
- * @return list of files
- */
- private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) {
- List<File> files = new ArrayList<>();
- for (YangFileInfo yangFile : yangFileInfo) {
- if (yangFile.isForTranslator()) {
- files.add(new File(yangFile.getYangFileName()));
- }
- }
- return files;
- }
-
- /**
* Creates a YANG file info set.
*
* @param yangFileList YANG files list
@@ -481,4 +465,20 @@
public void setYangFileInfoSet(Set<YangFileInfo> yangFileInfoSet) {
this.yangFileInfoSet = yangFileInfoSet;
}
+
+ /**
+ * Provides a list of files from list of strings.
+ *
+ * @param yangFileInfo set of yang file information
+ * @return list of files
+ */
+ private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) {
+ List<File> files = new ArrayList<>();
+ for (YangFileInfo yangFile : yangFileInfo) {
+ if (yangFile.isForTranslator()) {
+ files.add(new File(yangFile.getYangFileName()));
+ }
+ }
+ return files;
+ }
}
diff --git a/model/src/main/java/org/onosproject/yang/model/DefaultYangModel.java b/model/src/main/java/org/onosproject/yang/model/DefaultYangModel.java
index b1bb8f6..974a71d 100644
--- a/model/src/main/java/org/onosproject/yang/model/DefaultYangModel.java
+++ b/model/src/main/java/org/onosproject/yang/model/DefaultYangModel.java
@@ -33,7 +33,17 @@
/**
* Creates an instance of YANG model.
+ *
+ * @param b YANG model builder
*/
+ private DefaultYangModel(Builder b) {
+ moduleMap = b.moduleMap;
+ }
+
+ /**
+ * Creates an instance of YANG model.
+ */
+ @Deprecated
public DefaultYangModel() {
moduleMap = new LinkedHashMap<>();
}
@@ -62,6 +72,7 @@
}
@Override
+ @Deprecated
public void addModule(YangModuleId id, YangModule module) {
moduleMap.put(id, module);
}
@@ -97,4 +108,38 @@
.add("model", moduleMap)
.toString();
}
+
+ /**
+ * Retrieves a new YANG model builder.
+ *
+ * @return YANG model builder
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Represents implementation of YANG model builder.
+ */
+ public static class Builder implements YangModel.Builder {
+ private final Map<YangModuleId, YangModule> moduleMap;
+
+ /**
+ * Creates an instance of YANG model builder.
+ */
+ public Builder() {
+ moduleMap = new LinkedHashMap<>();
+ }
+
+ @Override
+ public Builder addModule(YangModuleId id, YangModule module) {
+ moduleMap.put(id, module);
+ return this;
+ }
+
+ @Override
+ public YangModel build() {
+ return new DefaultYangModel(this);
+ }
+ }
}
diff --git a/model/src/main/java/org/onosproject/yang/model/YangModel.java b/model/src/main/java/org/onosproject/yang/model/YangModel.java
index 4d50b16..e4cca01 100644
--- a/model/src/main/java/org/onosproject/yang/model/YangModel.java
+++ b/model/src/main/java/org/onosproject/yang/model/YangModel.java
@@ -55,6 +55,25 @@
@Deprecated
void addModule(YangModuleId id, YangModule module);
- // TODO: Make this class immutable post-ONS.
- // Either specify modules during construction or create a builder.
+ /**
+ * Abstraction of an entity that represents builder of YANG model.
+ */
+ interface Builder {
+
+ /**
+ * Adds YANG module information for a given module identifier.
+ *
+ * @param id module identifier
+ * @param module YANG module information
+ * @return builder
+ */
+ Builder addModule(YangModuleId id, YangModule module);
+
+ /**
+ * Builds an instance of YANG model.
+ *
+ * @return YANG model
+ */
+ YangModel build();
+ }
}
diff --git a/runtime/src/main/java/org/onosproject/yang/runtime/helperutils/YangApacheUtils.java b/runtime/src/main/java/org/onosproject/yang/runtime/helperutils/YangApacheUtils.java
index 521d266..3514ab9 100644
--- a/runtime/src/main/java/org/onosproject/yang/runtime/helperutils/YangApacheUtils.java
+++ b/runtime/src/main/java/org/onosproject/yang/runtime/helperutils/YangApacheUtils.java
@@ -95,7 +95,7 @@
*/
public static YangModel processYangModel(String path,
List<YangNode> curNodes) {
- DefaultYangModel model = new DefaultYangModel();
+ YangModel.Builder b = DefaultYangModel.builder();
YangModuleId id;
Iterator<YangNode> it = curNodes.iterator();
while (it.hasNext()) {
@@ -103,9 +103,9 @@
id = processModuleId((YangNode) node);
YangModule module =
new DefaultYangModule(id, get(node.getFileName()), get(path));
- model.addModule(id, module);
+ b.addModule(id, module);
}
- return model;
+ return b.build();
}
/**