cf7a93fb4aaefabe570ff7e7c5ce3b97f18a59a4
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.participant.intermediary.api.impl;
23
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
29 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
30 import org.onap.policy.clamp.acm.participant.intermediary.handler.AutomationCompositionHandler;
31 import org.onap.policy.clamp.acm.participant.intermediary.handler.ParticipantHandler;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
37 import org.onap.policy.clamp.models.acm.concepts.Participant;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
39 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
42 import org.springframework.stereotype.Component;
43
44 /**
45  * This class is api implementation used by participant intermediary.
46  */
47 @Component
48 public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryApi {
49
50     // The handler for the participant intermediary
51     private final ParticipantHandler participantHandler;
52
53     // The handler for the automationComposition intermediary
54     private final AutomationCompositionHandler automationCompositionHandler;
55
56     /**
57      * Constructor.
58      *
59      * @param participantHandler ParticipantHandler
60      * @param automationCompositionHandler AutomationCompositionHandler
61      */
62     public ParticipantIntermediaryApiImpl(ParticipantHandler participantHandler,
63             AutomationCompositionHandler automationCompositionHandler) {
64         this.participantHandler = participantHandler;
65         this.automationCompositionHandler = automationCompositionHandler;
66     }
67
68     @Override
69     public void registerAutomationCompositionElementListener(
70             AutomationCompositionElementListener automationCompositionElementListener) {
71         automationCompositionHandler.registerAutomationCompositionElementListener(automationCompositionElementListener);
72     }
73
74     @Override
75     public List<Participant> getParticipants(String name, String version) {
76         return List.of(participantHandler.getParticipant(name, version));
77     }
78
79     @Override
80     public Map<String, ToscaProperty> getAcElementDefinitionCommonProperties(ToscaConceptIdentifier acElementDef) {
81         return participantHandler.getAcElementDefinitionCommonProperties(acElementDef);
82     }
83
84     @Override
85     public Participant updateParticipantState(ToscaConceptIdentifier definition, ParticipantState state) {
86         return participantHandler.updateParticipantState(definition, state);
87     }
88
89     @Override
90     public AutomationCompositions getAutomationCompositions(String name, String version) {
91         return automationCompositionHandler.getAutomationCompositions();
92     }
93
94     @Override
95     public Map<UUID, AutomationCompositionElement> getAutomationCompositionElements(String name, String version) {
96         var automationCompositions =
97                 automationCompositionHandler.getAutomationCompositions().getAutomationCompositionList();
98
99         for (var automationComposition : automationCompositions) {
100             if (name.equals(automationComposition.getName())) {
101                 return automationComposition.getElements();
102             }
103         }
104         return new LinkedHashMap<>();
105     }
106
107     @Override
108     public AutomationCompositionElement getAutomationCompositionElement(UUID id) {
109         List<AutomationComposition> automationCompositions =
110                 automationCompositionHandler.getAutomationCompositions().getAutomationCompositionList();
111
112         for (AutomationComposition automationComposition : automationCompositions) {
113             AutomationCompositionElement acElement = automationComposition.getElements().get(id);
114             if (acElement != null) {
115                 return acElement;
116             }
117         }
118         return null;
119     }
120
121     @Override
122     public AutomationCompositionElement updateAutomationCompositionElementState(
123             ToscaConceptIdentifier automationCompositionId, UUID id, AutomationCompositionOrderedState currentState,
124             AutomationCompositionState newState, ParticipantMessageType messageType) {
125         return automationCompositionHandler.updateAutomationCompositionElementState(automationCompositionId, id,
126                 currentState, newState);
127     }
128 }