Convert TapiLinkImpl into a Component
[transportpce.git] / tapi / src / main / java / org / opendaylight / transportpce / tapi / utils / MappingUtils.java
1 /*
2  * Copyright © 2018 Orange, 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.HashMap;
11 import java.util.Map;
12 import java.util.Map.Entry;
13 import java.util.Set;
14 import org.opendaylight.transportpce.tapi.connectivity.ConnectivityUtils;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.create.input.ServiceAEnd;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.create.input.ServiceZEnd;
17 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.common.rev181210.Uuid;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.tapi.rev180928.EndPointType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.tapi.rev180928.service._interface.points.ServiceEndPoint;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public final class MappingUtils {
24
25     private static final Logger LOG = LoggerFactory.getLogger(MappingUtils.class);
26
27     private static Map<Uuid, GenericServiceEndpoint> map = new HashMap<>();
28
29     private MappingUtils() {
30     }
31
32     private static GenericServiceEndpoint createMapEntry(ServiceEndPoint sep) {
33         if (sep.getEndPoint().getServiceEndPointType().equals(EndPointType.Aend)) {
34             ServiceAEnd sepG = ConnectivityUtils.buildServiceAEnd(sep.getEndPoint().getNodeId().getValue(),
35                     sep.getEndPoint().getClli(),
36                     sep.getEndPoint().getTxDirection().values().stream().findFirst().get().getPort()
37                         .getPortDeviceName(),
38                     sep.getEndPoint().getTxDirection().values().stream().findFirst().get().getPort().getPortName(),
39                     sep.getEndPoint().getRxDirection().values().stream().findFirst().get().getPort()
40                         .getPortDeviceName(),
41                     sep.getEndPoint().getRxDirection().values().stream().findFirst().get().getPort().getPortName());
42             return new GenericServiceEndpoint(sepG, ServiceEndpointType.SERVICEAEND);
43         } else {
44             ServiceZEnd sepG = ConnectivityUtils.buildServiceZEnd(sep.getEndPoint().getNodeId().getValue(),
45                     sep.getEndPoint().getClli(),
46                     sep.getEndPoint().getTxDirection().values().stream().findFirst().get().getPort()
47                         .getPortDeviceName(),
48                     sep.getEndPoint().getTxDirection().values().stream().findFirst().get().getPort().getPortName(),
49                     sep.getEndPoint().getRxDirection().values().stream().findFirst().get().getPort()
50                         .getPortDeviceName(),
51                     sep.getEndPoint().getRxDirection().values().stream().findFirst().get().getPort().getPortName());
52             return new GenericServiceEndpoint(sepG, ServiceEndpointType.SERVICEZEND);
53         }
54     }
55
56     public static void addMapSEP(ServiceEndPoint sep) {
57         Uuid uuid = sep.getUuid();
58         if (!map.containsKey(uuid)) {
59             LOG.info("adding new entry {} into static map", uuid);
60             map.put(uuid, MappingUtils.createMapEntry(sep));
61         } else {
62             LOG.error("service-end-point entry already exist. Please DELETE it before PUT it...");
63         }
64     }
65
66     public static void deleteMapEntry(Uuid uuid) {
67         if (map.containsKey(uuid)) {
68             LOG.info("map size = {}", Integer.toString(map.size()));
69             map.remove(uuid);
70             LOG.info("map size = {}", Integer.toString(map.size()));
71         } else {
72             LOG.info("No existing entry with uuid {}", uuid);
73         }
74     }
75
76     public static void afficheMap() {
77         if (!map.isEmpty()) {
78             Set<Entry<Uuid, GenericServiceEndpoint>> entries = map.entrySet();
79             LOG.info("Displaying the static map...");
80             for (Entry<Uuid, GenericServiceEndpoint> entry : entries) {
81                 LOG.info("uuid = {} \t service end point = {}", entry.getKey(), entry.getValue().getValue());
82             }
83         } else {
84             LOG.info("Static map is empty - Nothing to display");
85         }
86
87     }
88
89     public static void deleteMap() {
90         if (!map.isEmpty()) {
91             map.clear();
92         } else {
93             LOG.info("Static map is already empty - Nothing to delete");
94         }
95     }
96
97     public static Map<Uuid, GenericServiceEndpoint> getMap() {
98         return map;
99     }
100
101 }