feat:Enhance sliceanalysis MS to use DCAE SDK dmaap-client lib
[dcaegen2/services.git] / components / slice-analysis-ms / src / main / java / org / onap / slice / analysis / ms / service / PolicyService.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  slice-analysis-ms
4  *  ================================================================================
5  *   Copyright (C) 2020-2021 Wipro Limited.
6  *   Copyright (C) 2022 Huawei Canada Limited.
7  *   Copyright (C) 2022 CTC, Inc.
8  *   ==============================================================================
9  *     Licensed under the Apache License, Version 2.0 (the "License");
10  *     you may not use this file except in compliance with the License.
11  *     You may obtain a copy of the License at
12  *
13  *          http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *     Unless required by applicable law or agreed to in writing, software
16  *     distributed under the License is distributed on an "AS IS" BASIS,
17  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  *     See the License for the specific language governing permissions and
19  *     limitations under the License.
20  *     ============LICENSE_END=========================================================
21  *
22  *******************************************************************************/
23
24 package org.onap.slice.analysis.ms.service;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31
32 import javax.annotation.PostConstruct;
33
34 import org.onap.slice.analysis.ms.dmaap.PolicyDmaapClient;
35 import org.onap.slice.analysis.ms.models.Configuration;
36 import org.onap.slice.analysis.ms.models.policy.AAI;
37 import org.onap.slice.analysis.ms.models.policy.AdditionalProperties;
38 import org.onap.slice.analysis.ms.models.policy.OnsetMessage;
39 import org.onap.slice.analysis.ms.models.policy.Payload;
40 import org.onap.slice.analysis.ms.models.policy.Sla;
41 import org.onap.slice.analysis.ms.models.policy.TransportNetwork;
42 import org.onap.slice.analysis.ms.service.ccvpn.RequestOwner;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.stereotype.Component;
46
47 import com.fasterxml.jackson.databind.ObjectMapper;
48
49 /**
50  * Serivce to generate and publish onsetMessage to ONAP/Policy
51  */
52 @Component
53 public class PolicyService {
54     private PolicyDmaapClient policyDmaapClient;
55     private static Logger log = LoggerFactory.getLogger(PolicyService.class);
56     private ObjectMapper objectMapper = new ObjectMapper();
57
58     /**
59      * Initialization
60      */
61     @PostConstruct
62     public void init() {
63         Configuration configuration = Configuration.getInstance();
64         policyDmaapClient = new PolicyDmaapClient(configuration);
65     }
66
67     protected <T> OnsetMessage formPolicyOnsetMessage(String snssai, AdditionalProperties<T> addProps, Map<String, String> serviceDetails) {
68         OnsetMessage onsetmsg = new OnsetMessage();
69         Payload payload = new Payload();
70         payload.setGlobalSubscriberId(serviceDetails.get("globalSubscriberId"));
71         payload.setSubscriptionServiceType(serviceDetails.get("subscriptionServiceType"));
72         payload.setNetworkType("AN");
73         payload.setName(serviceDetails.get("ranNFNSSIId"));
74         payload.setServiceInstanceID(serviceDetails.get("ranNFNSSIId"));
75
76         addProps.setModifyAction("");
77         Map<String, String> nsiInfo = new HashMap<>();
78         nsiInfo.put("nsiId", UUID.randomUUID().toString());
79         nsiInfo.put("nsiName", "");
80         addProps.setNsiInfo(nsiInfo);
81         addProps.setScriptName("AN");
82         addProps.setSliceProfileId(serviceDetails.get("sliceProfileId"));
83         addProps.setModifyAction("reconfigure");
84         List<String> snssaiList = new ArrayList<>();
85         snssaiList.add(snssai);
86         addProps.setSnssaiList(snssaiList);
87
88         payload.setAdditionalProperties(addProps);
89         try {
90             onsetmsg.setPayload(objectMapper.writeValueAsString(payload));
91         } catch (Exception e) {
92             log.error("Error while mapping payload as string , {}",e.getMessage());
93         }
94
95         onsetmsg.setClosedLoopControlName("ControlLoop-Slicing-116d7b00-dbeb-4d03-8719-d0a658fa735b");
96         onsetmsg.setClosedLoopAlarmStart(System.currentTimeMillis());
97         onsetmsg.setClosedLoopEventClient("microservice.sliceAnalysisMS");
98         onsetmsg.setClosedLoopEventStatus("ONSET");
99         onsetmsg.setRequestID(UUID.randomUUID().toString());
100         onsetmsg.setTarget("generic-vnf.vnf-id");
101         onsetmsg.setTargetType("VNF");
102         onsetmsg.setFrom("DCAE");
103         onsetmsg.setVersion("1.0.2");
104         AAI aai = new AAI();
105         aai.setVserverIsClosedLoopDisabled("false");
106         aai.setVserverProvStatus("ACTIVE");
107         aai.setGenericVnfVNFId(serviceDetails.get("ranNFNSSIId"));
108         onsetmsg.setAai(aai);
109         return onsetmsg;
110     }
111
112     protected <T> void sendOnsetMessageToPolicy(String snssai, AdditionalProperties<T> addProps, Map<String, String> serviceDetails) {
113         OnsetMessage onsetMessage = formPolicyOnsetMessage(snssai, addProps, serviceDetails);
114         String msg =  "";
115         try {
116             msg = objectMapper.writeValueAsString(onsetMessage);
117             log.info("Policy onset message for S-NSSAI: {} is {}", snssai, msg);
118             policyDmaapClient.sendNotificationToPolicy(msg);
119         }
120         catch (Exception e) {
121             log.error("Error sending notification to policy, {}",e.getMessage());
122         }
123     }
124
125     /**
126      * Generate onsetMessage for ccvpn service update operation
127      * @param cllId cloud leased line Id (ethernet service id)
128      * @param newBw new bandwidth value for bandwidth adjustment
129      * @param <T> type for additionalPropert
130      *           ies, can be omitted
131      * @return OnsetMessage result
132      */
133     public <T> OnsetMessage formPolicyOnsetMessageForCCVPN(String cllId, Integer newBw, RequestOwner owner) {
134         Sla sla = new Sla(2, newBw);
135         String transportNetworkId = cllId;
136         if (owner == RequestOwner.UUI) {
137             transportNetworkId += "-network-001";
138         } else if (owner == RequestOwner.DCAE) {
139             transportNetworkId += "-network-002";
140         }
141         TransportNetwork transportNetwork = new TransportNetwork(transportNetworkId, sla);
142         AdditionalProperties additionalProperties = new AdditionalProperties();
143         additionalProperties.setModifyAction("bandwidth");
144         additionalProperties.setEnableSdnc("true");
145         List<TransportNetwork> transportNetworks = new ArrayList();
146         transportNetworks.add(transportNetwork);
147         additionalProperties.setTransportNetworks(transportNetworks);
148
149         Payload payload = new Payload();
150         payload.setGlobalSubscriberId("IBNCustomer");
151         payload.setSubscriptionServiceType("IBN");
152         payload.setServiceType("CLL");
153         payload.setName("cloud-leased-line-101");
154         payload.setServiceInstanceID(cllId);
155         payload.setAdditionalProperties(additionalProperties);
156         payload.setModelInvariantUuid("6790ab0e-034f-11eb-adc1-0242ac120002");
157         payload.setModelUuid("6790ab0e-034f-11eb-adc1-0242ac120002");
158
159         OnsetMessage onsetmsg = new OnsetMessage();
160         try {
161             onsetmsg.setPayload(objectMapper.writeValueAsString(payload));
162         } catch (Exception e) {
163             log.error("Error while mapping payload as string , {}",e.getMessage());
164         }
165         onsetmsg.setClosedLoopControlName("ControlLoop-CCVPN-CLL-227e8b00-dbeb-4d03-8719-d0a658fb846c");
166         onsetmsg.setClosedLoopAlarmStart(System.currentTimeMillis());
167         onsetmsg.setClosedLoopEventClient("microservice.sliceAnalysisMS");
168         onsetmsg.setClosedLoopEventStatus("ONSET");
169         onsetmsg.setRequestID(UUID.randomUUID().toString());
170         onsetmsg.setTarget("generic-vnf.vnf-id");
171         onsetmsg.setTargetType("VNF");
172         onsetmsg.setFrom("DCAE");
173         onsetmsg.setVersion("1.0.2");
174         AAI aai = new AAI();
175         aai.setGenericVnfIsClosedLoopDisabled("false");
176         aai.setGenericVnfProvStatus("ACTIVE");
177         aai.setGenericVnfVNFId("00000000-0000-0000-0000-000000000000");
178         aai.setGenericVnfVnfName("00000");
179         onsetmsg.setAai(aai);
180         return onsetmsg;
181     }
182
183     /**
184      * Sending the onsetMessage to Onap-Policy through PolicyDmaapClient
185      * @param onsetMessage the onsetMessage about to send
186      * @param <T> type inherent from previous implementation can be omitted
187      */
188     public <T> void sendOnsetMessageToPolicy(OnsetMessage onsetMessage){
189         String msg =  "";
190         try {
191             msg = objectMapper.writeValueAsString(onsetMessage);
192             log.info("Sending onset message to Onap/Policy for ControlLoop-CCVPN-CLL, the msg: {}", msg);
193             policyDmaapClient.sendNotificationToPolicy(msg);
194         }
195         catch (Exception e) {
196             log.error("Error sending notification to policy, {}",e.getMessage());
197         }
198     }
199 }