move actors code in drools-applications to policy/models
[policy/models.git] / models-interactions / model-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-2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.appc;
23
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26
27 import java.util.Collections;
28 import java.util.List;
29
30 import org.onap.policy.appc.CommonHeader;
31 import org.onap.policy.appc.Request;
32 import org.onap.policy.controlloop.ControlLoopOperation;
33 import org.onap.policy.controlloop.VirtualControlLoopEvent;
34 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
35 import org.onap.policy.controlloop.policy.Policy;
36 import org.onap.policy.vnf.trafficgenerator.PgRequest;
37 import org.onap.policy.vnf.trafficgenerator.PgStream;
38 import org.onap.policy.vnf.trafficgenerator.PgStreams;
39
40
41 public class AppcActorServiceProvider implements Actor {
42     // Strings for targets
43     private static final String TARGET_VM = "VM";
44     private static final String TARGET_VNF = "VNF";
45
46     // Strings for recipes
47     private static final String RECIPE_RESTART = "Restart";
48     private static final String RECIPE_REBUILD = "Rebuild";
49     private static final String RECIPE_MIGRATE = "Migrate";
50     private static final String RECIPE_MODIFY = "ModifyConfig";
51
52     private static final ImmutableList<String> recipes =
53             ImmutableList.of(RECIPE_RESTART, RECIPE_REBUILD, RECIPE_MIGRATE, RECIPE_MODIFY);
54     private static final ImmutableMap<String, List<String>> targets = new ImmutableMap.Builder<String, List<String>>()
55             .put(RECIPE_RESTART, ImmutableList.of(TARGET_VM)).put(RECIPE_REBUILD, ImmutableList.of(TARGET_VM))
56             .put(RECIPE_MIGRATE, ImmutableList.of(TARGET_VM)).put(RECIPE_MODIFY, ImmutableList.of(TARGET_VNF)).build();
57     private static final ImmutableMap<String, List<String>> payloads = new ImmutableMap.Builder<String, List<String>>()
58             .put(RECIPE_MODIFY, ImmutableList.of("generic-vnf.vnf-id")).build();
59
60     @Override
61     public String actor() {
62         return "APPC";
63     }
64
65     @Override
66     public List<String> recipes() {
67         return ImmutableList.copyOf(recipes);
68     }
69
70     @Override
71     public List<String> recipeTargets(String recipe) {
72         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
73     }
74
75     @Override
76     public List<String> recipePayloads(String recipe) {
77         return ImmutableList.copyOf(payloads.getOrDefault(recipe, Collections.emptyList()));
78     }
79
80     /**
81      * Constructs an APPC request conforming to the legacy API. The legacy API will be deprecated in
82      * future releases as all legacy functionality is moved into the LCM API.
83      *
84      * @param onset the event that is reporting the alert for policy to perform an action
85      * @param operation the control loop operation specifying the actor, operation, target, etc.
86      * @param policy the policy the was specified from the yaml generated by CLAMP or through the
87      *        Policy GUI/API
88      * @return an APPC request conforming to the legacy API
89      */
90     public static Request constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation, Policy policy,
91             String targetVnf) {
92         /*
93          * Construct an APPC request
94          */
95         Request request = new Request();
96         request.setCommonHeader(new CommonHeader());
97         request.getCommonHeader().setRequestId(onset.getRequestId());
98         request.getCommonHeader().setSubRequestId(operation.getSubRequestId());
99         request.setAction(policy.getRecipe().substring(0, 1).toUpperCase() + policy.getRecipe().substring(1));
100
101         /*
102          * For now Policy generates the PG Streams as a demo, in the future the payload can be
103          * provided by CLAMP
104          */
105         request.getPayload().put("generic-vnf.vnf-id", targetVnf);
106
107         PgRequest pgRequest = new PgRequest();
108         pgRequest.pgStreams = new PgStreams();
109
110         PgStream pgStream;
111         for (int i = 0; i < 5; i++) {
112             pgStream = new PgStream();
113             pgStream.streamId = "fw_udp" + (i + 1);
114             pgStream.isEnabled = "true";
115             pgRequest.pgStreams.pgStream.add(pgStream);
116         }
117         request.getPayload().put("pg-streams", pgRequest.pgStreams);
118
119         /*
120          * Return the request
121          */
122
123         return request;
124     }
125
126
127 }