diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-05-18 15:05:51 +0200 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-05-24 08:36:34 +0000 |
commit | ee1202f9cb83b2b9bcf6ae3f8704bbf0f39b77a5 (patch) | |
tree | d3bcb31ef2138c90445199b3709802206c5e2cdc /v3po/vpp-cfg-init/src/main/java/io | |
parent | 5e1e4256dbd294cf2ccc3b0ad2435b403a420214 (diff) |
HONEYCOMB-61: Restore configuration and context from persisted files
Change-Id: I6edce127f8895f5d65998b4be71a0a111ca2e8bb
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Diffstat (limited to 'v3po/vpp-cfg-init/src/main/java/io')
4 files changed, 117 insertions, 0 deletions
diff --git a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/DataTreeInitializer.java b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/DataTreeInitializer.java index 3cb6f14e4..d760401f9 100644 --- a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/DataTreeInitializer.java +++ b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/DataTreeInitializer.java @@ -43,5 +43,9 @@ public interface DataTreeInitializer extends AutoCloseable { public InitializeException(final String message, final Throwable cause) { super(message, cause); } + + public InitializeException(final String msg) { + super(msg); + } } } diff --git a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/InterfacesInitializer.java b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/InterfacesInitializer.java index fde2a2893..9ecbe0c13 100644 --- a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/InterfacesInitializer.java +++ b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/InterfacesInitializer.java @@ -43,6 +43,8 @@ public class InterfacesInitializer extends AbstractDataTreeConverter<InterfacesS InstanceIdentifier.create(Interfaces.class)); } + // TODO move to v3po2vpp + @Override protected Interfaces convert(final InterfacesState operationalData) { LOG.debug("InterfacesInitializer.convert()"); diff --git a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/RestoringInitializer.java b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/RestoringInitializer.java new file mode 100644 index 000000000..eed22313c --- /dev/null +++ b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/RestoringInitializer.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.v3po.vpp.data.init; + +import static com.google.common.base.Preconditions.checkArgument; + +import io.fd.honeycomb.v3po.translate.util.JsonUtils; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; +import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction; +import org.opendaylight.controller.sal.core.api.model.SchemaService; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.vpp.data.init.rev160407.RestorationType; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; + +public class RestoringInitializer implements DataTreeInitializer { + + private final SchemaService schemaService; + private final Path path; + private final DOMDataBroker dataTree; + private final RestorationType restorationType; + private final LogicalDatastoreType datastoreType; + + public RestoringInitializer(@Nonnull final SchemaService schemaService, + @Nonnull final Path path, + @Nonnull final DOMDataBroker dataTree, + @Nonnull final RestorationType restorationType, + @Nonnull final LogicalDatastoreType datastoreType) { + this.schemaService = schemaService; + this.datastoreType = datastoreType; + this.path = checkStorage(path); + this.dataTree = dataTree; + this.restorationType = restorationType; + } + + private Path checkStorage(final Path path) { + try { + if(Files.exists(path)) { + checkArgument(!Files.isDirectory(path), "File %s is a directory", path); + checkArgument(Files.isReadable(path), "File %s is not readable", path); + } else { + return checkStorage(Files.createFile(path)); + } + } catch (IOException e) { + throw new IllegalArgumentException("Cannot use " + path + " for restoring data", e); + } + + return path; + } + + @Override + public void initialize() throws InitializeException { + if(!Files.exists(path)) { + return; + } + + try { + final ContainerNode containerNode = JsonUtils + .readJsonRoot(schemaService.getGlobalContext(), Files.newInputStream(path, StandardOpenOption.READ)); + + final DOMDataWriteTransaction domDataWriteTransaction = dataTree.newWriteOnlyTransaction(); + for (DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> dataContainerChild : containerNode + .getValue()) { + final YangInstanceIdentifier iid = YangInstanceIdentifier.create(dataContainerChild.getIdentifier()); + switch (restorationType) { + case Merge: + domDataWriteTransaction.merge(datastoreType, iid, dataContainerChild); + break; + case Put: + domDataWriteTransaction.put(datastoreType, iid, dataContainerChild); + break; + default: + throw new InitializeException( + "Unable to initialize data using " + restorationType + " restoration strategy. Unsupported"); + } + } + + // Block here to prevent subsequent initializers processing before context is fully restored + domDataWriteTransaction.submit().checkedGet(); + + } catch (IOException | TransactionCommitFailedException e) { + throw new InitializeException("Unable to restore data from " + path, e); + } + } + + @Override + public void close() {} +} diff --git a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/VppInitializer.java b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/VppInitializer.java index 77bb3a563..d7a67870a 100644 --- a/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/VppInitializer.java +++ b/v3po/vpp-cfg-init/src/main/java/io/fd/honeycomb/v3po/vpp/data/init/VppInitializer.java @@ -42,6 +42,8 @@ public class VppInitializer extends AbstractDataTreeConverter<VppState, Vpp> { super(bindingDataBroker, InstanceIdentifier.create(VppState.class), InstanceIdentifier.create(Vpp.class)); } + // TODO move to v3po2vpp + @Override protected Vpp convert(final VppState operationalData) { LOG.debug("VppInitializer.convert()"); |