Merge changes I453a92fd,I68ba6341
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / constraints / OperatorConstraints.java
1 /*
2  * Copyright © 2024 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.pce.constraints;
9
10 import java.util.BitSet;
11 import java.util.Map;
12 import java.util.concurrent.ExecutionException;
13 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
14 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
15 import org.opendaylight.transportpce.common.fixedflex.GridUtils;
16 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
17 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.optical.channel.types.rev230526.FrequencyTHz;
18 import org.opendaylight.yang.gen.v1.http.org.openroadm.controller.customization.rev230526.controller.parameters.SpectrumFilling;
19 import org.opendaylight.yang.gen.v1.http.org.openroadm.controller.customization.rev230526.controller.parameters.spectrum.filling.SpectrumFillingRules;
20 import org.opendaylight.yang.gen.v1.http.org.openroadm.controller.customization.rev230526.controller.parameters.spectrum.filling.SpectrumFillingRulesKey;
21 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ControllerBehaviourSettings;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /*
27  * Class to handle Operator Constraints associated with Specific Engineering rules
28  * as they are defined in the controller-behaviour-settings container of the service
29  * Data-Store.
30  *
31  */
32
33 public class OperatorConstraints {
34
35     /* Logging. */
36     private static final Logger LOG = LoggerFactory.getLogger(OperatorConstraints.class);
37     private static final String SPECTRUM_LOG_MSG =
38         "Specific Spectrum filling Rules have been defined for {} the spectrum range {} - {}";
39     private NetworkTransactionService networkTransaction;
40
41     public OperatorConstraints(NetworkTransactionService networkTransaction) {
42
43         this.networkTransaction = networkTransaction;
44
45     }
46
47     public BitSet getBitMapConstraint(String customerName) {
48         BitSet referenceBitSet = new BitSet(GridConstant.EFFECTIVE_BITS);
49         referenceBitSet.set(0, GridConstant.EFFECTIVE_BITS, true);
50         InstanceIdentifier<SpectrumFilling> sfIID =
51             InstanceIdentifier
52                 .builder(ControllerBehaviourSettings.class)
53                 .child(SpectrumFilling.class)
54                 .build();
55
56         try {
57             if (networkTransaction.read(LogicalDatastoreType.CONFIGURATION, sfIID).get().isPresent()) {
58                 SpectrumFilling spectrumConstraint = networkTransaction
59                     .read(LogicalDatastoreType.CONFIGURATION, sfIID)
60                     .get().orElseThrow();
61                 if (spectrumConstraint.getSpectrumFillingRules().isEmpty()) {
62                     return referenceBitSet;
63                 }
64                 if (customerName == null) {
65                     for (Map.Entry<SpectrumFillingRulesKey, SpectrumFillingRules> rule:
66                             spectrumConstraint.getSpectrumFillingRules().entrySet()) {
67                         var spectrumRangeOfAppl = rule.getValue().getSpectrumRangeOfAppliance();
68                         var dedicatedCustomer = spectrumRangeOfAppl.getDedicatedCustomer();
69                         if (dedicatedCustomer == null || dedicatedCustomer.isEmpty()) {
70                             continue;
71                         }
72                         // Spectrum portion is dedicated to some customers that do not include this one
73                         FrequencyTHz startFreq = spectrumRangeOfAppl.getStartEdgeFrequency();
74                         FrequencyTHz stopFreq = spectrumRangeOfAppl.getStopEdgeFrequency();
75                         referenceBitSet.set(
76                             GridUtils.getIndexFromFrequency(startFreq.getValue()),
77                             GridUtils.getIndexFromFrequency(stopFreq.getValue()),
78                             false);
79                         LOG.info(SPECTRUM_LOG_MSG,
80                             "other customers, preventing the customer from using", startFreq, stopFreq);
81                     }
82                     return referenceBitSet;
83                 }
84
85                 for (Map.Entry<SpectrumFillingRulesKey, SpectrumFillingRules> rule:
86                         spectrumConstraint.getSpectrumFillingRules().entrySet()) {
87                     var spectrumRangeOfAppl = rule.getValue().getSpectrumRangeOfAppliance();
88                     FrequencyTHz startFreq = spectrumRangeOfAppl.getStartEdgeFrequency();
89                     FrequencyTHz stopFreq = spectrumRangeOfAppl.getStopEdgeFrequency();
90                     var nonAuthorizedCustomer = spectrumRangeOfAppl.getNonAuthorizedCustomer();
91                     if (nonAuthorizedCustomer != null && nonAuthorizedCustomer.contains(customerName)) {
92                         //Customer shall not be put in this spectrum portion
93                         referenceBitSet.set(
94                             GridUtils.getIndexFromFrequency(startFreq.getValue()),
95                             GridUtils.getIndexFromFrequency(stopFreq.getValue()),
96                             false);
97                         LOG.info(SPECTRUM_LOG_MSG,
98                             "customer " + customerName + ", exluding it from", startFreq, stopFreq);
99                         continue;
100                     }
101                     var dedicatedCustomer = spectrumRangeOfAppl.getDedicatedCustomer();
102                     if (dedicatedCustomer == null || dedicatedCustomer.isEmpty()) {
103                         continue;
104                     }
105                     if (dedicatedCustomer.contains(customerName)) {
106                         // Spectrum portion is dedicated to customers including this one
107                         referenceBitSet.set(
108                             GridUtils.getIndexFromFrequency(startFreq.getValue()),
109                             GridUtils.getIndexFromFrequency(stopFreq.getValue()),
110                             true);
111                         LOG.info(SPECTRUM_LOG_MSG,
112                             "customer " + customerName + ", to dedicate", startFreq, stopFreq + " to it");
113                         continue;
114                     }
115                     // Spectrum portion is dedicated to some customers that do not include this one
116                     referenceBitSet.set(
117                         GridUtils.getIndexFromFrequency(startFreq.getValue()),
118                         GridUtils.getIndexFromFrequency(stopFreq.getValue()),
119                         false);
120                     LOG.info(SPECTRUM_LOG_MSG,
121                         "other customers, preventing the customer from using", startFreq, stopFreq);
122                 }
123                 return referenceBitSet;
124             }
125         } catch (InterruptedException | ExecutionException e1) {
126             LOG.error("Exception caught handling Spectrum filling Rules ", e1.getCause());
127         }
128         LOG.info("Did not succeed finding any Specific Spectrum filling Rules defined in Configuration Datastore");
129         return referenceBitSet;
130     }
131 }