move actors code in drools-applications to policy/models
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / main / java / org / onap / policy / controlloop / actor / sdnc / SdncActorServiceProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SdncActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2018 Huawei 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.sdnc;
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 import java.util.UUID;
30
31 import org.onap.policy.aai.AaiGetVnfResponse;
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.sdnc.SdncHealNetworkInfo;
37 import org.onap.policy.sdnc.SdncHealRequest;
38 import org.onap.policy.sdnc.SdncHealRequestHeaderInfo;
39 import org.onap.policy.sdnc.SdncHealRequestInfo;
40 import org.onap.policy.sdnc.SdncHealServiceInfo;
41 import org.onap.policy.sdnc.SdncRequest;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47 public class SdncActorServiceProvider implements Actor {
48     private static final Logger logger = LoggerFactory.getLogger(SdncActorServiceProvider.class);
49
50     // Strings for Sdnc Actor
51     private static final String SDNC_ACTOR = "SDNC";
52
53     // Strings for targets
54     private static final String TARGET_VM = "VM";
55
56     // Strings for recipes
57     private static final String RECIPE_REROUTE = "Reroute";
58
59     private static final ImmutableList<String> recipes = ImmutableList.of(RECIPE_REROUTE);
60     private static final ImmutableMap<String, List<String>> targets =
61             new ImmutableMap.Builder<String, List<String>>().put(RECIPE_REROUTE, ImmutableList.of(TARGET_VM)).build();
62
63     @Override
64     public String actor() {
65         return SDNC_ACTOR;
66     }
67
68     @Override
69     public List<String> recipes() {
70         return ImmutableList.copyOf(recipes);
71     }
72
73     @Override
74     public List<String> recipeTargets(String recipe) {
75         return ImmutableList.copyOf(targets.getOrDefault(recipe, Collections.emptyList()));
76     }
77
78     @Override
79     public List<String> recipePayloads(String recipe) {
80         return Collections.emptyList();
81     }
82
83     /**
84      * Construct a request.
85      *
86      * @param onset the onset event
87      * @param operation the control loop operation
88      * @param policy the policy
89      * @return the constructed request
90      */
91     public static SdncRequest constructRequest(VirtualControlLoopEvent onset, ControlLoopOperation operation,
92             Policy policy) {
93
94         if (!policy.getRecipe().equalsIgnoreCase(RECIPE_REROUTE)) {
95             return null;
96         }
97
98         // Construct an Sdnc request
99         String serviceInstance = onset.getAai().get("service-instance.service-instance-id");
100         if (serviceInstance == null || serviceInstance.isEmpty()) {
101             // This indicates that AAI Enrichment needs to be done by event producer. 
102             return null;
103         }
104         SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
105         serviceInfo.setServiceInstanceId(serviceInstance);
106         
107         String networkId = onset.getAai().get("network-information.network-id");
108         if (networkId == null || networkId.isEmpty()) {
109             // This indicates that AAI Enrichment needs to be done by event producer. 
110             return null;
111         }
112         SdncHealNetworkInfo networkInfo = new SdncHealNetworkInfo();        
113         networkInfo.setNetworkId(networkId);
114
115         SdncHealRequestInfo requestInfo = new SdncHealRequestInfo();
116         requestInfo.setRequestAction("ReoptimizeSOTNInstance");
117
118         SdncHealRequestHeaderInfo headerInfo = new SdncHealRequestHeaderInfo();
119         headerInfo.setSvcAction("reoptimize");
120         headerInfo.setSvcRequestId(UUID.randomUUID().toString());
121
122         SdncRequest request = new SdncRequest();
123         request.setNsInstanceId(serviceInstance);
124         request.setRequestId(onset.getRequestId());
125
126         SdncHealRequest healRequest = new SdncHealRequest();
127         healRequest.setRequestHeaderInfo(headerInfo);
128         healRequest.setNetworkInfo(networkInfo);
129         healRequest.setRequestInfo(requestInfo);
130         healRequest.setServiceInfo(serviceInfo);
131         request.setHealRequest(healRequest);
132
133         return request;
134     }
135 }