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 java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
30 import org.onap.policy.clamp.acm.element.main.parameters.AcElement;
31 import org.onap.policy.clamp.acm.element.service.ElementService;
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.base.PfModelRuntimeException;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.springframework.stereotype.Component;
40 public class MessageHandler {
42 private ElementType elementType;
43 private ToscaConceptIdentifier elementId;
45 private Map<ElementType, ElementService> map = new HashMap<>();
48 private List<ElementMessage> messages = new ArrayList<>();
53 * @param acElement AcElement
54 * @param elementServices ElementService list
56 public MessageHandler(AcElement acElement, List<ElementService> elementServices) {
57 elementId = acElement.getElementId();
58 elementServices.stream().forEach(elementService -> map.put(elementService.getType(), elementService));
62 * Active Element Service.
64 * @param elementConfig ElementConfig
66 public void active(@NonNull ElementConfig elementConfig) {
67 this.elementType = elementConfig.getElementType();
68 getActiveService().active(elementConfig);
72 * Update configuration.
74 * @param elementConfig ElementConfig
76 public void update(@NonNull ElementConfig elementConfig) {
77 if (elementType == null) {
78 throw new PfModelRuntimeException(Response.Status.CONFLICT, "ElementType not defined!");
80 if (!elementType.equals(elementConfig.getElementType())) {
81 throw new PfModelRuntimeException(Response.Status.CONFLICT, "wrong ElementType!");
83 getActiveService().update(elementConfig);
89 * @return ElementService
91 public ElementService getActiveService() {
92 if (elementType == null) {
93 throw new PfModelRuntimeException(Response.Status.CONFLICT, "ElementType not defined!");
95 var service = map.get(elementType);
96 if (service == null) {
97 throw new PfModelRuntimeException(Response.Status.CONFLICT, "ElementService not found!");
102 public void handleMessage(ElementMessage message) {
103 messages.add(message);
104 getActiveService().handleMessage(message);
107 public boolean appliesTo(final ToscaConceptIdentifier elementId) {
108 return this.elementId.equals(elementId);
111 public void deactivateElement() {
112 getActiveService().deactivate();