Add RFC8294 models
[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.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Builder for {@link MplsLabel} instances.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 @NonNullByDefault
29 public final class MplsLabelBuilder {
30     private static final Logger LOG = LoggerFactory.getLogger(MplsLabelBuilder.class);
31
32     @SuppressWarnings("null")
33     private static final LoadingCache<Entry<ClassLoader, String>, Optional<MplsLabel>> CLASS_CACHE =
34     CacheBuilder.newBuilder()
35             .weakKeys().build(new CacheLoader<Entry<ClassLoader, String>, Optional<MplsLabel>>() {
36                 @Override
37                 public Optional<MplsLabel> load(final Entry<ClassLoader, String> key) {
38                     return loadClass(key.getKey(), key.getValue());
39                 }
40             });
41
42     private MplsLabelBuilder() {
43
44     }
45
46     public static MplsLabel getDefaultInstance(final String defaultValue) {
47         if (defaultValue.startsWith("interface ")) {
48             final Optional<MplsLabel> optStatic = CLASS_CACHE.getUnchecked(
49                 new SimpleImmutableEntry<>(MplsLabelBuilder.class.getClassLoader(), defaultValue));
50             if (optStatic.isPresent()) {
51                 return optStatic.get();
52             }
53
54             final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
55             if (tccl != null) {
56                 final Optional<MplsLabel> optThreadLocal = CLASS_CACHE.getUnchecked(
57                     new SimpleImmutableEntry<>(tccl, defaultValue));
58                 if (optThreadLocal.isPresent()) {
59                     return optThreadLocal.get();
60                 }
61             }
62         }
63
64         return new MplsLabel(new MplsLabelGeneralUse(Long.valueOf(defaultValue)));
65     }
66
67     static Optional<MplsLabel> loadClass(final ClassLoader loader, final String key) {
68         final Class<?> cls;
69         try {
70             cls = ClassLoaderUtils.loadClass(loader, key);
71         } catch (ClassNotFoundException e) {
72             LOG.debug("%s not found in classloader of %s", key, loader);
73             return Optional.empty();
74         }
75
76         final Class<? extends MplsLabelSpecialPurposeValue> cast;
77         try {
78             cast = cls.asSubclass(MplsLabelSpecialPurposeValue.class);
79         } catch (ClassCastException e) {
80             LOG.warn("%s does not implement %s", MplsLabelSpecialPurposeValue.class);
81             return Optional.empty();
82         }
83
84         return Optional.of(new MplsLabel(cast));
85     }
86 }