git rid of Blueprint openflowplugin.xml
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / 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.impl;
9
10 import java.io.IOException;
11 import java.net.InetAddress;
12 import java.net.Socket;
13 import java.util.List;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.apache.aries.blueprint.annotation.service.Reference;
17 import org.apache.aries.blueprint.annotation.service.Service;
18 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
19 import org.opendaylight.infrautils.diagstatus.ServiceDescriptor;
20 import org.opendaylight.infrautils.diagstatus.ServiceState;
21 import org.opendaylight.infrautils.diagstatus.ServiceStatusProvider;
22 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
23 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProviderList;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 @Singleton
28 @Service(classes = ServiceStatusProvider.class)
29 public class OpenflowPluginDiagStatusProvider implements ServiceStatusProvider {
30
31     private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginDiagStatusProvider.class);
32     private static final String OPENFLOW_SERVICE_NAME = "OPENFLOW";
33     private static final int OF_PORT_11 = 6633;
34     private static final int OF_PORT_13 = 6653;
35
36     private final DiagStatusService diagStatusService;
37     private InetAddress defaultInetAddres;
38     private InetAddress legacyInetAddress;
39
40     @Inject
41     public OpenflowPluginDiagStatusProvider(final @Reference DiagStatusService diagStatusService,
42                                             final SwitchConnectionProviderList switchConnectionProviders) {
43         this.diagStatusService = diagStatusService;
44         setSwitchConnectionInetAddress(switchConnectionProviders);
45         diagStatusService.register(OPENFLOW_SERVICE_NAME);
46     }
47
48     private void setSwitchConnectionInetAddress(final List<SwitchConnectionProvider> switchConnectionProviders) {
49         switchConnectionProviders.forEach(switchConnectionProvider -> {
50             if (switchConnectionProvider.getConfiguration().getPort() == OF_PORT_11) {
51                 legacyInetAddress = switchConnectionProvider.getConfiguration().getAddress();
52             } else if (switchConnectionProvider.getConfiguration().getPort() == OF_PORT_13) {
53                 defaultInetAddres = switchConnectionProvider.getConfiguration().getAddress();
54             }
55         });
56     }
57
58     public void reportStatus(ServiceState serviceState) {
59         LOG.debug("reporting status as {} for {}", serviceState, OPENFLOW_SERVICE_NAME);
60         diagStatusService.report(new ServiceDescriptor(OPENFLOW_SERVICE_NAME, serviceState));
61     }
62
63     public void reportStatus(ServiceState serviceState, Throwable throwable) {
64         LOG.debug("reporting status as {} for {}", serviceState, OPENFLOW_SERVICE_NAME);
65         diagStatusService.report(new ServiceDescriptor(OPENFLOW_SERVICE_NAME, throwable));
66     }
67
68     public void reportStatus(ServiceState serviceState, String description) {
69         LOG.debug("reporting status as {} for {}", serviceState, OPENFLOW_SERVICE_NAME);
70         diagStatusService.report(new ServiceDescriptor(OPENFLOW_SERVICE_NAME, serviceState, description));
71     }
72
73     @Override
74     public ServiceDescriptor getServiceDescriptor() {
75         if (getApplicationNetworkState(OF_PORT_13, defaultInetAddres)
76                 && getApplicationNetworkState(OF_PORT_11, legacyInetAddress)) {
77             return new ServiceDescriptor(OPENFLOW_SERVICE_NAME, ServiceState.OPERATIONAL,
78                     "OF::PORTS:: 6653 and 6633 are up.");
79         } else {
80             return new ServiceDescriptor(OPENFLOW_SERVICE_NAME, ServiceState.ERROR,
81                     "OF::PORTS:: 6653 and 6633 are not up yet");
82         }
83     }
84
85     private boolean getApplicationNetworkState(int port, InetAddress inetAddress) {
86         Socket socket = null;
87         try {
88             if (inetAddress == null) {
89                 socket = new Socket("localhost", port);
90             } else {
91                 socket = new Socket(inetAddress, port);
92             }
93             LOG.debug("Socket connection established");
94             return true;
95         } catch (IOException e) {
96             return false;
97         } finally {
98             try {
99                 if (socket != null) {
100                     socket.close();
101                 }
102             } catch (IOException ex) {
103                 LOG.error("Failed to close socket : {}", socket, ex);
104             }
105         }
106     }
107 }