more sonar issues in drools-applications
[policy/drools-applications.git] / controlloop / common / actors / actor.appc / src / main / java / org / onap / policy / controlloop / actor / appc / APPCActorServiceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPCActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.appc;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.onap.policy.appc.CommonHeader;
30 import org.onap.policy.appc.Request;
31 import org.onap.policy.controlloop.ControlLoopOperation;
32 import org.onap.policy.controlloop.VirtualControlLoopEvent;
33 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
34 import org.onap.policy.controlloop.policy.Policy;
35 import org.onap.policy.vnf.trafficgenerator.PGRequest;
36 import org.onap.policy.vnf.trafficgenerator.PGStream;
37 import org.onap.policy.vnf.trafficgenerator.PGStreams;
38
39
40 public class APPCActorServiceProvider implements Actor {
41     // Strings for targets
42     private static final String TARGET_VM = "VM";
43     private static final String TARGET_VNF = "VNF";
44
45     // Strings for recipes
46     private static final String RECIPE_RESTART = "Restart";
47     private static final String RECIPE_REBUILD = "Rebuild";
48     private static final String RECIPE_MIGRATE = "Migrate";
49     private static final String RECIPE_MODIFY = "ModifyConfig";
50
51     private static final ImmutableList<String> recipes =
52             ImmutableList.of(RECIPE_RESTART, RECIPE_REBUILD, RECIPE_MIGRATE, RECIPE_MODIFY);
53     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
54             .put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).put(RECIPE_REBUILD, ImmutableList.of(TARGET_VM))
55             .put(RECIPE_MIGRATE, ImmutableList.of(TARGET_VM)).put(RECIPE_MODIFY, ImmutableList.of(TARGET_VNF)).build();
56     private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
57             .put(RECIPE_MODIFY, ImmutableList.of("generic-vnf.vnf-id")).build();
58
59     @Override
60     public String actor() {
61         return "APPC";
62     }
63
64     @Override
65     public List<String> recipes() {
66         return ImmutableList.copyOf(recipes);
67     }
68
69     @Override
70     public List<String> recipeTargets(String recipe) {
71         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
72     }
73
74     @Override
75     public List<String> recipePayloads(String recipe) {
76         return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
77     }
78
79     /**
80      * Constructs an APPC request conforming to the legacy API. The legacy API will be deprecated in
81      * future releases as all legacy functionality is moved into the LCM API.
82      * 
83      * @param onset the event that is reporting the alert for policy to perform an action
84      * @param operation the control loop operation specifying the actor, operation, target, etc.
85      * @param policy the policy the was specified from the yaml generated by CLAMP or through the
86      *        Policy GUI/API
87      * @return an APPC request conforming to the legacy API
88      */
89     public static Request constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
90             String targetVnf) {
91         /*
92          * Construct an APPC request
93          */
94         Request request = new Request();
95         request.setCommonHeader(new CommonHeader());
96         request.getCommonHeader().setRequestId(onset.getRequestId());
97         request.getCommonHeader().setSubRequestId(operation.getSubRequestId());
98         request.setAction(policy.getRecipe().substring(0, 1).toUpperCase() + policy.getRecipe().substring(1));
99
100         /*
101          * For now Policy generates the PG Streams as a demo, in the future the payload can be
102          * provided by CLAMP
103          */
104         request.getPayload().put("generic-vnf.vnf-id", targetVnf);
105
106         PGRequest pgRequest = new PGRequest();
107         pgRequest.pgStreams = new PGStreams();
108
109         PGStream pgStream;
110         for (int i = 0; i < 5; i++) {
111             pgStream = new PGStream();
112             pgStream.streamId = "fw_udp" + (i + 1);
113             pgStream.isEnabled = "true";
114             pgRequest.pgStreams.pgStream.add(pgStream);
115         }
116         request.getPayload().put("pg-streams", pgRequest.pgStreams);
117
118         /*
119          * Return the request
120          */
121
122         return request;
123     }
124
125
126 }