2d789d40de9e5b1862365be322b254842ce96530
[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.participant.intermediary.handler;
22
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.List;
26 import org.onap.policy.clamp.controlloop.models.messages.dmaap.participant.ParticipantMessageType;
27 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopStateChangeListener;
28 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ControlLoopUpdateListener;
29 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantHealthCheckListener;
30 import org.onap.policy.clamp.controlloop.participant.intermediary.comm.ParticipantStateChangeListener;
31 import org.onap.policy.clamp.controlloop.participant.intermediary.parameters.ParticipantParameters;
32 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
33 import org.onap.policy.common.endpoints.event.comm.TopicSource;
34 import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher;
35 import org.onap.policy.common.utils.services.ServiceManagerContainer;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.context.event.ContextClosedEvent;
38 import org.springframework.context.event.ContextRefreshedEvent;
39 import org.springframework.context.event.EventListener;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * This class activates the Participant Intermediary together with all its handlers.
44  */
45 @Component
46 public class IntermediaryActivator extends ServiceManagerContainer implements Closeable {
47
48     private final ApplicationContext applicationContext;
49
50     // Topics from which the participant receives and to which the participant sends messages
51     private List<TopicSource> topicSources;
52
53     /**
54      * Instantiate the activator for participant.
55      *
56      * @param applicationContext ApplicationContext
57      * @param parameters the ParticipantParameters
58      */
59     public IntermediaryActivator(final ApplicationContext applicationContext, final ParticipantParameters parameters) {
60         this.applicationContext = applicationContext;
61
62         topicSources = TopicEndpointManager.getManager()
63                 .addTopicSources(parameters.getIntermediaryParameters().getClampControlLoopTopics().getTopicSources());
64
65         // @formatter:off
66
67         addAction("Topic endpoint management",
68             () -> TopicEndpointManager.getManager().start(),
69             () -> TopicEndpointManager.getManager().shutdown());
70
71         addAction("Topic Message Dispatcher", this::registerMsgDispatcher, this::unregisterMsgDispatcher);
72         // @formatter:on
73     }
74
75     /**
76      * Handle ContextRefreshEvent.
77      *
78      * @param ctxRefreshedEvent ContextRefreshedEvent
79      */
80     @EventListener
81     public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) {
82         if (!isAlive()) {
83             start();
84         }
85     }
86
87     /**
88      * Handle ContextClosedEvent.
89      *
90      * @param ctxClosedEvent ContextClosedEvent
91      */
92     @EventListener
93     public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) {
94         if (isAlive()) {
95             stop();
96         }
97     }
98
99     /**
100      * Registers the dispatcher with the topic source(s).
101      */
102     private void registerMsgDispatcher() {
103         MessageTypeDispatcher msgDispatcher = applicationContext.getBean(MessageTypeDispatcher.class);
104
105         msgDispatcher.register(ParticipantMessageType.PARTICIPANT_STATE_CHANGE.name(),
106                 applicationContext.getBean(ParticipantStateChangeListener.class));
107
108         msgDispatcher.register(ParticipantMessageType.PARTICIPANT_HEALTH_CHECK.name(),
109                 applicationContext.getBean(ParticipantHealthCheckListener.class));
110
111         msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_STATE_CHANGE.name(),
112                 applicationContext.getBean(ControlLoopStateChangeListener.class));
113
114         msgDispatcher.register(ParticipantMessageType.PARTICIPANT_CONTROL_LOOP_UPDATE.name(),
115                 applicationContext.getBean(ControlLoopUpdateListener.class));
116
117         for (final TopicSource source : topicSources) {
118             source.register(msgDispatcher);
119         }
120     }
121
122     /**
123      * Unregisters the dispatcher from the topic source(s).
124      */
125     private void unregisterMsgDispatcher() {
126         MessageTypeDispatcher msgDispatcher = applicationContext.getBean(MessageTypeDispatcher.class);
127
128         for (final TopicSource source : topicSources) {
129             source.unregister(msgDispatcher);
130         }
131     }
132
133     @Override
134     public void close() throws IOException {
135         super.shutdown();
136     }
137 }