9e30c8809179c888b64b9ea022c7fd0f58bbbd0f
[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.acm.participant.simulator.main.handler;
22
23 import java.time.Instant;
24 import java.util.UUID;
25 import lombok.Setter;
26 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
27 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
28 import org.onap.policy.clamp.models.acm.concepts.AcElementStatistics;
29 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
31 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
32 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39
40 /**
41  * This class handles implementation of automationCompositionElement updates.
42  */
43 @Component
44 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandler.class);
47
48     @Setter
49     private ParticipantIntermediaryApi intermediaryApi;
50
51     /**
52      * Callback method to handle a automation composition element state change.
53      *
54      * @param automationCompositionElementId the ID of the automation composition element
55      * @param currentState the current state of the automation composition element
56      * @param newState the state to which the automation composition element is changing to
57      * @throws PfModelException in case of an exception
58      */
59     @Override
60     public void automationCompositionElementStateChange(ToscaConceptIdentifier automationCompositionId,
61         UUID automationCompositionElementId, AutomationCompositionState currentState,
62         AutomationCompositionOrderedState newState) throws PfModelException {
63         switch (newState) {
64             case UNINITIALISED:
65                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
66                     automationCompositionElementId, newState, AutomationCompositionState.UNINITIALISED,
67                     ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
68                 break;
69             case PASSIVE:
70                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
71                     automationCompositionElementId, newState, AutomationCompositionState.PASSIVE,
72                     ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
73                 break;
74             case RUNNING:
75                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
76                     automationCompositionElementId, newState, AutomationCompositionState.RUNNING,
77                     ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
78                 break;
79             default:
80                 LOGGER.debug("Unknown orderedstate {}", newState);
81                 break;
82         }
83     }
84
85     /**
86      * Callback method to handle an update on a automation composition element.
87      *
88      * @param element the information on the automation composition element
89      * @param acElementDefinition toscaNodeTemplate
90      * @throws PfModelException in case of an exception
91      */
92     @Override
93     public void automationCompositionElementUpdate(ToscaConceptIdentifier automationCompositionId,
94         AutomationCompositionElement element, ToscaNodeTemplate acElementDefinition) throws PfModelException {
95         intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
96             element.getOrderedState(), AutomationCompositionState.PASSIVE,
97             ParticipantMessageType.AUTOMATION_COMPOSITION_UPDATE);
98     }
99
100     @Override
101     public void handleStatistics(UUID automationCompositionElementId) throws PfModelException {
102         var acElement = intermediaryApi.getAutomationCompositionElement(automationCompositionElementId);
103         if (acElement != null) {
104             var acElementStatistics = new AcElementStatistics();
105             acElementStatistics.setState(acElement.getState());
106             acElementStatistics.setTimeStamp(Instant.now());
107             intermediaryApi.updateAutomationCompositionElementStatistics(automationCompositionElementId,
108                 acElementStatistics);
109         }
110     }
111
112 }