/* * Copyright © 2021 Nokia, 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.transportpce.tapi.topology; import com.google.common.collect.ImmutableClassToInstanceMap; import java.nio.charset.Charset; import java.util.Map; import java.util.UUID; import java.util.concurrent.ExecutionException; import org.opendaylight.mdsal.binding.api.RpcProviderService; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.transportpce.common.network.NetworkTransactionService; import org.opendaylight.transportpce.tapi.TapiStringConstants; import org.opendaylight.transportpce.tapi.impl.rpc.DeleteTapiLinkImpl; import org.opendaylight.transportpce.tapi.impl.rpc.InitRoadmRoadmTapiLinkImpl; import org.opendaylight.transportpce.tapi.impl.rpc.InitXpdrRdmTapiLinkImpl; import org.opendaylight.transportpce.tapi.utils.TapiLink; import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.DeleteTapiLink; import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.InitRoadmRoadmTapiLink; import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.InitXpdrRdmTapiLink; import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context; import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid; import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1; import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext; import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Link; import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology; import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyBuilder; import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyKey; import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.Rpc; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Component public class TapiNetworkUtilsImpl { private static final Logger LOG = LoggerFactory.getLogger(TapiNetworkUtilsImpl.class); private final Uuid tapiTopoUuid = new Uuid(UUID.nameUUIDFromBytes(TapiStringConstants.T0_FULL_MULTILAYER .getBytes(Charset.forName("UTF-8"))).toString()); private final NetworkTransactionService networkTransactionService; private Registration reg; @Activate public TapiNetworkUtilsImpl(@Reference RpcProviderService rpcProviderService, @Reference NetworkTransactionService networkTransactionService, @Reference TapiLink tapiLink) { this.networkTransactionService = networkTransactionService; this.reg = rpcProviderService.registerRpcImplementations(ImmutableClassToInstanceMap.>builder() .put(InitRoadmRoadmTapiLink.class, new InitRoadmRoadmTapiLinkImpl(tapiLink, this)) .put(InitXpdrRdmTapiLink.class, new InitXpdrRdmTapiLinkImpl(tapiLink, null)) .put(DeleteTapiLink.class, new DeleteTapiLinkImpl(this, networkTransactionService)) .build()); LOG.info("TapiNetworkUtilsImpl instantiated"); } @Deactivate public void close() { this.reg.close(); LOG.info("TapiNetworkUtilsImpl Closed"); } public Registration getRegisteredRpc() { return reg; } public Uuid getTapiTopoUuid() { return tapiTopoUuid; } public boolean putLinkInTopology(Link tapLink) { // TODO is this merge correct? Should we just merge topology by changing the nodes map?? // TODO: verify this is correct. Should we identify the context IID with the context UUID?? LOG.info("Creating tapi node in TAPI topology context"); InstanceIdentifier topoIID = InstanceIdentifier.builder(Context.class) .augmentation(Context1.class).child(TopologyContext.class) .child(Topology.class, new TopologyKey(tapiTopoUuid)) .build(); Topology topology = new TopologyBuilder().setUuid(tapiTopoUuid) .setLink(Map.of(tapLink.key(), tapLink)).build(); // merge in datastore this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topoIID, topology); try { this.networkTransactionService.commit().get(); } catch (InterruptedException | ExecutionException e) { LOG.error("Error populating TAPI topology: ", e); return false; } LOG.info("TAPI Link added succesfully."); return true; } }