1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 *******************************************************************************/
24 package org.onap.slice.analysis.ms.service;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.UUID;
32 import javax.annotation.PostConstruct;
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;
47 import com.fasterxml.jackson.databind.ObjectMapper;
50 * Serivce to generate and publish onsetMessage to ONAP/Policy
53 public class PolicyService {
54 private final static int SERVICE_RATE_INTERVAL = 5000; // in ms
55 private PolicyDmaapClient policyDmaapClient;
56 private static Logger log = LoggerFactory.getLogger(PolicyService.class);
57 private ObjectMapper objectMapper = new ObjectMapper();
58 private RateLimiter rateLimiter;
65 Configuration configuration = Configuration.getInstance();
66 policyDmaapClient = new PolicyDmaapClient(configuration);
67 rateLimiter = new RateLimiter(1, SERVICE_RATE_INTERVAL);
70 protected <T> OnsetMessage formPolicyOnsetMessage(String snssai, AdditionalProperties<T> addProps, Map<String, String> serviceDetails) {
71 OnsetMessage onsetmsg = new OnsetMessage();
72 Payload payload = new Payload();
73 payload.setGlobalSubscriberId(serviceDetails.get("globalSubscriberId"));
74 payload.setSubscriptionServiceType(serviceDetails.get("subscriptionServiceType"));
75 payload.setNetworkType("AN");
76 payload.setName(serviceDetails.get("ranNFNSSIId"));
77 payload.setServiceInstanceID(serviceDetails.get("ranNFNSSIId"));
79 addProps.setModifyAction("");
80 Map<String, String> nsiInfo = new HashMap<>();
81 nsiInfo.put("nsiId", UUID.randomUUID().toString());
82 nsiInfo.put("nsiName", "");
83 addProps.setNsiInfo(nsiInfo);
84 addProps.setScriptName("AN");
85 addProps.setSliceProfileId(serviceDetails.get("sliceProfileId"));
86 addProps.setModifyAction("reconfigure");
87 List<String> snssaiList = new ArrayList<>();
88 snssaiList.add(snssai);
89 addProps.setSnssaiList(snssaiList);
91 payload.setAdditionalProperties(addProps);
93 onsetmsg.setPayload(objectMapper.writeValueAsString(payload));
94 } catch (Exception e) {
95 log.error("Error while mapping payload as string , {}",e.getMessage());
98 onsetmsg.setClosedLoopControlName("ControlLoop-Slicing-116d7b00-dbeb-4d03-8719-d0a658fa735b");
99 onsetmsg.setClosedLoopAlarmStart(System.currentTimeMillis());
100 onsetmsg.setClosedLoopEventClient("microservice.sliceAnalysisMS");
101 onsetmsg.setClosedLoopEventStatus("ONSET");
102 onsetmsg.setRequestID(UUID.randomUUID().toString());
103 onsetmsg.setTarget("generic-vnf.vnf-id");
104 onsetmsg.setTargetType("VNF");
105 onsetmsg.setFrom("DCAE");
106 onsetmsg.setVersion("1.0.2");
108 aai.setVserverIsClosedLoopDisabled("false");
109 aai.setVserverProvStatus("ACTIVE");
110 aai.setGenericVnfVNFId(serviceDetails.get("ranNFNSSIId"));
111 onsetmsg.setAai(aai);
115 protected <T> void sendOnsetMessageToPolicy(String snssai, AdditionalProperties<T> addProps, Map<String, String> serviceDetails) {
116 OnsetMessage onsetMessage = formPolicyOnsetMessage(snssai, addProps, serviceDetails);
119 msg = objectMapper.writeValueAsString(onsetMessage);
120 log.info("Policy onset message for S-NSSAI: {} is {}", snssai, msg);
121 policyDmaapClient.sendNotificationToPolicy(msg);
123 catch (Exception e) {
124 log.error("Error sending notification to policy, {}",e.getMessage());
129 * Generate onsetMessage for ccvpn service update operation
130 * @param cllId cloud leased line Id (ethernet service id)
131 * @param newBw new bandwidth value for bandwidth adjustment
132 * @param <T> type for additionalPropert
133 * ies, can be omitted
134 * @return OnsetMessage result
136 public <T> OnsetMessage formPolicyOnsetMessageForCCVPN(String cllId, Integer newBw, RequestOwner owner) {
137 Sla sla = new Sla(2, newBw);
138 String transportNetworkId = cllId;
139 if (owner == RequestOwner.UUI) {
140 transportNetworkId += "-network-001";
141 } else if (owner == RequestOwner.DCAE) {
142 transportNetworkId += "-network-002";
144 TransportNetwork transportNetwork = new TransportNetwork(transportNetworkId, sla);
145 AdditionalProperties additionalProperties = new AdditionalProperties();
146 additionalProperties.setModifyAction("bandwidth");
147 additionalProperties.setEnableSdnc("true");
148 List<TransportNetwork> transportNetworks = new ArrayList();
149 transportNetworks.add(transportNetwork);
150 additionalProperties.setTransportNetworks(transportNetworks);
152 Payload payload = new Payload();
153 payload.setGlobalSubscriberId("IBNCustomer");
154 payload.setSubscriptionServiceType("IBN");
155 payload.setServiceType("CLL");
156 payload.setName("cloud-leased-line-101");
157 payload.setServiceInstanceID(cllId);
158 payload.setAdditionalProperties(additionalProperties);
159 payload.setModelInvariantUuid("6790ab0e-034f-11eb-adc1-0242ac120002");
160 payload.setModelUuid("6790ab0e-034f-11eb-adc1-0242ac120002");
162 OnsetMessage onsetmsg = new OnsetMessage();
164 onsetmsg.setPayload(objectMapper.writeValueAsString(payload));
165 } catch (Exception e) {
166 log.error("Error while mapping payload as string , {}",e.getMessage());
168 onsetmsg.setClosedLoopControlName("ControlLoop-CCVPN-CLL-227e8b00-dbeb-4d03-8719-d0a658fb846c");
169 onsetmsg.setClosedLoopAlarmStart(System.currentTimeMillis());
170 onsetmsg.setClosedLoopEventClient("microservice.sliceAnalysisMS");
171 onsetmsg.setClosedLoopEventStatus("ONSET");
172 onsetmsg.setRequestID(UUID.randomUUID().toString());
173 onsetmsg.setTarget("generic-vnf.vnf-id");
174 onsetmsg.setTargetType("VNF");
175 onsetmsg.setFrom("DCAE");
176 onsetmsg.setVersion("1.0.2");
178 aai.setGenericVnfIsClosedLoopDisabled("false");
179 aai.setGenericVnfProvStatus("ACTIVE");
180 aai.setGenericVnfVNFId("00000000-0000-0000-0000-000000000000");
181 aai.setGenericVnfVnfName("00000");
182 onsetmsg.setAai(aai);
187 * Sending the onsetMessage to Onap-Policy through PolicyDmaapClient
188 * @param onsetMessage the onsetMessage about to send
189 * @param <T> type inherent from previous implementation can be omitted
191 public <T> void sendOnsetMessageToPolicy(OnsetMessage onsetMessage){
194 msg = objectMapper.writeValueAsString(onsetMessage);
195 rateLimiter.getToken();
196 log.info("Sending onset message to Onap/Policy for ControlLoop-CCVPN-CLL, the msg: {}", msg);
197 policyDmaapClient.sendNotificationToPolicy(msg);
199 catch (Exception e) {
200 log.error("Error sending notification to policy, {}",e.getMessage());