Merge "Rename addDeleteOperationTotTxChain => addDeleteOperationToTxChain"
[openflowplugin.git] / openflowplugin-api / src / main / java / org / opendaylight / openflowplugin / api / diagstatus / OpenflowPluginDiagStatusProvider.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.api.diagstatus;
9
10 import java.io.IOException;
11 import java.net.Socket;
12 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
13 import org.opendaylight.infrautils.diagstatus.ServiceDescriptor;
14 import org.opendaylight.infrautils.diagstatus.ServiceState;
15 import org.opendaylight.infrautils.diagstatus.ServiceStatusProvider;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public class OpenflowPluginDiagStatusProvider implements ServiceStatusProvider {
20
21     private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginDiagStatusProvider.class);
22     private static final String OPENFLOW_SERVICE_NAME = "OPENFLOW";
23     private static final int OF_PORT_11 = 6633;
24     private static final int OF_PORT_13 = 6653;
25
26     private final DiagStatusService diagStatusService;
27     private volatile ServiceDescriptor serviceDescriptor;
28
29     public OpenflowPluginDiagStatusProvider(final DiagStatusService diagStatusService) {
30         this.diagStatusService = diagStatusService;
31         diagStatusService.register(OPENFLOW_SERVICE_NAME);
32     }
33
34     public void reportStatus(ServiceState serviceState, String description) {
35         LOG.debug("reporting status as {} for {}", serviceState, OPENFLOW_SERVICE_NAME);
36         serviceDescriptor = new ServiceDescriptor(OPENFLOW_SERVICE_NAME, serviceState, description);
37         diagStatusService.report(serviceDescriptor);
38     }
39
40     @Override
41     public ServiceDescriptor getServiceDescriptor() {
42
43         if (serviceDescriptor.getServiceState().equals(ServiceState.OPERATIONAL)) {
44             if (getApplicationNetworkState(OF_PORT_13) && getApplicationNetworkState(OF_PORT_11)) {
45                 return serviceDescriptor;
46             } else {
47                 serviceDescriptor = new ServiceDescriptor(OPENFLOW_SERVICE_NAME, ServiceState.ERROR,
48                         "OF::PORTS:: 6653 and 6633 are not up yet");
49                 return serviceDescriptor;
50             }
51         }
52         return serviceDescriptor;
53     }
54
55     private boolean getApplicationNetworkState(int port) {
56         Socket socket = null;
57         try {
58             socket = new Socket("localhost", port);
59             LOG.debug("Socket connection established");
60             return true;
61         } catch (IOException e) {
62             return false;
63         } finally {
64             try {
65                 if (socket != null) {
66                     socket.close();
67                 }
68             } catch (IOException ex) {
69                 LOG.error("Failed to close socket : {}", socket, ex);
70             }
71         }
72     }
73 }