700a6e372551004e38aad76c477b6b06bb62fecf
[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.Queue;
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     private Util() {
24         // Hidden on purpose
25     }
26
27     private static State insertObject(final MetricPceBuilder metricPceBuilder, final State state, final Object obj) {
28         switch (state) {
29             case START :
30                 if (obj instanceof ProcTime) {
31                     metricPceBuilder.setProcTime((ProcTime) obj);
32                     return State.PROC_TIME;
33                 }
34                 // fallthrough
35             case PROC_TIME :
36                 if (obj instanceof Overload) {
37                     metricPceBuilder.setOverload((Overload) obj);
38                     return State.OVERLOAD;
39                 }
40                 // fallthrough
41             case OVERLOAD :
42             case END :
43                 return State.END;
44             default:
45                 return state;
46         }
47     }
48
49     public static MetricPce validateMonitoringMetrics(final Queue<Object> objects) throws PCEPDeserializerException {
50         final Object pceId = objects.poll();
51         if (!(pceId instanceof PceId)) {
52             throw new PCEPDeserializerException("metric-pce-list must start with PCE-ID object.");
53         }
54
55         final MetricPceBuilder metricPceBuilder = new MetricPceBuilder().setPceId((PceId) pceId);
56         State state = State.START;
57         for (Object obj = objects.peek(); obj != null; obj = objects.peek()) {
58             state = insertObject(metricPceBuilder, state, obj);
59             if (state == State.END) {
60                 break;
61             }
62
63             objects.remove();
64         }
65
66         return metricPceBuilder.build();
67     }
68
69     private enum State {
70         START, PROC_TIME, OVERLOAD, END
71     }
72 }