7fad99e45ef0d18e8f8be3af7ff45b2aca1b6984
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / initialization / AbstractDeviceInitializer.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.device.initialization;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Collections;
14 import java.util.concurrent.Future;
15 import javax.annotation.Nonnull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.openflowplugin.api.ConnectionException;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
21 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public abstract class AbstractDeviceInitializer {
27
28     private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceInitializer.class);
29
30     /**
31      * Perform initial information gathering and store them to operational datastore.
32      *
33      * @param deviceContext device context
34      * @param switchFeaturesMandatory is switch features mandatory
35      * @param skipTableFeatures skip collecting of table features
36      * @param multipartWriterProvider multipart writer provider
37      * @param convertorExecutor convertor executor
38      */
39     @SuppressWarnings("checkstyle:IllegalCatch")
40     public Future<Void> initialize(@Nonnull final DeviceContext deviceContext,
41                                    final boolean switchFeaturesMandatory,
42                                    final boolean skipTableFeatures,
43                                    @Nullable final MultipartWriterProvider multipartWriterProvider,
44                                    @Nullable final ConvertorExecutor convertorExecutor) {
45         Preconditions.checkNotNull(deviceContext);
46
47         // Write node to datastore
48         LOG.debug("Initializing node information for node {}", deviceContext.getDeviceInfo().getLOGValue());
49         try {
50             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceContext
51                     .getDeviceInfo()
52                     .getNodeInstanceIdentifier(),
53                 new NodeBuilder()
54                     .setId(deviceContext.getDeviceInfo().getNodeId())
55                     .setNodeConnector(Collections.emptyList())
56                     .build());
57         } catch (final Exception e) {
58             LOG.warn("Failed to write node {} to DS ", deviceContext.getDeviceInfo().getNodeId(), e);
59             return Futures.immediateFailedFuture(new ConnectionException(
60                     "Failed to write node " + deviceContext.getDeviceInfo().getNodeId() + " to DS ", e));
61         }
62
63         // Get information about device
64         return initializeNodeInformation(deviceContext, switchFeaturesMandatory, skipTableFeatures,
65                 multipartWriterProvider, convertorExecutor);
66     }
67
68     protected abstract Future<Void> initializeNodeInformation(@Nonnull DeviceContext deviceContext,
69                                                               boolean switchFeaturesMandatory,
70                                                               boolean skipTableFeatures,
71                                                               @Nullable MultipartWriterProvider multipartWriterProvider,
72                                                               @Nullable ConvertorExecutor convertorExecutor);
73 }