Bump upstream dependencies to Ca
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / topology / TapiNetworkUtilsImpl.java
1 /*
2  * Copyright © 2021 Nokia, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.tapi.topology;
9
10 import com.google.common.collect.ImmutableClassToInstanceMap;
11 import java.nio.charset.Charset;
12 import java.util.Map;
13 import java.util.UUID;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.mdsal.binding.api.RpcProviderService;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
18 import org.opendaylight.transportpce.tapi.TapiStringConstants;
19 import org.opendaylight.transportpce.tapi.impl.rpc.DeleteTapiLinkImpl;
20 import org.opendaylight.transportpce.tapi.impl.rpc.InitRoadmRoadmTapiLinkImpl;
21 import org.opendaylight.transportpce.tapi.impl.rpc.InitXpdrRdmTapiLinkImpl;
22 import org.opendaylight.transportpce.tapi.utils.TapiLink;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.DeleteTapiLink;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.InitRoadmRoadmTapiLink;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.tapinetworkutils.rev230728.InitXpdrRdmTapiLink;
26 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Context;
27 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev221121.Uuid;
28 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.Context1;
29 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.context.TopologyContext;
30 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.Link;
31 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.Topology;
32 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyBuilder;
33 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev221121.topology.context.TopologyKey;
34 import org.opendaylight.yangtools.concepts.Registration;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.Rpc;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Deactivate;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 @Component
45 public class TapiNetworkUtilsImpl {
46
47     private static final Logger LOG = LoggerFactory.getLogger(TapiNetworkUtilsImpl.class);
48     private final Uuid tapiTopoUuid = new Uuid(UUID.nameUUIDFromBytes(TapiStringConstants.T0_FULL_MULTILAYER
49             .getBytes(Charset.forName("UTF-8"))).toString());
50
51     private final NetworkTransactionService networkTransactionService;
52     private Registration reg;
53
54     @Activate
55     public TapiNetworkUtilsImpl(@Reference RpcProviderService rpcProviderService,
56             @Reference NetworkTransactionService networkTransactionService, @Reference TapiLink tapiLink) {
57         this.networkTransactionService = networkTransactionService;
58         this.reg = rpcProviderService.registerRpcImplementations(ImmutableClassToInstanceMap.<Rpc<?, ?>>builder()
59             .put(InitRoadmRoadmTapiLink.class, new InitRoadmRoadmTapiLinkImpl(tapiLink, this))
60             .put(InitXpdrRdmTapiLink.class, new InitXpdrRdmTapiLinkImpl(tapiLink, null))
61             .put(DeleteTapiLink.class, new DeleteTapiLinkImpl(this, networkTransactionService))
62             .build());
63         LOG.info("TapiNetworkUtilsImpl instantiated");
64     }
65
66     @Deactivate
67     public void close() {
68         this.reg.close();
69         LOG.info("TapiNetworkUtilsImpl Closed");
70     }
71
72
73     public Registration getRegisteredRpc() {
74         return reg;
75     }
76
77     public Uuid getTapiTopoUuid() {
78         return tapiTopoUuid;
79     }
80
81     public boolean putLinkInTopology(Link tapLink) {
82         // TODO is this merge correct? Should we just merge topology by changing the nodes map??
83         // TODO: verify this is correct. Should we identify the context IID with the context UUID??
84         LOG.info("Creating tapi node in TAPI topology context");
85         InstanceIdentifier<Topology> topoIID = InstanceIdentifier.builder(Context.class)
86             .augmentation(Context1.class).child(TopologyContext.class)
87             .child(Topology.class, new TopologyKey(tapiTopoUuid))
88             .build();
89
90         Topology topology = new TopologyBuilder().setUuid(tapiTopoUuid)
91             .setLink(Map.of(tapLink.key(), tapLink)).build();
92
93         // merge in datastore
94         this.networkTransactionService.merge(LogicalDatastoreType.OPERATIONAL, topoIID,
95             topology);
96         try {
97             this.networkTransactionService.commit().get();
98
99         } catch (InterruptedException | ExecutionException e) {
100             LOG.error("Error populating TAPI topology: ", e);
101             return false;
102         }
103         LOG.info("TAPI Link added succesfully.");
104         return true;
105     }
106 }