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