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.handler;
23 import io.micrometer.core.annotation.Timed;
24 import java.util.HashMap;
25 import java.util.List;
27 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
29 import org.onap.policy.clamp.acm.element.main.parameters.AcElement;
30 import org.onap.policy.clamp.acm.element.service.ElementService;
31 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
32 import org.onap.policy.clamp.models.acm.messages.dmaap.element.ElementMessage;
33 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
34 import org.onap.policy.clamp.models.acm.messages.rest.element.ElementType;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.springframework.stereotype.Component;
39 public class MessageHandler {
41 private ElementType elementType;
42 private ToscaConceptIdentifier elementId;
44 private Map<ElementType, ElementService> map = new HashMap<>();
49 * @param acElement AcElement
50 * @param elementServices ElementService list
52 public MessageHandler(AcElement acElement, List<ElementService> elementServices) {
53 elementId = acElement.getElementId();
54 elementServices.stream().forEach(elementService -> map.put(elementService.getType(), elementService));
58 * Active Element Service.
60 * @param elementConfig ElementConfig
62 public void active(@NonNull ElementConfig elementConfig) {
63 this.elementType = elementConfig.getElementType();
64 getActiveService().active(elementConfig);
68 * Update configuration.
70 * @param elementConfig ElementConfig
72 public void update(@NonNull ElementConfig elementConfig) {
73 if (elementType == null) {
74 throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "ElementType not defined!");
76 if (!elementType.equals(elementConfig.getElementType())) {
77 throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "wrong ElementType!");
79 getActiveService().update(elementConfig);
85 * @return ElementService
87 public ElementService getActiveService() {
88 if (elementType == null) {
89 throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "ElementType not defined!");
91 var service = map.get(elementType);
92 if (service == null) {
93 throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "ElementService not found!");
98 @Timed(value = "listener.status", description = "STATUS messages received")
99 public void handleMessage(ElementMessage message) {
100 getActiveService().handleMessage(message);
103 public boolean appliesTo(final ToscaConceptIdentifier elementId) {
104 return this.elementId.equals(elementId);
107 public void deactivateElement() {
108 getActiveService().deactivate();