Added tests for yang.model.util
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / helpers / IdentityrefCodecImpl.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 org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec;
12 import org.opendaylight.yangtools.yang.data.codec.gson.helpers.IdentityValuesDTO.IdentityValue;
13 import org.opendaylight.yangtools.yang.model.api.Module;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 class IdentityrefCodecImpl extends AbstractCodecImpl implements IdentityrefCodec<IdentityValuesDTO> {
18     private static final Logger LOG = LoggerFactory.getLogger(IdentityrefCodecImpl.class);
19
20     IdentityrefCodecImpl(final SchemaContextUtils schema) {
21         super(schema);
22     }
23
24     @Override
25     public IdentityValuesDTO serialize(final QName data) {
26         return new IdentityValuesDTO(data.getNamespace().toString(), data.getLocalName(), data.getPrefix(), null);
27     }
28
29     @Override
30     public QName deserialize(final IdentityValuesDTO data) {
31         IdentityValue valueWithNamespace = data.getValuesWithNamespaces().get(0);
32         Module module = getModuleByNamespace(valueWithNamespace.getNamespace());
33         if (module == null) {
34             LOG.info("Module was not found for namespace {}", valueWithNamespace.getNamespace());
35             LOG.info("Idenetityref will be translated as NULL for data - {}", String.valueOf(valueWithNamespace));
36             return null;
37         }
38
39         return QName.create(module.getNamespace(), module.getRevision(), valueWithNamespace.getValue());
40     }
41
42 }