/* * 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.impl.trans.util; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; import io.fd.honeycomb.v3po.impl.trans.ChildVppReader; import java.lang.reflect.Method; import java.util.Collections; import java.util.List; import javax.annotation.Nonnull; import org.opendaylight.yangtools.yang.binding.Augmentation; import org.opendaylight.yangtools.yang.binding.ChildOf; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.Identifiable; import org.opendaylight.yangtools.yang.binding.Identifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; public final class VppReaderUtils { private VppReaderUtils() {} /** * Find next item in ID after provided type */ @Nonnull public static InstanceIdentifier.PathArgument getNextId(@Nonnull final InstanceIdentifier id, @Nonnull final InstanceIdentifier type) { // TODO this is inefficient(maybe, depending on actual Iterable type) final Iterable pathArguments = id.getPathArguments(); final int i = Iterables.indexOf(pathArguments, new Predicate() { @Override public boolean apply(final InstanceIdentifier.PathArgument input) { return input.getType().isAssignableFrom(type.getTargetType()); } }); Preconditions.checkArgument(i >= 0, "Unable to find %s type in %s", type.getTargetType(), id); return Iterables.get(pathArguments, i + 1); } public static List>> emptyChildReaderList() { return Collections.emptyList(); } public static List>> emptyAugReaderList() { return Collections.emptyList(); } public static List>> singletonAugReaderList( ChildVppReader> item) { return Collections.>>singletonList(item); } public static List>> singletonChildReaderList( ChildVppReader> item) { return Collections.>>singletonList(item); } /** * Replace last item in ID with a provided IdentifiableItem of the same type */ @SuppressWarnings("unchecked") @Nonnull public static , K extends Identifier> InstanceIdentifier getCurrentId( @Nonnull final InstanceIdentifier id, final InstanceIdentifier.IdentifiableItem currentBdItem) { final Iterable pathArguments = id.getPathArguments(); final Iterable withoutCurrent = Iterables.limit(pathArguments, Iterables.size(pathArguments) - 1); final Iterable concat = Iterables.concat(withoutCurrent, Collections.singleton(currentBdItem)); return (InstanceIdentifier) InstanceIdentifier.create(concat); } /** * Create IdentifiableItem from target type of provided ID with provided key */ @Nonnull public static , K extends Identifier> InstanceIdentifier.IdentifiableItem getCurrentIdItem( @Nonnull final InstanceIdentifier id, final K key) { return new InstanceIdentifier.IdentifiableItem<>(id.getTargetType(), key); } /** * Trim InstanceIdentifier at indexOf(type) */ @SuppressWarnings("unchecked") @Nonnull public static InstanceIdentifier cutId(@Nonnull final InstanceIdentifier id, @Nonnull final InstanceIdentifier type) { final Iterable pathArguments = id.getPathArguments(); final int i = Iterables.indexOf(pathArguments, new Predicate() { @Override public boolean apply(final InstanceIdentifier.PathArgument input) { return input.getType().equals(type.getTargetType()); } }); Preconditions.checkArgument(i >= 0, "ID %s does not contain %s", id, type); return (InstanceIdentifier) InstanceIdentifier.create(Iterables.limit(pathArguments, i + 1)); } /** * Find a specific method using reflection */ @Nonnull public static Optional findMethodReflex(@Nonnull final Class managedType, @Nonnull final String prefix, @Nonnull final List> paramTypes, @Nonnull final Class retType) { top: for (Method method : managedType.getMethods()) { if (!method.getName().toLowerCase().startsWith(prefix.toLowerCase())) { continue; } final Class[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length != paramTypes.size()) { continue; } for (int i = 0; i < parameterTypes.length; i++) { if (!parameterTypes[i].isAssignableFrom(paramTypes.get(i))) { continue top; } } if (!method.getReturnType().equals(retType)) { continue; } return Optional.of(method); } return Optional.absent(); } }