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