d975ec6bc24bb64123eb33ddae4938bfcd3beeb4
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.controlloop.runtime.supervision;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.concurrent.LinkedBlockingQueue;
26 import java.util.concurrent.ThreadPoolExecutor;
27 import java.util.concurrent.TimeUnit;
28 import lombok.RequiredArgsConstructor;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.aspectj.lang.annotation.After;
31 import org.aspectj.lang.annotation.AfterReturning;
32 import org.aspectj.lang.annotation.Aspect;
33 import org.aspectj.lang.annotation.Before;
34 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantRegister;
35 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantStatus;
36 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantUpdateAck;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.scheduling.annotation.Scheduled;
40 import org.springframework.stereotype.Component;
41
42 @Aspect
43 @Component
44 @RequiredArgsConstructor
45 public class SupervisionAspect implements Closeable {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionAspect.class);
48
49     private final SupervisionScanner supervisionScanner;
50
51     private ThreadPoolExecutor executor =
52             new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
53
54     @Scheduled(
55             fixedRateString = "${runtime.participantParameters.heartBeatMs}",
56             initialDelayString = "${runtime.participantParameters.heartBeatMs}")
57     public void schedule() {
58         LOGGER.info("Add scheduled scanning");
59         executor.execute(() -> supervisionScanner.run(true));
60     }
61
62     /**
63      * Intercept Messages from participant and run Supervision Scan.
64      */
65     @After("@annotation(MessageIntercept)")
66     public void doCheck() {
67         if (executor.getQueue().size() < 2) {
68             LOGGER.debug("Add scanning Message");
69             executor.execute(() -> supervisionScanner.run(false));
70         }
71     }
72
73     @Before("@annotation(MessageIntercept) && args(participantStatusMessage,..)")
74     public void handleParticipantStatus(ParticipantStatus participantStatusMessage) {
75         executor.execute(() -> supervisionScanner.handleParticipantStatus(participantStatusMessage.getParticipantId()));
76     }
77
78     /**
79      * Intercepts participant Register Message
80      * if there is a Commissioning starts an execution of handleParticipantRegister.
81      *
82      * @param participantRegisterMessage the ParticipantRegister message
83      * @param isCommissioning is Commissioning
84      */
85     @AfterReturning(
86             value = "@annotation(MessageIntercept) && args(participantRegisterMessage,..)",
87             returning = "isCommissioning")
88     public void handleParticipantRegister(ParticipantRegister participantRegisterMessage, boolean isCommissioning) {
89         if (isCommissioning) {
90             executor.execute(() -> supervisionScanner.handleParticipantRegister(new ImmutablePair<>(
91                     participantRegisterMessage.getParticipantId(), participantRegisterMessage.getParticipantType())));
92         }
93     }
94
95     @Before("@annotation(MessageIntercept) && args(participantUpdateAckMessage,..)")
96     public void handleParticipantUpdateAck(ParticipantUpdateAck participantUpdateAckMessage) {
97         executor.execute(() -> supervisionScanner.handleParticipantUpdateAck(new ImmutablePair<>(
98                 participantUpdateAckMessage.getParticipantId(), participantUpdateAckMessage.getParticipantType())));
99     }
100
101     @Override
102     public void close() throws IOException {
103         executor.shutdown();
104     }
105 }