Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / util / Util.java
1 /*
2  * Copyright (c) 2013 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.protocol.pcep.parser.util;
9
10 import java.util.List;
11 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.metrics.MetricPce;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.monitoring.metrics.MetricPceBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.overload.object.Overload;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pce.id.object.PceId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.proc.time.object.ProcTime;
18
19 /**
20  * Utilities used in pcep-base-parser.
21  */
22 public final class Util {
23
24     private Util() {
25         throw new UnsupportedOperationException();
26     }
27
28     private static State insertObject(final MetricPceBuilder metricPceBuilder, final State state, final Object obj) {
29         switch (state) {
30             case START :
31                 if (obj instanceof ProcTime) {
32                     metricPceBuilder.setProcTime((ProcTime) obj);
33                     return State.PROC_TIME;
34                 }
35                 // fallthrough
36             case PROC_TIME :
37                 if (obj instanceof Overload) {
38                     metricPceBuilder.setOverload((Overload) obj);
39                     return State.OVERLOAD;
40                 }
41                 // fallthrough
42             case OVERLOAD :
43             case END :
44                 return State.END;
45             default:
46                 return state;
47         }
48     }
49
50     public static MetricPce validateMonitoringMetrics(final List<Object> objects) throws PCEPDeserializerException {
51         final MetricPceBuilder metricPceBuilder = new MetricPceBuilder();
52         if (!(objects.get(0) instanceof PceId)) {
53             throw new PCEPDeserializerException("metric-pce-list must start with PCE-ID object.");
54         }
55         metricPceBuilder.setPceId((PceId) (objects.get(0)));
56         objects.remove(0);
57         State state = State.START;
58         while (!objects.isEmpty() && !state.equals(State.END)) {
59             final Object obj = objects.get(0);
60             state = insertObject(metricPceBuilder, state, obj);
61             if (!state.equals(State.END)) {
62                 objects.remove(0);
63             }
64         }
65         return metricPceBuilder.build();
66     }
67
68     private enum State {
69         START, PROC_TIME, OVERLOAD, END
70     }
71 }