diff options
Diffstat (limited to 'sdnr/northbound/addCMHandle/provider/src/main/java')
2 files changed, 154 insertions, 113 deletions
diff --git a/sdnr/northbound/addCMHandle/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProvider.java b/sdnr/northbound/addCMHandle/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProvider.java index 013e71440..1756615cb 100644 --- a/sdnr/northbound/addCMHandle/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProvider.java +++ b/sdnr/northbound/addCMHandle/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProvider.java @@ -20,49 +20,120 @@ package org.onap.ccsdk.features.sdnr.northbound.addCMHandle; +import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION; + import com.google.common.base.Preconditions; import com.google.common.util.concurrent.ListenableFuture; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration; -import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.client.config.ClientConfig; +import com.sun.jersey.api.client.config.DefaultClientConfig; +import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Properties; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.MediaType; + +import org.eclipse.jdt.annotation.NonNull; +import org.json.JSONObject; +import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor; +import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener; +import org.onap.ccsdk.sli.core.sli.provider.MdsalHelper; +import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.binding.api.DataObjectModification; +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.NotificationPublishService; +import org.opendaylight.mdsal.binding.api.RpcProviderService; +import org.opendaylight.mdsal.dom.api.DOMDataBroker; 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.CMHandleAPIService; +import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode; +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.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.concepts.ObjectRegistration; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class AddCMHandleProvider implements CMHandleAPIService { +public class AddCMHandleProvider implements CMHandleAPIService, NetconfNodeStateListener, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(AddCMHandleProvider.class); - - private final String appName = "addCMHandle"; - - private final DataBroker dataBroker; - private final RpcProviderRegistry rpcProviderRegistry; - private RpcRegistration<CMHandleAPIService> serviceRegistration; - + 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 final String PARSING_ERROR = "Could not create the request message to send to the server; no message will be sent"; + private final ExecutorService executor; + protected DataBroker dataBroker; + protected DOMDataBroker domDataBroker; + protected NotificationPublishService notificationService; + protected RpcProviderService rpcProviderRegistry; + private ObjectRegistration<CMHandleAPIService> rpcRegistration; + public static final InstanceIdentifier<Topology> NETCONF_TOPO_IID = InstanceIdentifier.create(NetworkTopology.class) + .child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName()))); + private static HashMap<String, String> config; + + public AddCMHandleProvider() { + + LOG.info("Creating provider for {}", APPLICATION_NAME); + executor = Executors.newFixedThreadPool(1); + this.dataBroker = null; + this.domDataBroker = null; + this.notificationService = null; + this.rpcProviderRegistry = null; + this.rpcRegistration = null; + } - public AddCMHandleProvider(final DataBroker dataBroker, RpcProviderRegistry rpcProviderRegistry) { + public void setDataBroker(DataBroker dataBroker) { this.dataBroker = dataBroker; - this.rpcProviderRegistry = rpcProviderRegistry; } - /** - * Method called when the blueprint container is created. - */ - public void init() { - serviceRegistration = rpcProviderRegistry.addRpcImplementation(CMHandleAPIService.class, this); + public void setDomDataBroker(DOMDataBroker domDataBroker) { + this.domDataBroker = domDataBroker; + } - LOG.debug("Initializing provider for " + appName); + public void setRpcProviderRegistry(RpcProviderService rpcProviderRegistry) { + this.rpcProviderRegistry = rpcProviderRegistry; + } - Preconditions.checkNotNull(dataBroker, "dataBroker must be set"); + public void setNotificationPublishService(NotificationPublishService notificationPublishService) { + this.notificationService = notificationPublishService; + } + + public void init() { + LOG.info("Initializing {} for {}", this.getClass().getName(), APPLICATION_NAME); + + if (rpcRegistration == null) { + if (rpcProviderRegistry != null) { + rpcRegistration = rpcProviderRegistry.registerRpcImplementation(CMHandleAPIService.class, this); + LOG.info("Initialization complete for {}", APPLICATION_NAME); + } else { + LOG.warn("Error initializing {} : rpcRegistry unset", APPLICATION_NAME); + } + } String propDir = System.getenv(SDNC_CONFIG_DIR); if (propDir == null) { @@ -72,13 +143,74 @@ public class AddCMHandleProvider implements CMHandleAPIService { propDir = propDir + "/"; } - LOG.debug("Initialization complete for " + appName); + // GET configuration from properties file + config = new HashMap<String, String>(); + + try { + FileInputStream fileInput = new FileInputStream(propDir + PROPERTIES_FILE_NAME); + Properties properties = new Properties(); + properties.load(fileInput); + fileInput.close(); + + for (String param : new String[] {"url", "user", "password", + "authentication, dmi-service-name"}) { + config.put(param, properties.getProperty(param)); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + LOG.info("addCMHandle Session Initiated"); + } + + @Override + public void onCreated(NodeId nNodeId, NetconfNode netconfNode) { + LOG.info("NetConf device connected ", nNodeId.getValue()); + JSONObject obj = new JSONObject(); + obj.put("cm-handle-id", nNodeId.getValue()); + obj.put("dmi-service-name", config.get("dmi-service-name")); + ClientConfig dmaapClientConfig = new DefaultClientConfig(); + dmaapClientConfig.getProperties().put(ClientConfig.PROPERTY_READ_TIMEOUT, 180000); + dmaapClientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, 60000); + Client dmaapClient = Client.create(dmaapClientConfig); + String authenticationMethod = config.get("authentication"); + ClientResponse response = null; + try { + if (authenticationMethod.equals("basic")) { + LOG.debug("Sending message to dmaap-message-router: {}", obj.toString()); + dmaapClient.addFilter(new HTTPBasicAuthFilter(config.get("user"), config.get("password"))); + + response = dmaapClient.resource(config.get("url")).type(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, obj); + } else { + response = dmaapClient.resource(config.get("url")).type(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, obj); + } + } catch (Exception e) { + LOG.error("Error while posting message to CM_HANDLE topic: {}", e); + } + + LOG.info("Received response from dmaap-message-router: \n {}", response.toString()); + } + + @Override + public void onRemoved(NodeId nNodeId) { + + LOG.info("NetConf device removed - nNodeId = {}", nNodeId); + } + + @Override + public void onStateChange(NodeId nNodeId, NetconfNode netconfNode) { + LOG.info("NetConf device state changed nNodeId = {}}", nNodeId); } /** * Method called when the blueprint container is destroyed. */ public void close() { + rpcRegistration.close(); LOG.debug("AddCMHandleProvider Closed"); } diff --git a/sdnr/northbound/addCMHandle/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/impl/AddCMHandleProvider.java b/sdnr/northbound/addCMHandle/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/impl/AddCMHandleProvider.java deleted file mode 100644 index 6e59e072d..000000000 --- a/sdnr/northbound/addCMHandle/provider/src/main/java/org/onap/ccsdk/features/sdnr/northbound/impl/AddCMHandleProvider.java +++ /dev/null @@ -1,91 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP : CCSDK - * ================================================================================ - * Copyright (C) 2021 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.impl; - -import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.ListenableFuture; - -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration; -import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; -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.CMHandleAPIService; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class AddCMHandleProvider implements CMHandleAPIService { - - private static final Logger LOG = LoggerFactory.getLogger(AddCMHandleProvider.class); - - private final String appName = "addCMHandle"; - - private final DataBroker dataBroker; - private final RpcProviderRegistry rpcProviderRegistry; - private RpcRegistration<CMHandleAPIService> serviceRegistration; - - private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR"; - private static final String PROPERTIES_FILE_NAME = "cm-handle.properties"; - private static final String PARSING_ERROR = - "Could not create the request message to send to the server; no message will be sent"; - - public AddCMHandleProvider(final DataBroker dataBroker, RpcProviderRegistry rpcProviderRegistry) { - this.dataBroker = dataBroker; - this.rpcProviderRegistry = rpcProviderRegistry; - } - - /** - * Method called when the blueprint container is created. - */ - public void init() { - serviceRegistration = rpcProviderRegistry.addRpcImplementation(CMHandleAPIService.class, this); - - LOG.debug("Initializing provider for " + appName); - - Preconditions.checkNotNull(dataBroker, "dataBroker must be set"); - - 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 + "/"; - } - - LOG.debug("Initialization complete for " + appName); - } - - /** - * Method called when the blueprint container is destroyed. - */ - public void close() { - LOG.debug("AddCMHandleProvider Closed"); - } - - @Override - public ListenableFuture<RpcResult<AddCMHandleOutput>> addCMHandle(AddCMHandleInput input) { - - return null; - } - -} |