2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.yangtools;
24 import java.io.IOException;
25 import javax.annotation.Nullable;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
28 import org.opendaylight.yangtools.concepts.Builder;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.FrameworkUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import com.fasterxml.jackson.annotation.JsonInclude.Include;
37 import com.fasterxml.jackson.core.JsonGenerator;
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.DeserializationFeature;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import com.fasterxml.jackson.databind.PropertyNamingStrategy;
42 import com.fasterxml.jackson.databind.SerializerProvider;
43 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
44 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder.Value;
45 import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
46 import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
47 import com.fasterxml.jackson.databind.module.SimpleModule;
48 import com.fasterxml.jackson.databind.ser.std.StdSerializer;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
53 * YangToolsMapper is a specific Jackson mapper configuration for opendaylight yangtools serialization or
54 * deserialization of DataObject to/from JSON TODO ChoiceIn and Credentials deserialization only for
55 * LoginPasswordBuilder
57 public class YangToolsMapper extends ObjectMapper {
59 private final Logger LOG = LoggerFactory.getLogger(YangToolsMapper.class);
60 private static final long serialVersionUID = 1L;
61 private static BundleContext context;
63 public YangToolsMapper() {
65 configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
66 setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);
67 setSerializationInclusion(Include.NON_NULL);
68 setAnnotationIntrospector(new YangToolsBuilderAnnotationIntrospector());
69 SimpleModule dateAndTimeSerializerModule = new SimpleModule();
70 dateAndTimeSerializerModule.addSerializer(DateAndTime.class, new CustomDateAndTimeSerializer());
71 registerModule(dateAndTimeSerializerModule);
72 Bundle bundle = FrameworkUtil.getBundle(YangToolsMapper.class);
73 context = bundle != null ? bundle.getBundleContext() : null;
77 public String writeValueAsString(Object value) throws JsonProcessingException {
78 // TODO Auto-generated method stub
79 return super.writeValueAsString(value);
83 * Get Builder object for yang tools interface.
85 * @param <T> yang-tools base datatype
86 * @param clazz class with interface.
87 * @return builder for interface or null if not existing
89 @SuppressWarnings("unchecked")
90 public @Nullable <T extends DataObject> Builder<T> getBuilder(Class<T> clazz) {
91 String builder = clazz.getName() + "Builder";
93 Class<?> clazzBuilder = findClass(builder);
94 return (Builder<T>) clazzBuilder.newInstance();
95 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
96 LOG.debug("Problem ", e);
102 * Callback for handling mapping failures.
106 public int getMappingFailures() {
111 * Provide mapping of string to attribute names, generated by yang-tools. "netconf-id" converted to "_netconfId"
113 * @param name with attribute name, not null or empty
114 * @return converted string or null if name was empty or null
116 public @Nullable static String toCamelCaseAttributeName(final String name) {
117 if (name == null || name.isEmpty())
120 final StringBuilder ret = new StringBuilder(name.length());
121 if (!name.startsWith("_"))
124 for (final String word : name.split("-")) {
125 if (!word.isEmpty()) {
127 ret.append(Character.toLowerCase(word.charAt(0)));
129 ret.append(Character.toUpperCase(word.charAt(0)));
131 ret.append(word.substring(1));
134 return ret.toString();
138 * Adapted Builder callbacks
140 private static class YangToolsBuilderAnnotationIntrospector extends JacksonAnnotationIntrospector {
141 private static final long serialVersionUID = 1L;
144 public Class<?> findPOJOBuilder(AnnotatedClass ac) {
146 String builder = null;
147 if (ac.getRawType().equals(Credentials.class)) {
149 "org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder";
150 //System.out.println(DataContainer.class.isAssignableFrom(ac.getRawType()));
151 //System.out.println(ChoiceIn.class.isAssignableFrom(ac.getRawType()));
153 } else if (ac.getRawType().equals(DateAndTime.class)) {
154 builder = DateAndTimeBuilder.class.getName();
158 if (ac.getRawType().isInterface()) {
159 builder = ac.getName() + "Builder";
162 if (builder != null) {
163 //System.out.println("XX1: "+ac.getRawType());
164 //System.out.println("XX2: "+builder);
165 //Class<?> innerBuilder = Class.forName(builder);
166 Class<?> innerBuilder = findClass(builder);
167 //System.out.println("Builder found: "+ innerBuilder);
170 } catch (ClassNotFoundException e) {
171 // No problem .. try next
173 return super.findPOJOBuilder(ac);
177 public Value findPOJOBuilderConfig(AnnotatedClass ac) {
178 if (ac.hasAnnotation(JsonPOJOBuilder.class)) {
179 return super.findPOJOBuilderConfig(ac);
181 return new JsonPOJOBuilder.Value("build", "set");
185 private static Class<?> findClass(String name) throws ClassNotFoundException {
186 // Try to find in other bundles
187 if (context != null) {
189 for (Bundle b : context.getBundles()) {
191 return b.loadClass(name);
192 } catch (ClassNotFoundException e) {
193 // No problem, this bundle doesn't have the class
196 throw new ClassNotFoundException("Can not find Class in OSGi context.");
198 return Class.forName(name);
200 // not found in any bundle
203 public static class DateAndTimeBuilder {
205 private final String _value;
207 public DateAndTimeBuilder(String v) {
211 public DateAndTime build() {
212 return new DateAndTime(_value);
216 public static class CustomDateAndTimeSerializer extends StdSerializer<DateAndTime> {
221 private static final long serialVersionUID = 1L;
223 public CustomDateAndTimeSerializer() {
227 protected CustomDateAndTimeSerializer(Class<DateAndTime> t) {
232 public void serialize(DateAndTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
233 gen.writeString(value.getValue());