c0ac329e071d5b827efea735bed9eaf3955df175
[policy/clamp.git] /
1 /*-
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
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.element.handler;
22
23 import io.micrometer.core.annotation.Timed;
24 import jakarta.ws.rs.core.Response;
25 import java.util.EnumMap;
26 import java.util.List;
27 import java.util.Map;
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;
37
38 @Component
39 public class MessageHandler {
40
41     private ElementType elementType;
42     private final ToscaConceptIdentifier elementId;
43
44     private final Map<ElementType, ElementService> map = new EnumMap<>(ElementType.class);
45
46     /**
47      * Constructor.
48      *
49      * @param acElement       AcElement
50      * @param elementServices ElementService list
51      */
52     public MessageHandler(AcElement acElement, List<ElementService> elementServices) {
53         elementId = acElement.getElementId();
54         elementServices.forEach(elementService -> map.put(elementService.getType(), elementService));
55     }
56
57     /**
58      * Active Element Service.
59      *
60      * @param elementConfig ElementConfig
61      */
62     public void active(@NonNull ElementConfig elementConfig) {
63         this.elementType = elementConfig.getElementType();
64         getActiveService().active(elementConfig);
65     }
66
67     /**
68      * Update configuration.
69      *
70      * @param elementConfig ElementConfig
71      */
72     public void update(@NonNull ElementConfig elementConfig) {
73         if (elementType == null) {
74             throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "ElementType not defined!");
75         }
76         if (!elementType.equals(elementConfig.getElementType())) {
77             throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "wrong ElementType!");
78         }
79         getActiveService().update(elementConfig);
80     }
81
82     /**
83      * Get Active Service.
84      *
85      * @return ElementService
86      */
87     public ElementService getActiveService() {
88         if (elementType == null) {
89             throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "ElementType not defined!");
90         }
91         var service = map.get(elementType);
92         if (service == null) {
93             throw new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "ElementService not found!");
94         }
95         return service;
96     }
97
98     @Timed(value = "listener.status", description = "STATUS messages received")
99     public void handleMessage(ElementMessage message) {
100         getActiveService().handleMessage(message);
101     }
102
103     public boolean appliesTo(final ToscaConceptIdentifier elementId) {
104         return this.elementId.equals(elementId);
105     }
106
107     public void deactivateElement() {
108         getActiveService().deactivate();
109         elementType = null;
110     }
111 }