/* * Copyright (c) 2015 Huawei, 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.nemo.user.vnspacemanager.structurestyle.updateintent; import com.google.common.base.Optional; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNetwork; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.PhysicalHosts; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.*; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalHostName; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.ConnectionDefinitions; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.MatchItemDefinitions; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.NodeDefinitions; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.definitions.ConnectionDefinition; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.definitions.MatchItemDefinition; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.ActionDefinitions; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.ConditionParameterDefinitions; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.action.definitions.ActionDefinition; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.condition.parameter.definitions.ConditionParameterDefinition; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutionException; /** * Created by z00293636 on 2015/12/3. */ public class GetDefinitions { private DataBroker dataBroker; private List nodeDefinitionList = null; private List connectionDefinitionsList = null; private List matchItemDefinitionList = null; private List conditionParameterDefinitionList = null; private List actionDefinitionList = null; private List physicalHostList = null; private static final Logger LOG = LoggerFactory.getLogger(GetDefinitions.class); public GetDefinitions(DataBroker dataBroker){ this.dataBroker = dataBroker; } public Map getNodeDefinition(){ fetchNodeDefinitions(); Map map = new HashMap(); if (nodeDefinitionList!=null){ for (NodeDefinition nodeDefinition : nodeDefinitionList){ map.put(nodeDefinition.getNodeType(),nodeDefinition); } } return map; } public Map getMatchItemDefinition(){ fetchMatchItemDefinitions(); Map map = new HashMap(); if (matchItemDefinitionList != null){ for (MatchItemDefinition matchItemDefinition : matchItemDefinitionList){ map.put(matchItemDefinition.getMatchItemName(),matchItemDefinition); } } return map; } public Map getConnectionDefinition(){ fetchConnectionDefinitionList(); Map map = new HashMap(); if (connectionDefinitionsList != null){ for (ConnectionDefinition connectionDefinition : connectionDefinitionsList){ map.put(connectionDefinition.getConnectionType(),connectionDefinition); } } return map; } public Map getActionDefinition(){ fetchActionDefinitions(); Map map = new HashMap(); if (actionDefinitionList!=null){ for (ActionDefinition actionDefinition : actionDefinitionList){ map.put(actionDefinition.getActionName(),actionDefinition); } } return map; } public Map getConditionParameterDefinition(){ fetchConditionParaDefinitions(); Map map = new HashMap(); if (conditionParameterDefinitionList!=null){ for (ConditionParameterDefinition conditionParameterDefinition : conditionParameterDefinitionList){ map.put(conditionParameterDefinition.getParameterName(),conditionParameterDefinition); } } return map; } public Map getPhysicalHost(){ fetchPhysicalHosts(); Map map = new HashMap(); if (physicalHostList!=null){ for (PhysicalHost physicalHost : physicalHostList){ map.put(physicalHost.getHostName(),physicalHost); } } return map; } private void setNodeDefinitionsList(List nodeDefinitiones){ this.nodeDefinitionList = nodeDefinitiones; } private void setMatchItemDefintionList(List matchItemDefinitions){ this.matchItemDefinitionList = matchItemDefinitions; } private void setConnectionDefinitionsList(List connectionDefinitions){ this.connectionDefinitionsList = connectionDefinitions; } private void setConditionParameterDefinitionList(List conditionParameterDefinitions){ this.conditionParameterDefinitionList = conditionParameterDefinitions; } private void setActionDefinitionList(List actionDefinitions){ this.actionDefinitionList = actionDefinitions; } private void setPhysicalHosts(List physicalHosts){ this.physicalHostList = physicalHosts; } private void fetchNodeDefinitions(){ InstanceIdentifier nodedefinitionId = InstanceIdentifier.builder(NodeDefinitions.class).build(); ListenableFuture> nodedefinitionFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, nodedefinitionId); Futures.addCallback(nodedefinitionFuture, new FutureCallback>() { @Override public void onSuccess(Optional result){ setNodeDefinitionsList(result.get().getNodeDefinition()); } @Override public void onFailure(Throwable t){ LOG.error("Can not read node definitions information.", t); } }); return ; } private void fetchConnectionDefinitionList(){ InstanceIdentifier connectiondefinitionId = InstanceIdentifier.builder(ConnectionDefinitions.class).build(); ListenableFuture> connectiondefinitionFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, connectiondefinitionId); Futures.addCallback(connectiondefinitionFuture, new FutureCallback>() { @Override public void onSuccess(Optional result) { setConnectionDefinitionsList(result.get().getConnectionDefinition()); } @Override public void onFailure(Throwable t) { LOG.error("Can not read connection definition information.", t); } }); return; } private void fetchMatchItemDefinitions(){ InstanceIdentifier matchitemdefinitionId = InstanceIdentifier.builder(MatchItemDefinitions.class).build(); ListenableFuture> matchitemdefinitionFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, matchitemdefinitionId); Futures.addCallback(matchitemdefinitionFuture, new FutureCallback>() { @Override public void onSuccess(Optional result) { setMatchItemDefintionList(result.get().getMatchItemDefinition()); } @Override public void onFailure(Throwable t) { LOG.error("Can not read match item definition information.", t); } }); return ; } private void fetchActionDefinitions(){ InstanceIdentifier actiondefinitionId = InstanceIdentifier.builder(ActionDefinitions.class).build(); ListenableFuture> actiondefinitionFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, actiondefinitionId); Futures.addCallback(actiondefinitionFuture, new FutureCallback>() { @Override public void onSuccess(Optional result) { setActionDefinitionList(result.get().getActionDefinition()); } @Override public void onFailure(Throwable t) { LOG.error("Can not read action definition information.", t); } }); return ; } private void fetchConditionParaDefinitions(){ InstanceIdentifier conditionparadefinitionId = InstanceIdentifier.builder(ConditionParameterDefinitions.class).build(); ListenableFuture> conditionparadefinitionFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, conditionparadefinitionId); Futures.addCallback(conditionparadefinitionFuture, new FutureCallback>() { @Override public void onSuccess(Optional result) { setConditionParameterDefinitionList(result.get().getConditionParameterDefinition()); } @Override public void onFailure(Throwable t) { LOG.error("Can not read condition parameter definition information.", t); } }); try { conditionparadefinitionFuture.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block LOG.error("Exception:",e); } catch (ExecutionException e){ // TODO Auto-generated catch block LOG.error("Exception:",e); } return ; } private void fetchPhysicalHosts(){ InstanceIdentifier physicalHostsInstanceIdentifier = InstanceIdentifier.builder(PhysicalNetwork.class).child(PhysicalHosts.class).build(); ListenableFuture> physicalHostsFuture = dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL, physicalHostsInstanceIdentifier); Futures.addCallback(physicalHostsFuture, new FutureCallback>() { @Override public void onSuccess(Optional result) { setPhysicalHosts(result.get().getPhysicalHost()); } @Override public void onFailure(Throwable t) { LOG.error("Can not read physical hosts information.", t); } }); return ; } }