83402578ac99cf736dfac90603d181c47156fbe3
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / DowngradeConstraints.java
1 /*
2  * Copyright © 2017 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.servicehandler;
9
10 import java.util.HashMap;
11 import java.util.Map;
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRoutingBuilder;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Diversity;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DiversityBuilder;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.ExcludeBuilder;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.IncludeBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Latency;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.LatencyBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierList;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierListKey;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraintsBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Class to Map Hard Constraints to Soft Constraints.
33  *
34  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
35  *
36  */
37 public final class DowngradeConstraints {
38
39     private static final Logger LOG = LoggerFactory.getLogger(DowngradeConstraints.class);
40
41     private DowngradeConstraints() {
42     }
43
44     /**
45      * Add Hard Constraints to Soft Constraints.
46      *
47      *
48      * @param hardConstraints to be added
49      * @param softConstraints to be modified
50      * @return SoftConstraints modified
51      */
52     public static SoftConstraints updateSoftConstraints(HardConstraints hardConstraints,
53             SoftConstraints softConstraints) {
54         SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(softConstraints);
55         if (hardConstraints.getCustomerCode() != null) {
56             if (!hardConstraints.getCustomerCode().isEmpty()) {
57                 softConstraintsBuilder.getCustomerCode().addAll(hardConstraints.getCustomerCode());
58             }
59         }
60         if (hardConstraints.getOperationalMode() != null) {
61             if (!hardConstraints.getOperationalMode().isEmpty()) {
62                 softConstraintsBuilder.getOperationalMode().addAll(hardConstraints.getOperationalMode());
63             }
64         }
65         if (hardConstraints.getDiversity() != null) {
66             if (softConstraints.getDiversity() != null) {
67                 softConstraintsBuilder
68                     .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity()));
69             }
70         }
71         if (hardConstraints.getExclude() != null) {
72             if (softConstraints.getExclude() != null) {
73                 softConstraintsBuilder
74                     .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude()));
75             }
76         }
77         if (hardConstraints.getInclude() != null) {
78             if (softConstraints.getInclude() != null) {
79                 softConstraintsBuilder
80                     .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude()));
81             }
82         }
83         if (hardConstraints.getLatency() != null) {
84             if (softConstraints.getLatency() != null) {
85                 softConstraintsBuilder
86                     .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency()));
87             }
88         }
89         if (hardConstraints.getCoRouting() != null) {
90             if (softConstraints.getCoRouting() != null) {
91                 softConstraintsBuilder
92                     .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting()));
93             }
94         }
95         return softConstraintsBuilder.build();
96     }
97
98     private static Include updateInclude(Include hard, Include soft) {
99         IncludeBuilder includeBldr = new IncludeBuilder(soft);
100         includeBldr.getFiberBundle().addAll(hard.getFiberBundle());
101         includeBldr.getNodeId().addAll(hard.getNodeId());
102         includeBldr.getSite().addAll(hard.getSite());
103         includeBldr.getSupportingServiceName().addAll(hard.getSupportingServiceName());
104         return includeBldr.build();
105     }
106
107     private static Exclude updateExclude(Exclude hard, Exclude soft) {
108         ExcludeBuilder excludeBldr = new ExcludeBuilder(soft);
109         excludeBldr.getFiberBundle().addAll(hard.getFiberBundle());
110         excludeBldr.getNodeId().addAll(hard.getNodeId());
111         excludeBldr.getSite().addAll(hard.getSite());
112         excludeBldr.getSupportingServiceName().addAll(hard.getSupportingServiceName());
113         return excludeBldr.build();
114     }
115
116     private static Diversity updateDiveristy(Diversity hard, Diversity soft) {
117         DiversityBuilder diversityBldr = new DiversityBuilder(soft);
118         if (!hard.getServiceIdentifierList().isEmpty()) {
119             Map<org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
120                     .constraints.ServiceIdentifierListKey,
121                 org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service
122                     .constraints.ServiceIdentifierList> sil = new HashMap<>(diversityBldr.getServiceIdentifierList());
123             sil.putAll(hard.getServiceIdentifierList());
124             diversityBldr.setServiceIdentifierList(sil);
125         }
126         return diversityBldr.build();
127     }
128
129     private static Latency updateLatency(Latency hard, Latency soft) {
130         LatencyBuilder latencyBldr = new LatencyBuilder(soft);
131         if (hard.getMaxLatency() != null) {
132             latencyBldr.setMaxLatency(hard.getMaxLatency());
133         }
134         return latencyBldr.build();
135     }
136
137     private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
138         CoRoutingBuilder coRoutingBldr = new CoRoutingBuilder(soft);
139         Map<ServiceIdentifierListKey, ServiceIdentifierList> serviceIdentifierList
140             = new HashMap<ServiceIdentifierListKey, ServiceIdentifierList>(coRoutingBldr.getServiceIdentifierList());
141         serviceIdentifierList.putAll(hard.getServiceIdentifierList());
142         return coRoutingBldr
143             .setServiceIdentifierList(serviceIdentifierList)
144             .build();
145     }
146
147     /**
148      * Remove all hard constraints except latency.
149      *
150      * @param hardConstraints HardConstarints to be downgraded
151      * @return HardConstraints downgraded
152      */
153     public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
154         HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
155         if (hardConstraints != null && hardConstraints.getLatency() != null) {
156             hardConstraintsBuilder.setLatency(hardConstraints.getLatency());
157         } else {
158             LOG.warn("latency value not found in HardContraints !");
159         }
160         return hardConstraintsBuilder.build();
161     }
162
163     /**
164      * Convert HardConstraints to SoftConstraints.
165      *
166      * @param hardConstraints to be converted.
167      * @return SoftConstraints converted.
168      */
169     public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
170         SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(hardConstraints);
171         return softConstraintsBuilder.build();
172     }
173 }