2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.md.sal.binding.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Stopwatch;
12 import com.google.common.util.concurrent.Uninterruptibles;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
15 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodecFactory;
16 import org.osgi.framework.BundleContext;
21 public final class RuntimeMappingModule extends AbstractRuntimeMappingModule {
22 private static final long WAIT_IN_MINUTES = 5;
24 private BundleContext bundleContext;
26 public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
27 final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
28 super(identifier, dependencyResolver);
31 public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
32 final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
33 final RuntimeMappingModule oldModule, final java.lang.AutoCloseable oldInstance) {
34 super(identifier, dependencyResolver, oldModule, oldInstance);
38 public void validate() {
40 Preconditions.checkNotNull(bundleContext);
41 // Add custom validation for module attributes here.
45 public boolean canReuseInstance(final AbstractRuntimeMappingModule oldModule) {
50 public java.lang.AutoCloseable createInstance() {
51 // This is kind of ugly - you might cringe (you've been warned). The BindingToNormalizedNodeCodec
52 // instance is advertised via blueprint so ideally we'd obtain it from the OSGi service registry.
53 // The config yang service identity declares the concrete BindingToNormalizedNodeCodec class
54 // and not an interface as the java-class so we must return a BindingToNormalizedNodeCodec instance.
55 // However we can't cast the instance obtained from the service registry to
56 // BindingToNormalizedNodeCodec b/c Aries may register a proxy if there are interceptors defined.
57 // By default karaf ships with the org.apache.aries.quiesce.api bundle which automatically adds
58 // an interceptor that adds stat tracking for service method calls. While this can be disabled, we
59 // shouldn't rely on it.
61 // Therefore we store a static instance in the BindingToNormalizedNodeCodecFactory which is created
62 // by blueprint via newInstance. We obtain the static instance here and busy wait if not yet available.
64 Stopwatch sw = Stopwatch.createStarted();
65 while(sw.elapsed(TimeUnit.MINUTES) <= WAIT_IN_MINUTES) {
66 BindingToNormalizedNodeCodec instance = BindingToNormalizedNodeCodecFactory.getInstance();
67 if(instance != null) {
71 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
74 throw new IllegalStateException("Could not obtain the BindingToNormalizedNodeCodec instance after " +
75 WAIT_IN_MINUTES + " minutes.");
78 public void setBundleContext(final BundleContext bundleContext) {
79 this.bundleContext = bundleContext;