diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-07-27 11:05:51 +0200 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-07-29 16:32:07 +0200 |
commit | 81c9cdfa5a9e91b91d73a6a2b20cff8b612d7de3 (patch) | |
tree | c941a2e5ea9df88acdfc64392b03228fdd18eb2b /vpp-common/vpp-translate-utils/src/main/java/io | |
parent | 10d33239e1e0d10a2f51e6739d18035e61600eb8 (diff) |
HONEYCOMB-130: Separate v3po plugin from HC infra
Creating folders:
- common/
- infra/
- v3po/
- vpp-common/
Change-Id: I2c39e1b17e39e7c0f0628f44aa5fe08563fa06e4
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Diffstat (limited to 'vpp-common/vpp-translate-utils/src/main/java/io')
18 files changed, 1175 insertions, 0 deletions
diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java new file mode 100644 index 000000000..e5adb54ad --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/DumpCacheManager.java @@ -0,0 +1,124 @@ +/* + * 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.translate.v3po.util.cache; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import io.fd.honeycomb.translate.v3po.util.cache.noop.NoopDumpPostProcessingFunction; +import io.fd.honeycomb.v3po.translate.ModificationCache; +import javax.annotation.Nonnull; +import org.openvpp.jvpp.dto.JVppReplyDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Manager responsible for returning Data object dumps<br> either from cache or by invoking specified {@link + * EntityDumpExecutor} + */ +public final class DumpCacheManager<T extends JVppReplyDump, U> { + + private static final Logger LOG = LoggerFactory.getLogger(DumpCacheManager.class); + + private final EntityDumpExecutor<T, U> dumpExecutor; + private final EntityDumpNonEmptyCheck<T> dumpNonEmptyCheck; + private final EntityDumpPostProcessingFunction<T> postProcessor; + + private DumpCacheManager(DumpCacheManagerBuilder<T, U> builder) { + this.dumpExecutor = builder.dumpExecutor; + this.dumpNonEmptyCheck = builder.dumpNonEmptyCheck; + this.postProcessor = builder.postProcessingFunction; + } + + /** + * Returns {@link Optional<T>} of dump + */ + public Optional<T> getDump(@Nonnull String entityKey, @Nonnull ModificationCache cache) + throws DumpExecutionFailedException { + + //this key binding to every log has its logic ,because every customizer have its own cache manager and if + //there is need for debugging/fixing some complex call with a lot of data,you can get lost in those logs + LOG.debug("Loading dump for KEY[{}]", entityKey); + + T dump = (T) cache.get(entityKey); + + if (dump == null) { + LOG.debug("Dump for KEY[{}] not present in cache,invoking dump executor", entityKey); + + dump = dumpExecutor.executeDump(); + + //this is not a critical exception, so its only logged here + try { + dumpNonEmptyCheck.assertNotEmpty(dump); + } catch (DumpCheckFailedException e) { + LOG.warn("Dump for KEY[{}] has been resolved as empty", entityKey, e); + return Optional.absent(); + } + + //no need to check if post processor active,if wasnt set,default no-op will be used + LOG.debug("Post-processing dump for KEY[{}]", entityKey); + dump = postProcessor.apply(dump); + + LOG.debug("Caching dump for KEY[{}]", entityKey); + cache.put(entityKey, dump); + return Optional.of(dump); + } else { + return Optional.of(dump); + } + } + + public static final class DumpCacheManagerBuilder<T extends JVppReplyDump, U> { + + private EntityDumpExecutor<T, U> dumpExecutor; + private EntityDumpNonEmptyCheck<T> dumpNonEmptyCheck; + private EntityDumpPostProcessingFunction<T> postProcessingFunction; + + public DumpCacheManagerBuilder() { + //for cases when user does not set specific post-processor + postProcessingFunction = new NoopDumpPostProcessingFunction<T>(); + } + + public DumpCacheManagerBuilder<T, U> withExecutor(@Nonnull EntityDumpExecutor<T, U> executor) { + this.dumpExecutor = executor; + return this; + } + + public DumpCacheManagerBuilder<T, U> withNonEmptyPredicate(@Nonnull EntityDumpNonEmptyCheck<T> check) { + this.dumpNonEmptyCheck = check; + return this; + } + + public DumpCacheManagerBuilder<T, U> withPostProcessingFunction( + EntityDumpPostProcessingFunction<T> postProcessingFunction) { + this.postProcessingFunction = postProcessingFunction; + return this; + } + + public DumpCacheManager<T, U> build() { + checkNotNull(dumpExecutor, "Dump executor cannot be null"); + checkNotNull(dumpNonEmptyCheck, "Dump verifier cannot be null"); + checkNotNull(postProcessingFunction, + "Dump post-processor cannot be null cannot be null, default implementation is used if its not set"); + + return new DumpCacheManager<>(this); + } + } +} + + diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpExecutor.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpExecutor.java new file mode 100644 index 000000000..b25c59a8a --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpExecutor.java @@ -0,0 +1,41 @@ +/* + * 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.translate.v3po.util.cache; + +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for classes that return dumps for Data objects + */ +public interface EntityDumpExecutor<T extends JVppReplyDump, U> { + + /** + * Performs dump on {link T} entity + * + * @return dump of specified {@link T} entity + * @throws DumpExecutionFailedException when dump fails + */ + public T executeDump() throws DumpExecutionFailedException; + + /** + * Bind dumping params for executor + */ + default public void bindDumpParams(U params) { + throw new UnsupportedOperationException("You should override this method if you need to bind dumping params"); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java new file mode 100644 index 000000000..d64e312fe --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java @@ -0,0 +1,33 @@ +/* + * 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.translate.v3po.util.cache; + +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.i.DumpEmptyException; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for classes that verifies if dump of data object is non-empty + */ +public interface EntityDumpNonEmptyCheck<T extends JVppReplyDump> { + + /** + * Verifies if data are non-empty,if not throws {@link DumpEmptyException} + * @throws DumpEmptyException + */ + public void assertNotEmpty(T data) throws DumpCheckFailedException; +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java new file mode 100644 index 000000000..2bc2446e9 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java @@ -0,0 +1,36 @@ +/* + * 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.translate.v3po.util.cache; + +import java.util.function.Function; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for class that are post-processing data dumped from vpp + */ +@FunctionalInterface +public interface EntityDumpPostProcessingFunction<T extends JVppReplyDump> extends Function<T, T> { + + + /** + * Performs postprocessing on dumped data + * + * @return Post-processed data + */ + @Override + T apply(T t); +}
\ No newline at end of file diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java new file mode 100644 index 000000000..39dabb62f --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java @@ -0,0 +1,31 @@ +/* + * 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.translate.v3po.util.cache.exceptions.check; + +/** + * Abstract parent of exceptions thrown while checking if dump is not empty + */ +public abstract class DumpCheckFailedException extends Exception { + + public DumpCheckFailedException(String message) { + super(message); + } + + public DumpCheckFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java new file mode 100644 index 000000000..8acaf1c9f --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java @@ -0,0 +1,40 @@ +/* + * 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.translate.v3po.util.cache.exceptions.check.i; + +import io.fd.honeycomb.translate.v3po.util.cache.EntityDumpNonEmptyCheck; +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; + +/** + * This exception occurs when dump is resolved as empty by {@link EntityDumpNonEmptyCheck} + */ +public class DumpEmptyException extends DumpCheckFailedException { + + /** + * Creates {@link DumpEmptyException} with specified reason + */ + public DumpEmptyException(String reason) { + super(reason); + } + + /** + * Creates {@link DumpEmptyException} with specified reason and sub-exception + */ + public DumpEmptyException(String reason, Exception e) { + super(reason, e); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java new file mode 100644 index 000000000..ba249b6c0 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java @@ -0,0 +1,27 @@ +/* + * 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.translate.v3po.util.cache.exceptions.execution; + +/** + * Default parent for all exceptions connected to dumping of VPP data + */ +public abstract class DumpExecutionFailedException extends Exception { + + public DumpExecutionFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java new file mode 100644 index 000000000..02b8722ab --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java @@ -0,0 +1,34 @@ +/* + * 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.translate.v3po.util.cache.exceptions.execution.i; + +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.openvpp.jvpp.VppBaseCallException; + +/** + * Wrapper exception for {@link org.openvpp.jvpp.VppBaseCallException} during dumping + */ +public class DumpCallFailedException extends DumpExecutionFailedException { + + public DumpCallFailedException(String message, VppBaseCallException cause) { + super(message, cause); + } + + public static final DumpCallFailedException wrapFailedCallException(String message, VppBaseCallException cause) { + return new DumpCallFailedException(message, cause); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java new file mode 100644 index 000000000..4bc825408 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java @@ -0,0 +1,35 @@ +/* + * 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.translate.v3po.util.cache.exceptions.execution.i; + +import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import java.util.concurrent.TimeoutException; + +/** + * Exception thrown when dump call ends in timeout + */ +public class DumpTimeoutException extends DumpExecutionFailedException { + + public DumpTimeoutException(String message, TimeoutException cause) { + super(message, cause); + + } + + public static final DumpTimeoutException wrapTimeoutException(String message, TimeoutException cause) { + return new DumpTimeoutException(message, cause); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java new file mode 100644 index 000000000..58bfe6f0b --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java @@ -0,0 +1,33 @@ +/* + * 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.translate.v3po.util.cache.noop; + +import io.fd.honeycomb.translate.v3po.util.cache.EntityDumpPostProcessingFunction; +import org.openvpp.jvpp.dto.JVppReplyDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NoopDumpPostProcessingFunction<T extends JVppReplyDump> implements EntityDumpPostProcessingFunction<T> { + + private static final Logger LOG = LoggerFactory.getLogger(NoopDumpPostProcessingFunction.class); + + @Override + public T apply(final T t) { + LOG.debug("Default post processing function called for {}", t); + return t; + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java new file mode 100644 index 000000000..1f2fbd838 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java @@ -0,0 +1,103 @@ +/* + * 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.translate.v3po.util; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; + +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.translate.spi.write.WriterCustomizer; +import io.fd.honeycomb.v3po.translate.write.WriteContext; +import io.fd.honeycomb.v3po.translate.write.WriteFailedException; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.future.FutureJVpp; + +/** + * Validation WriteCustomizers for Interface subnodes. + * Validates the type of interface. + * + * TODO this should be validated on model/DataTree level. However DataTree does not enforce When conditions + * Delete this class when DataTree handles when constraints properly + */ +public abstract class AbstractInterfaceTypeCustomizer<D extends DataObject> + extends FutureJVppCustomizer implements WriterCustomizer<D> { + + protected AbstractInterfaceTypeCustomizer(final FutureJVpp futureJvpp) { + super(futureJvpp); + } + + private void checkProperInterfaceType(@Nonnull final WriteContext writeContext, + @Nonnull final InstanceIdentifier<D> id) { + final InstanceIdentifier<Interface> ifcTypeFromIid = id.firstIdentifierOf(Interface.class); + checkArgument(ifcTypeFromIid != null, "Instance identifier does not contain {} type", Interface.class); + checkArgument(id.firstKeyOf(Interface.class) != null, "Instance identifier does not contain keyed {} type", + Interface.class); + final Optional<Interface> interfaceConfig = writeContext.readAfter(ifcTypeFromIid); + checkState(interfaceConfig.isPresent(), + "Unable to get Interface configuration for an interface: %s currently being updated", ifcTypeFromIid); + + IllegalInterfaceTypeException + .checkInterfaceType(interfaceConfig.get(), getExpectedInterfaceType()); + } + + protected abstract Class<? extends InterfaceType> getExpectedInterfaceType(); + + /** + * Validate expected interface type + */ + @Override + public final void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataAfter, + @Nonnull final WriteContext writeContext) throws WriteFailedException { + checkProperInterfaceType(writeContext, id); + writeInterface(id, dataAfter, writeContext); + } + + protected abstract void writeInterface(final InstanceIdentifier<D> id, final D dataAfter, + final WriteContext writeContext) + throws WriteFailedException; + + // Validation for update and delete is not necessary + + /** + * Indicates unexpected interface type + */ + protected static final class IllegalInterfaceTypeException extends IllegalArgumentException { + + private IllegalInterfaceTypeException(final String msg) { + super(msg); + } + + /** + * Check the type of interface equals expected type + * + * @throws IllegalInterfaceTypeException if type of interface is null or not expected + */ + static void checkInterfaceType(@Nonnull final Interface ifc, + @Nonnull final Class<? extends InterfaceType> expectedType) { + if (ifc.getType() == null || !expectedType.equals(ifc.getType())) { + throw new IllegalInterfaceTypeException(String.format( + "Unexpected interface type: %s for interface: %s. Expected interface is: %s", ifc.getType(), + ifc.getName(), expectedType)); + } + } + + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/FutureJVppCustomizer.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/FutureJVppCustomizer.java new file mode 100644 index 000000000..3f4f1f5ce --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/FutureJVppCustomizer.java @@ -0,0 +1,45 @@ +/* + * 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.translate.v3po.util; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import org.openvpp.jvpp.future.FutureJVpp; + +import javax.annotation.Nonnull; + +/** + * Abstract utility to hold the vppApi reference. + */ +@Beta +public abstract class FutureJVppCustomizer { + + private final FutureJVpp futureJvpp; + + public FutureJVppCustomizer(@Nonnull final FutureJVpp futureJvpp) { + this.futureJvpp = Preconditions.checkNotNull(futureJvpp, "futureJvpp should not be null"); + } + + /** + * Get vppApi reference + * + * @return vppApi reference + */ + public FutureJVpp getFutureJVpp() { + return futureJvpp; + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/NamingContext.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/NamingContext.java new file mode 100644 index 000000000..f6e0e6d48 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/NamingContext.java @@ -0,0 +1,188 @@ +/* + * 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.translate.v3po.util; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; + +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.translate.MappingContext; +import io.fd.honeycomb.v3po.translate.util.RWUtils; +import java.util.stream.Collector; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContextKey; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder; +import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility adapter on top of {@link MappingContext} storing integer to string mappings according to naming-context yang + * model + */ +public final class NamingContext implements AutoCloseable { + + private static final Logger LOG = LoggerFactory.getLogger(NamingContext.class); + + private final String artificialNamePrefix; + private final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext, NamingContextKey> + namingContextIid; + + private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector(); + + /** + * Create new naming context + * + * @param artificialNamePrefix artificial name to be used for items without a name in VPP (or not provided) + * @param instanceName name of this context instance. Will be used as list item identifier within context data tree + */ + public NamingContext(@Nonnull final String artificialNamePrefix, @Nonnull final String instanceName) { + this.artificialNamePrefix = artificialNamePrefix; + namingContextIid = InstanceIdentifier.create(Contexts.class).child( + org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext.class, + new NamingContextKey(instanceName)); + } + + /** + * Retrieve name for mapping stored provided mappingContext instance. If not present, artificial name will be + * generated. + * + * @param index index of a mapped item + * @param mappingContext mapping context providing context data for current transaction + * + * @return name mapped to provided index + */ + @Nonnull + public synchronized String getName(final int index, @Nonnull final MappingContext mappingContext) { + if (!containsName(index, mappingContext)) { + final String artificialName = getArtificialName(index); + addName(index, artificialName, mappingContext); + } + + final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class)); + checkState(read.isPresent(), "Mapping for index: %s is not present. But should be", index); + + return read.get().getMapping().stream() + .filter(mapping -> mapping.getIndex().equals(index)) + .collect(SINGLE_ITEM_COLLECTOR).getName(); + } + + /** + * Retrieve name for mapping stored provided mappingContext instance. if present + * + * @param index index of a mapped item + * @param mappingContext mapping context providing context data for current transaction + * + * @return name mapped to provided index + */ + @Nonnull + public synchronized Optional<String> getNameIfPresent(final int index, + @Nonnull final MappingContext mappingContext) { + final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class)); + + return read.isPresent() + ? Optional.of(read.get().getMapping().stream() + .filter(mapping -> mapping.getIndex().equals(index)) + .collect(SINGLE_ITEM_COLLECTOR) + .getName()) + : Optional.absent(); + } + + /** + * Check whether mapping is present for index. + * + * @param index index of a mapped item + * @param mappingContext mapping context providing context data for current transaction + * + * @return true if present, false otherwise + */ + public synchronized boolean containsName(final int index, @Nonnull final MappingContext mappingContext) { + final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class)); + return read.isPresent() + ? read.get().getMapping().stream().anyMatch(mapping -> mapping.getIndex().equals(index)) + : false; + } + + + /** + * Add mapping to current context + * + * @param index index of a mapped item + * @param name name of a mapped item + * @param mappingContext mapping context providing context data for current transaction + */ + public synchronized void addName(final int index, final String name, final MappingContext mappingContext) { + final KeyedInstanceIdentifier<Mapping, MappingKey> mappingIid = getMappingIid(name); + mappingContext.put(mappingIid, new MappingBuilder().setIndex(index).setName(name).build()); + } + + private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final String name) { + return namingContextIid.child(Mappings.class).child(Mapping.class, new MappingKey(name)); + } + + /** + * Remove mapping from current context + * + * @param name name of a mapped item + * @param mappingContext mapping context providing context data for current transaction + */ + public synchronized void removeName(final String name, final MappingContext mappingContext) { + mappingContext.delete(getMappingIid(name)); + } + + /** + * Returns index value associated with the given name. + * + * @param name the name whose associated index value is to be returned + * @param mappingContext mapping context providing context data for current transaction + * + * @return integer index value matching supplied name + * @throws IllegalArgumentException if name was not found + */ + public synchronized int getIndex(final String name, final MappingContext mappingContext) { + final Optional<Mapping> read = mappingContext.read(getMappingIid(name)); + checkArgument(read.isPresent(), "No mapping stored for name: %s", name); + return read.get().getIndex(); + + } + + /** + * Check whether mapping is present for name. + * + * @param name name of a mapped item + * @param mappingContext mapping context providing context data for current transaction + * + * @return true if present, false otherwise + */ + public synchronized boolean containsIndex(final String name, final MappingContext mappingContext) { + return mappingContext.read(getMappingIid(name)).isPresent(); + } + + private String getArtificialName(final int index) { + return artificialNamePrefix + index; + } + + @Override + public void close() throws Exception { + /// Not removing the mapping from backing storage + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/ReadTimeoutException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/ReadTimeoutException.java new file mode 100644 index 000000000..654b13825 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/ReadTimeoutException.java @@ -0,0 +1,33 @@ +/* + * 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.translate.v3po.util; + +import com.google.common.annotations.Beta; +import io.fd.honeycomb.v3po.translate.read.ReadFailedException; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Thrown when read method invocation times out. + */ +@Beta +public class ReadTimeoutException extends ReadFailedException { + + public ReadTimeoutException(final InstanceIdentifier<?> id, final Throwable cause) { + super(id, cause); + } + +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/SubInterfaceUtils.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/SubInterfaceUtils.java new file mode 100644 index 000000000..1df82af73 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/SubInterfaceUtils.java @@ -0,0 +1,28 @@ +/* + * 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.translate.v3po.util; + +public final class SubInterfaceUtils { + + private SubInterfaceUtils() { + throw new UnsupportedOperationException("Utility class cannot be instantiated."); + } + + public static String getSubInterfaceName(final String superIfName, final int subIfaceId) { + return String.format("%s.%d", superIfName, subIfaceId); + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TagRewriteOperation.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TagRewriteOperation.java new file mode 100644 index 000000000..316d1f8e2 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TagRewriteOperation.java @@ -0,0 +1,86 @@ +/* + * 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.translate.v3po.util; + +import com.google.common.base.Preconditions; +import com.google.common.primitives.UnsignedBytes; +import javax.annotation.Nonnegative; +import javax.annotation.Nullable; + +/** + * Defines vlan tag rewrite config options for VPP + * + * TODO corresponding enum (defined in l2_vtr.h) should be defined in vpe.api + * (does vpp's IDL support enum type definition?) + * which would allow to generate this class in jvpp + */ +public enum TagRewriteOperation { + disabled(0), + push_1(0), + push_2(0), + pop_1(1), + pop_2(2), + translate_1_to_1(1), + translate_1_to_2(1), + translate_2_to_1(2), + translate_2_to_2(2); + + private final static int MAX_INDEX = 3; + private final int code; + private final byte popTags; + + TagRewriteOperation(final int popTags) { + this.code = this.ordinal(); + this.popTags = UnsignedBytes.checkedCast(popTags); + } + + private static TagRewriteOperation[][] translation = new TagRewriteOperation[][] { + {disabled, push_1, push_2}, + {pop_1, translate_1_to_1, translate_1_to_2}, + {pop_2, translate_2_to_1, translate_2_to_2} + }; + + /** + * Returns VPP tag rewrite operation for given number of tags to pop and tags to push. + * @param toPop number of tags to pop (0..2) + * @param toPush number of tags to push (0..2) + * @return vpp tag rewrite operation for given input parameters + */ + public static TagRewriteOperation get(@Nonnegative final int toPop, @Nonnegative final int toPush) { + Preconditions.checkElementIndex(toPop, MAX_INDEX, "Illegal number of tags to pop"); + Preconditions.checkElementIndex(toPush, MAX_INDEX, "Illegal number of tags to push"); + return translation[toPop][toPush]; + } + + /** + * Returns VPP tag rewrite operation for given operation code. + * @param code VPP tag rewrite operation code + * @return vpp tag rewrite operation for given input parameter + */ + @Nullable + public static TagRewriteOperation get(@Nonnegative final int code) { + for (TagRewriteOperation operation : TagRewriteOperation.values()) { + if (code == operation.code){ + return operation; + } + } + return null; + } + + public byte getPopTags() { + return popTags; + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java new file mode 100644 index 000000000..1a6a50133 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java @@ -0,0 +1,225 @@ +/* + * 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.translate.v3po.util; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.base.Splitter; +import com.google.common.net.InetAddresses; +import java.net.UnknownHostException; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.function.BiConsumer; +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.VppBaseCallException; +import org.openvpp.jvpp.dto.JVppReply; + +public final class TranslateUtils { + + public static final Splitter COLON_SPLITTER = Splitter.on(':'); + public static final int DEFAULT_TIMEOUT_IN_SECONDS = 5; + + private TranslateUtils() { + } + + public static <REP extends JVppReply<?>> REP getReplyForWrite(@Nonnull Future<REP> future, + @Nonnull final InstanceIdentifier<?> replyType) + throws VppBaseCallException, WriteTimeoutException { + return getReplyForWrite(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS); + } + + public static <REP extends JVppReply<?>> REP getReplyForWrite(@Nonnull Future<REP> future, + @Nonnull final InstanceIdentifier<?> replyType, + @Nonnegative final int timeoutInSeconds) + throws VppBaseCallException, WriteTimeoutException { + try { + return getReply(future, timeoutInSeconds); + } catch (TimeoutException e) { + throw new WriteTimeoutException(replyType, e); + } + } + + public static <REP extends JVppReply<?>> REP getReplyForRead(@Nonnull Future<REP> future, + @Nonnull final InstanceIdentifier<?> replyType) + throws VppBaseCallException, ReadTimeoutException { + return getReplyForRead(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS); + } + + public static <REP extends JVppReply<?>> REP getReplyForRead(@Nonnull Future<REP> future, + @Nonnull final InstanceIdentifier<?> replyType, + @Nonnegative final int timeoutInSeconds) + throws VppBaseCallException, ReadTimeoutException { + try { + return getReply(future, timeoutInSeconds); + } catch (TimeoutException e) { + throw new ReadTimeoutException(replyType, e); + } + } + + public static <REP extends JVppReply<?>> REP getReply(@Nonnull Future<REP> future) + throws TimeoutException, VppBaseCallException { + return getReply(future, DEFAULT_TIMEOUT_IN_SECONDS); + } + + public static <REP extends JVppReply<?>> REP getReply(@Nonnull Future<REP> future, + @Nonnegative final int timeoutInSeconds) + throws TimeoutException, VppBaseCallException { + try { + checkArgument(timeoutInSeconds > 0, "Timeout cannot be < 0"); + return future.get(timeoutInSeconds, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IllegalStateException("Interrupted", e); + } catch (ExecutionException e) { + // Execution exception could generally contains any exception + // when using exceptions instead of return codes just rethrow it for processing on corresponding place + if (e instanceof ExecutionException && (e.getCause() instanceof VppBaseCallException)) { + throw (VppBaseCallException) (e.getCause()); + } + throw new IllegalStateException(e); + } + } + + /** + * Transform Ipv4 address to a byte array acceptable by VPP. VPP expects incoming byte array to be in the same order + * as the address. + * + * @return byte array with address bytes + */ + public static byte[] ipv4AddressNoZoneToArray(final Ipv4AddressNoZone ipv4Addr) { + byte[] retval = new byte[4]; + String[] dots = ipv4Addr.getValue().split("\\."); + + for (int d = 0; d < 4; d++) { + retval[d] = (byte) (Short.parseShort(dots[d]) & 0xff); + } + return retval; + } + + /** + * Parse byte array returned by VPP representing an Ipv4 address. Vpp returns IP byte arrays in reversed order. + * + * @return Ipv4AddressNoZone containing string representation of IPv4 address constructed from submitted bytes. No + * change in order. + */ + @Nonnull + public static Ipv4AddressNoZone arrayToIpv4AddressNoZone(@Nonnull byte[] ip) { + // VPP sends ipv4 in a 16 byte array + if (ip.length == 16) { + ip = Arrays.copyOfRange(ip, 0, 4); + } + try { + // Not reversing the byte array here!! because the IP coming from VPP is in reversed byte order + // compared to byte order it was submitted + return new Ipv4AddressNoZone(InetAddresses.toAddrString(InetAddresses.fromLittleEndianByteArray(ip))); + } catch (UnknownHostException e) { + throw new IllegalArgumentException("Unable to parse ipv4", e); + } + } + + /** + * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from + * vpp contain \u0000. + */ + public static String toString(final byte[] cString) { + return new String(cString).replaceAll("\\u0000", "").intern(); + } + + /** + * Parse string represented mac address (using ":" as separator) into a byte array + */ + @Nonnull + public static byte[] parseMac(@Nonnull final String macAddress) { + final List<String> parts = COLON_SPLITTER.splitToList(macAddress); + checkArgument(parts.size() == 6, "Mac address is expected to have 6 parts but was: %s", macAddress); + return parseMacLikeString(parts); + } + + private static byte[] parseMacLikeString(final List<String> strings) { + return strings.stream().limit(6).map(TranslateUtils::parseHexByte).collect( + () -> new byte[strings.size()], + new BiConsumer<byte[], Byte>() { + + private int i = -1; + + @Override + public void accept(final byte[] bytes, final Byte aByte) { + bytes[++i] = aByte; + } + }, + (bytes, bytes2) -> { + throw new UnsupportedOperationException("Parallel collect not supported"); + }); + } + + public static byte parseHexByte(final String aByte) { + return (byte) Integer.parseInt(aByte, 16); + } + + /** + * Returns 0 if argument is null or false, 1 otherwise. + * + * @param value Boolean value to be converted + * @return byte value equal to 0 or 1 + */ + public static byte booleanToByte(@Nullable final Boolean value) { + return value != null && value + ? (byte) 1 + : (byte) 0; + } + + /** + * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise. + * + * @param value byte value to be converted + * @return Boolean value + * @throws IllegalArgumentException if argument is neither 0 nor 1 + */ + @Nonnull + public static Boolean byteToBoolean(final byte value) { + if (value == 0) { + return Boolean.FALSE; + } else if (value == 1) { + return Boolean.TRUE; + } + throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value)); + } + + /** + * Reverses bytes in the byte array + * + * @param bytes input array + * @return reversed array + */ + public static byte[] reverseBytes(final byte[] bytes) { + final byte[] reversed = new byte[bytes.length]; + int i = 1; + for (byte aByte : bytes) { + reversed[bytes.length - i++] = aByte; + } + + return reversed; + } +} diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java new file mode 100644 index 000000000..9516459b2 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java @@ -0,0 +1,33 @@ +/* + * 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.translate.v3po.util; + +import com.google.common.annotations.Beta; +import io.fd.honeycomb.v3po.translate.write.WriteFailedException; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Thrown when write method invocation times out. + */ +@Beta +public class WriteTimeoutException extends WriteFailedException { + + public WriteTimeoutException(final InstanceIdentifier<?> id, final Throwable cause) { + super(id, cause); + } + +} |