3d8308bc1bd1deb6057e3c348eb052ccd65ecf66
[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.Loop;
29 import org.springframework.stereotype.Component;
30
31 @Component
32 public class ClampHttpClient extends AbstractHttpClient {
33
34     private final ClampEndPoints endPoints;
35     public static final String STATUS_NOT_FOUND = "STATUS_NOT_FOUND";
36     public static final String POLICY_NOT_FOUND = "POLICY_NOT_FOUND";
37
38     /**
39      * Constructor.
40      *
41      * @param parameters the DCAE parameters
42      */
43     public ClampHttpClient(ParticipantDcaeParameters parameters) {
44         super(parameters.getClampClientParameters());
45         this.endPoints = parameters.getClampClientEndPoints();
46     }
47
48     /**
49      * Create.
50      *
51      * @param loopName the loopName
52      * @param templateName the templateName
53      * @return the Loop object or null if error occurred
54      */
55     public Loop create(String loopName, String templateName) {
56         return executePost(String.format(endPoints.getCreate(), loopName, templateName), HttpStatus.SC_OK);
57     }
58
59     /**
60      * Deploy.
61      *
62      * @param loopName the loopName
63      * @return true
64      */
65     public boolean deploy(String loopName) {
66         return executePut(endPoints.getDeploy() + loopName, HttpStatus.SC_ACCEPTED);
67     }
68
69     /**
70      * Get Status.
71      *
72      * @param loopName the loopName
73      * @return the Loop object or null if error occurred
74      */
75     public Loop getstatus(String loopName) {
76         return executeGet(endPoints.getStatus() + loopName, HttpStatus.SC_OK);
77     }
78
79     /**
80      * Undeploy.
81      *
82      * @param loopName the loopName
83      * @return true
84      */
85     public boolean undeploy(String loopName) {
86         return executePut(endPoints.getUndeploy() + loopName, HttpStatus.SC_ACCEPTED);
87     }
88
89     /**
90      * Stop.
91      *
92      * @param loopName the loopName
93      * @return true
94      */
95     public boolean stop(String loopName) {
96         return executePut(endPoints.getStop() + loopName, HttpStatus.SC_OK);
97     }
98
99     /**
100      * Delete.
101      *
102      * @param loopName the loopName
103      * @return true
104      */
105     public boolean delete(String loopName) {
106         return executePut(endPoints.getDelete() + loopName, HttpStatus.SC_OK);
107     }
108
109     /**
110      * return status from Loop object.
111      *
112      * @param loop Loop
113      * @return status
114      */
115     public static String getStatusCode(Loop loop) {
116         if (loop == null || loop.getComponents() == null || loop.getComponents().isEmpty()) {
117             return STATUS_NOT_FOUND;
118         }
119         var externalComponent = loop.getComponents().get("DCAE");
120         if (externalComponent == null || externalComponent.getComponentState() == null) {
121             return STATUS_NOT_FOUND;
122         }
123
124         return externalComponent.getComponentState().getStateName();
125     }
126 }