3c11596486733ea564905dc788c0861ce1a79da4
[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 NetworkTransactionService networkTransaction;
38
39     public OperatorConstraints(NetworkTransactionService networkTransaction) {
40
41         this.networkTransaction = networkTransaction;
42
43     }
44
45     public BitSet getBitMapConstraint(String customerName) {
46         BitSet referenceBitSet = new BitSet(GridConstant.EFFECTIVE_BITS);
47         referenceBitSet.set(0, GridConstant.EFFECTIVE_BITS, true);
48         InstanceIdentifier<SpectrumFilling> sfIID =
49             InstanceIdentifier
50                 .builder(ControllerBehaviourSettings.class)
51                 .child(SpectrumFilling.class)
52                 .build();
53
54         try {
55             if (networkTransaction.read(LogicalDatastoreType.CONFIGURATION, sfIID).get().isPresent()) {
56                 SpectrumFilling spectrumConstraint = networkTransaction
57                     .read(LogicalDatastoreType.CONFIGURATION, sfIID).get().orElseThrow();
58                 if (spectrumConstraint.getSpectrumFillingRules().isEmpty()) {
59                     return referenceBitSet;
60                 }
61                 for (Map.Entry<SpectrumFillingRulesKey, SpectrumFillingRules> rule:
62                         spectrumConstraint.getSpectrumFillingRules().entrySet()) {
63                     FrequencyTHz startFreq = rule.getValue().getSpectrumRangeOfAppliance().getStartEdgeFrequency();
64                     FrequencyTHz stopFreq = rule.getValue().getSpectrumRangeOfAppliance().getStopEdgeFrequency();
65                     if (customerName != null
66                             && rule.getValue().getSpectrumRangeOfAppliance().getNonAuthorizedCustomer() != null
67                             && rule.getValue().getSpectrumRangeOfAppliance().getNonAuthorizedCustomer()
68                                 .contains(customerName)) {
69                         //Customer shall not be put in this spectrum portion
70                         referenceBitSet.set(
71                             GridUtils.getIndexFromFrequency(startFreq.getValue()),
72                             GridUtils.getIndexFromFrequency(stopFreq.getValue()), false);
73                         LOG.info("Specific Spectrum filling Rules have been defined for customer {}, exluding it from "
74                             + "the spectrum range {} - {} ", customerName, startFreq.toString(), stopFreq.toString());
75                     } else if (customerName != null
76                             && rule.getValue().getSpectrumRangeOfAppliance().getDedicatedCustomer() != null
77                             && rule.getValue().getSpectrumRangeOfAppliance()
78                                 .getDedicatedCustomer().contains(customerName)) {
79                         // Spectrum portion is dedicated to customers including this one
80                         referenceBitSet.set(
81                             GridUtils.getIndexFromFrequency(startFreq.getValue()),
82                             GridUtils.getIndexFromFrequency(stopFreq.getValue()), true);
83                         LOG.info("Specific Spectrum filling Rules have been defined for customer {}, to dedicate "
84                             + "spectrum range {} - {} to it ", customerName, startFreq.toString(), stopFreq.toString());
85                     } else if (rule.getValue().getSpectrumRangeOfAppliance().getDedicatedCustomer() != null
86                             && !rule.getValue().getSpectrumRangeOfAppliance().getDedicatedCustomer().isEmpty()) {
87                         // Spectrum portion is dedicated to some customers that do not include this one
88                         referenceBitSet.set(
89                             GridUtils.getIndexFromFrequency(startFreq.getValue()),
90                             GridUtils.getIndexFromFrequency(stopFreq.getValue()), false);
91                         LOG.info("Specific Spectrum filling Rules have been defined for other customers, preventing"
92                             + " the customer to use spectrum range {}--{}", startFreq.toString(), stopFreq.toString());
93                     }
94                 }
95                 return referenceBitSet;
96             }
97         } catch (InterruptedException | ExecutionException e1) {
98             LOG.error("Exception caught handling Spectrum filling Rules {} ", e1.getCause().toString());
99         }
100         LOG.info("Did not succeed finding any Specific Spectrum filling Rules defined in Configuration Datastore");
101         return referenceBitSet;
102     }
103 }