ed517b4f08fcc0cb0ca85d18fce565c6d8ef1673
[honeycomb.git] / infra / cfg-init / src / main / java / io / fd / honeycomb / data / init / RestoringInitializer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.data.init;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
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;
37
38 public class RestoringInitializer implements DataTreeInitializer {
39
40     private static final Logger LOG = LoggerFactory.getLogger(RestoringInitializer.class);
41
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;
47
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;
58     }
59
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);
64         }
65
66         return path;
67     }
68
69     @Override
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);
74             return;
75         }
76
77         try {
78             final ContainerNode containerNode = JsonUtils
79                 .readJsonRoot(schemaService.getGlobalContext(), Files.newInputStream(path, StandardOpenOption.READ));
80
81             final DOMDataWriteTransaction domDataWriteTransaction = dataTree.newWriteOnlyTransaction();
82             for (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : containerNode
83                 .getValue()) {
84                 final YangInstanceIdentifier iid = YangInstanceIdentifier.create(dataContainerChild.getIdentifier());
85                 LOG.trace("Restoring {} from {}", iid, path);
86
87                 switch (restorationType) {
88                     case Merge:
89                         domDataWriteTransaction.merge(datastoreType, iid, dataContainerChild);
90                         break;
91                     case Put:
92                         domDataWriteTransaction.put(datastoreType, iid, dataContainerChild);
93                         break;
94                     default:
95                         throw new InitializeException(
96                             "Unable to initialize data using " + restorationType + " restoration strategy. Unsupported");
97                 }
98             }
99
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);
103
104         } catch (IOException | TransactionCommitFailedException e) {
105             throw new InitializeException("Unable to restore data from " + path, e);
106         }
107     }
108
109     /**
110      * Type of operation to use when writing restored data.
111      */
112     public static enum RestorationType {
113         Put, Merge
114     }
115 }