5cf1b0df95d82f60bd91435e391d2ab6728045cd
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.clamp.controlloop.participant.dcae.httpclient;
24
25 import org.apache.http.HttpStatus;
26 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ClampEndPoints;
27 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameters;
28 import org.onap.policy.clamp.controlloop.participant.dcae.model.ExternalComponent;
29 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
30 import org.springframework.stereotype.Component;
31
32 @Component
33 public class ClampHttpClient extends AbstractHttpClient {
34
35     private final ClampEndPoints endPoints;
36     public static final String STATUS_NOT_FOUND = "STATUS_NOT_FOUND";
37     public static final String POLICY_NOT_FOUND = "POLICY_NOT_FOUND";
38
39     /**
40      * Constructor.
41      *
42      * @param parameters the DCAE parameters
43      */
44     public ClampHttpClient(ParticipantDcaeParameters parameters) {
45         super(parameters.getClampClientParameters());
46         this.endPoints = parameters.getClampClientEndPoints();
47     }
48
49     /**
50      * Create.
51      *
52      * @param loopName the loopName
53      * @param templateName the templateName
54      * @return the Loop object or null if error occurred
55      */
56     public Loop create(String loopName, String templateName) {
57         return executePost(String.format(endPoints.getCreate(), loopName, templateName), HttpStatus.SC_OK);
58     }
59
60     /**
61      * Deploy.
62      *
63      * @param loopName the loopName
64      * @return true
65      */
66     public boolean deploy(String loopName) {
67         return executePut(endPoints.getDeploy() + loopName, HttpStatus.SC_ACCEPTED);
68     }
69
70     /**
71      * Get Status.
72      *
73      * @param loopName the loopName
74      * @return the Loop object or null if error occurred
75      */
76     public Loop getstatus(String loopName) {
77         return executeGet(endPoints.getStatus() + loopName, HttpStatus.SC_OK);
78     }
79
80     /**
81      * Undeploy.
82      *
83      * @param loopName the loopName
84      * @return true
85      */
86     public boolean undeploy(String loopName) {
87         return executePut(endPoints.getUndeploy() + loopName, HttpStatus.SC_ACCEPTED);
88     }
89
90     /**
91      * Stop.
92      *
93      * @param loopName the loopName
94      * @return true
95      */
96     public boolean stop(String loopName) {
97         return executePut(endPoints.getStop() + loopName, HttpStatus.SC_OK);
98     }
99
100     /**
101      * Delete.
102      *
103      * @param loopName the loopName
104      * @return true
105      */
106     public boolean delete(String loopName) {
107         return executePut(endPoints.getDelete() + loopName, HttpStatus.SC_OK);
108     }
109
110     /**
111      * return status from Loop object.
112      *
113      * @param loop Loop
114      * @return status
115      */
116     public static String getStatusCode(Loop loop) {
117         if (loop == null || loop.getComponents() == null || loop.getComponents().isEmpty()) {
118             return STATUS_NOT_FOUND;
119         }
120         var externalComponent = loop.getComponents().get("DCAE");
121         if (externalComponent == null || externalComponent.getComponentState() == null) {
122             return STATUS_NOT_FOUND;
123         }
124
125         return externalComponent.getComponentState().getStateName();
126     }
127 }