2964c2a6e6f8a0cac89e05d9c6301c71c51c3594
[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.rev230526.service.create.input.ServiceAEnd;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.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().orElseThrow().getPort()
37                         .getPortDeviceName(),
38                     sep.getEndPoint().getTxDirection().values().stream().findFirst().orElseThrow().getPort()
39                         .getPortName(),
40                     sep.getEndPoint().getRxDirection().values().stream().findFirst().orElseThrow().getPort()
41                         .getPortDeviceName(),
42                     sep.getEndPoint().getRxDirection().values().stream().findFirst().orElseThrow().getPort()
43                         .getPortName());
44             return new GenericServiceEndpoint(sepG, ServiceEndpointType.SERVICEAEND);
45         } else {
46             ServiceZEnd sepG = ConnectivityUtils.buildServiceZEnd(sep.getEndPoint().getNodeId().getValue(),
47                     sep.getEndPoint().getClli(),
48                     sep.getEndPoint().getTxDirection().values().stream().findFirst().orElseThrow().getPort()
49                         .getPortDeviceName(),
50                     sep.getEndPoint().getTxDirection().values().stream().findFirst().orElseThrow().getPort()
51                         .getPortName(),
52                     sep.getEndPoint().getRxDirection().values().stream().findFirst().orElseThrow().getPort()
53                         .getPortDeviceName(),
54                     sep.getEndPoint().getRxDirection().values().stream().findFirst().orElseThrow().getPort()
55                         .getPortName());
56             return new GenericServiceEndpoint(sepG, ServiceEndpointType.SERVICEZEND);
57         }
58     }
59
60     public static void addMapSEP(ServiceEndPoint sep) {
61         Uuid uuid = sep.getUuid();
62         if (!map.containsKey(uuid)) {
63             LOG.info("adding new entry {} into static map", uuid);
64             map.put(uuid, MappingUtils.createMapEntry(sep));
65         } else {
66             LOG.error("service-end-point entry already exist. Please DELETE it before PUT it...");
67         }
68     }
69
70     public static void deleteMapEntry(Uuid uuid) {
71         if (map.containsKey(uuid)) {
72             LOG.info("map size = {}", Integer.toString(map.size()));
73             map.remove(uuid);
74             LOG.info("map size = {}", Integer.toString(map.size()));
75         } else {
76             LOG.info("No existing entry with uuid {}", uuid);
77         }
78     }
79
80     public static void afficheMap() {
81         if (!map.isEmpty()) {
82             Set<Entry<Uuid, GenericServiceEndpoint>> entries = map.entrySet();
83             LOG.info("Displaying the static map...");
84             for (Entry<Uuid, GenericServiceEndpoint> entry : entries) {
85                 LOG.info("uuid = {} \t service end point = {}", entry.getKey(), entry.getValue().getValue());
86             }
87         } else {
88             LOG.info("Static map is empty - Nothing to display");
89         }
90
91     }
92
93     public static void deleteMap() {
94         if (!map.isEmpty()) {
95             map.clear();
96         } else {
97             LOG.info("Static map is already empty - Nothing to delete");
98         }
99     }
100
101     public static Map<Uuid, GenericServiceEndpoint> getMap() {
102         return map;
103     }
104
105 }