aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/devicemanager-core/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/devicemanager/test/TestDcae.java
blob: a403fe4a62eaa298ecd951e20dd6df84ed668cc2 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk feature sdnr wt
 *  ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.ccsdk.features.sdnr.wt.devicemanager.test;

import static org.junit.Assert.fail;
import com.google.common.io.Files;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.DcaeProviderClient;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalDateAndTime;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalSeverity;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;

@SuppressWarnings("restriction")
public class TestDcae {

    private static final String ENABLEDDCAE_TESTCONFIG_FILENAME = "test2.properties";
    private static final File ENABLEDDCAE_TESTCONFIG_FILE = new File(ENABLEDDCAE_TESTCONFIG_FILENAME);
    private static final int DCAE_SERVER_PORT = 45451;
    private static final String URI = "/abc";
    private static final String TESTCONFIG_CONTENT = "[dcae]\n" + "dcaeUserCredentials=admin:admin\n"
            + "dcaeUrl=http://localhost:" + DCAE_SERVER_PORT + URI + "\n" + "dcaeHeartbeatPeriodSeconds=120\n"
            + "dcaeTestCollector=no\n" + "\n" + "[aots]\n" + "userPassword=passwd\n" + "soapurladd=off\n"
            + "soapaddtimeout=10\n" + "soapinqtimeout=20\n" + "userName=user\n" + "inqtemplate=inqreq.tmpl.xml\n"
            + "assignedto=userid\n" + "addtemplate=addreq.tmpl.xml\n"
            + "severitypassthrough=critical,major,minor,warning\n" + "systemuser=user\n" + "prt-offset=1200\n"
            + "soapurlinq=off\n" + "#smtpHost=\n" + "#smtpPort=\n" + "#smtpUsername=\n" + "#smtpPassword=\n"
            + "#smtpSender=\n" + "#smtpReceivers=\n" + "\n" + "[es]\n" + "esCluster=sendateodl5\n" + "\n" + "[aai]\n"
            + "#keep comment\n" + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=off\n"
            + "aaiUserCredentials=AAI:AAI\n" + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n"
            + "aaiApiVersion=aai/v13\n" + "aaiPropertiesFile=aaiclient.properties\n" + "aaiApplicationId=SDNR\n"
            + "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n"
            + "aaiPcks12ClientCertPassphrase=adminadmin\n" + "aaiClientConnectionTimeout=30000\n" + "\n" + "[pm]\n"
            + "pmCluster=sendateodl5\n" + "pmEnabled=true\n" + "\n" + "";
    private HttpServer server;
    private ExecutorService httpThreadPool;
    private ConfigurationFileRepresentation cfg;

    @Test
    public void test2() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        DcaeProviderClient provider = new DcaeProviderClient(cfg, "mountpoint", null);

        String mountPointName = "testDevice 01";
        String type = "Unit";
        String model = "Horizon Compact+";
        String vendor = "DragonWave-X";
        String ipv4 = "127.0.0.1";
        String ipv6 = "::1";
        List<String> ifInfos = new ArrayList<>();
        ifInfos.add("LP-MWPS-RADIO");
        new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
        System.out.println("registering device");
        boolean neDeviceAlarm = false;
        ProblemNotificationXml notification = new ProblemNotificationXml(mountPointName, "network-element",
                "problemName", InternalSeverity.Critical, 123, InternalDateAndTime.getTestpattern());
        provider.sendProblemNotification(new NodeId(mountPointName), notification, neDeviceAlarm);

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            provider.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Before
    public void initDcaeTestWebserver() throws IOException {
        try {
            Files.asCharSink(ENABLEDDCAE_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
        } catch (IOException e1) {
            fail(e1.getMessage());
        }
        cfg = new ConfigurationFileRepresentation(ENABLEDDCAE_TESTCONFIG_FILENAME);
        // cfg.reload(ENABLEDDCAE_TESTCONFIG_FILENAME);
        /*
         * cfg = new HtDevicemanagerConfiguration(ENABLEDDCAE_TESTCONFIG_FILENAME);
         * DcaeConfig.reload();
         */
        try {
            this.server = HttpServer.create(new InetSocketAddress(DCAE_SERVER_PORT), 0);
        } catch (Exception e) {
            fail(e.getMessage());
        }
        this.httpThreadPool = Executors.newFixedThreadPool(5);
        this.server.setExecutor(this.httpThreadPool);
        this.server.createContext(URI, new MyHandler());
        // server.createContext("/", new MyRootHandler());
        this.server.setExecutor(null); // creates a default executor
        this.server.start();
        System.out.println("http server started");
    }

    @After
    public void stopTestWebserver() {
        if (this.server != null) {
            this.server.stop(0);
            this.httpThreadPool.shutdownNow();
            System.out.println("http server stopped");
        }
        if (ENABLEDDCAE_TESTCONFIG_FILE.exists()) {
            ENABLEDDCAE_TESTCONFIG_FILE.delete();
        }
    }

    static class MyHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange t) throws IOException {
            String method = t.getRequestMethod();
            System.out.println("req method: " + method);
            OutputStream os = null;
            try {
                String res = "";
                if (method.equals("POST")) {
                    t.sendResponseHeaders(200, res.length());
                    os = t.getResponseBody();
                    os.write(res.getBytes());
                } else {
                    t.sendResponseHeaders(404, 0);
                }
                System.out.println("req handled successful");

            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                if (os != null) {
                    os.close();
                }
            }
        }
    }
}