2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.data.init;
19 import static com.google.common.base.Preconditions.checkArgument;
21 import io.fd.honeycomb.translate.util.JsonUtils;
22 import java.io.IOException;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.StandardOpenOption;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
31 import org.opendaylight.controller.sal.core.api.model.SchemaService;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class RestoringInitializer implements DataTreeInitializer {
40 private static final Logger LOG = LoggerFactory.getLogger(RestoringInitializer.class);
42 private final SchemaService schemaService;
43 private final Path path;
44 private final DOMDataBroker dataTree;
45 private final RestorationType restorationType;
46 private final LogicalDatastoreType datastoreType;
48 public RestoringInitializer(@Nonnull final SchemaService schemaService,
49 @Nonnull final Path path,
50 @Nonnull final DOMDataBroker dataTree,
51 @Nonnull final RestorationType restorationType,
52 @Nonnull final LogicalDatastoreType datastoreType) {
53 this.schemaService = schemaService;
54 this.datastoreType = datastoreType;
55 this.path = checkStorage(path);
56 this.dataTree = dataTree;
57 this.restorationType = restorationType;
60 private Path checkStorage(final Path path) {
61 if (Files.exists(path)) {
62 checkArgument(!Files.isDirectory(path), "File %s is a directory", path);
63 checkArgument(Files.isReadable(path), "File %s is not readable", path);
70 public void initialize() throws InitializeException {
71 LOG.debug("Starting restoration of {} from {} using {}", dataTree, path, restorationType);
72 if (!Files.exists(path)) {
73 LOG.debug("Persist file {} does not exist. Skipping restoration", path);
78 final ContainerNode containerNode = JsonUtils
79 .readJsonRoot(schemaService.getGlobalContext(), Files.newInputStream(path, StandardOpenOption.READ));
81 final DOMDataWriteTransaction domDataWriteTransaction = dataTree.newWriteOnlyTransaction();
82 for (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : containerNode
84 final YangInstanceIdentifier iid = YangInstanceIdentifier.create(dataContainerChild.getIdentifier());
85 LOG.trace("Restoring {} from {}", iid, path);
87 switch (restorationType) {
89 domDataWriteTransaction.merge(datastoreType, iid, dataContainerChild);
92 domDataWriteTransaction.put(datastoreType, iid, dataContainerChild);
95 throw new InitializeException(
96 "Unable to initialize data using " + restorationType + " restoration strategy. Unsupported");
100 // Block here to prevent subsequent initializers processing before context is fully restored
101 domDataWriteTransaction.submit().checkedGet();
102 LOG.debug("Data from {} restored successfully", path);
104 } catch (IOException | TransactionCommitFailedException e) {
105 throw new InitializeException("Unable to restore data from " + path, e);
110 * Type of operation to use when writing restored data.
112 public static enum RestorationType {