2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 Wipro Limited.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.features.sdnr.northbound.addCMHandle;
23 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
25 import com.google.common.base.Preconditions;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.sun.jersey.api.client.Client;
28 import com.sun.jersey.api.client.ClientResponse;
29 import com.sun.jersey.api.client.WebResource;
30 import com.sun.jersey.api.client.config.ClientConfig;
31 import com.sun.jersey.api.client.config.DefaultClientConfig;
32 import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
34 import java.io.FileInputStream;
35 import java.io.FileNotFoundException;
36 import java.io.IOException;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.Properties;
41 import java.util.concurrent.ExecutorService;
42 import java.util.concurrent.Executors;
43 import java.util.concurrent.Future;
45 import javax.ws.rs.HttpMethod;
46 import javax.ws.rs.core.MediaType;
48 import org.eclipse.jdt.annotation.NonNull;
49 import org.json.JSONObject;
50 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
51 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener;
52 import org.onap.ccsdk.sli.core.sli.provider.MdsalHelper;
53 import org.opendaylight.mdsal.binding.api.DataBroker;
54 import org.opendaylight.mdsal.binding.api.DataObjectModification;
55 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
56 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
57 import org.opendaylight.mdsal.binding.api.DataTreeModification;
58 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
59 import org.opendaylight.mdsal.binding.api.RpcProviderService;
60 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
61 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev210615.AddCMHandleInput;
62 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev210615.AddCMHandleOutput;
63 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.rev210615.CMHandleAPIService;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
73 import org.opendaylight.yangtools.concepts.ListenerRegistration;
74 import org.opendaylight.yangtools.concepts.ObjectRegistration;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.opendaylight.yangtools.yang.common.RpcResult;
77 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
81 public class AddCMHandleProvider implements CMHandleAPIService, NetconfNodeStateListener, AutoCloseable {
83 private static final Logger LOG = LoggerFactory.getLogger(AddCMHandleProvider.class);
84 private final String APPLICATION_NAME = "addCMHandle";
85 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
86 private static final String PROPERTIES_FILE_NAME = "cm-handle.properties";
87 private static final String PARSING_ERROR =
88 "Could not create the request message to send to the server; no message will be sent";
89 private final ExecutorService executor;
90 protected DataBroker dataBroker;
91 protected DOMDataBroker domDataBroker;
92 protected NotificationPublishService notificationService;
93 protected RpcProviderService rpcProviderRegistry;
94 private ObjectRegistration<CMHandleAPIService> rpcRegistration;
95 public static final InstanceIdentifier<Topology> NETCONF_TOPO_IID = InstanceIdentifier.create(NetworkTopology.class)
96 .child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
97 private static HashMap<String, String> config;
99 public AddCMHandleProvider() {
101 LOG.info("Creating provider for {}", APPLICATION_NAME);
102 executor = Executors.newFixedThreadPool(1);
103 this.dataBroker = null;
104 this.domDataBroker = null;
105 this.notificationService = null;
106 this.rpcProviderRegistry = null;
107 this.rpcRegistration = null;
110 public void setDataBroker(DataBroker dataBroker) {
111 this.dataBroker = dataBroker;
114 public void setDomDataBroker(DOMDataBroker domDataBroker) {
115 this.domDataBroker = domDataBroker;
118 public void setRpcProviderRegistry(RpcProviderService rpcProviderRegistry) {
119 this.rpcProviderRegistry = rpcProviderRegistry;
122 public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
123 this.notificationService = notificationPublishService;
127 LOG.info("Initializing {} for {}", this.getClass().getName(), APPLICATION_NAME);
129 if (rpcRegistration == null) {
130 if (rpcProviderRegistry != null) {
131 rpcRegistration = rpcProviderRegistry.registerRpcImplementation(CMHandleAPIService.class, this);
132 LOG.info("Initialization complete for {}", APPLICATION_NAME);
134 LOG.warn("Error initializing {} : rpcRegistry unset", APPLICATION_NAME);
138 String propDir = System.getenv(SDNC_CONFIG_DIR);
139 if (propDir == null) {
140 LOG.error("Environment variable SDNC_CONFIG_DIR is not set");
141 propDir = "/opt/onap/ccsdk/data/properties/";
142 } else if (!propDir.endsWith("/")) {
143 propDir = propDir + "/";
146 // GET configuration from properties file
147 config = new HashMap<String, String>();
149 try (FileInputStream fileInput = new FileInputStream(propDir + PROPERTIES_FILE_NAME)) {
150 Properties properties = new Properties();
151 properties.load(fileInput);
153 for (String param : new String[] {"url", "user", "password",
154 "authentication, dmi-service-name"}) {
155 config.put(param, properties.getProperty(param));
157 } catch (IOException e) {
158 LOG.error("Error while reading properties file: ", e);
161 LOG.info("addCMHandle Session Initiated");
165 public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
166 LOG.info("NetConf device connected {}", nNodeId.getValue());
167 JSONObject obj = new JSONObject();
168 obj.put("cm-handle-id", nNodeId.getValue());
169 obj.put("dmi-service-name", config.get("dmi-service-name"));
170 ClientConfig dmaapClientConfig = new DefaultClientConfig();
171 dmaapClientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, 180000);
172 dmaapClientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 60000);
173 Client dmaapClient = Client.create(dmaapClientConfig);
174 String authenticationMethod = config.get("authentication");
175 ClientResponse response = null;
177 if ("basic".equals(authenticationMethod)) {
178 LOG.debug("Sending message to dmaap-message-router: {}", obj.toString());
179 dmaapClient.addFilter(new HTTPBasicAuthFilter(config.get("user"), config.get("password")));
181 response = dmaapClient.resource(config.get("url")).type(MediaType.APPLICATION_JSON)
182 .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, obj);
184 response = dmaapClient.resource(config.get("url")).type(MediaType.APPLICATION_JSON)
185 .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, obj);
187 LOG.info("Received response from dmaap-message-router: \n {}", response.toString());
188 } catch (Exception e) {
189 LOG.error("Error while posting message to CM_HANDLE topic: ", e);
195 public void onRemoved(NodeId nNodeId) {
197 LOG.info("NetConf device removed - nNodeId = {}", nNodeId);
201 public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) {
202 LOG.info("NetConf device state changed nNodeId = {}}", nNodeId);
206 * Method called when the blueprint container is destroyed.
208 public void close() {
209 rpcRegistration.close();
210 LOG.debug("AddCMHandleProvider Closed");
214 public ListenableFuture<RpcResult<AddCMHandleOutput>> addCMHandle(AddCMHandleInput input) {