0f28d7c54062478ccdf5e6dc1a3ddefe83033ea0
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * BBS-RELOCATION-CPE-AUTHENTICATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA 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.bbs.event.processor.pipelines;
22
23 import static org.onap.bbs.event.processor.config.ApplicationConstants.CONSUME_REREGISTRATION_TASK_NAME;
24 import static org.onap.bbs.event.processor.config.ApplicationConstants.DCAE_BBS_EVENT_PROCESSOR_MS_INSTANCE;
25 import static org.onap.bbs.event.processor.config.ApplicationConstants.RETRIEVE_HSI_CFS_SERVICE_INSTANCE_TASK_NAME;
26 import static org.onap.bbs.event.processor.config.ApplicationConstants.RETRIEVE_PNF_TASK_NAME;
27 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.INSTANCE_UUID;
28 import static org.onap.dcaegen2.services.sdk.rest.services.model.logging.MdcVariables.RESPONSE_CODE;
29
30 import java.time.Duration;
31 import java.time.Instant;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.UUID;
36 import java.util.concurrent.TimeoutException;
37
38 import javax.net.ssl.SSLException;
39
40 import org.onap.bbs.event.processor.config.ApplicationConfiguration;
41 import org.onap.bbs.event.processor.exceptions.EmptyDmaapResponseException;
42 import org.onap.bbs.event.processor.model.ControlLoopPublisherDmaapModel;
43 import org.onap.bbs.event.processor.model.ImmutableControlLoopPublisherDmaapModel;
44 import org.onap.bbs.event.processor.model.PnfAaiObject;
45 import org.onap.bbs.event.processor.model.ReRegistrationConsumerDmaapModel;
46 import org.onap.bbs.event.processor.model.RelationshipListAaiObject;
47 import org.onap.bbs.event.processor.model.ServiceInstanceAaiObject;
48 import org.onap.bbs.event.processor.tasks.AaiClientTask;
49 import org.onap.bbs.event.processor.tasks.DmaapPublisherTask;
50 import org.onap.bbs.event.processor.tasks.DmaapReRegistrationConsumerTask;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.slf4j.MDC;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.http.ResponseEntity;
56 import org.springframework.stereotype.Component;
57 import org.springframework.util.StringUtils;
58
59 import reactor.core.publisher.Flux;
60 import reactor.core.publisher.Mono;
61
62 @Component
63 public class ReRegistrationPipeline {
64
65     private static final Logger LOGGER = LoggerFactory.getLogger(ReRegistrationPipeline.class);
66
67     private static final String POLICY_NAME = "Nomadic_ONT";
68
69     private DmaapReRegistrationConsumerTask consumerTask;
70     private DmaapPublisherTask publisherTask;
71     private AaiClientTask aaiClientTask;
72
73     private ApplicationConfiguration configuration;
74
75     private Map<String, String> mdcContextMap;
76
77     @Autowired
78     ReRegistrationPipeline(ApplicationConfiguration configuration,
79                            DmaapReRegistrationConsumerTask consumerTask,
80                            DmaapPublisherTask publisherTask,
81                            AaiClientTask aaiClientTask,
82                            Map<String, String> mdcContextMap) {
83         this.configuration = configuration;
84         this.consumerTask = consumerTask;
85         this.publisherTask = publisherTask;
86         this.aaiClientTask = aaiClientTask;
87         this.mdcContextMap = mdcContextMap;
88     }
89
90     /**
91      * PNF re-registration processing pipeline for BBS uS.
92      */
93     public void processPnfReRegistrationEvents() {
94         MDC.setContextMap(mdcContextMap);
95         LOGGER.info("Process next Re-Registration events");
96         executePipeline()
97                 .subscribe(this::onSuccess, this::onError, this::onComplete);
98         LOGGER.trace("Reactive PNF Re-registration pipeline subscribed - Execution started");
99     }
100
101     Flux<ResponseEntity<String>> executePipeline() {
102         return
103             // Consume Re-Registration from DMaaP
104             consumeReRegistrationsFromDmaap()
105             // Fetch PNF from A&AI
106             .flatMap(this::fetchPnfFromAai)
107             // Fetch related HSI CFS instance from A&AI
108             .flatMap(this::fetchHsiCfsServiceInstanceFromAai)
109             // Trigger Policy for relocation
110             .flatMap(this::triggerPolicy);
111     }
112
113     private void onSuccess(ResponseEntity<String> responseCode) {
114         MDC.put(RESPONSE_CODE, responseCode.getStatusCode().toString());
115         LOGGER.info("PNF Re-Registration event successfully handled. "
116                         + "Publishing to DMaaP for Policy returned a status code of ({} {})",
117                 responseCode.getStatusCode().value(), responseCode.getStatusCode().getReasonPhrase());
118         MDC.remove(RESPONSE_CODE);
119     }
120
121     private void onError(Throwable throwable) {
122         LOGGER.error("Aborted PNF Re-Registration events processing. Error: {}", throwable.getMessage());
123     }
124
125     private void onComplete() {
126         LOGGER.info("PNF Re-Registration processing pipeline has been completed");
127     }
128
129     private Flux<PipelineState> consumeReRegistrationsFromDmaap() {
130         return Flux.defer(() -> {
131             MDC.put(INSTANCE_UUID, UUID.randomUUID().toString());
132             try {
133                 return consumerTask.execute(CONSUME_REREGISTRATION_TASK_NAME)
134                         .timeout(Duration.ofSeconds(configuration.getPipelinesTimeoutInSeconds()))
135                         .doOnError(e -> {
136                             if (e instanceof TimeoutException) {
137                                 LOGGER.warn("Timed out waiting for DMaaP response");
138                             } else if (e instanceof EmptyDmaapResponseException) {
139                                 LOGGER.info("Nothing to consume from DMaaP");
140                             } else {
141                                 LOGGER.error("DMaaP Consumer error: {}", e.getMessage());
142                             }
143                         })
144                         .onErrorResume(
145                             e -> e instanceof Exception,
146                             e -> Mono.empty())
147                         .map(event -> {
148                             // For each message, we have to keep separate state. This state will be enhanced
149                             // in each step and handed off to the next processing step
150                             PipelineState state = new PipelineState();
151                             state.setReRegistrationEvent(event);
152                             return state;
153                         });
154             } catch (SSLException e) {
155                 return Flux.error(e);
156             }
157         });
158     }
159
160     private Mono<PipelineState> fetchPnfFromAai(PipelineState state) {
161
162         ReRegistrationConsumerDmaapModel vesEvent = state.getReRegistrationEvent();
163         String pnfName = vesEvent.getCorrelationId();
164         String url = String.format("/aai/v14/network/pnfs/pnf/%s?depth=all", pnfName);
165         LOGGER.debug("Processing Step: Retrieve PNF. Url: ({})", url);
166
167         return aaiClientTask.executePnfRetrieval(RETRIEVE_PNF_TASK_NAME, url)
168                 .timeout(Duration.ofSeconds(configuration.getPipelinesTimeoutInSeconds()))
169                 .doOnError(TimeoutException.class,
170                         e -> LOGGER.warn("Timed out waiting for A&AI response")
171                 )
172                 .doOnError(e -> LOGGER.error("Error while retrieving PNF: {}",
173                         e.getMessage())
174                 )
175                 .onErrorResume(
176                     e -> e instanceof Exception,
177                     e -> Mono.empty())
178                 .map(p -> {
179                     state.setPnfAaiObject(p);
180                     return state;
181                 });
182     }
183
184     private Mono<PipelineState> fetchHsiCfsServiceInstanceFromAai(PipelineState state) {
185
186         if (state == null || state.getPnfAaiObject() == null) {
187             return Mono.empty();
188         }
189
190         // At this point, we have both the VES-event of the re-registration and the PNF object retrieved from A&AI
191         // We can check if this processing needs to continue in case of a true relocation
192         if (isNotReallyAnOntRelocation(state)) {
193             return Mono.empty();
194         }
195
196         PnfAaiObject pnf = state.getPnfAaiObject();
197         // Assuming that the PNF will only have a single service-instance relationship pointing
198         // towards the HSI CFS service
199         String serviceInstanceId = pnf.getRelationshipListAaiObject().getRelationshipEntries()
200                 .stream()
201                 .filter(e -> "service-instance".equals(e.getRelatedTo()))
202                 .flatMap(e -> e.getRelationshipData().stream())
203                 .filter(d -> "service-instance.service-instance-id".equals(d.getRelationshipKey()))
204                 .map(RelationshipListAaiObject.RelationshipDataEntryAaiObject::getRelationshipValue)
205                 .findFirst().orElse("");
206
207         if (StringUtils.isEmpty(serviceInstanceId)) {
208             LOGGER.error("Unable to retrieve HSI CFS service instance from PNF {}",
209                     state.getPnfAaiObject().getPnfName());
210             return Mono.empty();
211         }
212
213         String url = String.format("/aai/v14/nodes/service-instances/service-instance/%s?depth=all",
214                 serviceInstanceId);
215         LOGGER.debug("Processing Step: Retrieve HSI CFS Service. Url: ({})", url);
216         return aaiClientTask.executeServiceInstanceRetrieval(RETRIEVE_HSI_CFS_SERVICE_INSTANCE_TASK_NAME, url)
217                 .timeout(Duration.ofSeconds(configuration.getPipelinesTimeoutInSeconds()))
218                 .doOnError(TimeoutException.class,
219                         e -> LOGGER.warn("Timed out waiting for A&AI response")
220                 )
221                 .doOnError(e -> LOGGER.error("Error while retrieving HSI CFS Service instance: {}",
222                         e.getMessage())
223                 )
224                 .onErrorResume(
225                     e -> e instanceof Exception,
226                     e -> Mono.empty())
227                 .map(s -> {
228                     state.setHsiCfsServiceInstance(s);
229                     return state;
230                 });
231     }
232
233     private boolean isNotReallyAnOntRelocation(PipelineState state) {
234         List<RelationshipListAaiObject.RelationshipEntryAaiObject> relationshipEntries =
235                 state.getPnfAaiObject().getRelationshipListAaiObject().getRelationshipEntries();
236
237         // If no logical-link, fail further processing
238         if (relationshipEntries.stream().noneMatch(e -> "logical-link".equals(e.getRelatedTo()))) {
239             LOGGER.warn("PNF {} does not have any logical-links bridged. Stop further processing",
240                     state.getPnfAaiObject().getPnfName());
241             return true;
242         }
243
244         // Assuming PNF will only have one logical-link per BBS use case design
245         boolean isNotRelocation = relationshipEntries
246                 .stream()
247                 .filter(e -> "logical-link".equals(e.getRelatedTo()))
248                 .flatMap(e -> e.getRelationshipData().stream())
249                 .anyMatch(d -> d.getRelationshipValue()
250                         .equals(state.getReRegistrationEvent().getAttachmentPoint()));
251
252
253         if (isNotRelocation) {
254             LOGGER.warn("Not a Relocation for PNF {} with attachment point {}",
255                     state.getPnfAaiObject().getPnfName(),
256                     state.getReRegistrationEvent().getAttachmentPoint());
257         }
258         return isNotRelocation;
259     }
260
261     private Mono<ResponseEntity<String>> triggerPolicy(PipelineState state) {
262
263         if (state == null || state.getHsiCfsServiceInstance() == null) {
264             return Mono.empty();
265         }
266
267         ControlLoopPublisherDmaapModel event = buildTriggeringPolicyEvent(state);
268         return publisherTask.execute(event)
269                 .timeout(Duration.ofSeconds(configuration.getPipelinesTimeoutInSeconds()))
270                 .doOnError(TimeoutException.class,
271                         e -> LOGGER.warn("Timed out waiting for DMaaP confirmation")
272                 )
273                 .doOnError(e -> LOGGER.error("Error while triggering Policy: {}", e.getMessage()))
274                 .onErrorResume(
275                     e -> e instanceof Exception,
276                     e -> Mono.empty());
277     }
278
279     private ControlLoopPublisherDmaapModel buildTriggeringPolicyEvent(PipelineState state) {
280
281         String cfsServiceInstanceId = state.getHsiCfsServiceInstance().getServiceInstanceId();
282
283         String attachmentPoint = state.getReRegistrationEvent().getAttachmentPoint();
284         String remoteId = state.getReRegistrationEvent().getRemoteId();
285         String cvlan = state.getReRegistrationEvent().getCVlan();
286         String svlan = state.getReRegistrationEvent().getSVlan();
287
288         Map<String, String> enrichmentData = new HashMap<>();
289         enrichmentData.put("service-information.hsia-cfs-service-instance-id", cfsServiceInstanceId);
290
291         enrichmentData.put("attachmentPoint", attachmentPoint);
292         enrichmentData.put("remoteId", remoteId);
293         enrichmentData.put("cvlan", cvlan);
294         enrichmentData.put("svlan", svlan);
295
296         ControlLoopPublisherDmaapModel triggerEvent = ImmutableControlLoopPublisherDmaapModel.builder()
297                 .closedLoopEventClient(DCAE_BBS_EVENT_PROCESSOR_MS_INSTANCE)
298                 .policyVersion(configuration.getPolicyVersion())
299                 .policyName(POLICY_NAME)
300                 .policyScope(configuration.getReRegistrationCloseLoopPolicyScope())
301                 .targetType(configuration.getCloseLoopTargetType())
302                 .aaiEnrichmentData(enrichmentData)
303                 .closedLoopAlarmStart(Instant.now().getEpochSecond())
304                 .closedLoopEventStatus(configuration.getCloseLoopEventStatus())
305                 .closedLoopControlName(configuration.getReRegistrationCloseLoopControlName())
306                 .version(configuration.getCloseLoopVersion())
307                 .target(configuration.getCloseLoopTarget())
308                 .requestId(UUID.randomUUID().toString())
309                 .originator(configuration.getCloseLoopOriginator())
310                 .build();
311         LOGGER.debug("Processing Step: Publish for Policy");
312         LOGGER.trace("Trigger Policy event: ({})",triggerEvent);
313         return triggerEvent;
314     }
315
316     private static class PipelineState {
317
318         private ReRegistrationConsumerDmaapModel reRegistrationEvent;
319         private PnfAaiObject pnfAaiObject;
320         private ServiceInstanceAaiObject hsiCfsServiceInstance;
321
322         ReRegistrationConsumerDmaapModel getReRegistrationEvent() {
323             return reRegistrationEvent;
324         }
325
326         void setReRegistrationEvent(ReRegistrationConsumerDmaapModel reRegistrationEvent) {
327             this.reRegistrationEvent = reRegistrationEvent;
328         }
329
330         PnfAaiObject getPnfAaiObject() {
331             return pnfAaiObject;
332         }
333
334         void setPnfAaiObject(PnfAaiObject pnfAaiObject) {
335             this.pnfAaiObject = pnfAaiObject;
336         }
337
338         ServiceInstanceAaiObject getHsiCfsServiceInstance() {
339             return hsiCfsServiceInstance;
340         }
341
342         void setHsiCfsServiceInstance(ServiceInstanceAaiObject hsiCfsServiceInstance) {
343             this.hsiCfsServiceInstance = hsiCfsServiceInstance;
344         }
345     }
346 }