Bump versions to 10.0.0-SNAPSHOT
[mdsal.git] / model / ietf / rfc8294-ietf-routing-types / src / main / java / org / opendaylight / yang / gen / v1 / urn / ietf / params / xml / ns / yang / ietf / routing / types / rev171204 / MplsLabelBuilder.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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.types.rev171204;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import java.util.AbstractMap.SimpleImmutableEntry;
15 import java.util.Map.Entry;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.opendaylight.yangtools.util.ClassLoaderUtils;
19 import org.opendaylight.yangtools.yang.common.Uint32;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Builder for {@link MplsLabel} instances.
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 @NonNullByDefault
30 public final class MplsLabelBuilder {
31     private static final Logger LOG = LoggerFactory.getLogger(MplsLabelBuilder.class);
32
33     @SuppressWarnings("null")
34     private static final LoadingCache<Entry<ClassLoader, String>, Optional<MplsLabel>> CLASS_CACHE =
35     CacheBuilder.newBuilder()
36             .weakKeys().build(new CacheLoader<Entry<ClassLoader, String>, Optional<MplsLabel>>() {
37                 @Override
38                 public Optional<MplsLabel> load(final Entry<ClassLoader, String> key) {
39                     return loadClass(key.getKey(), key.getValue());
40                 }
41             });
42
43     private MplsLabelBuilder() {
44
45     }
46
47     public static MplsLabel getDefaultInstance(final String defaultValue) {
48         if (defaultValue.startsWith("interface ")) {
49             final Optional<MplsLabel> optStatic = CLASS_CACHE.getUnchecked(
50                 new SimpleImmutableEntry<>(MplsLabelBuilder.class.getClassLoader(), defaultValue));
51             if (optStatic.isPresent()) {
52                 return optStatic.get();
53             }
54
55             final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
56             if (tccl != null) {
57                 final Optional<MplsLabel> optThreadLocal = CLASS_CACHE.getUnchecked(
58                     new SimpleImmutableEntry<>(tccl, defaultValue));
59                 if (optThreadLocal.isPresent()) {
60                     return optThreadLocal.get();
61                 }
62             }
63         }
64
65         return new MplsLabel(new MplsLabelGeneralUse(Uint32.valueOf(defaultValue)));
66     }
67
68     static Optional<MplsLabel> loadClass(final ClassLoader loader, final String key) {
69         final Class<?> cls;
70         try {
71             cls = ClassLoaderUtils.loadClass(loader, key);
72         } catch (ClassNotFoundException e) {
73             LOG.debug("%s not found in classloader of %s", key, loader);
74             return Optional.empty();
75         }
76
77         final Class<? extends MplsLabelSpecialPurposeValue> cast;
78         try {
79             cast = cls.asSubclass(MplsLabelSpecialPurposeValue.class);
80         } catch (ClassCastException e) {
81             LOG.warn("%s does not implement %s", MplsLabelSpecialPurposeValue.class);
82             return Optional.empty();
83         }
84
85         return Optional.of(new MplsLabel(cast));
86     }
87 }