Migrate sal-common-testutil odlparent-1.8.0-Carbon
[controller.git] / opendaylight / commons / protocol-framework / src / main / java / org / opendaylight / controller / config / yang / protocol / framework / TimedReconnectStrategyFactoryModule.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.protocol.framework;
9
10 import io.netty.util.concurrent.EventExecutor;
11
12 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
13 import org.opendaylight.protocol.framework.ReconnectStrategy;
14 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
15 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
16
17 import com.google.common.base.Preconditions;
18
19 /**
20 *
21 */
22 @Deprecated
23 public final class TimedReconnectStrategyFactoryModule extends org.opendaylight.controller.config.yang.protocol.framework.AbstractTimedReconnectStrategyFactoryModule
24  {
25
26     public TimedReconnectStrategyFactoryModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
27         super(identifier, dependencyResolver);
28     }
29
30     public TimedReconnectStrategyFactoryModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver,
31             final TimedReconnectStrategyFactoryModule oldModule, final java.lang.AutoCloseable oldInstance) {
32
33         super(identifier, dependencyResolver, oldModule, oldInstance);
34     }
35
36     @Override
37     protected void customValidation(){
38         JmxAttributeValidationException.checkNotNull(getSleepFactor(), "value is not set.", sleepFactorJmxAttribute);
39         JmxAttributeValidationException.checkCondition(getSleepFactor().doubleValue() >= 1, "value " + getSleepFactor()
40                 + " is less than 1", sleepFactorJmxAttribute);
41
42         JmxAttributeValidationException.checkNotNull(getConnectTime(), "value is not set.", connectTimeJmxAttribute);
43         JmxAttributeValidationException.checkCondition(getConnectTime() >= 0, "value " + getConnectTime()
44                 + " is less than 0", connectTimeJmxAttribute);
45
46         JmxAttributeValidationException.checkNotNull(getMinSleep(), "value is not set.", minSleepJmxAttribute);
47         JmxAttributeValidationException.checkCondition(getMaxSleep() == null || getMinSleep() <= getMaxSleep(),
48                 "value " + getMinSleep() + " is greter than MaxSleep " + getMaxSleep(), minSleepJmxAttribute);
49     }
50
51     @Override
52     public java.lang.AutoCloseable createInstance() {
53         return new TimedReconnectStrategyFactoryCloseable(getTimedReconnectExecutorDependency(),
54                 getConnectTime(), getMinSleep(), getSleepFactor().doubleValue(), getMaxSleep(), getMaxAttempts(),
55                 getDeadline());
56     }
57
58     private static final class TimedReconnectStrategyFactoryCloseable implements ReconnectStrategyFactory, AutoCloseable {
59
60         private final EventExecutor executor;
61         private final Long deadline, maxAttempts, maxSleep;
62         private final double sleepFactor;
63         private final int connectTime;
64         private final long minSleep;
65
66         public TimedReconnectStrategyFactoryCloseable(final EventExecutor executor, final int connectTime, final long minSleep, final double sleepFactor,
67                 final Long maxSleep, final Long maxAttempts, final Long deadline) {
68             Preconditions.checkArgument(maxSleep == null || minSleep <= maxSleep);
69             Preconditions.checkArgument(sleepFactor >= 1);
70             Preconditions.checkArgument(connectTime >= 0);
71             this.executor = Preconditions.checkNotNull(executor);
72             this.deadline = deadline;
73             this.maxAttempts = maxAttempts;
74             this.minSleep = minSleep;
75             this.maxSleep = maxSleep;
76             this.sleepFactor = sleepFactor;
77             this.connectTime = connectTime;
78         }
79
80         @Override
81         public void close() throws Exception {
82             // no-op
83         }
84
85         @Override
86         public ReconnectStrategy createReconnectStrategy() {
87             return new TimedReconnectStrategy(this.executor,
88                     this.connectTime, this.minSleep, this.sleepFactor, this.maxSleep, this.maxAttempts,
89                     this.deadline);
90         }
91
92     }
93 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.