HONEYCOMB-130: Rename infra packages(remove vpp/v3po)
[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.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;
38
39 public class RestoringInitializer implements DataTreeInitializer {
40
41     private static final Logger LOG = LoggerFactory.getLogger(RestoringInitializer.class);
42
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;
48
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;
59     }
60
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);
65         }
66
67         return path;
68     }
69
70     @Override
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);
75             return;
76         }
77
78         try {
79             final ContainerNode containerNode = JsonUtils
80                 .readJsonRoot(schemaService.getGlobalContext(), Files.newInputStream(path, StandardOpenOption.READ));
81
82             final DOMDataWriteTransaction domDataWriteTransaction = dataTree.newWriteOnlyTransaction();
83             for (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : containerNode
84                 .getValue()) {
85                 final YangInstanceIdentifier iid = YangInstanceIdentifier.create(dataContainerChild.getIdentifier());
86                 LOG.trace("Restoring {} from {}", iid, path);
87
88                 switch (restorationType) {
89                     case Merge:
90                         domDataWriteTransaction.merge(datastoreType, iid, dataContainerChild);
91                         break;
92                     case Put:
93                         domDataWriteTransaction.put(datastoreType, iid, dataContainerChild);
94                         break;
95                     default:
96                         throw new InitializeException(
97                             "Unable to initialize data using " + restorationType + " restoration strategy. Unsupported");
98                 }
99             }
100
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);
104
105         } catch (IOException | TransactionCommitFailedException e) {
106             throw new InitializeException("Unable to restore data from " + path, e);
107         }
108     }
109
110     @Override
111     public void close() {}
112 }