Added tests for yang.model.util
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / helpers / AbstractCodecImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.data.codec.gson.helpers;
9
10 import com.google.common.base.Preconditions;
11
12 import java.net.URI;
13
14 import org.opendaylight.yangtools.yang.model.api.Module;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 abstract class AbstractCodecImpl {
19     private static final Logger LOG = LoggerFactory.getLogger(AbstractCodecImpl.class);
20     private final SchemaContextUtils schema;
21
22     protected AbstractCodecImpl(final SchemaContextUtils schema) {
23         this.schema = Preconditions.checkNotNull(schema);
24     }
25
26     protected final SchemaContextUtils getSchema() {
27         return schema;
28     }
29
30     protected final Module getModuleByNamespace(final String namespace) {
31         URI validNamespace = resolveValidNamespace(namespace);
32
33         Module module = schema.findModuleByNamespace(validNamespace);
34         if (module == null) {
35             LOG.info("Module for namespace " + validNamespace + " wasn't found.");
36             return null;
37         }
38         return module;
39     }
40
41     protected final URI resolveValidNamespace(final String namespace) {
42         URI validNamespace = schema.findNamespaceByModuleName(namespace);
43         if (validNamespace == null) {
44             validNamespace = URI.create(namespace);
45         }
46
47         return validNamespace;
48     }
49 }