Set all cross references of policy/models
[policy/models.git] / models-interactions / model-actors / actorServiceProvider / src / main / java / org / onap / policy / controlloop / actorserviceprovider / impl / StartConfigPartial.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.impl;
22
23 import lombok.Getter;
24 import org.onap.policy.common.capabilities.Configurable;
25 import org.onap.policy.common.capabilities.Startable;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Partial implementation of an object that is both startable and configurable. It
31  * provides the high level methods defined in the interface, while deferring the details
32  * to abstract methods that must be provided by the subclasses. It also manages the
33  * current {@link #state}.
34  *
35  * @param <T> type of parameters expected by {@link #configure(Object)}
36  */
37 public abstract class StartConfigPartial<T> implements Startable, Configurable<T> {
38     private static final Logger logger = LoggerFactory.getLogger(StartConfigPartial.class);
39
40     @Getter
41     private final String fullName;
42
43     public enum State {
44         IDLE, CONFIGURED, ALIVE
45     }
46
47     private State state = State.IDLE;
48
49     /**
50      * Constructs the object.
51      *
52      * @param fullName full name of this object, used for logging and exception purposes
53      */
54     protected StartConfigPartial(String fullName) {
55         this.fullName = fullName;
56     }
57
58     @Override
59     public synchronized boolean isAlive() {
60         return (state == State.ALIVE);
61     }
62
63     /**
64      * Determines if this object has been configured.
65      *
66      * @return {@code true} if this object has been configured, {@code false} otherwise
67      */
68     public synchronized boolean isConfigured() {
69         return (state != State.IDLE);
70     }
71
72     @Override
73     public synchronized void configure(T parameters) {
74         if (isAlive()) {
75             throw new IllegalStateException("attempt to reconfigure, but already running " + getFullName());
76         }
77
78         logger.info("initializing {}", getFullName());
79
80         doConfigure(parameters);
81
82         state = State.CONFIGURED;
83     }
84
85     @Override
86     public synchronized boolean start() {
87         switch (state) {
88             case ALIVE:
89                 logger.info("{} is already running", getFullName());
90                 break;
91
92             case CONFIGURED:
93                 logger.info("starting {}", getFullName());
94                 doStart();
95                 state = State.ALIVE;
96                 break;
97
98             case IDLE:
99             default:
100                 throw new IllegalStateException("attempt to start unconfigured " + getFullName());
101         }
102
103         return true;
104     }
105
106     @Override
107     public synchronized boolean stop() {
108         if (isAlive()) {
109             logger.info("stopping {}", getFullName());
110             state = State.CONFIGURED;
111             doStop();
112
113         } else {
114             logger.info("{} is not running", getFullName());
115         }
116
117         return true;
118     }
119
120     @Override
121     public synchronized void shutdown() {
122         if (!isAlive()) {
123             logger.info("{} is not running", getFullName());
124             return;
125         }
126
127         logger.info("shutting down actor {}", getFullName());
128         state = State.CONFIGURED;
129         doShutdown();
130     }
131
132     /**
133      * Configures this object.
134      *
135      * @param parameters configuration parameters
136      */
137     protected abstract void doConfigure(T parameters);
138
139     /**
140      * Starts this object.
141      */
142     protected abstract void doStart();
143
144     /**
145      * Stops this object.
146      */
147     protected abstract void doStop();
148
149     /**
150      * Shuts down this object.
151      */
152     protected abstract void doShutdown();
153 }