Bharat saraswal | 59e7ac9 | 2017-01-19 19:51:50 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yang; |
| 18 | |
| 19 | import org.onosproject.yang.compiler.api.YangCompilerException; |
| 20 | |
| 21 | import java.io.FileInputStream; |
| 22 | import java.io.FileNotFoundException; |
| 23 | import java.io.InputStream; |
| 24 | import java.nio.file.Path; |
| 25 | import java.util.Objects; |
| 26 | |
| 27 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Represents default YANG module. |
| 32 | */ |
| 33 | public class DefaultYangModule implements YangModule { |
| 34 | |
| 35 | private YangModuleId id; |
| 36 | private Path yangSrc; |
| 37 | private Path metadata; |
| 38 | |
| 39 | /** |
| 40 | * Creates an instance of default YANG module. |
| 41 | * |
| 42 | * @param id YANG module id |
| 43 | * @param yangSrc YANG source file path |
| 44 | * @param metadata YANG metadata source file path |
| 45 | */ |
| 46 | public DefaultYangModule(YangModuleId id, Path yangSrc, Path metadata) { |
| 47 | checkNotNull(yangSrc); |
| 48 | checkNotNull(metadata); |
| 49 | checkNotNull(id); |
| 50 | this.id = id; |
| 51 | this.yangSrc = yangSrc; |
| 52 | this.metadata = metadata; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public YangModuleId getYangModuleId() { |
| 57 | return id; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public InputStream getYangSource() { |
| 62 | try { |
| 63 | return new FileInputStream(yangSrc.toString()); |
| 64 | } catch (FileNotFoundException e) { |
| 65 | throw new YangCompilerException("Yang source file not found." + |
| 66 | yangSrc); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public InputStream getMetadata() { |
| 72 | try { |
| 73 | return new FileInputStream(metadata.toString()); |
| 74 | } catch (FileNotFoundException e) { |
| 75 | throw new YangCompilerException("metadata source file not found." + |
| 76 | metadata); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int hashCode() { |
| 82 | return Objects.hash(id, yangSrc, metadata); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean equals(Object obj) { |
| 87 | if (obj == null) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | DefaultYangModule that = (DefaultYangModule) obj; |
| 92 | return Objects.equals(id, that.id) && |
| 93 | Objects.equals(yangSrc, that.yangSrc) && |
| 94 | Objects.equals(metadata, that.metadata); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public String toString() { |
| 99 | return toStringHelper(getClass()) |
| 100 | .add("moduleId", id) |
| 101 | .add("yangSource", yangSrc) |
| 102 | .add("yangMetadata", metadata) |
| 103 | .toString(); |
| 104 | } |
| 105 | } |