Modify config Module impls to co-exist with blueprint
[controller.git] / opendaylight / md-sal / sal-binding-config / src / main / java / org / opendaylight / controller / config / yang / md / sal / binding / impl / RuntimeMappingModule.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.controller.config.yang.md.sal.binding.impl;
9
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;
17
18 /**
19  *
20 **/
21 public final class RuntimeMappingModule extends AbstractRuntimeMappingModule {
22     private static final long WAIT_IN_MINUTES = 5;
23
24     private BundleContext bundleContext;
25
26     public RuntimeMappingModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier,
27             final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
28         super(identifier, dependencyResolver);
29     }
30
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);
35     }
36
37     @Override
38     public void validate() {
39         super.validate();
40         Preconditions.checkNotNull(bundleContext);
41         // Add custom validation for module attributes here.
42     }
43
44     @Override
45     public boolean canReuseInstance(final AbstractRuntimeMappingModule oldModule) {
46         return true;
47     }
48
49     @Override
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.
60         //
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.
63
64         Stopwatch sw = Stopwatch.createStarted();
65         while(sw.elapsed(TimeUnit.MINUTES) <= WAIT_IN_MINUTES) {
66             BindingToNormalizedNodeCodec instance = BindingToNormalizedNodeCodecFactory.getInstance();
67             if(instance != null) {
68                 return instance;
69             }
70
71             Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
72         }
73
74         throw new IllegalStateException("Could not obtain the BindingToNormalizedNodeCodec instance after " +
75                 WAIT_IN_MINUTES + " minutes.");
76     }
77
78     public void setBundleContext(final BundleContext bundleContext) {
79         this.bundleContext = bundleContext;
80     }
81 }