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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.element.service;
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;
40 public class StarterService extends AbstractElementService implements AutoCloseable {
42 private ScheduledThreadPoolExecutor timerPool;
43 private ScheduledFuture<?> future;
44 private ToscaConceptIdentifier receiver;
45 private ToscaConceptIdentifier elementId;
47 private final MessagePublisher messagePublisher;
49 public StarterService(MessagePublisher messagePublisher, AcElement acElement) {
50 this.messagePublisher = messagePublisher;
51 this.elementId = acElement.getElementId();
55 public ElementType getType() {
56 return ElementType.STARTER;
60 * Deactivate Scheduled ThreadPool Executor.
63 public void deactivate() {
64 if (timerPool != null) {
74 public void active(ElementConfig elementConfig) {
75 if (timerPool != null) {
76 throw new PfModelRuntimeException(Response.Status.CONFLICT, "StarterService alredy actived!");
78 receiver = elementConfig.getElementId();
80 timerPool = new ScheduledThreadPoolExecutor(1);
81 timerPool.setRemoveOnCancelPolicy(true);
82 future = timerPool.scheduleAtFixedRate(this::sendMessage, elementConfig.getTimerSec(),
83 elementConfig.getTimerSec(), TimeUnit.MILLISECONDS);
86 private void sendMessage() {
87 var messasge = new ElementStatus();
88 messasge.setElementId(receiver);
90 messasge.setMessage("starter: " + elementId);
91 messagePublisher.publishMsg(messasge);
95 public void update(ElementConfig elementConfig) {
96 if (timerPool == null) {
97 throw new PfModelRuntimeException(Response.Status.CONFLICT, "StarterService not actived!");
102 future = timerPool.scheduleAtFixedRate(this::sendMessage, elementConfig.getTimerSec(),
103 elementConfig.getTimerSec(), TimeUnit.MILLISECONDS);
107 public void close() throws Exception {