/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl; import java.lang.reflect.Method; import java.util.concurrent.ExecutionException; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext; import org.opendaylight.ovsdb.openstack.netvirt.translator.INeutronObject; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.CheckedFuture; public abstract class AbstractNeutronInterface implements AutoCloseable { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractNeutronInterface.class); private static final int DEDASHED_UUID_LENGTH = 32; private static final int DEDASHED_UUID_START = 0; private static final int DEDASHED_UUID_DIV1 = 8; private static final int DEDASHED_UUID_DIV2 = 12; private static final int DEDASHED_UUID_DIV3 = 16; private static final int DEDASHED_UUID_DIV4 = 20; private DataBroker db; AbstractNeutronInterface(ProviderContext providerContext) { this.db = providerContext.getSALService(DataBroker.class); } public DataBroker getDataBroker() { return db; } protected abstract InstanceIdentifier createInstanceIdentifier(T item); protected abstract T toMd(S neutronObject); protected abstract T toMd(String uuid); protected B readMd(InstanceIdentifier path) { B result = null; final ReadOnlyTransaction transaction = getDataBroker().newReadOnlyTransaction(); CheckedFuture, ReadFailedException> future = transaction.read(LogicalDatastoreType.CONFIGURATION, path); if (future != null) { try { result = future.checkedGet().orNull(); } catch (ReadFailedException e) { LOGGER.warn("Failed to read {}", path, e); } } transaction.close(); return result; } protected boolean addMd(S neutronObject) { // TODO think about adding existence logic return updateMd(neutronObject); } protected boolean updateMd(S neutronObject) { WriteTransaction transaction = getDataBroker().newWriteOnlyTransaction(); T item = toMd(neutronObject); InstanceIdentifier iid = createInstanceIdentifier(item); transaction.put(LogicalDatastoreType.CONFIGURATION, iid, item,true); CheckedFuture future = transaction.submit(); try { future.get(); } catch (InterruptedException | ExecutionException e) { LOGGER.warn("Transation failed ",e); return false; } return true; } protected boolean removeMd(T item) { WriteTransaction transaction = getDataBroker().newWriteOnlyTransaction(); InstanceIdentifier iid = createInstanceIdentifier(item); transaction.delete(LogicalDatastoreType.CONFIGURATION, iid); CheckedFuture future = transaction.submit(); try { future.get(); } catch (InterruptedException | ExecutionException e) { LOGGER.warn("Transation failed ",e); return false; } return true; } protected Uuid toUuid(String uuid) { Preconditions.checkNotNull(uuid); Uuid result; try { result = new Uuid(uuid); } catch(IllegalArgumentException e) { // OK... someone didn't follow RFC 4122... lets try this the hard way String dedashed = uuid.replace("-", ""); if(dedashed.length() == DEDASHED_UUID_LENGTH) { String redashed = dedashed.substring(DEDASHED_UUID_START, DEDASHED_UUID_DIV1) + "-" + dedashed.substring(DEDASHED_UUID_DIV1, DEDASHED_UUID_DIV2) + "-" + dedashed.substring(DEDASHED_UUID_DIV2, DEDASHED_UUID_DIV3) + "-" + dedashed.substring(DEDASHED_UUID_DIV3, DEDASHED_UUID_DIV4) + "-" + dedashed.substring(DEDASHED_UUID_DIV4, DEDASHED_UUID_LENGTH); result = new Uuid(redashed); } else { throw e; } } return result; } // this method uses reflection to update an object from it's delta. protected boolean overwrite(Object target, Object delta) { Method[] methods = target.getClass().getMethods(); for(Method toMethod: methods){ if(toMethod.getDeclaringClass().equals(target.getClass()) && toMethod.getName().startsWith("set")){ String toName = toMethod.getName(); String fromName = toName.replace("set", "get"); try { Method fromMethod = delta.getClass().getMethod(fromName); Object value = fromMethod.invoke(delta, (Object[])null); if(value != null){ toMethod.invoke(target, value); } } catch (Exception e) { LOGGER.error("Error in overwrite", e); return false; } } } return true; } @Override public void close() throws Exception { // TODO Auto-generated method stub } }