2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl;
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;
28 @Service(classes = ServiceStatusProvider.class)
29 public class OpenflowPluginDiagStatusProvider implements ServiceStatusProvider {
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;
36 private final DiagStatusService diagStatusService;
37 private InetAddress defaultInetAddres;
38 private InetAddress legacyInetAddress;
41 public OpenflowPluginDiagStatusProvider(final @Reference DiagStatusService diagStatusService,
42 final SwitchConnectionProviderList switchConnectionProviders) {
43 this.diagStatusService = diagStatusService;
44 setSwitchConnectionInetAddress(switchConnectionProviders);
45 diagStatusService.register(OPENFLOW_SERVICE_NAME);
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();
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));
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));
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));
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.");
80 return new ServiceDescriptor(OPENFLOW_SERVICE_NAME, ServiceState.ERROR,
81 "OF::PORTS:: 6653 and 6633 are not up yet");
85 private boolean getApplicationNetworkState(int port, InetAddress inetAddress) {
88 if (inetAddress == null) {
89 socket = new Socket("localhost", port);
91 socket = new Socket(inetAddress, port);
93 LOG.debug("Socket connection established");
95 } catch (IOException e) {
102 } catch (IOException ex) {
103 LOG.error("Failed to close socket : {}", socket, ex);