aboutsummaryrefslogtreecommitdiffstats
path: root/controlloop/common/feature-controlloop-management/src/main/java/org/onap/policy/drools/server/restful/RestControlLoopManager.java
blob: b33ab77042bce631fcbd0a7b34482ff2093a7fba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.server.restful;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import org.onap.policy.aai.AaiManager;
import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
import org.onap.policy.drools.apps.controlloop.feature.management.ControlLoopManagementFeature;
import org.onap.policy.drools.system.PolicyEngineConstants;
import org.onap.policy.rest.RestManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Telemetry Extensions for Control Loops in the PDP-D.
 */

@Path("/policy/pdp")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class RestControlLoopManager implements PolicyApi {
    private static final Logger logger = LoggerFactory.getLogger(RestControlLoopManager.class);

    /**
     * GET control loops.
     *
     * @param controllerName controller name.
     * @param sessionName session name.
     * @return list of controller names.
     */
    @GET
    @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops")
    public Response controlLoops(
        @PathParam("controller") String controllerName,
        @PathParam("session") String sessionName) {

        try {
            List<String> controlLoopNames =
                ControlLoopManagementFeature.controlLoops(controllerName, sessionName)
                    .map(ControlLoopParams::getClosedLoopControlName)
                    .collect(Collectors.toList());

            return Response.status(Response.Status.OK).entity(controlLoopNames).build();
        } catch (IllegalArgumentException e) {
            logger.error("'GET' controlloops threw an exception", e);
            return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
        }
    }

    /**
     * GET control loop.
     *
     * @param controllerName controller name.
     * @param sessionName session name.
     * @param controlLoopName control loop name.
     * @return control loop.
     */
    @GET
    @Path("engine/controllers/{controller}/drools/facts/{session}/controlloops/{controlLoopName}")
    public Response controlLoop(
        @PathParam("controller") String controllerName,
        @PathParam("session") String sessionName,
        @PathParam("controlLoopName") String controlLoopName) {

        try {
            List<ControlLoopParams> controlLoopParams =
                ControlLoopManagementFeature.controlLoops(controllerName, sessionName)
                    .filter(c -> c.getClosedLoopControlName().equals(controlLoopName))
                    .collect(Collectors.toList());

            return Response.status(Response.Status.OK).entity(controlLoopParams).build();
        } catch (IllegalArgumentException e) {
            logger.error("'GET' controlloop threw an exception", e);
            return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
        }
    }

    /**
     * AAI Custom Query.
     *
     * @param vserverId vServer identifier.
     * @return query results.
     */
    @GET
    @Path("engine/tools/controlloops/aai/customQuery/{vserverId}")
    public Response aaiCustomQuery(@PathParam("vserverId") String vserverId) {
        var mgr = PolicyEngineConstants.getManager();

        return Response
            .status(Status.OK)
            .entity(new AaiManager(new RestManager())
                .getCustomQueryResponse(
                    mgr.getEnvironmentProperty("aai.url"),
                    mgr.getEnvironmentProperty("aai.username"),
                    mgr.getEnvironmentProperty("aai.password"),
                    UUID.randomUUID(),
                    vserverId))
            .build();
    }

}