Merge branch 'mdsal-trace' from controller
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / ResourceYangModuleInfo.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.binding;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * Base utility class for providing YANG module info backed by class resources.
21  *
22  * @author Robert Varga
23  */
24 @Beta
25 @NonNullByDefault
26 public abstract class ResourceYangModuleInfo implements YangModuleInfo {
27     @Override
28     public final InputStream openYangTextStream() throws IOException {
29         final InputStream ret = ResourceYangModuleInfo.this.getClass()
30                 .getResourceAsStream(verifyNotNull(resourceName()));
31         if (ret == null) {
32             throw new IOException("Failed to open resource " + resourceName());
33         }
34         return ret;
35     }
36
37     @Override
38     public final String toString() {
39         return addToStringHelperAttributes(MoreObjects.toStringHelper(this)).toString();
40     }
41
42     protected ToStringHelper addToStringHelperAttributes(final ToStringHelper helper) {
43         return helper.add("resource", verifyNotNull(resourceName()));
44     }
45
46     protected abstract String resourceName();
47 }