Service-restoration implementation
[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.List;
11
12 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.CoRoutingOrGeneral;
13 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.CoRouting;
14 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.CoRoutingBuilder;
15 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.General;
16 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.GeneralBuilder;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.general.Diversity;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.general.DiversityBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.general.Exclude;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.general.ExcludeBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.general.Include;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.general.IncludeBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.general.Latency;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.routing.constraints.HardConstraints;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.routing.constraints.HardConstraintsBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.routing.constraints.SoftConstraints;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.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         List<String> hardCustomerCode = hardConstraints.getCustomerCode();
56         if (!hardCustomerCode.isEmpty()) {
57             softConstraintsBuilder.getCustomerCode().addAll(hardCustomerCode);
58         } else {
59             LOG.warn("hard constraints customer code list is empty !");
60         }
61         CoRoutingOrGeneral coRoutingOrGeneral = hardConstraints.getCoRoutingOrGeneral();
62         if (coRoutingOrGeneral != null) {
63             if (coRoutingOrGeneral instanceof General) {
64                 General hardGeneral = (General) coRoutingOrGeneral;
65                 if (softConstraintsBuilder.getCoRoutingOrGeneral() instanceof General) {
66                     General softGeneral = (General) softConstraintsBuilder.getCoRoutingOrGeneral();
67                     updateGeneral(hardGeneral, softGeneral);
68                 } else {
69                     softConstraintsBuilder.setCoRoutingOrGeneral(hardGeneral);
70                 }
71             } else if (coRoutingOrGeneral instanceof CoRouting) {
72                 CoRouting hardCoRouting = (CoRouting) coRoutingOrGeneral;
73                 if (softConstraintsBuilder.getCoRoutingOrGeneral() instanceof CoRouting) {
74                     CoRouting softCoRouting = (CoRouting) softConstraintsBuilder.getCoRoutingOrGeneral();
75                     updateCoRouting(hardCoRouting, softCoRouting);
76                 } else {
77                     softConstraintsBuilder.setCoRoutingOrGeneral(hardCoRouting);
78                 }
79             }
80         } else {
81             LOG.warn("hard constraints CoRoutingOrGeneral is null !");
82         }
83         return softConstraintsBuilder.build();
84     }
85
86     private static General updateGeneral(General hard, General soft) {
87         GeneralBuilder result = new GeneralBuilder(soft);
88         try {
89             result.setDiversity(updateDiveristy(hard.getDiversity(), soft.getDiversity()));
90             result.setExclude(updateExclude(hard.getExclude(), soft.getExclude()));
91             result.setInclude(updateInclude(hard.getInclude(), soft.getInclude()));
92         } catch (NullPointerException e) {
93             LOG.warn("failed to update some 'General' parameters !", e);
94         }
95         return result.build();
96     }
97
98     private static Include updateInclude(Include hard, Include soft) {
99         IncludeBuilder result = new IncludeBuilder(soft);
100         if (hard != null) {
101             result.getFiberBundle().addAll(hard.getFiberBundle());
102             result.getNodeId().addAll(hard.getNodeId());
103             result.getSite().addAll(hard.getSite());
104             result.getSupportingServiceName().addAll(hard.getSupportingServiceName());
105         }
106         return result.build();
107     }
108
109     private static Exclude updateExclude(Exclude hard, Exclude soft) throws NullPointerException {
110         ExcludeBuilder result = new ExcludeBuilder(soft);
111         if (hard != null) {
112             result.getFiberBundle().addAll(hard.getFiberBundle());
113             result.getNodeId().addAll(hard.getNodeId());
114             result.getSite().addAll(hard.getSite());
115             result.getSupportingServiceName().addAll(hard.getSupportingServiceName());
116         }
117         return result.build();
118     }
119
120     private static Diversity updateDiveristy(Diversity hard, Diversity soft) throws NullPointerException {
121         DiversityBuilder result = new DiversityBuilder(soft);
122         if (hard != null) {
123             result.getExistingService().addAll(hard.getExistingService());
124             result.setExistingServiceApplicability(hard.getExistingServiceApplicability());
125         }
126         return result.build();
127     }
128
129     private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) {
130         CoRoutingBuilder result = new CoRoutingBuilder(soft);
131         try {
132             result.setCoRouting(
133                     updateCoCoRouting(hard.getCoRouting(), soft.getCoRouting()));
134         } catch (NullPointerException e) {
135             LOG.warn("failed to update some 'CoRouting' parameters !", e);
136         }
137         return result.build();
138     }
139
140     private static org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing
141         .or.general.co.routing.CoRouting updateCoCoRouting(org.opendaylight.yang.gen.v1.http.org.openroadm.routing
142                 .constrains.rev161014.constraints.co.routing.or.general.co.routing.CoRouting hard, org.opendaylight
143                     .yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general.co
144                         .routing.CoRouting soft) throws NullPointerException {
145         org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constrains.rev161014.constraints.co.routing.or.general
146             .co.routing.CoRoutingBuilder result = new org.opendaylight.yang.gen.v1.http.org.openroadm.routing
147                 .constrains.rev161014.constraints.co.routing.or.general.co.routing.CoRoutingBuilder(soft);
148         if (hard != null) {
149             result.getExistingService().addAll(hard.getExistingService());
150         }
151         return result.build();
152     }
153
154     /**
155      * Remove all hard constraints except latency.
156      *
157      * @param hardConstraints HardConstarints to be downgraded
158      * @return HardConstraints downgraded
159      */
160     public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) {
161         HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder();
162         CoRoutingOrGeneral coRoutingOrGeneral = hardConstraints.getCoRoutingOrGeneral();
163         Latency latency = null;
164         if (coRoutingOrGeneral instanceof General) {
165             General general = (General) coRoutingOrGeneral;
166             if (general != null) {
167                 latency = general.getLatency();
168                 if (latency != null) {
169                     hardConstraintsBuilder.setCoRoutingOrGeneral(new GeneralBuilder().setLatency(latency).build());
170                 } else {
171                     LOG.warn("latency value not found in HardContraints !");
172                 }
173             }
174         }
175         return hardConstraintsBuilder.build();
176     }
177
178     /**
179      * Convert HardConstraints to SoftConstraints.
180      *
181      * @param hardConstraints to be converted.
182      * @return SoftConstraints converted.
183      */
184     public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) {
185         SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(hardConstraints);
186         return softConstraintsBuilder.build();
187     }
188 }