479fa44b6b1597d11abf92c56085f1597c2856a6
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.element.service;
22
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.ScheduledThreadPoolExecutor;
25 import java.util.concurrent.TimeUnit;
26 import javax.ws.rs.core.Response;
27 import org.onap.policy.clamp.acm.element.handler.MessagePublisher;
28 import org.onap.policy.clamp.acm.element.main.parameters.AcElement;
29 import org.onap.policy.clamp.models.acm.messages.dmaap.element.ElementStatus;
30 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
31 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementType;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
34 import org.springframework.stereotype.Service;
35
36 /**
37  * Starter Service.
38  */
39 @Service
40 public class StarterService extends AbstractElementService implements AutoCloseable {
41
42     private ScheduledThreadPoolExecutor timerPool;
43     private ScheduledFuture<?> future;
44     private ToscaConceptIdentifier receiver;
45     private ToscaConceptIdentifier elementId;
46
47     private final MessagePublisher messagePublisher;
48
49     public StarterService(MessagePublisher messagePublisher, AcElement acElement) {
50         this.messagePublisher = messagePublisher;
51         this.elementId = acElement.getElementId();
52     }
53
54     @Override
55     public ElementType getType() {
56         return ElementType.STARTER;
57     }
58
59     /**
60      * Deactivate Scheduled ThreadPool Executor.
61      */
62     @Override
63     public void deactivate() {
64         if (timerPool != null) {
65             if (future != null) {
66                 future.cancel(true);
67             }
68             timerPool.shutdown();
69             timerPool = null;
70         }
71     }
72
73     @Override
74     public void active(ElementConfig elementConfig) {
75         if (timerPool != null) {
76             throw new PfModelRuntimeException(Response.Status.CONFLICT, "StarterService alredy actived!");
77         }
78         receiver = elementConfig.getReceiverId();
79
80         timerPool = new ScheduledThreadPoolExecutor(1);
81         timerPool.setRemoveOnCancelPolicy(true);
82         future = timerPool.scheduleAtFixedRate(this::sendMessage, elementConfig.getTimerMs(),
83                 elementConfig.getTimerMs(), TimeUnit.MILLISECONDS);
84     }
85
86     private void sendMessage() {
87         var messasge = new ElementStatus();
88         messasge.setElementId(receiver);
89         // Add Tracking
90         messasge.setMessage("starter: " + elementId);
91         messagePublisher.publishMsg(messasge);
92     }
93
94     @Override
95     public void update(ElementConfig elementConfig) {
96         if (timerPool == null) {
97             throw new PfModelRuntimeException(Response.Status.CONFLICT, "StarterService not actived!");
98         }
99         if (future != null) {
100             future.cancel(true);
101         }
102         future = timerPool.scheduleAtFixedRate(this::sendMessage, elementConfig.getTimerMs(),
103                 elementConfig.getTimerMs(), TimeUnit.MILLISECONDS);
104     }
105
106     @Override
107     public void close() throws Exception {
108         deactivate();
109     }
110 }