Use ByteBuf.readRetainedSlice()
[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 package org.opendaylight.openflowplugin.impl.device.initialization;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Collections;
14 import java.util.concurrent.Future;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.common.api.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     private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviceInitializer.class);
28
29     /**
30      * Perform initial information gathering and store them to operational datastore.
31      *
32      * @param deviceContext device context
33      * @param switchFeaturesMandatory is switch features mandatory
34      * @param skipTableFeatures skip collecting of table features
35      * @param multipartWriterProvider multipart writer provider
36      * @param convertorExecutor convertor executor
37      */
38     @SuppressWarnings("checkstyle:IllegalCatch")
39     public Future<Void> initialize(@NonNull final DeviceContext deviceContext,
40                                    final boolean switchFeaturesMandatory,
41                                    final boolean skipTableFeatures,
42                                    @Nullable final MultipartWriterProvider multipartWriterProvider,
43                                    @Nullable final ConvertorExecutor convertorExecutor) {
44         requireNonNull(deviceContext);
45
46         // Write node to datastore
47         LOG.debug("Initializing node information for node {}", deviceContext.getDeviceInfo());
48         try {
49             deviceContext.writeToTransaction(LogicalDatastoreType.OPERATIONAL, deviceContext
50                     .getDeviceInfo()
51                     .getNodeInstanceIdentifier(),
52                 new NodeBuilder()
53                     .setId(deviceContext.getDeviceInfo().getNodeId())
54                     .setNodeConnector(Collections.emptyMap())
55                     .build());
56         } catch (final Exception e) {
57             LOG.warn("Failed to write node {} to DS ", deviceContext.getDeviceInfo().getNodeId(), e);
58             return Futures.immediateFailedFuture(new ConnectionException(
59                     "Failed to write node " + deviceContext.getDeviceInfo().getNodeId() + " to DS ", e));
60         }
61
62         // Get information about device
63         return initializeNodeInformation(deviceContext, switchFeaturesMandatory, skipTableFeatures,
64                 multipartWriterProvider, convertorExecutor);
65     }
66
67     protected abstract Future<Void> initializeNodeInformation(@NonNull DeviceContext deviceContext,
68                                                               boolean switchFeaturesMandatory,
69                                                               boolean skipTableFeatures,
70                                                               @Nullable MultipartWriterProvider multipartWriterProvider,
71                                                               @Nullable ConvertorExecutor convertorExecutor);
72 }