aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/northbound/addCMHandle/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProvider.java
blob: 1c4cb98b483c720f71efc8b2df27c5b40e6505e7 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*-
 * ============LICENSE_START=======================================================
 * ONAP : CCSDK
 * ================================================================================
 * Copyright (C) 2021-2022 Wipro Limited.
 * ================================================================================
 * 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.northbound.addCMHandle;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.util.concurrent.ListenableFuture;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import org.eclipse.jdt.annotation.NonNull;
import org.onap.ccsdk.features.sdnr.northbound.addCMHandle.HttpRequester;
import org.onap.ccsdk.features.sdnr.northbound.addCMHandle.models.CpsCmHandleRequestBody;
import org.onap.ccsdk.sli.core.utils.common.EnvProperties;
import org.opendaylight.mdsal.binding.api.DataBroker;
import org.opendaylight.mdsal.binding.api.DataObjectModification;
import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
import org.opendaylight.mdsal.binding.api.DataTreeModification;
import org.opendaylight.mdsal.binding.api.MountPointService;
import org.opendaylight.mdsal.binding.api.NotificationPublishService;
import org.opendaylight.mdsal.binding.api.RpcProviderService;
import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.dom.api.DOMMountPointService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev210615.AddCMHandleInput;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev210615.AddCMHandleOutput;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev210615.AddCMHandleOutputBuilder;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev210615.CMHandleAPIService;
import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev210615.status.StatusBuilder;
import org.opendaylight.yang.gen.v1.org.onap.cps.ncmp.rev210520.DmiRegistryBuilder;
import org.opendaylight.yang.gen.v1.org.onap.cps.ncmp.rev210520.dmi.registry.CmHandle;
import org.opendaylight.yang.gen.v1.org.onap.cps.ncmp.rev210520.dmi.registry.CmHandleBuilder;
import org.opendaylight.yang.gen.v1.org.onap.cps.ncmp.rev210520.dmi.registry.CmHandleKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.common.RpcResult;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AddCMHandleProvider implements CMHandleAPIService, AutoCloseable {

    private static final Logger LOG = LoggerFactory.getLogger(AddCMHandleProvider.class);
    private final ObjectMapper objMapper = new ObjectMapper();
    private final String APPLICATION_NAME = "addCMHandle";
    private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
    private static final String PROPERTIES_FILE_NAME = "cm-handle.properties";
    private static HashMap<String, String> config;
    private ListenerRegistration<AddCmHandleListener> listener;
    private static final @NonNull InstanceIdentifier<Node> NETCONF_NODE_TOPO_IID =
            InstanceIdentifier.create(NetworkTopology.class)
                    .child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
                    .child(Node.class);
    private static final @NonNull DataTreeIdentifier<Node> NETCONF_NODE_TOPO_TREE_ID =
            DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, NETCONF_NODE_TOPO_IID);

    private DataBroker dataBroker;
    private MountPointService mountPointService;
    private DOMMountPointService domMountPointService;
    private RpcProviderService rpcProviderRegistry;
    @SuppressWarnings("unused")
    private NotificationPublishService notificationPublishService;
    @SuppressWarnings("unused")
    private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
    private YangParserFactory yangParserFactory;
    private BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer;
    private Boolean isInitializationSuccessful = false;
    private Long lastNotificationSentOn = Long.valueOf(0);
    private List<String> nodeIdList = new ArrayList<>();

    public AddCMHandleProvider() {

        LOG.info("Creating provider for {}", APPLICATION_NAME);
        this.dataBroker = null;
        this.mountPointService = null;
        this.domMountPointService = null;
        this.rpcProviderRegistry = null;
        this.notificationPublishService = null;
        this.clusterSingletonServiceProvider = null;
        this.yangParserFactory = null;
        this.bindingNormalizedNodeSerializer = null;

    }

    public void setDataBroker(DataBroker dataBroker) {
        this.dataBroker = dataBroker;
    }

    public void setRpcProviderRegistry(RpcProviderService rpcProviderRegistry) {
        this.rpcProviderRegistry = rpcProviderRegistry;
    }

    public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
        this.notificationPublishService = notificationPublishService;
    }

    public void setMountPointService(MountPointService mountPointService) {
        this.mountPointService = mountPointService;
    }

    public void setDomMountPointService(DOMMountPointService domMountPointService) {
        this.domMountPointService = domMountPointService;
    }

    public void setClusterSingletonService(ClusterSingletonServiceProvider clusterSingletonService) {
        this.clusterSingletonServiceProvider = clusterSingletonService;
    }

    public void setYangParserFactory(YangParserFactory yangParserFactory) {
        this.yangParserFactory = yangParserFactory;
    }

    public void setBindingNormalizedNodeSerializer(BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer) {
        this.bindingNormalizedNodeSerializer = bindingNormalizedNodeSerializer;
        LOG.info("Init bindingNormalizedNodeSerializer");
    }

    public Boolean isInitializationSuccessful() {
        return isInitializationSuccessful;
    }

    public void init() {
        LOG.info("Initializing {} for {}", this.getClass().getName(), APPLICATION_NAME);

        String propDir = System.getenv(SDNC_CONFIG_DIR);
        if (propDir == null) {
            LOG.error("Environment variable SDNC_CONFIG_DIR is not set");
            propDir = "/opt/onap/ccsdk/data/properties/";
        } else if (!propDir.endsWith("/")) {
            propDir = propDir + "/";
        }

        config = new HashMap<String, String>();

        try (FileInputStream fileInput = new FileInputStream(propDir + PROPERTIES_FILE_NAME)) {
            EnvProperties properties = new EnvProperties();
            properties.load(fileInput);

            for (String param : new String[] {"cpsUrl", "user", "password", "dmaapUrl", "dmiServiceName", "client",
                    "timerThreshold"}) {
                config.put(param, properties.getProperty(param));
            }
        } catch (IOException e) {
            LOG.error("Error while reading properties file: ", e);
        }

        listener = dataBroker.registerDataTreeChangeListener(NETCONF_NODE_TOPO_TREE_ID, new AddCmHandleListener());
        isInitializationSuccessful = true;
        LOG.info("Initialization complete for {}", APPLICATION_NAME);
        LOG.info("addCMHandle Session Initiated");
    }

    /**
     * AddCmHandleListener
     */
    private class AddCmHandleListener implements DataTreeChangeListener<Node> {

        @Override
        public void onDataTreeChanged(@NonNull Collection<DataTreeModification<Node>> changes) {
            LOG.info("AddCmHandleListener TreeChange enter changes: {}", changes.size());
            LOG.info("config: " + config);
            String nodeId = getNodeId(changes);
            if (Objects.nonNull(nodeId) && !(nodeIdList.contains(nodeId))) {
                nodeIdList.add(nodeId);
            }
            Timestamp currentTime = new Timestamp(System.currentTimeMillis());
            Long difference = currentTime.getTime() - lastNotificationSentOn;
            if (difference > Long.valueOf(config.get("timerThreshold"))) {
                sendNotification(nodeIdList);
                nodeIdList.clear();
            }

        }
    }

    /**
     * Method to get nodeId.
     */
    private String getNodeId(@NonNull Collection<DataTreeModification<Node>> changes) {
        String nodeIdString = null;
        for (final DataTreeModification<Node> change : changes) {

            final DataObjectModification<Node> root = change.getRootNode();
            try {
                ModificationType modificationTyp = root.getModificationType();
                if ((modificationTyp != ModificationType.DELETE)) {

                    Node node = root.getDataAfter();
                    NodeId nodeId = node != null ? node.getNodeId() : null;
                    if (nodeId == null) {
                        LOG.info("without nodeid");
                    } else {
                        nodeIdString = nodeId.getValue();
                        LOG.info("AddCmHandle for nodeId: {}", nodeIdString);
                    }
                }

            } catch (NullPointerException | IllegalStateException e) {
                LOG.info("Data not available at ", e);
            }
        }

        return nodeIdString;
    }

    /**
     * Method called when cm-handle notification is to be sent.
     */
    protected void sendNotification(List<String> nodeIdList) {

        String sendNotificationTo = config.get("client");
        lastNotificationSentOn = new Timestamp(System.currentTimeMillis()).getTime();
        if (sendNotificationTo.equalsIgnoreCase("CPS")) {
            sendNotificationToCps(nodeIdList);

        }
        if (sendNotificationTo.equalsIgnoreCase("DMAAP")) {
            sendNotificationToDmaap(nodeIdList);
        }

        else {
            sendNotificationToCps(nodeIdList);
            sendNotificationToDmaap(nodeIdList);
        }
        lastNotificationSentOn = new Timestamp(System.currentTimeMillis()).getTime();

    }

    /**
     * Method called when cm-handle notification is to be sent to CPS.
     */
    protected String sendNotificationToCps(List<String> nodeIdList) {

        LOG.info("Sending Notification to CPS");
        String userCredential = config.get("user") + ":" + config.get("password");
        String url = config.get("cpsUrl");
        String requestBody = null;
        CpsCmHandleRequestBody cpsCmHandleRequestBody = new CpsCmHandleRequestBody(nodeIdList);
        LOG.info("url {}", url);
        LOG.info("userCredential: {}", userCredential);
        try {
            requestBody = objMapper.writeValueAsString(cpsCmHandleRequestBody);
            LOG.info("requestBody{} ", requestBody);
        } catch (JsonProcessingException e) {
            LOG.error("ERROR: {}", e);
        }
        String response = HttpRequester.sendPostRequest(url, userCredential, requestBody);
        LOG.info("response from CPS: {} ", response);
        return response;

    }

    /**
     * Method called when cm-handle notification is to be sent to Dmaap.
     */
    protected String sendNotificationToDmaap(List<String> nodeIdList) {

        LOG.info("Sending Notification to Dmaap");
        String url = config.get("dmaapUrl");
        Map<CmHandleKey, CmHandle> values = new HashMap<>();
        nodeIdList.forEach(nodeId -> {
            CmHandleBuilder cmHandleBuilder = new CmHandleBuilder();
            cmHandleBuilder.setDmiServiceName(config.get("dmiServiceName"));
            cmHandleBuilder.setId(nodeId);
            CmHandleKey cmHandleKey = new CmHandleKey(nodeId);
            values.put(cmHandleKey, cmHandleBuilder.build());
        });
        DmiRegistryBuilder dmiRegistryBuilder = new DmiRegistryBuilder();
        dmiRegistryBuilder.setCmHandle(values);

        String requestBody = null;
        LOG.info("url: {}", url);
        try {
            requestBody = objMapper.writeValueAsString(dmiRegistryBuilder.build());
            LOG.info("requestBody: {}", requestBody);
        } catch (JsonProcessingException e) {
            LOG.error("ERROR: {}", e);
        }
        String response = HttpRequester.sendPostRequest(url, null, requestBody);
        LOG.info("response from Dmaap: {}", response);
        return response;

    }

    /**
     * Method called when the blueprint container is destroyed.
     */
    @Override
    public void close() {
        if (Objects.nonNull(listener)) {
            listener.close();
        }
        LOG.debug("AddCMHandleProvider Closed");
    }

    @Override
    public ListenableFuture<RpcResult<AddCMHandleOutput>> addCMHandle(AddCMHandleInput input) {
        StatusBuilder statusBuilder = new StatusBuilder();
        statusBuilder.setMessage("SUCCESS");
        return RpcResultBuilder.success(new AddCMHandleOutputBuilder().setStatus(statusBuilder.build()).build())
                .buildFuture();

    }
}