Refactoring of cisco-xr-driver and impl modules.
[unimgr.git] / cisco-xr-driver / src / main / java / org / opendaylight / unimgr / mef / nrp / cisco / xr / common / helper / BandwidthProfileComposition.java
1 /*
2  * Copyright (c) 2016 Cisco Systems 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.unimgr.mef.nrp.cisco.xr.common.helper;
9
10 import org.opendaylight.yang.gen.v1.urn.mef.nrp.bandwidth.profile.rev160630.GNRPBwpFlow;
11 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.g_nrp_connadaptspec.EgressBwpFlow;
12 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.g_nrp_connadaptspec.IngressBwpFlow;
13 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.g_nrp_uni_terminationspec.EgressBwpUni;
14 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.g_nrp_uni_terminationspec.IngressBwpUni;
15
16 import java.util.Optional;
17
18 public class BandwidthProfileComposition {
19
20     public enum BwpDirection {
21         INGRESS,
22         EGRESS
23     }
24
25     public enum BwpApplicability {
26         DEFAULT,
27         EVC,
28         UNI
29     }
30
31     public static BandwidthProfileCompositionBuilder builder() {
32         return new BandwidthProfileCompositionBuilder();
33     }
34
35     private static Optional<GNRPBwpFlow> convert(Optional<? extends GNRPBwpFlow> input) {
36         if(input.isPresent()) {
37             return Optional.of(input.get());
38         }
39
40         return Optional.empty();
41     }
42
43     private Optional<IngressBwpFlow> ingressBwProfilePerEvc;
44
45     private Optional<EgressBwpFlow> egressBwProfilePerEvc;
46
47     private Optional<IngressBwpUni> ingressBwProfilePerUni;
48
49     private Optional<EgressBwpUni> egressBwProfilePerUni;
50
51     private Optional<IngressBwpFlow> defaultIngressBwProfile;
52
53     private Optional<EgressBwpFlow> defaultEgressBwProfile;
54
55     private BandwidthProfileComposition(BandwidthProfileCompositionBuilder builder) {
56         this.ingressBwProfilePerEvc = builder.ingressBwProfilePerEvc;
57         this.egressBwProfilePerEvc = builder.egressBwProfilePerEvc;
58         this.ingressBwProfilePerUni = builder.ingressBwProfilePerUni;
59         this.egressBwProfilePerUni = builder.egressBwProfilePerUni;
60         this.defaultIngressBwProfile = builder.defaultIngressBwProfile;
61         this.defaultEgressBwProfile = builder.defaultEgressBwProfile;
62     }
63
64     public Optional<IngressBwpFlow> getIngressBwProfilePerEvc() {
65         return ingressBwProfilePerEvc;
66     }
67
68     public Optional<EgressBwpFlow> getEgressBwProfilePerEvc() {
69         return egressBwProfilePerEvc;
70     }
71
72     public Optional<IngressBwpUni> getIngressBwProfilePerUni() {
73         return ingressBwProfilePerUni;
74     }
75
76     public Optional<EgressBwpUni> getEgressBwProfilePerUni() {
77         return egressBwProfilePerUni;
78     }
79
80     public Optional<IngressBwpFlow> getDefaultIngressBwProfile() {
81         return defaultIngressBwProfile;
82     }
83
84     public Optional<EgressBwpFlow> getDefaultEgressBwProfile() {
85         return defaultEgressBwProfile;
86     }
87
88     public Optional<GNRPBwpFlow> get(BwpDirection direction, BwpApplicability applicability) {
89         switch(direction) {
90             case INGRESS:
91                 switch(applicability) {
92                     case DEFAULT:
93                         return convert(defaultIngressBwProfile);
94                     case EVC:
95                         return convert(ingressBwProfilePerEvc);
96                     case UNI:
97                         return convert(ingressBwProfilePerUni);
98                     default:
99                         return Optional.empty();
100                 }
101             case EGRESS:
102                 switch(applicability) {
103                     case DEFAULT:
104                         return convert(defaultEgressBwProfile);
105                     case EVC:
106                         return convert(egressBwProfilePerEvc);
107                     case UNI:
108                         return convert(egressBwProfilePerUni);
109                     default:
110                         return Optional.empty();
111                 }
112             default:
113                 return Optional.empty();
114         }
115     }
116
117     public boolean hasAnyProfileDefined() {
118         return ingressBwProfilePerEvc.isPresent() ||
119                egressBwProfilePerEvc.isPresent() ||
120                ingressBwProfilePerUni.isPresent() ||
121                egressBwProfilePerUni.isPresent() ||
122                defaultIngressBwProfile.isPresent() ||
123                defaultEgressBwProfile.isPresent();
124     }
125
126     public static class BandwidthProfileCompositionBuilder {
127         private Optional<IngressBwpFlow> ingressBwProfilePerEvc;
128
129         private Optional<EgressBwpFlow> egressBwProfilePerEvc;
130
131         private Optional<IngressBwpUni> ingressBwProfilePerUni;
132
133         private Optional<EgressBwpUni> egressBwProfilePerUni;
134
135         private Optional<IngressBwpFlow> defaultIngressBwProfile;
136
137         private Optional<EgressBwpFlow> defaultEgressBwProfile;
138
139         private BandwidthProfileCompositionBuilder() {
140             ingressBwProfilePerEvc = Optional.empty();
141             egressBwProfilePerEvc = Optional.empty();
142             ingressBwProfilePerUni = Optional.empty();
143             egressBwProfilePerUni = Optional.empty();
144             defaultIngressBwProfile = Optional.empty();
145             defaultEgressBwProfile = Optional.empty();
146         }
147
148         public BandwidthProfileCompositionBuilder ingressBwProfilePerEvc(Optional<IngressBwpFlow> ingressBwProfilePerEvc) {
149             this.ingressBwProfilePerEvc = ingressBwProfilePerEvc;
150             return this;
151         }
152
153         public BandwidthProfileCompositionBuilder egressBwProfilePerEvc(Optional<EgressBwpFlow> egressBwProfilePerEvc) {
154             this.egressBwProfilePerEvc = egressBwProfilePerEvc;
155             return this;
156         }
157
158         public BandwidthProfileCompositionBuilder ingressBwProfilePerUni(Optional<IngressBwpUni> ingressBwProfilePerUni) {
159             this.ingressBwProfilePerUni = ingressBwProfilePerUni;
160             return this;
161         }
162
163         public BandwidthProfileCompositionBuilder egressBwProfilePerUni(Optional<EgressBwpUni> egressBwProfilePerUni) {
164             this.egressBwProfilePerUni = egressBwProfilePerUni;
165             return this;
166         }
167
168         public BandwidthProfileCompositionBuilder defaultIngressBwProfile(Optional<IngressBwpFlow> defaultIngressBwProfile) {
169             this.defaultIngressBwProfile = defaultIngressBwProfile;
170             return this;
171         }
172
173         public BandwidthProfileCompositionBuilder defaultEgressBwProfile(Optional<EgressBwpFlow> defaultEgressBwProfile) {
174             this.defaultEgressBwProfile = defaultEgressBwProfile;
175             return this;
176         }
177
178         public BandwidthProfileComposition build() {
179             return new BandwidthProfileComposition(this);
180         }
181     }
182 }