Move pcep base parser Activator to its own bundle
[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.rev131005.Object;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.metrics.MetricPce;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.monitoring.metrics.MetricPceBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.object.Overload;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pce.id.object.PceId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.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         case PROC_TIME :
36             if (obj instanceof Overload) {
37                 metricPceBuilder.setOverload((Overload) obj);
38                 return State.OVERLOAD;
39             }
40         case OVERLOAD :
41         case END :
42             return State.END;
43         default:
44             return state;
45         }
46     }
47
48     public static MetricPce validateMonitoringMetrics(final List<Object> objects) throws PCEPDeserializerException {
49         final MetricPceBuilder metricPceBuilder = new MetricPceBuilder();
50         if (!(objects.get(0) instanceof PceId)) {
51             throw new PCEPDeserializerException("metric-pce-list must start with PCE-ID object.");
52         }
53         metricPceBuilder.setPceId((PceId) (objects.get(0)));
54         objects.remove(0);
55         State state = State.START;
56         while (!objects.isEmpty() && !state.equals(State.END)) {
57             final Object obj = objects.get(0);
58             state = insertObject(metricPceBuilder, state, obj);
59             if (!state.equals(State.END)) {
60                 objects.remove(0);
61             }
62         }
63         return metricPceBuilder.build();
64     }
65
66     private enum State {
67         START, PROC_TIME, OVERLOAD, END;
68     }
69 }