Fix modernization issues
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / YnlListener.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.clustering.it.provider.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.concurrent.atomic.AtomicLong;
13 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeYnlOutput;
14 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.UnsubscribeYnlOutputBuilder;
15 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.target.rev170215.IdSequence;
16 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.target.rev170215.OdlMdsalLowlevelTargetListener;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class YnlListener implements OdlMdsalLowlevelTargetListener {
21     private static final Logger LOG = LoggerFactory.getLogger(YnlListener.class);
22
23     private final String id;
24
25     private final AtomicLong localNumber = new AtomicLong();
26     private final AtomicLong allNot = new AtomicLong();
27     private final AtomicLong idNot = new AtomicLong();
28     private final AtomicLong errNot = new AtomicLong();
29
30     public YnlListener(final String id) {
31         this.id = requireNonNull(id);
32     }
33
34     @Override
35     public void onIdSequence(final IdSequence notification) {
36         LOG.debug("Received id-sequence notification, : {}", notification);
37
38         allNot.incrementAndGet();
39
40         if (notification.getId().equals(id)) {
41             idNot.incrementAndGet();
42
43             localNumber.getAndUpdate(value -> {
44                 if (notification.getSequenceNumber() - value == 1) {
45                     return value + 1;
46                 }
47                 errNot.getAndIncrement();
48                 return value;
49             });
50         }
51     }
52
53     public UnsubscribeYnlOutput getOutput() {
54         return new UnsubscribeYnlOutputBuilder()
55                 .setAllNot(allNot.get())
56                 .setErrNot(errNot.get())
57                 .setIdNot(idNot.get())
58                 .setLocalNumber(localNumber.get())
59                 .build();
60     }
61 }