blob: 18358bcb73b9d9838a146db4cc558c98f507b3ea [file] [log] [blame]
Bharat saraswal59e7ac92017-01-19 19:51:50 +05301/*
2 * Copyright 2017-present Open Networking Laboratory
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
17package org.onosproject.yang;
18
19import java.util.LinkedHashMap;
20import java.util.LinkedHashSet;
21import java.util.Map;
22import java.util.Objects;
23import java.util.Set;
24
25import static com.google.common.base.MoreObjects.toStringHelper;
26
27/**
28 * Represents default YANG model implementation.
29 */
30public class DefaultYangModel implements YangModel {
31
32 private final Map<YangModuleId, YangModule> moduleMap;
33
34 /**
35 * Creates an instance of YANG model.
36 */
37 public DefaultYangModel() {
38 moduleMap = new LinkedHashMap<>();
39 }
40
41 @Override
42 public Set<YangModule> getYangModules() {
43 Set<YangModule> modules = new LinkedHashSet<>();
44 for (Map.Entry<YangModuleId, YangModule> entry : moduleMap.entrySet()) {
45 modules.add(entry.getValue());
46 }
47 return modules;
48 }
49
50 @Override
51 public Set<YangModuleId> getYangModulesId() {
52 Set<YangModuleId> ids = new LinkedHashSet<>();
53 for (Map.Entry<YangModuleId, YangModule> entry : moduleMap.entrySet()) {
54 ids.add(entry.getKey());
55 }
56 return ids;
57 }
58
59 @Override
60 public YangModule getYangModule(YangModuleId id) {
61 return moduleMap.get(id);
62 }
63
64 @Override
65 public void addModule(YangModuleId id, YangModule module) {
66 moduleMap.put(id, module);
67 }
68
69 @Override
70 public int hashCode() {
71 return Objects.hash(moduleMap);
72 }
73
74 @Override
75 public boolean equals(Object obj) {
76 if (obj == null) {
77 return false;
78 }
79 DefaultYangModel that = (DefaultYangModel) obj;
80
81 if (moduleMap.size() == that.moduleMap.size()) {
82 for (Map.Entry<YangModuleId, YangModule> entry : moduleMap.entrySet()) {
83 if (!that.moduleMap.containsKey(entry.getKey()) ||
84 !that.moduleMap.containsValue(entry.getValue())) {
85 return false;
86 }
87 }
88 } else {
89 return false;
90 }
91 return true;
92 }
93
94 @Override
95 public String toString() {
96 return toStringHelper(getClass())
97 .add("model", moduleMap)
98 .toString();
99 }
100}