Merge "Replace odl-dlux-core with odl-dluxapps-topology"
[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 java.util.Collections;
13 import java.util.concurrent.ExecutionException;
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      * @param deviceContext device context
33      * @param multipartWriterProvider multipart writer provider
34      */
35     public void initialize(@Nonnull final DeviceContext deviceContext,
36                            final boolean switchFeaturesMandatory,
37                            @Nullable final MultipartWriterProvider multipartWriterProvider,
38                            @Nullable final ConvertorExecutor convertorExecutor) throws ExecutionException,InterruptedException {
39         Preconditions.checkNotNull(deviceContext);
40
41         // Write node to datastore
42         LOG.debug("Initializing node information for node {}", deviceContext.getDeviceInfo().getLOGValue());
43         try {
44             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceContext
45                     .getDeviceInfo()
46                     .getNodeInstanceIdentifier(),
47                 new NodeBuilder()
48                     .setId(deviceContext.getDeviceInfo().getNodeId())
49                     .setNodeConnector(Collections.emptyList())
50                     .build());
51         } catch (final Exception e) {
52             LOG.warn("Failed to write node {} to DS ", deviceContext.getDeviceInfo().getNodeId(), e);
53             throw new ExecutionException(new ConnectionException("Failed to write node " + deviceContext.getDeviceInfo().getNodeId() + " to DS ", e));
54         }
55
56         // Synchronously get information about device
57         initializeNodeInformation(deviceContext, switchFeaturesMandatory, multipartWriterProvider, convertorExecutor)
58             .get();
59     }
60
61     protected abstract Future<Void> initializeNodeInformation(@Nonnull final DeviceContext deviceContext,
62                                                               final boolean switchFeaturesMandatory,
63                                                               @Nullable final MultipartWriterProvider multipartWriterProvider,
64                                                               @Nullable final ConvertorExecutor convertorExecutor);
65 }