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.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.data.init.rev160407.RestorationType;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class RestoringInitializer implements DataTreeInitializer {
41 private static final Logger LOG = LoggerFactory.getLogger(RestoringInitializer.class);
43 private final SchemaService schemaService;
44 private final Path path;
45 private final DOMDataBroker dataTree;
46 private final RestorationType restorationType;
47 private final LogicalDatastoreType datastoreType;
49 public RestoringInitializer(@Nonnull final SchemaService schemaService,
50 @Nonnull final Path path,
51 @Nonnull final DOMDataBroker dataTree,
52 @Nonnull final RestorationType restorationType,
53 @Nonnull final LogicalDatastoreType datastoreType) {
54 this.schemaService = schemaService;
55 this.datastoreType = datastoreType;
56 this.path = checkStorage(path);
57 this.dataTree = dataTree;
58 this.restorationType = restorationType;
61 private Path checkStorage(final Path path) {
62 if (Files.exists(path)) {
63 checkArgument(!Files.isDirectory(path), "File %s is a directory", path);
64 checkArgument(Files.isReadable(path), "File %s is not readable", path);
71 public void initialize() throws InitializeException {
72 LOG.debug("Starting restoration of {} from {} using {}", dataTree, path, restorationType);
73 if(!Files.exists(path)) {
74 LOG.debug("Persist file {} does not exist. Skipping restoration", path);
79 final ContainerNode containerNode = JsonUtils
80 .readJsonRoot(schemaService.getGlobalContext(), Files.newInputStream(path, StandardOpenOption.READ));
82 final DOMDataWriteTransaction domDataWriteTransaction = dataTree.newWriteOnlyTransaction();
83 for (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : containerNode
85 final YangInstanceIdentifier iid = YangInstanceIdentifier.create(dataContainerChild.getIdentifier());
86 LOG.trace("Restoring {} from {}", iid, path);
88 switch (restorationType) {
90 domDataWriteTransaction.merge(datastoreType, iid, dataContainerChild);
93 domDataWriteTransaction.put(datastoreType, iid, dataContainerChild);
96 throw new InitializeException(
97 "Unable to initialize data using " + restorationType + " restoration strategy. Unsupported");
101 // Block here to prevent subsequent initializers processing before context is fully restored
102 domDataWriteTransaction.submit().checkedGet();
103 LOG.debug("Data from {} restored successfully", path);
105 } catch (IOException | TransactionCommitFailedException e) {
106 throw new InitializeException("Unable to restore data from " + path, e);
111 public void close() {}