Adaptation of XPDR mapping from OR to TAPI
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / utils / TapiInitialORMapping.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.utils;
9
10 import java.util.Comparator;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Optional;
14 import java.util.TreeMap;
15 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
16 import org.opendaylight.transportpce.tapi.connectivity.ConnectivityUtils;
17 import org.opendaylight.transportpce.tapi.topology.TapiTopologyException;
18 import org.opendaylight.transportpce.tapi.topology.TopologyUtils;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.Service;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceList;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.ServicesKey;
23 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.tapi.context.ServiceInterfacePoint;
24 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.tapi.context.ServiceInterfacePointKey;
25 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectivityService;
26 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.connectivity.context.ConnectivityServiceKey;
27 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.Topology;
28 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.topology.context.TopologyKey;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class TapiInitialORMapping {
33
34     private static final Logger LOG = LoggerFactory.getLogger(TapiInitialORMapping.class);
35     private final TapiContext tapiContext;
36     private final TopologyUtils topologyUtils;
37     private final ConnectivityUtils connectivityUtils;
38     private final ServiceDataStoreOperations serviceDataStoreOperations;
39
40     public TapiInitialORMapping(TopologyUtils topologyUtils, ConnectivityUtils connectivityUtils,
41                                 TapiContext tapiContext, ServiceDataStoreOperations serviceDataStoreOperations) {
42         this.topologyUtils = topologyUtils;
43         this.tapiContext = tapiContext;
44         this.connectivityUtils = connectivityUtils;
45         this.serviceDataStoreOperations = serviceDataStoreOperations;
46     }
47
48     public void performTopoInitialMapping() {
49         // creation of both topologies but with the fully roadm infrastructure.
50         try {
51             LOG.info("Performing initial mapping between OR and TAPI models.");
52             Topology t0FullMultiLayer = this.topologyUtils.createFullOtnTopology();
53             Map<TopologyKey, Topology> topologyMap = new HashMap<>();
54             topologyMap.put(t0FullMultiLayer.key(), t0FullMultiLayer);
55             this.tapiContext.updateTopologyContext(topologyMap);
56             Map<ServiceInterfacePointKey, ServiceInterfacePoint> sipMap = this.topologyUtils.getSipMap();
57             this.tapiContext.updateSIPContext(sipMap);
58             this.connectivityUtils.setSipMap(sipMap);
59         } catch (TapiTopologyException e) {
60             LOG.error("error building TAPI topology", e);
61         }
62     }
63
64     public void performServInitialMapping() {
65         Optional<ServiceList> optOrServices = this.serviceDataStoreOperations.getServices();
66         if (!optOrServices.isPresent()) {
67             LOG.error("Couldnt obtain OR services from datastore");
68             return;
69         }
70         ServiceList orServices = optOrServices.get();
71         if (orServices.getServices() == null) {
72             LOG.info("No services in datastore. No mapping needed");
73             return;
74         }
75         /*
76         Map<ServicesKey, Services> orderedServices = orServices.getServices().entrySet().stream()
77             .sorted(Comparator.comparing(serv -> serv.getValue().getServiceAEnd().getServiceFormat().getName()))
78             .collect(Collectors.toMap(Map.Entry::getKey,
79                 Map.Entry::getValue, (left, right) -> left, LinkedHashMap::new));
80
81          */
82         Map<ServicesKey, Services> orderedServices = new TreeMap<>(Comparator.comparing(s ->
83             orServices.getServices().get(s).getServiceAEnd().getServiceFormat().getName()).reversed());
84         orderedServices.putAll(orServices.getServices());
85         LOG.info("orderedServices = {}", orderedServices);
86         // TODO order services correctly. First OTU, then ODU and then DSR
87         Map<ConnectivityServiceKey, ConnectivityService> connServMap = new HashMap<>();
88         for (Service service:orderedServices.values()) {
89             // map services
90             // connections needed to be created --> looking at path description
91             ConnectivityService connServ = this.connectivityUtils.mapORServiceToTapiConnectivity(service);
92             connServMap.put(connServ.key(), connServ);
93         }
94         // Put in datastore connectivity services and connections
95         this.tapiContext.updateConnectivityContext(connServMap, this.connectivityUtils.getConnectionFullMap());
96     }
97 }