summaryrefslogtreecommitdiffstats
path: root/utils/DmaapPublisher/src/main/java/org/openecomp/sdc/dmaap/DmaapPublisher.java
blob: fd558356ed50fdbd1199578f6caab70c997780d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package org.openecomp.sdc.dmaap;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionHandlerFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import java.util.stream.IntStream;


import static org.openecomp.sdc.dmaap.Util.*;

public class DmaapPublisher {
    private static final Logger logger = LoggerFactory.getLogger(DmaapPublisher.class);    
    private static RequestManager requestManager ;
    private static final ConcurrentLinkedDeque notificationBuffer = new ConcurrentLinkedDeque();


    private static final List<Long> registeredTasks = new CopyOnWriteArrayList<>();
    private DmaapPublisher() {}

    public static void add(String notification){
        notificationBuffer.add( notification );
    }
    public static void addAll(List<String> notifications){
        notificationBuffer.addAll( notifications );
    }
    public static void main(String[] args) {
        doPublish(args);
    }

    private static void doPublish( String[] args ) {
        CliArgs cliArgs = new CliArgs();
        CmdLineParser parser = new CmdLineParser(cliArgs);

        try {
            // parse the arguments.
            parser.parseArgument( args );
            doPublish( cliArgs );
        }
        catch(CmdLineException e) {
            logger.error("#doPublish - failed to parse arguments.", e);
            printUsage(parser, e);
            return;
        }
    }

    public static void doPublish( CliArgs cliArgs ){
        try {
            // parse the arguments.
            DmaapPublishTool tool = new DmaapPublishTool( toPath(cliArgs.getYamlPath() , cliArgs.getYamlFilename()) , cliArgs.getNotificationData()  );
            Collection<String> notifications = new ArrayList<String>( notificationBuffer );
            tool.addNotifications( notifications );
            notificationBuffer.removeAll(notifications);
            Integer concurrentRequestCount = 1;
            if ( StringUtils.isNotBlank( cliArgs.getConcurrentRequests() ) )
                concurrentRequestCount = Integer.parseInt( cliArgs.getConcurrentRequests() );
            requestManager = new RequestManager( concurrentRequestCount );

            IntStream.range(0,concurrentRequestCount).forEach( it -> {
                                        //region -  report upon finish mechanishem
                                        long ticket = System.nanoTime();
                                        registeredTasks.add( ticket );
                                        Consumer callback = ( uniqueTicket ) -> {
                                            synchronized ( registeredTasks ){
                                                registeredTasks.remove( (long)uniqueTicket );
                                                registeredTasks.notifyAll();
                                            }};

                                        RunnableReporter task = new RunnableReporter( ticket , tool , cliArgs , callback );
                                        requestManager.getExecutor().execute( task ) ;
            });
        }
        catch(NumberFormatException e) {
            logger.error("#doPublish - failed to parse argument CR.", e);
            return;
        }
        catch(Exception e) {
            logger.error("#doPublish - failed to publish.", e);
        }
    }

    public static class RunnableReporter implements Runnable{

            final private long ticket ;
            final private DmaapPublishTool tool;
            final private CliArgs cliArgs;
            final Consumer reporter;

            public RunnableReporter(final long ticket , final DmaapPublishTool tool , final CliArgs args ,  Consumer reporter){
                this.ticket = ticket ;
                this.tool = tool ;
                this.cliArgs = args ;
                this.reporter = reporter;
            }
            @Override
            public void run() {
                try {
                    tool.publish( cliArgs.getYamlPath() );
                    reporter.accept(ticket);
                }catch(IOException e){
                    logger.error("#doPublish - failed to publish.", e);
                }catch(InterruptedException e){
                    logger.error("#doPublish - cannot complete publish, thread interuppted.", e);
                    Thread.currentThread().interrupt();
                }
            }
    }


    public static List<Long> getRegisteredTasks() {
        return registeredTasks;
    }

    public static void preparePublish( String path,  String filename , String concurrentRequests ){

            CliArgs cliArgs = new CliArgs();
            if ( StringUtils.isNotBlank( filename ) )
                cliArgs.setYamlFilename( filename );
            if ( StringUtils.isNotBlank( path ) )
                cliArgs.setYamlPath( path );
            if ( NumberUtils.isCreatable( concurrentRequests ) )
                cliArgs.setConcurrentRequests(  concurrentRequests );

            doPublish( cliArgs );

    }


    private static void printUsage(CmdLineParser parser, CmdLineException e) {
        System.err.println( e.getMessage() );
        System.err.println("java DmaapPublisher [options...] arguments...");
        // print the list of available options
        parser.printUsage(System.err);
        System.err.println();
        // print option sample. This is useful some time
        System.err.println("  Example: java DmaapPublisher " + parser.printExample(OptionHandlerFilter.ALL));
        
    }
}