org.onosproject.north.aaa.api.parser.impl.ScopeParser.java Source code

Java tutorial

Introduction

Here is the source code for org.onosproject.north.aaa.api.parser.impl.ScopeParser.java

Source

/*
 * Copyright 2014-2016 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.north.aaa.api.parser.impl;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.onosproject.north.aaa.api.JsonParserService;
import org.onosproject.north.aaa.api.ParseException;
import org.onosproject.north.aaa.store.Scope;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

/**
 * Parse and build scope object class from scopes.json file.
 */
public class ScopeParser implements JsonParserService<Scope> {
    @Override
    public Set<Scope> parseJson(InputStream stream) throws IOException, ParseException {
        // begin parsing JSON to Application class
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(stream);
        JsonParser jp = jsonNode.traverse();
        Set<Scope> scopeSet = new HashSet<>();

        // continue parsing the token till the end of input is reached
        while (!jp.isClosed()) {
            // get the token
            JsonToken token = jp.nextToken();
            // if its the last token then we are done
            if (token == null) {
                break;
            }
            if (JsonToken.FIELD_NAME.equals(token) && "scopes".equals(jp.getCurrentName())) {
                token = jp.nextToken();
                if (!JsonToken.START_ARRAY.equals(token)) {
                    // bail out
                    throw new ParseException("expected ARRAY after scopes");
                }

                while (true) {
                    token = jp.nextToken();
                    if (JsonToken.END_ARRAY.equals(token)) {
                        // bail out
                        break;
                    }
                    if (JsonToken.START_OBJECT.equals(token)) {
                        Scope scope = jsonToScope(jp);
                        scopeSet.add(scope);
                    }
                }
            }
        }
        return scopeSet;
    }

    public Scope jsonToScope(JsonParser jp) throws IOException, ParseException {
        Scope.Builder builder = Scope.builder();

        while (true) {
            JsonToken token = jp.nextToken();
            if (JsonToken.END_OBJECT.equals(token)) {
                // bail out
                break;
            }

            if (JsonToken.FIELD_NAME.equals(token) && "scopeId".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildScopeId(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "scopeDesc".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildScopeDesc(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "accesses".equals(jp.getCurrentName())) {
                token = jp.nextToken();
                if (!JsonToken.START_ARRAY.equals(token)) {
                    // bail out
                    throw new ParseException("expected ARRAY after accesses");
                }
                Set<String> accesses = new HashSet<>();

                while (true) {
                    token = jp.nextToken();
                    if (JsonToken.END_ARRAY.equals(token)) {
                        // bail out
                        break;
                    }
                    accesses.add(jp.getText());
                }
                builder.buildRestAccess(accesses);
            }
        }
        return builder.buildAll();
    }
}