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